OpenTTD Source 20250312-master-gcdcc6b491d
help_gui.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10#include "stdafx.h"
11#include "gui.h"
12#include "window_gui.h"
13#include "textfile_gui.h"
14#include "fileio_func.h"
15#include "table/control_codes.h"
16#include "string_func.h"
17#include "openttd.h"
18#include "help_gui.h"
19
20#include "widgets/help_widget.h"
21#include "widgets/misc_widget.h"
22
23#include "safeguards.h"
24
25static const std::string README_FILENAME = "README.md";
26static const std::string CHANGELOG_FILENAME = "changelog.md";
27static const std::string KNOWN_BUGS_FILENAME = "known-bugs.md";
28static const std::string LICENSE_FILENAME = "COPYING.md";
29static const std::string FONTS_FILENAME = "fonts.md";
30
31static const std::string WEBSITE_LINK = "https://www.openttd.org/";
32static const std::string WIKI_LINK = "https://wiki.openttd.org/";
33static const std::string BUGTRACKER_LINK = "https://bugs.openttd.org/";
34static const std::string COMMUNITY_LINK = "https://community.openttd.org/";
35
37static constexpr size_t CHANGELOG_VERSIONS_LIMIT = 20;
38
45static std::optional<std::string> FindGameManualFilePath(std::string_view filename, Subdirectory subdir)
46{
47 static const Searchpath searchpaths[] = {
49 };
50
51 for (Searchpath sp : searchpaths) {
52 auto file_path = FioGetDirectory(sp, subdir) + filename.data();
53 if (FioCheckFileExists(file_path, NO_DIRECTORY)) return file_path;
54 }
55
56 return {};
57}
58
62 {
63 this->ConstructWindow();
64
65 /* Mark the content of these files as trusted. */
66 this->trusted = true;
67
68 auto filepath = FindGameManualFilePath(filename, subdir);
69 /* The user could, in theory, have moved the file. So just show an empty window if that is the case. */
70 if (!filepath.has_value()) {
71 return;
72 }
73
74 this->filepath = filepath.value();
76 this->OnClick({ 0, 0 }, WID_TF_WRAPTEXT, 1);
77 }
78
79 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
80 {
81 if (widget == WID_TF_CAPTION) {
82 return GetString(stringid, this->filename);
83 }
84
85 return this->Window::GetWidgetString(widget, stringid);
86 }
87
88 void AfterLoadText() override
89 {
90 if (this->filename == CHANGELOG_FILENAME) {
91 this->link_anchors.clear();
92 this->AfterLoadChangelog();
93 }
95 }
96
103 {
104 uint versions = 0;
105
106 /* Look for lines beginning with ###, they indicate a release name. */
107 for (size_t line_index = 0; line_index < this->lines.size(); ++line_index) {
108 const Line &line = this->lines[line_index];
109 if (!line.text.starts_with("###")) continue;
110
112 this->lines.resize(line_index - 2);
113 break;
114 }
115
116 ++versions;
117 }
118 }
119};
120
122struct HelpWindow : public Window {
123
124 HelpWindow(WindowDesc &desc, WindowNumber number) : Window(desc)
125 {
126 this->InitNested(number);
127
128 this->EnableTextfileButton(README_FILENAME, BASE_DIR, WID_HW_README);
129 this->EnableTextfileButton(CHANGELOG_FILENAME, BASE_DIR, WID_HW_CHANGELOG);
130 this->EnableTextfileButton(KNOWN_BUGS_FILENAME, BASE_DIR, WID_HW_KNOWN_BUGS);
131 this->EnableTextfileButton(LICENSE_FILENAME, BASE_DIR, WID_HW_LICENSE);
132 this->EnableTextfileButton(FONTS_FILENAME, DOCS_DIR, WID_HW_FONTS);
133 }
134
135 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
136 {
137 switch (widget) {
138 case WID_HW_README:
139 new GameManualTextfileWindow(README_FILENAME, BASE_DIR);
140 break;
141 case WID_HW_CHANGELOG:
142 new GameManualTextfileWindow(CHANGELOG_FILENAME, BASE_DIR);
143 break;
144 case WID_HW_KNOWN_BUGS:
145 new GameManualTextfileWindow(KNOWN_BUGS_FILENAME, BASE_DIR);
146 break;
147 case WID_HW_LICENSE:
148 new GameManualTextfileWindow(LICENSE_FILENAME, BASE_DIR);
149 break;
150 case WID_HW_FONTS:
151 new GameManualTextfileWindow(FONTS_FILENAME, DOCS_DIR);
152 break;
153 case WID_HW_WEBSITE:
154 OpenBrowser(WEBSITE_LINK);
155 break;
156 case WID_HW_WIKI:
157 OpenBrowser(WIKI_LINK);
158 break;
159 case WID_HW_BUGTRACKER:
160 OpenBrowser(BUGTRACKER_LINK);
161 break;
162 case WID_HW_COMMUNITY:
163 OpenBrowser(COMMUNITY_LINK);
164 break;
165 }
166 }
167
168private:
169 void EnableTextfileButton(std::string_view filename, Subdirectory subdir, WidgetID button_widget)
170 {
171 this->GetWidget<NWidgetLeaf>(button_widget)->SetDisabled(!FindGameManualFilePath(filename, subdir).has_value());
172 }
173};
174
175static constexpr NWidgetPart _nested_helpwin_widgets[] = {
177 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
178 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetStringTip(STR_HELP_WINDOW_CAPTION),
179 EndContainer(),
180
181 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
183 NWidget(WWT_FRAME, COLOUR_DARK_GREEN), SetStringTip(STR_HELP_WINDOW_WEBSITES),
184 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_WEBSITE), SetStringTip(STR_HELP_WINDOW_MAIN_WEBSITE), SetMinimalSize(128, 12), SetFill(1, 0),
185 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_WIKI), SetStringTip(STR_HELP_WINDOW_MANUAL_WIKI), SetMinimalSize(128, 12), SetFill(1, 0),
186 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_BUGTRACKER), SetStringTip(STR_HELP_WINDOW_BUGTRACKER), SetMinimalSize(128, 12), SetFill(1, 0),
187 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_COMMUNITY), SetStringTip(STR_HELP_WINDOW_COMMUNITY), SetMinimalSize(128, 12), SetFill(1, 0),
188 EndContainer(),
189
190 NWidget(WWT_FRAME, COLOUR_DARK_GREEN), SetStringTip(STR_HELP_WINDOW_DOCUMENTS),
191 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_README), SetStringTip(STR_HELP_WINDOW_README), SetMinimalSize(128, 12), SetFill(1, 0),
192 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_CHANGELOG), SetStringTip(STR_HELP_WINDOW_CHANGELOG), SetMinimalSize(128, 12), SetFill(1, 0),
193 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_KNOWN_BUGS),SetStringTip(STR_HELP_WINDOW_KNOWN_BUGS), SetMinimalSize(128, 12), SetFill(1, 0),
194 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_LICENSE), SetStringTip(STR_HELP_WINDOW_LICENSE), SetMinimalSize(128, 12), SetFill(1, 0),
195 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_FONTS), SetStringTip(STR_HELP_WINDOW_FONTS), SetMinimalSize(128, 12), SetFill(1, 0),
196 EndContainer(),
197 EndContainer(),
198 EndContainer(),
199};
200
201static WindowDesc _helpwin_desc(
202 WDP_CENTER, nullptr, 0, 0,
203 WC_HELPWIN, WC_NONE,
204 {},
205 _nested_helpwin_widgets
206);
207
208void ShowHelpWindow()
209{
210 AllocateWindowDescFront<HelpWindow>(_helpwin_desc, 0);
211}
static const WidgetDimensions unscaled
Unscaled widget dimensions.
Definition window_gui.h:94
Control codes that are embedded in the translation strings.
bool FioCheckFileExists(const std::string &filename, Subdirectory subdir)
Check whether the given file exists.
Definition fileio.cpp:122
Functions for Standard In/Out file operations.
Searchpath
Types of searchpaths OpenTTD might use.
@ SP_SHARED_DIR
Search in the shared directory, like 'Shared Files' under Windows.
@ SP_INSTALLATION_DIR
Search in the installation directory.
@ SP_BINARY_DIR
Search in the directory where the binary resides.
@ SP_WORKING_DIR
Search in the working directory.
@ SP_APPLICATION_BUNDLE_DIR
Search within the application bundle.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
@ NO_DIRECTORY
A path without any base directory.
@ BASE_DIR
Base directory for all subdirectories.
@ DOCS_DIR
Subdirectory for documentation.
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
constexpr NWidgetPart SetPIP(uint8_t pre, uint8_t inter, uint8_t post)
Widget part function for setting a pre/inter/post spaces.
constexpr NWidgetPart SetPadding(uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
Widget part function for setting additional space around a widget.
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
GUI functions that shouldn't be here.
static constexpr size_t CHANGELOG_VERSIONS_LIMIT
Only show the first 20 changelog versions in the textfile viewer.
Definition help_gui.cpp:37
static std::optional< std::string > FindGameManualFilePath(std::string_view filename, Subdirectory subdir)
Find the path to the game manual file.
Definition help_gui.cpp:45
GUI to access manuals and related.
Types related to the help window widgets.
Types related to the misc widgets.
@ WID_TF_WRAPTEXT
Whether or not to wrap the text.
Definition misc_widget.h:53
@ WID_TF_CAPTION
The caption of the window.
Definition misc_widget.h:50
Some generic types.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:426
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Window class displaying the game manual textfile viewer.
Definition help_gui.cpp:60
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
Definition help_gui.cpp:79
void AfterLoadChangelog()
For changelog files, truncate the file after CHANGELOG_VERSIONS_LIMIT versions.
Definition help_gui.cpp:102
void AfterLoadText() override
Post-processing after the text is loaded.
Definition help_gui.cpp:88
Window class displaying the help window.
Definition help_gui.cpp:122
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition help_gui.cpp:135
Partial widget specification to allow NWidgets to be written nested.
Coordinates of a point in 2D.
std::string text
Contents of the line.
Window for displaying a textfile.
bool trusted
Whether the content is trusted (read: not from content like NewGRFs, etc).
std::vector< Line > lines
#text, split into lines in a table with lines.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
virtual void AfterLoadText()
Post-processing after the text is loaded.
std::string filepath
Full path to the filename.
std::vector< Hyperlink > link_anchors
Anchor names of headings that can be linked to.
std::string filename
Filename of the textfile.
virtual void LoadTextfile(const std::string &textfile, Subdirectory dir)
Loads the textfile text from file and setup lines.
High level window description.
Definition window_gui.h:168
Number to differentiate different windows of the same class.
Data structure for an opened window.
Definition window_gui.h:274
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:502
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:973
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1749
GUI functions related to textfiles.
@ TFT_GAME_MANUAL
Game manual/documentation file.
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:65
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:40
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:51
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:59
@ WWT_FRAME
Frame.
Definition widget_type.h:50
Functions, definitions and such used only by the GUI.
@ WDP_CENTER
Center the window.
Definition window_gui.h:146
int WidgetID
Widget ID.
Definition window_type.h:20
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:47