]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/RadioButtonGroup.C
(Rob Lahaye): miscellaneous bits 'n' bobs.
[lyx.git] / src / frontends / xforms / RadioButtonGroup.C
1 /**
2  * \file RadioButtonGroup.C
3  * Copyright 1995 Matthias Ettrich.
4  * Copyright 2000 Baruch Even
5  * This file is part of LyX, the document processor.
6  * Licence details can be found in the file COPYING.
7  *
8  * \author Baruch Even
9  * \author Rob Lahaye
10  *
11  * Full author contact details are available in file CREDITS
12  */
13
14 #include <config.h>
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include "RadioButtonGroup.h"
21 #include FORMS_H_LOCATION
22
23 #include "debug.h" // for lyxerr
24 #include "support/lyxfunctional.h"
25
26 #include <algorithm>
27 #include <iterator>
28
29 using std::find_if;
30 using std::endl;
31
32
33 void RadioButtonGroup::init(FL_OBJECT * ob, size_type value)
34 {
35         map.push_back(ButtonValuePair(ob, value));
36 }
37
38
39 void RadioButtonGroup::set(size_type value)
40 {
41         ButtonValueMap::const_iterator it =
42                 find_if(map.begin(), map.end(),
43                         lyx::equal_2nd_in_pair<ButtonValuePair>(value));
44
45         if (it != map.end()) {
46                 set(it->first);
47         } else {
48                 // We found nothing: report it and do nothing.
49                 lyxerr << "BUG: Requested value in RadioButtonGroup "
50                         "doesn't exist" << endl;
51         }
52 }
53
54
55 void RadioButtonGroup::set(FL_OBJECT * ob)
56 {
57         fl_set_button(ob, 1);
58 }
59
60
61 template < typename T >
62 struct is_set_button {
63         bool operator() (T const & item) const
64         {
65                 return fl_get_button((item).first);
66         }
67 };
68
69
70 RadioButtonGroup::size_type RadioButtonGroup::get() const
71 {
72         // Find the active button.
73         ButtonValueMap::const_iterator it =
74                 find_if(map.begin(), map.end(),
75                         is_set_button<ButtonValuePair> ());
76
77         if (it != map.end())
78                 return it->second;
79
80         // We found nothing: report it and return 0
81         lyxerr << "BUG: No active radio button found." << endl;
82         return 0;
83 }