Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSvc / DtUtil2 / SharedProcs.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 /* $XConsortium: SharedProcs.c /main/7 1996/08/28 17:19:21 drk $ */
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  **
33  **   File:        SharedProcs.c
34  **
35  **   Project:     DT
36  **
37  **   Description: Contains the set of functions which are of general
38  **                use to all DT clients.
39  **               
40  **
41  ****************************************************************************
42  ************************************<+>*************************************/
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <limits.h>
49 #include <stdio.h>
50 #include <time.h>
51
52 #include <Xm/Xm.h>
53 #include <Xm/XmP.h>
54 #include <Xm/VendorSEP.h>
55 #include <Xm/MessageB.h>
56 #include <Xm/RowColumn.h>
57 #include <Xm/MwmUtil.h>
58 #include <Xm/Protocols.h>
59 #include <X11/ShellP.h>
60 #include <X11/Shell.h>
61 #include <X11/Xatom.h>
62
63
64 #include <Dt/DtP.h>
65 #include <Dt/Connect.h>
66 #include <Dt/DtNlUtils.h>
67
68 #include "SharedProcs.h"
69
70
71 /* Defines */
72 #define RW_ALL S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
73
74
75 /********    Static Function Declarations    ********/
76
77
78 /********    End Static Function Declarations    ********/
79
80
81 /************************************************************************
82  *
83  *  _DtStripSpaces
84  *
85  *     Strip all leading and trailing spaces.
86  *
87  ************************************************************************/
88
89 String
90 _DtStripSpaces(
91         String string )
92
93 {
94    int i;
95    String space;
96
97    if (string == NULL)
98       return (string);
99
100
101    /* Strip off leading spaces first */
102    i = 0;
103    while (
104 #ifdef NLS16
105           (!is_multibyte || (mblen(string + i, MB_CUR_MAX) == 1)) &&
106 #endif
107           isspace((u_char)string[i]))
108    {
109       i++;
110    }
111
112    /* Copy over the leading spaces */
113    strcpy(string, string + i);
114
115    /* Drop out, if the string is now empty */
116    if ((i = strlen(string) - 1) < 0)
117       return(string);
118
119    /* Strip off trailing spaces */
120 #ifdef NLS16
121    if (!is_multibyte)
122    {
123 #endif
124       /* No multibyte; simply work back through the string */
125       while ((i >= 0) && (isspace((u_char)string[i])))
126          i--;
127       string[i + 1] = '\0';
128 #ifdef NLS16
129    }
130    else
131    {
132       /* Work forward, looking for a trailing space of spaces */
133       int len;
134
135       i = 0;
136       space = NULL;
137
138       while (string[i])
139       {
140          if (((len = mblen(string + i, MB_CUR_MAX)) == 1) && isspace((u_char)string[i]))
141          {
142             /* Found a space */
143             if (space == NULL)
144                space = string + i;
145          }
146          else if (space)
147             space = NULL;
148
149          /* if there is an invalid character, treat as a valid one-byte */
150          if (len == -1)
151            len = 1;
152          
153          i += len;
154   
155       }
156
157       if (space)
158          *space = '\0';
159    }
160 #endif
161
162    return (string);
163 }
164
165 /************************************************************************
166  *
167  *  _DtMessage
168  *      Create and display an error message.
169  *
170  ************************************************************************/
171
172 void
173 _DtMessage(
174         Widget w,
175         char *title,
176         char *message_text,
177         XtPointer helpIdStr,
178         void (*helpCallback)() )
179
180 {
181    _DtMessageDialog(w, title, message_text, helpIdStr, False,
182                  NULL, _DtMessageOK, _DtMessageClose, helpCallback, True, 
183                  ERROR_DIALOG);
184 }
185
186
187 /************************************************************************
188  *
189  * Generic warning/error dialog creation function.
190  *
191  ************************************************************************/
192
193 Widget
194 _DtMessageDialog(
195         Widget w,
196         char *title,
197         char *message_text,
198         XtPointer helpIdStr,
199         Boolean cancel_btn,
200         void (*cancel_callback)(),
201         void (*ok_callback)(),
202         void (*close_callback)(),
203         void (*help_callback)(),
204         Boolean deleteOnClose,
205         int dialogType )
206
207 {
208    Widget message;
209    Widget widget;
210    XmString message_string;
211    XWindowAttributes attributes;
212    Arg args[10];
213    XmString okString, cancelString, helpString;
214
215    okString = XmStringCreateLocalized((Dt11GETMESSAGE(28,1, "OK")));
216    cancelString = XmStringCreateLocalized((Dt11GETMESSAGE(28,2, "Cancel")));
217    helpString = XmStringCreateLocalized((Dt11GETMESSAGE(28,3, "Help")));
218
219    XtSetArg (args[0], XmNautoUnmanage, False);
220    XtSetArg (args[1], XmNcancelLabelString, cancelString);
221    XtSetArg (args[2], XmNokLabelString, okString);
222    XtSetArg (args[3], XmNhelpLabelString, helpString);
223    XtSetArg (args[4], XmNuseAsyncGeometry, True);
224
225    /*  Search up to get the topmost shell  */
226
227    while (XtParent (w) != NULL && !(XtIsSubclass (w, shellWidgetClass)))
228       w = XtParent (w);
229
230    switch (dialogType)
231    {  
232       case ERROR_DIALOG:
233         message = XmCreateErrorDialog(w, title, args, 5);
234       break;
235
236       case WARNING_DIALOG:
237         message = XmCreateWarningDialog(w, title, args, 5);
238       break;
239
240       case QUESTION_DIALOG:
241         message = XmCreateQuestionDialog(w, title, args, 5);
242       break;
243     }
244    if (XtWindow (w) != 0)
245       XGetWindowAttributes(XtDisplay (w), XtWindow (w), &attributes);
246    else
247       attributes.map_state = IsUnmapped;
248
249    if (attributes.map_state == IsUnmapped)
250    {
251       XtSetArg(args[0], XmNx, (WidthOfScreen(XtScreen (w)) - 350) / 2);
252       XtSetArg(args[1], XmNy, (HeightOfScreen(XtScreen (w)) - 200) / 2);
253       XtSetArg(args[2], XmNdefaultPosition, False);
254       XtSetValues(message, args, 3);
255    }
256
257
258    /*  Adjust the decorations and title for the dialog shell of the dialog  */
259
260    XtSetArg(args[0], XmNtitle, title);
261    XtSetArg(args[1], XmNmwmFunctions, MWM_FUNC_MOVE);
262    XtSetArg(args[2], XmNmwmDecorations, MWM_DECOR_BORDER | MWM_DECOR_TITLE);
263    XtSetValues(XtParent (message), args, 3);
264
265    widget = XmMessageBoxGetChild(message, XmDIALOG_CANCEL_BUTTON);
266    if (!cancel_btn)
267       XtUnmanageChild(widget);
268    else if (cancel_callback)
269    {
270       XtAddCallback(widget, XmNactivateCallback, cancel_callback, 
271                      (caddr_t) message);
272       XtSetArg(args[0], XmNcancelButton, widget);
273       XtSetValues(message, args, 1);
274    }
275
276    widget = XmMessageBoxGetChild(message, XmDIALOG_OK_BUTTON);
277    XtSetArg(args[0], XmNmarginWidth, 10);
278    XtSetArg(args[1], XmNmarginHeight, 2);
279    XtSetValues(widget, args, 2);
280    XtAddCallback(widget, XmNactivateCallback, ok_callback, (caddr_t) message);
281   
282    widget = XmMessageBoxGetChild(message, XmDIALOG_HELP_BUTTON);
283    if (helpIdStr != NULL)
284    {
285       if (help_callback)
286       {
287          XtAddCallback(widget, XmNactivateCallback, help_callback, 
288                        (caddr_t) helpIdStr);
289       }
290       XtSetValues(widget, args, 2);
291    }
292    else
293       XtUnmanageChild(widget);
294
295    widget = XmMessageBoxGetChild(message, XmDIALOG_MESSAGE_LABEL);
296    message_string = XmStringCreateLocalized(message_text);
297    XtSetArg(args[0], XmNlabelString, message_string);
298    XtSetValues(widget, args, 1);
299    XmStringFree(message_string);
300
301    XtManageChild(message);
302
303    if (deleteOnClose)
304    {
305       if (close_callback == NULL)
306          close_callback = _DtMessageClose;
307
308       XtAddEventHandler(XtParent (message), StructureNotifyMask, True,
309                          (XtEventHandler)close_callback, message);
310    }
311
312    XmStringFree(okString);
313    XmStringFree(cancelString);
314    XmStringFree(helpString);
315
316    return(message);
317 }
318
319
320
321
322 /************************************************************************
323  *
324  *  _DtMessageOK
325  *      Close the error message box.
326  *
327  ************************************************************************/ 
328
329 /* ARGSUSED */
330 void
331 _DtMessageOK(
332         Widget w,
333         XtPointer client_data,
334         XtPointer call_data )
335
336 {
337    XtUnmanageChild(client_data);
338 }
339
340
341
342
343 /************************************************************************
344  *
345  *  _DtMessageClose
346  *      Close the error message box.
347  *
348  ************************************************************************/ 
349
350 void
351 _DtMessageClose(
352         Widget w,
353         XtPointer client_data,
354         XEvent *event )
355
356 {
357    if (event->type == UnmapNotify)
358    {
359       XtRemoveEventHandler(XtParent(client_data), StructureNotifyMask, 
360                             True, (XtEventHandler)_DtMessageClose, client_data);
361
362       XtUnmanageChild(client_data);
363       XtDestroyWidget(w);
364    }
365 }
366
367