SDL_SetTextInputRect() has been renamed to SDL_SetTextInputArea()

The new function includes the cursor position so IME UI elements can be placed relative to the cursor, as well as having the whole text area available so on-screen keyboards can avoid it.
This commit is contained in:
Sam Lantinga
2024-06-28 13:17:04 -07:00
parent e324c7d692
commit bdd531986b
35 changed files with 201 additions and 148 deletions

View File

@@ -73,23 +73,21 @@ static void UpdateTextWindowInputRect(SDL_WindowID id)
SDLTest_TextWindow *textwindow = windowstates[i].textwindow;
int w, h;
SDL_Rect rect;
int cursor = 0;
int current = textwindow->current;
const char *current_line = textwindow->lines[current];
SDL_GetWindowSize(state->windows[i], &w, &h);
rect.x = (int)TEXT_WINDOW_OFFSET_X;
if (current_line) {
rect.x += (int)SDL_utf8strlen(current_line) * FONT_CHARACTER_SIZE;
cursor = (int)SDL_utf8strlen(current_line) * FONT_CHARACTER_SIZE;
}
rect.x = (int)TEXT_WINDOW_OFFSET_X;
rect.y = (int)TEXT_WINDOW_OFFSET_Y + current * FONT_LINE_HEIGHT;
#if 1
rect.w = FONT_CHARACTER_SIZE;
#else
rect.w = (int)(w - (2 * TEXT_WINDOW_OFFSET_X));
#endif
rect.h = FONT_LINE_HEIGHT;
SDL_SetTextInputRect(state->windows[i], &rect);
rect.h = FONT_CHARACTER_SIZE;
SDL_SetTextInputArea(state->windows[i], &rect, cursor);
return;
}
}

View File

