The Battle for Wesnoth  1.19.0-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"
25 #include "gui/widgets/window.hpp"
26 
27 #ifdef GUI2_EXPERIMENTAL_LISTBOX
28 #include "gui/widgets/list.hpp"
29 #else
30 #endif
31 
32 
33 namespace gui2::dialogs
34 {
35 
36 REGISTER_DIALOG(help_browser)
37 
39  : modal_dialog(window_id())
40  , initial_topic_("introduction")
41  , help_cfg_(game_config_manager::get()->game_config().mandatory_child("help"))
42 {
43 }
44 
46 {
47  tree_view& topic_tree = find_widget<tree_view>(&window, "topic_tree", false);
48  multi_page& topic_pages = find_widget<multi_page>(&window, "topic_text_pages", false);
49 
51  std::bind(&help_browser::on_topic_select, this));
52 
53  window.keyboard_capture(&topic_tree);
54 
55  unsigned id = 0;
56 
57  for(const auto& topic : help_cfg_.child_range("topic")) {
60 
61  item["label"] = topic["title"];
62  data.emplace("topic_name", item);
63 
64  topic_tree.add_node("topic", data).set_id(std::to_string(id));
65 
66  // FIXME: maybe using a multi page isn't a good idea here... :| it causes massive lag when opening.
67  item.clear();
68  data.clear();
69 
70  item["label"] = topic["text"].empty() ? "" : topic["text"].str();
71  data.emplace("topic_text", item);
72 
73  topic_pages.add_page(data);
74 
75  ++id;
76  }
77 
79 }
80 
82 {
83  tree_view& tree = find_widget<tree_view>(get_window(), "topic_tree", false);
84 
85  if(tree.empty()) {
86  return;
87  }
88 
89  assert(tree.selected_item());
90 
91  if(tree.selected_item()->id().empty()) {
92  return;
93  }
94 
95  const unsigned topic_i = lexical_cast<unsigned>(tree.selected_item()->id());
96  find_widget<multi_page>(get_window(), "topic_text_pages", false).select_page(topic_i);
97 
98 }
99 
100 } // namespace dialogs
child_itors child_range(config_key_type key)
Definition: config.cpp:273
Help browser dialog.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Abstract base class for all modal dialogs.
window * get_window()
Returns a pointer to the dialog's window.
A multi page is a control that contains several 'pages' of which only one is visible.
Definition: multi_page.hpp:50
grid & add_page(const widget_item &item)
Adds single page to the grid.
Definition: multi_page.cpp:43
A tree view is a control that holds several items of the same or different types.
Definition: tree_view.hpp:60
bool empty() const
Definition: tree_view.cpp:99
tree_view_node & add_node(const std::string &id, const widget_data &data, const int index=-1)
Definition: tree_view.cpp:56
tree_view_node * selected_item()
Definition: tree_view.hpp:109
void set_id(const std::string &id)
Definition: widget.cpp:98
const std::string & id() const
Definition: widget.cpp:110
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
void keyboard_capture(widget *widget)
Definition: window.cpp:1221
This file contains the window object, this object is a top level container which has the event manage...
CURSOR_TYPE get()
Definition: cursor.cpp:216
Game configuration data as global variables.
Definition: build_info.cpp:60
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
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
Definition: help.cpp:53
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:412
std::string_view data
Definition: picture.cpp:194