]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/ButtonController.C
extended ButtonController so that only FL_OBJECT *s in a trigger_change_
[lyx.git] / src / frontends / xforms / ButtonController.C
1 #include <config.h>
2
3 #include FORMS_H_LOCATION
4
5 #ifdef __GNUG__
6 #pragma implementation
7 #endif
8
9 #include "ButtonController.h"
10 #include "support/LAssert.h"
11 #include "gettext.h" // _()
12 //#include "debug.h"
13
14 using std::find;
15 using std::vector;
16
17 ButtonController::ButtonController(ButtonPolicy * bp,
18                                    char const * cancel, char const * close)
19         : bp_(bp), okay_(0), apply_(0), cancel_(0), undo_all_(0),
20           read_only_(), trigger_change_(),
21           cancel_label(cancel), close_label(close)
22 {
23         Assert(bp);
24 }
25
26
27 void ButtonController::refresh()
28 {
29         if (okay_) {
30                 if (bp_->buttonStatus(ButtonPolicy::OKAY)) {
31                         fl_activate_object(okay_);
32                         fl_set_object_lcol(okay_, FL_BLACK);
33                 } else {
34                         fl_deactivate_object(okay_);
35                         fl_set_object_lcol(okay_, FL_INACTIVE);
36                 }
37         }
38         if (apply_) {
39                 if (bp_->buttonStatus(ButtonPolicy::APPLY)) {
40                         fl_activate_object(apply_);
41                         fl_set_object_lcol(apply_, FL_BLACK);
42                 } else {
43                         fl_deactivate_object(apply_);
44                         fl_set_object_lcol(apply_, FL_INACTIVE);
45                 }
46         }
47         if (undo_all_) {
48                 if (bp_->buttonStatus(ButtonPolicy::UNDO_ALL)) {
49                         fl_activate_object(undo_all_);
50                         fl_set_object_lcol(undo_all_, FL_BLACK);
51                 } else {
52                         fl_deactivate_object(undo_all_);
53                         fl_set_object_lcol(undo_all_,
54                                            FL_INACTIVE);
55                 }
56         }
57         if (cancel_) {
58                 if (bp_->buttonStatus(ButtonPolicy::CANCEL)) {
59                         fl_set_object_label(cancel_,
60                                             _(cancel_label));
61                 } else {
62                         fl_set_object_label(cancel_,
63                                             _(close_label));
64                 }
65         }
66         if (!read_only_.empty()) {
67                 if (bp_->isReadOnly()) {
68                         std::list<FL_OBJECT *>::iterator
69                                 end = read_only_.end();
70                         for (std::list<FL_OBJECT *>::iterator
71                                      iter = read_only_.begin();
72                              iter != end;
73                              ++iter) {
74                                 fl_deactivate_object(*iter);
75                                 fl_set_object_lcol(*iter,
76                                                    FL_INACTIVE);
77                         }
78                 } else {
79                         std::list<FL_OBJECT *>::iterator
80                                 end = read_only_.end();
81                         for (std::list<FL_OBJECT *>::iterator
82                                      iter = read_only_.begin();
83                              iter != end;
84                              ++iter) {
85                                 fl_activate_object(*iter);
86                                 fl_set_object_lcol(*iter,
87                                                    FL_BLACK);
88                         }
89                 }
90         }
91 }
92
93
94 void ButtonController::input(ButtonPolicy::SMInput in)
95 {
96         //lyxerr << "ButtonController::input: bp_[" << bp_ << "]" << endl;
97         bp_->input(in);
98         refresh();
99 }
100
101
102 void ButtonController::ok()
103 {
104         input(ButtonPolicy::SMI_OKAY);
105 }
106
107
108 void ButtonController::apply()
109 {
110         input(ButtonPolicy::SMI_APPLY);
111 }
112
113
114 void ButtonController::cancel()
115 {
116         input(ButtonPolicy::SMI_CANCEL);
117 }
118
119
120 void ButtonController::undoAll()
121 {
122         input(ButtonPolicy::SMI_UNDO_ALL);
123 }
124
125
126 void ButtonController::hide()
127 {
128         input(ButtonPolicy::SMI_HIDE);
129 }
130
131
132 bool ButtonController::readOnly(bool ro)
133 {
134         if (ro) {
135                 input(ButtonPolicy::SMI_READ_ONLY);
136         } else {
137                 input(ButtonPolicy::SMI_READ_WRITE);
138         }
139         return ro;
140 }
141
142
143 void ButtonController::readWrite()
144 {
145         readOnly(false);
146 }
147
148
149 bool ButtonController::valid(bool v, FL_OBJECT * obj)
150
151         if (obj && !trigger_change_.empty()) {
152                 vector<FL_OBJECT *>::const_iterator cit =
153                         find(trigger_change_.begin(), trigger_change_.end(),
154                              obj);
155
156                 // Only trigger a change if the obj is in the list
157                 if (cit != trigger_change_.end()) {
158                         if (v) {
159                                 input(ButtonPolicy::SMI_VALID);
160                         } else {
161                                 input(ButtonPolicy::SMI_INVALID);
162                         }
163                 }
164         } else {
165                 if (v) {
166                         input(ButtonPolicy::SMI_VALID);
167                 } else {
168                         input(ButtonPolicy::SMI_INVALID);
169                 }
170         }
171         
172         return v;
173 }
174
175
176 void ButtonController::invalid()
177 {
178         valid(false);
179 }