]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/RadioButtonGroup.C
major GUII cleanup + Baruchs patch + Angus's patch + removed a couple of generated...
[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 using std::bind2nd;
27 using std::endl;
28
29 void RadioButtonGroup::registerRadioButton(FL_OBJECT *button, int value)
30 {
31     map.push_back( ButtonValuePair(button, value) );
32 }
33
34
35 void RadioButtonGroup::reset()
36 {
37     map.clear(); 
38 }
39
40     // Functor to help us in our work, we should try to find how to achieve
41     // this with only STL predicates, but its easier to write this than to
42     // dig. If you can find the equivalent STL predicate combination, let me 
43     // know.
44     //
45     // The idea is to take a pair and a value and return true when the second
46     // element in the pair equals the value.
47     template <typename T>
48     struct equal_to_second_in_pair {
49         typedef bool                    result_type;
50         typedef T       first_argument_type;
51         typedef typename T::second_type second_argument_type;
52
53         bool operator() (
54            pair<typename T::first_type, typename T::second_type> const & left,
55            typename T::second_type const & right) const
56         {
57             return left.second == right;
58         }
59     };
60
61 void RadioButtonGroup::setButton(int value)
62 {
63     ButtonValueMap::const_iterator it = 
64         find_if(map.begin(), map.end(),
65                 bind2nd(equal_to_second_in_pair<ButtonValuePair>(),
66                     value));
67
68     // If we found nothing, report it and return
69     if (it == map.end()) {
70         lyxerr << "BUG: Requested value in RadioButtonGroup doesn't exists"
71             << endl;
72     } else {
73         fl_set_button((*it).first, 1);
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 int  RadioButtonGroup::getButton()
87 {
88     // Find the first button that is active
89     ButtonValueMap::iterator it =
90         find_if(map.begin(), map.end(),
91                 is_set_button<ButtonValuePair>() );
92
93     // If such a button was found, return its value.
94     if (it != map.end()) {
95         return (*it).second;
96     }
97
98     lyxerr << "BUG: No radio button found to be active." << endl;
99
100     // Else return 0.
101     return 0;
102 }
103