The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
group.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 The Battle for Wesnoth Project http://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #ifndef GUI_WIDGETS_GROUP_HPP_INCLUDED
15 #define GUI_WIDGETS_GROUP_HPP_INCLUDED
16 
19 #include "gui/widgets/widget.hpp"
20 #include "utils/iterable_pair.hpp"
21 
22 #include <vector>
23 #include "utils/functional.hpp"
24 
25 namespace gui2
26 {
27 
28 template <class T>
29 class tgroup
30 {
31 public:
32  typedef typename std::pair<tselectable_*, T> group_type;
33  typedef typename std::vector<group_type> group_list;
35  typedef typename group_list::const_iterator group_iterator_const;
36 
37  /**
38  * Adds a widget/value pair to the group vector. A callback is set
39  * that sets all members' toggle states to false when clicked. This
40  * happens before individual widget handlers fire, meaning that the
41  * clicked widget will remain the only one selected.
42  */
43  void add_member(tselectable_* widget, const T& value)
44  {
45  members_.push_back(std::make_pair(widget, value));
46 
47  dynamic_cast<twidget*>(widget)->connect_signal<event::LEFT_BUTTON_CLICK>(std::bind(
49  }
50 
51  /**
52  * Removes a member from the group vector.
53  */
55  {
56  members_.erase(std::find_if(members_.begin(), members_.end(),
57  [&widget](const group_type& member){ return member.first == widget; }));
58  }
59 
60  /**
61  * Clears the entire group of members.
62  */
63  void clear()
64  {
65  members_.clear();
66  }
67 
68  /**
69  * Group member getters
70  */
71  std::pair<group_iterator, group_iterator> members()
72  {
73  return std::make_pair(members_.begin(), members_.end());
74  }
75 
76  std::pair<group_iterator_const, group_iterator_const> members() const
77  {
78  return std::make_pair(members_.begin(), members_.end());
79  }
80 
81  /**
82  * The default actions to take when clicking on one of the widgets
83  * in the group.
84  */
86  {
87  for(auto& member : members())
88  {
89  member.first->set_value(false);
90  }
91  }
92 
93  /**
94  * Returns the value paired with the currently activiely toggled member
95  * of the group.
96  */
98  {
99  for(auto& member : members())
100  {
101  if(member.first->get_value_bool()) {
102  return member.second;
103  }
104  }
105 
106  return T();
107  }
108 
109  /**
110  * Sets the toggle values for all widgets besides the one associated
111  * with the specified value to false.
112  */
113  void set_member_states(const T& value)
114  {
115  for(auto& member : members())
116  {
117  member.first->set_value(member.second == value);
118  }
119  }
120 
121 private:
122  group_list members_;
123 
124 };
125 
126 } // namespace gui2
127 
128 #endif
void clear()
Clears the entire group of members.
Definition: group.hpp:63
void remove_member(tselectable_ *widget)
Removes a member from the group vector.
Definition: group.hpp:54
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
std::vector< group_type > group_list
Definition: group.hpp:33
GLsizei const GLfloat * value
Definition: glew.h:1817
std::pair< group_iterator, group_iterator > members()
Group member getters.
Definition: group.hpp:71
group_list members_
Definition: group.hpp:122
std::pair< group_iterator_const, group_iterator_const > members() const
Definition: group.hpp:76
Small abstract helper class.
Definition: selectable.hpp:32
T get_active_member_value()
Returns the value paired with the currently activiely toggled member of the group.
Definition: group.hpp:97
std::pair< tselectable_ *, T > group_type
Definition: group.hpp:32
Base class for all widgets.
Definition: widget.hpp:49
void add_member(tselectable_ *widget, const T &value)
Adds a widget/value pair to the group vector.
Definition: group.hpp:43
void group_operator()
The default actions to take when clicking on one of the widgets in the group.
Definition: group.hpp:85
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
void set_member_states(const T &value)
Sets the toggle values for all widgets besides the one associated with the specified value to false...
Definition: group.hpp:113
group_list::const_iterator group_iterator_const
Definition: group.hpp:35
group_list::iterator group_iterator
Definition: group.hpp:34