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