Merge branch 'linux1'
[oweals/cde.git] / cde / programs / dtmail / MotifApp / CmdList.C
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 librararies 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 /* $XConsortium: CmdList.C /main/3 1995/11/06 15:59:50 rswiston $ */
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
61 ////////////////////////////////////////////////////////////
62 // CmdList.C: Maintain a list of Cmd objects
63 ////////////////////////////////////////////////////////////
64
65 ////////////////////////////////////////////////////////////
66 // MODIFIED TO INHERIT FROM CMD - not described in Book
67 ///////////////////////////////////////////////////////////
68
69
70 #include "CmdList.h"
71
72 CmdList::CmdList() : Cmd("CmdList", "CmdList", 1)
73 {
74    _contents = 0;
75    _numElements = 0;
76    _pane = NULL;
77 }
78
79 CmdList::CmdList(char *name, char *label ) : Cmd(name, label, 1)
80 {
81     // The list is initially empty
82     
83     _contents    = 0;
84     _numElements = 0;
85 }
86
87 CmdList::~CmdList()
88 {
89     // free the list
90     
91     delete []_contents;
92 }
93
94 void CmdList::add ( Cmd *cmd )
95 {
96     int i;
97     Cmd **newList;
98
99     // CmdList can only be undone if all Cmds it contains can be undone
100     
101     if(!cmd->hasUndo())
102         _hasUndo = 0;
103     
104     // Allocate a list large enough for one more element
105     
106     newList = new Cmd*[_numElements + 1];
107     
108     // Copy the contents of the previous list to
109     // the new list
110     
111     for( i = 0; i < _numElements; i++)
112         newList[i] = _contents[i];
113     
114     // Free the old list
115     
116     if (_contents)
117         delete []_contents;
118     
119     // Make the new list the current list
120     
121     _contents =  newList;
122     
123     // Add the command to the list and update the list size.
124     
125     _contents[_numElements] = cmd;
126     
127     _numElements++;
128 }
129
130 Cmd *CmdList::operator[] ( int index )
131 {
132     // Return the indexed element
133     
134     return _contents[index];
135 }
136
137
138 void CmdList::doit()
139 {
140     for( int i = 0; i < _numElements; i++)
141         _contents[i]->execute();
142 }
143
144 void CmdList::undoit()
145 {
146     if(_hasUndo)
147         for( int i = _numElements - 1; i >=0; i--)
148             _contents[i]->undo();
149 }