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