OpenTTD Source: src/textfile_gui.h Source File

OpenTTD Source 14.0-RC3

textfile_gui.h
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 #ifndef TEXTFILE_GUI_H
11 #define TEXTFILE_GUI_H
12
13 #include "fileio_type.h"
14 #include "strings_func.h"
15 #include "textfile_type.h"
16 #include "window_gui.h"
17
18 std::optional<std::string> GetTextfile(TextfileType type, Subdirectory dir, const std::string &filename);
19
21 struct TextfileWindow : public Window, MissingGlyphSearcher {
22 TextfileType file_type;
23 Scrollbar *vscroll;
24 Scrollbar *hscroll;
25
26 void UpdateWidgetSize(WidgetID widget, Dimension *size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension *fill, [[maybe_unused]] Dimension *resize) override;
27 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override;
28 void DrawWidget(const Rect &r, WidgetID widget) const override;
29 void OnResize() override;
30 void OnInvalidateData(int data = 0, bool gui_scope = true) override;
31 void OnDropdownSelect(WidgetID widget, int index) override;
32
33 void Reset() override;
34 FontSize DefaultSize() override;
35 std::optional<std::string_view> NextString() override;
36 bool Monospace() override;
37 void SetFontNames(FontCacheSettings *settings, const char *font_name, const void *os_data) override;
38 void ScrollToLine(size_t line);
39
40 virtual void LoadTextfile(const std::string &textfile, Subdirectory dir);
41
42 protected:
43 TextfileWindow(TextfileType file_type);
44 void ConstructWindow();
45
46 struct Line {
47 int top{0};
48 int bottom{0};
49 std::string text{};
50 TextColour colour{TC_WHITE};
51
52 Line(int top, std::string_view text) : top(top), bottom(top + 1), text(text) {}
53 Line() {}
54 };
55
56 struct Hyperlink {
57 size_t line;
58 size_t begin;
59 size_t end;
60 std::string destination;
61 };
62
63 struct HistoryEntry {
64 std::string filepath;
65 int scrollpos;
66 };
67
68 std::string filename{};
69 std::string filepath{};
70
71 std::vector<Line> lines;
72 std::vector<size_t> jumplist;
73 std::vector<Hyperlink> links;
74 std::vector<Hyperlink> link_anchors;
75 std::vector<HistoryEntry> history;
76 size_t history_pos{0};
77 bool trusted{false};
78
79 void LoadText(std::string_view buf);
80 void FindHyperlinksInMarkdown(Line &line, size_t line_index);
81
87 virtual void OnHyperlinkClick(const Hyperlink &link);
88
92 virtual void AfterLoadText();
93
94 void NavigateToFile(std::string newfile, size_t line);
95 void AppendHistory(const std::string &filepath);
96 void UpdateHistoryScrollpos();
97 void NavigateHistory(int delta);
98
99 private:
100 uint search_iterator{0};
101 uint max_length{0};
102
103 uint ReflowContent();
104 uint GetContentHeight();
105 void SetupScrollbars(bool force_reflow);
106 void CheckHyperlinkClick(Point pt);
107
108 void AfterLoadMarkdown();
109 };
110
111 #endif /* TEXTFILE_GUI_H */
TextfileWindow::DefaultSize
FontSize DefaultSize() override
Get the default (font) size of the string.
Definition: textfile_gui.cpp:626
TextfileWindow::OnHyperlinkClick
virtual void OnHyperlinkClick(const Hyperlink &link)
Handle the clicking on a hyperlink.
Definition: textfile_gui.cpp:400
TextfileWindow::history
std::vector< HistoryEntry > history
Browsing history in this window.
Definition: textfile_gui.h:75
TextfileWindow::AfterLoadMarkdown
void AfterLoadMarkdown()
Post-processing of markdown files.
Definition: textfile_gui.cpp:508
MissingGlyphSearcher
A searcher for missing glyphs.
Definition: strings_func.h:115
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
TextfileWindow::Line::bottom
int bottom
Bottom scroll position in visual lines.
Definition: textfile_gui.h:48
TextfileWindow::ReflowContent
uint ReflowContent()
Get the total height of the content displayed in this window, if wrapping is disabled.
Definition: textfile_gui.cpp:108
TextfileWindow::OnResize
void OnResize() override
Called after the window got resized.
Definition: textfile_gui.cpp:586
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:253
TextfileWindow::Line::top
int top
Top scroll position in visual lines.
Definition: textfile_gui.h:47
TextfileWindow::SetFontNames
void SetFontNames(FontCacheSettings *settings, const char *font_name, const void *os_data) override
Set the right font names.
Definition: textfile_gui.cpp:643
Scrollbar
Scrollbar data structure.
Definition: widget_type.h:680
TextfileWindow::file_type
TextfileType file_type
Type of textfile to view.
Definition: textfile_gui.h:22
TextfileWindow::CheckHyperlinkClick
void CheckHyperlinkClick(Point pt)
Check if the user clicked on a hyperlink, and handle it if so.
Definition: textfile_gui.cpp:305
TextfileWindow::NavigateHistory
void NavigateHistory(int delta)
Navigate through the history, either forward or backward.
Definition: textfile_gui.cpp:378
WidgetID
int WidgetID
Widget ID.
Definition: window_type.h:18
TextfileWindow::NextString
std::optional< std::string_view > NextString() override
Get the next string to search through.
Definition: textfile_gui.cpp:631
window_gui.h
textfile_type.h
TextfileWindow::HistoryEntry::scrollpos
int scrollpos
Scrolling position the file was at at navigation time.
Definition: textfile_gui.h:65
TextfileWindow::Hyperlink
Definition: textfile_gui.h:56
TextfileWindow::history_pos
size_t history_pos
Position in browsing history (for forward movement).
Definition: textfile_gui.h:76
FontCacheSettings
Settings for the four different fonts.
Definition: fontcache.h:216
Window::resize
ResizeInfo resize
Resize information.
Definition: window_gui.h:308
TextfileWindow::AfterLoadText
virtual void AfterLoadText()
Post-processing after the text is loaded.
Definition: textfile_gui.cpp:496
TextfileWindow::Line
Definition: textfile_gui.h:46
TextfileWindow::links
std::vector< Hyperlink > links
Clickable links in lines.
Definition: textfile_gui.h:73
TextfileWindow::Hyperlink::destination
std::string destination
Destination for the link.
Definition: textfile_gui.h:60
TextfileWindow::LoadText
void LoadText(std::string_view buf)
Load a text into the textfile viewer.
Definition: textfile_gui.cpp:825
TextfileWindow::LoadTextfile
virtual void LoadTextfile(const std::string &textfile, Subdirectory dir)
Loads the textfile text from file and setup lines.
Definition: textfile_gui.cpp:765
TextfileWindow::SetupScrollbars
void SetupScrollbars(bool force_reflow)
Set scrollbars to the right lengths.
Definition: textfile_gui.cpp:148
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
TextfileWindow::Line::colour
TextColour colour
Colour to render text line in.
Definition: textfile_gui.h:50
TextfileWindow::FindHyperlinksInMarkdown
void FindHyperlinksInMarkdown(Line &line, size_t line_index)
Find any hyperlinks in a given line.
Definition: textfile_gui.cpp:243
TextfileWindow::jumplist
std::vector< size_t > jumplist
Table of contents list, line numbers.
Definition: textfile_gui.h:72
TextfileWindow::HistoryEntry::filepath
std::string filepath
File the history entry is in.
Definition: textfile_gui.h:64
TextfileWindow::hscroll
Scrollbar * hscroll
Horizontal scrollbar.
Definition: textfile_gui.h:24
TextfileWindow::max_length
uint max_length
Maximum length of unwrapped text line.
Definition: textfile_gui.h:101
TextfileWindow::Reset
void Reset() override
Reset the search, i.e.
Definition: textfile_gui.cpp:621
strings_func.h
TextfileWindow::search_iterator
uint search_iterator
Iterator for the font check search.
Definition: textfile_gui.h:100
TextfileWindow::UpdateHistoryScrollpos
void UpdateHistoryScrollpos()
Update the scroll position to the current, so we can restore there if we go back.
Definition: textfile_gui.cpp:368
TextfileWindow::filepath
std::string filepath
Full path to the filename.
Definition: textfile_gui.h:69
TextfileWindow::Monospace
bool Monospace() override
Whether to search for a monospace font or not.
Definition: textfile_gui.cpp:638
TextfileWindow::lines
std::vector< Line > lines
#text, split into lines in a table with lines.
Definition: textfile_gui.h:71
fileio_type.h
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
TextfileWindow::trusted
bool trusted
Whether the content is trusted (read: not from content like NewGRFs, etc).
Definition: textfile_gui.h:77
TextfileWindow::AppendHistory
void AppendHistory(const std::string &filepath)
Append the new location to the history, so the user can go back.
Definition: textfile_gui.cpp:355
GetTextfile
std::optional< std::string > GetTextfile(TextfileType type, Subdirectory dir, const std::string &filename)
Search a textfile file next to the given content.
Definition: textfile_gui.cpp:865
TextfileWindow::Hyperlink::begin
size_t begin
Character position on line the link begins.
Definition: textfile_gui.h:58
TextfileWindow::link_anchors
std::vector< Hyperlink > link_anchors
Anchor names of headings that can be linked to.
Definition: textfile_gui.h:74
TextfileWindow::Hyperlink::line
size_t line
Which line the link is on.
Definition: textfile_gui.h:57
TextfileWindow::Line::text
std::string text
Contents of the line.
Definition: textfile_gui.h:49
TextfileWindow
Window for displaying a textfile.
Definition: textfile_gui.h:21
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:202
Window
Data structure for an opened window.
Definition: window_gui.h:267
TextfileType
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:14
TextfileWindow::NavigateToFile
void NavigateToFile(std::string newfile, size_t line)
Navigate to the requested file.
Definition: textfile_gui.cpp:434
TextfileWindow::filename
std::string filename
Filename of the textfile.
Definition: textfile_gui.h:68
TextfileWindow::HistoryEntry
Definition: textfile_gui.h:63
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:75
TextfileWindow::Hyperlink::end
size_t end
Character position on line the link end.
Definition: textfile_gui.h:59
TextfileWindow::vscroll
Scrollbar * vscroll
Vertical scrollbar.
Definition: textfile_gui.h:23