1 /* $TOG: Cmd.C /main/4 1998/07/24 16:04:37 mgreess $ */
5 * RESTRICTED CONFIDENTIAL INFORMATION:
7 * The information in this document is subject to special
8 * restrictions in a confidential disclosure agreement bertween
9 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
10 * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
11 * Sun's specific written approval. This documment and all copies
12 * and derivative works thereof must be returned or destroyed at
15 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
19 ///////////////////////////////////////////////////////////////////////////////
20 //////////////////////////////////////////////////////////////////////////////
21 // This example code is from the book:
23 // Object-Oriented Programming with C++ and OSF/Motif
26 // Prentice Hall, 1992
29 // Copyright 1991 by Prentice Hall
30 // All Rights Reserved
32 // Permission to use, copy, modify, and distribute this software for
33 // any purpose except publication and without fee is hereby granted, provided
34 // that the above copyright notice appear in all copies of the software.
35 ///////////////////////////////////////////////////////////////////////////////
36 //////////////////////////////////////////////////////////////////////////////
39 ///////////////////////////////////////////////////////
41 ///////////////////////////////////////////////////////
45 #include "CmdInterface.h"
47 extern Cmd *theUndoCmd; // External object that reverses the
48 // most recent Cmd when executed
50 Cmd *Cmd::_lastCmd = NULL; // Pointer to most recent Cmd
52 Cmd::Cmd ( char *name, char *label, int active )
54 // Initialize all data members
60 _activationList = NULL;
61 _deactivationList = NULL;
65 _label = strdup(label);
67 _label = strdup(name);
73 delete _activationList;
74 delete _deactivationList;
80 void Cmd::registerInterface ( CmdInterface *ci )
82 // Make a new list, large enough for the new object
84 CmdInterface **newList = new CmdInterface*[_numInterfaces + 1];
86 // Copy the contents of the previous list to
89 for( int i = 0; i < _numInterfaces; i++)
97 // Install the new list
101 // Add the object to the list and update the list size.
103 _ci[_numInterfaces] = ci;
116 // Activate the associated interfaces
118 for ( int i = 0; i < _numInterfaces; i++ )
121 // Save the current value of active before setting the new state
123 _previouslyActive = _active;
127 void Cmd::deactivate()
129 // Deactivate the associated interfaces
131 for ( int i = 0; i < _numInterfaces; i++ )
132 _ci[i]->deactivate ();
134 // Save the current value of active before setting the new state
136 _previouslyActive = _active;
142 // Activate or deactivate, as necessary,
143 // to return to the previous state
145 if ( _previouslyActive )
152 void Cmd::addToActivationList ( Cmd *cmd )
154 if ( !_activationList )
155 _activationList = new CmdList();
157 _activationList->add ( cmd );
160 void Cmd::addToDeactivationList ( Cmd *cmd )
162 if ( !_deactivationList )
163 _deactivationList = new CmdList();
165 _deactivationList->add ( cmd );
167 #endif /* DEAD_WOOD */
173 // If a command is inactive, it cannot be executed
179 // Activate or deactivate the global theUndoCmd,
180 // and remember the last command, as needed
184 Cmd::_lastCmd = this;
185 theUndoCmd->activate();
189 Cmd::_lastCmd = NULL;
190 theUndoCmd->deactivate();
193 // Process the commands that depend on this one
195 if ( _activationList )
196 for ( i = 0; i < _activationList->size(); i++ )
197 (*_activationList)[i]->activate();
199 if ( _deactivationList )
200 for ( i = 0; i < _deactivationList->size(); i++ )
201 (*_deactivationList)[i]->deactivate();
204 // Call the derived class's doit member function to
205 // perform the action represented by this object
215 // Call the derived class's undoit() member function.
219 // The system only supports one level of undo, and this is it,
220 // so deactivate the undo facility.
222 theUndoCmd->deactivate();
224 // Reverse the effects of the execute() member function by
225 // reverting all dependent objects to their previous state
227 if ( _activationList )
228 for ( i = 0; i < _activationList->size(); i++ )
229 (*_activationList)[i]->revert();
231 if ( _deactivationList )
232 for ( i = 0; i < _deactivationList->size(); i++ )
233 (*_deactivationList)[i]->revert();
236 #ifndef CAN_INLINE_VIRTUALS
242 #endif /* ! CAN_INLINE_VIRTUALS */