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