2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $XConsortium: SharedProcs.c /main/7 1996/08/28 17:19:21 drk $ */
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. *
30 /*************************************<+>*************************************
31 *****************************************************************************
33 ** File: SharedProcs.c
37 ** Description: Contains the set of functions which are of general
38 ** use to all DT clients.
41 ****************************************************************************
42 ************************************<+>*************************************/
44 #include <sys/types.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>
66 #include <Dt/Connect.h>
67 #include <Dt/DtNlUtils.h>
69 #include <DtXinerama.h>
71 #include "SharedProcs.h"
75 #define RW_ALL S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
78 /******** Static Function Declarations ********/
79 static void MessageDialogPopupCB(Widget w, XtPointer client_data,
82 /******** End Static Function Declarations ********/
85 /************************************************************************
89 * Strip all leading and trailing spaces.
91 ************************************************************************/
105 /* Strip off leading spaces first */
109 (!is_multibyte || (mblen(string + i, MB_CUR_MAX) == 1)) &&
111 isspace((u_char)string[i]))
116 /* Copy over the leading spaces */
117 strcpy(string, string + i);
119 /* Drop out, if the string is now empty */
120 if ((i = strlen(string) - 1) < 0)
123 /* Strip off trailing spaces */
128 /* No multibyte; simply work back through the string */
129 while ((i >= 0) && (isspace((u_char)string[i])))
131 string[i + 1] = '\0';
136 /* Work forward, looking for a trailing space of spaces */
144 if (((len = mblen(string + i, MB_CUR_MAX)) == 1) && isspace((u_char)string[i]))
153 /* if there is an invalid character, treat as a valid one-byte */
169 /************************************************************************
172 * Create and display an error message.
174 ************************************************************************/
182 void (*helpCallback)() )
185 _DtMessageDialog(w, title, message_text, helpIdStr, False,
186 NULL, _DtMessageOK, _DtMessageClose, helpCallback, True,
191 /************************************************************************
193 * Generic warning/error dialog creation function.
195 ************************************************************************/
204 void (*cancel_callback)(),
205 void (*ok_callback)(),
206 void (*close_callback)(),
207 void (*help_callback)(),
208 Boolean deleteOnClose,
214 XmString message_string;
215 XWindowAttributes attributes;
217 XmString okString, cancelString, helpString;
219 okString = XmStringCreateLocalized((Dt11GETMESSAGE(28,1, "OK")));
220 cancelString = XmStringCreateLocalized((Dt11GETMESSAGE(28,2, "Cancel")));
221 helpString = XmStringCreateLocalized((Dt11GETMESSAGE(28,3, "Help")));
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);
229 /* Search up to get the topmost shell */
231 while (XtParent (w) != NULL && !(XtIsSubclass (w, shellWidgetClass)))
237 message = XmCreateErrorDialog(w, title, args, 5);
241 message = XmCreateWarningDialog(w, title, args, 5);
244 case QUESTION_DIALOG:
245 message = XmCreateQuestionDialog(w, title, args, 5);
248 if (XtWindow (w) != 0)
249 XGetWindowAttributes(XtDisplay (w), XtWindow (w), &attributes);
251 attributes.map_state = IsUnmapped;
254 * If parent widget isn't mapped, attach a callback
255 * procedure that'll center the message dialog on screen.
257 if (attributes.map_state == IsUnmapped)
258 XtAddCallback(XtParent(message),XmNpopupCallback,
259 MessageDialogPopupCB,(XtPointer)w);
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);
267 widget = XmMessageBoxGetChild(message, XmDIALOG_CANCEL_BUTTON);
269 XtUnmanageChild(widget);
270 else if (cancel_callback)
272 XtAddCallback(widget, XmNactivateCallback, cancel_callback,
274 XtSetArg(args[0], XmNcancelButton, widget);
275 XtSetValues(message, args, 1);
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);
284 widget = XmMessageBoxGetChild(message, XmDIALOG_HELP_BUTTON);
285 if (helpIdStr != NULL)
289 XtAddCallback(widget, XmNactivateCallback, help_callback,
290 (caddr_t) helpIdStr);
292 XtSetValues(widget, args, 2);
295 XtUnmanageChild(widget);
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);
303 XtManageChild(message);
307 if (close_callback == NULL)
308 close_callback = _DtMessageClose;
310 XtAddEventHandler(XtParent (message), StructureNotifyMask, True,
311 (XtEventHandler)close_callback, message);
314 XmStringFree(okString);
315 XmStringFree(cancelString);
316 XmStringFree(helpString);
324 /************************************************************************
327 * Close the error message box.
329 ************************************************************************/
335 XtPointer client_data,
336 XtPointer call_data )
339 XtUnmanageChild(client_data);
345 /************************************************************************
348 * Close the error message box.
350 ************************************************************************/
355 XtPointer client_data,
359 if (event->type == UnmapNotify)
361 XtRemoveEventHandler(XtParent(client_data), StructureNotifyMask,
362 True, (XtEventHandler)_DtMessageClose, client_data);
364 XtUnmanageChild(client_data);
370 * Center a message dialog on screen once it is managed.
371 * client_data is expected to contain the parent shell widget handle.
373 static void MessageDialogPopupCB(Widget w, XtPointer client_data,
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;
381 DtXineramaInfo_t *dt_xi;
387 scr_w=WidthOfScreen(XtScreen(w));
388 scr_h=HeightOfScreen(XtScreen(w));
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)))){
395 unsigned int pw_x=XtX((Widget)client_data);
396 unsigned int pw_y=XtY((Widget)client_data);
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)){
408 #endif /* USE_XINERAMA */
410 msg_x=off_x+(scr_w-msg_w)/2;
411 msg_y=off_y+(scr_h-msg_h)/2;
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);