Skip to content

Commit

Permalink
editbox: Set the system text input rect
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Oct 9, 2023
1 parent 6806e00 commit f8a584f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/framework/wzapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,14 @@ WzString wzGetSelection();
unsigned int wzGetCurrentKey();
void wzDelay(unsigned int delay); //delay in ms
// unicode text support
void StartTextInput(void* pTextInputRequester);
struct WzTextInputRect
{
int x;
int y;
int width;
int height;
};
void StartTextInput(void* pTextInputRequester, const WzTextInputRect& textInputRect);
void StopTextInput(void* pTextInputResigner);
bool isInTextInputMode();
bool wzSeemsLikeNonTouchPlatform();
Expand Down
16 changes: 15 additions & 1 deletion lib/sdl/main_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,16 @@ bool get_scrap(char **dst)
}
}

void StartTextInput(void* pTextInputRequester)
void StartTextInput(void* pTextInputRequester, const WzTextInputRect& textInputRect)
{
if (!GetTextEventsOwner)
{
SDL_Rect rect;
rect.x = textInputRect.x;
rect.y = textInputRect.y;
rect.w = textInputRect.width;
rect.h = textInputRect.height;
SDL_SetTextInputRect(&rect);
SDL_StartTextInput(); // enable text events
debug(LOG_INPUT, "SDL text events started");
}
Expand All @@ -280,6 +286,14 @@ void StartTextInput(void* pTextInputRequester)
debug(LOG_INPUT, "StartTextInput called by new input requester before old requester called StopTextInput");
}
GetTextEventsOwner = pTextInputRequester;

// on some platforms, it seems like we also need to call SDL_SetTextInputRect every frame to properly set the text input rect (?)
SDL_Rect rect;
rect.x = textInputRect.x;
rect.y = textInputRect.y;
rect.w = textInputRect.width;
rect.h = textInputRect.height;
SDL_SetTextInputRect(&rect);
}

void StopTextInput(void* pTextInputResigner)
Expand Down
2 changes: 1 addition & 1 deletion lib/widget/editbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ void W_EDITBOX::run(W_CONTEXT *psContext)
return;
}
dirty = true;
StartTextInput(this);
StartTextInput(this, {screenPosX(), screenPosY(), width(), height()});
/* If there is a mouse click outside of the edit box - stop editing */
int mx = psContext->mx;
int my = psContext->my;
Expand Down

0 comments on commit f8a584f

Please sign in to comment.