OpenTTD Source: src/textfile_gui.h Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef TEXTFILE_GUI_H
11#define TEXTFILE_GUI_H
12
13#include "misc/alternating_iterator.hpp"
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
22 struct TextfileWindow : public Window, MissingGlyphSearcher {
23 TextfileType file_type{};
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:
49 TextfileWindow(Window *parent, TextfileType file_type);
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;
72 int scrollpos;
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);
103 void UpdateHistoryScrollpos();
104 void NavigateHistory(int delta);
105
106private:
107 uint search_iterator = 0;
108 int max_width = 0;
109 size_t num_lines = 0;
110
111 using LineIterator = std::vector<Line>::iterator;
112 using ReflowIterator = AlternatingIterator<LineIterator>;
113
114 ReflowIterator reflow_iter;
115 ReflowIterator reflow_end;
116
117 LineIterator visible_first;
118 LineIterator visible_last;
119
120 enum class ReflowState : uint8_t {
121 None,
122 Reflowed,
123 VisibleReflowed,
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 */
alternating_iterator.hpp
Iterator adaptor that takes items alternating from a middle position.
AlternatingIterator< LineIterator >
MissingGlyphSearcher
A searcher for missing glyphs.
Definition strings_func.h:156
Scrollbar
Scrollbar data structure.
Definition widget_type.h:711
fileio_type.h
Types for Standard In/Out file operations.
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition fluidsynth.cpp:25
FontSize
FontSize
Available font sizes.
Definition gfx_type.h:248
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
strings_func.h
Functions related to OTTD's strings.
Coord2D< int >
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:56
FontCacheSettings
Settings for the four different fonts.
Definition fontcache.h:180
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
TextfileWindow::HistoryEntry
Definition textfile_gui.h:70
TextfileWindow::HistoryEntry::scrollpos
int scrollpos
Scrolling position the file was at at navigation time.
Definition textfile_gui.h:72
TextfileWindow::HistoryEntry::filepath
std::string filepath
File the history entry is in.
Definition textfile_gui.h:71
TextfileWindow::Hyperlink
Definition textfile_gui.h:63
TextfileWindow::Hyperlink::end
size_t end
Character position on line the link end.
Definition textfile_gui.h:66
TextfileWindow::Hyperlink::line
size_t line
Which line the link is on.
Definition textfile_gui.h:64
TextfileWindow::Hyperlink::begin
size_t begin
Character position on line the link begins.
Definition textfile_gui.h:65
TextfileWindow::Hyperlink::destination
std::string destination
Destination for the link.
Definition textfile_gui.h:67
TextfileWindow::Line
Definition textfile_gui.h:52
TextfileWindow::Line::colour
TextColour colour
Colour to render text line in.
Definition textfile_gui.h:56
TextfileWindow::Line::num_lines
int num_lines
Number of visual lines for this line.
Definition textfile_gui.h:53
TextfileWindow::Line::text
std::string text
Contents of the line.
Definition textfile_gui.h:57
TextfileWindow
Window for displaying a textfile.
Definition textfile_gui.h:22
TextfileWindow::hscroll
Scrollbar * hscroll
Horizontal scrollbar.
Definition textfile_gui.h:25
TextfileWindow::OnInvalidateData
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition textfile_gui.cpp:616
TextfileWindow::visible_first
LineIterator visible_first
Iterator to first visible element.
Definition textfile_gui.h:117
TextfileWindow::visible_last
LineIterator visible_last
Iterator to last visible element.
Definition textfile_gui.h:118
TextfileWindow::Reset
void Reset() override
Reset the search, i.e.
Definition textfile_gui.cpp:740
TextfileWindow::reflow_iter
ReflowIterator reflow_iter
Current iterator for reflow.
Definition textfile_gui.h:114
TextfileWindow::search_iterator
uint search_iterator
Iterator for the font check search.
Definition textfile_gui.h:107
TextfileWindow::SetupScrollbars
void SetupScrollbars()
Set scrollbars to the right lengths.
Definition textfile_gui.cpp:136
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:334
TextfileWindow::Monospace
bool Monospace() override
Whether to search for a monospace font or not.
Definition textfile_gui.cpp:757
TextfileWindow::GetHyperlink
const Hyperlink * GetHyperlink(Point pt) const
Get the hyperlink at the given position.
Definition textfile_gui.cpp:284
TextfileWindow::NavigateToFile
void NavigateToFile(std::string newfile, size_t line)
Navigate to the requested file.
Definition textfile_gui.cpp:413
TextfileWindow::NextString
std::optional< std::string_view > NextString() override
Get the next string to search through.
Definition textfile_gui.cpp:750
TextfileWindow::reflow_end
ReflowIterator reflow_end
End iterator for reflow.
Definition textfile_gui.h:115
TextfileWindow::trusted
bool trusted
Whether the content is trusted (read: not from content like NewGRFs, etc).
Definition textfile_gui.h:84
TextfileWindow::lines
std::vector< Line > lines
#text, split into lines in a table with lines.
Definition textfile_gui.h:78
TextfileWindow::LoadText
void LoadText(std::string_view buf)
Load a text into the textfile viewer.
Definition textfile_gui.cpp:907
TextfileWindow::OnTooltip
bool OnTooltip(Point pt, WidgetID widget, TooltipCloseCondition close_cond) override
Event to display a custom tooltip.
Definition textfile_gui.cpp:537
TextfileWindow::OnClick
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition textfile_gui.cpp:504
TextfileWindow::UpdateHistoryScrollpos
void UpdateHistoryScrollpos()
Update the scroll position to the current, so we can restore there if we go back.
Definition textfile_gui.cpp:347
TextfileWindow::AfterLoadText
virtual void AfterLoadText()
Post-processing after the text is loaded.
Definition textfile_gui.cpp:475
TextfileWindow::filepath
std::string filepath
Full path to the filename.
Definition textfile_gui.h:76
TextfileWindow::OnScrollbarScroll
void OnScrollbarScroll(WidgetID widget) override
Notify window that a scrollbar position has been updated.
Definition textfile_gui.cpp:705
TextfileWindow::file_type
TextfileType file_type
Type of textfile to view.
Definition textfile_gui.h:23
TextfileWindow::num_lines
size_t num_lines
Number of lines of text, taking account of wrapping.
Definition textfile_gui.h:109
TextfileWindow::AfterLoadMarkdown
void AfterLoadMarkdown()
Post-processing of markdown files.
Definition textfile_gui.cpp:487
TextfileWindow::DefaultSize
FontSize DefaultSize() override
Get the default (font) size of the string.
Definition textfile_gui.cpp:745
TextfileWindow::OnInit
void OnInit() override
Notification that the nested widget tree gets initialized.
Definition textfile_gui.cpp:602
TextfileWindow::SetFontNames
void SetFontNames(FontCacheSettings *settings, std::string_view font_name, const void *os_data) override
Set the right font names.
Definition textfile_gui.cpp:762
TextfileWindow::DrawWidget
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
Definition textfile_gui.cpp:549
TextfileWindow::OnRealtimeTick
void OnRealtimeTick(uint delta_ms) override
Called periodically.
Definition textfile_gui.cpp:679
TextfileWindow::OnDropdownSelect
void OnDropdownSelect(WidgetID widget, int index, int) override
A dropdown option associated to this window has been selected.
Definition textfile_gui.cpp:624
TextfileWindow::history_pos
size_t history_pos
Position in browsing history (for forward movement).
Definition textfile_gui.h:83
TextfileWindow::link_anchors
std::vector< Hyperlink > link_anchors
Anchor names of headings that can be linked to.
Definition textfile_gui.h:81
TextfileWindow::NavigateHistory
void NavigateHistory(int delta)
Navigate through the history, either forward or backward.
Definition textfile_gui.cpp:357
TextfileWindow::OnHyperlinkClick
virtual void OnHyperlinkClick(const Hyperlink &link)
Handle the clicking on a hyperlink.
Definition textfile_gui.cpp:379
TextfileWindow::history
std::vector< HistoryEntry > history
Browsing history in this window.
Definition textfile_gui.h:82
TextfileWindow::UpdateWidgetSize
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.
Definition textfile_gui.cpp:122
TextfileWindow::links
std::vector< Hyperlink > links
Clickable links in lines.
Definition textfile_gui.h:80
TextfileWindow::ReflowContent
void ReflowContent()
Reset the reflow process to start on the next UI tick.
Definition textfile_gui.cpp:110
TextfileWindow::ReflowState
ReflowState
Definition textfile_gui.h:120
TextfileWindow::ReflowState::VisibleReflowed
@ VisibleReflowed
Visible content has been reflowed.
TextfileWindow::ReflowState::None
@ None
Nothing has been reflowed.
TextfileWindow::ReflowState::Reflowed
@ Reflowed
Content has been reflowed.
TextfileWindow::filename
std::string filename
Filename of the textfile.
Definition textfile_gui.h:75
TextfileWindow::vscroll
Scrollbar * vscroll
Vertical scrollbar.
Definition textfile_gui.h:24
TextfileWindow::LoadTextfile
virtual void LoadTextfile(const std::string &textfile, Subdirectory dir)
Loads the textfile text from file and setup lines.
Definition textfile_gui.cpp:851
TextfileWindow::max_width
int max_width
Maximum length of unwrapped text line.
Definition textfile_gui.h:108
TextfileWindow::OnResize
void OnResize() override
Called after the window got resized.
Definition textfile_gui.cpp:592
TextfileWindow::jumplist
std::vector< size_t > jumplist
Table of contents list, line numbers.
Definition textfile_gui.h:79
TextfileWindow::FindHyperlinksInMarkdown
void FindHyperlinksInMarkdown(Line &line, size_t line_index)
Find any hyperlinks in a given line.
Definition textfile_gui.cpp:225
Window
Data structure for an opened window.
Definition window_gui.h:274
Window::parent
Window * parent
Parent window.
Definition window_gui.h:329
Window::resize
ResizeInfo resize
Resize information.
Definition window_gui.h:315
GetTextfile
std::optional< std::string > GetTextfile(TextfileType type, Subdirectory dir, std::string_view filename)
Search a textfile file next to the given content.
Definition textfile_gui.cpp:939
textfile_type.h
Types related to textfiles.
TextfileType
TextfileType
Additional text files accompanying Tar archives.
Definition textfile_type.h:14
window_gui.h
Functions, definitions and such used only by the GUI.
WidgetID
int WidgetID
Widget ID.
Definition window_type.h:20