From bf8757817392d99049472989af0cc7f5cf9c3e7f Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 30 May 2021 07:38:37 +0100 Subject: [PATCH] Fix #264: Crash before calling SDL_SetTextInputRect with no window focused --- src/video/sdl2_v.cpp | 3 +++ src/window.cpp | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/video/sdl2_v.cpp b/src/video/sdl2_v.cpp index bd928b0807..50746b8655 100644 --- a/src/video/sdl2_v.cpp +++ b/src/video/sdl2_v.cpp @@ -50,6 +50,7 @@ static std::string _editing_text; static void SetTextInputRect(); +bool IsWindowFocused(); Point GetFocusedWindowCaret(); Point GetFocusedWindowTopLeft(); bool FocusedWindowIsConsole(); @@ -432,6 +433,8 @@ bool VideoDriver_SDL_Base::ClaimMousePointer() static void SetTextInputRect() { + if (!IsWindowFocused()) return; + SDL_Rect winrect; Point caret = GetFocusedWindowCaret(); Point win = GetFocusedWindowTopLeft(); diff --git a/src/window.cpp b/src/window.cpp index cd359492b1..7fd15c75f2 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -452,6 +452,11 @@ void SetFocusedWindow(Window *w) if (_focused_window != nullptr) _focused_window->OnFocus(old_focused); } +bool IsWindowFocused() +{ + return _focused_window != nullptr; +} + Point GetFocusedWindowCaret() { return _focused_window->GetCaretPosition(); @@ -519,7 +524,7 @@ bool Window::SetFocusedWidget(int widget_index) if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus(); } this->nested_focus = this->GetWidget(widget_index); - if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxGainedFocus(); + if (_focused_window == this && this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxGainedFocus(); return true; }