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