Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSvc / DtUtil2 / DtUtil.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 /* $TOG: DtUtil.c /main/10 1998/07/30 12:13:08 mgreess $ */
24 /*
25  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
26  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
27  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
28  * (c) Copyright 1993, 1994 Novell, Inc.                                *
29  */
30 /******************************************************************************
31  *
32  * File Name: DtUtil.c
33  *
34  *  Contains the DT functions used by an application to connect to the
35  *  underlying communications mechanism.
36  *
37  *****************************************************************************/
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <netdb.h>
42 #include <X11/Xlib.h>
43 #include <X11/Intrinsic.h>
44 #include <X11/StringDefs.h>
45 #include <Xm/Xm.h>
46 #include <Dt/DtP.h>
47 #include <Dt/UserMsg.h>
48 #include <Dt/DtNlUtils.h>
49 #include <Dt/EnvControlP.h>
50 #include "DtSvcLock.h"
51
52 /*****************************************
53  *
54  * External functions not defined in header files.
55  *
56  *****************************************/
57
58 /********    End Public Function Declarations    ********/
59
60 /********    Static Function Declarations    ********/
61
62 static Boolean DtBigInitialize( 
63                         Display *display,
64                         Widget widget,
65                         char *name,
66                         char *toolClass,
67                         XtAppContext app_context) ;
68 static void InitButtonLabels( void ) ;
69 static void DtGlobalsInitialize( 
70                         Display *display,
71                         char *name,
72                         char *toolClass) ;
73
74 /********    End Static Function Declarations    ********/
75
76
77 /*****************************************
78  *
79  * Global variables 
80  *
81  *****************************************/
82
83 XrmDatabase _DtResourceDatabase = NULL; /* This Dt global indicates which
84                                            Xrm database should be read for
85                                            resources. */
86
87 XtAppContext _DtAppContext = NULL;      /* This Dt global keeps track of the
88                                            app-context, if one has been 
89                                            specified.  Note that libXv does
90                                            not yet support multiple app-
91                                            contexts. */
92 XtAppContext *_DtInitAppContextp = NULL;
93 Widget   _DtInitTtContextWidget = NULL;
94
95 Display *_DtDisplay = NULL;             /* This global variable is saved
96                                            when a client invokes "DtInitialize"
97                                            It is used later to get resources
98                                            when the DT databases are loaded.*/
99 char *_DtApplicationName = NULL;        /* This global variable is the
100                                            client's "ApplicationName". */
101 char *_DtApplicationClass = NULL;       /* This global variable is the
102                                            client's "ApplicationClass". */
103 char *_DtToolClass = NULL;              /* Tool class passed to _DtInit...() */
104
105
106 /* Localizable button labels */
107 const char * _DtOkString = NULL;
108 const char * _DtCancelString = NULL;
109 const char * _DtHelpString = NULL;
110 const char * _DtApplyString = NULL;
111 const char * _DtCloseString = NULL;
112
113
114 /*********************************************
115  *
116  * Initialization Functions
117  *
118  *********************************************/
119
120 Boolean 
121 DtAppInitialize(
122         XtAppContext app_context,
123         Display *display,
124         Widget widget,
125         char *name,
126         char *toolClass )
127 {
128     Boolean result;
129
130     _DtSvcAppLock(app_context);
131    result = DtBigInitialize (display, widget, name, toolClass, app_context);
132    _DtSvcAppUnlock(app_context);
133    return (result);
134 }
135
136 Boolean 
137 DtInitialize(
138         Display *display,
139         Widget widget,
140         char *name,
141         char *toolClass )
142 {
143     Boolean result;
144     _DtSvcDisplayToAppContext(display);
145
146     _DtSvcAppLock(app);
147    result = DtBigInitialize (display, widget, name, toolClass, NULL);
148    _DtSvcAppUnlock(app);
149    return (result);
150 }
151
152 static Boolean 
153 DtBigInitialize(
154         Display *display,
155         Widget widget,
156         char *name,
157         char *toolClass,
158         XtAppContext app_context )
159    
160 {
161    static Boolean initialized = False;
162
163    /* Initialization can only be performed once. */
164    _DtSvcProcessLock();
165    if (initialized) {
166       _DtSvcProcessUnlock();
167       return (False);
168    }
169
170    /* Preserve the pre-Dt environ and add Dt-specifics to environ */
171    (void) _DtEnvControl (DT_ENV_SET); 
172
173    /* Initialize a bunch of miscellaneous things. */
174    DtNlInitialize();
175    InitButtonLabels();
176    DtGlobalsInitialize (display, name, toolClass);
177
178    if ( XmIsGadget(widget) )
179         _DtInitTtContextWidget = XtParent(widget);
180    else
181         _DtInitTtContextWidget = widget;
182
183    if (app_context)
184        _DtAppContext =  app_context;
185    else
186        _DtAppContext =  XtWidgetToApplicationContext(_DtInitTtContextWidget);
187    _DtInitAppContextp = &_DtAppContext;
188
189    initialized = TRUE;
190    _DtSvcProcessUnlock();
191    return (initialized);
192 }
193
194
195 /* Initialize the global button labels */
196
197 static void 
198 InitButtonLabels( void )
199
200 {
201    _DtOkString = XtNewString(Dt11GETMESSAGE(28, 1, "OK"));
202    _DtCancelString = XtNewString(Dt11GETMESSAGE(28, 2, "Cancel"));
203    _DtHelpString = XtNewString(Dt11GETMESSAGE(28, 3, "Help"));
204    _DtApplyString = XtNewString(Dt11GETMESSAGE(28, 4, "Apply"));
205    _DtCloseString = XtNewString(Dt11GETMESSAGE(28, 5, "Close"));
206 }
207
208 static void 
209 DtGlobalsInitialize(
210         Display *display,
211         char *name,
212         char *toolClass )
213 {
214    _DtResourceDatabase = XtDatabase (display);
215    DtProgName = name;
216    _DtToolClass = XtNewString(toolClass);
217
218    XeToolClass = XtNewString (toolClass);
219
220    /*
221     * Save the application name and application class.
222     */
223    _DtDisplay = display;
224    XtGetApplicationNameAndClass (display,
225                                  &_DtApplicationName,
226                                  &_DtApplicationClass);
227 }