]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/ButtonController.h
Dekel's Spacing.C patch + Garst's setOK complaint + use the right policy for FormDocument
[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 */
33 template <class Policy>
34 class ButtonController : public noncopyable
35 {
36 public:
37         /**@name Constructors and Deconstructors */
38         //@{
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(char const * cancel, char const * close)
48                 : 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
54         /**@name Initialise Button Functions */
55         //@{
56         /// Call refresh() when finished setting the buttons.
57         void setOK(FL_OBJECT * obj)
58                 { okay_ = obj; }
59         ///
60         void setApply(FL_OBJECT * obj)
61                 { apply_ = obj; }
62         ///
63         void setCancel(FL_OBJECT * obj)
64                 { cancel_ = obj; }
65         ///
66         void setUndoAll(FL_OBJECT * obj)
67                 { undo_all_ = obj; }
68         ///
69         void setCancelTrueLabel(char const * c)
70                 { cancel_label = c; }
71         ///
72         void setCancelFalseLabel(char const * c)
73                 { close_label = c; }
74         ///
75         void addReadOnly(FL_OBJECT * obj)
76                 { read_only_.push_front(obj); }
77         ///
78         void eraseReadOnly()
79                 { read_only_.erase(read_only_.begin(), read_only_.end()); }
80         //@}
81
82         /**@name Action Functions */
83         //@{
84         ///
85         void input(ButtonPolicy::SMInput in)
86                 {
87                         bp_.input(in);
88                         refresh();
89                 }
90         ///
91         void ok()
92                 { input(ButtonPolicy::SMI_OKAY); }
93         ///
94         void apply()
95                 { input(ButtonPolicy::SMI_APPLY); }
96         ///
97         void cancel()
98                 { input(ButtonPolicy::SMI_CANCEL); }
99         ///
100         void undoAll()
101                 { input(ButtonPolicy::SMI_UNDO_ALL); }
102         ///
103         void hide()
104                 { input(ButtonPolicy::SMI_HIDE); }
105         /// Passthrough function -- returns its input value
106         bool read_only(bool ro = true)
107                 {
108                         if (ro) {
109                                 input(ButtonPolicy::SMI_READ_ONLY);
110                         } else {
111                                 input(ButtonPolicy::SMI_READ_WRITE);
112                         }
113                         return ro;
114                 }
115         ///
116         void read_write()
117                 { read_only(false); }
118         /// Passthrough function -- returns its input value
119         bool valid(bool v = true)
120                 { 
121                         if (v) {
122                                 input(ButtonPolicy::SMI_VALID);
123                         } else {
124                                 input(ButtonPolicy::SMI_INVALID);
125                         }
126                         return v;
127                 }
128         ///
129         void invalid()
130                 { valid(false); }
131         /// force a refresh of the buttons
132         void refresh()
133                 {
134                         if (okay_) {
135                                 if (bp_.buttonStatus(ButtonPolicy::OKAY)) {
136                                         fl_activate_object(okay_);
137                                         fl_set_object_lcol(okay_, FL_BLACK);
138                                 } else {
139                                         fl_deactivate_object(okay_);
140                                         fl_set_object_lcol(okay_, FL_INACTIVE);
141                                 }
142                         }
143                         if (apply_) {
144                                 if (bp_.buttonStatus(ButtonPolicy::APPLY)) {
145                                         fl_activate_object(apply_);
146                                         fl_set_object_lcol(apply_, FL_BLACK);
147                                 } else {
148                                         fl_deactivate_object(apply_);
149                                         fl_set_object_lcol(apply_, FL_INACTIVE);
150                                 }
151                         }
152                         if (undo_all_) {
153                                 if (bp_.buttonStatus(ButtonPolicy::UNDO_ALL)) {
154                                         fl_activate_object(undo_all_);
155                                         fl_set_object_lcol(undo_all_, FL_BLACK);
156                                 } else {
157                                         fl_deactivate_object(undo_all_);
158                                         fl_set_object_lcol(undo_all_,
159                                                            FL_INACTIVE);
160                                 }
161                         }
162                         if (cancel_) {
163                                 if (bp_.buttonStatus(ButtonPolicy::CANCEL)) {
164                                         fl_set_object_label(cancel_,
165                                                             cancel_label);
166                                 } else {
167                                         fl_set_object_label(cancel_,
168                                                             close_label);
169                                 }
170                         }
171                         if (!read_only_.empty()) {
172                                 if (bp_.isReadOnly()) {
173                                         for (std::list<FL_OBJECT *>::iterator
174                                                      iter = read_only_.begin();
175                                              iter != read_only_.end();
176                                              ++iter) {
177                                                 fl_deactivate_object(*iter);
178                                                 fl_set_object_lcol(undo_all_,
179                                                                    FL_INACTIVE);
180                                         }
181                                 } else {
182                                         for (std::list<FL_OBJECT *>::iterator
183                                                      iter = read_only_.begin();
184                                              iter != read_only_.end();
185                                              ++iter) {
186                                                 fl_activate_object(undo_all_);
187                                                 fl_set_object_lcol(undo_all_,
188                                                                    FL_BLACK);
189                                         }
190                                 }
191                         }
192                 }
193         //@}
194 private:
195         ///
196         Policy bp_;
197         /**@name Button Widgets */
198         //@{
199         ///
200         FL_OBJECT * okay_;
201         ///
202         FL_OBJECT * apply_;
203         ///
204         FL_OBJECT * cancel_;
205         ///
206         FL_OBJECT * undo_all_;
207         /// List of items to be deactivated when in one of the read-only states
208         std::list<FL_OBJECT *> read_only_;
209         //@}
210         /**@name Cancel/Close Button Labels */
211         //@{
212         ///
213         char const * cancel_label;
214         ///
215         char const * close_label;       
216         //@}
217 };
218
219 #endif