]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/RadioButtonGroup.C
introduce namespace lyx::support
[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
17 #include "RadioButtonGroup.h"
18 #include "lyx_forms.h"
19
20 #include "support/LAssert.h"
21 #include "debug.h" // for lyxerr
22 #include "support/lyxfunctional.h"
23
24 #include <algorithm>
25 #include <iterator>
26
27 using namespace lyx::support;
28
29 using std::find_if;
30 using std::endl;
31
32
33 void RadioButtonGroup::init(FL_OBJECT * ob, size_type value)
34 {
35         // Object must be a ROUND3DBUTTON (let all radio buttons look the same)
36         // and of type RADIO_BUTTON (otherwise it ain't work).
37         Assert(ob && ob->objclass == FL_ROUND3DBUTTON
38                         && ob->type == FL_RADIO_BUTTON);
39
40         map.push_back(ButtonValuePair(ob, value));
41 }
42
43
44 void RadioButtonGroup::set(size_type value) const
45 {
46         ButtonValueMap::const_iterator it =
47                 find_if(map.begin(), map.end(),
48                         lyx::equal_2nd_in_pair<ButtonValuePair>(value));
49
50         if (it != map.end()) {
51                 fl_set_button(it->first, 1);
52         } else {
53                 // We found nothing: report it and do nothing.
54                 lyxerr << "BUG: Requested value in RadioButtonGroup "
55                         "doesn't exist" << endl;
56         }
57 }
58
59
60 void RadioButtonGroup::set(FL_OBJECT * ob) const
61 {
62         // Object must be member of the radiobutton group.
63         bool isMember = false;
64         ButtonValueMap::const_iterator it = map.begin();
65         for (; it != map.end() && !isMember; ++it) {
66                 isMember = it->first == ob;
67         }
68
69         if (isMember) {
70                 fl_set_button(ob, 1);
71         } else {
72                 // Object is not a member; report it and do nothing.
73                 lyxerr << "BUG: Requested object is not a member of "
74                         << "the RadioButtonGroup." << endl;
75         }
76 }
77
78
79 template < typename T >
80 struct is_set_button {
81         bool operator() (T const & item) const
82         {
83                 return fl_get_button((item).first);
84         }
85 };
86
87
88 RadioButtonGroup::size_type RadioButtonGroup::get() const
89 {
90         // Find the active button.
91         ButtonValueMap::const_iterator it =
92                 find_if(map.begin(), map.end(),
93                         is_set_button<ButtonValuePair> ());
94
95         if (it != map.end())
96                 return it->second;
97
98         // We found nothing: report it and return 0
99         lyxerr << "BUG: No active radio button found." << endl;
100         return 0;
101 }