2002-03-11 17:00:41 +00:00
|
|
|
/**
|
|
|
|
* \file RadioButtonGroup.C
|
|
|
|
* Copyright 1995 Matthias Ettrich.
|
|
|
|
* Copyright 1995-2001 The LyX Team.
|
|
|
|
* Copyright 2000 Baruch Even
|
|
|
|
* See the file COPYING.
|
2000-07-31 12:30:10 +00:00
|
|
|
*
|
2002-03-11 17:00:41 +00:00
|
|
|
* \author Baruch Even, baruch.even@writeme.com
|
|
|
|
*/
|
2000-07-31 12:30:10 +00:00
|
|
|
|
2002-03-21 21:21:28 +00:00
|
|
|
#include <config.h>
|
2000-07-31 12:30:10 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
2002-03-21 21:21:28 +00:00
|
|
|
#endif
|
2000-07-31 12:30:10 +00:00
|
|
|
|
|
|
|
#include "RadioButtonGroup.h"
|
|
|
|
|
|
|
|
#include "debug.h" // for lyxerr
|
2002-02-16 15:59:55 +00:00
|
|
|
#include "support/lyxfunctional.h"
|
|
|
|
|
|
|
|
//#include <functional>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
2000-07-31 12:30:10 +00:00
|
|
|
|
|
|
|
using std::find_if;
|
2002-02-16 15:59:55 +00:00
|
|
|
//using std::bind2nd;
|
2000-07-31 13:36:03 +00:00
|
|
|
using std::endl;
|
2000-07-31 12:30:10 +00:00
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2000-07-31 12:30:10 +00:00
|
|
|
void RadioButtonGroup::registerRadioButton(FL_OBJECT *button, int value)
|
|
|
|
{
|
2002-02-16 15:59:55 +00:00
|
|
|
map.push_back(ButtonValuePair(button, value));
|
2000-07-31 12:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RadioButtonGroup::reset()
|
|
|
|
{
|
2000-08-14 09:44:53 +00:00
|
|
|
map.clear();
|
2000-07-31 12:30:10 +00:00
|
|
|
}
|
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
|
|
#if 0
|
2000-08-14 09:44:53 +00:00
|
|
|
// Functor to help us in our work, we should try to find how to achieve
|
|
|
|
// this with only STL predicates, but its easier to write this than to
|
|
|
|
// dig. If you can find the equivalent STL predicate combination, let me
|
|
|
|
// know.
|
|
|
|
//
|
|
|
|
// The idea is to take a pair and a value and return true when the second
|
|
|
|
// element in the pair equals the value.
|
|
|
|
template < typename T >
|
|
|
|
struct equal_to_second_in_pair
|
|
|
|
{
|
|
|
|
typedef bool result_type;
|
|
|
|
typedef T first_argument_type;
|
|
|
|
typedef typename T::second_type second_argument_type;
|
|
|
|
|
|
|
|
bool operator() (
|
2002-03-21 16:59:12 +00:00
|
|
|
pair < typename T::first_type, typename T::second_type > const & left,
|
|
|
|
typename T::second_type const & right) const
|
2000-08-14 09:44:53 +00:00
|
|
|
{
|
|
|
|
return left.second == right;
|
|
|
|
}
|
|
|
|
};
|
2002-02-16 15:59:55 +00:00
|
|
|
#endif
|
|
|
|
|
2000-07-31 12:30:10 +00:00
|
|
|
|
|
|
|
void RadioButtonGroup::setButton(int value)
|
|
|
|
{
|
2002-02-16 15:59:55 +00:00
|
|
|
#if 0
|
2000-08-14 09:44:53 +00:00
|
|
|
ButtonValueMap::const_iterator it =
|
2002-03-21 16:59:12 +00:00
|
|
|
find_if(map.begin(), map.end(),
|
|
|
|
bind2nd(equal_to_second_in_pair<ButtonValuePair>(),
|
|
|
|
value));
|
2002-02-16 15:59:55 +00:00
|
|
|
#else
|
|
|
|
ButtonValueMap::const_iterator it =
|
|
|
|
find_if(map.begin(), map.end(),
|
|
|
|
lyx::equal_2nd_in_pair<ButtonValuePair>(value));
|
|
|
|
#endif
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2000-08-14 09:44:53 +00:00
|
|
|
// If we found nothing, report it and return
|
|
|
|
if (it == map.end()) {
|
|
|
|
lyxerr << "BUG: Requested value in RadioButtonGroup doesn't exists"
|
2002-03-21 16:59:12 +00:00
|
|
|
<< endl;
|
2000-08-14 09:44:53 +00:00
|
|
|
}
|
|
|
|
else {
|
2001-07-12 11:11:10 +00:00
|
|
|
fl_set_button(it->first, 1);
|
2000-08-14 09:44:53 +00:00
|
|
|
}
|
|
|
|
|
2000-07-31 12:30:10 +00:00
|
|
|
}
|
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2000-08-14 09:44:53 +00:00
|
|
|
template < typename T >
|
2000-07-31 12:30:10 +00:00
|
|
|
struct is_set_button {
|
2000-08-14 09:44:53 +00:00
|
|
|
bool operator() (T const & item) const
|
|
|
|
{
|
2002-02-16 15:59:55 +00:00
|
|
|
return fl_get_button((item).first);
|
2000-08-14 09:44:53 +00:00
|
|
|
}
|
2000-07-31 12:30:10 +00:00
|
|
|
};
|
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2000-08-14 09:44:53 +00:00
|
|
|
int RadioButtonGroup::getButton()
|
2000-07-31 12:30:10 +00:00
|
|
|
{
|
2000-08-14 09:44:53 +00:00
|
|
|
// Find the first button that is active
|
|
|
|
ButtonValueMap::iterator it =
|
2002-03-21 16:59:12 +00:00
|
|
|
find_if(map.begin(), map.end(),
|
|
|
|
is_set_button<ButtonValuePair> ());
|
2000-07-31 12:30:10 +00:00
|
|
|
|
2000-08-14 09:44:53 +00:00
|
|
|
// If such a button was found, return its value.
|
|
|
|
if (it != map.end()) {
|
2001-07-12 11:11:10 +00:00
|
|
|
return it->second;
|
2000-08-14 09:44:53 +00:00
|
|
|
}
|
2000-07-31 12:30:10 +00:00
|
|
|
|
2000-08-14 09:44:53 +00:00
|
|
|
lyxerr << "BUG: No radio button found to be active." << endl;
|
2000-07-31 12:30:10 +00:00
|
|
|
|
2000-08-14 09:44:53 +00:00
|
|
|
// Else return 0.
|
|
|
|
return 0;
|
2000-07-31 12:30:10 +00:00
|
|
|
}
|