The Battle for Wesnoth  1.17.0-dev
help_browser.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 by Charles Dang <exodia339@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "game_config_manager.hpp"
21 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/image.hpp"
25 #include "gui/widgets/settings.hpp"
26 #include "gui/widgets/settings.hpp"
27 #include "gui/widgets/text_box.hpp"
30 #include "gui/widgets/window.hpp"
31 
32 #ifdef GUI2_EXPERIMENTAL_LISTBOX
33 #include "gui/widgets/list.hpp"
34 #else
35 #include "gui/widgets/listbox.hpp"
36 #endif
37 
38 #include "help/help.hpp"
39 
40 namespace gui2::dialogs
41 {
42 
43 REGISTER_DIALOG(help_browser)
44 
46  : initial_topic_("introduction")
47  , help_cfg_(game_config_manager::get()->game_config().child("help"))
48 {
49 }
50 
52 {
53  tree_view& topic_tree = find_widget<tree_view>(&window, "topic_tree", false);
54  multi_page& topic_pages = find_widget<multi_page>(&window, "topic_text_pages", false);
55 
57  std::bind(&help_browser::on_topic_select, this));
58 
59  window.keyboard_capture(&topic_tree);
60 
61  unsigned id = 0;
62 
63  for(const auto& topic : help_cfg_.child_range("topic")) {
64  std::map<std::string, string_map> data;
66 
67  item["label"] = topic["title"];
68  data.emplace("topic_name", item);
69 
70  topic_tree.add_node("topic", data).set_id(std::to_string(id));
71 
72  // FIXME: maybe using a multi page isn't a good idea here... :| it causes massive lag when opening.
73  item.clear();
74  data.clear();
75 
76  item["label"] = topic["text"].empty() ? "" : topic["text"].str();
77  data.emplace("topic_text", item);
78 
79  topic_pages.add_page(data);
80 
81  ++id;
82  }
83 
85 }
86 
88 {
89  tree_view& tree = find_widget<tree_view>(get_window(), "topic_tree", false);
90 
91  if(tree.empty()) {
92  return;
93  }
94 
95  assert(tree.selected_item());
96 
97  if(tree.selected_item()->id().empty()) {
98  return;
99  }
100 
101  const unsigned topic_i = lexical_cast<unsigned>(tree.selected_item()->id());
102  find_widget<multi_page>(get_window(), "topic_text_pages", false).select_page(topic_i);
103 
104 }
105 
106 } // namespace dialogs
const std::string & id() const
Definition: widget.cpp:109
This file contains the window object, this object is a top level container which has the event manage...
child_itors child_range(config_key_type key)
Definition: config.cpp:360
tree_view_node * selected_item()
Definition: tree_view.hpp:115
window * get_window() const
Returns a pointer to the dialog&#39;s window.
To lexical_cast(From value)
Lexical cast converts one type to another.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:186
This file contains the settings handling of the widget library.
A tree view is a control that holds several items of the same or different types. ...
Definition: tree_view.hpp:59
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:214
Game configuration data as global variables.
Definition: build_info.cpp:58
CURSOR_TYPE get()
Definition: cursor.cpp:215
Help browser dialog.
std::map< std::string, t_string > string_map
Definition: widget.hpp:25
A multi page is a control that contains several &#39;pages&#39; of which only one is visible.
Definition: multi_page.hpp:48
bool empty() const
Definition: tree_view.cpp:110
void set_id(const std::string &id)
Definition: widget.cpp:97
Definition: help.cpp:56
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
tree_view_node & add_node(const std::string &id, const std::map< std::string, string_map > &data, const int index=-1)
Definition: tree_view.cpp:56
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:409