Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtappbuilder / src / abmf / connect.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: connect.c /main/3 1995/11/06 18:03:27 rswiston $
26  * 
27  * @(#)connect.c        3.25 13 Feb 1994        cde_app_builder/src/abmf
28  * 
29  * RESTRICTED CONFIDENTIAL INFORMATION:
30  * 
31  * The information in this document is subject to special restrictions in a
32  * confidential disclosure agreement between HP, IBM, Sun, USL, SCO and
33  * Univel.  Do not distribute this document outside HP, IBM, Sun, USL, SCO,
34  * or Univel without Sun's specific written approval.  This document and all
35  * copies and derivative works thereof must be returned or destroyed at Sun's
36  * request.
37  * 
38  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
39  * 
40  */
41
42 /*
43  * connect.c - Handles connections stuff
44  */
45
46 #include <stdio.h>
47 #include <string.h>
48 #include <ab_private/trav.h>
49 #include "ui_header_fileP.h"    /* abmfP_comp_get_widget_specific_includes */
50 #include "obj_namesP.h"
51 #include "connectP.h"
52
53 #define APP_PREFIX_LEN  4
54 #define APP_PREFIX      "_app"
55
56 static int      abmfP_set_action_names_for_obj(ABObj rootObj);
57
58
59 STRING
60 abmfP_get_action_name(ABObj action)
61 {
62     static char         name[256];
63     STRING              parent_name = (STRING) NULL;
64     ABObj               from = NULL, parent = NULL;
65     STRING              tmpName = NULL;
66     *name = 0;
67
68     from = obj_get_from(action);
69     if (action->info.action.auto_named && (from != NULL))
70     {
71         /* If the source object is the project,
72          * then this is an "Application" connection.
73          * This probably won't happen because right
74          * now "Application" connections can only be
75          * of type AB_FUNC_USER_DEF (which means auto-
76          * named is FALSE).
77          */
78         if (obj_is_project(from))
79         {
80             parent = obj_get_project(action);
81             parent_name = (STRING) util_malloc(
82                                 strlen(obj_get_name(parent)) 
83                                 + APP_PREFIX_LEN + 1);
84             strcpy(parent_name, obj_get_name(parent));
85             strcat(parent_name, APP_PREFIX);
86         }
87         else
88         {
89             parent = obj_get_module(from);
90             parent_name = obj_get_name(parent);
91         }
92
93         if (!util_strempty(parent_name) &&
94             !util_strempty(obj_get_name(from)))
95         {
96             sprintf(name, "%s_%s%s",
97                     parent_name,
98                     obj_get_name(from),
99                     obj_get_func_name_suffix(action));
100         }
101
102         if (obj_is_project(from))
103         {
104             util_free(parent_name);
105         }
106     }
107     else
108     {
109         if ((tmpName = obj_get_func_name(action)) != NULL)
110         {
111             sprintf(name, "%s", tmpName);
112         }
113     }
114
115     return (name[0] == 0 ? NULL : name);
116 }
117
118
119 /*
120  *
121  */
122 int
123 abmfP_tree_set_action_names(ABObj rootObj)
124 {
125     int                 return_value = 0;
126     AB_TRAVERSAL        moduleTrav;
127     ABObj               module = NULL;
128
129     if (!obj_is_project(rootObj))
130     {
131         return_value = abmfP_set_action_names_for_obj(rootObj);
132     }
133     else
134     {
135         /* for efficiency, we'll only visit those modules that are 
136          * going to be written
137          */
138         abmfP_set_action_names_for_obj(rootObj);        /* project 1st */
139
140         for (trav_open(&moduleTrav, rootObj, AB_TRAV_MODULES);
141                 (module = trav_next(&moduleTrav)) != NULL; )
142         {
143             if (obj_get_write_me(module))
144             {
145                 abmfP_set_action_names_for_obj(module); /* each module */
146             }
147         }
148         trav_close(&moduleTrav);
149     }
150
151     return return_value;
152 }
153
154
155 static int
156 abmfP_set_action_names_for_obj(ABObj rootObj)
157 {
158     int                 return_value = 0;
159     int                 actionNum = -1;
160     AB_TRAVERSAL        actionTrav;
161     ABObj               action = NULL;
162     ABObj               fromObj = NULL;
163     char                buf[256];
164
165     if (mfobj_data(rootObj) == NULL)
166     {
167         return -1;
168     }
169
170     for (trav_open(&actionTrav, rootObj, AB_TRAV_ACTIONS_FOR_OBJ);
171                 (action = trav_next(&actionTrav)) != NULL;)
172     {
173         if (   (obj_get_func_type(action) != AB_FUNC_USER_DEF)
174             && ((fromObj = obj_get_from(action)) != NULL) )
175         {
176                 action->info.action.auto_named = TRUE;
177                 actionNum = mfobj_data(fromObj)->num_auto_callbacks + 1;
178                 mfobj_data(fromObj)->num_auto_callbacks = actionNum;
179                 sprintf(buf, "_CB%d", actionNum);
180                 obj_set_func_name_suffix(action, buf);
181         }
182     }
183     trav_close(&actionTrav);
184
185     return return_value;
186 }
187
188
189 int
190 abmfP_get_connect_includes(StringList includeFiles, ABObj projOrModule)
191 {
192     AB_TRAVERSAL        trav;
193     BOOL                actionIsCrossModule = FALSE;
194     ABObj               project = obj_get_project(projOrModule);
195     ABObj               action = NULL;
196     ABObj               toObj = NULL;
197     ABObj               compRoot = NULL;
198     ABObj               toModule = NULL;
199     ABObj               win_parent = NULL;
200     ABObj               popup_win = NULL;
201     char                headerFilename[MAXPATHLEN+1];
202     *headerFilename = 0;
203
204     strlist_set_is_unique(includeFiles, TRUE);
205     assert(project != NULL);
206     if (project == NULL)
207     {
208         return NULL;
209     }
210
211     for (trav_open(&trav, projOrModule, AB_TRAV_ACTIONS_FOR_OBJ);
212         (action = trav_next(&trav)) != NULL; )
213     {
214         actionIsCrossModule = obj_is_cross_module(action);
215         toObj = obj_get_to(action);
216         if (toObj == NULL)
217         {
218             continue;
219         }
220
221         toModule = obj_get_module(toObj);
222
223         if (actionIsCrossModule)
224         {
225             sprintf(headerFilename, "\"%s\"", 
226                 abmfP_get_ui_header_file_name(toModule));
227             strlist_add_str(includeFiles,  headerFilename, NULL);
228         }
229
230         /*
231          * Check to see if the popup's window parent is in another module
232          */
233         compRoot = obj_get_root(toObj);
234         popup_win = NULL;
235         if (obj_is_popup_win(compRoot))
236         {
237             popup_win = compRoot;
238         }
239         if (popup_win == NULL) 
240         {
241             popup_win = obj_get_parent_of_type(compRoot, AB_TYPE_DIALOG);
242         }
243         if (popup_win == NULL)
244         {
245             popup_win = obj_get_parent_of_type(compRoot, AB_TYPE_BASE_WINDOW);
246         }
247
248         if (popup_win != NULL)
249         {
250             win_parent= obj_get_win_parent(popup_win);
251             if (win_parent != NULL)
252             {
253                toModule = obj_get_module(win_parent);
254                if (toModule != NULL)
255                {
256                    sprintf(headerFilename, "\"%s\"", 
257                         abmfP_get_ui_header_file_name(toModule));
258                    strlist_add_str(includeFiles, headerFilename, NULL);
259                }
260             }
261         }
262
263         /*
264          * For some actions, we need widget-specific convenience
265          * functions and resource strings that exist in the header files.
266          */
267         if (obj_get_func_type(action) == AB_FUNC_BUILTIN)
268         {
269             switch (obj_get_func_builtin(action))
270             {
271                 case AB_STDACT_SET_LABEL:
272                 case AB_STDACT_SET_TEXT:
273                 case AB_STDACT_SET_VALUE:
274                     abmfP_comp_get_widget_specific_includes(
275                         includeFiles, obj_get_root(toObj));
276                 break;
277             }
278         }
279     }
280     trav_close(&trav);
281
282     return 0;
283 }
284