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