]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/ButtonController.h
More pref work from Angus
[lyx.git] / src / frontends / xforms / ButtonController.h
1 // -*- C++ -*-
2 /* ButtonController.h
3  * Controls the activation of the OK, Apply and Cancel buttons.
4  * Actually supports 4 buttons in all and it's up to the user to decide on
5  * the activation policy and which buttons correspond to which output of the
6  * state machine.
7  * Author: Allan Rae <rae@lyx.org>
8  * This file is part of
9  * ======================================================
10  *
11  *           LyX, The Document Processor
12  *
13  *           Copyright 1995 Matthias Ettrich
14  *           Copyright 1995-2000 The LyX Team.
15  *
16  *           This file Copyright 2000
17  *           Allan Rae
18  * ======================================================
19  */
20
21 #ifndef BUTTONCONTROLLER_H
22 #define BUTTONCONTROLLER_H
23
24 #include "ButtonPolicies.h"
25 #include <list>
26
27 /** General purpose button controller for up to four buttons.
28     Controls the activation of the OK, Apply and Cancel buttons.
29     Actually supports 4 buttons in all and it's up to the user to decide on
30     the activation policy and which buttons correspond to which output of the
31     state machine.
32     @author Allan Rae <rae@lyx.org>
33     20001001 Switch from template implementation to taking Policy parameter.
34              Allows FormBase to provide a ButtonController for any dialog.
35 */
36 class ButtonController : public noncopyable
37 {
38 public:
39         /** Constructor.
40             The cancel/close label entries are _not_ managed within the class
41             thereby allowing you to reassign at will and to use static labels.
42             It also means if you really don't want to have the Cancel button
43             label be different when there is nothing changed in the dialog then
44             you can just assign "Cancel" to both labels.  Or even reuse this
45             class for something completely different.
46          */
47         ButtonController(ButtonPolicy * bp, char const * cancel, char const * close)
48                 : bp_(bp), okay_(0), apply_(0), cancel_(0), undo_all_(0),
49                   read_only_(), cancel_label(cancel), close_label(close) {}
50         /// Somebody else owns the FL_OBJECTs we just manipulate them.
51         ~ButtonController() {}
52
53         /* Initialise Button Functions */
54         /// Call refresh() when finished setting the buttons.
55         void setOK(FL_OBJECT * obj) {
56                 okay_ = obj;
57         }
58         ///
59         void setApply(FL_OBJECT * obj) {
60                 apply_ = obj;
61         }
62         ///
63         void setCancel(FL_OBJECT * obj) {
64                 cancel_ = obj;
65         }
66         ///
67         void setUndoAll(FL_OBJECT * obj) {
68                 undo_all_ = obj;
69         }
70         ///
71         void setCancelTrueLabel(char const * c) {
72                 cancel_label = c;
73         }
74         ///
75         void setCancelFalseLabel(char const * c) {
76                 close_label = c;
77         }
78         ///
79         void addReadOnly(FL_OBJECT * obj) {
80                 read_only_.push_front(obj);
81         }
82         ///
83         void eraseReadOnly() {
84                 read_only_.erase(read_only_.begin(), read_only_.end());
85         }
86
87         /* Action Functions */
88         /// force a refresh of the buttons
89         void refresh() {
90                 if (okay_) {
91                         if (bp_->buttonStatus(ButtonPolicy::OKAY)) {
92                                 fl_activate_object(okay_);
93                                 fl_set_object_lcol(okay_, FL_BLACK);
94                         } else {
95                                 fl_deactivate_object(okay_);
96                                 fl_set_object_lcol(okay_, FL_INACTIVE);
97                         }
98                 }
99                 if (apply_) {
100                         if (bp_->buttonStatus(ButtonPolicy::APPLY)) {
101                                 fl_activate_object(apply_);
102                                 fl_set_object_lcol(apply_, FL_BLACK);
103                         } else {
104                                 fl_deactivate_object(apply_);
105                                 fl_set_object_lcol(apply_, FL_INACTIVE);
106                         }
107                 }
108                 if (undo_all_) {
109                         if (bp_->buttonStatus(ButtonPolicy::UNDO_ALL)) {
110                                 fl_activate_object(undo_all_);
111                                 fl_set_object_lcol(undo_all_, FL_BLACK);
112                         } else {
113                                 fl_deactivate_object(undo_all_);
114                                 fl_set_object_lcol(undo_all_,
115                                                    FL_INACTIVE);
116                         }
117                 }
118                 if (cancel_) {
119                         if (bp_->buttonStatus(ButtonPolicy::CANCEL)) {
120                                 fl_set_object_label(cancel_,
121                                                     cancel_label);
122                         } else {
123                                 fl_set_object_label(cancel_,
124                                                     close_label);
125                         }
126                 }
127                 if (!read_only_.empty()) {
128                         if (bp_->isReadOnly()) {
129                                 std::list<FL_OBJECT *>::iterator
130                                         end = read_only_.end();
131                                 for (std::list<FL_OBJECT *>::iterator
132                                      iter = read_only_.begin();
133                                      iter != end;
134                                      ++iter) {
135                                         fl_deactivate_object(*iter);
136                                         fl_set_object_lcol(*iter,
137                                                            FL_INACTIVE);
138                                 }
139                         } else {
140                                 std::list<FL_OBJECT *>::iterator
141                                         end = read_only_.end();
142                                 for (std::list<FL_OBJECT *>::iterator
143                                      iter = read_only_.begin();
144                                      iter != end;
145                                      ++iter) {
146                                         fl_activate_object(*iter);
147                                         fl_set_object_lcol(*iter,
148                                                            FL_BLACK);
149                                 }
150                         }
151                 }
152         }
153         ///
154         void input(ButtonPolicy::SMInput in) {
155                 bp_->input(in);
156                 refresh();
157         }
158         ///
159         void ok() {
160                 input(ButtonPolicy::SMI_OKAY);
161         }
162         ///
163         void apply() {
164                 input(ButtonPolicy::SMI_APPLY);
165         }
166         ///
167         void cancel() {
168                 input(ButtonPolicy::SMI_CANCEL);
169         }
170         ///
171         void undoAll() {
172                 input(ButtonPolicy::SMI_UNDO_ALL);
173         }
174         ///
175         void hide() {
176                 input(ButtonPolicy::SMI_HIDE);
177         }
178         /// Passthrough function -- returns its input value
179         bool readOnly(bool ro = true) {
180                 if (ro) {
181                         input(ButtonPolicy::SMI_READ_ONLY);
182                 } else {
183                         input(ButtonPolicy::SMI_READ_WRITE);
184                 }
185                 return ro;
186         }
187         ///
188         void readWrite() {
189                 readOnly(false);
190         }
191         /// Passthrough function -- returns its input value
192         bool valid(bool v = true) { 
193                 if (v) {
194                         input(ButtonPolicy::SMI_VALID);
195                 } else {
196                         input(ButtonPolicy::SMI_INVALID);
197                 }
198                 return v;
199         }
200         ///
201         void invalid() {
202                 valid(false);
203         }
204 private:
205         ///
206         ButtonPolicy * bp_;
207         ///
208         FL_OBJECT * okay_;
209         ///
210         FL_OBJECT * apply_;
211         ///
212         FL_OBJECT * cancel_;
213         ///
214         FL_OBJECT * undo_all_;
215         /// List of items to be deactivated when in one of the read-only states
216         std::list<FL_OBJECT *> read_only_;
217         ///
218         char const * cancel_label;
219         ///
220         char const * close_label;       
221 };
222
223 #endif