Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtappbuilder / src / ab / tmodeP.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 /*
25  *      $XConsortium: tmodeP.c /main/3 1995/11/06 17:54:59 rswiston $
26  *
27  * @(#)tmodeP.c 1.1 15 Jan 1995 cde_app_builder/src/ab
28  *
29  *      RESTRICTED CONFIDENTIAL INFORMATION:
30  *      
31  *      The information in this document is subject to special
32  *      restrictions in a confidential disclosure agreement between
33  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
34  *      document outside HP, IBM, Sun, USL, SCO, or Univel without
35  *      Sun's specific written approval.  This document and all copies
36  *      and derivative works thereof must be returned or destroyed at
37  *      Sun's request.
38  *
39  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
40  *
41  */
42
43
44 /*
45 ** File: tmodeP.c - Common Test Mode functionality
46 */
47
48 #include "tmodeP.h"
49 #include <ab_private/trav.h>
50 #include <ab_private/util.h>
51 #include <ab_private/ui_util.h>
52
53
54 /*****************************************************************************
55 **                                                                          **
56 **       Private Function Declarations                                      **
57 **                                                                          **
58 *****************************************************************************/
59 static ABObjList tmodeP_list_create(
60                      void
61                  );
62 static int       tmodeP_list_destroy(
63                      ABObjList list
64                  );
65
66
67 /*****************************************************************************
68 **                                                                          **
69 **       Function Definitions                                               **
70 **                                                                          **
71 *****************************************************************************/
72
73 /*
74  * Test Mode Data Structure
75  */
76
77 /* Create a Test Mode data structure and initialize it to it's defaults     */
78 int
79 tmodeP_obj_create_data(
80     ABObj obj
81 )
82 {
83     if (!obj)
84         return ERROR;
85
86     if (tmodeP_obj_has_data(obj))
87         tmodeP_obj_destroy_data(obj);
88
89     tmodeP_obj_data(obj) = (TestModeData) util_malloc(sizeof(TestModeDataRec));
90
91     if (!tmodeP_obj_has_data(obj))
92         return ERROR;
93
94     return tmodeP_obj_construct_data(obj);
95 }
96
97 /* Destroy a Test Mode data structure                                       */
98 int
99 tmodeP_obj_destroy_data(
100     ABObj obj
101 )
102 {
103     if (!obj)
104         return ERROR;
105
106     if (tmodeP_obj_has_data(obj))
107         util_free(tmodeP_obj_data(obj));
108
109     tmodeP_obj_data(obj) = NULL;
110     return OK;
111 }
112
113 /* Initialize a Test Mode data structure to it's defaults                   */
114 int
115 tmodeP_obj_construct_data(
116     ABObj obj
117 )
118 {
119     if (!obj || !obj->test_mode_data)
120         return ERROR;
121
122     tmodeP_obj_set_width( obj, 0);
123     tmodeP_obj_set_height(obj, 0);
124     tmodeP_obj_set_x(     obj, 0);
125     tmodeP_obj_set_y(     obj, 0);
126
127     return OK;
128 }
129
130
131 /*
132  * Window List
133  */
134 static ABObjList wlist = NULL;               /* windows in the project      */
135
136 /* Create the window list. (and populate it)                                */
137 int
138 tmodeP_window_list_create(
139     ABObj     project
140 )
141 {
142     if (!project)
143         return ERROR;
144     
145     if (wlist)
146         tmodeP_list_destroy(wlist);
147
148     if ((wlist = tmodeP_list_create()) == NULL)
149         return ERROR;
150     
151     if (tmodeP_window_list_construct(project) != OK)
152     {
153         tmodeP_list_destroy(wlist);
154         return ERROR;
155     }
156     
157     return OK;
158 }
159
160 /* Populate the window list                                                 */
161 int
162 tmodeP_window_list_construct(
163     ABObj     project
164 )
165 {
166     ABObj               obj;
167     AB_TRAVERSAL        trav;
168     int                 status;
169     
170     if (!project || !wlist)
171         return ERROR;
172
173     /* populate the list */
174     for (trav_open(&trav, project, AB_TRAV_WINDOWS);
175          (obj = trav_next(&trav)) != NULL; )
176     {
177         status = objlist_add_obj(wlist, obj, NULL);
178         if (status == 0)
179         {
180             tmodeP_obj_construct_flags(obj);
181             if (tmodeP_obj_create_data(obj) != OK)
182                 util_dprintf(1,
183                     "tmodeP_window_list_construct: Unable to create data.\n");      
184         }
185         else if (status == ERR_DUPLICATE_KEY)
186             util_dprintf(1,
187                 "tmodeP_window_list_construct: Duplicate window in obj structure.\n");
188         else
189             util_dprintf(1,
190                 "tmodeP_window_list_construct: Unable to add window to list.\n");
191     }
192     trav_close(&trav);
193
194     return OK;
195 }
196
197 /* UnPopulate the window list                                               */
198 int
199 tmodeP_window_list_destruct(
200     void
201 )
202 {
203     int     i;
204     ABObj obj = NULL;
205     
206     if (!wlist)
207         return ERROR;
208     
209     /* trav the list of windows, destroying test data */
210     for (i=0; i < objlist_get_num_objs(wlist); i++)
211     {
212         obj = objlist_get_obj(wlist, i, NULL);
213         
214         if (!obj)
215         {
216             util_dprintf(1,
217                 "tmodeP_window_list_destruct: %d window's obj is missing.\n", i);
218             continue;
219         }
220
221         tmodeP_obj_construct_flags(obj);
222         
223         if (tmodeP_obj_has_data(obj))
224         {
225             if (tmodeP_obj_destroy_data(obj) == ERROR)
226             {
227                 util_dprintf(1,
228                     "tmodeP_window_list_destruct: Can't free test mode data for %d window.\n", i);
229                 continue;
230             }
231         }
232     }
233
234     return OK;
235 }
236
237 /* Destroy the window list                                                  */
238 int
239 tmodeP_window_list_destroy(
240     void
241 )
242 {
243     if (!wlist)
244         return OK;
245     
246     /* clean the list */
247     if (tmodeP_window_list_destruct() != OK)
248         util_dprintf(1, "tmodeP_window_list_destroy: Can't destroy some test mode data.\n");
249     
250     /* destroy the list */
251     tmodeP_list_destroy(wlist);
252     wlist = NULL;
253     
254     return OK;
255 }
256
257 /*
258 ** iterate the supplied function over the window list
259 */
260 void
261 tmodeP_window_list_iterate(
262     ABObjListIterFn fn
263 )
264 {
265     int    i;
266     ABObj  obj = NULL;
267     
268     if (!wlist)
269         return;
270     
271     objlist_iterate(wlist, fn);
272 }
273
274 /*
275 ** foreach of the windows in the window list
276 **      find the window's shell
277 **      XtAddEventHandler() to the shell
278 **         client_data = obj.
279 */
280 int
281 tmodeP_window_list_add_handler(
282     EventMask          event_mask,
283     Boolean            nonmaskable,
284     XtEventHandler     event_handler
285 )
286 {
287     int    i;
288     Widget w;
289     ABObj  obj = NULL;
290     
291     if (!wlist)
292         return ERROR;
293     
294     /* trav the list of windows */
295     for (i=0; i < objlist_get_num_objs(wlist); i++)
296     {
297         obj = objlist_get_obj(wlist, i, NULL);
298         
299         if (!obj)
300         {
301             util_dprintf(1,
302                 "tmodeP_window_list_add_handler: %d window is missing obj.\n", i);
303             continue;
304         }
305
306         if (!obj->ui_handle)
307         {
308             util_dprintf(2,
309                 "tmodeP_window_list_add_handler: no ui_handle %d window.\n", i);
310             continue;
311         }
312
313         w = ui_get_ancestor_shell(obj->ui_handle);
314         
315         XtAddEventHandler(
316             w,                  /* Widget */
317             event_mask,         /* EventMask */
318             nonmaskable,        /* non-maskable events - Boolean */
319             event_handler,      /* XtEventHandler */
320             (XtPointer) obj     /* client_data */
321         );
322     }
323     return OK;
324 }
325
326 /*
327 ** foreach of the windows in the window list
328 **      find the window's shell
329 **      XtRemoveEventHandler() on the shell
330 **         client_data = obj.
331 */
332 int
333 tmodeP_window_list_remove_handler(
334     EventMask          event_mask,
335     Boolean            nonmaskable,
336     XtEventHandler     event_handler
337 )
338 {
339     int    i;
340     Widget w;
341     ABObj  obj = NULL;
342     
343     if (!wlist)                 /* can't remove the handlers from a */
344         return OK;              /* non-existant list, but it's not an error */
345     
346     /* trav the list of windows */
347     for (i=0; i < objlist_get_num_objs(wlist); i++)
348     {
349         obj = objlist_get_obj(wlist, i, NULL);
350         
351         if (!obj)
352         {
353             util_dprintf(1,
354                 "tmodeP_window_list_remove_handler: %d window is missing obj.\n", i);
355             continue;
356         }
357
358         if (!obj->ui_handle)
359         {
360             util_dprintf(2,
361                 "tmodeP_window_list_remove_handler: no ui_handle %d window.\n", i);
362             continue;
363         }
364
365         w = ui_get_ancestor_shell(obj->ui_handle);
366         
367         XtRemoveEventHandler(
368             w,                  /* Widget */
369             event_mask,         /* EventMask */
370             nonmaskable,        /* non-maskable events - Boolean */
371             event_handler,      /* XtEventHandler */
372             (XtPointer) obj     /* client_data */
373         );
374     }
375     return OK;
376 }
377
378     
379 /*****************************************************************************
380 **                                                                          **
381 **       Private Function Definitions                                       **
382 **                                                                          **
383 *****************************************************************************/
384
385 /*
386  * List Manipulation
387  */
388
389 /* Create a List                                                            */
390 static ABObjList
391 tmodeP_list_create(
392     void
393 )
394 {
395     ABObjList list = NULL;
396     
397     if ((list = objlist_create()) == NULL)
398         return NULL;
399
400     objlist_set_sort_order(list, OBJLIST_SORT_BEST);
401     
402     return list;
403 }
404
405 /* Destroy a List                                                           */
406 static int
407 tmodeP_list_destroy(
408     ABObjList list
409 )
410 {
411     if (list)
412         objlist_destroy(list);
413     return OK;
414 }