Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXUndoList.h
1 /********************************************************************************
2 * *
3 * U n d o / R e d o - a b l e C o m m a n d *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2000,2024 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXUNDOLIST_H
22 #define FXUNDOLIST_H
23 
24 #ifndef FXOBJECT_H
25 #include "FXObject.h"
26 #endif
27 
28 namespace FX {
29 
30 
31 // Declarations
32 class FXUndoList;
33 
34 
55 class FXAPI FXCommand : public FXObject {
56  FXDECLARE_ABSTRACT(FXCommand)
57 private:
58  FXival refs;
59 private:
60  FXCommand(const FXCommand& org);
61  FXCommand &operator=(const FXCommand&);
62 protected:
63  FXCommand():refs(0){}
64 public:
65 
66  // Return reference count
67  FXival nrefs() const { return refs; }
68 
69  // Increment reference count
70  void ref(){ ++refs; }
71 
72  // Decrement reference count
73  void unref(){ if(--refs<=0) delete this; }
74 
78  virtual void undo() = 0;
79 
83  virtual void redo() = 0;
84 
93  virtual FXuval size() const;
94 
99  virtual FXString undoName() const;
100 
105  virtual FXString redoName() const;
106 
113  virtual FXbool canMerge() const;
114 
125  virtual FXuint mergeWith(FXCommand* command);
126  };
127 
128 
129 // A marked pointer to a command
131 
132 // An array of marked command pointers
134 
135 
154 class FXAPI FXCommandGroup : public FXCommand {
155  FXDECLARE(FXCommandGroup)
156  friend class FXUndoList;
157 private:
158  FXCommandArray command;
159  FXCommandGroup *group;
160 private:
162  FXCommandGroup &operator=(const FXCommandGroup&);
163 public:
164 
166  FXCommandGroup():group(nullptr){ }
167 
169  FXbool empty(){ return command.no()==0; }
170 
172  virtual void undo();
173 
175  virtual void redo();
176 
178  virtual FXuval size() const;
179 
181  virtual void clear();
182 
184  virtual ~FXCommandGroup();
185  };
186 
187 
210 class FXAPI FXUndoList : public FXCommandGroup {
211  FXDECLARE(FXUndoList)
212 private:
213  FXuval space; // Total memory in the undo commands
214  FXint undocount; // Number of undo records
215  FXint redocount; // Number of redo records
216  FXint marker; // Marker value
217  FXbool markset; // Mark is set
218  FXbool alternate; // Keep alternate history
219  FXbool working; // Currently busy with undo or redo
220 private:
221  FXUndoList(const FXUndoList&);
222  FXUndoList &operator=(const FXUndoList&);
223 public:
224  long onCmdUndo(FXObject*,FXSelector,void*);
225  long onUpdUndo(FXObject*,FXSelector,void*);
226  long onCmdRedo(FXObject*,FXSelector,void*);
227  long onUpdRedo(FXObject*,FXSelector,void*);
228  long onCmdClear(FXObject*,FXSelector,void*);
229  long onUpdClear(FXObject*,FXSelector,void*);
230  long onCmdRevert(FXObject*,FXSelector,void*);
231  long onUpdRevert(FXObject*,FXSelector,void*);
232  long onCmdUndoAll(FXObject*,FXSelector,void*);
233  long onCmdRedoAll(FXObject*,FXSelector,void*);
234  long onUpdUndoCount(FXObject*,FXSelector,void*);
235  long onUpdRedoCount(FXObject*,FXSelector,void*);
236  long onCmdAltHistory(FXObject*,FXSelector,void*);
237  long onUpdAltHistory(FXObject*,FXSelector,void*);
238  long onCmdDumpStats(FXObject*,FXSelector,void*);
239 public:
240  enum{
241  ID_CLEAR=FXWindow::ID_LAST,
242  ID_REVERT,
243  ID_UNDO,
244  ID_REDO,
245  ID_UNDO_ALL,
246  ID_REDO_ALL,
247  ID_UNDO_COUNT,
248  ID_REDO_COUNT,
249  ID_ALT_HISTORY,
250  ID_DUMP_STATS,
251  ID_LAST
252  };
253 public:
254 
258  FXUndoList();
259 
265  FXbool busy() const { return working; }
266 
278  FXbool cut();
279 
287  FXbool add(FXCommand* cmd,FXbool doit=false,FXbool merge=true);
288 
295  FXbool begin(FXCommandGroup *command);
296 
303  FXbool end();
304 
310  FXbool abort();
311 
315  virtual void undo();
316 
320  virtual void redo();
321 
325  void undoAll();
326 
330  void redoAll();
331 
335  void revert();
336 
340  FXbool canUndo() const;
341 
345  FXbool canRedo() const;
346 
350  FXbool canRevert() const;
351 
355  FXCommand* current() const;
356 
361  virtual FXString undoName() const;
362 
367  virtual FXString redoName() const;
368 
370  virtual FXuval size() const;
371 
373  virtual void clear();
374 
376  FXint undoCount() const { return undocount; }
377 
379  FXint redoCount() const { return redocount; }
380 
387  void trimCount(FXint nc);
388 
395  void trimSize(FXuval sz);
396 
402  void trimMark();
403 
412  void mark();
413 
417  void unmark();
418 
423  FXbool marked() const;
424 
432  void setAlternateHistory(FXbool flag){ alternate=flag; }
433 
437  FXbool getAlternateHistory() const { return alternate; }
438 
440  void dumpStats();
441 
443  virtual ~FXUndoList();
444  };
445 
446 }
447 
448 #endif
FXint redoCount() const
Number of redo records.
Definition: FXUndoList.h:379
FXint undoCount() const
Number of undo records.
Definition: FXUndoList.h:376
FXbool busy() const
Return true if currently inside undo or redo operation; this is useful to avoid generating another un...
Definition: FXUndoList.h:265
void setAlternateHistory(FXbool flag)
Enable or disable alternate history mode.
Definition: FXUndoList.h:432
Base class for undoable commands records.
Definition: FXUndoList.h:55
Marked pointer keeps a flag bit inside of a dynamically allocated pointer; this is possible because m...
Definition: FXMarkedPtr.h:34
Definition: FX4Splitter.h:28
FXbool getAlternateHistory() const
Returns true if alternate history mode is in effect.
Definition: FXUndoList.h:437
The UndoList class manages a list of undoable commands.
Definition: FXUndoList.h:210
Object is the base class for all objects in FOX; in order to receive messages from the user interface...
Definition: FXObject.h:138
Group of undoable commands.
Definition: FXUndoList.h:154
FXCommandGroup()
Construct initially empty undo command group.
Definition: FXUndoList.h:166
FXbool empty()
Return true if empty.
Definition: FXUndoList.h:169
FXival no() const
Return number of items.
Definition: FXArray.h:69
FXString provides essential string manipulation capabilities in FOX.
Definition: FXString.h:42

Copyright © 1997-2022 Jeroen van der Zijp