summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gfx.cpp4
-rw-r--r--src/gfx_func.h1
-rw-r--r--src/lang/english.txt2
-rw-r--r--src/script/api/script_window.hpp1
-rw-r--r--src/textfile_gui.cpp97
-rw-r--r--src/textfile_gui.h6
-rw-r--r--src/widgets/misc_widget.h1
7 files changed, 80 insertions, 32 deletions
diff --git a/src/gfx.cpp b/src/gfx.cpp
index 3fabf0b09..aa81f99f8 100644
--- a/src/gfx.cpp
+++ b/src/gfx.cpp
@@ -535,9 +535,9 @@ int DrawString(int left, int right, int top, StringID str, TextColour colour, St
* @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
index aa18f3f70..709fc2cc9 100644
--- a/src/gfx_func.h
+++ b/src/gfx_func.h
@@ -123,6 +123,7 @@ void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
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
index ad0cac3fe..013d979d5 100644
--- a/src/lang/english.txt
+++ b/src/lang/english.txt
@@ -3941,6 +3941,8 @@ STR_AI_SETTINGS_START_DELAY :Number of days
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
index 391d3a4e4..5335af1b7 100644
--- a/src/script/api/script_window.hpp
+++ b/src/script/api/script_window.hpp
@@ -1558,6 +1558,7 @@ public:
/** 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
index 987fa09cd..05c534c59 100644
--- 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 @@ TextfileWindow::TextfileWindow(TextfileType file_type) : Window(&_textfile_desc)
this->hscroll = this->GetScrollbar(WID_TF_HSCROLLBAR);
this->FinishInitNested();
this->GetWidget<NWidgetCore>(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 @@ TextfileWindow::TextfileWindow(TextfileType file_type) : Window(&_textfile_desc)
free(this->text);
}
+/**
+ * Get the total height of the content displayed in this window, if wrapping is disabled.
+ * @return the height in pixels
+ */
+uint TextfileWindow::GetContentHeight()
+{
+ int max_width = this->GetWidget<NWidgetCore>(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 @@ TextfileWindow::TextfileWindow(TextfileType file_type) : Window(&_textfile_desc)
}
}
+/** 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 @@ TextfileWindow::TextfileWindow(TextfileType file_type) : Window(&_textfile_desc)
{
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 @@ TextfileWindow::TextfileWindow(TextfileType file_type) : Window(&_textfile_desc)
}
/**
- * 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 @@ TextfileWindow::TextfileWindow(TextfileType file_type) : Window(&_textfile_desc)
}
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
index a2d2af8f9..9495fa3f0 100644
--- a/src/textfile_gui.h
+++ b/src/textfile_gui.h
@@ -22,12 +22,10 @@ const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filenam
/** 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<const char *, 64> 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 @@ struct TextfileWindow : public Window, MissingGlyphSearcher {
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 @@ struct TextfileWindow : public Window, MissingGlyphSearcher {
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
index a54e83035..a6dd081a9 100644
--- a/src/widgets/misc_widget.h
+++ b/src/widgets/misc_widget.h
@@ -48,6 +48,7 @@ enum QueryWidgets {
/** 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.