Merge branch 'linux1'
[oweals/cde.git] / cde / programs / dtmail / MotifApp / PromptDialogManager.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 librararies 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: PromptDialogManager.C /main/4 1996/04/21 19:40:30 drk $ */
24 /*
25  *+SNOTICE
26  *
27  *      $XConsortium: PromptDialogManager.C /main/4 1996/04/21 19:40:30 drk $
28  *
29  *      RESTRICTED CONFIDENTIAL INFORMATION:
30  *      
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
37  *      Sun's request.
38  *
39  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
40  *
41  *+ENOTICE
42  */
43
44 ///////////////////////////////////////////////////////////////////////////////
45 //////////////////////////////////////////////////////////////////////////////
46 //         This example code is from the book:
47 //
48 //           Object-Oriented Programming with C++ and OSF/Motif
49 //         by
50 //           Douglas Young
51 //           Prentice Hall, 1992
52 //           ISBN 0-13-630252-1 
53 //
54 //         Copyright 1991 by Prentice Hall
55 //         All Rights Reserved
56 //
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 //////////////////////////////////////////////////////////////////////////////
62
63
64 ///////////////////////////////////////////////////////////
65 // PromptDialogManager.C: 
66 //////////////////////////////////////////////////////////
67 #include "PromptDialogManager.h"
68 #include <Xm/Xm.h>
69 #include <Xm/SelectioB.h>
70 #include <assert.h>
71 // Define an instance to be available throughout the framework.
72
73 PromptDialogManager *thePromptDialogManager = 
74     new PromptDialogManager ( "PromptDialog" );
75
76 PromptDialogManager::PromptDialogManager ( char   *name ) : 
77                                  DialogManager ( name )
78 {
79     // Empty
80 }
81
82 Widget PromptDialogManager::createDialog ( Widget parent )
83 {
84   Widget dialog = XmCreatePromptDialog ( parent, _name, NULL, 0);
85     
86   XtVaSetValues ( dialog,
87                   XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL,
88                   NULL );
89   
90   return dialog;
91 }
92
93
94 Widget
95 PromptDialogManager::post( char *title,
96                            char         *text,
97                            void         *clientData,
98                            DialogCallback ok,
99                            DialogCallback cancel,
100                             DialogCallback help )
101 {
102
103     // Get a dialog widget from the cache
104     
105     Widget dialog = getDialog();
106     
107     // Make sure the dialog exists, and that it is an XmMessageBox
108     // or subclass, since the callbacks assume this widget type
109     
110     assert ( dialog != NULL );
111     assert ( XtIsSubclass ( dialog, xmSelectionBoxWidgetClass ) );
112         
113         // Convert the text string to a compound string and 
114         // specify this to be the message displayed in the dialog.
115
116     XmString titleStr = XmStringCreateLocalized (title);
117     XmString xmstr = XmStringCreateLocalized ( text );
118     XtVaSetValues ( dialog, 
119                     XmNmessageString, xmstr,
120                     XmNdialogTitle, titleStr,
121                     NULL );
122     XmStringFree ( xmstr );
123     XmStringFree ( titleStr );
124     
125     // Create an object to carry the additional data needed
126     // to cache the dialogs.
127     
128     DialogCallbackData *dcb = new DialogCallbackData( this, 
129                                                      clientData,
130                                                      ok, cancel, 
131                                                      help );
132     // Install callback function for each button 
133     // support by Motif dialogs. If there is no help callback
134     // unmanage the corresponding button instead, if possible.
135
136     if ( ok )
137       XtAddCallback ( dialog, 
138                       XmNokCallback, 
139                       &DialogManager::okCallback,
140                       (XtPointer) dcb );
141     else
142       {
143         Widget w = XmSelectionBoxGetChild ( dialog,
144                                           XmDIALOG_OK_BUTTON );
145         XtUnmanageChild ( w );
146       }
147
148
149     if ( cancel )
150       XtAddCallback ( dialog, 
151                       XmNcancelCallback, 
152                       &DialogManager::cancelCallback,
153                       (XtPointer) dcb );
154     else
155       {
156         Widget w = XmSelectionBoxGetChild ( dialog,
157                                           XmDIALOG_CANCEL_BUTTON );
158         XtUnmanageChild ( w );
159       }
160     
161     
162     if ( help )     
163         XtAddCallback ( dialog, 
164                        XmNhelpCallback, 
165                        &DialogManager::helpCallback,
166                        (XtPointer) dcb );
167     else
168     {
169         Widget w = XmSelectionBoxGetChild ( dialog,
170                                          XmDIALOG_HELP_BUTTON );
171         XtUnmanageChild ( w );
172     }
173     
174     // Post the dialog.
175     
176     XtManageChild ( dialog );
177
178     
179     return dialog;
180 }
181
182 Widget
183 PromptDialogManager::post( char *title,
184                            char         *text,
185                            Widget wid,
186                            void         *clientData,
187                            DialogCallback ok,
188                            DialogCallback cancel,
189                             DialogCallback help )
190 {
191
192     // Get a dialog widget from the cache
193     
194     Widget dialog = getDialog(wid);
195     
196     // Make sure the dialog exists, and that it is an XmMessageBox
197     // or subclass, since the callbacks assume this widget type
198     
199     assert ( dialog != NULL );
200     assert ( XtIsSubclass ( dialog, xmSelectionBoxWidgetClass ) );
201         
202         // Convert the text string to a compound string and 
203         // specify this to be the message displayed in the dialog.
204
205     XmString titleStr = XmStringCreateLocalized (title);
206     XmString xmstr = XmStringCreateLocalized ( text );
207     XtVaSetValues ( dialog, 
208                     XmNmessageString, xmstr,
209                     XmNdialogTitle, titleStr,
210                     NULL );
211     XmStringFree ( xmstr );
212     XmStringFree ( titleStr );
213     
214     // Create an object to carry the additional data needed
215     // to cache the dialogs.
216     
217     DialogCallbackData *dcb = new DialogCallbackData( this, 
218                                                      clientData,
219                                                      ok, cancel, 
220                                                      help );
221     // Install callback function for each button 
222     // support by Motif dialogs. If there is no help callback
223     // unmanage the corresponding button instead, if possible.
224
225     if ( ok )
226       XtAddCallback ( dialog, 
227                       XmNokCallback, 
228                       &DialogManager::okCallback,
229                       (XtPointer) dcb );
230     else
231       {
232         Widget w = XmSelectionBoxGetChild ( dialog,
233                                           XmDIALOG_OK_BUTTON );
234         XtUnmanageChild ( w );
235       }
236
237
238     if ( cancel )
239       XtAddCallback ( dialog, 
240                       XmNcancelCallback, 
241                       &DialogManager::cancelCallback,
242                       (XtPointer) dcb );
243     else
244       {
245         Widget w = XmSelectionBoxGetChild ( dialog,
246                                           XmDIALOG_CANCEL_BUTTON );
247         XtUnmanageChild ( w );
248       }
249     
250     
251     if ( help )     
252         XtAddCallback ( dialog, 
253                        XmNhelpCallback, 
254                        &DialogManager::helpCallback,
255                        (XtPointer) dcb );
256     else
257     {
258         Widget w = XmSelectionBoxGetChild ( dialog,
259                                          XmDIALOG_HELP_BUTTON );
260         XtUnmanageChild ( w );
261     }
262     
263     // Post the dialog.
264     
265     XtManageChild ( dialog );
266
267     
268     return dialog;
269 }