cwidget  0.5.17
radiogroup.h
1 // radiogroup.h -*-c++-*-
2 //
3 // Ok, here's how radio-button-like behavior is implemented:
4 //
5 // Radio-groups store some group of buttons. They constrain the buttons
6 // so that exactly one is on. This is done by various devious means:
7 // the first button added is always selected, and subsequently added selected
8 // buttons override previously selected buttons. You can manually select a
9 // button through this class (by ID) or via the button's own set_checked
10 // routine.
11 //
12 // (note that radio-groups are NOT WIDGETS!!!)
13 // (note that this does NOT attempt to memory-manage its "children"!)
14 // (note that you should generally delete this at the same time as its
15 // children, or Bad Things[tm] may happen.. (more specifically, if you
16 // delete a selected child, some other random option will be selected))
17 //
18 // Oh, one more note: although any togglebutton can be used in a radio
19 // widget, passing in checkbuttons has a high probability of causing weird
20 // things. Use radiobuttons. (if you want them to look like checkbuttons,
21 // use the extended togglebutton constructor..)
22 
23 #ifndef RADIOGROUP_H
24 #define RADIOGROUP_H
25 
26 #include <cwidget/generic/util/ref_ptr.h>
27 
28 #include <vector>
29 
30 #include <sigc++/connection.h>
31 #include <sigc++/trackable.h>
32 
33 namespace cwidget
34 {
35  namespace widgets
36  {
37  class togglebutton;
38 
39  class radiogroup:public sigc::trackable
40  {
41  struct item
42  {
44  int id;
45 
46  // Needed, unfortunately.
47  sigc::connection destroyed_conn, pressed_conn;
48 
49  item(const util::ref_ptr<togglebutton> &_b, int _id,
50  const sigc::connection &_dconn, const sigc::connection &_pconn)
51  :b(_b), id(_id), destroyed_conn(_dconn), pressed_conn(_pconn) {}
52  };
53 
54  typedef std::vector<item> itemlist;
55 
56  itemlist items;
57 
58  // The index of the currently selected button
59  itemlist::size_type selected;
60 
61  // Called when a particular button is selected.
62  // The argument is the *index* of the button.
63  void button_pressed(itemlist::size_type index);
64  public:
65  radiogroup();
66  ~radiogroup();
67 
68  void add_button(const util::ref_ptr<togglebutton> &b, int id);
69  void rem_button(const util::ref_ptr<togglebutton> &b);
70 
71  void rem_button_bare(togglebutton &b);
72 
74  bool selection_valid();
75 
77  int get_selected();
78 
79  // Selects a button by id.
80  void select(int id);
81 
83  void destroy();
84 
85  // Emitted when one of the sub-items is chosen. (you could also collect
86  // the individual button signals; this is just a higher-level view of it)
87  sigc::signal1<void, int> item_selected;
88  };
89  }
90 }
91 
92 #endif
Definition: ref_ptr.h:19
int get_selected()
Definition: radiogroup.cc:30
Definition: radiogroup.h:39
The namespace containing everything defined by cwidget.
Definition: columnify.cc:26
Definition: togglebutton.h:17
void destroy()
Destroy this radio group.
Definition: radiogroup.cc:117
bool selection_valid()
Definition: radiogroup.cc:25