Merge branch 'master' into jgrpp

# Conflicts:
#	cmake/SourceList.cmake
#	src/build_vehicle_gui.cpp
#	src/company_gui.cpp
#	src/console_cmds.cpp
#	src/depot_base.h
#	src/elrail.cpp
#	src/network/core/udp.cpp
#	src/network/network_admin.cpp
#	src/network/network_chat_gui.cpp
#	src/network/network_gui.cpp
#	src/network/network_server.cpp
#	src/newgrf.cpp
#	src/newgrf_engine.cpp
#	src/newgrf_railtype.cpp
#	src/newgrf_railtype.h
#	src/newgrf_storage.h
#	src/os/unix/crashlog_unix.cpp
#	src/rail.h
#	src/rail_cmd.cpp
#	src/rail_gui.cpp
#	src/road_cmd.cpp
#	src/road_map.h
#	src/saveload/labelmaps_sl.cpp
#	src/settings_gui.cpp
#	src/settings_type.h
#	src/sl/oldloader_sl.cpp
#	src/station_cmd.cpp
#	src/station_gui.cpp
#	src/table/settings/world_settings.ini
#	src/tests/test_script_admin.cpp
#	src/textfile_gui.cpp
#	src/toolbar_gui.cpp
#	src/train_cmd.cpp
#	src/tunnelbridge_cmd.cpp
#	src/vehicle_gui.cpp
#	src/widget.cpp
#	src/window.cpp
#	src/window_gui.h
#	src/window_type.h
This commit is contained in:
Jonathan G Rennison
2023-11-19 12:19:17 +00:00
230 changed files with 2458 additions and 1106 deletions

View File

