The Battle for Wesnoth  1.19.5+dev
help_browser.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
3  by Charles Dang <exodia339@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include "game_config_manager.hpp"
21 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/image.hpp"
23 #include "gui/widgets/label.hpp"
27 #include "gui/widgets/settings.hpp"
28 #include "gui/widgets/text_box.hpp"
32 #include "gui/widgets/window.hpp"
33 #include "log.hpp"
34 #include "video.hpp"
35 
36 #ifdef GUI2_EXPERIMENTAL_LISTBOX
37 #include "gui/widgets/list.hpp"
38 #else
39 #include "gui/widgets/listbox.hpp"
40 #endif
41 
42 #include "help/help.hpp"
43 #include "help/help_impl.hpp"
44 #include "font/pango/escape.hpp"
45 #include "font/pango/hyperlink.hpp"
46 
47 namespace gui2::dialogs
48 {
49 
50 REGISTER_DIALOG(help_browser)
51 
52 help_browser::help_browser(const help::section& toplevel, const std::string& initial)
53  : modal_dialog(window_id())
54  , initial_topic_(initial.empty() ? help::default_show_topic : initial)
55  , toplevel_(toplevel)
56  , history_()
57  , history_pos_(0)
58 {
59  if(initial_topic_.compare(0, 2, "..") == 0) {
60  initial_topic_.replace(0, 2, "+");
61  } else {
62  initial_topic_.insert(0, "-");
63  }
65 }
66 
68 {
69  tree_view& topic_tree = find_widget<tree_view>("topic_tree");
70 
71  button& back_button = find_widget<button>("back");
72  button& next_button = find_widget<button>("next");
73 
74  rich_label& topic_text = find_widget<rich_label>("topic_text");
75 
76  next_button.set_active(false);
77  back_button.set_active(false);
78  connect_signal_mouse_left_click(back_button, std::bind(&help_browser::on_history_navigate, this, true));
79  connect_signal_mouse_left_click(next_button, std::bind(&help_browser::on_history_navigate, this, false));
80 
81  toggle_button& contents = find_widget<toggle_button>("contents");
82 
83  if (video::window_size().x <= 800) {
84  contents.set_value(false);
85  connect_signal_mouse_left_click(contents, std::bind([&]() {
86  topic_tree.set_visible(topic_tree.get_visible() == widget::visibility::visible
90  }));
92  } else {
93  contents.set_value(true);
95  }
96 
97  topic_text.register_link_callback(std::bind(&help_browser::show_topic, this, std::placeholders::_1, true));
98 
100 
101  keyboard_capture(&topic_tree);
102 
104 
105  tree_view_node& initial_node = topic_tree.find_widget<tree_view_node>(initial_topic_);
106  initial_node.select_node(true);
107 
108  on_topic_select();
109 }
110 
111 void help_browser::add_topics_for_section(const help::section& parent_section, tree_view_node& parent_node)
112 {
113  for(const help::section& section : parent_section.sections) {
114  tree_view_node& section_node = add_topic(section.id, section.title, true, parent_node);
115 
116  add_topics_for_section(section, section_node);
117  }
118 
119  for(const help::topic& topic : parent_section.topics) {
120  if(topic.id.compare(0, 2, "..") != 0) {
121  add_topic(topic.id, topic.title, false, parent_node);
122  }
123  }
124 }
125 
126 tree_view_node& help_browser::add_topic(const std::string& topic_id, const std::string& topic_title,
127  bool expands, tree_view_node& parent)
128 {
130  widget_item item;
131 
132  item["label"] = topic_title;
133  data.emplace("topic_name", item);
134 
135  tree_view_node& new_node = parent.add_child(expands ? "section" : "topic", data);
136  new_node.set_id(std::string(expands ? "+" : "-") + topic_id);
137 
138  return new_node;
139 }
140 void help_browser::show_topic(std::string topic_id, bool add_to_history)
141 {
142  if(topic_id.empty()) {
143  return;
144  }
145 
146  if(topic_id[0] == '+') {
147  topic_id.replace(topic_id.begin(), topic_id.begin() + 1, 2, '.');
148  }
149 
150  if(topic_id[0] == '-') {
151  topic_id.erase(topic_id.begin(), topic_id.begin() + 1);
152  }
153 
154  auto iter = parsed_pages_.find(topic_id);
155  if(iter == parsed_pages_.end()) {
156  const help::topic* topic = help::find_topic(toplevel_, topic_id);
157  if(!topic) {
158  ERR_GUI_P << "Help browser tried to show topic with id '" << topic_id
159  << "' but that topic could not be found." << std::endl;
160  return;
161  }
162 
164  widget_item item;
165 
166  item["label"] = topic->title;
167  data.emplace("topic_title", item);
168 
169  find_widget<label>("topic_title").set_label(topic->title);
170  find_widget<rich_label>("topic_text").set_topic(topic);
171 
173  scrollbar_panel& scroll = find_widget<scrollbar_panel>("topic_scroll_panel");
175  }
176 
177  if (add_to_history) {
178  // history pos is 0 initially, so it's already at first entry
179  // no need increment first time
180  if (!history_.empty()) {
181  history_pos_++;
182  }
183  history_.push_back(topic_id);
184 
185  find_widget<button>("back").set_active(history_pos_ != 0);
186 
187  }
188 }
189 
191 {
192  tree_view& topic_tree = find_widget<tree_view>("topic_tree");
193 
194  if(topic_tree.empty()) {
195  return;
196  }
197 
198  tree_view_node* selected = topic_tree.selected_item();
199  assert(selected);
200 
201  if (selected->id()[0] != '+' && video::window_size().x <= 800) {
202  find_widget<toggle_button>("contents").set_value(false);
204  }
205 
206  show_topic(selected->id());
207 }
208 
210 {
211  if(backwards) {
212  history_pos_--;
213  } else {
214  history_pos_++;
215  }
216  find_widget<button>("back").set_active(!history_.empty() && history_pos_ != 0);
217  find_widget<button>("next").set_active(!history_.empty() && history_pos_ != (history_.size()-1));
218 
219  const std::string topic_id = history_.at(history_pos_);
220  show_topic(topic_id, false);
221 }
222 
223 } // namespace dialogs
Simple push button.
Definition: button.hpp:36
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:64
Help browser dialog.
const help::section & toplevel_
void show_topic(std::string topic_id, bool add_to_history=true)
std::map< std::string, int > parsed_pages_
virtual void pre_show() override
Actions to be taken before showing the window.
void on_history_navigate(bool backwards)
tree_view_node & add_topic(const std::string &topic_id, const std::string &topic_title, bool expands, tree_view_node &parent)
std::vector< std::string > history_
void add_topics_for_section(const help::section &parent_section, tree_view_node &parent_node)
Abstract base class for all modal dialogs.
A rich_label takes marked up text and shows it correctly formatted and wrapped but no scrollbars are ...
Definition: rich_label.hpp:40
void register_link_callback(std::function< void(std::string)> link_handler)
Definition: rich_label.cpp:811
@ BEGIN
Go to begin position.
Definition: scrollbar.hpp:53
void scroll_vertical_scrollbar(const scrollbar_base::scroll_mode scroll)
Scrolls the vertical scrollbar.
virtual void set_value(unsigned selected, bool fire_event=false) override
Inherited from selectable_item.
void select_node(bool expand_parents=false)
const tree_view_node & get_root_node() const
Definition: tree_view.hpp:53
bool empty() const
Definition: tree_view.cpp:99
tree_view_node * selected_item()
Definition: tree_view.hpp:98
NOT_DANGLING T * find_widget(const std::string &id, const bool must_be_active, const bool must_exist)
Gets a widget with the wanted id.
Definition: widget.hpp:742
void set_visible(const visibility visible)
Definition: widget.cpp:479
void set_id(const std::string &id)
Definition: widget.cpp:98
visibility get_visible() const
Definition: widget.cpp:506
@ visible
The user sets the widget visible, that means:
@ invisible
The user set the widget invisible, that means:
widget * parent()
Definition: widget.cpp:170
void keyboard_capture(widget *widget)
Definition: window.cpp:1207
void invalidate_layout()
Updates the size of the window.
Definition: window.cpp:761
#define ERR_GUI_P
Definition: log.hpp:69
This file contains the window object, this object is a top level container which has the event manage...
Standard logging facilities (interface).
std::string selected
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:203
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:36
std::map< std::string, t_string > widget_item
Definition: widget.hpp:33
void init_help()
Definition: help.cpp:178
const topic * find_topic(const section &sec, const std::string &id)
Search for the topic with the specified identifier in the section and its subsections.
Definition: help_impl.cpp:1294
const std::string default_show_topic
Definition: help_impl.cpp:81
point window_size()
Returns the size of the window in display units / screen coordinates.
Definition: video.cpp:419
std::string_view data
Definition: picture.cpp:178
This file contains the settings handling of the widget library.
A section contains topics and sections along with title and ID.
Definition: help_impl.hpp:148
section_list sections
Definition: help_impl.hpp:168
std::string id
Definition: help_impl.hpp:166
std::string title
Definition: help_impl.hpp:166
topic_list topics
Definition: help_impl.hpp:167
A topic contains a title, an id and some text.
Definition: help_impl.hpp:115
std::string id
Definition: help_impl.hpp:139
std::string title
Definition: help_impl.hpp:139