]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/ButtonController.h
cleanup ButtonController and rename a few things in ButtonPolicies
[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 */
34 template <class Policy>
35 class ButtonController : public noncopyable
36 {
37 public:
38         /** Constructor.
39             The cancel/close label entries are _not_ managed within the class
40             thereby allowing you to reassign at will and to use static labels.
41             It also means if you really don't want to have the Cancel button
42             label be different when there is nothing changed in the dialog then
43             you can just assign "Cancel" to both labels.  Or even reuse this
44             class for something completely different.
45          */
46         ButtonController(char const * cancel, char const * close)
47                 : bp_(), okay_(0), apply_(0), cancel_(0), undo_all_(0),
48                   read_only_(), cancel_label(cancel), close_label(close) {}
49         /// Somebody else owns the FL_OBJECTs we just manipulate them.
50         ~ButtonController() {}
51
52         /* Initialise Button Functions */
53         /// Call refresh() when finished setting the buttons.
54         void setOK(FL_OBJECT * obj) {
55                 okay_ = obj;
56         }
57         ///
58         void setApply(FL_OBJECT * obj) {
59                 apply_ = obj;
60         }
61         ///
62         void setCancel(FL_OBJECT * obj) {
63                 cancel_ = obj;
64         }
65         ///
66         void setUndoAll(FL_OBJECT * obj) {
67                 undo_all_ = obj;
68         }
69         ///
70         void setCancelTrueLabel(char const * c) {
71                 cancel_label = c;
72         }
73         ///
74         void setCancelFalseLabel(char const * c) {
75                 close_label = c;
76         }
77         ///
78         void addReadOnly(FL_OBJECT * obj) {
79                 read_only_.push_front(obj);
80         }
81         ///
82         void eraseReadOnly() {
83                 read_only_.erase(read_only_.begin(), read_only_.end());
84         }
85
86         /* Action Functions */
87         ///
88         void input(ButtonPolicy::SMInput in) {
89                 bp_.input(in);
90                 refresh();
91         }
92         ///
93         void ok() {
94                 input(ButtonPolicy::SMI_OKAY);
95         }
96         ///
97         void apply() {
98                 input(ButtonPolicy::SMI_APPLY);
99         }
100         ///
101         void cancel() {
102                 input(ButtonPolicy::SMI_CANCEL);
103         }
104         ///
105         void undoAll() {
106                 input(ButtonPolicy::SMI_UNDO_ALL);
107         }
108         ///
109         void hide() {
110                 input(ButtonPolicy::SMI_HIDE);
111         }
112         /// Passthrough function -- returns its input value
113         bool readOnly(bool ro = true) {
114                 if (ro) {
115                         input(ButtonPolicy::SMI_READ_ONLY);
116                 } else {
117                         input(ButtonPolicy::SMI_READ_WRITE);
118                 }
119                 return ro;
120         }
121         ///
122         void readWrite() {
123                 read_only(false);
124         }
125         /// Passthrough function -- returns its input value
126         bool valid(bool v = true) { 
127                 if (v) {
128                         input(ButtonPolicy::SMI_VALID);
129                 } else {
130                         input(ButtonPolicy::SMI_INVALID);
131                 }
132                 return v;
133         }
134         ///
135         void invalid() {
136                 valid(false);
137         }
138         /// force a refresh of the buttons
139         void refresh() {
140                 if (okay_) {
141                         if (bp_.buttonStatus(ButtonPolicy::OKAY)) {
142                                 fl_activate_object(okay_);
143                                 fl_set_object_lcol(okay_, FL_BLACK);
144                         } else {
145                                 fl_deactivate_object(okay_);
146                                 fl_set_object_lcol(okay_, FL_INACTIVE);
147                         }
148                 }
149                 if (apply_) {
150                         if (bp_.buttonStatus(ButtonPolicy::APPLY)) {
151                                 fl_activate_object(apply_);
152                                 fl_set_object_lcol(apply_, FL_BLACK);
153                         } else {
154                                 fl_deactivate_object(apply_);
155                                 fl_set_object_lcol(apply_, FL_INACTIVE);
156                         }
157                 }
158                 if (undo_all_) {
159                         if (bp_.buttonStatus(ButtonPolicy::UNDO_ALL)) {
160                                 fl_activate_object(undo_all_);
161                                 fl_set_object_lcol(undo_all_, FL_BLACK);
162                         } else {
163                                 fl_deactivate_object(undo_all_);
164                                 fl_set_object_lcol(undo_all_,
165                                                    FL_INACTIVE);
166                         }
167                 }
168                 if (cancel_) {
169                         if (bp_.buttonStatus(ButtonPolicy::CANCEL)) {
170                                 fl_set_object_label(cancel_,
171                                                     cancel_label);
172                         } else {
173                                 fl_set_object_label(cancel_,
174                                                     close_label);
175                         }
176                 }
177                 if (!read_only_.empty()) {
178                         if (bp_.isReadOnly()) {
179                                 std::list<FL_OBJECT *>::iterator
180                                         end = read_only_.end();
181                                 for (std::list<FL_OBJECT *>::iterator
182                                      iter = read_only_.begin();
183                                      iter != end;
184                                      ++iter) {
185                                         fl_deactivate_object(*iter);
186                                         fl_set_object_lcol(*iter,
187                                                            FL_INACTIVE);
188                                 }
189                         } else {
190                                 std::list<FL_OBJECT *>::iterator
191                                         end = read_only_.end();
192                                 for (std::list<FL_OBJECT *>::iterator
193                                      iter = read_only_.begin();
194                                      iter != end;
195                                      ++iter) {
196                                         fl_activate_object(*iter);
197                                         fl_set_object_lcol(*iter,
198                                                            FL_BLACK);
199                                 }
200                         }
201                 }
202         }
203 private:
204         ///
205         Policy bp_;
206         ///
207         FL_OBJECT * okay_;
208         ///
209         FL_OBJECT * apply_;
210         ///
211         FL_OBJECT * cancel_;
212         ///
213         FL_OBJECT * undo_all_;
214         /// List of items to be deactivated when in one of the read-only states
215         std::list<FL_OBJECT *> read_only_;
216         ///
217         char const * cancel_label;
218         ///
219         char const * close_label;       
220 };
221
222 #endif