@@ -361,14 +361,14 @@ static int keyboard_startStopTextInput(void *arg)
return TEST_COMPLETED;
}
/* Internal function to test SDL_SetTextInputRect */
static void testSetTextInputRect(SDL_Window *window, SDL_Rect refRect)
/* Internal function to test SDL_SetTextInputArea */
static void testSetTextInputArea(SDL_Window *window, SDL_Rect refRect)
{
SDL_Rect testRect;
testRect = refRect;
SDL_SetTextInputRect(window, &testRect);
SDLTest_AssertPass("Call to SDL_SetTextInputRect with refRect(x:%d,y:%d,w:%d,h:%d)", refRect.x, refRect.y, refRect.w, refRect.h);
SDL_SetTextInputArea(window, &testRect, 0);
SDLTest_AssertPass("Call to SDL_SetTextInputArea with refRect(x:%d,y:%d,w:%d,h:%d)", refRect.x, refRect.y, refRect.w, refRect.h);
SDLTest_AssertCheck(
(refRect.x == testRect.x) && (refRect.y == testRect.y) && (refRect.w == testRect.w) && (refRect.h == testRect.h),
"Check that input data was not modified, expected: x:%d,y:%d,w:%d,h:%d, got: x:%d,y:%d,w:%d,h:%d",
@@ -377,11 +377,11 @@ static void testSetTextInputRect(SDL_Window *window, SDL_Rect refRect)
}
/**
* Check call to SDL_SetTextInputRect
* Check call to SDL_SetTextInputArea
*
* \sa SDL_SetTextInputRect
* \sa SDL_SetTextInputArea
*/
static int keyboard_setTextInputRect(void *arg)
static int keyboard_setTextInputArea(void *arg)
{
SDL_Window *window = SDL_GetKeyboardFocus();
SDL_Rect refRect;
@@ -391,77 +391,77 @@ static int keyboard_setTextInputRect(void *arg)
refRect.y = SDLTest_RandomIntegerInRange(1, 50);
refRect.w = SDLTest_RandomIntegerInRange(10, 50);
refRect.h = SDLTest_RandomIntegerInRange(10, 50);
testSetTextInputRect(window, refRect);
testSetTextInputArea(window, refRect);
/* Normal visible refRect, origin 0,0 */
refRect.x = 0;
refRect.y = 0;
refRect.w = SDLTest_RandomIntegerInRange(10, 50);
refRect.h = SDLTest_RandomIntegerInRange(10, 50);
testSetTextInputRect(window, refRect);
testSetTextInputArea(window, refRect);
/* 1Pixel refRect */
refRect.x = SDLTest_RandomIntegerInRange(10, 50);
refRect.y = SDLTest_RandomIntegerInRange(10, 50);
refRect.w = 1;
refRect.h = 1;
testSetTextInputRect(window, refRect);
testSetTextInputArea(window, refRect);
/* 0pixel refRect */
refRect.x = 1;
refRect.y = 1;
refRect.w = 1;
refRect.h = 0;
testSetTextInputRect(window, refRect);
testSetTextInputArea(window, refRect);
/* 0pixel refRect */
refRect.x = 1;
refRect.y = 1;
refRect.w = 0;
refRect.h = 1;
testSetTextInputRect(window, refRect);
testSetTextInputArea(window, refRect);
/* 0pixel refRect */
refRect.x = 1;
refRect.y = 1;
refRect.w = 0;
refRect.h = 0;
testSetTextInputRect(window, refRect);
testSetTextInputArea(window, refRect);
/* 0pixel refRect */
refRect.x = 0;
refRect.y = 0;
refRect.w = 0;
refRect.h = 0;
testSetTextInputRect(window, refRect);
testSetTextInputArea(window, refRect);
/* negative refRect */
refRect.x = SDLTest_RandomIntegerInRange(-200, -100);
refRect.y = SDLTest_RandomIntegerInRange(-200, -100);
refRect.w = 50;
refRect.h = 50;
testSetTextInputRect(window, refRect);
testSetTextInputArea(window, refRect);
/* oversized refRect */
refRect.x = SDLTest_RandomIntegerInRange(1, 50);
refRect.y = SDLTest_RandomIntegerInRange(1, 50);
refRect.w = 5000;
refRect.h = 5000;
testSetTextInputRect(window, refRect);
testSetTextInputArea(window, refRect);
/* NULL refRect */
SDL_SetTextInputRect(window, NULL);
SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)");
SDL_SetTextInputArea(window, NULL, 0);
SDLTest_AssertPass("Call to SDL_SetTextInputArea(NULL)");
return TEST_COMPLETED;
}
/**
* Check call to SDL_SetTextInputRect with invalid data
* Check call to SDL_SetTextInputArea with invalid data
*
* \sa SDL_SetTextInputRect
* \sa SDL_SetTextInputArea
*/
static int keyboard_setTextInputRectNegative(void *arg)
static int keyboard_setTextInputAreaNegative(void *arg)
{
/* Some platforms set also an error message; prepare for checking it */
#if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_ANDROID) || defined(SDL_VIDEO_DRIVER_COCOA)
@@ -473,8 +473,8 @@ static int keyboard_setTextInputRectNegative(void *arg)
#endif
/* NULL refRect */
SDL_SetTextInputRect(SDL_GetKeyboardFocus(), NULL);
SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)");
SDL_SetTextInputArea(SDL_GetKeyboardFocus(), NULL, 0);
SDLTest_AssertPass("Call to SDL_SetTextInputArea(NULL)");
/* Some platforms set also an error message; so check it */
#if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_ANDROID) || defined(SDL_VIDEO_DRIVER_COCOA)
@@ -666,11 +666,11 @@ static const SDLTest_TestCaseReference keyboardTest7 = {
};
static const SDLTest_TestCaseReference keyboardTest8 = {
(SDLTest_TestCaseFp)keyboard_setTextInputRect, "keyboard_setTextInputRect", "Check call to SDL_SetTextInputRect", TEST_ENABLED
(SDLTest_TestCaseFp)keyboard_setTextInputArea, "keyboard_setTextInputArea", "Check call to SDL_SetTextInputArea", TEST_ENABLED
};
static const SDLTest_TestCaseReference keyboardTest9 = {
(SDLTest_TestCaseFp)keyboard_setTextInputRectNegative, "keyboard_setTextInputRectNegative", "Check call to SDL_SetTextInputRect with invalid data", TEST_ENABLED
(SDLTest_TestCaseFp)keyboard_setTextInputAreaNegative, "keyboard_setTextInputAreaNegative", "Check call to SDL_SetTextInputArea with invalid data", TEST_ENABLED
};
static const SDLTest_TestCaseReference keyboardTest10 = {

View File

@@ -457,6 +457,8 @@ static Uint32 utf8_decode(char *p, size_t len)
static void InitInput(void)
{
int i;
/* Prepare a rect for text input */
textRect.x = textRect.y = 100.0f;
textRect.w = DEFAULT_WINDOW_WIDTH - 2 * textRect.x;
@@ -466,7 +468,9 @@ static void InitInput(void)
markedRect = textRect;
markedText[0] = 0;
SDL_StartTextInput(state->windows[0]);
for (i = 0; i < state->num_windows; ++i) {
SDL_StartTextInput(state->windows[i]);
}
}
@@ -649,6 +653,18 @@ static void DrawCandidates(int rendererID, SDL_FRect *cursorRect)
}
}
static void UpdateTextInputArea(SDL_Window *window, const SDL_FRect *cursorRect)
{
SDL_Rect rect;
int cursor_offset = (int)(cursorRect->x - textRect.x);
rect.x = (int)textRect.x;
rect.y = (int)textRect.y;
rect.w = (int)textRect.w;
rect.h = (int)textRect.h;
SDL_SetTextInputArea(window, &rect, cursor_offset);
}
static void CleanupVideo(void)
{
SDL_StopTextInput(state->windows[0]);
@@ -827,16 +843,8 @@ static void RedrawWindow(int rendererID)
/* Draw the candidates */
DrawCandidates(rendererID, &cursorRect);
{
SDL_Rect inputrect;
/* The input rect is a square at the cursor insertion point */
inputrect.x = (int)cursorRect.x;
inputrect.y = (int)cursorRect.y;
inputrect.w = (int)cursorRect.h;
inputrect.h = (int)cursorRect.h;
SDL_SetTextInputRect(state->windows[0], &inputrect);
}
/* Update the area used to draw composition UI */
UpdateTextInputArea(state->windows[0], &cursorRect);
}
static void Redraw(void)