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 librararies 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: DialogManager.C /main/5 1996/04/21 19:32:05 drk $ */
27 * $XConsortium: DialogManager.C /main/5 1996/04/21 19:32:05 drk $
29 * RESTRICTED CONFIDENTIAL INFORMATION:
31 * The information in this document is subject to special
32 * restrictions in a confidential disclosure agreement bertween
33 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
34 * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
35 * Sun's specific written approval. This documment and all copies
36 * and derivative works thereof must be returned or destroyed at
39 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
44 ///////////////////////////////////////////////////////////////////////////////
45 //////////////////////////////////////////////////////////////////////////////
46 // This example code is from the book:
48 // Object-Oriented Programming with C++ and OSF/Motif
51 // Prentice Hall, 1992
54 // Copyright 1991 by Prentice Hall
55 // All Rights Reserved
57 // Permission to use, copy, modify, and distribute this software for
58 // any purpose except publication and without fee is hereby granted, provided
59 // that the above copyright notice appear in all copies of the software.
60 ///////////////////////////////////////////////////////////////////////////////
61 //////////////////////////////////////////////////////////////////////////////
64 //////////////////////////////////////////////////////////
65 // DialogManager.C: Support cached dialog widgets
66 //////////////////////////////////////////////////////////
67 #include "DialogManager.h"
68 #include "Application.h"
69 #include <Xm/MessageB.h>
77 DialogManager::DialogManager ( char *name ): UIComponent ( name )
82 Widget DialogManager::getDialog()
84 Widget newDialog = NULL;
86 // If the permanent widget exists and is not in use,
89 if ( _w && !XtIsManaged ( _w ) )
92 // Get a widget from the derived class
94 newDialog = createDialog ( theApplication->baseWidget() ) ;
96 // If this is a temporary dialog, install callbacks to
97 // destroy it when the user pops it down.
101 XtAddCallback ( newDialog,
103 &DialogManager::destroyTmpDialogCallback,
106 XtAddCallback ( newDialog,
108 &DialogManager::destroyTmpDialogCallback,
111 else // If this is the first dialog to be
112 _w = newDialog; // created, save it to be used again.
117 Widget DialogManager::getDialog(
121 Widget newDialog = NULL;
123 // If the permanent widget exists and is not in use,
126 if ( _w && !XtIsManaged ( _w ) )
129 // Get a widget from the derived class
130 // Parent the dialog to the widget passed in
132 newDialog = createDialog (w);
134 // If this is a temporary dialog, install callbacks to
135 // destroy it when the user pops it down.
139 XtAddCallback ( newDialog,
141 &DialogManager::destroyTmpDialogCallback,
144 XtAddCallback ( newDialog,
146 &DialogManager::destroyTmpDialogCallback,
149 else // If this is the first dialog to be
150 _w = newDialog; // created, save it to be used again.
155 void DialogManager::destroyTmpDialogCallback ( Widget w,
157 XtPointer clientData)
159 XtDestroyWidget ( w );
161 // We must set the wiget handle to NULL to prevent multiple
163 ((DialogManager *)clientData)->_w = NULL;
166 Widget DialogManager::post (char *title,
171 DialogCallback cancel,
174 // Get a dialog widget from the cache
176 Widget dialog = getDialog(wid);
178 // Make sure the dialog exists, and that it is an XmMessageBox
179 // or subclass, since the callbacks assume this widget type
181 assert ( dialog != NULL );
182 // assert ( XtIsSubclass ( dialog, xmMessageBoxWidgetClass ) );
184 // Convert the text string to a compound string and
185 // specify this to be the message displayed in the dialog.
187 XmString titleStr = XmStringCreateLocalized (title);
188 XmString xmstr = XmStringCreateLocalized ( text );
189 XtVaSetValues ( dialog,
190 XmNmessageString, xmstr,
191 XmNdialogTitle, titleStr,
193 XmStringFree ( xmstr );
194 XmStringFree ( titleStr );
196 // Create an object to carry the additional data needed
197 // to cache the dialogs.
199 DialogCallbackData *dcb = new DialogCallbackData( this,
203 // Install callback function for each button
204 // support by Motif dialogs. If there is no help callback
205 // unmanage the corresponding button instead, if possible.
208 XtAddCallback ( dialog,
210 &DialogManager::okCallback,
214 Widget w = XmMessageBoxGetChild ( dialog,
215 XmDIALOG_OK_BUTTON );
216 XtUnmanageChild ( w );
221 XtAddCallback ( dialog,
223 &DialogManager::cancelCallback,
227 Widget w = XmMessageBoxGetChild ( dialog,
228 XmDIALOG_CANCEL_BUTTON );
229 XtUnmanageChild ( w );
234 XtAddCallback ( dialog,
236 &DialogManager::helpCallback,
240 Widget w = XmMessageBoxGetChild ( dialog,
241 XmDIALOG_HELP_BUTTON );
242 XtUnmanageChild ( w );
247 XtManageChild ( dialog );
253 Widget DialogManager::post (char *title,
257 DialogCallback cancel,
260 // Get a dialog widget from the cache
262 Widget dialog = getDialog();
264 // Make sure the dialog exists, and that it is an XmMessageBox
265 // or subclass, since the callbacks assume this widget type
267 assert ( dialog != NULL );
268 // assert ( XtIsSubclass ( dialog, xmMessageBoxWidgetClass ) );
270 // Convert the text string to a compound string and
271 // specify this to be the message displayed in the dialog.
273 XmString titleStr = XmStringCreateLocalized (title);
274 XmString xmstr = XmStringCreateLocalized ( text );
275 XtVaSetValues ( dialog,
276 XmNmessageString, xmstr,
277 XmNdialogTitle, titleStr,
279 XmStringFree ( xmstr );
280 XmStringFree ( titleStr );
282 // Create an object to carry the additional data needed
283 // to cache the dialogs.
285 DialogCallbackData *dcb = new DialogCallbackData( this,
289 // Install callback function for each button
290 // support by Motif dialogs. If there is no help callback
291 // unmanage the corresponding button instead, if possible.
294 XtAddCallback ( dialog,
296 &DialogManager::okCallback,
300 Widget w = XmMessageBoxGetChild ( dialog,
301 XmDIALOG_OK_BUTTON );
302 XtUnmanageChild ( w );
307 XtAddCallback ( dialog,
309 &DialogManager::cancelCallback,
313 Widget w = XmMessageBoxGetChild ( dialog,
314 XmDIALOG_CANCEL_BUTTON );
315 XtUnmanageChild ( w );
320 XtAddCallback ( dialog,
322 &DialogManager::helpCallback,
326 Widget w = XmMessageBoxGetChild ( dialog,
327 XmDIALOG_HELP_BUTTON );
328 XtUnmanageChild ( w );
333 XtManageChild ( dialog );
339 void DialogManager::okCallback ( Widget w,
340 XtPointer clientData,
343 DialogCallbackData *dcd = (DialogCallbackData *) clientData;
344 DialogManager *obj = (DialogManager *) dcd->dialogManager();
345 DialogCallback callback;
347 // If caller specified an ok callback, call the function
349 if ( ( callback = dcd->ok() ) != NULL )
350 ( *callback )( dcd->clientData() );
352 // Reset for the next time
354 obj->cleanup ( w, dcd );
357 void DialogManager::cancelCallback ( Widget w,
358 XtPointer clientData,
361 DialogCallbackData *dcd = (DialogCallbackData *) clientData;
362 DialogManager *obj = (DialogManager *) dcd->dialogManager();
363 DialogCallback callback;
365 if ( ( callback = dcd->cancel() ) != NULL )
366 ( *callback )( dcd->clientData() );
368 obj->cleanup ( w, dcd );
371 void DialogManager::helpCallback ( Widget w,
372 XtPointer clientData,
375 DialogCallbackData *dcd = (DialogCallbackData *) clientData;
376 DialogManager *obj = (DialogManager *) dcd->dialogManager();
377 DialogCallback callback;
379 if ( ( callback = dcd->help() ) != NULL )
380 ( *callback )( dcd->clientData() );
382 obj->cleanup ( w, dcd );
385 void DialogManager::cleanup ( Widget w, DialogCallbackData *dcd )
387 // Remove all callbacks to avoid having duplicate
388 // callback functions installed.
390 XtRemoveCallback ( w,
392 &DialogManager::okCallback,
395 XtRemoveCallback ( w,
397 &DialogManager::cancelCallback,
400 XtRemoveCallback ( w,
402 &DialogManager::helpCallback,
405 // Delete the DialogCallbackData instance for this posting
411 DialogManager::forceUpdate( Widget w )
413 Widget diashell, topshell;
414 Window diawindow, topwindow;
417 XWindowAttributes xwa;
421 XtAppContext cxt=XtWidgetToApplicationContext( w );
422 for (diashell=w;!XtIsShell(diashell);diashell=XtParent(diashell));
423 for ( topshell=diashell;XtIsTopLevelShell( topshell );
424 topshell = XtParent( topshell ) );
426 // if (XtIsRealized(diashell) && XtIsRealized(topshell)){
427 dpy=XtDisplay(diashell);
428 diawindow=XtWindow(diashell);
429 topwindow=XtWindow(topshell);
430 while ( XGetWindowAttributes(dpy,diawindow,&xwa) &&
431 xwa.map_state != IsViewable && XEventsQueued(dpy,QueuedAlready)){
432 // if ( XGetWindowAttributes( dpy, topwindow, &xwa ) &&
433 // xwa.map_state != IsViewable )
435 XtAppNextEvent( cxt, &event );
436 XtDispatchEvent( &event );
439 XmUpdateDisplay(topshell);
444 // Added this extra functionality
447 my_okCallback( int *data )
453 my_cancelCallback( int *data )
459 DialogManager::post_and_return(
466 XmString okLabel, cancelLabel;
468 // They may have been set via the overloaded post_and_return()
469 // method before. Reset them to their default values...
471 okLabel = XmStringCreateLocalized(GETMSG(catd, 1, 2, "OK"));
472 cancelLabel = XmStringCreateLocalized(GETMSG(catd, 1, 3, "Cancel"));
474 Widget dlg = this->getDialog(wid);
476 // Make sure the dialog exists, and that it is an XmMessageBox
477 // or subclass, since the callbacks assume this widget type
479 assert ( dlg != NULL );
482 XmNokLabelString, okLabel,
483 XmNcancelLabelString, cancelLabel,
487 this->post(title_str,
491 ( DialogCallback ) &my_okCallback,
492 ( DialogCallback ) &my_cancelCallback);
494 forceUpdate( dialog );
496 XtAppProcessEvent(XtWidgetToApplicationContext(dialog), XtIMAll );
498 // Process just one more event to pop down dialog.
499 XtAppProcessEvent(XtWidgetToApplicationContext(dialog), XtIMAll );
506 DialogManager::post_and_return(
516 okLabel = XmStringCreateLocalized(okLabelString);
518 Widget dlg = this->getDialog(wid);
520 // Make sure the dialog exists, and that it is an XmMessageBox
521 // or subclass, since the callbacks assume this widget type
523 assert ( dlg != NULL );
526 XmNokLabelString, okLabel,
529 Widget dialog = this->post(title_str,
533 ( DialogCallback ) &my_okCallback);
535 forceUpdate( dialog );
537 XtAppProcessEvent(XtWidgetToApplicationContext(dialog), XtIMAll );
539 // Process just one more event to pop down dialog.
540 XtAppProcessEvent(XtWidgetToApplicationContext(dialog), XtIMAll );
547 DialogManager::post_and_return(
551 char *cancelLabelString,
557 XmString okLabel, cancelLabel;
559 okLabel = XmStringCreateLocalized(okLabelString);
560 cancelLabel = XmStringCreateLocalized(cancelLabelString);
562 Widget dlg = this->getDialog(wid);
564 // Make sure the dialog exists, and that it is an XmMessageBox
565 // or subclass, since the callbacks assume this widget type
567 assert ( dlg != NULL );
570 XmNokLabelString, okLabel,
571 XmNcancelLabelString, cancelLabel,
574 Widget dialog = this->post(title_str,
578 ( DialogCallback ) &my_okCallback,
579 ( DialogCallback ) &my_cancelCallback);
581 forceUpdate( dialog );
583 XtAppProcessEvent(XtWidgetToApplicationContext(dialog), XtIMAll );
585 // Process just one more event to pop down dialog.
586 XtAppProcessEvent(XtWidgetToApplicationContext(dialog), XtIMAll );