]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/RadioButtonGroup.C
Change Assert to BOOST_ASSERT.
[lyx.git] / src / frontends / xforms / RadioButtonGroup.C
1 /**
2  * \file RadioButtonGroup.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Baruch Even
7  * \author Rob Lahaye
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "RadioButtonGroup.h"
15
16 #include "xforms_helpers.h"
17
18 #include "debug.h"
19
20 #include "support/lyxfunctional.h"
21
22 #include "lyx_forms.h"
23
24 using std::endl;
25
26
27 void RadioButtonGroup::init(FL_OBJECT * ob, size_type value)
28 {
29         // Object must be a ROUND3DBUTTON (let all radio buttons look the same)
30         // and of type RADIO_BUTTON (otherwise it ain't work).
31         BOOST_ASSERT(ob && ob->objclass == FL_ROUND3DBUTTON
32                         && ob->type == FL_RADIO_BUTTON);
33
34         map.push_back(ButtonValuePair(ob, value));
35 }
36
37
38 void RadioButtonGroup::set(size_type value) const
39 {
40         ButtonValueMap::const_iterator it =
41                 find_if(map.begin(), map.end(),
42                         lyx::equal_2nd_in_pair<ButtonValuePair>(value));
43
44         if (it != map.end()) {
45                 fl_set_button(it->first, 1);
46         } else {
47                 // We found nothing: report it and do nothing.
48                 lyxerr << "BUG: Requested value in RadioButtonGroup "
49                         "doesn't exist" << endl;
50         }
51 }
52
53
54 void RadioButtonGroup::set(FL_OBJECT * ob) const
55 {
56         // Object must be member of the radiobutton group.
57         bool isMember = false;
58         ButtonValueMap::const_iterator it = map.begin();
59         for (; it != map.end() && !isMember; ++it) {
60                 isMember = it->first == ob;
61         }
62
63         if (isMember) {
64                 fl_set_button(ob, 1);
65         } else {
66                 // Object is not a member; report it and do nothing.
67                 lyxerr << "BUG: Requested object is not a member of "
68                         << "the RadioButtonGroup." << endl;
69         }
70 }
71
72
73 template < typename T >
74 struct is_set_button {
75         bool operator() (T const & item) const
76         {
77                 return fl_get_button((item).first);
78         }
79 };
80
81
82 void RadioButtonGroup::unset() const
83 {
84         // Find the active button.
85         ButtonValueMap::const_iterator it =
86                 find_if(map.begin(), map.end(),
87                         is_set_button<ButtonValuePair> ());
88
89         if (it == map.end())
90                 // Nothing to do. No button is set.
91                 return;
92
93         fl_set_button(it->first, 0);
94 }
95
96
97 RadioButtonGroup::size_type RadioButtonGroup::get() const
98 {
99         // Find the active button.
100         ButtonValueMap::const_iterator it =
101                 find_if(map.begin(), map.end(),
102                         is_set_button<ButtonValuePair> ());
103
104         if (it != map.end())
105                 return it->second;
106
107         // We found nothing: report it and return 0
108         lyxerr << "BUG: No active radio button found." << endl;
109         return 0;
110 }
111
112
113 void RadioButtonGroup::setEnabled(bool enable)
114 {
115         ButtonValueMap::iterator it  = map.begin();
116         ButtonValueMap::iterator end = map.end();
117         for (; it != end; ++it) {
118                 ::setEnabled(it->first, enable);
119         }
120 }