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
28 void RadioButtonGroup::registerRadioButton(FL_OBJECT *button, int value)
31 bvec.push_back(button);
32 vvec.push_back(value);
34 map.push_back( ButtonValuePair(button, value) );
38 void RadioButtonGroup::reset()
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
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.
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;
61 pair<typename T::first_type, typename T::second_type> const & left,
62 typename T::second_type const & right) const
64 return left.second == right;
68 void RadioButtonGroup::setButton(int value)
71 ValueVector::iterator vit =
72 find_if(vvec.begin(), vvec.end(),
73 bind2nd(equal_to<int>(), value));
75 if (vit == vvec.end()) {
76 lyxerr << "BUG: Requested value in RadioButtonGroup doesn't exists"
81 unsigned n = std::distance(vvec.begin(), vit);
83 fl_set_button(bvec[n], 1);
87 ButtonValueMap::const_iterator it =
89 find_if(map.begin(), map.end(),
90 bind2nd(equal_to_second_in_pair<ButtonValuePair>(),
93 std::find_if(map.begin(), map.end(),
95 std::bind2nd(std::equal_to<int>(), value)
97 std::select2nd<ButtonValuePair>()
101 // If we found nothing, report it and return
102 if (it == map.end()) {
103 lyxerr << "BUG: Requested value in RadioButtonGroup doesn't exists"
106 fl_set_button((*it).first, 1);
112 struct is_set_button {
113 bool operator() (T const & item) const
115 return fl_get_button( (item).first );
119 int RadioButtonGroup::getButton()
122 ButtonVector::const_iterator bit = bvec.begin();
123 ValueVector::const_iterator vit = vvec.begin();
125 while (bit != bvec.end()) {
126 if (fl_get_button(*bit))
136 // Find the first button that is active
137 ButtonValueMap::iterator it =
138 find_if(map.begin(), map.end(),
139 is_set_button<ButtonValuePair>() );
141 // If such a button was found, return its value.
142 if (it != map.end()) {
146 lyxerr << "BUG: No radio button found to be active." << endl;