Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtmail / MotifApp / PromptDialogManager.C
1 /* $XConsortium: PromptDialogManager.C /main/4 1996/04/21 19:40:30 drk $ */
2 /*
3  *+SNOTICE
4  *
5  *      $XConsortium: PromptDialogManager.C /main/4 1996/04/21 19:40:30 drk $
6  *
7  *      RESTRICTED CONFIDENTIAL INFORMATION:
8  *      
9  *      The information in this document is subject to special
10  *      restrictions in a confidential disclosure agreement bertween
11  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
12  *      document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
13  *      Sun's specific written approval.  This documment and all copies
14  *      and derivative works thereof must be returned or destroyed at
15  *      Sun's request.
16  *
17  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
18  *
19  *+ENOTICE
20  */
21
22 ///////////////////////////////////////////////////////////////////////////////
23 //////////////////////////////////////////////////////////////////////////////
24 //         This example code is from the book:
25 //
26 //           Object-Oriented Programming with C++ and OSF/Motif
27 //         by
28 //           Douglas Young
29 //           Prentice Hall, 1992
30 //           ISBN 0-13-630252-1 
31 //
32 //         Copyright 1991 by Prentice Hall
33 //         All Rights Reserved
34 //
35 //  Permission to use, copy, modify, and distribute this software for 
36 //  any purpose except publication and without fee is hereby granted, provided 
37 //  that the above copyright notice appear in all copies of the software.
38 ///////////////////////////////////////////////////////////////////////////////
39 //////////////////////////////////////////////////////////////////////////////
40
41
42 ///////////////////////////////////////////////////////////
43 // PromptDialogManager.C: 
44 //////////////////////////////////////////////////////////
45 #include "PromptDialogManager.h"
46 #include <Xm/Xm.h>
47 #include <Xm/SelectioB.h>
48 #include <assert.h>
49 // Define an instance to be available throughout the framework.
50
51 PromptDialogManager *thePromptDialogManager = 
52     new PromptDialogManager ( "PromptDialog" );
53
54 PromptDialogManager::PromptDialogManager ( char   *name ) : 
55                                  DialogManager ( name )
56 {
57     // Empty
58 }
59
60 Widget PromptDialogManager::createDialog ( Widget parent )
61 {
62   Widget dialog = XmCreatePromptDialog ( parent, _name, NULL, 0);
63     
64   XtVaSetValues ( dialog,
65                   XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL,
66                   NULL );
67   
68   return dialog;
69 }
70
71
72 Widget
73 PromptDialogManager::post( char *title,
74                            char         *text,
75                            void         *clientData,
76                            DialogCallback ok,
77                            DialogCallback cancel,
78                             DialogCallback help )
79 {
80
81     // Get a dialog widget from the cache
82     
83     Widget dialog = getDialog();
84     
85     // Make sure the dialog exists, and that it is an XmMessageBox
86     // or subclass, since the callbacks assume this widget type
87     
88     assert ( dialog != NULL );
89     assert ( XtIsSubclass ( dialog, xmSelectionBoxWidgetClass ) );
90         
91         // Convert the text string to a compound string and 
92         // specify this to be the message displayed in the dialog.
93
94     XmString titleStr = XmStringCreateLocalized (title);
95     XmString xmstr = XmStringCreateLocalized ( text );
96     XtVaSetValues ( dialog, 
97                     XmNmessageString, xmstr,
98                     XmNdialogTitle, titleStr,
99                     NULL );
100     XmStringFree ( xmstr );
101     XmStringFree ( titleStr );
102     
103     // Create an object to carry the additional data needed
104     // to cache the dialogs.
105     
106     DialogCallbackData *dcb = new DialogCallbackData( this, 
107                                                      clientData,
108                                                      ok, cancel, 
109                                                      help );
110     // Install callback function for each button 
111     // support by Motif dialogs. If there is no help callback
112     // unmanage the corresponding button instead, if possible.
113
114     if ( ok )
115       XtAddCallback ( dialog, 
116                       XmNokCallback, 
117                       &DialogManager::okCallback,
118                       (XtPointer) dcb );
119     else
120       {
121         Widget w = XmSelectionBoxGetChild ( dialog,
122                                           XmDIALOG_OK_BUTTON );
123         XtUnmanageChild ( w );
124       }
125
126
127     if ( cancel )
128       XtAddCallback ( dialog, 
129                       XmNcancelCallback, 
130                       &DialogManager::cancelCallback,
131                       (XtPointer) dcb );
132     else
133       {
134         Widget w = XmSelectionBoxGetChild ( dialog,
135                                           XmDIALOG_CANCEL_BUTTON );
136         XtUnmanageChild ( w );
137       }
138     
139     
140     if ( help )     
141         XtAddCallback ( dialog, 
142                        XmNhelpCallback, 
143                        &DialogManager::helpCallback,
144                        (XtPointer) dcb );
145     else
146     {
147         Widget w = XmSelectionBoxGetChild ( dialog,
148                                          XmDIALOG_HELP_BUTTON );
149         XtUnmanageChild ( w );
150     }
151     
152     // Post the dialog.
153     
154     XtManageChild ( dialog );
155
156     
157     return dialog;
158 }
159
160 Widget
161 PromptDialogManager::post( char *title,
162                            char         *text,
163                            Widget wid,
164                            void         *clientData,
165                            DialogCallback ok,
166                            DialogCallback cancel,
167                             DialogCallback help )
168 {
169
170     // Get a dialog widget from the cache
171     
172     Widget dialog = getDialog(wid);
173     
174     // Make sure the dialog exists, and that it is an XmMessageBox
175     // or subclass, since the callbacks assume this widget type
176     
177     assert ( dialog != NULL );
178     assert ( XtIsSubclass ( dialog, xmSelectionBoxWidgetClass ) );
179         
180         // Convert the text string to a compound string and 
181         // specify this to be the message displayed in the dialog.
182
183     XmString titleStr = XmStringCreateLocalized (title);
184     XmString xmstr = XmStringCreateLocalized ( text );
185     XtVaSetValues ( dialog, 
186                     XmNmessageString, xmstr,
187                     XmNdialogTitle, titleStr,
188                     NULL );
189     XmStringFree ( xmstr );
190     XmStringFree ( titleStr );
191     
192     // Create an object to carry the additional data needed
193     // to cache the dialogs.
194     
195     DialogCallbackData *dcb = new DialogCallbackData( this, 
196                                                      clientData,
197                                                      ok, cancel, 
198                                                      help );
199     // Install callback function for each button 
200     // support by Motif dialogs. If there is no help callback
201     // unmanage the corresponding button instead, if possible.
202
203     if ( ok )
204       XtAddCallback ( dialog, 
205                       XmNokCallback, 
206                       &DialogManager::okCallback,
207                       (XtPointer) dcb );
208     else
209       {
210         Widget w = XmSelectionBoxGetChild ( dialog,
211                                           XmDIALOG_OK_BUTTON );
212         XtUnmanageChild ( w );
213       }
214
215
216     if ( cancel )
217       XtAddCallback ( dialog, 
218                       XmNcancelCallback, 
219                       &DialogManager::cancelCallback,
220                       (XtPointer) dcb );
221     else
222       {
223         Widget w = XmSelectionBoxGetChild ( dialog,
224                                           XmDIALOG_CANCEL_BUTTON );
225         XtUnmanageChild ( w );
226       }
227     
228     
229     if ( help )     
230         XtAddCallback ( dialog, 
231                        XmNhelpCallback, 
232                        &DialogManager::helpCallback,
233                        (XtPointer) dcb );
234     else
235     {
236         Widget w = XmSelectionBoxGetChild ( dialog,
237                                          XmDIALOG_HELP_BUTTON );
238         XtUnmanageChild ( w );
239     }
240     
241     // Post the dialog.
242     
243     XtManageChild ( dialog );
244
245     
246     return dialog;
247 }