Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Basic / OutlineList.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 /*
24  * $XConsortium: OutlineList.C /main/4 1996/10/04 11:13:58 drk $
25  *
26  * Copyright (c) 1992 HaL Computer Systems, Inc.  All rights reserved.
27  * UNPUBLISHED -- rights reserved under the Copyright Laws of the United
28  * States.  Use of a copyright notice is precautionary only and does not
29  * imply publication or disclosure.
30  * 
31  * This software contains confidential information and trade secrets of HaL
32  * Computer Systems, Inc.  Use, disclosure, or reproduction is prohibited
33  * without the prior express written permission of HaL Computer Systems, Inc.
34  * 
35  *                         RESTRICTED RIGHTS LEGEND
36  * Use, duplication, or disclosure by the Government is subject to
37  * restrictions as set forth in subparagraph (c)(l)(ii) of the Rights in
38  * Technical Data and Computer Software clause at DFARS 252.227-7013.
39  *                        HaL Computer Systems, Inc.
40  *                  1315 Dell Avenue, Campbell, CA  95008
41  * 
42  */
43
44 #define C_OutlineList
45 #define C_OutlineElement
46 #define L_Basic
47
48 #include "Prelude.h"
49
50 #define OUTLINE_ELEMENT(I) ((OutlineElement *)f_list_element[i])
51
52
53 // /////////////////////////////////////////////////////////////////
54 // class constructor
55 // /////////////////////////////////////////////////////////////////
56
57 OutlineList::OutlineList (int initial_size, int increment,
58                           List::grow_method_t grow_method)
59 : List (initial_size, increment, grow_method)
60 {
61 }
62
63
64 // /////////////////////////////////////////////////////////////////
65 // class destructor
66 // /////////////////////////////////////////////////////////////////
67
68 OutlineList::~OutlineList ()
69 {
70   // recursively nuke lists
71 }
72
73
74 // /////////////////////////////////////////////////////////////////
75 // count_expanded - return the number expanded below + number here
76 // /////////////////////////////////////////////////////////////////
77
78 u_int
79 OutlineList::count_expanded (BitHandle data_handle)
80 {
81   u_int i, count = 0;
82
83   for (i = 0; i < f_length; i++)
84     if (OUTLINE_ELEMENT(i)->is_expanded (data_handle) &&
85         OUTLINE_ELEMENT(i)->children() != NULL)
86       count += OUTLINE_ELEMENT(i)->f_children->count_expanded (data_handle);
87
88   count += f_length;
89
90   return (count);
91 }
92
93
94 // /////////////////////////////////////////////////////////////////////////
95 // copy_selected - copy selected flags from src pos to dest pos
96 // /////////////////////////////////////////////////////////////////////////
97
98 void
99 OutlineList::copy_selected(BitHandle src, BitHandle dest)
100 {
101   for (int i = 0 ; i < length(); i++)
102     {
103       if (OUTLINE_ELEMENT(i)->is_selected(src))
104         OUTLINE_ELEMENT(i)->set_selected(dest) ;
105       else
106         OUTLINE_ELEMENT(i)->unset_selected(dest);
107       // do children
108       if (OUTLINE_ELEMENT(i)->children_cached() &&
109           OUTLINE_ELEMENT(i)->has_children())
110         OUTLINE_ELEMENT(i)->children()->copy_selected(src, dest);
111     }
112 }
113
114
115 // /////////////////////////////////////////////////////////////////////////
116 // copy_expanded - copy expanded flags from src pos to dest pos
117 // /////////////////////////////////////////////////////////////////////////
118
119 void
120 OutlineList::copy_expanded (BitHandle src, BitHandle dest)
121 {
122   for (int i = 0 ; i < length(); i++)
123     {
124       if (OUTLINE_ELEMENT(i)->is_expanded (src))
125         OUTLINE_ELEMENT(i)->set_expanded (dest);
126       else
127         OUTLINE_ELEMENT(i)->set_contracted (dest);
128       // do children
129       if (OUTLINE_ELEMENT(i)->children_cached() &&
130           OUTLINE_ELEMENT(i)->has_children())
131         OUTLINE_ELEMENT(i)->children()->copy_expanded (src, dest);
132     }
133 }
134
135
136 // /////////////////////////////////////////////////////////////////////////
137 // recursive_select - if item is (un)selected, (un)select all children
138 // /////////////////////////////////////////////////////////////////////////
139
140 void
141 OutlineList::recursive_select(BitHandle data_handle)
142 {
143   for (int i = 0 ; i < length() ; i++ )
144     if (OUTLINE_ELEMENT(i)->children_cached() &&
145         OUTLINE_ELEMENT(i)->has_children())
146       {
147         if (OUTLINE_ELEMENT(i)->is_selected(data_handle))
148           OUTLINE_ELEMENT(i)->children()->select_all(data_handle);
149         else
150           OUTLINE_ELEMENT(i)->children()->deselect_all(data_handle);
151       }
152 }
153
154
155 // /////////////////////////////////////////////////////////////////////////
156 // select_all - select all items and same for children
157 // /////////////////////////////////////////////////////////////////////////
158
159 void
160 OutlineList::select_all(BitHandle data_handle)
161 {
162   for (int i = 0 ; i < length(); i++)
163     {
164       OUTLINE_ELEMENT(i)->set_selected(data_handle);
165       if (OUTLINE_ELEMENT(i)->children_cached() &&
166           OUTLINE_ELEMENT(i)->has_children())
167         OUTLINE_ELEMENT(i)->children()->select_all(data_handle);
168     }
169   
170 }
171
172
173 // /////////////////////////////////////////////////////////////////////////
174 // deselect_all - unselect all items and same for children
175 // /////////////////////////////////////////////////////////////////////////
176
177 void
178 OutlineList::deselect_all(BitHandle data_handle)
179 {
180   for (int i = 0 ; i < length(); i++)
181     {
182       OUTLINE_ELEMENT(i)->unset_selected(data_handle);
183       if (OUTLINE_ELEMENT(i)->children_cached() &&
184           OUTLINE_ELEMENT(i)->has_children())
185         OUTLINE_ELEMENT(i)->children()->deselect_all(data_handle);
186     }
187   
188 }
189
190
191 // /////////////////////////////////////////////////////////////////
192 // OutlineElement::print - print representation of self
193 // /////////////////////////////////////////////////////////////////
194
195 #ifdef DEBUG
196 #undef PRINT_POINTER
197 #define PRINT_POINTER(PTR) \
198   printf ("  PTR = 0x%04lx\n", PTR)
199 #undef PRINT_BITS
200 #define PRINT_BITS(BITS) \
201   { long bits = BITS; printf ("  BITS = 0x%04x", BITS); \
202     for (int i = 0; i < sizeof(BITS) * 8; i++, bits <<= 1) { \
203       if (!(i%8)) putchar (' '); \
204       (bits & (1L << (sizeof(BITS) * 8)-1)) ? putchar('1') : putchar('0'); } \
205     putchar ('\n'); } 
206
207 void
208 OutlineElement::print ()
209 {
210 #if 0
211   PRINT_OBJECT (OutlineElement);
212   PRINT_POINTER (f_object);
213   PRINT_POINTER (f_children);
214   PRINT_INT (f_level);
215   PRINT_BITS (f_selected);
216   PRINT_BITS (f_expanded);
217   PRINT_POINTER (f_xm_string);
218 #else
219   puts ("WARNING: Can't print OutlineElement due to source code restriction.");
220 #endif
221 }
222 #endif /* DEBUG */
223
224 // /////////////////////////////////////////////////////////////////
225 // selected_items
226 // /////////////////////////////////////////////////////////////////
227
228 List *
229 OutlineList::selected_items (BitHandle data_handle, List *l)
230 {
231   if (l == NULL)
232     l = new List (8, 8, List::GROW_MULTIPLY);
233
234   for (int i = 0; i < length(); i++)
235     {
236       if (OUTLINE_ELEMENT(i)->is_selected (data_handle))
237         l->append (OUTLINE_ELEMENT(i));
238       if (OUTLINE_ELEMENT(i)->children_cached() &&
239           OUTLINE_ELEMENT(i)->has_children())
240         OUTLINE_ELEMENT(i)->children()->selected_items (data_handle, l);
241     }
242
243   return (l);
244 }