mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-05-13 11:48:05 +08:00
Added SDL_EVENT_TEXT_EDITING_CANDIDATES
This allows applications that have set SDL_HINT_IME_SHOW_UI to "0" to get candidates lists they can draw themselves. Fixes https://github.com/libsdl-org/SDL/issues/4855
This commit is contained in:
@@ -163,6 +163,7 @@ typedef enum SDL_EventType
|
||||
input language or keyboard layout change. */
|
||||
SDL_EVENT_KEYBOARD_ADDED, /**< A new keyboard has been inserted into the system */
|
||||
SDL_EVENT_KEYBOARD_REMOVED, /**< A keyboard has been removed */
|
||||
SDL_EVENT_TEXT_EDITING_CANDIDATES, /**< Keyboard text editing candidates */
|
||||
|
||||
/* Mouse events */
|
||||
SDL_EVENT_MOUSE_MOTION = 0x400, /**< Mouse moved */
|
||||
@@ -344,15 +345,34 @@ typedef struct SDL_KeyboardEvent
|
||||
*/
|
||||
typedef struct SDL_TextEditingEvent
|
||||
{
|
||||
SDL_EventType type; /**< SDL_EVENT_TEXT_EDITING */
|
||||
SDL_EventType type; /**< SDL_EVENT_TEXT_EDITING */
|
||||
Uint32 reserved;
|
||||
Uint64 timestamp; /**< In nanoseconds, populated using SDL_GetTicksNS() */
|
||||
SDL_WindowID windowID; /**< The window with keyboard focus, if any */
|
||||
const char *text; /**< The editing text */
|
||||
Sint32 start; /**< The start cursor of selected editing text, or -1 if not set */
|
||||
Sint32 length; /**< The length of selected editing text, or -1 if not set */
|
||||
Uint64 timestamp; /**< In nanoseconds, populated using SDL_GetTicksNS() */
|
||||
SDL_WindowID windowID; /**< The window with keyboard focus, if any */
|
||||
const char *text; /**< The editing text */
|
||||
Sint32 start; /**< The start cursor of selected editing text, or -1 if not set */
|
||||
Sint32 length; /**< The length of selected editing text, or -1 if not set */
|
||||
} SDL_TextEditingEvent;
|
||||
|
||||
/**
|
||||
* Keyboard IME candidates event structure (event.edit_candidates.*)
|
||||
*
|
||||
* The candidates follow the SDL_GetStringRule.
|
||||
*
|
||||
* \since This struct is available since SDL 3.0.0.
|
||||
*/
|
||||
typedef struct SDL_TextEditingCandidatesEvent
|
||||
{
|
||||
SDL_EventType type; /**< SDL_EVENT_TEXT_EDITING_CANDIDATES */
|
||||
Uint32 reserved;
|
||||
Uint64 timestamp; /**< In nanoseconds, populated using SDL_GetTicksNS() */
|
||||
SDL_WindowID windowID; /**< The window with keyboard focus, if any */
|
||||
const char * const *candidates; /**< The list of candidates, or NULL if there are no candidates available */
|
||||
Sint32 num_candidates; /**< The number of strings in `candidates` */
|
||||
Sint32 selected_candidate; /**< The index of the selected candidate, or -1 if no candidate is selected */
|
||||
SDL_bool horizontal; /**< SDL_TRUE if the list is horizontal, SDL_FALSE if it's vertical */
|
||||
} SDL_TextEditingCandidatesEvent;
|
||||
|
||||
/**
|
||||
* Keyboard text input event structure (event.text.*)
|
||||
*
|
||||
@@ -846,6 +866,7 @@ typedef union SDL_Event
|
||||
SDL_KeyboardDeviceEvent kdevice; /**< Keyboard device change event data */
|
||||
SDL_KeyboardEvent key; /**< Keyboard event data */
|
||||
SDL_TextEditingEvent edit; /**< Text editing event data */
|
||||
SDL_TextEditingCandidatesEvent edit_candidates; /**< Text editing candidates event data */
|
||||
SDL_TextInputEvent text; /**< Text input event data */
|
||||
SDL_MouseDeviceEvent mdevice; /**< Mouse device change event data */
|
||||
SDL_MouseMotionEvent motion; /**< Mouse motion event data */
|
||||
|
||||
@@ -360,6 +360,12 @@ static void SDL_LogEvent(const SDL_Event *event)
|
||||
event->edit.text, (int)event->edit.start, (int)event->edit.length);
|
||||
break;
|
||||
|
||||
SDL_EVENT_CASE(SDL_EVENT_TEXT_EDITING_CANDIDATES)
|
||||
(void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u num_candidates=%d selected_candidate=%d)",
|
||||
(uint)event->edit_candidates.timestamp, (uint)event->edit_candidates.windowID,
|
||||
(int)event->edit_candidates.num_candidates, (int)event->edit_candidates.selected_candidate);
|
||||
break;
|
||||
|
||||
SDL_EVENT_CASE(SDL_EVENT_TEXT_INPUT)
|
||||
(void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u text='%s')", (uint)event->text.timestamp, (uint)event->text.windowID, event->text.text);
|
||||
break;
|
||||
|
||||
@@ -659,6 +659,47 @@ int SDL_SendEditingText(const char *text, int start, int length)
|
||||
return posted;
|
||||
}
|
||||
|
||||
int SDL_SendEditingTextCandidates(char **candidates, int num_candidates, int selected_candidate, SDL_bool horizontal)
|
||||
{
|
||||
SDL_Keyboard *keyboard = &SDL_keyboard;
|
||||
int posted;
|
||||
|
||||
if (!SDL_TextInputActive(keyboard->focus)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Post the event, if desired */
|
||||
posted = 0;
|
||||
if (SDL_EventEnabled(SDL_EVENT_TEXT_EDITING_CANDIDATES)) {
|
||||
SDL_Event event;
|
||||
|
||||
event.type = SDL_EVENT_TEXT_EDITING_CANDIDATES;
|
||||
event.common.timestamp = 0;
|
||||
event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0;
|
||||
if (num_candidates > 0) {
|
||||
const char **event_candidates = (const char **)SDL_AllocateEventMemory((num_candidates + 1) * sizeof(*event_candidates));
|
||||
if (!event_candidates) {
|
||||
return 0;
|
||||
}
|
||||
for (int i = 0; i < num_candidates; ++i) {
|
||||
event_candidates[i] = SDL_AllocateEventString(candidates[i]);
|
||||
}
|
||||
event_candidates[num_candidates] = NULL;
|
||||
event.edit_candidates.candidates = event_candidates;
|
||||
event.edit_candidates.num_candidates = num_candidates;
|
||||
event.edit_candidates.selected_candidate = selected_candidate;
|
||||
event.edit_candidates.horizontal = horizontal;
|
||||
} else {
|
||||
event.edit_candidates.candidates = NULL;
|
||||
event.edit_candidates.num_candidates = 0;
|
||||
event.edit_candidates.selected_candidate = -1;
|
||||
event.edit_candidates.horizontal = SDL_FALSE;
|
||||
}
|
||||
posted = (SDL_PushEvent(&event) > 0);
|
||||
}
|
||||
return posted;
|
||||
}
|
||||
|
||||
void SDL_QuitKeyboard(void)
|
||||
{
|
||||
for (int i = SDL_keyboard_count; i--;) {
|
||||
|
||||
@@ -78,6 +78,9 @@ extern int SDL_SendKeyboardText(const char *text);
|
||||
/* Send editing text for selected range from start to end */
|
||||
extern int SDL_SendEditingText(const char *text, int start, int length);
|
||||
|
||||
/* Send editing text candidates, which will be copied into the event */
|
||||
int SDL_SendEditingTextCandidates(char **candidates, int num_candidates, int selected_candidate, SDL_bool horizontal);
|
||||
|
||||
/* Shutdown the keyboard subsystem */
|
||||
extern void SDL_QuitKeyboard(void);
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -440,20 +440,14 @@ struct SDL_VideoData
|
||||
int ime_selected_length;
|
||||
|
||||
SDL_bool ime_candidates_open;
|
||||
SDL_bool ime_candlist;
|
||||
WCHAR *ime_candidates;
|
||||
DWORD ime_candcount;
|
||||
char *ime_candidates[MAX_CANDLIST];
|
||||
int ime_candcount;
|
||||
DWORD ime_candref;
|
||||
DWORD ime_candsel;
|
||||
UINT ime_candpgsize;
|
||||
int ime_candlistindexbase;
|
||||
SDL_bool ime_candvertical;
|
||||
SDL_bool ime_horizontal_candidates;
|
||||
|
||||
SDL_bool ime_dirty;
|
||||
SDL_Rect ime_rect;
|
||||
SDL_Rect ime_candlistrect;
|
||||
int ime_winwidth;
|
||||
int ime_winheight;
|
||||
|
||||
HKL ime_hkl;
|
||||
void *ime_himm32;
|
||||
@@ -474,7 +468,6 @@ struct SDL_VideoData
|
||||
DWORD ime_convmodesinkcookie;
|
||||
TSFSink *ime_uielemsink;
|
||||
TSFSink *ime_ippasink;
|
||||
LONG ime_uicontext;
|
||||
#endif /* !SDL_DISABLE_WINDOWS_IME */
|
||||
|
||||
BYTE pre_hook_key_state[256];
|
||||
|
||||
+214
-4
@@ -53,6 +53,10 @@ static int cursor_length = 0;
|
||||
static SDL_bool cursor_visible;
|
||||
static Uint64 last_cursor_change;
|
||||
static SDL_BlendMode highlight_mode;
|
||||
static char **candidates;
|
||||
static int num_candidates;
|
||||
static int selected_candidate;
|
||||
static SDL_bool horizontal_candidates;
|
||||
#ifdef HAVE_SDL_TTF
|
||||
static TTF_Font *font;
|
||||
#else
|
||||
@@ -319,7 +323,16 @@ static int unifont_load_texture(Uint32 textureID)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static Sint32 unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_FRect *dst)
|
||||
static int unifont_glyph_width(Uint32 codepoint)
|
||||
{
|
||||
if (codepoint > UNIFONT_MAX_CODEPOINT ||
|
||||
unifontGlyph[codepoint].width == 0) {
|
||||
codepoint = UNIFONT_REPLACEMENT;
|
||||
}
|
||||
return unifontGlyph[codepoint].width;
|
||||
}
|
||||
|
||||
static int unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_FRect *dst)
|
||||
{
|
||||
SDL_Texture *texture;
|
||||
Uint32 textureID;
|
||||
@@ -456,9 +469,190 @@ static void InitInput(void)
|
||||
SDL_StartTextInput(state->windows[0]);
|
||||
}
|
||||
|
||||
|
||||
static void ClearCandidates(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < num_candidates; ++i) {
|
||||
SDL_free(candidates[i]);
|
||||
}
|
||||
SDL_free(candidates);
|
||||
candidates = NULL;
|
||||
num_candidates = 0;
|
||||
}
|
||||
|
||||
static void SaveCandidates(SDL_Event *event)
|
||||
{
|
||||
int i;
|
||||
|
||||
ClearCandidates();
|
||||
|
||||
num_candidates = event->edit_candidates.num_candidates;
|
||||
if (num_candidates > 0) {
|
||||
candidates = (char **)SDL_malloc(num_candidates * sizeof(*candidates));
|
||||
if (!candidates) {
|
||||
num_candidates = 0;
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < num_candidates; ++i) {
|
||||
candidates[i] = SDL_strdup(event->edit_candidates.candidates[i]);
|
||||
}
|
||||
selected_candidate = event->edit_candidates.selected_candidate;
|
||||
horizontal_candidates = event->edit_candidates.horizontal;
|
||||
}
|
||||
}
|
||||
|
||||
static void DrawCandidates(int rendererID, SDL_FRect *cursorRect)
|
||||
{
|
||||
SDL_Renderer *renderer = state->renderers[rendererID];
|
||||
int i;
|
||||
int output_w = 0, output_h = 0;
|
||||
float w = 0.0f, h = 0.0f;
|
||||
SDL_FRect candidatesRect, dstRect, underlineRect;
|
||||
|
||||
if (num_candidates == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Calculate the size of the candidate list */
|
||||
for (i = 0; i < num_candidates; ++i) {
|
||||
if (!candidates[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
#ifdef HAVE_SDL_TTF
|
||||
/* FIXME */
|
||||
#else
|
||||
if (horizontal_candidates) {
|
||||
char *utext = candidates[i];
|
||||
Uint32 codepoint;
|
||||
size_t len;
|
||||
float advance = 0.0f;
|
||||
|
||||
if (i > 0) {
|
||||
advance += unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE;
|
||||
}
|
||||
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
|
||||
advance += unifont_glyph_width(codepoint) * UNIFONT_DRAW_SCALE;
|
||||
utext += len;
|
||||
}
|
||||
w += advance;
|
||||
h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
||||
} else {
|
||||
char *utext = candidates[i];
|
||||
Uint32 codepoint;
|
||||
size_t len;
|
||||
float advance = 0.0f;
|
||||
|
||||
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
|
||||
advance += unifont_glyph_width(codepoint) * UNIFONT_DRAW_SCALE;
|
||||
utext += len;
|
||||
}
|
||||
w = SDL_max(w, advance);
|
||||
if (i > 0) {
|
||||
h += 2.0f;
|
||||
}
|
||||
h += UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Position the candidate window */
|
||||
SDL_GetCurrentRenderOutputSize(renderer, &output_w, &output_h);
|
||||
candidatesRect.x = cursorRect->x;
|
||||
candidatesRect.y = cursorRect->y + cursorRect->h + 2.0f;
|
||||
candidatesRect.w = 1.0f + 2.0f + w + 2.0f + 1.0f;
|
||||
candidatesRect.h = 1.0f + 2.0f + h + 2.0f + 1.0f;
|
||||
if ((candidatesRect.x + candidatesRect.w) > output_w) {
|
||||
candidatesRect.x = (output_w - candidatesRect.w);
|
||||
if (candidatesRect.x < 0.0f) {
|
||||
candidatesRect.x = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
/* Draw the candidate background */
|
||||
SDL_SetRenderDrawColor(renderer, 0xAA, 0xAA, 0xAA, 0xFF);
|
||||
SDL_RenderFillRect(renderer, &candidatesRect);
|
||||
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
|
||||
SDL_RenderRect(renderer, &candidatesRect);
|
||||
|
||||
/* Draw the candidates */
|
||||
dstRect.x = candidatesRect.x + 3.0f;
|
||||
dstRect.y = candidatesRect.y + 3.0f;
|
||||
for (i = 0; i < num_candidates; ++i) {
|
||||
if (!candidates[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
#ifdef HAVE_SDL_TTF
|
||||
/* FIXME */
|
||||
#else
|
||||
dstRect.w = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
||||
dstRect.h = UNIFONT_GLYPH_SIZE * UNIFONT_DRAW_SCALE;
|
||||
|
||||
if (horizontal_candidates) {
|
||||
char *utext = candidates[i];
|
||||
Uint32 codepoint;
|
||||
size_t len;
|
||||
float start;
|
||||
|
||||
if (i > 0) {
|
||||
dstRect.x += unifont_draw_glyph(' ', rendererID, &dstRect) * UNIFONT_DRAW_SCALE;
|
||||
}
|
||||
|
||||
start = dstRect.x + 2 * unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE;
|
||||
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
|
||||
dstRect.x += unifont_draw_glyph(codepoint, rendererID, &dstRect) * UNIFONT_DRAW_SCALE;
|
||||
utext += len;
|
||||
}
|
||||
|
||||
if (i == selected_candidate) {
|
||||
underlineRect.x = start;
|
||||
underlineRect.y = dstRect.y + dstRect.h - 2;
|
||||
underlineRect.h = 2;
|
||||
underlineRect.w = dstRect.x - start;
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
|
||||
SDL_RenderFillRect(renderer, &underlineRect);
|
||||
}
|
||||
} else {
|
||||
char *utext = candidates[i];
|
||||
Uint32 codepoint;
|
||||
size_t len;
|
||||
float start;
|
||||
|
||||
dstRect.x = candidatesRect.x + 3.0f;
|
||||
|
||||
start = dstRect.x + 2 * unifont_glyph_width(' ') * UNIFONT_DRAW_SCALE;
|
||||
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))) != 0) {
|
||||
dstRect.x += unifont_draw_glyph(codepoint, rendererID, &dstRect) * UNIFONT_DRAW_SCALE;
|
||||
utext += len;
|
||||
}
|
||||
|
||||
if (i == selected_candidate) {
|
||||
underlineRect.x = start;
|
||||
underlineRect.y = dstRect.y + dstRect.h - 2;
|
||||
underlineRect.h = 2;
|
||||
underlineRect.w = dstRect.x - start;
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
|
||||
SDL_RenderFillRect(renderer, &underlineRect);
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
dstRect.y += 2.0f;
|
||||
}
|
||||
dstRect.y += dstRect.h;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
static void CleanupVideo(void)
|
||||
{
|
||||
SDL_StopTextInput(state->windows[0]);
|
||||
ClearCandidates();
|
||||
#ifdef HAVE_SDL_TTF
|
||||
TTF_CloseFont(font);
|
||||
TTF_Quit();
|
||||
@@ -630,6 +824,9 @@ static void RedrawWindow(int rendererID)
|
||||
SDL_RenderFillRect(renderer, &cursorRect);
|
||||
}
|
||||
|
||||
/* Draw the candidates */
|
||||
DrawCandidates(rendererID, &cursorRect);
|
||||
|
||||
{
|
||||
SDL_Rect inputrect;
|
||||
|
||||
@@ -681,13 +878,16 @@ int main(int argc, char *argv[])
|
||||
|
||||
consumed = SDLTest_CommonArg(state, i);
|
||||
if (SDL_strcmp(argv[i], "--font") == 0) {
|
||||
if (*argv[i+1]) {
|
||||
fontname = argv[i+1];
|
||||
if (*argv[i + 1]) {
|
||||
fontname = argv[i + 1];
|
||||
consumed = 2;
|
||||
}
|
||||
} else if (SDL_strcmp(argv[i], "--disable-ui") == 0) {
|
||||
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "0");
|
||||
consumed = 1;
|
||||
}
|
||||
if (consumed <= 0) {
|
||||
static const char *options[] = { "[--font fontfile]", NULL };
|
||||
static const char *options[] = { "[--font fontfile] [--disable-ui]", NULL };
|
||||
SDLTest_CommonLogUsage(state, argv[0], options);
|
||||
return 1;
|
||||
}
|
||||
@@ -814,6 +1014,16 @@ int main(int argc, char *argv[])
|
||||
cursor_length = event.edit.length;
|
||||
break;
|
||||
|
||||
case SDL_EVENT_TEXT_EDITING_CANDIDATES:
|
||||
SDL_Log("text candidates:\n");
|
||||
for (i = 0; i < event.edit_candidates.num_candidates; ++i) {
|
||||
SDL_Log("%c%s\n", i == event.edit_candidates.selected_candidate ? '>' : ' ', event.edit_candidates.candidates[i]);
|
||||
}
|
||||
|
||||
ClearCandidates();
|
||||
SaveCandidates(&event);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user