3 * Provides a state machine implementation of the various button policies
5 * Author: Allan Rae <rae@lyx.org>
7 * ======================================================
9 * LyX, The Document Processor
11 * Copyright 1995 Matthias Ettrich
12 * Copyright 1995-2000 The LyX Team.
14 * This file Copyright 2000
16 * ======================================================
19 #ifndef BUTTONPOLICIES_H
20 #define BUTTONPOLICIES_H
24 #include <boost/utility.hpp>
26 #include "support/LOstream.h"
28 /** An abstract base class for button policies.
29 A state machine implementation of the various button policies used by the
30 dialogs. Only the policy is implemented here. Separate ButtonController
31 classes are needed for each GUI implementation.
33 Policy | ReadOnly | Apply Button | Repeated Apply
34 ========================================================================
36 OkCancelReadOnly | Y | N | -
37 OkApplyCancel | N | Y | Y
38 OkApplyCancelReadOnly | Y | Y | Y
39 NoRepeatedApply | N | Y | N
40 NoRepeatedApplyReadOnly | Y | Y | N
41 Preferences | N | Y | No (Ok-Close)
42 Ignorant | N/A | N/A | N/A
43 ========================================================================
46 The name of the policy
48 Does the policy treat read-only docs differently to read-write docs?
49 This usually means that when an SMI_READ_ONLY input arrives then
50 all the buttons are disabled except Cancel/Close. The state
51 machine tracks the inputs (valid/invalid) and has states for all
52 combinations. When an SMI_READ_WRITE input arrives the appropriate
53 machine state is entered (just as if the document had always been
55 NOTE: If a dialog doesn't care about the read-only status of a document
56 (and uses an appropriate policy) it can never get into a read-only state
57 so isReadOnly() can only ever return false even though the document may
60 Simply means that it is alright to use the Apply button multiple times
61 without requiring a change of the dialog contents. If no repeating is
62 allowed the Ok+Apply buttons are deactivated. The Preferences dialog
63 has its own special version of repeated apply handling because its Ok
64 button is actually a Save button -- its always reasonable to Save the
65 preferences if the dialog has changed since the last save.
67 The IgnorantPolicy is a special case that allows anything.
69 class ButtonPolicy : public noncopyable {
72 virtual ~ButtonPolicy() {}
74 /** The various possible state names.
75 Not all state-machines have this many states. However, we need
76 to define them all here so we can share the code.
99 /// The various button types.
102 CLOSE = 0, // Not a real button, but effectively !CANCEL
113 static const Button ALL_BUTTONS =
114 Button(OKAY | APPLY | CANCEL | UNDO_ALL);
116 /** State machine inputs.
117 All the policies so far have both CANCEL and HIDE always going to
118 INITIAL. This won't necessarily be true for all [future] policies
119 though so I'll leave those two as distinct inputs rather than merge
120 them. For example, a dialog that doesn't update it's input fields
121 when reshown after being hidden needs a policy where CANCEL and
122 HIDE are treated differently.
144 SMI_TOTAL // not a real input
147 /// Trigger a transition with this input.
148 virtual void input(SMInput) = 0;
149 /// Activation status of a button
150 virtual bool buttonStatus(Button) const = 0;
151 /// Are we in a read-only state?
152 virtual bool isReadOnly() const = 0;
154 /// Transition map of the state machine.
155 typedef std::vector<State> StateArray;
157 typedef std::vector<StateArray> StateMachine;
158 /// The state outputs are the status of the buttons.
159 typedef std::vector<int> StateOutputs;
164 std::ostream & operator<<(std::ostream & os, ButtonPolicy::State st)
172 std::ostream & operator<<(std::ostream & os, ButtonPolicy::SMInput smi)
179 //--------------------- Actual Policy Classes -----------------------------
181 /** Ok and Cancel buttons for dialogs with read-only operation.
182 Note: This scheme supports the relabelling of Cancel to Close and
184 This is based on the value of the bool state of the Button::CANCEL.
185 true == Cancel, false == Close
187 class OkCancelPolicy : public ButtonPolicy {
192 //virtual ~OkCancelPolicy() {}
194 /// Trigger a transition with this input.
195 virtual void input(SMInput);
196 /** Activation status of a button.
197 We assume that we haven't gotten into an undefined state.
198 This is reasonable since we can only reach states defined
199 in the state machine and they should all have been defined in
200 the outputs_ variable. Perhaps we can do something at compile
201 time to check that all the states have corresponding outputs.
203 virtual bool buttonStatus(Button button) const {
204 return button & outputs_[state_];
206 /// Are we in a read-only state?
207 virtual bool isReadOnly() const {
213 /// Which buttons are active for a given state.
214 StateOutputs outputs_;
216 StateMachine state_machine_;
220 /** Ok and Cancel buttons for dialogs where read-only operation is blocked.
221 The state machine design for this policy allows changes to occur within
222 the dialog while a file is read-only -- the okay button is disabled until
223 a read-write input is given. When the file is made read-write the dialog
224 will then be in the correct state (as if the file had always been
226 Note: This scheme supports the relabelling of Cancel to Close
228 This is based on the value of the bool state of the Button::CANCEL.
229 true == Cancel, false == Close
231 class OkCancelReadOnlyPolicy : public ButtonPolicy {
234 OkCancelReadOnlyPolicy();
236 //virtual ~OkCancelReadOnlyPolicy() {}
238 /// Trigger a transition with this input.
239 virtual void input(SMInput);
240 /// Activation status of a button.
241 virtual bool buttonStatus(Button button) const {
242 return button & outputs_[state_];
244 /// Are we in a read-only state?
245 virtual bool isReadOnly() const {
246 return RO_INITIAL == state_
247 || RO_VALID == state_
248 || RO_INVALID == state_
249 || RO_APPLIED == state_;
254 /// Which buttons are active for a given state.
255 StateOutputs outputs_;
257 StateMachine state_machine_;
261 /** Ok, Apply and Cancel buttons for dialogs where read-only operation
263 Repeated Apply are not allowed. Likewise, Ok cannot follow Apply without
264 some valid input. That is, the dialog contents must change between
265 each Apply or Apply and Ok.
266 The state machine design for this policy allows changes to occur within
267 the dialog while a file is read-only -- the Ok+Apply buttons are disabled
268 until a read-write input is given. When the file is made read-write the
269 dialog will then be in the correct state (as if the file had always been
271 Note: This scheme supports the relabelling of Cancel to Close
273 This is based on the value of the bool state of the Button::CANCEL.
274 true == Cancel, false == Close
276 class NoRepeatedApplyReadOnlyPolicy : public ButtonPolicy
280 NoRepeatedApplyReadOnlyPolicy();
282 //virtual ~NoRepeatedApplyReadOnlyPolicy() {}
284 /// Trigger a transition with this input.
285 virtual void input(SMInput);
286 /// Activation status of a button.
287 virtual bool buttonStatus(Button button) const {
288 return button & outputs_[state_];
290 /// Are we in a read-only state?
291 virtual bool isReadOnly() const {
292 return RO_INITIAL == state_
293 || RO_VALID == state_
294 || RO_INVALID == state_
295 || RO_APPLIED == state_;
300 /// Which buttons are active for a given state.
301 StateOutputs outputs_;
303 StateMachine state_machine_;
307 /** Ok, Apply and Cancel buttons for dialogs where read-only
308 operation is blocked.
309 Repeated Apply is allowed. Likewise, Ok can follow Apply.
310 The state machine design for this policy allows changes to occur within
311 the dialog while a file is read-only -- the Ok+Apply buttons are disabled
312 until a read-write input is given. When the file is made read-write the
313 dialog will then be in the correct state (as if the file had always been
315 Note: This scheme supports the relabelling of Cancel to Close
317 This is based on the value of the bool state of the Button::CANCEL.
318 true == Cancel, false == Close
320 class OkApplyCancelReadOnlyPolicy : public ButtonPolicy {
323 OkApplyCancelReadOnlyPolicy();
325 //virtual ~OkApplyCancelReadOnlyPolicy() {}
327 /// Trigger a transition with this input.
328 virtual void input(SMInput);
329 /// Activation status of a button.
330 virtual bool buttonStatus(Button button) const {
331 return button & outputs_[state_];
333 /// Are we in a read-only state?
334 virtual bool isReadOnly() const {
335 return RO_INITIAL == state_
336 || RO_VALID == state_
337 || RO_INVALID == state_
338 || RO_APPLIED == state_;
343 /// Which buttons are active for a given state.
344 StateOutputs outputs_;
346 StateMachine state_machine_;
350 /** Ok, Apply and Cancel buttons for dialogs where repeated Apply is allowed.
351 Note: This scheme supports the relabelling of Cancel to Close
353 This is based on the value of the bool state of the Button::CANCEL.
354 true == Cancel, false == Close
356 class OkApplyCancelPolicy : public ButtonPolicy {
359 OkApplyCancelPolicy();
361 //virtual ~OkApplyCancelPolicy() {}
363 /// Trigger a transition with this input.
364 virtual void input(SMInput);
365 /// Activation status of a button.
366 virtual bool buttonStatus(Button button) const {
367 return button & outputs_[state_];
369 /// Are we in a read-only state?
370 virtual bool isReadOnly() const {
376 /// Which buttons are active for a given state.
377 StateOutputs outputs_;
379 StateMachine state_machine_;
383 /** Ok, Apply and Cancel buttons for dialogs with no repeated Apply.
384 Note: This scheme supports the relabelling of Cancel to Close
386 This is based on the value of the bool state of the Button::CANCEL.
387 true == Cancel, false == Close
389 class NoRepeatedApplyPolicy : public ButtonPolicy {
392 NoRepeatedApplyPolicy();
394 //virtual ~NoRepeatedApplyPolicy() {}
396 /// Trigger a transition with this input.
397 virtual void input(SMInput);
398 /// Activation status of a button.
399 virtual bool buttonStatus(Button button) const {
400 return button & outputs_[state_];
402 /// Are we in a read-only state?
403 virtual bool isReadOnly() const {
409 /// Which buttons are active for a given state.
410 StateOutputs outputs_;
412 StateMachine state_machine_;
416 /** Defines the policy used by the Preferences dialog.
417 Four buttons: Ok (Save), Apply, Cancel/Close, Restore.
418 Note: This scheme supports the relabelling of Cancel to Close
420 This is based on the value of the bool state of the Button::CANCEL.
421 true == Cancel, false == Close
423 class PreferencesPolicy : public ButtonPolicy {
428 //virtual ~PreferencesPolicy() {}
430 /// Trigger a transition with this input.
431 virtual void input(SMInput);
432 /// Activation status of a button.
433 virtual bool buttonStatus(Button button) const {
434 return button & outputs_[state_];
436 /// Are we in a read-only state?
437 virtual bool isReadOnly() const {
443 /// Which buttons are active for a given state.
444 StateOutputs outputs_;
446 StateMachine state_machine_;
450 /** Defines the policy used by dialogs that are forced to support a button
451 controller when they either don't have a use for one or are not ready to
452 use one. This may be useful when testing a new button policy but wishing
453 to minimise problems to users by supplying an anything-goes policy via a
454 preprocessor directive.
456 class IgnorantPolicy : public ButtonPolicy {
458 //virtual ~IgnorantPolicy() {}
460 /// Trigger a transition with this input.
461 virtual void input(SMInput) {}
462 /// Activation status of a button.
463 virtual bool buttonStatus(Button) const {
466 /// Are we in a read-only state?
467 virtual bool isReadOnly() const {