home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / MotifApp / Extras / SubMenus / CmdList.C next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  2.5 KB  |  108 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////
  3. //         This example code is from the book:
  4. //
  5. //           Object-Oriented Programming with C++ and OSF/Motif
  6. //         by
  7. //           Douglas Young
  8. //           Prentice Hall, 1992
  9. //           ISBN 0-13-630252-1    
  10. //
  11. //         Copyright 1991 by Prentice Hall
  12. //         All Rights Reserved
  13. //
  14. //  Permission to use, copy, modify, and distribute this software for 
  15. //  any purpose except publication and without fee is hereby granted, provided 
  16. //  that the above copyright notice appear in all copies of the software.
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. ////////////////////////////////////////////////////////////
  22. // CmdList.C: Maintain a list of Cmd objects
  23. ////////////////////////////////////////////////////////////
  24.  
  25. ////////////////////////////////////////////////////////////
  26. // MODIFIED TO INHERIT FROM CMD - not described in Book
  27. ///////////////////////////////////////////////////////////
  28.  
  29.  
  30. #include "CmdList.h"
  31.  
  32. CmdList::CmdList() : Cmd("CmdList", 1)
  33. {
  34.    _contents = 0;
  35.    _numElements = 0;
  36. }
  37.  
  38. CmdList::CmdList(char *name ) : Cmd(name, 1)
  39. {
  40.     // The list is initially empty
  41.     
  42.     _contents    = 0;
  43.     _numElements = 0;
  44. }
  45.  
  46. CmdList::~CmdList()
  47. {
  48.     // free the list
  49.     
  50.     delete []_contents;
  51. }
  52.  
  53. void CmdList::add ( Cmd *cmd )
  54. {
  55.     int i;
  56.     Cmd **newList;
  57.  
  58.     // CmdList can only be undone if all Cmds it contains can be undone
  59.     
  60.     if(!cmd->hasUndo())
  61.     _hasUndo = 0;
  62.     
  63.     // Allocate a list large enough for one more element
  64.     
  65.     newList = new Cmd*[_numElements + 1];
  66.     
  67.     // Copy the contents of the previous list to
  68.     // the new list
  69.     
  70.     for( i = 0; i < _numElements; i++)
  71.     newList[i] = _contents[i];
  72.     
  73.     // Free the old list
  74.     
  75.     delete []_contents;
  76.     
  77.     // Make the new list the current list
  78.     
  79.     _contents =  newList;
  80.     
  81.     // Add the command to the list and update the list size.
  82.     
  83.     _contents[_numElements] = cmd;
  84.     
  85.     _numElements++;
  86. }
  87.  
  88. Cmd *CmdList::operator[] ( int index )
  89. {
  90.     // Return the indexed element
  91.     
  92.     return _contents[index];
  93. }
  94.  
  95.  
  96. void CmdList::doit()
  97. {
  98.     for( int i = 0; i < _numElements; i++)
  99.     _contents[i]->execute();
  100. }
  101.  
  102. void CmdList::undoit()
  103. {
  104.     if(_hasUndo)
  105.     for( int i = _numElements - 1; i >=0; i--)
  106.         _contents[i]->undo();
  107. }
  108.