2 /* This file is part of
3 * =================================================
5 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich.
7 * Copyright 1995-2000 The LyX Team.
9 * This file Copyright 2000 Baruch Even
10 * ================================================= */
14 #pragma implementation
18 #include "RadioButtonGroup.h"
20 #include "debug.h" // for lyxerr
27 void RadioButtonGroup::registerRadioButton(FL_OBJECT *button, int value)
30 bvec.push_back(button);
31 vvec.push_back(value);
33 map.push_back( ButtonValuePair(button, value) );
37 void RadioButtonGroup::reset()
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
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.
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;
60 pair<typename T::first_type, typename T::second_type> const & left,
61 typename T::second_type const & right) const
63 return left.second == right;
67 void RadioButtonGroup::setButton(int value)
70 ValueVector::iterator vit =
71 find_if(vvec.begin(), vvec.end(),
72 bind2nd(equal_to<int>(), value));
74 if (vit == vvec.end()) {
75 lyxerr << "BUG: Requested value in RadioButtonGroup doesn't exists"
80 unsigned n = std::distance(vvec.begin(), vit);
82 fl_set_button(bvec[n], 1);
86 ButtonValueMap::const_iterator it =
88 find_if(map.begin(), map.end(),
89 bind2nd(equal_to_second_in_pair<ButtonValuePair>(),
92 std::find_if(map.begin(), map.end(),
94 std::bind2nd(std::equal_to<int>(), value)
96 std::select2nd<ButtonValuePair>()
100 // If we found nothing, report it and return
101 if (it == map.end()) {
102 lyxerr << "BUG: Requested value in RadioButtonGroup doesn't exists"
105 fl_set_button((*it).first, 1);
111 struct is_set_button {
112 bool operator() (T const & item) const
114 return fl_get_button( (item).first );
118 int RadioButtonGroup::getButton()
121 ButtonVector::const_iterator bit = bvec.begin();
122 ValueVector::const_iterator vit = vvec.begin();
124 while (bit != bvec.end()) {
125 if (fl_get_button(*bit))
135 // Find the first button that is active
136 ButtonValueMap::iterator it =
137 find_if(map.begin(), map.end(),
138 is_set_button<ButtonValuePair>() );
140 // If such a button was found, return its value.
141 if (it != map.end()) {
145 lyxerr << "BUG: No radio button found to be active." << endl;