The Battle for Wesnoth  1.19.0-dev
selectable_item.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2024
3  by Mark de Wever <koraq@xs4all.nl>
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 <functional>
19 #include <cassert>
20 
21 namespace gui2
22 {
23 
24 class widget;
25 
26 /**
27  * Small abstract helper class.
28  *
29  * Parts of the engine inherit this class so we can have generic
30  * selectable items.
31  */
33 {
34 public:
35  virtual ~selectable_item()
36  {
37  }
38 
39  /** Is the styled_widget selected? */
40  virtual unsigned get_value() const = 0;
41 
42  /** Select the styled_widget. */
43  virtual void set_value(unsigned value, bool fire_event = false) = 0;
44 
45  /** The number of states, that is 2 for normal buttons, 3 for tristate buttons. */
46  virtual unsigned num_states() const = 0;
47 
48  bool get_value_bool() const
49  {
50  assert(num_states() == 2);
51  return get_value() != 0;
52  }
53 
54  void set_value_bool(bool value, bool fire_event = false)
55  {
56  assert(num_states() == 2);
57  return set_value(value, fire_event);
58  }
59 };
60 
61 } // namespace gui2
Small abstract helper class.
void set_value_bool(bool value, bool fire_event=false)
virtual void set_value(unsigned value, bool fire_event=false)=0
Select the styled_widget.
virtual unsigned get_value() const =0
Is the styled_widget selected?
virtual unsigned num_states() const =0
The number of states, that is 2 for normal buttons, 3 for tristate buttons.
bool fire_event(const ui_event event, const std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
Generic file dialog.