]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/ButtonController.h
major GUII cleanup + Baruchs patch + Angus's patch + removed a couple of generated...
[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
26 /** General purpose button controller for up to four buttons.
27     Controls the activation of the OK, Apply and Cancel buttons.
28     Actually supports 4 buttons in all and it's up to the user to decide on
29     the activation policy and which buttons correspond to which output of the
30     state machine.
31 */
32 template <class Policy>
33 class ButtonController : public noncopyable
34 {
35 public:
36         /**@name Constructors and Deconstructors */
37         //@{
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                   cancel_label(cancel), close_label(close) {}
49         /// Somebody else owns the FL_OBJECTs we just manipulate them.
50         ~ButtonController() {}
51         //@}
52
53         /**@name Initialise Button Functions */
54         //@{
55         /// Call refresh() when finished setting the buttons.
56         void setOkay(FL_OBJECT * obj)
57                 { okay_ = obj; }
58         ///
59         void setApply(FL_OBJECT * obj)
60                 { apply_ = obj; }
61         ///
62         void setCancel(FL_OBJECT * obj)
63                 { cancel_ = obj; }
64         ///
65         void setUndoAll(FL_OBJECT * obj)
66                 { undo_all_ = obj; }
67         ///
68         void setCancelTrueLabel(char const * c)
69                 { cancel_label = c; }
70         ///
71         void setCancelFalseLabel(char const * c)
72                 { close_label = c; }
73         //@}
74
75         /**@name Action Functions */
76         //@{
77         ///
78         void input(ButtonPolicy::SMInput in)
79                 {
80                         bp_.input(in);
81                         refresh();
82                 }
83         ///
84         void okay()
85                 { input(ButtonPolicy::SMI_OKAY); }
86         ///
87         void apply()
88                 { input(ButtonPolicy::SMI_APPLY); }
89         ///
90         void cancel()
91                 { input(ButtonPolicy::SMI_CANCEL); }
92         ///
93         void undoAll()
94                 { input(ButtonPolicy::SMI_UNDO_ALL); }
95         ///
96         void hide()
97                 { input(ButtonPolicy::SMI_HIDE); }
98         /// Passthrough function -- returns its input value
99         bool read_only(bool ro = true)
100                 {
101                         if (ro) {
102                                 input(ButtonPolicy::SMI_READ_ONLY);
103                         } else {
104                                 input(ButtonPolicy::SMI_READ_WRITE);
105                         }
106                         return ro;
107                 }
108         ///
109         void read_write()
110                 { read_only(false); }
111         /// Passthrough function -- returns its input value
112         bool valid(bool v = true)
113                 { 
114                         if (v) {
115                                 input(ButtonPolicy::SMI_VALID);
116                         } else {
117                                 input(ButtonPolicy::SMI_INVALID);
118                         }
119                         return v;
120                 }
121         ///
122         void invalid()
123                 { valid(false); }
124         /// force a refresh of the buttons
125         void refresh()
126                 {
127                         if (okay_) {
128                                 if (bp_.buttonStatus(ButtonPolicy::OKAY)) {
129                                         fl_activate_object(okay_);
130                                         fl_set_object_lcol(okay_, FL_BLACK);
131                                 } else {
132                                         fl_deactivate_object(okay_);
133                                         fl_set_object_lcol(okay_, FL_INACTIVE);
134                                 }
135                         }
136                         if (apply_) {
137                                 if (bp_.buttonStatus(ButtonPolicy::APPLY)) {
138                                         fl_activate_object(apply_);
139                                         fl_set_object_lcol(apply_, FL_BLACK);
140                                 } else {
141                                         fl_deactivate_object(apply_);
142                                         fl_set_object_lcol(apply_, FL_INACTIVE);
143                                 }
144                         }
145                         if (undo_all_) {
146                                 if (bp_.buttonStatus(ButtonPolicy::UNDO_ALL)) {
147                                         fl_activate_object(undo_all_);
148                                         fl_set_object_lcol(undo_all_, FL_BLACK);
149                                 } else {
150                                         fl_deactivate_object(undo_all_);
151                                         fl_set_object_lcol(undo_all_,
152                                                            FL_INACTIVE);
153                                 }
154                         }
155                         if (cancel_) {
156                                 if (bp_.buttonStatus(ButtonPolicy::CANCEL)) {
157                                         fl_set_object_label(cancel_,
158                                                             cancel_label);
159                                 } else {
160                                         fl_set_object_label(cancel_,
161                                                             close_label);
162                                 }
163                         }
164                 }
165         //@}
166 private:
167         ///
168         Policy bp_;
169         /**@name Button Widgets */
170         //@{
171         ///
172         FL_OBJECT * okay_;
173         ///
174         FL_OBJECT * apply_;
175         ///
176         FL_OBJECT * cancel_;
177         ///
178         FL_OBJECT * undo_all_;
179         //@}
180         /**@name Cancel/Close Button Labels */
181         //@{
182         ///
183         char const * cancel_label;
184         ///
185         char const * close_label;       
186         //@}
187 };
188
189 #endif