Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtappbuilder / src / ab / abobj_list.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: abobj_list.c /main/3 1995/11/06 17:16:26 rswiston $
26  *
27  * @(#)abobj_list.c     1.7 25 Jul 1994 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  * File: abobj_list.c - handles a scrolling list of ABObjs
45  */
46
47 #include <stdio.h>
48 #include <Xm/List.h>
49 #include <ab_private/abobj_list.h>
50 #include <ab_private/ui_util.h>
51 #include <ab_private/trav.h>
52
53
54 /*************************************************************************
55 **                                                                      **
56 **       Private Function Declarations                                  **
57 **                                                                      **
58 **************************************************************************/
59
60 /*************************************************************************
61 **                                                                      **
62 **       Data                                                           **
63 **                                                                      **
64 **************************************************************************/
65
66 /*************************************************************************
67 **                                                                      **
68 **       Function Definitions                                           **
69 **                                                                      **
70 **************************************************************************/
71
72 /*
73 ** Traverse the project and load all objects into the scrolling list.  This
74 ** is a generalized function that traverses all salient objects as candidates
75 ** to be displayed.  It takes an additional test function as an argument, and
76 ** applies that function to each object in turn.  The object will appear in
77 ** the list only if the test function returns non-zero.
78 **
79 ** ("He's making a list, and checking it twice..."  (well, o.k., once))
80 */
81 int
82 abobj_list_load(
83         Widget          list,
84         ABObj           root,
85         ABObjTestFunc   list_obj_func
86 )
87 {
88     int         return_value = 0;
89
90     if ((root == NULL) || (list == NULL)) return(0);
91
92     /* Clear out the list so we can start afresh */
93     XmListDeleteAllItems(list);
94
95     return_value = abobj_list_update(list, root, list_obj_func);
96     return return_value;
97 }
98
99
100 int
101 abobj_list_update(
102         Widget          list,
103         ABObj           root,
104         ABObjTestFunc   list_obj_func
105 )
106 {
107     ABObj               obj = NULL;
108     AB_TRAVERSAL    trav;
109     STRING          modname;
110     int             item_count = 0;
111     int                 item_pos = 0;
112
113     if ((root == NULL) || (list == NULL)) return(0);
114
115     for( (trav_open(&trav,root,AB_TRAV_SALIENT));
116          (obj = trav_next(&trav)) != NULL; ) 
117         {
118             if (obj_get_name(obj) != NULL)
119             {
120                 modname = abobj_get_moduled_name(obj);
121                 if((*list_obj_func)(obj))
122                 {
123                     if (ui_list_find_item(list,modname,&item_pos)
124                             == ERR_NOT_FOUND)
125                     {
126                         ui_list_add_item(list,modname,0);
127                     }
128                     item_count++;
129                 }
130                 else
131                 {
132                     ui_list_delete_item(list,modname);
133                 }
134                 XtFree(modname);
135             }
136         }
137     trav_close(&trav);
138     return(item_count);
139 }
140
141
142 /*
143  * An object was created - add it to the list (maybe)
144  *
145  * Returns a negative ERR_ code if an error occurs
146  * Returns 0 if the object was not added
147  * Returns 1 if the object was added to the list
148  */
149 int
150 abobj_list_obj_created(
151     Widget              list,
152     ABObj               obj,
153     ABObjTestFunc       list_obj_test
154 )
155 {
156     int         return_value = 0;
157     int         rc = 0;                 /* return code */
158
159     if ((obj == NULL) || (list == NULL)) return(0);
160
161     if (!list_obj_test(obj))
162     {
163         return_value = 0;
164         goto epilogue;
165     }
166     if (obj_get_name(obj) != NULL)
167     {
168         STRING  modname = abobj_get_moduled_name(obj);
169         rc = ui_list_add_item(list, modname, 0);
170         if (rc >= 0)
171         {
172             return_value = 1;
173         }
174         else
175         {
176             return_value = rc;
177         }
178
179         XtFree(modname); modname = NULL;
180     }
181
182 epilogue:
183     return return_value;
184 }
185
186
187 /*
188 ** Some object has been deleted and we should act accordingly.
189 ** Note -- this is a synthetic callback generated from within AppBuilder,
190 ** not a traditional Xt-style callback (hence the different argument style)
191 */
192 int
193 abobj_list_obj_destroyed(
194     Widget              list,
195     ABObj               obj,
196     ABObjTestFunc       list_obj_test
197 )
198 {
199     STRING      modname = NULL;
200
201     if ((obj == NULL) || (list == NULL)) return(0);
202
203     /* 
204     ** Check to make sure this is an object we'd have in the list to 
205     ** begin with.
206     */
207     if (list_obj_test(obj)) {
208         modname = abobj_get_moduled_name(obj);
209         ui_list_delete_item(list, modname);
210         XtFree(modname);
211     }
212     return(0);
213 }
214
215 /*
216 ** Some object has been renamed and we should act accordingly.
217 ** Note -- this is a synthetic callback generated from within AppBuilder,
218 ** not a traditional Xt-style callback (hence the different argument style)
219 */
220 int
221 abobj_list_obj_renamed(
222     Widget              list,
223     ABObj               obj,
224     STRING              old_name,
225     ABObjTestFunc       list_obj_test
226 )
227 {
228     int         rc = 0;         /* return code */
229     STRING      new_modname = NULL;
230     STRING      old_modname = NULL;
231     ABObj       module = obj_get_module(obj);
232
233     if ((obj == NULL) || (list == NULL)) return(0);
234
235     if (obj_is_module(obj))
236     {
237         ui_list_replace_item_prefix(
238             list, old_name, obj_get_name(obj));
239     }
240     else if ((module != NULL) && list_obj_test(obj)) {
241
242         new_modname = abobj_get_moduled_name(obj);
243         old_modname = abobj_alloc_moduled_name(
244                         obj_get_name(module), old_name);
245         rc = ui_list_replace_item(list, old_modname, new_modname);
246         if (rc == ERR_NOT_FOUND)
247         {
248             ui_list_add_item(list, new_modname, 0);
249         }
250         XtFree(new_modname);
251         XtFree(old_modname);
252     }
253     return(0);
254 }
255
256
257 /*
258  * Completely rebuild all that is known about this object or tree
259  */
260 int
261 abobj_list_obj_updated(
262     Widget              list,
263     ObjEvUpdateInfo     info,
264     ABObjTestFunc       list_obj_test
265 )
266 {
267     return abobj_list_update(list, info->obj, list_obj_test);
268 }
269
270 int
271 abobj_list_obj_reparented(
272     Widget              list,
273     ObjEvReparentInfo   info,
274     ABObjTestFunc       list_obj_test
275 )
276 {
277     static char full_name[BUFSIZ];
278     STRING      moduled_name = NULL,
279                 module = NULL,
280                 name = NULL;
281     ABObj       obj = NULL;
282     int         ret = 0;
283
284     obj = info->obj; 
285     if ((obj == NULL) || (list == NULL)) return(0);
286
287     if (obj_get_parent(obj) == NULL)
288     {
289         module = obj_get_name(obj_get_module(info->old_parent));
290         strcpy(full_name, module);
291         strcat(full_name, " :: ");
292         strcat(full_name, obj_get_name(obj));
293         name = full_name;
294     }
295     else
296         name = abobj_get_moduled_name(obj);
297
298     if (list_obj_test(obj))
299         ret = ui_list_add_item(list, name, 0);
300     else
301         ret = ui_list_delete_item(list, name);
302
303     return (ret);
304 }