Fix: Don't allow right-click to close world generation progress window. (#13084)
[openttd-github.git] / src / textfile_gui.h
blobab365d42d520ec52faa10fcd0051e790b6373c4a
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 */
8 /** @file textfile_gui.h GUI functions related to textfiles. */
10 #ifndef TEXTFILE_GUI_H
11 #define TEXTFILE_GUI_H
13 #include "fileio_type.h"
14 #include "strings_func.h"
15 #include "textfile_type.h"
16 #include "window_gui.h"
18 std::optional<std::string> GetTextfile(TextfileType type, Subdirectory dir, const std::string &filename);
20 /** Window for displaying a textfile */
21 struct TextfileWindow : public Window, MissingGlyphSearcher {
22 TextfileType file_type; ///< Type of textfile to view.
23 Scrollbar *vscroll; ///< Vertical scrollbar.
24 Scrollbar *hscroll; ///< Horizontal scrollbar.
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;
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);
40 virtual void LoadTextfile(const std::string &textfile, Subdirectory dir);
42 protected:
43 TextfileWindow(TextfileType file_type);
44 void ConstructWindow();
46 struct Line {
47 int top = 0; ///< Top scroll position in visual lines.
48 int bottom = 0; ///< Bottom scroll position in visual lines.
49 std::string text{}; ///< Contents of the line.
50 TextColour colour = TC_WHITE; ///< Colour to render text line in.
52 Line(int top, std::string_view text) : top(top), bottom(top + 1), text(text) {}
53 Line() {}
56 struct Hyperlink {
57 size_t line; ///< Which line the link is on.
58 size_t begin; ///< Character position on line the link begins.
59 size_t end; ///< Character position on line the link end.
60 std::string destination; ///< Destination for the link.
63 struct HistoryEntry {
64 std::string filepath; ///< File the history entry is in.
65 int scrollpos; ///< Scrolling position the file was at at navigation time.
68 std::string filename{}; ///< Filename of the textfile.
69 std::string filepath{}; ///< Full path to the filename.
71 std::vector<Line> lines; ///< #text, split into lines in a table with lines.
72 std::vector<size_t> jumplist; ///< Table of contents list, line numbers.
73 std::vector<Hyperlink> links; ///< Clickable links in lines.
74 std::vector<Hyperlink> link_anchors; ///< Anchor names of headings that can be linked to.
75 std::vector<HistoryEntry> history; ///< Browsing history in this window.
76 size_t history_pos = 0; ///< Position in browsing history (for forward movement).
77 bool trusted = false; ///< Whether the content is trusted (read: not from content like NewGRFs, etc).
79 void LoadText(std::string_view buf);
80 void FindHyperlinksInMarkdown(Line &line, size_t line_index);
82 /**
83 * Handle the clicking on a hyperlink.
85 * @param link The link that is clicked on.
87 virtual void OnHyperlinkClick(const Hyperlink &link);
89 /**
90 * Post-processing after the text is loaded.
92 virtual void AfterLoadText();
94 void NavigateToFile(std::string newfile, size_t line);
95 void AppendHistory(const std::string &filepath);
96 void UpdateHistoryScrollpos();
97 void NavigateHistory(int delta);
99 private:
100 uint search_iterator = 0; ///< Iterator for the font check search.
101 uint max_length = 0; ///< Maximum length of unwrapped text line.
103 uint ReflowContent();
104 uint GetContentHeight();
105 void SetupScrollbars(bool force_reflow);
106 void CheckHyperlinkClick(Point pt);
108 void AfterLoadMarkdown();
111 #endif /* TEXTFILE_GUI_H */