3 * \file ButtonPolicies.h
4 * Read the file COPYING
8 * Full author contact details are available in file CREDITS
10 * Provides a state machine implementation of the various button policies
11 * used by the dialogs.
14 #ifndef BUTTONPOLICIES_H
15 #define BUTTONPOLICIES_H
19 #include <boost/utility.hpp>
21 #include "support/LOstream.h"
23 /** An abstract base class for button policies.
24 A state machine implementation of the various button policies used by the
25 dialogs. Only the policy is implemented here. Separate ButtonController
26 classes are needed for each GUI implementation.
28 Policy | ReadOnly | Apply Button | Repeated Apply
29 ========================================================================
31 OkCancelReadOnly | Y | N | -
32 OkApplyCancel | N | Y | Y
33 OkApplyCancelReadOnly | Y | Y | Y
34 NoRepeatedApply | N | Y | N
35 NoRepeatedApplyReadOnly | Y | Y | N
36 Preferences | N | Y | No (Ok-Close)
37 Ignorant | N/A | N/A | N/A
38 ========================================================================
41 The name of the policy
43 Does the policy treat read-only docs differently to read-write docs?
44 This usually means that when an SMI_READ_ONLY input arrives then
45 all the buttons are disabled except Cancel/Close. The state
46 machine tracks the inputs (valid/invalid) and has states for all
47 combinations. When an SMI_READ_WRITE input arrives the appropriate
48 machine state is entered (just as if the document had always been
50 NOTE: If a dialog doesn't care about the read-only status of a document
51 (and uses an appropriate policy) it can never get into a read-only state
52 so isReadOnly() can only ever return false even though the document may
55 Simply means that it is alright to use the Apply button multiple times
56 without requiring a change of the dialog contents. If no repeating is
57 allowed the Ok+Apply buttons are deactivated. The Preferences dialog
58 has its own special version of repeated apply handling because its Ok
59 button is actually a Save button -- its always reasonable to Save the
60 preferences if the dialog has changed since the last save.
62 The IgnorantPolicy is a special case that allows anything.
64 class ButtonPolicy : boost::noncopyable {
67 virtual ~ButtonPolicy() {}
69 /** The various possible state names.
70 Not all state-machines have this many states. However, we need
71 to define them all here so we can share the code.
94 /// The various button types.
97 CLOSE = 0, // Not a real button, but effectively !CANCEL
108 static const Button ALL_BUTTONS =
109 Button(OKAY | APPLY | CANCEL | RESTORE);
111 /** State machine inputs.
112 All the policies so far have both CANCEL and HIDE always going to
113 INITIAL. This won't necessarily be true for all [future] policies
114 though so I'll leave those two as distinct inputs rather than merge
115 them. For example, a dialog that doesn't update it's input fields
116 when reshown after being hidden needs a policy where CANCEL and
117 HIDE are treated differently.
120 /// the dialog contents are now valid
122 /// the dialog contents are now invalid
124 /// an apply-and-hide action has happened
126 /// an apply action has happened
128 /// a cancel action has happened
130 /// a restore action has happened
132 /// the dialog has been hidden
134 /// the dialog contents are read-only
136 /// the dialog contents can be modified
138 /// the state of the dialog contents has not changed
144 /// Trigger a transition with this input.
145 virtual void input(SMInput) = 0;
146 /// Activation status of a button
147 virtual bool buttonStatus(Button) const = 0;
148 /// Are we in a read-only state?
149 virtual bool isReadOnly() const = 0;
151 /// Transition map of the state machine.
152 typedef std::vector<State> StateArray;
154 typedef std::vector<StateArray> StateMachine;
155 /// The state outputs are the status of the buttons.
156 typedef std::vector<int> StateOutputs;
161 std::ostream & operator<<(std::ostream & os, ButtonPolicy::State st)
169 std::ostream & operator<<(std::ostream & os, ButtonPolicy::SMInput smi)
176 //--------------------- Actual Policy Classes -----------------------------
178 /** Ok and Cancel buttons for dialogs with read-only operation.
179 Note: This scheme supports the relabelling of Cancel to Close and
181 This is based on the value of the bool state of the Button::CANCEL.
182 true == Cancel, false == Close
184 class OkCancelPolicy : public ButtonPolicy {
189 //virtual ~OkCancelPolicy() {}
191 /// Trigger a transition with this input.
192 virtual void input(SMInput);
193 /** Activation status of a button.
194 We assume that we haven't gotten into an undefined state.
195 This is reasonable since we can only reach states defined
196 in the state machine and they should all have been defined in
197 the outputs_ variable. Perhaps we can do something at compile
198 time to check that all the states have corresponding outputs.
200 virtual bool buttonStatus(Button button) const {
201 return button & outputs_[state_];
203 /// Are we in a read-only state?
204 virtual bool isReadOnly() const {
210 /// Which buttons are active for a given state.
211 StateOutputs outputs_;
213 StateMachine state_machine_;
216 /** Ok and Cancel buttons for dialogs where read-only operation is blocked.
217 The state machine design for this policy allows changes to occur within
218 the dialog while a file is read-only -- the okay button is disabled until
219 a read-write input is given. When the file is made read-write the dialog
220 will then be in the correct state (as if the file had always been
222 Note: This scheme supports the relabelling of Cancel to Close
224 This is based on the value of the bool state of the Button::CANCEL.
225 true == Cancel, false == Close
227 class OkCancelReadOnlyPolicy : public ButtonPolicy {
230 OkCancelReadOnlyPolicy();
232 //virtual ~OkCancelReadOnlyPolicy() {}
234 /// Trigger a transition with this input.
235 virtual void input(SMInput);
236 /// Activation status of a button.
237 virtual bool buttonStatus(Button button) const {
238 return button & outputs_[state_];
240 /// Are we in a read-only state?
241 virtual bool isReadOnly() const {
242 return RO_INITIAL == state_
243 || RO_VALID == state_
244 || RO_INVALID == state_
245 || RO_APPLIED == state_;
250 /// Which buttons are active for a given state.
251 StateOutputs outputs_;
253 StateMachine state_machine_;
257 /** Ok, Apply and Cancel buttons for dialogs where read-only operation
259 Repeated Apply are not allowed. Likewise, Ok cannot follow Apply without
260 some valid input. That is, the dialog contents must change between
261 each Apply or Apply and Ok.
262 The state machine design for this policy allows changes to occur within
263 the dialog while a file is read-only -- the Ok+Apply buttons are disabled
264 until a read-write input is given. When the file is made read-write the
265 dialog will then be in the correct state (as if the file had always been
267 Note: This scheme supports the relabelling of Cancel to Close
269 This is based on the value of the bool state of the Button::CANCEL.
270 true == Cancel, false == Close
272 class NoRepeatedApplyReadOnlyPolicy : public ButtonPolicy
276 NoRepeatedApplyReadOnlyPolicy();
278 //virtual ~NoRepeatedApplyReadOnlyPolicy() {}
280 /// Trigger a transition with this input.
281 virtual void input(SMInput);
282 /// Activation status of a button.
283 virtual bool buttonStatus(Button button) const {
284 return button & outputs_[state_];
286 /// Are we in a read-only state?
287 virtual bool isReadOnly() const {
288 return RO_INITIAL == state_
289 || RO_VALID == state_
290 || RO_INVALID == state_
291 || RO_APPLIED == state_;
296 /// Which buttons are active for a given state.
297 StateOutputs outputs_;
299 StateMachine state_machine_;
303 /** Ok, Apply and Cancel buttons for dialogs where read-only
304 operation is blocked.
305 Repeated Apply is allowed. Likewise, Ok can follow Apply.
306 The state machine design for this policy allows changes to occur within
307 the dialog while a file is read-only -- the Ok+Apply buttons are disabled
308 until a read-write input is given. When the file is made read-write the
309 dialog will then be in the correct state (as if the file had always been
311 Note: This scheme supports the relabelling of Cancel to Close
313 This is based on the value of the bool state of the Button::CANCEL.
314 true == Cancel, false == Close
316 class OkApplyCancelReadOnlyPolicy : public ButtonPolicy {
319 OkApplyCancelReadOnlyPolicy();
321 //virtual ~OkApplyCancelReadOnlyPolicy() {}
323 /// Trigger a transition with this input.
324 virtual void input(SMInput);
325 /// Activation status of a button.
326 virtual bool buttonStatus(Button button) const {
327 return button & outputs_[state_];
329 /// Are we in a read-only state?
330 virtual bool isReadOnly() const {
331 return RO_INITIAL == state_
332 || RO_VALID == state_
333 || RO_INVALID == state_
334 || RO_APPLIED == state_;
339 /// Which buttons are active for a given state.
340 StateOutputs outputs_;
342 StateMachine state_machine_;
346 /** Ok, Apply and Cancel buttons for dialogs where repeated Apply is allowed.
347 Note: This scheme supports the relabelling of Cancel to Close
349 This is based on the value of the bool state of the Button::CANCEL.
350 true == Cancel, false == Close
352 class OkApplyCancelPolicy : public ButtonPolicy {
355 OkApplyCancelPolicy();
357 //virtual ~OkApplyCancelPolicy() {}
359 /// Trigger a transition with this input.
360 virtual void input(SMInput);
361 /// Activation status of a button.
362 virtual bool buttonStatus(Button button) const {
363 return button & outputs_[state_];
365 /// Are we in a read-only state?
366 virtual bool isReadOnly() const {
372 /// Which buttons are active for a given state.
373 StateOutputs outputs_;
375 StateMachine state_machine_;
379 /** Ok, Apply and Cancel buttons for dialogs with no repeated Apply.
380 Note: This scheme supports the relabelling of Cancel to Close
382 This is based on the value of the bool state of the Button::CANCEL.
383 true == Cancel, false == Close
385 class NoRepeatedApplyPolicy : public ButtonPolicy {
388 NoRepeatedApplyPolicy();
390 //virtual ~NoRepeatedApplyPolicy() {}
392 /// Trigger a transition with this input.
393 virtual void input(SMInput);
394 /// Activation status of a button.
395 virtual bool buttonStatus(Button button) const {
396 return button & outputs_[state_];
398 /// Are we in a read-only state?
399 virtual bool isReadOnly() const {
405 /// Which buttons are active for a given state.
406 StateOutputs outputs_;
408 StateMachine state_machine_;
412 /** Defines the policy used by the Preferences dialog.
413 Four buttons: Ok (Save), Apply, Cancel/Close, Restore.
414 Note: This scheme supports the relabelling of Cancel to Close
416 This is based on the value of the bool state of the Button::CANCEL.
417 true == Cancel, false == Close
419 class PreferencesPolicy : public ButtonPolicy {
424 //virtual ~PreferencesPolicy() {}
426 /// Trigger a transition with this input.
427 virtual void input(SMInput);
428 /// Activation status of a button.
429 virtual bool buttonStatus(Button button) const {
430 return button & outputs_[state_];
432 /// Are we in a read-only state?
433 virtual bool isReadOnly() const {
439 /// Which buttons are active for a given state.
440 StateOutputs outputs_;
442 StateMachine state_machine_;
446 /** Defines the policy used by dialogs that are forced to support a button
447 controller when they either don't have a use for one or are not ready to
448 use one. This may be useful when testing a new button policy but wishing
449 to minimise problems to users by supplying an anything-goes policy via a
450 preprocessor directive.
452 class IgnorantPolicy : public ButtonPolicy {
454 //virtual ~IgnorantPolicy() {}
456 /// Trigger a transition with this input.
457 virtual void input(SMInput) {}
458 /// Activation status of a button.
459 virtual bool buttonStatus(Button) const {
462 /// Are we in a read-only state?
463 virtual bool isReadOnly() const {