2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: Cmd.C /main/4 1998/07/24 16:04:37 mgreess $ */
27 * RESTRICTED CONFIDENTIAL INFORMATION:
29 * The information in this document is subject to special
30 * restrictions in a confidential disclosure agreement bertween
31 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
32 * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
33 * Sun's specific written approval. This documment and all copies
34 * and derivative works thereof must be returned or destroyed at
37 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
41 ///////////////////////////////////////////////////////////////////////////////
42 //////////////////////////////////////////////////////////////////////////////
43 // This example code is from the book:
45 // Object-Oriented Programming with C++ and OSF/Motif
48 // Prentice Hall, 1992
51 // Copyright 1991 by Prentice Hall
52 // All Rights Reserved
54 // Permission to use, copy, modify, and distribute this software for
55 // any purpose except publication and without fee is hereby granted, provided
56 // that the above copyright notice appear in all copies of the software.
57 ///////////////////////////////////////////////////////////////////////////////
58 //////////////////////////////////////////////////////////////////////////////
61 ///////////////////////////////////////////////////////
63 ///////////////////////////////////////////////////////
67 #include "CmdInterface.h"
69 extern Cmd *theUndoCmd; // External object that reverses the
70 // most recent Cmd when executed
72 Cmd *Cmd::_lastCmd = NULL; // Pointer to most recent Cmd
74 Cmd::Cmd ( char *name, char *label, int active )
76 // Initialize all data members
82 _activationList = NULL;
83 _deactivationList = NULL;
87 _label = strdup(label);
89 _label = strdup(name);
95 delete _activationList;
96 delete _deactivationList;
102 void Cmd::registerInterface ( CmdInterface *ci )
104 // Make a new list, large enough for the new object
106 CmdInterface **newList = new CmdInterface*[_numInterfaces + 1];
108 // Copy the contents of the previous list to
111 for( int i = 0; i < _numInterfaces; i++)
119 // Install the new list
123 // Add the object to the list and update the list size.
125 _ci[_numInterfaces] = ci;
138 // Activate the associated interfaces
140 for ( int i = 0; i < _numInterfaces; i++ )
143 // Save the current value of active before setting the new state
145 _previouslyActive = _active;
149 void Cmd::deactivate()
151 // Deactivate the associated interfaces
153 for ( int i = 0; i < _numInterfaces; i++ )
154 _ci[i]->deactivate ();
156 // Save the current value of active before setting the new state
158 _previouslyActive = _active;
164 // Activate or deactivate, as necessary,
165 // to return to the previous state
167 if ( _previouslyActive )
174 void Cmd::addToActivationList ( Cmd *cmd )
176 if ( !_activationList )
177 _activationList = new CmdList();
179 _activationList->add ( cmd );
182 void Cmd::addToDeactivationList ( Cmd *cmd )
184 if ( !_deactivationList )
185 _deactivationList = new CmdList();
187 _deactivationList->add ( cmd );
189 #endif /* DEAD_WOOD */
195 // If a command is inactive, it cannot be executed
201 // Activate or deactivate the global theUndoCmd,
202 // and remember the last command, as needed
206 Cmd::_lastCmd = this;
207 theUndoCmd->activate();
211 Cmd::_lastCmd = NULL;
212 theUndoCmd->deactivate();
215 // Process the commands that depend on this one
217 if ( _activationList )
218 for ( i = 0; i < _activationList->size(); i++ )
219 (*_activationList)[i]->activate();
221 if ( _deactivationList )
222 for ( i = 0; i < _deactivationList->size(); i++ )
223 (*_deactivationList)[i]->deactivate();
226 // Call the derived class's doit member function to
227 // perform the action represented by this object
237 // Call the derived class's undoit() member function.
241 // The system only supports one level of undo, and this is it,
242 // so deactivate the undo facility.
244 theUndoCmd->deactivate();
246 // Reverse the effects of the execute() member function by
247 // reverting all dependent objects to their previous state
249 if ( _activationList )
250 for ( i = 0; i < _activationList->size(); i++ )
251 (*_activationList)[i]->revert();
253 if ( _deactivationList )
254 for ( i = 0; i < _deactivationList->size(); i++ )
255 (*_deactivationList)[i]->revert();
258 #ifndef CAN_INLINE_VIRTUALS
264 #endif /* ! CAN_INLINE_VIRTUALS */