From 23eec0b7b32ad1b39d927e8a2c662ade1f376e5a Mon Sep 17 00:00:00 2001 From: PeterN Date: Sun, 25 Dec 2022 00:40:55 +0000 Subject: [PATCH] Fix #8971: Resize QueryStrings with interface scale change. (#10281) * Fix: Use width of caret symbol '_' for text entry. This replaces an arbitrary pixel width with the space actually required. * Fix #8971: Update QueryString sizes with interface scale change. --- src/misc_gui.cpp | 13 +++++++++---- src/window.cpp | 16 +++++++++++++++- src/window_gui.h | 1 + 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index 44ccd9eac4..2641f7227e 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -789,6 +789,11 @@ void QueryString::HandleEditBox(Window *w, int wid) } } +static int GetCaretWidth() +{ + return GetCharacterWidth(FS_NORMAL, '_'); +} + void QueryString::DrawEditBox(const Window *w, int wid) const { const NWidgetLeaf *wi = w->GetWidget(wid); @@ -821,7 +826,7 @@ void QueryString::DrawEditBox(const Window *w, int wid) const /* We will take the current widget length as maximum width, with a small * space reserved at the end for the caret to show */ const Textbuf *tb = &this->text; - int delta = std::min(0, (fr.right - fr.left) - tb->pixels - 10); + int delta = std::min(0, (fr.right - fr.left) - tb->pixels - GetCaretWidth()); if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs; @@ -858,7 +863,7 @@ Point QueryString::GetCaretPosition(const Window *w, int wid) const /* Clamp caret position to be inside out current width. */ const Textbuf *tb = &this->text; - int delta = std::min(0, (r.right - r.left) - tb->pixels - 10); + int delta = std::min(0, (r.right - r.left) - tb->pixels - GetCaretWidth()); if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs; Point pt = {r.left + tb->caretxoffs + delta, r.top}; @@ -887,7 +892,7 @@ Rect QueryString::GetBoundingRect(const Window *w, int wid, const char *from, co /* Clamp caret position to be inside our current width. */ const Textbuf *tb = &this->text; - int delta = std::min(0, r.Width() - tb->pixels - 10); + int delta = std::min(0, r.Width() - tb->pixels - GetCaretWidth()); if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs; /* Get location of first and last character. */ @@ -920,7 +925,7 @@ const char *QueryString::GetCharAtPosition(const Window *w, int wid, const Point /* Clamp caret position to be inside our current width. */ const Textbuf *tb = &this->text; - int delta = std::min(0, r.Width() - tb->pixels - 10); + int delta = std::min(0, r.Width() - tb->pixels - GetCaretWidth()); if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs; return ::GetCharAtPosition(tb->buf, pt.x - delta - r.left); diff --git a/src/window.cpp b/src/window.cpp index 2ae61648ee..81952d27db 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -354,6 +354,17 @@ QueryString *Window::GetQueryString(uint widnum) return query != this->querystrings.End() ? query->second : nullptr; } +/** + * Update size of all QueryStrings of this window. + */ +void Window::UpdateQueryStringSize() +{ + for (auto &qs : this->querystrings) + { + qs.second->text.UpdateSize(); + } +} + /** * Get the current input text if an edit box has the focus. * @return The currently focused input text or nullptr if no input focused. @@ -3353,7 +3364,10 @@ void HideVitalWindows() void ReInitWindow(Window *w, bool zoom_changed) { if (w == nullptr) return; - if (zoom_changed) w->nested_root->AdjustPaddingForZoom(); + if (zoom_changed) { + w->nested_root->AdjustPaddingForZoom(); + w->UpdateQueryStringSize(); + } w->ReInit(); } diff --git a/src/window_gui.h b/src/window_gui.h index 8bd5dc39ee..00dfd9960a 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -276,6 +276,7 @@ public: const QueryString *GetQueryString(uint widnum) const; QueryString *GetQueryString(uint widnum); + void UpdateQueryStringSize(); virtual const char *GetFocusedText() const; virtual const char *GetCaret() const;