Convert uses of XKeycodeToKeysym (deprecated) to XkbKeycodeToKeysym
[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 libraries 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 <ctype.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <limits.h>
50 #include <stdio.h>
51 #include <time.h>
52
53 #include <Xm/Xm.h>
54 #include <Xm/XmP.h>
55 #include <Xm/VendorSEP.h>
56 #include <Xm/MessageB.h>
57 #include <Xm/RowColumn.h>
58 #include <Xm/MwmUtil.h>
59 #include <Xm/Protocols.h>
60 #include <X11/ShellP.h>
61 #include <X11/Shell.h>
62 #include <X11/Xatom.h>
63
64
65 #include <Dt/DtP.h>
66 #include <Dt/Connect.h>
67 #include <Dt/DtNlUtils.h>
68 #ifdef USE_XINERAMA
69 #include <DtXinerama.h>
70 #endif
71 #include "SharedProcs.h"
72
73
74 /* Defines */
75 #define RW_ALL S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
76
77
78 /********    Static Function Declarations    ********/
79 static void MessageDialogPopupCB(Widget w, XtPointer client_data,
80         XtPointer call_data);
81
82 /********    End Static Function Declarations    ********/
83
84
85 /************************************************************************
86  *
87  *  _DtStripSpaces
88  *
89  *     Strip all leading and trailing spaces.
90  *
91  ************************************************************************/
92
93 String
94 _DtStripSpaces(
95         String string )
96
97 {
98    int i;
99    String space;
100
101    if (string == NULL)
102       return (string);
103
104
105    /* Strip off leading spaces first */
106    i = 0;
107    while (
108 #ifdef NLS16
109           (!is_multibyte || (mblen(string + i, MB_CUR_MAX) == 1)) &&
110 #endif
111           isspace((u_char)string[i]))
112    {
113       i++;
114    }
115
116    /* Copy over the leading spaces */
117    strcpy(string, string + i);
118
119    /* Drop out, if the string is now empty */
120    if ((i = strlen(string) - 1) < 0)
121       return(string);
122
123    /* Strip off trailing spaces */
124 #ifdef NLS16
125    if (!is_multibyte)
126    {
127 #endif
128       /* No multibyte; simply work back through the string */
129       while ((i >= 0) && (isspace((u_char)string[i])))
130          i--;
131       string[i + 1] = '\0';
132 #ifdef NLS16
133    }
134    else
135    {
136       /* Work forward, looking for a trailing space of spaces */
137       int len;
138
139       i = 0;
140       space = NULL;
141
142       while (string[i])
143       {
144          if (((len = mblen(string + i, MB_CUR_MAX)) == 1) && isspace((u_char)string[i]))
145          {
146             /* Found a space */
147             if (space == NULL)
148                space = string + i;
149          }
150          else if (space)
151             space = NULL;
152
153          /* if there is an invalid character, treat as a valid one-byte */
154          if (len == -1)
155            len = 1;
156          
157          i += len;
158   
159       }
160
161       if (space)
162          *space = '\0';
163    }
164 #endif
165
166    return (string);
167 }
168
169 /************************************************************************
170  *
171  *  _DtMessage
172  *      Create and display an error message.
173  *
174  ************************************************************************/
175
176 void
177 _DtMessage(
178         Widget w,
179         char *title,
180         char *message_text,
181         XtPointer helpIdStr,
182         void (*helpCallback)() )
183
184 {
185    _DtMessageDialog(w, title, message_text, helpIdStr, False,
186                  NULL, _DtMessageOK, _DtMessageClose, helpCallback, True, 
187                  ERROR_DIALOG);
188 }
189
190
191 /************************************************************************
192  *
193  * Generic warning/error dialog creation function.
194  *
195  ************************************************************************/
196
197 Widget
198 _DtMessageDialog(
199         Widget w,
200         char *title,
201         char *message_text,
202         XtPointer helpIdStr,
203         Boolean cancel_btn,
204         void (*cancel_callback)(),
205         void (*ok_callback)(),
206         void (*close_callback)(),
207         void (*help_callback)(),
208         Boolean deleteOnClose,
209         int dialogType )
210
211 {
212    Widget message;
213    Widget widget;
214    XmString message_string;
215    XWindowAttributes attributes;
216    Arg args[10];
217    XmString okString, cancelString, helpString;
218
219    okString = XmStringCreateLocalized((Dt11GETMESSAGE(28,1, "OK")));
220    cancelString = XmStringCreateLocalized((Dt11GETMESSAGE(28,2, "Cancel")));
221    helpString = XmStringCreateLocalized((Dt11GETMESSAGE(28,3, "Help")));
222
223    XtSetArg (args[0], XmNautoUnmanage, False);
224    XtSetArg (args[1], XmNcancelLabelString, cancelString);
225    XtSetArg (args[2], XmNokLabelString, okString);
226    XtSetArg (args[3], XmNhelpLabelString, helpString);
227    XtSetArg (args[4], XmNuseAsyncGeometry, True);
228
229    /*  Search up to get the topmost shell  */
230
231    while (XtParent (w) != NULL && !(XtIsSubclass (w, shellWidgetClass)))
232       w = XtParent (w);
233
234    switch (dialogType)
235    {  
236       case ERROR_DIALOG:
237         message = XmCreateErrorDialog(w, title, args, 5);
238       break;
239
240       case WARNING_DIALOG:
241         message = XmCreateWarningDialog(w, title, args, 5);
242       break;
243
244       case QUESTION_DIALOG:
245         message = XmCreateQuestionDialog(w, title, args, 5);
246       break;
247     }
248    if (XtWindow (w) != 0)
249       XGetWindowAttributes(XtDisplay (w), XtWindow (w), &attributes);
250    else
251       attributes.map_state = IsUnmapped;
252
253    /*
254     * If parent widget isn't mapped, attach a callback
255     * procedure that'll center the message dialog on screen.
256         */
257    if (attributes.map_state == IsUnmapped)
258         XtAddCallback(XtParent(message),XmNpopupCallback,
259                         MessageDialogPopupCB,(XtPointer)w);
260
261    /*  Adjust the decorations and title for the dialog shell of the dialog  */
262    XtSetArg(args[0], XmNtitle, title);
263    XtSetArg(args[1], XmNmwmFunctions, MWM_FUNC_MOVE);
264    XtSetArg(args[2], XmNmwmDecorations, MWM_DECOR_BORDER | MWM_DECOR_TITLE);
265    XtSetValues(XtParent (message), args, 3);
266
267    widget = XmMessageBoxGetChild(message, XmDIALOG_CANCEL_BUTTON);
268    if (!cancel_btn)
269       XtUnmanageChild(widget);
270    else if (cancel_callback)
271    {
272       XtAddCallback(widget, XmNactivateCallback, cancel_callback, 
273                      (caddr_t) message);
274       XtSetArg(args[0], XmNcancelButton, widget);
275       XtSetValues(message, args, 1);
276    }
277
278    widget = XmMessageBoxGetChild(message, XmDIALOG_OK_BUTTON);
279    XtSetArg(args[0], XmNmarginWidth, 10);
280    XtSetArg(args[1], XmNmarginHeight, 2);
281    XtSetValues(widget, args, 2);
282    XtAddCallback(widget, XmNactivateCallback, ok_callback, (caddr_t) message);
283   
284    widget = XmMessageBoxGetChild(message, XmDIALOG_HELP_BUTTON);
285    if (helpIdStr != NULL)
286    {
287       if (help_callback)
288       {
289          XtAddCallback(widget, XmNactivateCallback, help_callback, 
290                        (caddr_t) helpIdStr);
291       }
292       XtSetValues(widget, args, 2);
293    }
294    else
295       XtUnmanageChild(widget);
296
297    widget = XmMessageBoxGetChild(message, XmDIALOG_MESSAGE_LABEL);
298    message_string = XmStringCreateLocalized(message_text);
299    XtSetArg(args[0], XmNlabelString, message_string);
300    XtSetValues(widget, args, 1);
301    XmStringFree(message_string);
302
303    XtManageChild(message);
304
305    if (deleteOnClose)
306    {
307       if (close_callback == NULL)
308          close_callback = _DtMessageClose;
309
310       XtAddEventHandler(XtParent (message), StructureNotifyMask, True,
311                          (XtEventHandler)close_callback, message);
312    }
313
314    XmStringFree(okString);
315    XmStringFree(cancelString);
316    XmStringFree(helpString);
317
318    return(message);
319 }
320
321
322
323
324 /************************************************************************
325  *
326  *  _DtMessageOK
327  *      Close the error message box.
328  *
329  ************************************************************************/ 
330
331 /* ARGSUSED */
332 void
333 _DtMessageOK(
334         Widget w,
335         XtPointer client_data,
336         XtPointer call_data )
337
338 {
339    XtUnmanageChild(client_data);
340 }
341
342
343
344
345 /************************************************************************
346  *
347  *  _DtMessageClose
348  *      Close the error message box.
349  *
350  ************************************************************************/ 
351
352 void
353 _DtMessageClose(
354         Widget w,
355         XtPointer client_data,
356         XEvent *event )
357
358 {
359    if (event->type == UnmapNotify)
360    {
361       XtRemoveEventHandler(XtParent(client_data), StructureNotifyMask, 
362                             True, (XtEventHandler)_DtMessageClose, client_data);
363
364       XtUnmanageChild(client_data);
365       XtDestroyWidget(w);
366    }
367 }
368
369 /*
370  * Center a message dialog on screen once it is managed.
371  * client_data is expected to contain the parent shell widget handle.
372  */
373 static void MessageDialogPopupCB(Widget w, XtPointer client_data,
374         XtPointer call_data)
375 {
376         Position msg_x, msg_y;
377         unsigned int scr_w, scr_h, off_x=0, off_y=0;
378         Dimension msg_w=0, msg_h=0;
379         Arg args[2];
380         #ifdef USE_XINERAMA
381         DtXineramaInfo_t *dt_xi;
382         #endif
383
384         msg_w=XtWidth(w);
385         msg_h=XtHeight(w);
386
387         scr_w=WidthOfScreen(XtScreen(w));
388         scr_h=HeightOfScreen(XtScreen(w));
389
390         #ifdef USE_XINERAMA
391         /* determine xinerama screen number the parent shell resides on,
392          * and override scr_w/scr_h and off_x/off_y on success */
393         if((dt_xi=_DtXineramaInit(XtDisplay(w)))){
394                 int i;
395                 unsigned int pw_x=XtX((Widget)client_data);
396                 unsigned int pw_y=XtY((Widget)client_data);
397
398                 for(i=0; i<dt_xi->numscreens; i++){
399                         unsigned int sw,sh,sx,sy;
400                         _DtXineramaGetScreen(dt_xi,i,&sw,&sh,&sx,&sy);
401                         if(pw_x>=sx && pw_x<(sx+sw) && pw_y>=sy && pw_y<(sy+sh)){
402                                 off_x=sx; off_y=sy;
403                                 scr_w=sw; scr_h=sh;
404                                 break;
405                         }
406                 }
407         }
408         #endif /* USE_XINERAMA */
409
410         msg_x=off_x+(scr_w-msg_w)/2;
411         msg_y=off_y+(scr_h-msg_h)/2;
412
413         XtSetArg(args[0],XmNx,msg_x);
414         XtSetArg(args[1],XmNy,msg_y);
415         XtSetValues(w,args,2);
416         XtRemoveCallback(w,XmNpopupCallback,MessageDialogPopupCB,client_data);
417 }