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