annotate src/textfile_gui.cpp @ 20477:e845688ff873 draft

(svn r25437) -Codechange: rework the FreeTypeSettings structure to make it better grouped
author rubidium <rubidium@openttd.org>
date Sun, 23 Jun 2013 15:23:22 +0000
parents 63dba929ba42
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19078
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
1 /* $Id$ */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
2
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
3 /*
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
4 * This file is part of OpenTTD.
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
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.
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
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.
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
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/>.
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
8 */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
9
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
10 /** @file textfile_gui.cpp Implementation of textfile window. */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
11
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
12 #include "stdafx.h"
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
13 #include "fileio_func.h"
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
14 #include "fontcache.h"
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
15 #include "gfx_type.h"
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
16 #include "gfx_func.h"
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
17 #include "string_func.h"
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
18 #include "textfile_gui.h"
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
19
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
20 #include "widgets/misc_widget.h"
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
21
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
22 #include "table/strings.h"
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
23
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
24
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
25 /** Widgets for the textfile window. */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
26 static const NWidgetPart _nested_textfile_widgets[] = {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
27 NWidget(NWID_HORIZONTAL),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
28 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
29 NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_TF_CAPTION), SetDataTip(STR_NULL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
20342
63dba929ba42 (svn r25294) -Feature: Add another button to window title bars to resize the window to its default size.
frosch <frosch@openttd.org>
parents: 20338
diff changeset
30 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
19078
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
31 EndContainer(),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
32 NWidget(NWID_HORIZONTAL),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
33 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_TF_BACKGROUND), SetMinimalSize(200, 125), SetResize(1, 12), SetScrollbar(WID_TF_VSCROLLBAR),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
34 EndContainer(),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
35 NWidget(NWID_VERTICAL),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
36 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_TF_VSCROLLBAR),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
37 EndContainer(),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
38 EndContainer(),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
39 NWidget(NWID_HORIZONTAL),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
40 NWidget(NWID_HSCROLLBAR, COLOUR_MAUVE, WID_TF_HSCROLLBAR),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
41 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
42 EndContainer(),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
43 };
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
44
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
45 /** Window definition for the textfile window */
20335
cadf6aaa0573 (svn r25287) -Codechange: Keep a reference to the WindowDesc in the Window after construction.
frosch <frosch@openttd.org>
parents: 19767
diff changeset
46 static WindowDesc _textfile_desc(
20338
491ddf49f744 (svn r25290) -Add: Assign string names to notable windows.
frosch <frosch@openttd.org>
parents: 20335
diff changeset
47 WDP_CENTER, "textfile", 630, 460,
19078
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
48 WC_TEXTFILE, WC_NONE,
19767
2a11204038e3 (svn r24700) -Cleanup: Remove WDF_UNCLICK_BUTTONS and make it the default.
frosch <frosch@openttd.org>
parents: 19560
diff changeset
49 0,
19078
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
50 _nested_textfile_widgets, lengthof(_nested_textfile_widgets)
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
51 );
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
52
20335
cadf6aaa0573 (svn r25287) -Codechange: Keep a reference to the WindowDesc in the Window after construction.
frosch <frosch@openttd.org>
parents: 19767
diff changeset
53 TextfileWindow::TextfileWindow(TextfileType file_type) : Window(&_textfile_desc), file_type(file_type)
19078
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
54 {
20335
cadf6aaa0573 (svn r25287) -Codechange: Keep a reference to the WindowDesc in the Window after construction.
frosch <frosch@openttd.org>
parents: 19767
diff changeset
55 this->CreateNestedTree();
19078
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
56 this->vscroll = this->GetScrollbar(WID_TF_VSCROLLBAR);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
57 this->hscroll = this->GetScrollbar(WID_TF_HSCROLLBAR);
20335
cadf6aaa0573 (svn r25287) -Codechange: Keep a reference to the WindowDesc in the Window after construction.
frosch <frosch@openttd.org>
parents: 19767
diff changeset
58 this->FinishInitNested();
19560
b64f37b0e118 (svn r24467) -Codechange [FS#5236]: make textfile window class slightly more general (LordAro)
yexo <yexo@openttd.org>
parents: 19081
diff changeset
59 this->GetWidget<NWidgetCore>(WID_TF_CAPTION)->SetDataTip(STR_TEXTFILE_README_CAPTION + file_type, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
19078
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
60 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
61
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
62 /* virtual */ TextfileWindow::~TextfileWindow()
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
63 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
64 free(this->text);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
65 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
66
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
67 /* virtual */ void TextfileWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
68 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
69 switch (widget) {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
70 case WID_TF_BACKGROUND:
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
71 this->line_height = FONT_HEIGHT_MONO + 2;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
72 resize->height = this->line_height;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
73
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
74 size->height = 4 * resize->height + TOP_SPACING + BOTTOM_SPACING; // At least 4 lines are visible.
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
75 size->width = max(200u, size->width); // At least 200 pixels wide.
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
76 break;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
77 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
78 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
79
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
80 /* virtual */ void TextfileWindow::DrawWidget(const Rect &r, int widget) const
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
81 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
82 if (widget != WID_TF_BACKGROUND) return;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
83
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
84 int width = r.right - r.left + 1 - WD_BEVEL_LEFT - WD_BEVEL_RIGHT;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
85 int height = r.bottom - r.top + 1 - WD_BEVEL_LEFT - WD_BEVEL_RIGHT;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
86
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
87 DrawPixelInfo new_dpi;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
88 if (!FillDrawPixelInfo(&new_dpi, r.left + WD_BEVEL_LEFT, r.top, width, height)) return;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
89 DrawPixelInfo *old_dpi = _cur_dpi;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
90 _cur_dpi = &new_dpi;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
91
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
92 int left, right;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
93 if (_current_text_dir == TD_RTL) {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
94 left = width + WD_BEVEL_RIGHT - WD_FRAMETEXT_RIGHT - this->hscroll->GetCount();
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
95 right = width + WD_BEVEL_RIGHT - WD_FRAMETEXT_RIGHT - 1 + this->hscroll->GetPosition();
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
96 } else {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
97 left = WD_FRAMETEXT_LEFT - WD_BEVEL_LEFT - this->hscroll->GetPosition();
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
98 right = WD_FRAMETEXT_LEFT - WD_BEVEL_LEFT + this->hscroll->GetCount() - 1;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
99 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
100 int top = TOP_SPACING;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
101 for (uint i = 0; i < this->vscroll->GetCapacity() && i + this->vscroll->GetPosition() < this->lines.Length(); i++) {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
102 DrawString(left, right, top + i * this->line_height, this->lines[i + this->vscroll->GetPosition()], TC_WHITE, SA_LEFT, false, FS_MONO);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
103 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
104
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
105 _cur_dpi = old_dpi;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
106 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
107
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
108 /* virtual */ void TextfileWindow::OnResize()
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
109 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
110 this->vscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND, TOP_SPACING + BOTTOM_SPACING);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
111 this->hscroll->SetCapacityFromWidget(this, WID_TF_BACKGROUND);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
112 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
113
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
114 /* virtual */ void TextfileWindow::Reset()
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
115 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
116 this->search_iterator = 0;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
117 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
118
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
119 /* virtual */ FontSize TextfileWindow::DefaultSize()
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
120 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
121 return FS_MONO;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
122 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
123
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
124 /* virtual */ const char *TextfileWindow::NextString()
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
125 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
126 if (this->search_iterator >= this->lines.Length()) return NULL;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
127
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
128 return this->lines[this->search_iterator++];
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
129 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
130
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
131 /* virtual */ bool TextfileWindow::Monospace()
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
132 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
133 return true;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
134 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
135
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
136 /* virtual */ void TextfileWindow::SetFontNames(FreeTypeSettings *settings, const char *font_name)
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
137 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
138 #ifdef WITH_FREETYPE
20477
e845688ff873 (svn r25437) -Codechange: rework the FreeTypeSettings structure to make it better grouped
rubidium <rubidium@openttd.org>
parents: 20342
diff changeset
139 strecpy(settings->mono.font, font_name, lastof(settings->mono.font));
19078
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
140 #endif /* WITH_FREETYPE */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
141 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
142
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
143 /**
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
144 * Loads the textfile text from file, and setup #lines, #max_length, and both scrollbars.
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
145 */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
146 /* virtual */ void TextfileWindow::LoadTextfile(const char *textfile, Subdirectory dir)
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
147 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
148 if (textfile == NULL) return;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
149
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
150 this->lines.Clear();
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
151
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
152 /* Get text from file */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
153 size_t filesize;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
154 FILE *handle = FioFOpenFile(textfile, "rb", dir, &filesize);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
155 if (handle == NULL) return;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
156
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
157 this->text = ReallocT(this->text, filesize + 1);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
158 size_t read = fread(this->text, 1, filesize, handle);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
159 fclose(handle);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
160
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
161 if (read != filesize) return;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
162
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
163 this->text[filesize] = '\0';
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
164
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
165 /* Replace tabs and line feeds with a space since str_validate removes those. */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
166 for (char *p = this->text; *p != '\0'; p++) {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
167 if (*p == '\t' || *p == '\r') *p = ' ';
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
168 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
169
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
170 /* Check for the byte-order-mark, and skip it if needed. */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
171 char *p = this->text + (strncmp("\xEF\xBB\xBF", this->text, 3) == 0 ? 3 : 0);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
172
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
173 /* Make sure the string is a valid UTF-8 sequence. */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
174 str_validate(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
175
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
176 /* Split the string on newlines. */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
177 *this->lines.Append() = p;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
178 for (; *p != '\0'; p++) {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
179 if (*p == '\n') {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
180 *p = '\0';
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
181 *this->lines.Append() = p + 1;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
182 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
183 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
184
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
185 CheckForMissingGlyphs(true, this);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
186
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
187 /* Initialize scrollbars */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
188 this->vscroll->SetCount(this->lines.Length());
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
189
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
190 this->max_length = 0;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
191 for (uint i = 0; i < this->lines.Length(); i++) {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
192 this->max_length = max(this->max_length, GetStringBoundingBox(this->lines[i], FS_MONO).width);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
193 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
194 this->hscroll->SetCount(this->max_length + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
195 this->hscroll->SetStepSize(10); // Speed up horizontal scrollbar
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
196 }
19081
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
197
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
198 /**
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
199 * Search a textfile file next to the given content.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
200 * @param type The type of the textfile to search for.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
201 * @param dir The subdirectory to search in.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
202 * @param filename The filename of the content to look for.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
203 * @return The path to the textfile, \c NULL otherwise.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
204 */
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
205 const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
206 {
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
207 static const char * const prefixes[] = {
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
208 "readme",
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
209 "changelog",
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
210 "license",
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
211 };
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
212 assert_compile(lengthof(prefixes) == TFT_END);
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
213
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
214 const char *prefix = prefixes[type];
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
215
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
216 if (filename == NULL) return NULL;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
217
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
218 static char file_path[MAX_PATH];
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
219 strecpy(file_path, filename, lastof(file_path));
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
220
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
221 char *slash = strrchr(file_path, PATHSEPCHAR);
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
222 if (slash == NULL) return NULL;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
223
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
224 seprintf(slash + 1, lastof(file_path), "%s_%s.txt", prefix, GetCurrentLanguageIsoCode());
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
225 if (FioCheckFileExists(file_path, dir)) return file_path;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
226
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
227 seprintf(slash + 1, lastof(file_path), "%s_%.2s.txt", prefix, GetCurrentLanguageIsoCode());
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
228 if (FioCheckFileExists(file_path, dir)) return file_path;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
229
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
230 seprintf(slash + 1, lastof(file_path), "%s.txt", prefix);
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
231 return FioCheckFileExists(file_path, dir) ? file_path : NULL;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
232 }