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 libraries 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;
85 _previouslyActive = 0;
88 _label = strdup(label);
90 _label = strdup(name);
96 delete _activationList;
97 delete _deactivationList;
103 void Cmd::registerInterface ( CmdInterface *ci )
105 // Make a new list, large enough for the new object
107 CmdInterface **newList = new CmdInterface*[_numInterfaces + 1];
109 // Copy the contents of the previous list to
112 for( int i = 0; i < _numInterfaces; i++)
120 // Install the new list
124 // Add the object to the list and update the list size.
126 _ci[_numInterfaces] = ci;
139 // Activate the associated interfaces
141 for ( int i = 0; i < _numInterfaces; i++ )
144 // Save the current value of active before setting the new state
146 _previouslyActive = _active;
150 void Cmd::deactivate()
152 // Deactivate the associated interfaces
154 for ( int i = 0; i < _numInterfaces; i++ )
155 _ci[i]->deactivate ();
157 // Save the current value of active before setting the new state
159 _previouslyActive = _active;
165 // Activate or deactivate, as necessary,
166 // to return to the previous state
168 if ( _previouslyActive )
175 void Cmd::addToActivationList ( Cmd *cmd )
177 if ( !_activationList )
178 _activationList = new CmdList();
180 _activationList->add ( cmd );
183 void Cmd::addToDeactivationList ( Cmd *cmd )
185 if ( !_deactivationList )
186 _deactivationList = new CmdList();
188 _deactivationList->add ( cmd );
190 #endif /* DEAD_WOOD */
196 // If a command is inactive, it cannot be executed
202 // Activate or deactivate the global theUndoCmd,
203 // and remember the last command, as needed
207 Cmd::_lastCmd = this;
208 theUndoCmd->activate();
212 Cmd::_lastCmd = NULL;
213 theUndoCmd->deactivate();
216 // Process the commands that depend on this one
218 if ( _activationList )
219 for ( i = 0; i < _activationList->size(); i++ )
220 (*_activationList)[i]->activate();
222 if ( _deactivationList )
223 for ( i = 0; i < _deactivationList->size(); i++ )
224 (*_deactivationList)[i]->deactivate();
227 // Call the derived class's doit member function to
228 // perform the action represented by this object
238 // Call the derived class's undoit() member function.
242 // The system only supports one level of undo, and this is it,
243 // so deactivate the undo facility.
245 theUndoCmd->deactivate();
247 // Reverse the effects of the execute() member function by
248 // reverting all dependent objects to their previous state
250 if ( _activationList )
251 for ( i = 0; i < _activationList->size(); i++ )
252 (*_activationList)[i]->revert();
254 if ( _deactivationList )
255 for ( i = 0; i < _deactivationList->size(); i++ )
256 (*_deactivationList)[i]->revert();
259 #ifndef CAN_INLINE_VIRTUALS
265 #endif /* ! CAN_INLINE_VIRTUALS */