class CGXCommand: public CObject
The CGXCommand class is an abstract base class for storing undo information for commands. If you want to support undo/redo for your commands, you should derive a class from CGXCommand and store all information necessary for undoing the operation into the command.
Objective Grid maintains CGXCommand objects in an undo and redo list. When the user gives the command to undo the last operation, it removes the first CGXCommand object in the list and calls the object’s Execute method. In your CGXCommand-derived class you should override the Execute method and embed the necessary steps to undo the command.
Basic steps for adding undo/redo support for a specific command are:
-
Derive a class from CGXCommand.
-
Insert the GRID_DECLARE_DYNAMIC / IMPLEMENT_DYNAMIC and the DECLARE_COMMAND / IMPLEMENT_COMMAND macros (see the example).
-
Provide a constructor.
-
Override the Execute method.
- In your command method you should create the CGXCommand-derived object and add it to the undo/redo list. This is done by calling AddCommand.
Now your command supports undo and redo.
#include <gxall.h>
Example
The following example explains some basic steps for implementing undo/redo in your code:
a) derive a class from CGXCommand, insert the GRID_DECLARE_DYNAMIC / IMPLEMENT_DYNAMIC and the DECLARE_COMMAND / IMPLEMENT_COMMAND macros, provide a constructor and override the Execute method:
// header file:
class CGXSetFrozenRowsCmd: public CGXCommand
{
GRID_DECLARE_DYNAMIC(CGXSetFrozenRowsCmd);
DECLARE_COMMAND(CGXSetFrozenRowsCmd);
public:
// Construction
CGXSetFrozenRowsCmd(ROWCOL nFrozenRows, ROWCOL nHeaderRows);
// Operation
virtual BOOL Execute(CGXGridCore* pGrid, GXCmdType ctType);
// Data
ROWCOL m_nFrozenRows;
ROWCOL m_nHeaderRows;
};
// implementation file (.cpp)
IMPLEMENT_DYNAMIC(CGXSetFrozenColsCmd, CGXCommand);
IMPLEMENT_COMMAND(CGXSetFrozenColsCmd, GX_IDM_SETFROZENCOLS);
// GX_IDM_SETFROZENCOLS is a string resource id with
// the description of the command.
CGXSetFrozenRowsCmd::CGXSetFrozenRowsCmd(ROWCOL nFrozenRows, ROWCOL nHeaderRows)
{
m_nFrozenRows = nFrozenRows;
m_nHeaderRows = nHeaderRows;
}
BOOL CGXSetFrozenRowsCmd::Execute(CGXGridCore* pGrid, GXCmdType ctCmd)
{
return pGrid->SetFrozenRows(m_nFrozenRows, m_nHeaderRows, GX_UPDATENOW, ctCmd);
}
b) In your command method you should create the CGXCommand-derived object and add it to the undo/redo list. This is done by calling AddCommand.
BOOL CGXGridCore::SetFrozenRows(ROWCOL nFrozenRows, ROWCOL nHeaderRows, UINT flags, GXCmdType ctCmd)
{
ASSERT(nHeaderRows <= nFrozenRows);
// ASSERTION-> Rows with headers must be frozen ->END
ROWCOL nOldFrozenRows = GetFrozenRows();
ROWCOL nOldHeaderRows = GetHeaderRows();
if (StoreFrozenRows(nFrozenRows, nHeaderRows))
{
UpdateFrozenRows(nOldFrozenRows, nOldHeaderRows, flags, TRUE);
if (ctCmd != gxRollback && m_pParam->m_bUndoEnabled)
AddCommand(new CGXSetFrozenRowsCmd(nOldFrozenRows, nOldHeaderRows), ctCmd);
return TRUE;
}
return FALSE;
}
See Also
CGXGridCore::AddCommand CGXGridCore::Undo CGXGridCore::BeginTrans