2
0

Cmd.C 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  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)
  10. * any later version.
  11. *
  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
  16. * details.
  17. *
  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
  22. */
  23. /* $TOG: Cmd.C /main/4 1998/07/24 16:04:37 mgreess $ */
  24. /*
  25. *+SNOTICE
  26. *
  27. * RESTRICTED CONFIDENTIAL INFORMATION:
  28. *
  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
  35. * Sun's request.
  36. *
  37. * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
  38. *
  39. *+ENOTICE
  40. */
  41. ///////////////////////////////////////////////////////////////////////////////
  42. //////////////////////////////////////////////////////////////////////////////
  43. // This example code is from the book:
  44. //
  45. // Object-Oriented Programming with C++ and OSF/Motif
  46. // by
  47. // Douglas Young
  48. // Prentice Hall, 1992
  49. // ISBN 0-13-630252-1
  50. //
  51. // Copyright 1991 by Prentice Hall
  52. // All Rights Reserved
  53. //
  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. //////////////////////////////////////////////////////////////////////////////
  59. ///////////////////////////////////////////////////////
  60. // Cmd.C
  61. ///////////////////////////////////////////////////////
  62. #include <stdlib.h>
  63. #include "Cmd.h"
  64. #include "CmdList.h"
  65. #include "CmdInterface.h"
  66. extern Cmd *theUndoCmd; // External object that reverses the
  67. // most recent Cmd when executed
  68. Cmd *Cmd::_lastCmd = NULL; // Pointer to most recent Cmd
  69. Cmd::Cmd ( char *name, char *label, int active )
  70. {
  71. // Initialize all data members
  72. _name = name;
  73. _active = active;
  74. _numInterfaces = 0;
  75. _ci = NULL;
  76. _activationList = NULL;
  77. _deactivationList = NULL;
  78. _hasUndo = TRUE;
  79. _previouslyActive = 0;
  80. if (label) {
  81. _label = strdup(label);
  82. } else {
  83. _label = strdup(name);
  84. }
  85. }
  86. Cmd::~Cmd()
  87. {
  88. delete _activationList;
  89. delete _deactivationList;
  90. free (_label);
  91. if (_ci)
  92. delete [] _ci;
  93. }
  94. void Cmd::registerInterface ( CmdInterface *ci )
  95. {
  96. // Make a new list, large enough for the new object
  97. CmdInterface **newList = new CmdInterface*[_numInterfaces + 1];
  98. // Copy the contents of the previous list to
  99. // the new list
  100. for( int i = 0; i < _numInterfaces; i++)
  101. newList[i] = _ci[i];
  102. // Free the old list
  103. if (_ci)
  104. delete []_ci;
  105. // Install the new list
  106. _ci = newList;
  107. // Add the object to the list and update the list size.
  108. _ci[_numInterfaces] = ci;
  109. _numInterfaces++;
  110. if ( ci )
  111. if ( _active )
  112. ci->activate();
  113. else
  114. ci->deactivate();
  115. }
  116. void Cmd::activate()
  117. {
  118. // Activate the associated interfaces
  119. for ( int i = 0; i < _numInterfaces; i++ )
  120. _ci[i]->activate ();
  121. // Save the current value of active before setting the new state
  122. _previouslyActive = _active;
  123. _active = TRUE;
  124. }
  125. void Cmd::deactivate()
  126. {
  127. // Deactivate the associated interfaces
  128. for ( int i = 0; i < _numInterfaces; i++ )
  129. _ci[i]->deactivate ();
  130. // Save the current value of active before setting the new state
  131. _previouslyActive = _active;
  132. _active = FALSE;
  133. }
  134. void Cmd::revert()
  135. {
  136. // Activate or deactivate, as necessary,
  137. // to return to the previous state
  138. if ( _previouslyActive )
  139. activate();
  140. else
  141. deactivate();
  142. }
  143. #ifdef DEAD_WOOD
  144. void Cmd::addToActivationList ( Cmd *cmd )
  145. {
  146. if ( !_activationList )
  147. _activationList = new CmdList();
  148. _activationList->add ( cmd );
  149. }
  150. void Cmd::addToDeactivationList ( Cmd *cmd )
  151. {
  152. if ( !_deactivationList )
  153. _deactivationList = new CmdList();
  154. _deactivationList->add ( cmd );
  155. }
  156. #endif /* DEAD_WOOD */
  157. void Cmd::execute()
  158. {
  159. int i;
  160. // If a command is inactive, it cannot be executed
  161. if ( !_active )
  162. return;
  163. // Activate or deactivate the global theUndoCmd,
  164. // and remember the last command, as needed
  165. if ( _hasUndo )
  166. {
  167. Cmd::_lastCmd = this;
  168. theUndoCmd->activate();
  169. }
  170. else
  171. {
  172. Cmd::_lastCmd = NULL;
  173. theUndoCmd->deactivate();
  174. }
  175. // Process the commands that depend on this one
  176. if ( _activationList )
  177. for ( i = 0; i < _activationList->size(); i++ )
  178. (*_activationList)[i]->activate();
  179. if ( _deactivationList )
  180. for ( i = 0; i < _deactivationList->size(); i++ )
  181. (*_deactivationList)[i]->deactivate();
  182. // Call the derived class's doit member function to
  183. // perform the action represented by this object
  184. doit();
  185. }
  186. void Cmd::undo()
  187. {
  188. int i;
  189. // Call the derived class's undoit() member function.
  190. undoit();
  191. // The system only supports one level of undo, and this is it,
  192. // so deactivate the undo facility.
  193. theUndoCmd->deactivate();
  194. // Reverse the effects of the execute() member function by
  195. // reverting all dependent objects to their previous state
  196. if ( _activationList )
  197. for ( i = 0; i < _activationList->size(); i++ )
  198. (*_activationList)[i]->revert();
  199. if ( _deactivationList )
  200. for ( i = 0; i < _deactivationList->size(); i++ )
  201. (*_deactivationList)[i]->revert();
  202. }
  203. #ifndef CAN_INLINE_VIRTUALS
  204. const char *const
  205. Cmd::className(void)
  206. {
  207. return "Cmd";
  208. }
  209. #endif /* ! CAN_INLINE_VIRTUALS */