OpenTTD Source 20250607-master-g6b5cde463a
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
14#include "fileio_type.h"
15#include "strings_func.h"
16#include "textfile_type.h"
17#include "window_gui.h"
18
19std::optional<std::string> GetTextfile(TextfileType type, Subdirectory dir, std::string_view filename);
20
24 Scrollbar *vscroll = nullptr;
25 Scrollbar *hscroll = nullptr;
26
27 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override;
28 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override;
29 bool OnTooltip([[maybe_unused]] Point pt, WidgetID widget, TooltipCloseCondition close_cond) override;
30 void DrawWidget(const Rect &r, WidgetID widget) const override;
31 void OnResize() override;
32 void OnInit() override;
33 void OnInvalidateData(int data = 0, bool gui_scope = true) override;
34 void OnDropdownSelect(WidgetID widget, int index, int) override;
35 void OnRealtimeTick(uint delta_ms) override;
36 void OnScrollbarScroll(WidgetID widget) override;
37
38 void Reset() override;
39 FontSize DefaultSize() override;
40 std::optional<std::string_view> NextString() override;
41 bool Monospace() override;
42 void SetFontNames(FontCacheSettings *settings, std::string_view font_name, const void *os_data) override;
43 void ScrollToLine(size_t line);
44 bool IsTextWrapped() const;
45
46 virtual void LoadTextfile(const std::string &textfile, Subdirectory dir);
47
48protected:
50 void ConstructWindow();
51
52 struct Line {
53 int num_lines = 1;
54 int wrapped_width = 0;
55 int max_width = -1;
56 TextColour colour = TC_WHITE;
57 std::string text{};
58
59 Line(std::string_view text) : text(text) {}
60 Line() {}
61 };
62
63 struct Hyperlink {
64 size_t line = 0;
65 size_t begin = 0;
66 size_t end = 0;
67 std::string destination{};
68 };
69
70 struct HistoryEntry {
71 std::string filepath;
73 };
74
75 std::string filename{};
76 std::string filepath{};
77
78 std::vector<Line> lines{};
79 std::vector<size_t> jumplist{};
80 std::vector<Hyperlink> links{};
81 std::vector<Hyperlink> link_anchors{};
82 std::vector<HistoryEntry> history{};
83 size_t history_pos = 0;
84 bool trusted = false;
85
86 void LoadText(std::string_view buf);
87 void FindHyperlinksInMarkdown(Line &line, size_t line_index);
88
94 virtual void OnHyperlinkClick(const Hyperlink &link);
95
99 virtual void AfterLoadText();
100
101 void NavigateToFile(std::string newfile, size_t line);
102 void AppendHistory(const std::string &filepath);
104 void NavigateHistory(int delta);
105
106private:
108 int max_width = 0;
109 size_t num_lines = 0;
110
111 using LineIterator = std::vector<Line>::iterator;
113
116
117 LineIterator visible_first;
118 LineIterator visible_last;
119
120 enum class ReflowState : uint8_t {
121 None,
122 Reflowed,
124 };
125
126 std::vector<TextfileWindow::Line>::iterator GetIteratorFromPosition(int pos);
127 void UpdateVisibleIterators();
128 void ReflowContent();
129 ReflowState ContinueReflow();
130 void SetupScrollbars();
131 const Hyperlink *GetHyperlink(Point pt) const;
132
133 void AfterLoadMarkdown();
134};
135
136#endif /* TEXTFILE_GUI_H */
Iterator adaptor that takes items alternating from a middle position.
A searcher for missing glyphs.
Scrollbar data structure.
Types for Standard In/Out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:87
fluid_settings_t * settings
FluidSynth settings handle.
FontSize
Available font sizes.
Definition gfx_type.h:250
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:302
Functions related to OTTD's strings.
Dimensions (a width and height) of a rectangle in 2D.
Settings for the four different fonts.
Definition fontcache.h:200
Coordinates of a point in 2D.
Specification of a rectangle with absolute coordinates of all edges.
int scrollpos
Scrolling position the file was at at navigation time.
std::string filepath
File the history entry is in.
TextColour colour
Colour to render text line in.
int num_lines
Number of visual lines for this line.
std::string text
Contents of the line.
Window for displaying a textfile.
Scrollbar * hscroll
Horizontal scrollbar.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
LineIterator visible_first
Iterator to first visible element.
LineIterator visible_last
Iterator to last visible element.
void Reset() override
Reset the search, i.e.
ReflowIterator reflow_iter
Current iterator for reflow.
uint search_iterator
Iterator for the font check search.
void SetupScrollbars()
Set scrollbars to the right lengths.
void AppendHistory(const std::string &filepath)
Append the new location to the history, so the user can go back.
bool Monospace() override
Whether to search for a monospace font or not.
const Hyperlink * GetHyperlink(Point pt) const
Get the hyperlink at the given position.
void NavigateToFile(std::string newfile, size_t line)
Navigate to the requested file.
std::optional< std::string_view > NextString() override
Get the next string to search through.
ReflowIterator reflow_end
End iterator for reflow.
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 LoadText(std::string_view buf)
Load a text into the textfile viewer.
bool OnTooltip(Point pt, WidgetID widget, TooltipCloseCondition close_cond) override
Event to display a custom tooltip.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
void UpdateHistoryScrollpos()
Update the scroll position to the current, so we can restore there if we go back.
virtual void AfterLoadText()
Post-processing after the text is loaded.
std::string filepath
Full path to the filename.
void OnScrollbarScroll(WidgetID widget) override
Notify window that a scrollbar position has been updated.
TextfileType file_type
Type of textfile to view.
size_t num_lines
Number of lines of text, taking account of wrapping.
void AfterLoadMarkdown()
Post-processing of markdown files.
FontSize DefaultSize() override
Get the default (font) size of the string.
void OnInit() override
Notification that the nested widget tree gets initialized.
void SetFontNames(FontCacheSettings *settings, std::string_view font_name, const void *os_data) override
Set the right font names.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
void OnRealtimeTick(uint delta_ms) override
Called periodically.
void OnDropdownSelect(WidgetID widget, int index, int) override
A dropdown option associated to this window has been selected.
size_t history_pos
Position in browsing history (for forward movement).
std::vector< Hyperlink > link_anchors
Anchor names of headings that can be linked to.
void NavigateHistory(int delta)
Navigate through the history, either forward or backward.
virtual void OnHyperlinkClick(const Hyperlink &link)
Handle the clicking on a hyperlink.
std::vector< HistoryEntry > history
Browsing history in this window.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
std::vector< Hyperlink > links
Clickable links in lines.
void ReflowContent()
Reset the reflow process to start on the next UI tick.
@ VisibleReflowed
Visible content has been reflowed.
@ None
Nothing has been reflowed.
@ Reflowed
Content has been reflowed.
std::string filename
Filename of the textfile.
Scrollbar * vscroll
Vertical scrollbar.
virtual void LoadTextfile(const std::string &textfile, Subdirectory dir)
Loads the textfile text from file and setup lines.
int max_width
Maximum length of unwrapped text line.
void OnResize() override
Called after the window got resized.
std::vector< size_t > jumplist
Table of contents list, line numbers.
void FindHyperlinksInMarkdown(Line &line, size_t line_index)
Find any hyperlinks in a given line.
Data structure for an opened window.
Definition window_gui.h:273
Window * parent
Parent window.
Definition window_gui.h:328
ResizeInfo resize
Resize information.
Definition window_gui.h:314
std::optional< std::string > GetTextfile(TextfileType type, Subdirectory dir, std::string_view filename)
Search a textfile file next to the given content.
Types related to textfiles.
TextfileType
Additional text files accompanying Tar archives.
Functions, definitions and such used only by the GUI.
int WidgetID
Widget ID.
Definition window_type.h:20