diff --git a/src/gfx.cpp b/src/gfx.cpp --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -535,9 +535,9 @@ * @param maxw maximum string width * @return height of pixels of string when it is drawn */ -static int GetStringHeight(const char *str, int maxw) +int GetStringHeight(const char *str, int maxw, FontSize fontsize) { - Layouter layout(str, maxw); + Layouter layout(str, maxw, TC_FROMSTRING, fontsize); return layout.GetBounds().height; } diff --git a/src/gfx_func.h b/src/gfx_func.h --- a/src/gfx_func.h +++ b/src/gfx_func.h @@ -123,6 +123,7 @@ Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize = FS_NORMAL); Dimension GetStringBoundingBox(StringID strid); +int GetStringHeight(const char *str, int maxw, FontSize fontsize = FS_NORMAL); int GetStringHeight(StringID str, int maxw); int GetStringLineCount(StringID str, int maxw); Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion); diff --git a/src/lang/english.txt b/src/lang/english.txt --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -3941,6 +3941,8 @@ STR_TEXTFILE_README_CAPTION :{WHITE}{STRING} readme of {RAW_STRING} STR_TEXTFILE_CHANGELOG_CAPTION :{WHITE}{STRING} changelog of {RAW_STRING} STR_TEXTFILE_LICENCE_CAPTION :{WHITE}{STRING} licence of {RAW_STRING} +STR_TEXTFILE_WRAP_TEXT :{WHITE}Wrap text +STR_TEXTFILE_WRAP_TEXT_TOOLTIP :{BLACK}Wrap the text of the window so it all fits without having to scroll STR_TEXTFILE_VIEW_README :{BLACK}View readme STR_TEXTFILE_VIEW_CHANGELOG :{BLACK}Changelog STR_TEXTFILE_VIEW_LICENCE :{BLACK}Licence diff --git a/src/script/api/script_window.hpp b/src/script/api/script_window.hpp --- a/src/script/api/script_window.hpp +++ b/src/script/api/script_window.hpp @@ -1558,6 +1558,7 @@ /** Widgets of the #TextfileWindow class. */ enum TextfileWidgets { WID_TF_CAPTION = ::WID_TF_CAPTION, ///< The caption of the window. + WID_TF_WRAPTEXT = ::WID_TF_WRAPTEXT, ///< Whether or not to wrap the text. WID_TF_BACKGROUND = ::WID_TF_BACKGROUND, ///< Panel to draw the textfile on. WID_TF_VSCROLLBAR = ::WID_TF_VSCROLLBAR, ///< Vertical scrollbar to scroll through the textfile up-and-down. WID_TF_HSCROLLBAR = ::WID_TF_HSCROLLBAR, ///< Horizontal scrollbar to scroll through the textfile left-to-right. diff --git a/src/textfile_gui.cpp b/src/textfile_gui.cpp --- a/src/textfile_gui.cpp +++ b/src/textfile_gui.cpp @@ -21,12 +21,12 @@ #include "table/strings.h" - /** Widgets for the textfile window. */ static const NWidgetPart _nested_textfile_widgets[] = { NWidget(NWID_HORIZONTAL), NWidget(WWT_CLOSEBOX, COLOUR_MAUVE), NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_TF_CAPTION), SetDataTip(STR_NULL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), + NWidget(WWT_TEXTBTN, COLOUR_MAUVE, WID_TF_WRAPTEXT), SetDataTip(STR_TEXTFILE_WRAP_TEXT, STR_TEXTFILE_WRAP_TEXT_TOOLTIP), NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE), EndContainer(), NWidget(NWID_HORIZONTAL), @@ -57,6 +57,9 @@ this->hscroll = this->GetScrollbar(WID_TF_HSCROLLBAR); this->FinishInitNested(); this->GetWidget(WID_TF_CAPTION)->SetDataTip(STR_TEXTFILE_README_CAPTION + file_type, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS); + + this->hscroll->SetStepSize(10); // Speed up horizontal scrollbar + this->vscroll->SetStepSize(FONT_HEIGHT_MONO); } /* virtual */ TextfileWindow::~TextfileWindow() @@ -64,12 +67,27 @@ free(this->text); } +/** + * Get the total height of the content displayed in this window. + * @return the height in pixels + */ +uint TextfileWindow::GetContentHeight() +{ + int max_width = this->GetWidget(WID_TF_BACKGROUND)->current_x - WD_FRAMETEXT_LEFT - WD_FRAMERECT_RIGHT; + + uint height = 0; + for (uint i = 0; i < this->lines.Length(); i++) { + height += GetStringHeight(this->lines[i], max_width, FS_MONO); + } + + return height; +} + /* virtual */ void TextfileWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) { switch (widget) { case WID_TF_BACKGROUND: - this->line_height = FONT_HEIGHT_MONO + 2; - resize->height = this->line_height; + resize->height = 1; size->height = 4 * resize->height + TOP_SPACING + BOTTOM_SPACING; // At least 4 lines are visible. size->width = max(200u, size->width); // At least 200 pixels wide. @@ -77,29 +95,60 @@ } } +/** Set scrollbars to the right lengths. */ +void TextfileWindow::SetupScrollbars() +{ + if (IsWidgetLowered(WID_TF_WRAPTEXT)) { + this->vscroll->SetCount(this->GetContentHeight()); + this->hscroll->SetCount(0); + } else { + uint max_length = 0; + for (uint i = 0; i < this->lines.Length(); i++) { + max_length = max(max_length, GetStringBoundingBox(this->lines[i], FS_MONO).width); + } + this->vscroll->SetCount(this->lines.Length() * FONT_HEIGHT_MONO); + this->hscroll->SetCount(max_length + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT); + } + + this->SetWidgetDisabledState(WID_TF_HSCROLLBAR, IsWidgetLowered(WID_TF_WRAPTEXT)); +} + +/* virtual */ void TextfileWindow::OnClick(Point pt, int widget, int click_count) +{ + switch (widget) { + case WID_TF_WRAPTEXT: + this->ToggleWidgetLoweredState(WID_TF_WRAPTEXT); + this->SetupScrollbars(); + this->InvalidateData(); + break; + } +} + /* virtual */ void TextfileWindow::DrawWidget(const Rect &r, int widget) const { if (widget != WID_TF_BACKGROUND) return; - int width = r.right - r.left + 1 - WD_BEVEL_LEFT - WD_BEVEL_RIGHT; - int height = r.bottom - r.top + 1 - WD_BEVEL_LEFT - WD_BEVEL_RIGHT; + const int x = r.left + WD_FRAMETEXT_LEFT; + const int y = r.top + WD_FRAMETEXT_TOP; + const int right = r.right - WD_FRAMETEXT_RIGHT; + const int bottom = r.bottom - WD_FRAMETEXT_BOTTOM; DrawPixelInfo new_dpi; - if (!FillDrawPixelInfo(&new_dpi, r.left + WD_BEVEL_LEFT, r.top, width, height)) return; + if (!FillDrawPixelInfo(&new_dpi, x, y, right - x + 1, r.bottom - y + 1)) return; DrawPixelInfo *old_dpi = _cur_dpi; _cur_dpi = &new_dpi; - int left, right; - if (_current_text_dir == TD_RTL) { - left = width + WD_BEVEL_RIGHT - WD_FRAMETEXT_RIGHT - this->hscroll->GetCount(); - right = width + WD_BEVEL_RIGHT - WD_FRAMETEXT_RIGHT - 1 + this->hscroll->GetPosition(); - } else { - left = WD_FRAMETEXT_LEFT - WD_BEVEL_LEFT - this->hscroll->GetPosition(); - right = WD_FRAMETEXT_LEFT - WD_BEVEL_LEFT + this->hscroll->GetCount() - 1; - } - int top = TOP_SPACING; - for (uint i = 0; i < this->vscroll->GetCapacity() && i + this->vscroll->GetPosition() < this->lines.Length(); i++) { - DrawString(left, right, top + i * this->line_height, this->lines[i + this->vscroll->GetPosition()], TC_WHITE, SA_LEFT, false, FS_MONO); + /* Draw content (now coordinates given to DrawString* are local to the new clipping region). */ + int line_height = FONT_HEIGHT_MONO; + int y_offset = -this->vscroll->GetPosition(); + + for (uint i = 0; i < this->lines.Length(); i++) { + if (IsWidgetLowered(WID_TF_WRAPTEXT)) { + y_offset = DrawStringMultiLine(0, right - x, y_offset, bottom - y, this->lines[i], TC_WHITE, SA_TOP | SA_LEFT, false, FS_MONO); + } else { + DrawString(-this->hscroll->GetPosition(), right - x, y_offset, this->lines[i], TC_WHITE, SA_TOP | SA_LEFT, false, FS_MONO); + y_offset += line_height; // margin to previous element + } } _cur_dpi = old_dpi; @@ -109,6 +158,8 @@ { this->vscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND, TOP_SPACING + BOTTOM_SPACING); this->hscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND); + + this->SetupScrollbars(); } /* virtual */ void TextfileWindow::Reset() @@ -141,7 +192,7 @@ } /** - * Loads the textfile text from file, and setup #lines, #max_length, and both scrollbars. + * Loads the textfile text from file and setup #lines. */ /* virtual */ void TextfileWindow::LoadTextfile(const char *textfile, Subdirectory dir) { @@ -183,16 +234,6 @@ } CheckForMissingGlyphs(true, this); - - /* Initialize scrollbars */ - this->vscroll->SetCount(this->lines.Length()); - - this->max_length = 0; - for (uint i = 0; i < this->lines.Length(); i++) { - this->max_length = max(this->max_length, GetStringBoundingBox(this->lines[i], FS_MONO).width); - } - this->hscroll->SetCount(this->max_length + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT); - this->hscroll->SetStepSize(10); // Speed up horizontal scrollbar } /** diff --git a/src/textfile_gui.h b/src/textfile_gui.h --- a/src/textfile_gui.h +++ b/src/textfile_gui.h @@ -22,12 +22,10 @@ /** Window for displaying a textfile */ struct TextfileWindow : public Window, MissingGlyphSearcher { TextfileType file_type; ///< Type of textfile to view. - int line_height; ///< Height of a line in the display widget. Scrollbar *vscroll; ///< Vertical scrollbar. Scrollbar *hscroll; ///< Horizontal scrollbar. char *text; ///< Lines of text from the NewGRF's textfile. SmallVector lines; ///< #text, split into lines in a table with lines. - uint max_length; ///< The longest line in the textfile (in pixels). uint search_iterator; ///< Iterator for the font check search. static const int TOP_SPACING = WD_FRAMETEXT_TOP; ///< Additional spacing at the top of the #WID_TF_BACKGROUND widget. @@ -36,6 +34,7 @@ TextfileWindow(TextfileType file_type); virtual ~TextfileWindow(); virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize); + virtual void OnClick(Point pt, int widget, int click_count); virtual void DrawWidget(const Rect &r, int widget) const; virtual void OnResize(); virtual void Reset(); @@ -44,6 +43,9 @@ virtual bool Monospace(); virtual void SetFontNames(FreeTypeSettings *settings, const char *font_name); virtual void LoadTextfile(const char *textfile, Subdirectory dir); +private: + uint GetContentHeight(); + void SetupScrollbars(); }; #endif /* TEXTFILE_GUI_H */ diff --git a/src/widgets/misc_widget.h b/src/widgets/misc_widget.h --- a/src/widgets/misc_widget.h +++ b/src/widgets/misc_widget.h @@ -48,6 +48,7 @@ /** Widgets of the #TextfileWindow class. */ enum TextfileWidgets { WID_TF_CAPTION, ///< The caption of the window. + WID_TF_WRAPTEXT, ///< Whether or not to wrap the text. WID_TF_BACKGROUND, ///< Panel to draw the textfile on. WID_TF_VSCROLLBAR, ///< Vertical scrollbar to scroll through the textfile up-and-down. WID_TF_HSCROLLBAR, ///< Horizontal scrollbar to scroll through the textfile left-to-right.