]> git.lyx.org Git - features.git/blob - src/frontends/xforms/RadioButtonGroup.C
1289c75cdd512aeb5317971ab0a5627427196453
[features.git] / src / frontends / xforms / RadioButtonGroup.C
1 // -*- C++ -*-
2 /* This file is part of
3  * =================================================
4  * 
5  *          LyX, The Document Processor
6  *          Copyright 1995 Matthias Ettrich.
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  *          This file Copyright 2000 Baruch Even
10  * ================================================= */
11
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include <config.h>
18 #include "RadioButtonGroup.h"
19
20 #include "debug.h" // for lyxerr
21
22 #include <functional>
23 #include <algorithm>
24 #include <iterator>
25 using std::find_if;
26 using std::endl;
27
28 void RadioButtonGroup::registerRadioButton(FL_OBJECT *button, int value)
29 {
30 #if 0
31     bvec.push_back(button);
32     vvec.push_back(value);
33 #endif
34     map.push_back( ButtonValuePair(button, value) );
35 }
36
37
38 void RadioButtonGroup::reset()
39 {
40 #if 0
41     bvec.clear();
42     vvec.clear();
43 #endif
44     map.clear(); 
45 }
46
47     // Functor to help us in our work, we should try to find how to achieve
48     // this with only STL predicates, but its easier to write this than to
49     // dig. If you can find the equivalent STL predicate combination, let me 
50     // know.
51     //
52     // The idea is to take a pair and a value and return true when the second
53     // element in the pair equals the value.
54     template <typename T>
55     struct equal_to_second_in_pair {
56         typedef bool                    result_type;
57         typedef T              first_argument_type;
58         typedef typename T::second_type second_argument_type;
59
60         bool operator() (
61            pair<typename T::first_type, typename T::second_type> const & left,
62            typename T::second_type const & right) const
63         {
64             return left.second == right;
65         }
66     };
67
68 void RadioButtonGroup::setButton(int value)
69 {
70 #if 0
71     ValueVector::iterator vit =
72         find_if(vvec.begin(), vvec.end(),
73             bind2nd(equal_to<int>(), value));
74     
75     if (vit == vvec.end()) {
76         lyxerr << "BUG: Requested value in RadioButtonGroup doesn't exists"
77             << endl;
78         return;
79     }
80
81     unsigned n = std::distance(vvec.begin(), vit);
82
83     fl_set_button(bvec[n], 1);
84 #endif
85
86
87     ButtonValueMap::const_iterator it = 
88 #if 0
89         find_if(map.begin(), map.end(),
90                 bind2nd(equal_to_second_in_pair<ButtonValuePair>(),
91                     value));
92 #else
93         std::find_if(map.begin(), map.end(),
94                 std::compose1(
95                     std::bind2nd(std::equal_to<int>(), value)
96                     ,
97                     std::select2nd<ButtonValuePair>()
98                 )
99         );
100 #endif
101     // If we found nothing, report it and return
102     if (it == map.end()) {
103         lyxerr << "BUG: Requested value in RadioButtonGroup doesn't exists"
104             << endl;
105     } else {
106         fl_set_button((*it).first, 1);
107     }
108     
109 }
110
111 template<typename T>
112 struct is_set_button {
113     bool operator() (T const & item) const
114     {
115         return fl_get_button( (item).first );
116     }
117 };
118
119 int  RadioButtonGroup::getButton()
120 {
121 #if 0
122     ButtonVector::const_iterator bit = bvec.begin();
123     ValueVector::const_iterator vit = vvec.begin();
124
125     while (bit != bvec.end()) {
126         if (fl_get_button(*bit))
127             return *vit;
128
129         bit++;
130         vit++;
131     }
132
133     return 0;
134 #endif
135
136     // Find the first button that is active
137     ButtonValueMap::iterator it =
138         find_if(map.begin(), map.end(),
139                 is_set_button<ButtonValuePair>() );
140
141     // If such a button was found, return its value.
142     if (it != map.end()) {
143         return (*it).second;
144     }
145
146     lyxerr << "BUG: No radio button found to be active." << endl;
147
148     // Else return 0.
149     return 0;
150 }
151