]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/checkedwidgets.C
fix crash with "save as"
[lyx.git] / src / frontends / xforms / checkedwidgets.C
1 /**
2  * \file checkedwidgets.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "checkedwidgets.h"
18 #include "xforms_helpers.h"
19 #include "lyxlength.h"
20 #include "lyxgluelength.h"
21 #include "support/LAssert.h"
22 #include "support/lstrings.h"
23 #include "LString.h"
24
25 #include FORMS_H_LOCATION
26
27
28 void addCheckedLyXLength(ButtonControllerBase & bc,
29                          FL_OBJECT * input, FL_OBJECT * label)
30 {
31         bc.addCheckedWidget(new CheckedLyXLength(input, label));
32 }
33
34
35 void addCheckedGlueLength(ButtonControllerBase & bc,
36                           FL_OBJECT * input, FL_OBJECT * label)
37 {
38         bc.addCheckedWidget(new CheckedGlueLength(input, label));
39 }
40
41
42 namespace {
43
44 void setWidget(bool valid, FL_OBJECT * input, FL_OBJECT * label)
45 {
46         // define color to mark invalid input
47         FL_COLOR const alert_col = FL_RED;
48
49         FL_COLOR const lcol = valid ? FL_LCOL : alert_col;
50         if (label->lcol != lcol && isActive(label)) {
51                 fl_set_object_lcol(label, lcol);
52         }
53         if (input->lcol != lcol && isActive(input)) {
54                 fl_set_object_lcol(input, lcol);
55         }
56
57         // Reflect the validity of the data in the background color of the
58         // input widget only when this widget is not being edited.
59         FL_COLOR const icol1 = valid ? FL_COL1 : alert_col;
60         if (input->col1 != icol1) {
61                 fl_set_object_color(input, icol1, FL_MCOL);
62         }
63 }
64
65 } // namespace anon
66
67
68 CheckedLyXLength::CheckedLyXLength(FL_OBJECT * input, FL_OBJECT * label)
69         : input_(input), label_(label ? label : input)
70 {
71         lyx::Assert(input && input->objclass == FL_INPUT);
72 }
73
74
75 bool CheckedLyXLength::check() const
76 {
77         string const str = getString(input_);
78         bool const valid = !isActive(input_) || str.empty()
79                 || isStrDbl(str) || isValidLength(str);
80
81         // set the color of label and input widget
82         setWidget(valid, input_, label_);
83
84         return valid;
85 }
86
87
88 CheckedGlueLength::CheckedGlueLength(FL_OBJECT * input, FL_OBJECT * label)
89         : input_(input), label_(label ? label : input)
90 {
91         lyx::Assert(input && input->objclass == FL_INPUT);
92 }
93
94
95 bool CheckedGlueLength::check() const
96 {
97         string const str = getString(input_);
98         bool const valid = !isActive(input_) || str.empty()
99                 || isStrDbl(str) || isValidGlueLength(str);
100
101         // set the color of label and input widget
102         setWidget(valid, input_, label_);
103
104         return valid;
105 }