The Battle for Wesnoth  1.15.12+dev
menu_button.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
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 
22 
23 class config;
24 
25 namespace gui2
26 {
27 namespace implementation
28 {
29  struct builder_menu_button;
30 }
31 
32 // ------------ WIDGET -----------{
33 
34 /**
35  * @ingroup GUIWidgetWML
36  *
37  * A menu_button is a styled_widget to choose an element from a list of elements.
38  *
39  * When a menu_button has a return value it sets the return value for the window.
40  * Normally this closes the window and returns this value to the caller.
41  * The return value can either be defined by the user or determined from the id of the menu_button.
42  * The return value has a higher precedence as the one defined by the id.
43  * (Of course it's weird to give a menu_button an id and then override its return value.)
44  *
45  * When the menu_button doesn't have a standard id, but you still want to use the return value of that id, use return_value_id instead.
46  * This has a higher precedence as return_value.
47  *
48  * List with the menu_button specific variables:
49  * Key |Type |Default |Description
50  * ---------------|------------------------------------|---------|-----------
51  * return_value_id| @ref guivartype_string "string" |"" |The return value id.
52  * return_value | @ref guivartype_int "int" |0 |The return value.
53  *
54  * The following states exist:
55  * * state_enabled - the menu_button is enabled.
56  * * state_disabled - the menu_button is disabled.
57  * * state_pressed - the left mouse menu_button is down.
58  * * state_focused - the mouse is over the menu_button.
59  */
61 {
62 public:
63  explicit menu_button(const implementation::builder_menu_button& builder);
64 
65  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
66 
67  /** See @ref styled_widget::set_active. */
68  virtual void set_active(const bool active) override;
69 
70  /** See @ref styled_widget::get_active. */
71  virtual bool get_active() const override;
72 
73  /** See @ref styled_widget::get_state. */
74  virtual unsigned get_state() const override;
75 
76  /***** ***** ***** setters / getters for members ***** ****** *****/
77 
78  void set_values(const std::vector<::config>& values, unsigned selected = 0);
79 
80  void set_selected(unsigned selected, bool fire_event = true);
81 
82  /** Inherited from selectable_item */
83  virtual unsigned get_value() const override { return selected_; }
84 
85  /** Inherited from selectable_item */
86  virtual void set_value(unsigned value, bool fire_event = false) override { set_selected(value, fire_event); }
87 
88  /** Inherited from selectable_item */
89  virtual unsigned num_states() const override { return values_.size(); }
90 
91  /** Returns the value of the selected row */
92  std::string get_value_string() const
93  {
94  return values_[selected_]["label"];
95  }
96 
97  /** Returns the entire config object for the selected row. */
98  const ::config& get_value_config() const
99  {
100  return values_[selected_];
101  }
102 
103  void set_keep_open(const bool keep_open)
104  {
105  keep_open_ = keep_open;
106  }
107 
108 private:
109  /**
110  * Possible states of the widget.
111  *
112  * Note the order of the states must be the same as defined in settings.hpp.
113  */
114  enum state_t {
119  };
120 
121  void set_state(const state_t state);
122 
123  /**
124  * Current state of the widget.
125  *
126  * The state of the widget determines what to render and how the widget
127  * reacts to certain 'events'.
128  */
130 
131  std::vector<::config> values_;
132 
133  unsigned selected_;
134 
136 
137 public:
138  /** Static type getter that does not rely on the widget being constructed. */
139  static const std::string& type();
140 
141 private:
142  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
143  virtual const std::string& get_control_type() const override;
144 
145  /***** ***** ***** signal handlers ***** ****** *****/
146 
147  void signal_handler_mouse_enter(const event::ui_event event, bool& handled);
148 
149  void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
150 
151  void signal_handler_left_button_down(const event::ui_event event, bool& handled);
152 
153  void signal_handler_left_button_up(const event::ui_event event, bool& handled);
154 
155  void signal_handler_left_button_click(const event::ui_event event, bool& handled);
156 
157  void signal_handler_sdl_wheel_up(const event::ui_event event, bool& handled);
158 
159  void signal_handler_sdl_wheel_down(const event::ui_event event, bool& handled);
160 };
161 
162 // }---------- DEFINITION ---------{
163 
165 {
166  explicit menu_button_definition(const config& cfg);
167 
169  {
170  explicit resolution(const config& cfg);
171  };
172 };
173 
174 // }---------- BUILDER -----------{
175 
176 class styled_widget;
177 
178 namespace implementation
179 {
180 
182 {
183 public:
184  explicit builder_menu_button(const config& cfg);
185 
187 
188  virtual widget* build() const override;
189 
190 private:
191  std::vector<::config> options_;
192 };
193 
194 } // namespace implementation
195 
196 // }------------ END --------------
197 
198 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
Small abstract helper class.
std::string get_value_string() const
Returns the value of the selected row.
Definition: menu_button.hpp:92
virtual widget * build() const =0
const ::config & get_value_config() const
Returns the entire config object for the selected row.
Definition: menu_button.hpp:98
A menu_button is a styled_widget to choose an element from a list of elements.
Definition: menu_button.hpp:60
Base class for all widgets.
Definition: widget.hpp:49
virtual void set_value(unsigned value, bool fire_event=false) override
Inherited from selectable_item.
Definition: menu_button.hpp:86
Generic file dialog.
Definition: field-fwd.hpp:22
virtual unsigned get_value() const override
Inherited from selectable_item.
Definition: menu_button.hpp:83
std::string selected
state_t
Possible states of the widget.
state_t state_
Current state of the widget.
bool fire_event(const ui_event event, std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
std::vector<::config > values_
void set_keep_open(const bool keep_open)
Base class for all visible items.
point resolution()
Definition: general.cpp:387
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
virtual unsigned num_states() const override
Inherited from selectable_item.
Definition: menu_button.hpp:89
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
ui_event
The event send to the dispatcher.
Definition: handler.hpp:47