annotate src/textfile_gui.cpp @ 19081:c4cc3952965b draft

(svn r23935) -Codechange: generalise GetTextfile
author rubidium <rubidium@openttd.org>
date Sun, 12 Feb 2012 10:51:43 +0000
parents 8cb1275f2bb3
children b64f37b0e118
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,
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
48 WDF_UNCLICK_BUTTONS,
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);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
58 }
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 /* virtual */ TextfileWindow::~TextfileWindow()
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 free(this->text);
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
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
65 /* 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
66 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
67 switch (widget) {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
68 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
69 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
70 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
71
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
72 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
73 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
74 break;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
75 }
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 /* 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
79 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
80 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
81
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
82 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
83 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
84
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
85 DrawPixelInfo new_dpi;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
86 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
87 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
88 _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
89
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
90 int left, right;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
91 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
92 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
93 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
94 } else {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
95 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
96 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
97 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
98 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
99 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
100 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
101 }
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 _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
104 }
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 /* 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
107 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
108 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
109 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
110 }
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 /* 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
113 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
114 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
115 }
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 /* 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
118 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
119 return FS_MONO;
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
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
122 /* 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
123 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
124 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
125
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
126 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
127 }
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 /* 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
130 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
131 return true;
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
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
134 /* 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
135 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
136 #ifdef WITH_FREETYPE
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
137 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
138 #endif /* WITH_FREETYPE */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
139 }
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 * 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
143 */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
144 /* 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
145 {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
146 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
147
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
148 this->lines.Clear();
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 /* 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
151 size_t filesize;
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
152 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
153 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
154
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
155 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
156 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
157 fclose(handle);
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
158
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
159 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
160
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
161 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
162
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
163 /* 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
164 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
165 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
166 }
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 /* 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
169 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
170
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
171 /* 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
172 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
173
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
174 /* 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
175 *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
176 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
177 if (*p == '\n') {
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
178 *p = '\0';
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
179 *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
180 }
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 CheckForMissingGlyphs(true, this);
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 /* Initialize scrollbars */
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
186 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
187
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
188 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
189 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
190 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
191 }
8cb1275f2bb3 (svn r23932) -Codechange: split the NewGRF text window into its own source files
rubidium <rubidium@openttd.org>
parents:
diff changeset
192 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
193 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
194 }
19081
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
195
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 * Search a textfile file next to the given content.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
198 * @param type The type of the textfile to search for.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
199 * @param dir The subdirectory to search in.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
200 * @param filename The filename of the content to look for.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
201 * @return The path to the textfile, \c NULL otherwise.
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
202 */
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
203 const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
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 static const char * const prefixes[] = {
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
206 "readme",
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
207 "changelog",
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
208 "license",
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
209 };
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
210 assert_compile(lengthof(prefixes) == TFT_END);
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 const char *prefix = prefixes[type];
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 if (filename == NULL) return NULL;
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 static char file_path[MAX_PATH];
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
217 strecpy(file_path, filename, lastof(file_path));
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
218
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
219 char *slash = strrchr(file_path, PATHSEPCHAR);
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
220 if (slash == NULL) return NULL;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
221
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
222 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
223 if (FioCheckFileExists(file_path, dir)) return file_path;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
224
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
225 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
226 if (FioCheckFileExists(file_path, dir)) return file_path;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
227
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
228 seprintf(slash + 1, lastof(file_path), "%s.txt", prefix);
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
229 return FioCheckFileExists(file_path, dir) ? file_path : NULL;
c4cc3952965b (svn r23935) -Codechange: generalise GetTextfile
rubidium <rubidium@openttd.org>
parents: 19078
diff changeset
230 }