2000-07-31 12:30:10 +00:00
|
|
|
/* This file is part of
|
|
|
|
* =================================================
|
|
|
|
*
|
|
|
|
* LyX, The Document Processor
|
|
|
|
* Copyright 1995 Matthias Ettrich.
|
2001-05-30 13:53:44 +00:00
|
|
|
* Copyright 1995-2001 The LyX Team.
|
2000-07-31 12:30:10 +00:00
|
|
|
*
|
|
|
|
* This file Copyright 2000 Baruch Even
|
|
|
|
* ================================================= */
|
|
|
|
|
2000-08-14 09:44:53 +00:00
|
|
|
#include <config.h>
|
2000-07-31 12:30:10 +00:00
|
|
|
|
2001-03-15 13:37:04 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
|
|
|
|
2000-07-31 12:30:10 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
2000-08-14 09:44:53 +00:00
|
|
|
#endif
|
2000-07-31 12:30:10 +00:00
|
|
|
|
|
|
|
#include "RadioButtonGroup.h"
|
|
|
|
|
|
|
|
#include "debug.h" // for lyxerr
|
|
|
|
|
|
|
|
using std::find_if;
|
2000-08-01 09:35:42 +00:00
|
|
|
using std::bind2nd;
|
2000-07-31 13:36:03 +00:00
|
|
|
using std::endl;
|
2000-07-31 12:30:10 +00:00
|
|
|
|
|
|
|
void RadioButtonGroup::registerRadioButton(FL_OBJECT *button, int value)
|
|
|
|
{
|
2000-08-14 09:44:53 +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
|
|
|
}
|
|
|
|
|
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() (
|
|
|
|
pair < typename T::first_type, typename T::second_type > const & left,
|
|
|
|
typename T::second_type const & right) const
|
|
|
|
{
|
|
|
|
return left.second == right;
|
|
|
|
}
|
|
|
|
};
|
2000-07-31 12:30:10 +00:00
|
|
|
|
|
|
|
void RadioButtonGroup::setButton(int value)
|
|
|
|
{
|
2000-08-14 09:44:53 +00:00
|
|
|
ButtonValueMap::const_iterator it =
|
|
|
|
find_if(map.begin(), map.end(),
|
|
|
|
bind2nd(equal_to_second_in_pair < ButtonValuePair > (),
|
|
|
|
value));
|
|
|
|
|
|
|
|
// If we found nothing, report it and return
|
|
|
|
if (it == map.end()) {
|
|
|
|
lyxerr << "BUG: Requested value in RadioButtonGroup doesn't exists"
|
|
|
|
<< endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fl_set_button((*it).first, 1);
|
|
|
|
}
|
|
|
|
|
2000-07-31 12:30:10 +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
|
|
|
|
{
|
|
|
|
return fl_get_button( (item).first );
|
|
|
|
}
|
2000-07-31 12:30:10 +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 =
|
|
|
|
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()) {
|
|
|
|
return (*it).second;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|