]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/RadioButtonGroup.C
iThe cursor now behaves properly in the dialogs...
[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
19 #include "debug.h" // for lyxerr
20 #include "support/lyxfunctional.h"
21
22 //#include <functional>
23 #include <algorithm>
24 #include <iterator>
25
26 using std::find_if;
27 //using std::bind2nd;
28 using std::endl;
29
30
31 void RadioButtonGroup::registerRadioButton(FL_OBJECT *button, int value)
32 {
33         map.push_back(ButtonValuePair(button, value));
34 }
35
36
37 void RadioButtonGroup::reset()
38 {
39         map.clear();
40 }
41
42
43 #if 0
44 // Functor to help us in our work, we should try to find how to achieve
45 // this with only STL predicates, but its easier to write this than to
46 // dig. If you can find the equivalent STL predicate combination, let me
47 // know.
48 //
49 // The idea is to take a pair and a value and return true when the second
50 // element in the pair equals the value.
51 template < typename T >
52 struct equal_to_second_in_pair
53 {
54         typedef bool result_type;
55         typedef T       first_argument_type;
56         typedef typename T::second_type second_argument_type;
57
58         bool operator() (
59             pair < typename T::first_type, typename T::second_type > const & left,
60             typename T::second_type const & right) const
61         {
62                 return left.second == right;
63         }
64 };
65 #endif
66
67
68 void RadioButtonGroup::setButton(int value)
69 {
70 #if 0
71         ButtonValueMap::const_iterator it =
72             find_if(map.begin(), map.end(),
73                     bind2nd(equal_to_second_in_pair<ButtonValuePair>(),
74                             value));
75 #else
76         ButtonValueMap::const_iterator it =
77                 find_if(map.begin(), map.end(),
78                         lyx::equal_2nd_in_pair<ButtonValuePair>(value));
79 #endif
80         
81         // If we found nothing, report it and return
82         if (it == map.end()) {
83                 lyxerr << "BUG: Requested value in RadioButtonGroup doesn't exists"
84                 << endl;
85         }
86         else {
87                 fl_set_button(it->first, 1);
88         }
89
90 }
91
92
93 template < typename T >
94 struct is_set_button {
95         bool operator() (T const & item) const
96         {
97                 return fl_get_button((item).first);
98         }
99 };
100
101
102 int RadioButtonGroup::getButton()
103 {
104         // Find the first button that is active
105         ButtonValueMap::iterator it =
106             find_if(map.begin(), map.end(),
107                     is_set_button<ButtonValuePair> ());
108
109         // If such a button was found, return its value.
110         if (it != map.end()) {
111                 return it->second;
112         }
113
114         lyxerr << "BUG: No radio button found to be active." << endl;
115
116         // Else return 0.
117         return 0;
118 }
119