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