Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtmail / MotifApp / CmdList.C
1 /* $XConsortium: CmdList.C /main/3 1995/11/06 15:59:50 rswiston $ */
2 /*
3  *+SNOTICE
4  *
5  *      RESTRICTED CONFIDENTIAL INFORMATION:
6  *      
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
13  *      Sun's request.
14  *
15  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
16  *
17  *+ENOTICE
18  */
19 ///////////////////////////////////////////////////////////////////////////////
20 //////////////////////////////////////////////////////////////////////////////
21 //         This example code is from the book:
22 //
23 //           Object-Oriented Programming with C++ and OSF/Motif
24 //         by
25 //           Douglas Young
26 //           Prentice Hall, 1992
27 //           ISBN 0-13-630252-1 
28 //
29 //         Copyright 1991 by Prentice Hall
30 //         All Rights Reserved
31 //
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 //////////////////////////////////////////////////////////////////////////////
37
38
39 ////////////////////////////////////////////////////////////
40 // CmdList.C: Maintain a list of Cmd objects
41 ////////////////////////////////////////////////////////////
42
43 ////////////////////////////////////////////////////////////
44 // MODIFIED TO INHERIT FROM CMD - not described in Book
45 ///////////////////////////////////////////////////////////
46
47
48 #include "CmdList.h"
49
50 CmdList::CmdList() : Cmd("CmdList", "CmdList", 1)
51 {
52    _contents = 0;
53    _numElements = 0;
54    _pane = NULL;
55 }
56
57 CmdList::CmdList(char *name, char *label ) : Cmd(name, label, 1)
58 {
59     // The list is initially empty
60     
61     _contents    = 0;
62     _numElements = 0;
63 }
64
65 CmdList::~CmdList()
66 {
67     // free the list
68     
69     delete []_contents;
70 }
71
72 void CmdList::add ( Cmd *cmd )
73 {
74     int i;
75     Cmd **newList;
76
77     // CmdList can only be undone if all Cmds it contains can be undone
78     
79     if(!cmd->hasUndo())
80         _hasUndo = 0;
81     
82     // Allocate a list large enough for one more element
83     
84     newList = new Cmd*[_numElements + 1];
85     
86     // Copy the contents of the previous list to
87     // the new list
88     
89     for( i = 0; i < _numElements; i++)
90         newList[i] = _contents[i];
91     
92     // Free the old list
93     
94     if (_contents)
95         delete []_contents;
96     
97     // Make the new list the current list
98     
99     _contents =  newList;
100     
101     // Add the command to the list and update the list size.
102     
103     _contents[_numElements] = cmd;
104     
105     _numElements++;
106 }
107
108 Cmd *CmdList::operator[] ( int index )
109 {
110     // Return the indexed element
111     
112     return _contents[index];
113 }
114
115
116 void CmdList::doit()
117 {
118     for( int i = 0; i < _numElements; i++)
119         _contents[i]->execute();
120 }
121
122 void CmdList::undoit()
123 {
124     if(_hasUndo)
125         for( int i = _numElements - 1; i >=0; i--)
126             _contents[i]->undo();
127 }