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

FXUndoList.h

00001 /********************************************************************************
00002 *                                                                               *
00003 *                  U n d o / R e d o - a b l e   C o m m a n d                  *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2000,2002 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (at your option) any later version.            *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXUndoList.h,v 1.16 2002/01/18 22:42:55 jeroen Exp $                     *
00023 ********************************************************************************/
00024 #ifndef FXUNDOLIST_H
00025 #define FXUNDOLIST_H
00026 
00027 #ifndef FXOBJECT_H
00028 #include "FXObject.h"
00029 #endif
00030 
00031 
00032 
00033 class FXUndoList;
00034 
00035 
00036 /// Base class for undoable commands
00037 class FXAPI FXCommand {
00038   friend class FXUndoList;
00039 private:
00040   FXCommand *next;
00041 private:
00042   FXCommand(const FXCommand&);
00043   FXCommand &operator=(const FXCommand&);
00044 protected:
00045   FXCommand():next(NULL){}
00046 public:
00047 
00048   /**
00049   * Undo this command; this should save the
00050   * information for a subsequent redo.
00051   */
00052   virtual void undo() = 0;
00053 
00054   /**
00055   * Redo this command; this should save the
00056   * information for a subsequent undo.
00057   */
00058   virtual void redo() = 0;
00059 
00060   /**
00061   * Return the size of the information in the undo record.
00062   * The undo list may be trimmed to limit memory usage to
00063   * a certain limit.
00064   */
00065   virtual FXuint size() const = 0;
00066 
00067   /**
00068   * Name of the undo command to be shown on a button;
00069   * for example, "Undo Delete".
00070   */
00071   virtual FXString undoName() const = 0;
00072 
00073   /**
00074   * Name of the redo command to be shown on a button;
00075   * for example, "Redo Delete".
00076   */
00077   virtual FXString redoName() const = 0;
00078 
00079   /// Delete undo command
00080   virtual ~FXCommand(){}
00081   };
00082 
00083 
00084 /**
00085 * The Undo List class manages a list of undoable commands.
00086 */
00087 class FXAPI FXUndoList : public FXObject {
00088   FXDECLARE(FXUndoList)
00089 private:
00090   FXCommand *redolist;      // List yet to be done from this point
00091   FXCommand *undolist;      // List which has been done from this point
00092   FXint      marker;        // Marker value
00093   FXint      count;         // Number of undo records
00094   FXuint     size;          // Size of undo records
00095 public:
00096   long onCmdUndo(FXObject*,FXSelector,void*);
00097   long onUpdUndo(FXObject*,FXSelector,void*);
00098   long onCmdRedo(FXObject*,FXSelector,void*);
00099   long onUpdRedo(FXObject*,FXSelector,void*);
00100   long onCmdClear(FXObject*,FXSelector,void*);
00101   long onUpdClear(FXObject*,FXSelector,void*);
00102   long onCmdRevert(FXObject*,FXSelector,void*);
00103   long onUpdRevert(FXObject*,FXSelector,void*);
00104   long onCmdUndoAll(FXObject*,FXSelector,void*);
00105   long onCmdRedoAll(FXObject*,FXSelector,void*);
00106 public:
00107   enum{
00108     ID_CLEAR=FXWindow::ID_LAST,
00109     ID_REVERT,
00110     ID_UNDO,
00111     ID_REDO,
00112     ID_UNDO_ALL,
00113     ID_REDO_ALL,
00114     ID_LAST
00115     };
00116 public:
00117 
00118   /// Make new empty undo list, initially unmarked.
00119   FXUndoList();
00120 
00121   /// Cut the redo list
00122   void cut();
00123 
00124   /// Add new command, executing if desired
00125   FXbool add(FXCommand* command,FXbool doit=FALSE);
00126 
00127   /// Undo last command
00128   FXbool undo();
00129 
00130   /// Redo next command
00131   FXbool redo();
00132 
00133   /// Undo all commands
00134   FXbool undoAll();
00135 
00136   /// Redo all commands
00137   FXbool redoAll();
00138 
00139   /// Revert to marked
00140   FXbool revert();
00141 
00142   /// Can we undo more commands
00143   FXbool canUndo() const;
00144 
00145   /// Can we redo more commands
00146   FXbool canRedo() const;
00147 
00148   /// Can revert to marked
00149   FXbool canRevert() const;
00150 
00151   /// Current undo command
00152   FXCommand* current() const { return undolist; }
00153 
00154   /// Number of undo records
00155   FXint undoCount() const { return count; }
00156 
00157   /// Size of undo information
00158   FXuint undoSize() const { return size; }
00159 
00160   /// Clear list, and unmark all states.
00161   void clear();
00162 
00163   /// Trim undo list down to at most nc commands
00164   void trimCount(FXint nc);
00165 
00166   /// Trim undo list down to at most size sz
00167   void trimSize(FXuint sz);
00168 
00169   /**
00170   * Mark the current state of the undo list, which is initially unmarked.
00171   * There can be only one active mark at any time.
00172   */
00173   void mark();
00174 
00175   /**
00176   * Unmark all states in the undo list.
00177   */
00178   void unmark();
00179 
00180   /**
00181   * Check if the current state was marked, if the application has returned
00182   * to the previously marked state.
00183   */
00184   FXbool marked() const;
00185 
00186   /// Clean up
00187   ~FXUndoList();
00188   };
00189 
00190 
00191 #endif