diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 0b0693e8e4..5eeaa10403 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1712,8 +1712,7 @@ public: AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi); - ir.left -= this->hscroll->GetPosition(); - ir.right += this->hscroll->GetCapacity() - this->hscroll->GetPosition(); + ir = ScrollRect(ir, *this->hscroll, 1); if (this->industries.empty()) { DrawString(ir, STR_INDUSTRY_DIRECTORY_NONE); diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp index 7663d0aca1..9e9868b94b 100644 --- a/src/script/script_gui.cpp +++ b/src/script/script_gui.cpp @@ -910,7 +910,7 @@ struct ScriptDebugWindow : public Window { AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi); - fr.left -= this->hscroll->GetPosition(); + fr = ScrollRect(fr, *this->hscroll, 1); auto [first, last] = this->vscroll->GetVisibleRangeIterators(log); for (auto it = first; it != last; ++it) { diff --git a/src/textfile_gui.cpp b/src/textfile_gui.cpp index 8606e29dd5..eec8220940 100644 --- a/src/textfile_gui.cpp +++ b/src/textfile_gui.cpp @@ -156,7 +156,7 @@ void TextfileWindow::SetupScrollbars(bool force_reflow) } else { uint height = force_reflow ? this->ReflowContent() : this->GetContentHeight(); this->vscroll->SetCount(ClampTo(height)); - this->hscroll->SetCount(this->max_length + WidgetDimensions::scaled.frametext.Horizontal()); + this->hscroll->SetCount(this->max_length); } this->SetWidgetDisabledState(WID_TF_HSCROLLBAR, IsWidgetLowered(WID_TF_WRAPTEXT)); @@ -568,6 +568,9 @@ void TextfileWindow::AfterLoadMarkdown() /* Draw content (now coordinates given to DrawString* are local to the new clipping region). */ fr = fr.Translate(-fr.left, -fr.top); int line_height = GetCharacterHeight(FS_MONO); + + if (!IsWidgetLowered(WID_TF_WRAPTEXT)) fr = ScrollRect(fr, *this->hscroll, 1); + int pos = this->vscroll->GetPosition(); int cap = this->vscroll->GetCapacity(); @@ -577,9 +580,9 @@ void TextfileWindow::AfterLoadMarkdown() int y_offset = (line.top - pos) * line_height; if (IsWidgetLowered(WID_TF_WRAPTEXT)) { - DrawStringMultiLine(0, fr.right, y_offset, fr.bottom, line.text, line.colour, SA_TOP | SA_LEFT, false, FS_MONO); + DrawStringMultiLine(fr.left, fr.right, y_offset, fr.bottom, line.text, line.colour, SA_TOP | SA_LEFT, false, FS_MONO); } else { - DrawString(-this->hscroll->GetPosition(), fr.right, y_offset, line.text, line.colour, SA_TOP | SA_LEFT, false, FS_MONO); + DrawString(fr.left, fr.right, y_offset, line.text, line.colour, SA_TOP | SA_LEFT, false, FS_MONO); } } } diff --git a/src/widget.cpp b/src/widget.cpp index 33ff168212..6308f3ebc5 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -2401,6 +2401,35 @@ void Scrollbar::SetCapacityFromWidget(Window *w, WidgetID widget, int padding) } } +/** + * Apply 'scroll' to a rect to be drawn in. + * @param r Rect to be 'scrolled'. + * @param sb The scrollbar affecting the scroll. + * @param resize_step Resize step of the widget/scrollbar (1 if the scrollbar is pixel-based.) + * @returns Scrolled rect. + */ +Rect ScrollRect(Rect r, const Scrollbar &sb, int resize_step) +{ + const int count = sb.GetCount() * resize_step; + const int position = sb.GetPosition() * resize_step; + + if (sb.IsVertical()) { + r.top -= position; + r.bottom = r.top + count; + } else { + bool rtl = _current_text_dir == TD_RTL; + if (rtl) { + r.right += position; + r.left = r.right - count; + } else { + r.left -= position; + r.right = r.left + count; + } + } + + return r; +} + /** * Scrollbar widget. * @param tp Scrollbar type. (horizontal/vertical) diff --git a/src/window_gui.h b/src/window_gui.h index 1c5b7d8d5c..2c31324e66 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -1051,5 +1051,6 @@ extern SpecialMouseMode _special_mouse_mode; void SetFocusedWindow(Window *w); void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y); +Rect ScrollRect(Rect r, const Scrollbar &sb, int resize_step = 1); #endif /* WINDOW_GUI_H */