Skip to content

Commit 3666805

Browse files
committed
Support preedit candidate
Support the GLFW preedit candidate feature. This feature supports only Win32 currently. We can use this feature by enabling `FLAG_MANAGE_PREEDIT_CANDIDATE` flag on Win32.
1 parent 7bdd014 commit 3666805

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/raylib.h

+11
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ typedef struct FilePathList {
503503
typedef enum {
504504
FLAG_VSYNC_HINT = 0x00000040, // Set to try enabling V-Sync on GPU
505505
FLAG_FULLSCREEN_MODE = 0x00000002, // Set to run program in fullscreen
506+
FLAG_MANAGE_PREEDIT_CANDIDATE = 0x00020000, // Set to manage the drawing of preedit candidates by the application side
506507
FLAG_WINDOW_RESIZABLE = 0x00000004, // Set to allow resizable window
507508
FLAG_WINDOW_UNDECORATED = 0x00000008, // Set to disable window decoration (frame and buttons)
508509
FLAG_WINDOW_HIDDEN = 0x00000080, // Set to hide window
@@ -1092,6 +1093,7 @@ RLAPI unsigned char *DecodeDataBase64(const unsigned char *data, int *outputSize
10921093
//------------------------------------------------------------------------------------
10931094
// Input Handling Functions (Module: core)
10941095
//------------------------------------------------------------------------------------
1096+
10951097
// Callback for preedit text.
10961098
// preeditLength: The length of preedit text
10971099
// preeditString: The preedit text (unicode)
@@ -1101,6 +1103,13 @@ RLAPI unsigned char *DecodeDataBase64(const unsigned char *data, int *outputSize
11011103
// caret: The index of the caret in preeditString
11021104
typedef void (*PreeditCallback)(int preeditLength, int *preeditString, int blockCount, int *blockSizes, int focusedBlock, int caret);
11031105

1106+
// Callback for preedit candidate, which is called only when `FLAG_MANAGE_PREEDIT_CANDIDATE` ConfigFlag is enabled on Win32
1107+
// candidatesCount: The number of all preedit candidates
1108+
// selectedIndex: The index of the currently selected candidate in the all candidates
1109+
// pageStart: The index of the first candidate on the current displaying page
1110+
// pageSize: The number of the candidates on the current displaying page
1111+
typedef void (*PreeditCandidateCallback)(int candidatesCount, int selectedIndex, int pageStart, int pageSize);
1112+
11041113
// Input-related functions: keyboard
11051114
RLAPI bool IsKeyPressed(int key); // Check if a key has been pressed once
11061115
RLAPI bool IsKeyDown(int key); // Check if a key is being pressed
@@ -1115,6 +1124,8 @@ RLAPI void GetPreeditCursorRectangle(int *x, int *y, int *w, int *h); // Get the
11151124
RLAPI bool IsImeOn(void); // Check if IME is ON
11161125
RLAPI void SetImeStatus(bool on); // Set IME status
11171126
RLAPI void ResetPreedit(void); // Reset preedit text
1127+
RLAPI void SetPreeditCandidateCallback(PreeditCandidateCallback callback); // Set a callback for preedit candidates
1128+
RLAPI int *GetPreeditCandidate(int index, int *textCount); // Get the text of the preedie candidate. This can be used only when `FLAG_MANAGE_PREEDIT_CANDIDATE` ConfigFlag is enabled on Win32
11181129

11191130
// Input-related functions: gamepads
11201131
RLAPI bool IsGamepadAvailable(int gamepad); // Check if a gamepad is available

src/rcore.c

+25-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ typedef struct CoreData {
438438
int charPressedQueue[MAX_CHAR_PRESSED_QUEUE]; // Input characters queue (unicode)
439439
int charPressedQueueCount; // Input characters queue count
440440

441-
PreeditCallback preeditCallback; // Preedit callback
441+
PreeditCallback preeditCallback; // Preedit callback
442+
PreeditCandidateCallback preeditCandidateCallback; // Preedit candidate callback
442443

443444
#if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
444445
int defaultMode; // Default keyboard mode
@@ -642,6 +643,7 @@ static void WindowDropCallback(GLFWwindow *window, int count, const char **paths
642643
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); // GLFW3 Keyboard Callback, runs on key pressed
643644
static void CharCallback(GLFWwindow *window, unsigned int key); // GLFW3 Char Key Callback, runs on key pressed (get char value)
644645
static void PreeditCallbackInner(GLFWwindow *window, int preeditLength, unsigned int *preeditString, int blockCount, int *blockSizes, int focusedBlock, int caret); // GLFW3 Preedit Callback
646+
static void PreeditCandidateCallbackInner(GLFWwindow *window, int candidatesCount, int selectedIndex, int pageStart, int pageSize); // GLFW3 Preedit Candidate Callback
645647
static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods); // GLFW3 Mouse Button Callback, runs on mouse button pressed
646648
static void MouseCursorPosCallback(GLFWwindow *window, double x, double y); // GLFW3 Cursor Position Callback, runs on mouse move
647649
static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset); // GLFW3 Srolling Callback, runs on mouse wheel
@@ -3590,6 +3592,18 @@ void ResetPreedit(void)
35903592
glfwResetPreeditText(CORE.Window.handle);
35913593
}
35923594

3595+
// Set a callback for preedit candidate
3596+
void SetPreeditCandidateCallback(PreeditCandidateCallback callback)
3597+
{
3598+
CORE.Input.Keyboard.preeditCandidateCallback = callback;
3599+
}
3600+
3601+
// Get the text of the preedie candidate
3602+
int *GetPreeditCandidate(int index, int *textCount)
3603+
{
3604+
return (int *)glfwGetPreeditCandidate(CORE.Window.handle, index, textCount);
3605+
}
3606+
35933607
// Set a custom key to exit program
35943608
// NOTE: default exitKey is ESCAPE
35953609
void SetExitKey(int key)
@@ -3956,6 +3970,9 @@ static bool InitGraphicsDevice(int width, int height)
39563970
glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, GLFW_FALSE);
39573971
#endif
39583972

3973+
if ((CORE.Window.flags & FLAG_MANAGE_PREEDIT_CANDIDATE) > 0) glfwInitHint(GLFW_MANAGE_PREEDIT_CANDIDATE, GLFW_TRUE); // Manage the drawing of preedit candidates.
3974+
else glfwInitHint(GLFW_MANAGE_PREEDIT_CANDIDATE, GLFW_FALSE); // Leave the drawing of preedit candidates to the IME
3975+
39593976
if (!glfwInit())
39603977
{
39613978
TRACELOG(LOG_WARNING, "GLFW: Failed to initialize GLFW");
@@ -4187,6 +4204,7 @@ static bool InitGraphicsDevice(int width, int height)
41874204
glfwSetKeyCallback(CORE.Window.handle, KeyCallback);
41884205
glfwSetCharCallback(CORE.Window.handle, CharCallback);
41894206
glfwSetPreeditCallback(CORE.Window.handle, PreeditCallbackInner);
4207+
glfwSetPreeditCandidateCallback(CORE.Window.handle, PreeditCandidateCallbackInner);
41904208
glfwSetMouseButtonCallback(CORE.Window.handle, MouseButtonCallback);
41914209
glfwSetCursorPosCallback(CORE.Window.handle, MouseCursorPosCallback); // Track mouse position changes
41924210
glfwSetScrollCallback(CORE.Window.handle, MouseScrollCallback);
@@ -5433,6 +5451,12 @@ static void PreeditCallbackInner(GLFWwindow *window, int preeditLength, unsigned
54335451
CORE.Input.Keyboard.preeditCallback(preeditLength, (int *)preeditString, blockCount, blockSizes, focusedBlock, caret);
54345452
}
54355453

5454+
// GLFW3 Preedit Candidate Callback
5455+
static void PreeditCandidateCallbackInner(GLFWwindow *window, int candidatesCount, int selectedIndex, int pageStart, int pageSize)
5456+
{
5457+
if (!CORE.Input.Keyboard.preeditCandidateCallback) return;
5458+
CORE.Input.Keyboard.preeditCandidateCallback(candidatesCount, selectedIndex, pageStart, pageSize);
5459+
}
54365460

54375461
// GLFW3 Mouse Button Callback, runs on mouse button pressed
54385462
static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)

0 commit comments

Comments
 (0)