]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/RadioButtonGroup.C
xforms clean-up, described in detail in my mail of 31 May. See
[lyx.git] / src / frontends / xforms / RadioButtonGroup.C
1 /**
2  * \file RadioButtonGroup.C
3  * Copyright 1995 Matthias Ettrich.
4  * Copyright 1995-2001 The LyX Team.
5  * Copyright 2000 Baruch Even
6  * See the file COPYING.
7  *
8  * \author Baruch Even, baruch.even@writeme.com
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "RadioButtonGroup.h"
18 #include FORMS_H_LOCATION
19
20 #include "debug.h" // for lyxerr
21 #include "support/lyxfunctional.h"
22
23 //#include <functional>
24 #include <algorithm>
25 #include <iterator>
26
27 using std::find_if;
28 //using std::bind2nd;
29 using std::endl;
30
31
32 void RadioButtonGroup::init(FL_OBJECT *button, size_type value)
33 {
34         map.push_back(ButtonValuePair(button, value));
35 }
36
37
38 void RadioButtonGroup::reset()
39 {
40         map.clear();
41 }
42
43
44 void RadioButtonGroup::set(size_type value)
45 {
46         ButtonValueMap::const_iterator it =
47                 find_if(map.begin(), map.end(),
48                         lyx::equal_2nd_in_pair<ButtonValuePair>(value));
49
50         // If we found nothing, report it and return
51         if (it == map.end()) {
52                 lyxerr << "BUG: Requested value in RadioButtonGroup doesn't exists"
53                        << endl;
54         }
55         else {
56                 fl_set_button(it->first, 1);
57         }
58
59 }
60
61
62 template < typename T >
63 struct is_set_button {
64         bool operator() (T const & item) const
65         {
66                 return fl_get_button((item).first);
67         }
68 };
69
70
71 RadioButtonGroup::size_type RadioButtonGroup::get() const
72 {
73         // Find the first button that is active
74         ButtonValueMap::const_iterator it =
75                 find_if(map.begin(), map.end(),
76                         is_set_button<ButtonValuePair> ());
77
78         // If such a button was found, return its value.
79         if (it != map.end()) {
80                 return it->second;
81         }
82
83         lyxerr << "BUG: No radio button found to be active." << endl;
84
85         // Else return 0.
86         return 0;
87 }