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 
17 #include "gui/dialogs/help_browser.hpp"
18 
19 #include "game_config_manager.hpp"
20 #include "gui/auxiliary/find_widget.hpp"
21 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/image.hpp"
23 #include "gui/widgets/multi_page.hpp"
24 #include "gui/widgets/scroll_label.hpp"
25 #include "gui/widgets/settings.hpp"
26 #include "gui/widgets/settings.hpp"
27 #include "gui/widgets/text_box.hpp"
28 #include "gui/widgets/tree_view.hpp"
29 #include "gui/widgets/tree_view_node.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
41 {
42 namespace dialogs
43 {
44 
REGISTER_DIALOG(help_browser)45 REGISTER_DIALOG(help_browser)
46 
47 help_browser::help_browser()
48 	: initial_topic_("introduction")
49 	, help_cfg_(game_config_manager::get()->game_config().child("help"))
50 {
51 }
52 
pre_show(window & window)53 void help_browser::pre_show(window& window)
54 {
55 	tree_view& topic_tree = find_widget<tree_view>(&window, "topic_tree", false);
56 	multi_page& topic_pages = find_widget<multi_page>(&window, "topic_text_pages", false);
57 
58 	topic_tree.set_selection_change_callback(std::bind(&help_browser::on_topic_select, this, std::ref(window)));
59 
60 	window.keyboard_capture(&topic_tree);
61 
62 	unsigned id = 0;
63 
64 	for(const auto& topic : help_cfg_.child_range("topic")) {
65 		std::map<std::string, string_map> data;
66 		string_map item;
67 
68 		item["label"] = topic["title"];
69 		data.emplace("topic_name", item);
70 
71 		topic_tree.add_node("topic", data).set_id(std::to_string(id));
72 
73 		// FIXME: maybe using a multi page isn't a good idea here... :| it causes massive lag when opening.
74 		item.clear();
75 		data.clear();
76 
77 		item["label"] = topic["text"].empty() ? "" : topic["text"].str();
78 		data.emplace("topic_text", item);
79 
80 		topic_pages.add_page(data);
81 
82 		++id;
83 	}
84 
85 	on_topic_select(window);
86 }
87 
on_topic_select(window & window)88 void help_browser::on_topic_select(window& window)
89 {
90 	tree_view& tree = find_widget<tree_view>(&window, "topic_tree", false);
91 
92 	if(tree.empty()) {
93 		return;
94 	}
95 
96 	assert(tree.selected_item());
97 
98 	if(tree.selected_item()->id().empty()) {
99 		return;
100 	}
101 
102 	const unsigned topic_i = lexical_cast<unsigned>(tree.selected_item()->id());
103 	find_widget<multi_page>(&window, "topic_text_pages", false).select_page(topic_i);
104 
105 }
106 
107 } // namespace dialogs
108 } // namespace gui2
109