Add non-animated SSSE3 blitter
[openttd/fttd.git] / src / textfile_gui.cpp
blob95f2b0d77338c63e433c0297f4798d85d8a6251e
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file textfile_gui.cpp Implementation of textfile window. */
12 #include "stdafx.h"
13 #include "fileio_func.h"
14 #include "fontcache.h"
15 #include "gfx_type.h"
16 #include "gfx_func.h"
17 #include "string_func.h"
18 #include "textfile_gui.h"
20 #include "widgets/misc_widget.h"
22 #include "table/strings.h"
24 /** Widgets for the textfile window. */
25 static const NWidgetPart _nested_textfile_widgets[] = {
26 NWidget(NWID_HORIZONTAL),
27 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
28 NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_TF_CAPTION), SetDataTip(STR_NULL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
29 NWidget(WWT_TEXTBTN, COLOUR_MAUVE, WID_TF_WRAPTEXT), SetDataTip(STR_TEXTFILE_WRAP_TEXT, STR_TEXTFILE_WRAP_TEXT_TOOLTIP),
30 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
31 EndContainer(),
32 NWidget(NWID_HORIZONTAL),
33 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_TF_BACKGROUND), SetMinimalSize(200, 125), SetResize(1, 12), SetScrollbar(WID_TF_VSCROLLBAR),
34 EndContainer(),
35 NWidget(NWID_VERTICAL),
36 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_TF_VSCROLLBAR),
37 EndContainer(),
38 EndContainer(),
39 NWidget(NWID_HORIZONTAL),
40 NWidget(NWID_HSCROLLBAR, COLOUR_MAUVE, WID_TF_HSCROLLBAR),
41 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
42 EndContainer(),
45 /** Window definition for the textfile window */
46 static WindowDesc _textfile_desc(
47 WDP_CENTER, "textfile", 630, 460,
48 WC_TEXTFILE, WC_NONE,
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);
61 this->hscroll->SetStepSize(10); // Speed up horizontal scrollbar
62 this->vscroll->SetStepSize(FONT_HEIGHT_MONO);
65 /* virtual */ TextfileWindow::~TextfileWindow()
67 free(this->text);
70 /**
71 * Get the total height of the content displayed in this window, if wrapping is disabled.
72 * @return the height in pixels
74 uint TextfileWindow::GetContentHeight()
76 int max_width = this->GetWidget<NWidgetCore>(WID_TF_BACKGROUND)->current_x - WD_FRAMETEXT_LEFT - WD_FRAMERECT_RIGHT;
78 uint height = 0;
79 for (uint i = 0; i < this->lines.Length(); i++) {
80 height += GetStringHeight(this->lines[i], max_width, FS_MONO);
83 return height;
86 /* virtual */ void TextfileWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
88 switch (widget) {
89 case WID_TF_BACKGROUND:
90 resize->height = 1;
92 size->height = 4 * resize->height + TOP_SPACING + BOTTOM_SPACING; // At least 4 lines are visible.
93 size->width = max(200u, size->width); // At least 200 pixels wide.
94 break;
98 /** Set scrollbars to the right lengths. */
99 void TextfileWindow::SetupScrollbars()
101 if (IsWidgetLowered(WID_TF_WRAPTEXT)) {
102 this->vscroll->SetCount(this->GetContentHeight());
103 this->hscroll->SetCount(0);
104 } else {
105 uint max_length = 0;
106 for (uint i = 0; i < this->lines.Length(); i++) {
107 max_length = max(max_length, GetStringBoundingBox(this->lines[i], FS_MONO).width);
109 this->vscroll->SetCount(this->lines.Length() * FONT_HEIGHT_MONO);
110 this->hscroll->SetCount(max_length + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
113 this->SetWidgetDisabledState(WID_TF_HSCROLLBAR, IsWidgetLowered(WID_TF_WRAPTEXT));
116 /* virtual */ void TextfileWindow::OnClick(Point pt, int widget, int click_count)
118 switch (widget) {
119 case WID_TF_WRAPTEXT:
120 this->ToggleWidgetLoweredState(WID_TF_WRAPTEXT);
121 this->SetupScrollbars();
122 this->InvalidateData();
123 break;
127 /* virtual */ void TextfileWindow::DrawWidget(const Rect &r, int widget) const
129 if (widget != WID_TF_BACKGROUND) return;
131 const int x = r.left + WD_FRAMETEXT_LEFT;
132 const int y = r.top + WD_FRAMETEXT_TOP;
133 const int right = r.right - WD_FRAMETEXT_RIGHT;
134 const int bottom = r.bottom - WD_FRAMETEXT_BOTTOM;
136 DrawPixelInfo new_dpi;
137 if (!FillDrawPixelInfo(&new_dpi, x, y, right - x + 1, bottom - y + 1)) return;
138 DrawPixelInfo *old_dpi = _cur_dpi;
139 _cur_dpi = &new_dpi;
141 /* Draw content (now coordinates given to DrawString* are local to the new clipping region). */
142 int line_height = FONT_HEIGHT_MONO;
143 int y_offset = -this->vscroll->GetPosition();
145 for (uint i = 0; i < this->lines.Length(); i++) {
146 if (IsWidgetLowered(WID_TF_WRAPTEXT)) {
147 y_offset = DrawStringMultiLine(0, right - x, y_offset, bottom - y, this->lines[i], TC_WHITE, SA_TOP | SA_LEFT, false, FS_MONO);
148 } else {
149 DrawString(-this->hscroll->GetPosition(), right - x, y_offset, this->lines[i], TC_WHITE, SA_TOP | SA_LEFT, false, FS_MONO);
150 y_offset += line_height; // margin to previous element
154 _cur_dpi = old_dpi;
157 /* virtual */ void TextfileWindow::OnResize()
159 this->vscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND, TOP_SPACING + BOTTOM_SPACING);
160 this->hscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND);
162 this->SetupScrollbars();
165 /* virtual */ void TextfileWindow::Reset()
167 this->search_iterator = 0;
170 /* virtual */ FontSize TextfileWindow::DefaultSize()
172 return FS_MONO;
175 /* virtual */ const char *TextfileWindow::NextString()
177 if (this->search_iterator >= this->lines.Length()) return NULL;
179 return this->lines[this->search_iterator++];
182 /* virtual */ bool TextfileWindow::Monospace()
184 return true;
187 /* virtual */ void TextfileWindow::SetFontNames(FreeTypeSettings *settings, const char *font_name)
189 #ifdef WITH_FREETYPE
190 strecpy(settings->mono.font, font_name, lastof(settings->mono.font));
191 #endif /* WITH_FREETYPE */
195 * Loads the textfile text from file and setup #lines.
197 /* virtual */ void TextfileWindow::LoadTextfile(const char *textfile, Subdirectory dir)
199 if (textfile == NULL) return;
201 this->lines.Clear();
203 /* Get text from file */
204 size_t filesize;
205 FILE *handle = FioFOpenFile(textfile, "rb", dir, &filesize);
206 if (handle == NULL) return;
208 this->text = ReallocT(this->text, filesize + 1);
209 size_t read = fread(this->text, 1, filesize, handle);
210 fclose(handle);
212 if (read != filesize) return;
214 this->text[filesize] = '\0';
216 /* Replace tabs and line feeds with a space since str_validate removes those. */
217 for (char *p = this->text; *p != '\0'; p++) {
218 if (*p == '\t' || *p == '\r') *p = ' ';
221 /* Check for the byte-order-mark, and skip it if needed. */
222 char *p = this->text + (strncmp("\xEF\xBB\xBF", this->text, 3) == 0 ? 3 : 0);
224 /* Make sure the string is a valid UTF-8 sequence. */
225 str_validate(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
227 /* Split the string on newlines. */
228 *this->lines.Append() = p;
229 for (; *p != '\0'; p++) {
230 if (*p == '\n') {
231 *p = '\0';
232 *this->lines.Append() = p + 1;
236 CheckForMissingGlyphs(true, this);
240 * Search a textfile file next to the given content.
241 * @param type The type of the textfile to search for.
242 * @param dir The subdirectory to search in.
243 * @param filename The filename of the content to look for.
244 * @return The path to the textfile, \c NULL otherwise.
246 const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
248 static const char * const prefixes[] = {
249 "readme",
250 "changelog",
251 "license",
253 assert_compile(lengthof(prefixes) == TFT_END);
255 const char *prefix = prefixes[type];
257 if (filename == NULL) return NULL;
259 static char file_path[MAX_PATH];
260 strecpy(file_path, filename, lastof(file_path));
262 char *slash = strrchr(file_path, PATHSEPCHAR);
263 if (slash == NULL) return NULL;
265 seprintf(slash + 1, lastof(file_path), "%s_%s.txt", prefix, GetCurrentLanguageIsoCode());
266 if (FioCheckFileExists(file_path, dir)) return file_path;
268 seprintf(slash + 1, lastof(file_path), "%s_%.2s.txt", prefix, GetCurrentLanguageIsoCode());
269 if (FioCheckFileExists(file_path, dir)) return file_path;
271 seprintf(slash + 1, lastof(file_path), "%s.txt", prefix);
272 return FioCheckFileExists(file_path, dir) ? file_path : NULL;