4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file textfile_gui.cpp Implementation of textfile window. */
13 #include "fileio_func.h"
14 #include "fontcache.h"
17 #include "string_func.h"
18 #include "textfile_gui.h"
20 #include "widgets/misc_widget.h"
22 #include "table/strings.h"
25 /** Widgets for the textfile window. */
26 static const NWidgetPart _nested_textfile_widgets
[] = {
27 NWidget(NWID_HORIZONTAL
),
28 NWidget(WWT_CLOSEBOX
, COLOUR_MAUVE
),
29 NWidget(WWT_CAPTION
, COLOUR_MAUVE
, WID_TF_CAPTION
), SetDataTip(STR_NULL
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
30 NWidget(WWT_DEFSIZEBOX
, COLOUR_MAUVE
),
32 NWidget(NWID_HORIZONTAL
),
33 NWidget(WWT_PANEL
, COLOUR_MAUVE
, WID_TF_BACKGROUND
), SetMinimalSize(200, 125), SetResize(1, 12), SetScrollbar(WID_TF_VSCROLLBAR
),
35 NWidget(NWID_VERTICAL
),
36 NWidget(NWID_VSCROLLBAR
, COLOUR_MAUVE
, WID_TF_VSCROLLBAR
),
39 NWidget(NWID_HORIZONTAL
),
40 NWidget(NWID_HSCROLLBAR
, COLOUR_MAUVE
, WID_TF_HSCROLLBAR
),
41 NWidget(WWT_RESIZEBOX
, COLOUR_MAUVE
),
45 /** Window definition for the textfile window */
46 static WindowDesc
_textfile_desc(
47 WDP_CENTER
, "textfile", 630, 460,
50 _nested_textfile_widgets
, lengthof(_nested_textfile_widgets
)
53 TextfileWindow::TextfileWindow(TextfileType file_type
) : Window(&_textfile_desc
), file_type(file_type
)
55 this->CreateNestedTree();
56 this->vscroll
= this->GetScrollbar(WID_TF_VSCROLLBAR
);
57 this->hscroll
= this->GetScrollbar(WID_TF_HSCROLLBAR
);
58 this->FinishInitNested();
59 this->GetWidget
<NWidgetCore
>(WID_TF_CAPTION
)->SetDataTip(STR_TEXTFILE_README_CAPTION
+ file_type
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
);
62 /* virtual */ TextfileWindow::~TextfileWindow()
67 /* virtual */ void TextfileWindow::UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
70 case WID_TF_BACKGROUND
:
71 this->line_height
= FONT_HEIGHT_MONO
+ 2;
72 resize
->height
= this->line_height
;
74 size
->height
= 4 * resize
->height
+ TOP_SPACING
+ BOTTOM_SPACING
; // At least 4 lines are visible.
75 size
->width
= max(200u, size
->width
); // At least 200 pixels wide.
80 /* virtual */ void TextfileWindow::DrawWidget(const Rect
&r
, int widget
) const
82 if (widget
!= WID_TF_BACKGROUND
) return;
84 int width
= r
.right
- r
.left
+ 1 - WD_BEVEL_LEFT
- WD_BEVEL_RIGHT
;
85 int height
= r
.bottom
- r
.top
+ 1 - WD_BEVEL_LEFT
- WD_BEVEL_RIGHT
;
87 DrawPixelInfo new_dpi
;
88 if (!FillDrawPixelInfo(&new_dpi
, r
.left
+ WD_BEVEL_LEFT
, r
.top
, width
, height
)) return;
89 DrawPixelInfo
*old_dpi
= _cur_dpi
;
93 if (_current_text_dir
== TD_RTL
) {
94 left
= width
+ WD_BEVEL_RIGHT
- WD_FRAMETEXT_RIGHT
- this->hscroll
->GetCount();
95 right
= width
+ WD_BEVEL_RIGHT
- WD_FRAMETEXT_RIGHT
- 1 + this->hscroll
->GetPosition();
97 left
= WD_FRAMETEXT_LEFT
- WD_BEVEL_LEFT
- this->hscroll
->GetPosition();
98 right
= WD_FRAMETEXT_LEFT
- WD_BEVEL_LEFT
+ this->hscroll
->GetCount() - 1;
100 int top
= TOP_SPACING
;
101 for (uint i
= 0; i
< this->vscroll
->GetCapacity() && i
+ this->vscroll
->GetPosition() < this->lines
.Length(); i
++) {
102 DrawString(left
, right
, top
+ i
* this->line_height
, this->lines
[i
+ this->vscroll
->GetPosition()], TC_WHITE
, SA_LEFT
, false, FS_MONO
);
108 /* virtual */ void TextfileWindow::OnResize()
110 this->vscroll
->SetCapacityFromWidget(this, WID_TF_BACKGROUND
, TOP_SPACING
+ BOTTOM_SPACING
);
111 this->hscroll
->SetCapacityFromWidget(this, WID_TF_BACKGROUND
);
114 /* virtual */ void TextfileWindow::Reset()
116 this->search_iterator
= 0;
119 /* virtual */ FontSize
TextfileWindow::DefaultSize()
124 /* virtual */ const char *TextfileWindow::NextString()
126 if (this->search_iterator
>= this->lines
.Length()) return NULL
;
128 return this->lines
[this->search_iterator
++];
131 /* virtual */ bool TextfileWindow::Monospace()
136 /* virtual */ void TextfileWindow::SetFontNames(FreeTypeSettings
*settings
, const char *font_name
)
139 strecpy(settings
->mono
.font
, font_name
, lastof(settings
->mono
.font
));
140 #endif /* WITH_FREETYPE */
144 * Loads the textfile text from file, and setup #lines, #max_length, and both scrollbars.
146 /* virtual */ void TextfileWindow::LoadTextfile(const char *textfile
, Subdirectory dir
)
148 if (textfile
== NULL
) return;
152 /* Get text from file */
154 FILE *handle
= FioFOpenFile(textfile
, "rb", dir
, &filesize
);
155 if (handle
== NULL
) return;
157 this->text
= ReallocT(this->text
, filesize
+ 1);
158 size_t read
= fread(this->text
, 1, filesize
, handle
);
161 if (read
!= filesize
) return;
163 this->text
[filesize
] = '\0';
165 /* Replace tabs and line feeds with a space since str_validate removes those. */
166 for (char *p
= this->text
; *p
!= '\0'; p
++) {
167 if (*p
== '\t' || *p
== '\r') *p
= ' ';
170 /* Check for the byte-order-mark, and skip it if needed. */
171 char *p
= this->text
+ (strncmp("\xEF\xBB\xBF", this->text
, 3) == 0 ? 3 : 0);
173 /* Make sure the string is a valid UTF-8 sequence. */
174 str_validate(p
, this->text
+ filesize
, SVS_REPLACE_WITH_QUESTION_MARK
| SVS_ALLOW_NEWLINE
);
176 /* Split the string on newlines. */
177 *this->lines
.Append() = p
;
178 for (; *p
!= '\0'; p
++) {
181 *this->lines
.Append() = p
+ 1;
185 CheckForMissingGlyphs(true, this);
187 /* Initialize scrollbars */
188 this->vscroll
->SetCount(this->lines
.Length());
190 this->max_length
= 0;
191 for (uint i
= 0; i
< this->lines
.Length(); i
++) {
192 this->max_length
= max(this->max_length
, GetStringBoundingBox(this->lines
[i
], FS_MONO
).width
);
194 this->hscroll
->SetCount(this->max_length
+ WD_FRAMETEXT_LEFT
+ WD_FRAMETEXT_RIGHT
);
195 this->hscroll
->SetStepSize(10); // Speed up horizontal scrollbar
199 * Search a textfile file next to the given content.
200 * @param type The type of the textfile to search for.
201 * @param dir The subdirectory to search in.
202 * @param filename The filename of the content to look for.
203 * @return The path to the textfile, \c NULL otherwise.
205 const char *GetTextfile(TextfileType type
, Subdirectory dir
, const char *filename
)
207 static const char * const prefixes
[] = {
212 assert_compile(lengthof(prefixes
) == TFT_END
);
214 const char *prefix
= prefixes
[type
];
216 if (filename
== NULL
) return NULL
;
218 static char file_path
[MAX_PATH
];
219 strecpy(file_path
, filename
, lastof(file_path
));
221 char *slash
= strrchr(file_path
, PATHSEPCHAR
);
222 if (slash
== NULL
) return NULL
;
224 seprintf(slash
+ 1, lastof(file_path
), "%s_%s.txt", prefix
, GetCurrentLanguageIsoCode());
225 if (FioCheckFileExists(file_path
, dir
)) return file_path
;
227 seprintf(slash
+ 1, lastof(file_path
), "%s_%.2s.txt", prefix
, GetCurrentLanguageIsoCode());
228 if (FioCheckFileExists(file_path
, dir
)) return file_path
;
230 seprintf(slash
+ 1, lastof(file_path
), "%s.txt", prefix
);
231 return FioCheckFileExists(file_path
, dir
) ? file_path
: NULL
;