@@ -15,10 +15,16 @@
#include "gfx_func.h"
#include "string_func.h"
#include "textfile_gui.h"
#include "widgets/dropdown_type.h"
#include "gfx_layout.h"
#include "debug.h"
#include "debug_fmt.h"
#include "openttd.h"
#include "widgets/misc_widget.h"
#include "table/strings.h"
#include "table/control_codes.h"
#if defined(WITH_ZLIB)
#include <zlib.h>
@@ -28,16 +34,33 @@
#include <lzma.h>
#endif
#include <regex>
#include "safeguards.h"
/** Widgets for the textfile window. */
static const NWidgetPart _nested_textfile_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
NWidget(WWT_PUSHARROWBTN, COLOUR_MAUVE, WID_TF_NAVBACK), SetFill(0, 1), SetMinimalSize(15, 1), SetDataTip(AWV_DECREASE, STR_TEXTFILE_NAVBACK_TOOLTIP),
NWidget(WWT_PUSHARROWBTN, COLOUR_MAUVE, WID_TF_NAVFORWARD), SetFill(0, 1), SetMinimalSize(15, 1), SetDataTip(AWV_INCREASE, STR_TEXTFILE_NAVFORWARD_TOOLTIP),
NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_TF_CAPTION), SetDataTip(STR_NULL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
NWidget(WWT_TEXTBTN, COLOUR_MAUVE, WID_TF_WRAPTEXT), SetDataTip(STR_TEXTFILE_WRAP_TEXT, STR_TEXTFILE_WRAP_TEXT_TOOLTIP),
NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
EndContainer(),
NWidget(NWID_SELECTION, INVALID_COLOUR, WID_TF_SEL_JUMPLIST),
NWidget(WWT_PANEL, COLOUR_MAUVE),
NWidget(NWID_HORIZONTAL), SetPIP(WidgetDimensions::unscaled.frametext.left, 0, WidgetDimensions::unscaled.frametext.right),
/* As this widget can be toggled, it needs to be a multiplier of FS_MONO. So add a spacer that ensures this. */
NWidget(NWID_SPACER), SetMinimalSize(1, 0), SetMinimalTextLines(2, 0, FS_MONO),
NWidget(NWID_VERTICAL),
NWidget(NWID_SPACER), SetFill(1, 1), SetResize(1, 0),
NWidget(WWT_DROPDOWN, COLOUR_MAUVE, WID_TF_JUMPLIST), SetDataTip(STR_TEXTFILE_JUMPLIST, STR_TEXTFILE_JUMPLIST_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
NWidget(NWID_SPACER), SetFill(1, 1), SetResize(1, 0),
EndContainer(),
EndContainer(),
EndContainer(),
EndContainer(),
NWidget(NWID_HORIZONTAL),
NWidget(WWT_PANEL, COLOUR_MAUVE, WID_TF_BACKGROUND), SetMinimalSize(200, 125), SetResize(1, 12), SetScrollbar(WID_TF_VSCROLLBAR),
EndContainer(),
@@ -56,7 +79,7 @@ static WindowDesc _textfile_desc(
WDP_CENTER, "textfile", 630, 460,
WC_TEXTFILE, WC_NONE,
0,
_nested_textfile_widgets, lengthof(_nested_textfile_widgets)
std::begin(_nested_textfile_widgets), std::end(_nested_textfile_widgets)
);
TextfileWindow::TextfileWindow(TextfileType file_type) : Window(&_textfile_desc), file_type(file_type)
@@ -66,7 +89,10 @@ TextfileWindow::TextfileWindow(TextfileType file_type) : Window(&_textfile_desc)
this->hscroll = this->GetScrollbar(WID_TF_HSCROLLBAR);
this->FinishInitNested(file_type);
this->GetWidget<NWidgetCore>(WID_TF_CAPTION)->SetDataTip(STR_TEXTFILE_README_CAPTION + file_type, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
this->GetWidget<NWidgetStacked>(WID_TF_SEL_JUMPLIST)->SetDisplayedPlane(SZSP_HORIZONTAL);
this->DisableWidget(WID_TF_NAVBACK);
this->DisableWidget(WID_TF_NAVFORWARD);
this->hscroll->SetStepSize(10); // Speed up horizontal scrollbar
}
@@ -130,6 +156,367 @@ void TextfileWindow::SetupScrollbars(bool force_reflow)
this->SetWidgetDisabledState(WID_TF_HSCROLLBAR, IsWidgetLowered(WID_TF_WRAPTEXT));
}
/** Regular expression that searches for Markdown links. */
static const std::regex _markdown_link_regex{"\\[(.+?)\\]\\((.+?)\\)", std::regex_constants::ECMAScript | std::regex_constants::optimize};
/** Types of link we support in markdown files. */
enum class LinkType {
Internal, ///< Internal link, or "anchor" in HTML language.
Web, ///< Link to an external website.
File, ///< Link to a local file.
Unknown, ///< Unknown link.
};
/**
* Classify the type of hyperlink the destination describes.
*
* @param destination The hyperlink destination.
* @param trusted Whether we trust the content of this file.
* @return LinkType The classification of the link.
*/
static LinkType ClassifyHyperlink(const std::string &destination, bool trusted)
{
if (destination.empty()) return LinkType::Unknown;
if (StrStartsWith(destination, "#")) return LinkType::Internal;
/* Only allow external / internal links for sources we trust. */
if (!trusted) return LinkType::Unknown;
if (StrStartsWith(destination, "http://")) return LinkType::Web;
if (StrStartsWith(destination, "https://")) return LinkType::Web;
if (StrStartsWith(destination, "./")) return LinkType::File;
return LinkType::Unknown;
}
/**
* Create a valid slug for the anchor.
*
* @param line The line to create the slug for.
* @return std::string The slug.
*/
static std::string MakeAnchorSlug(const std::string &line)
{
std::string r = "#";
uint state = 0;
for (char c : line) {
if (state == 0) {
/* State 0: Skip leading hashmarks and spaces. */
if (c == '#') continue;
if (c == ' ') continue;
state = 1;
}
if (state == 2) {
/* State 2: Wait for a non-space/dash character.
* When found, output a dash and that character. */
if (c == ' ' || c == '-') continue;
r += '-';
state = 1;
}
if (state == 1) {
/* State 1: Normal text.
* Lowercase alphanumerics,
* spaces and dashes become dashes,
* everything else is removed. */
if (isalnum(c)) {
r += tolower(c);
} else if (c == ' ' || c == '-') {
state = 2;
}
}
}
return r;
}
/**
* Find any hyperlinks in a given line.
*
* @param line The line to search for hyperlinks.
* @param line_index The index of the line.
*/
void TextfileWindow::FindHyperlinksInMarkdown(Line &line, size_t line_index)
{
std::string::const_iterator last_match_end = line.text.cbegin();
std::string fixed_line;
char ccbuf[5];
std::sregex_iterator matcher{ line.text.cbegin(), line.text.cend(), _markdown_link_regex};
while (matcher != std::sregex_iterator()) {
std::smatch match = *matcher;
Hyperlink link;
link.line = line_index;
link.destination = match[2].str();
this->links.push_back(link);
LinkType link_type = ClassifyHyperlink(link.destination, this->trusted);
StringControlCode link_colour;
switch (link_type) {
case LinkType::Internal:
link_colour = SCC_GREEN;
break;
case LinkType::Web:
link_colour = SCC_LTBLUE;
break;
case LinkType::File:
link_colour = SCC_LTBROWN;
break;
default:
/* Don't make other link types fancy as they aren't handled (yet). */
link_colour = SCC_CONTROL_END;
break;
}
if (link_colour != SCC_CONTROL_END) {
/* Format the link to look like a link. */
fixed_line += std::string(last_match_end, match[0].first);
this->links.back().begin = fixed_line.length();
fixed_line += std::string(ccbuf, Utf8Encode(ccbuf, SCC_PUSH_COLOUR));
fixed_line += std::string(ccbuf, Utf8Encode(ccbuf, link_colour));
fixed_line += match[1].str();
this->links.back().end = fixed_line.length();
fixed_line += std::string(ccbuf, Utf8Encode(ccbuf, SCC_POP_COLOUR));
last_match_end = match[0].second;
}
/* Find next link. */
++matcher;
}
if (last_match_end == line.text.cbegin()) return; // nothing found
/* Add remaining text on line. */
fixed_line += std::string(last_match_end, line.text.cend());
/* Overwrite original line text with "fixed" line text. */
line.text = fixed_line;
}
/**
* Check if the user clicked on a hyperlink, and handle it if so.
*
* @param pt The loation the user clicked.
*/
void TextfileWindow::CheckHyperlinkClick(Point pt)
{
if (this->links.empty()) return;
/* Which line was clicked. */
const int clicked_row = this->GetRowFromWidget(pt.y, WID_TF_BACKGROUND, WidgetDimensions::scaled.frametext.top, FONT_HEIGHT_MONO) + this->GetScrollbar(WID_TF_VSCROLLBAR)->GetPosition();
size_t line_index;
size_t subline;
if (IsWidgetLowered(WID_TF_WRAPTEXT)) {
auto it = std::find_if(std::begin(this->lines), std::end(this->lines), [clicked_row](const Line &l) { return l.top <= clicked_row && l.bottom > clicked_row; });
if (it == this->lines.cend()) return;
line_index = it - this->lines.cbegin();
subline = clicked_row - it->top;
Debug(misc, 4, "TextfileWindow check hyperlink: clicked_row={}, line_index={}, line.top={}, subline={}", clicked_row, line_index, it->top, subline);
} else {
line_index = clicked_row / FONT_HEIGHT_MONO;
subline = 0;
}
/* Find hyperlinks in this line. */
std::vector<Hyperlink> found_links;
for (const auto &link : this->links) {
if (link.line == line_index) found_links.push_back(link);
}
if (found_links.empty()) return;
/* Build line layout to figure out character position that was clicked. */
uint window_width = IsWidgetLowered(WID_TF_WRAPTEXT) ? this->GetWidget<NWidgetCore>(WID_TF_BACKGROUND)->current_x - WidgetDimensions::scaled.frametext.Horizontal() : INT_MAX;
Layouter layout(this->lines[line_index].text, window_width, this->lines[line_index].colour, FS_MONO);
assert(subline < layout.size());
ptrdiff_t char_index = layout.GetCharAtPosition(pt.x - WidgetDimensions::scaled.frametext.left, subline);
if (char_index < 0) return;
Debug(misc, 4, "TextfileWindow check hyperlink click: line={}, subline={}, char_index={}", line_index, subline, (int)char_index);
/* Found character index in line, check if any links are at that position. */
for (const auto &link : found_links) {
Debug(misc, 4, "Checking link from char {} to {}", link.begin, link.end);
if ((size_t)char_index >= link.begin && (size_t)char_index < link.end) {
Debug(misc, 4, "Activating link with destination: {}", link.destination);
this->OnHyperlinkClick(link);
return;
}
}
}
/**
* Append the new location to the history, so the user can go back.
*
* @param filepath The location the user is navigating to.
*/
void TextfileWindow::AppendHistory(const std::string &filepath)
{
this->history.erase(this->history.begin() + this->history_pos + 1, this->history.end());
this->UpdateHistoryScrollpos();
this->history.push_back(HistoryEntry{ filepath, 0 });
this->EnableWidget(WID_TF_NAVBACK);
this->DisableWidget(WID_TF_NAVFORWARD);
this->history_pos = this->history.size() - 1;
}
/**
* Update the scroll position to the current, so we can restore there if we go back.
*/
void TextfileWindow::UpdateHistoryScrollpos()
{
this->history[this->history_pos].scrollpos = this->GetScrollbar(WID_TF_VSCROLLBAR)->GetPosition();
}
/**
* Navigate through the history, either forward or backward.
*
* @param delta The direction to navigate.
*/
void TextfileWindow::NavigateHistory(int delta)
{
if (delta == 0) return;
if (delta < 0 && static_cast<int>(this->history_pos) < -delta) return;
if (delta > 0 && this->history_pos + delta >= this->history.size()) return;
this->UpdateHistoryScrollpos();
this->history_pos += delta;
if (this->history[this->history_pos].filepath != this->filepath) {
this->filepath = this->history[this->history_pos].filepath;
this->filename = this->filepath.substr(this->filepath.find_last_of(PATHSEP) + 1);
this->LoadTextfile(this->filepath, NO_DIRECTORY);
}
this->SetWidgetDisabledState(WID_TF_NAVFORWARD, this->history_pos + 1 >= this->history.size());
this->SetWidgetDisabledState(WID_TF_NAVBACK, this->history_pos == 0);
this->GetScrollbar(WID_TF_VSCROLLBAR)->SetPosition(this->history[this->history_pos].scrollpos);
this->GetScrollbar(WID_TF_HSCROLLBAR)->SetPosition(0);
this->SetDirty();
}
/* virtual */ void TextfileWindow::OnHyperlinkClick(const Hyperlink &link)
{
switch (ClassifyHyperlink(link.destination, this->trusted)) {
case LinkType::Internal:
{
auto it = std::find_if(this->link_anchors.cbegin(), this->link_anchors.cend(), [&](const Hyperlink &other) { return link.destination == other.destination; });
if (it != this->link_anchors.cend()) {
this->AppendHistory(this->filepath);
this->ScrollToLine(it->line);
this->UpdateHistoryScrollpos();
}
break;
}
case LinkType::Web:
OpenBrowser(link.destination.c_str());
break;
case LinkType::File:
this->NavigateToFile(link.destination, 0);
break;
default:
/* Do nothing */
break;
}
}
/**
* Navigate to the requested file.
*
* @param newfile The file to navigate to.
* @param line The line to scroll to.
*/
void TextfileWindow::NavigateToFile(std::string newfile, size_t line)
{
/* Double-check that the file link begins with ./ as a relative path. */
if (!StrStartsWith(newfile, "./")) return;
/* Get the path portion of the current file path. */
std::string newpath = this->filepath;
size_t pos = newpath.find_last_of(PATHSEPCHAR);
if (pos == std::string::npos) {
newpath.clear();
} else {
newpath.erase(pos + 1);
}
/* Check and remove for anchor in link. Do this before we find the filename, as people might have a / after the hash. */
size_t anchor_pos = newfile.find_first_of('#');
std::string anchor;
if (anchor_pos != std::string::npos) {
anchor = newfile.substr(anchor_pos);
newfile.erase(anchor_pos);
}
/* Now the anchor is gone, check if this is a markdown or textfile. */
if (!StrEndsWithIgnoreCase(newfile, ".md") && !StrEndsWithIgnoreCase(newfile, ".txt")) return;
/* Convert link destination to acceptable local filename (replace forward slashes with correct path separator). */
newfile = newfile.substr(2);
if (PATHSEPCHAR != '/') {
for (char &c : newfile) {
if (c == '/') c = PATHSEPCHAR;
}
}
/* Paste the two together and check file exists. */
newpath = newpath + newfile;
if (!FioCheckFileExists(newpath, NO_DIRECTORY)) return;
/* Update history. */
this->AppendHistory(newpath);
/* Load the new file. */
this->filepath = newpath;
this->filename = newpath.substr(newpath.find_last_of(PATHSEP) + 1);
this->LoadTextfile(this->filepath, NO_DIRECTORY);
this->GetScrollbar(WID_TF_HSCROLLBAR)->SetPosition(0);
this->GetScrollbar(WID_TF_VSCROLLBAR)->SetPosition(0);
if (anchor.empty() || line != 0) {
this->ScrollToLine(line);
} else {
auto anchor_dest = std::find_if(this->link_anchors.cbegin(), this->link_anchors.cend(), [&](const Hyperlink &other) { return anchor == other.destination; });
if (anchor_dest != this->link_anchors.cend()) {
this->ScrollToLine(anchor_dest->line);
this->UpdateHistoryScrollpos();
} else {
this->ScrollToLine(0);
}
}
}
/* virtual */ void TextfileWindow::AfterLoadText()
{
this->link_anchors.clear();
if (StrEndsWithIgnoreCase(this->filename, ".md")) this->AfterLoadMarkdown();
this->GetWidget<NWidgetStacked>(WID_TF_SEL_JUMPLIST)->SetDisplayedPlane(this->jumplist.empty() ? SZSP_HORIZONTAL : 0);
}
/**
* Post-processing of markdown files.
*/
void TextfileWindow::AfterLoadMarkdown()
{
for (size_t line_index = 0; line_index < this->lines.size(); ++line_index) {
Line &line = this->lines[line_index];
/* Find and mark all hyperlinks in the line. */
this->FindHyperlinksInMarkdown(line, line_index);
/* All lines beginning with # are headings. */
if (!line.text.empty() && line.text[0] == '#') {
this->jumplist.push_back(line_index);
this->lines[line_index].colour = TC_GOLD;
this->link_anchors.emplace_back(Hyperlink{ line_index, 0, 0, MakeAnchorSlug(line.text) });
}
}
}
/* virtual */ void TextfileWindow::OnClick(Point pt, int widget, int click_count)
{
switch (widget) {
@@ -137,6 +524,29 @@ void TextfileWindow::SetupScrollbars(bool force_reflow)
this->ToggleWidgetLoweredState(WID_TF_WRAPTEXT);
this->InvalidateData();
break;
case WID_TF_JUMPLIST: {
DropDownList list;
for (size_t line : this->jumplist) {
SetDParamStr(0, this->lines[line].text);
DropDownListStringItem *item = new DropDownListStringItem(STR_TEXTFILE_JUMPLIST_ITEM, (int)line, false);
list.emplace_back(item);
}
ShowDropDownList(this, std::move(list), -1, widget);
break;
}
case WID_TF_NAVBACK:
this->NavigateHistory(-1);
break;
case WID_TF_NAVFORWARD:
this->NavigateHistory(+1);
break;
case WID_TF_BACKGROUND:
this->CheckHyperlinkClick(pt);
break;
}
}
@@ -162,9 +572,9 @@ void TextfileWindow::SetupScrollbars(bool force_reflow)
int y_offset = (line.top - pos) * line_height;
if (IsWidgetLowered(WID_TF_WRAPTEXT)) {
DrawStringMultiLine(0, fr.right, y_offset, fr.bottom, line.text, TC_BLACK, SA_TOP | SA_LEFT, false, FS_MONO);
DrawStringMultiLine(0, fr.right, y_offset, fr.bottom, line.text, line.colour, SA_TOP | SA_LEFT, false, FS_MONO);
} else {
DrawString(-this->hscroll->GetPosition(), fr.right, y_offset, line.text, TC_BLACK, SA_TOP | SA_LEFT, false, FS_MONO);
DrawString(-this->hscroll->GetPosition(), fr.right, y_offset, line.text, line.colour, SA_TOP | SA_LEFT, false, FS_MONO);
}
}
}
@@ -184,6 +594,26 @@ void TextfileWindow::SetupScrollbars(bool force_reflow)
this->SetupScrollbars(true);
}
void TextfileWindow::OnDropdownSelect(int widget, int index)
{
if (widget != WID_TF_JUMPLIST) return;
this->ScrollToLine(index);
}
void TextfileWindow::ScrollToLine(size_t line)
{
Scrollbar *sb = this->GetScrollbar(WID_TF_VSCROLLBAR);
int newpos;
if (this->IsWidgetLowered(WID_TF_WRAPTEXT)) {
newpos = this->lines[line].top;
} else {
newpos = static_cast<int>(line);
}
sb->SetPosition(std::min(newpos, sb->GetCount() - sb->GetCapacity()));
this->SetDirty();
}
/* virtual */ void TextfileWindow::Reset()
{
this->search_iterator = 0;
@@ -330,14 +760,20 @@ static void Xunzip(byte **bufp, size_t *sizep)
*/
/* virtual */ void TextfileWindow::LoadTextfile(const char *textfile, Subdirectory dir)
{
if (textfile == nullptr) return;
this->lines.clear();
this->jumplist.clear();
this->GetWidget<NWidgetStacked>(WID_TF_SEL_JUMPLIST)->SetDisplayedPlane(SZSP_HORIZONTAL);
this->ReInit();
if (textfile == nullptr) return;
/* Get text from file */
size_t filesize;
FILE *handle = FioFOpenFile(textfile, "rb", dir, &filesize);
if (handle == nullptr) return;
/* Early return on empty files. */
if (filesize == 0) return;
char *buf = MallocT<char>(filesize);
size_t read = fread(buf, 1, filesize, handle);
@@ -370,7 +806,13 @@ static void Xunzip(byte **bufp, size_t *sizep)
/* Check for the byte-order-mark, and skip it if needed. */
if (StrStartsWith(sv_buf, u8"\ufeff")) sv_buf.remove_prefix(3);
/* Replace any invalid characters with a question-mark. This copies the buf in the process. */
/* Update the filename. */
this->filepath = textfile;
this->filename = this->filepath.substr(this->filepath.find_last_of(PATHSEP) + 1);
/* If it's the first file being loaded, add to history. */
if (this->history.empty()) this->history.push_back(HistoryEntry{ this->filepath, 0 });
/* Process the loaded text into lines, and do any further parsing needed. */
this->LoadText(sv_buf);
free(buf);
}
@@ -384,11 +826,11 @@ static void Xunzip(byte **bufp, size_t *sizep)
*/
void TextfileWindow::LoadText(std::string_view buf)
{
this->text = StrMakeValid(buf, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE | SVS_REPLACE_TAB_CR_NL_WITH_SPACE);
std::string text = StrMakeValid(buf, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE | SVS_REPLACE_TAB_CR_NL_WITH_SPACE);
this->lines.clear();
/* Split the string on newlines. */
std::string_view p(this->text);
std::string_view p(text);
int row = 0;
auto next = p.find_first_of('\n');
while (next != std::string_view::npos) {
@@ -407,6 +849,8 @@ void TextfileWindow::LoadText(std::string_view buf)
}
this->max_length = max_length;
this->AfterLoadText();
CheckForMissingGlyphs(true, this);
}
@@ -426,6 +870,9 @@ const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filenam
};
static_assert(lengthof(prefixes) == TFT_CONTENT_END);
/* Only the generic text file types allowed for this function */
if (type >= TFT_CONTENT_END) return nullptr;
const char *prefix = prefixes[type];
if (filename == nullptr) return nullptr;
@@ -438,11 +885,14 @@ const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filenam
static const char * const exts[] = {
"txt",
"md",
#if defined(WITH_ZLIB)
"txt.gz",
"md.gz",
#endif
#if defined(WITH_LIBLZMA)
"txt.xz",
"md.xz",
#endif
};