File: cmdproc.h

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (259 lines) | stat: -rw-r--r-- 7,961 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/////////////////////////////////////////////////////////////////////////////
// Name:        cmdproc.h
// Purpose:     interface of wxCommandProcessor and wxCommand
// Author:      wxWidgets team
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

/**
    @class wxCommand

    wxCommand is a base class for modelling an application command, which is an
    action usually performed by selecting a menu item, pressing a toolbar
    button or any other means provided by the application to change the data or
    view.

    @library{wxcore}
    @category{docview}

    @see @ref overview_docview_wxcommand
*/
class wxCommand : public wxObject
{
public:
    /**
        Constructor. wxCommand is an abstract class, so you will need to derive
        a new class and call this constructor from your own constructor.

        @param canUndo
            Tells the command processor whether this command is undo-able. You
            can achieve the same functionality by overriding the CanUndo()
            member function (if for example the criteria for undoability is
            context-dependent).
        @param name
            Must be supplied for the command processor to display the command
            name in the application's edit menu.
    */
    wxCommand(bool canUndo = false, const wxString& name = wxEmptyString);

    /**
        Destructor.
    */
    virtual ~wxCommand();

    /**
        Returns @true if the command can be undone, @false otherwise.
    */
    virtual bool CanUndo() const;

    /**
        Override this member function to execute the appropriate action when
        called.

        @return @true to indicate that the action has taken place, @false
                otherwise. Returning @false will indicate to the command
                processor that the action is not undoable and should not be
                added to the command history.
    */
    virtual bool Do() = 0;

    /**
        Returns the command name.
    */
    virtual wxString GetName() const;

    /**
        Override this member function to un-execute a previous Do.

        How you implement this command is totally application dependent, but
        typical strategies include:

        - Perform an inverse operation on the last modified piece of data in
          the document. When redone, a copy of data stored in command is pasted
          back or some operation reapplied. This relies on the fact that you
          know the ordering of Undos; the user can never Undo at an arbitrary
          position in the command history.
        - Restore the entire document state (perhaps using document
          transacting). Potentially very inefficient, but possibly easier to
          code if the user interface and data are complex, and an "inverse
          execute" operation is hard to write. The docview sample uses the
          first method, to remove or restore segments in the drawing.

        @return @true to indicate that the action has taken place, @false
                otherwise. Returning @false will indicate to the command
                processor that the action is not redoable and no change should
                be made to the command history.
    */
    virtual bool Undo() = 0;
};



/**
    @class wxCommandProcessor

    wxCommandProcessor is a class that maintains a history of wxCommands, with
    undo/redo functionality built-in. Derive a new class from this if you want
    different behaviour.

    @library{wxcore}
    @category{docview}

    @see @ref overview_docview_wxcommandproc, wxCommand
*/
class wxCommandProcessor : public wxObject
{
public:
    /**
        Constructor.

        @param maxCommands
            May be set to a positive integer to limit the number of commands
            stored to it, otherwise (and by default) the list of commands can
            grow arbitrarily.
    */
    wxCommandProcessor(int maxCommands = -1);

    /**
        Destructor.
    */
    virtual ~wxCommandProcessor();

    /**
        Returns @true if the currently-active command can be undone, @false
        otherwise.
    */
    virtual bool CanUndo() const;

    /**
        Returns @true if the currently-active command can be redone, @false
        otherwise.
    */
    virtual bool CanRedo() const;

    /**
        Deletes all commands in the list and sets the current command pointer
        to @NULL.
    */
    virtual void ClearCommands();

    /**
        Returns the list of commands.
    */
    wxList& GetCommands();

    /**
        Returns the current command.
    */
    wxCommand *GetCurrentCommand() const;

    /**
        Returns the edit menu associated with the command processor.
    */
    wxMenu* GetEditMenu() const;

    /**
        Returns the maximum number of commands that the command processor
        stores.
    */
    int GetMaxCommands() const;

    /**
        Returns the string that will be appended to the Redo menu item.
    */
    const wxString& GetRedoAccelerator() const;

    /**
        Returns the string that will be shown for the redo menu item.
    */
    wxString GetRedoMenuLabel() const;

    /**
        Returns the string that will be appended to the Undo menu item.
    */
    const wxString& GetUndoAccelerator() const;

    /**
        Returns the string that will be shown for the undo menu item.
    */
    wxString GetUndoMenuLabel() const;

    /**
        Initializes the command processor, setting the current command to the
        last in the list (if any), and updating the edit menu (if one has been
        specified).
    */
    virtual void Initialize();

    /**
        Returns a boolean value that indicates if changes have been made since
        the last save operation. This only works if MarkAsSaved() is called
        whenever the project is saved.
    */
    virtual bool IsDirty() const;

    /**
        You must call this method whenever the project is saved if you plan to
        use IsDirty().
    */
    void MarkAsSaved();

    /**
        Executes (redoes) the current command (the command that has just been
        undone if any).
    */
    virtual bool Redo();

    /**
        Tells the command processor to update the Undo and Redo items on this
        menu as appropriate. Set this to @NULL if the menu is about to be
        destroyed and command operations may still be performed, or the command
        processor may try to access an invalid pointer.
    */
    void SetEditMenu(wxMenu* menu);

    /**
        Sets the menu labels according to the currently set menu and the
        current command state.
    */
    virtual void SetMenuStrings();

    /**
        Sets the string that will be appended to the Redo menu item.
    */
    void SetRedoAccelerator(const wxString& accel);

    /**
        Sets the string that will be appended to the Undo menu item.
    */
    void SetUndoAccelerator(const wxString& accel);

    /**
        Submits a new command to the command processor.

        The command processor calls wxCommand::Do() to execute the command;
        if it succeeds, the command is stored in the history list, and the
        associated edit menu (if any) updated appropriately.
        If it fails, the command is deleted immediately. Once Submit() has been
        called, the passed command should not be deleted directly by the application.

        @param command
            The command to submit
        @param storeIt
            Indicates whether the successful command should be stored in the
            history list.
    */
    virtual bool Submit(wxCommand* command, bool storeIt = true);

    /**
        Just store the command without executing it. The command is stored in the
        history list, and the associated edit menu (if any) updated appropriately.
    */
    virtual void Store(wxCommand *command);

    /**
        Undoes the last command executed.
    */
    virtual bool Undo();
};