The Battle for Wesnoth  1.17.0-dev
help_menu.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
3  by David White <dave@whitevine.net>
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 #pragma once
17 
18 #include <set> // for set
19 #include <string> // for string, basic_string
20 #include <vector> // for vector
21 #include "widgets/menu.hpp" // for menu
22 
23 class CVideo;
24 struct surface_restorer;
25 
26 namespace help { struct section; }
27 namespace help { struct topic; }
28 
29 namespace help {
30 
31 /**
32  * The menu to the left in the help browser, where topics can be
33  * navigated through and chosen.
34  */
35 class help_menu : public gui::menu
36 {
37 public:
38  help_menu(CVideo &video, const section &toplevel, int max_height=-1);
39  int process();
40 
41  /**
42  * Make the topic the currently selected one, and expand all
43  * sections that need to be expanded to show it.
44  */
45  void select_topic(const topic &t);
46 
47  /**
48  * If a topic has been chosen, return that topic, otherwise
49  * nullptr. If one topic is returned, it will not be returned again,
50  * if it is not re-chosen.
51  */
52  const topic *chosen_topic();
53 
54 private:
55  /** Information about an item that is visible in the menu. */
56  struct visible_item {
57  visible_item(const section *_sec, const std::string &visible_string, int level);
58  visible_item(const topic *_t, const std::string &visible_string, int level);
59  // Invariant, one if these should be nullptr. The constructors
60  // enforce it.
61  const topic *t;
62  const section *sec;
63  std::string visible_string;
64  int level;
65  bool operator==(const visible_item &vis_item) const;
66  bool operator==(const section &sec) const;
67  bool operator==(const topic &t) const;
68  };
69 
70  /** Regenerate what items are visible by checking what sections are expanded. */
71  void update_visible_items(const section &top_level, unsigned starting_level=0);
72 
73  /** Return true if the section is expanded. */
74  bool expanded(const section &sec);
75 
76  /** Mark a section as expanded. Do not update the visible items or anything. */
77  void expand(const section &sec);
78 
79  /**
80  * Contract (close) a section. That is, mark it as not expanded,
81  * visible items are not updated.
82  */
83  void contract(const section &sec);
84 
85  /**
86  * Return the string to use as the prefix for the icon part of the
87  * menu-string at the specified level.
88  */
89  std::string indent_list(const std::string &icon, const unsigned level);
90  /** Return the string to use as the menu-string for sections at the specified level. */
91  std::string get_string_to_show(const section &sec, const unsigned level);
92  /** Return the string to use as the menu-string for topics at the specified level. */
93  std::string get_string_to_show(const topic &topic, const unsigned level);
94 
95  /** Draw the currently visible items. */
96  void display_visible_items();
97 
98  /**
99  * Internal recursive thingie. did_expand will be true if any
100  * section was expanded, otherwise untouched.
101  */
102  bool select_topic_internal(const topic &t, const section &sec);
103 
104  std::vector<visible_item> visible_items_;
106  std::set<const section*> expanded_;
110 };
111 
112 } // end namespace help
void contract(const section &sec)
Contract (close) a section.
Definition: help_menu.cpp:61
surface_restorer restorer_
Definition: help_menu.hpp:107
std::string get_string_to_show(const section &sec, const unsigned level)
Return the string to use as the menu-string for sections at the specified level.
Definition: help_menu.cpp:101
visible_item selected_item_
Definition: help_menu.hpp:109
topic const * chosen_topic_
Definition: help_menu.hpp:108
A section contains topics and sections along with title and ID.
Definition: help_impl.hpp:146
Definition: video.hpp:32
bool expanded(const section &sec)
Return true if the section is expanded.
Definition: help_menu.cpp:49
visible_item(const section *_sec, const std::string &visible_string, int level)
Definition: help_menu.cpp:219
void display_visible_items()
Draw the currently visible items.
Definition: help_menu.cpp:206
const section & toplevel_
Definition: help_menu.hpp:105
std::string indent_list(const std::string &icon, const unsigned level)
Return the string to use as the prefix for the icon part of the menu-string at the specified level...
Definition: help_menu.cpp:91
Information about an item that is visible in the menu.
Definition: help_menu.hpp:56
void expand(const section &sec)
Mark a section as expanded.
Definition: help_menu.cpp:54
help_menu(CVideo &video, const section &toplevel, int max_height=-1)
Definition: help_menu.cpp:33
std::set< const section * > expanded_
Definition: help_menu.hpp:106
std::vector< visible_item > visible_items_
Definition: help_menu.hpp:104
bool operator==(const visible_item &vis_item) const
Definition: help_menu.cpp:235
void select_topic(const topic &t)
Make the topic the currently selected one, and expand all sections that need to be expanded to show i...
Definition: help_menu.cpp:137
CVideo & video() const
Definition: widget.hpp:84
double t
Definition: astarsearch.cpp:65
The menu to the left in the help browser, where topics can be navigated through and chosen...
Definition: help_menu.hpp:35
A topic contains a title, an id and some text.
Definition: help_impl.hpp:112
void update_visible_items(const section &top_level, unsigned starting_level=0)
Regenerate what items are visible by checking what sections are expanded.
Definition: help_menu.cpp:68
const topic * chosen_topic()
If a topic has been chosen, return that topic, otherwise nullptr.
Definition: help_menu.cpp:199
Definition: help.cpp:57
bool select_topic_internal(const topic &t, const section &sec)
Internal recursive thingie.
Definition: help_menu.cpp:117