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