]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/RadioButtonGroup.C
* translator.h: use bind, equal_to instead of equal_1st_in_pair
[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 "lyx_forms.h"
21
22 #include <boost/assert.hpp>
23 #include <boost/bind.hpp>
24
25 using boost::bind;
26
27 using std::endl;
28 using std::equal_to;
29
30 namespace lyx {
31 namespace frontend {
32
33 void RadioButtonGroup::init(FL_OBJECT * ob, size_type value)
34 {
35         // Object must be a ROUND3DBUTTON (let all radio buttons look the same)
36         // and of type RADIO_BUTTON (otherwise it ain't work).
37         BOOST_ASSERT(ob && ob->objclass == FL_ROUND3DBUTTON
38                         && ob->type == FL_RADIO_BUTTON);
39
40         map.push_back(ButtonValuePair(ob, value));
41 }
42
43
44 void RadioButtonGroup::set(size_type value) const
45 {
46         ButtonValueMap::const_iterator it =
47                 find_if(map.begin(), map.end(),
48                         bind(equal_to<size_type>(),
49                              bind(&ButtonValuePair::second, _1),
50                              value));
51
52         if (it != map.end()) {
53                 fl_set_button(it->first, 1);
54         } else {
55                 // We found nothing: report it and do nothing.
56                 lyxerr << "BUG: Requested value in RadioButtonGroup "
57                         "doesn't exist" << endl;
58         }
59 }
60
61
62 void RadioButtonGroup::set(FL_OBJECT * ob) const
63 {
64         // Object must be member of the radiobutton group.
65         bool isMember = false;
66         ButtonValueMap::const_iterator it = map.begin();
67         for (; it != map.end() && !isMember; ++it) {
68                 isMember = it->first == ob;
69         }
70
71         if (isMember) {
72                 fl_set_button(ob, 1);
73         } else {
74                 // Object is not a member; report it and do nothing.
75                 lyxerr << "BUG: Requested object is not a member of "
76                         << "the RadioButtonGroup." << endl;
77         }
78 }
79
80
81 template <typename T>
82 struct is_set_button : public std::unary_function<T, bool> {
83         bool operator() (T const & item) const
84         {
85                 return fl_get_button((item).first);
86         }
87 };
88
89
90 void RadioButtonGroup::unset() const
91 {
92         // Find the active button.
93         ButtonValueMap::const_iterator it =
94                 find_if(map.begin(), map.end(),
95                         is_set_button<ButtonValuePair> ());
96
97         if (it == map.end())
98                 // Nothing to do. No button is set.
99                 return;
100
101         fl_set_button(it->first, 0);
102 }
103
104
105 size_type RadioButtonGroup::get() const
106 {
107         // Find the active button.
108         ButtonValueMap::const_iterator it =
109                 find_if(map.begin(), map.end(),
110                         is_set_button<ButtonValuePair> ());
111
112         if (it != map.end())
113                 return it->second;
114
115         // We found nothing: report it and return 0
116         lyxerr << "BUG: No active radio button found." << endl;
117         return 0;
118 }
119
120
121 void RadioButtonGroup::setEnabled(bool enable)
122 {
123         ButtonValueMap::iterator it  = map.begin();
124         ButtonValueMap::iterator end = map.end();
125         for (; it != end; ++it) {
126                 lyx::frontend::setEnabled(it->first, enable);
127         }
128 }
129
130 } // namespace frontend
131 } // namespace lyx