annotate src/textfile_gui.cpp @ 19767:2a11204038e3 draft

(svn r24700) -Cleanup: Remove WDF_UNCLICK_BUTTONS and make it the default.
author frosch <frosch@openttd.org>
date Sun, 11 Nov 2012 16:10:43 +0000
parents b64f37b0e118
children cadf6aaa0573
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),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
30 EndContainer(),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
31 NWidget(NWID_HORIZONTAL),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
32 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
33 EndContainer(),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
34 NWidget(NWID_VERTICAL),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
35 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
36 EndContainer(),
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 NWidget(NWID_HORIZONTAL),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
39 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
40 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
41 EndContainer(),
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
42 };
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 /** Window definition 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
45 static const WindowDesc _textfile_desc(
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
46 WDP_CENTER, 630, 460,
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
47 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
48 0,
19078
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
49 _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
50 );
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 TextfileWindow::TextfileWindow(TextfileType file_type) : Window(), file_type(file_type)
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
53 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
54 this->CreateNestedTree(&_textfile_desc);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
55 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
56 this->hscroll = this->GetScrollbar(WID_TF_HSCROLLBAR);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
57 this->FinishInitNested(&_textfile_desc);
19560
b64f37b0e118 (svn r24467) -Codechange [FS#5236]: make textfile window class slightly more general (LordAro)
yexo <yexo@openttd.org>
parents: 19081
diff changeset
58 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
59 }
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 /* virtual */ TextfileWindow::~TextfileWindow()
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
62 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
63 free(this->text);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
64 }
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 /* 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
67 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
68 switch (widget) {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
69 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
70 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
71 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
72
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
73 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
74 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
75 break;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
76 }
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 /* 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
80 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
81 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
82
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
83 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
84 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
85
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
86 DrawPixelInfo new_dpi;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
87 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
88 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
89 _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
90
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
91 int left, right;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
92 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
93 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
94 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
95 } else {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
96 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
97 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
98 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
99 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
100 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
101 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
102 }
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 _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
105 }
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 /* 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
108 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
109 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
110 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
111 }
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 /* 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
114 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
115 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
116 }
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 /* 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
119 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
120 return FS_MONO;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
121 }
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 /* 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
124 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
125 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
126
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
127 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
128 }
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 /* 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
131 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
132 return true;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
133 }
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 /* 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
136 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
137 #ifdef WITH_FREETYPE
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
138 strecpy(settings->mono_font, font_name, lastof(settings->mono_font));
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
139 #endif /* WITH_FREETYPE */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
140 }
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 * 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
144 */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
145 /* 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
146 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
147 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
148
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
149 this->lines.Clear();
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
150
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
151 /* 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
152 size_t filesize;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
153 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
154 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
155
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
156 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
157 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
158 fclose(handle);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
159
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
160 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
161
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
162 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
163
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
164 /* 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
165 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
166 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
167 }
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 /* 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
170 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
171
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
172 /* 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
173 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
174
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
175 /* 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
176 *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
177 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
178 if (*p == '\n') {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
179 *p = '\0';
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
180 *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
181 }
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 CheckForMissingGlyphs(true, this);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
185
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
186 /* Initialize scrollbars */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
187 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
188
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
189 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
190 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
191 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
192 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
193 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
194 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
195 }
19081
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
196
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 * Search a textfile file next to the given content.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
199 * @param type The type of the textfile to search for.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
200 * @param dir The subdirectory to search in.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
201 * @param filename The filename of the content to look for.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
202 * @return The path to the textfile, \c NULL otherwise.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
203 */
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
204 const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
205 {
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
206 static const char * const prefixes[] = {
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
207 "readme",
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
208 "changelog",
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
209 "license",
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
210 };
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
211 assert_compile(lengthof(prefixes) == TFT_END);
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
212
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
213 const char *prefix = prefixes[type];
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
214
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
215 if (filename == NULL) return NULL;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
216
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
217 static char file_path[MAX_PATH];
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
218 strecpy(file_path, filename, lastof(file_path));
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
219
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
220 char *slash = strrchr(file_path, PATHSEPCHAR);
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
221 if (slash == NULL) return NULL;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
222
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
223 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
224 if (FioCheckFileExists(file_path, dir)) return file_path;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
225
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
226 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
227 if (FioCheckFileExists(file_path, dir)) return file_path;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
228
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
229 seprintf(slash + 1, lastof(file_path), "%s.txt", prefix);
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
230 return FioCheckFileExists(file_path, dir) ? file_path : NULL;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
231 }