/**
* \file RadioButtonGroup.C
- * Copyright 1995 Matthias Ettrich.
- * Copyright 1995-2001 The LyX Team.
- * Copyright 2000 Baruch Even
- * See the file COPYING.
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- * \author Baruch Even, baruch.even@writeme.com
+ * \author Baruch Even
+ * \author Rob Lahaye
+ *
+ * Full author contact details are available in file CREDITS.
*/
#include <config.h>
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
#include "RadioButtonGroup.h"
-#include "debug.h" // for lyxerr
-#include "support/lyxfunctional.h"
+#include "xforms_helpers.h"
-//#include <functional>
-#include <algorithm>
-#include <iterator>
+#include "debug.h"
-using std::find_if;
-//using std::bind2nd;
-using std::endl;
+#include "support/lyxfunctional.h"
+#include "lyx_forms.h"
-void RadioButtonGroup::registerRadioButton(FL_OBJECT *button, int value)
-{
- map.push_back(ButtonValuePair(button, value));
-}
-
+#include <boost/assert.hpp>
-void RadioButtonGroup::reset()
-{
- map.clear();
-}
+using std::endl;
-#if 0
-// 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
+void RadioButtonGroup::init(FL_OBJECT * ob, size_type value)
{
- typedef bool result_type;
- typedef T first_argument_type;
- typedef typename T::second_type second_argument_type;
+ // Object must be a ROUND3DBUTTON (let all radio buttons look the same)
+ // and of type RADIO_BUTTON (otherwise it ain't work).
+ BOOST_ASSERT(ob && ob->objclass == FL_ROUND3DBUTTON
+ && ob->type == FL_RADIO_BUTTON);
- bool operator() (
- pair < typename T::first_type, typename T::second_type > const & left,
- typename T::second_type const & right) const
- {
- return left.second == right;
- }
-};
-#endif
+ map.push_back(ButtonValuePair(ob, value));
+}
-void RadioButtonGroup::setButton(int value)
+void RadioButtonGroup::set(size_type value) const
{
-#if 0
- ButtonValueMap::const_iterator it =
- find_if(map.begin(), map.end(),
- bind2nd(equal_to_second_in_pair<ButtonValuePair>(),
- value));
-#else
ButtonValueMap::const_iterator it =
find_if(map.begin(), map.end(),
lyx::equal_2nd_in_pair<ButtonValuePair>(value));
-#endif
- // If we found nothing, report it and return
- if (it == map.end()) {
- lyxerr << "BUG: Requested value in RadioButtonGroup doesn't exists"
- << endl;
- }
- else {
+ if (it != map.end()) {
fl_set_button(it->first, 1);
+ } else {
+ // We found nothing: report it and do nothing.
+ lyxerr << "BUG: Requested value in RadioButtonGroup "
+ "doesn't exist" << endl;
}
+}
+
+void RadioButtonGroup::set(FL_OBJECT * ob) const
+{
+ // Object must be member of the radiobutton group.
+ bool isMember = false;
+ ButtonValueMap::const_iterator it = map.begin();
+ for (; it != map.end() && !isMember; ++it) {
+ isMember = it->first == ob;
+ }
+
+ if (isMember) {
+ fl_set_button(ob, 1);
+ } else {
+ // Object is not a member; report it and do nothing.
+ lyxerr << "BUG: Requested object is not a member of "
+ << "the RadioButtonGroup." << endl;
+ }
}
-template < typename T >
-struct is_set_button {
+template <typename T>
+struct is_set_button : public std::unary_function<T, bool> {
bool operator() (T const & item) const
{
return fl_get_button((item).first);
};
-int RadioButtonGroup::getButton()
+void RadioButtonGroup::unset() const
{
- // Find the first button that is active
- ButtonValueMap::iterator it =
+ // Find the active button.
+ ButtonValueMap::const_iterator it =
find_if(map.begin(), map.end(),
is_set_button<ButtonValuePair> ());
- // If such a button was found, return its value.
- if (it != map.end()) {
- return it->second;
- }
+ if (it == map.end())
+ // Nothing to do. No button is set.
+ return;
+
+ fl_set_button(it->first, 0);
+}
- lyxerr << "BUG: No radio button found to be active." << endl;
- // Else return 0.
+RadioButtonGroup::size_type RadioButtonGroup::get() const
+{
+ // Find the active button.
+ ButtonValueMap::const_iterator it =
+ find_if(map.begin(), map.end(),
+ is_set_button<ButtonValuePair> ());
+
+ if (it != map.end())
+ return it->second;
+
+ // We found nothing: report it and return 0
+ lyxerr << "BUG: No active radio button found." << endl;
return 0;
}
+
+
+void RadioButtonGroup::setEnabled(bool enable)
+{
+ ButtonValueMap::iterator it = map.begin();
+ ButtonValueMap::iterator end = map.end();
+ for (; it != end; ++it) {
+ ::setEnabled(it->first, enable);
+ }
+}