The Battle for Wesnoth  1.15.1+dev
selectable_item.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 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 
17 #include "utils/functional.hpp"
18 #include <cassert>
19 
20 namespace gui2
21 {
22 
23 class widget;
24 
25 /**
26  * Small abstract helper class.
27  *
28  * Parts of the engine inherit this class so we can have generic
29  * selectable items.
30  */
32 {
33 public:
34  virtual ~selectable_item()
35  {
36  }
37 
38  /** Is the styled_widget selected? */
39  virtual unsigned get_value() const = 0;
40 
41  /** Select the styled_widget. */
42  virtual void set_value(unsigned value, bool fire_event = false) = 0;
43 
44  /** The number of states, that is 2 for normal buttons, 3 for tristate buttons. */
45  virtual unsigned num_states() const = 0;
46 
47  bool get_value_bool() const
48  {
49  assert(num_states() == 2);
50  return get_value() != 0;
51  }
52 
53  void set_value_bool(bool value, bool fire_event = false)
54  {
55  assert(num_states() == 2);
56  return set_value(value, fire_event);
57  }
58 };
59 
60 } // namespace gui2
void set_value_bool(bool value, bool fire_event=false)
Small abstract helper class.
virtual unsigned num_states() const =0
The number of states, that is 2 for normal buttons, 3 for tristate buttons.
Generic file dialog.
Definition: field-fwd.hpp:22
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.
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?