-fpermissive to allow GCC to compile old C++
[oweals/cde.git] / cde / programs / dtmail / MotifApp / SelectFileCmd.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 /* $TOG: SelectFileCmd.C /main/9 1998/10/26 17:57:37 mgreess $ */
24 /*
25  *+SNOTICE
26  *
27  *      RESTRICTED CONFIDENTIAL INFORMATION:
28  *      
29  *      The information in this document is subject to special
30  *      restrictions in a confidential disclosure agreement bertween
31  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
32  *      document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
33  *      Sun's specific written approval.  This documment and all copies
34  *      and derivative works thereof must be returned or destroyed at
35  *      Sun's request.
36  *
37  *      Copyright 1993, 1994, 1995 Sun Microsystems, Inc.  All rights reserved.
38  *
39  *+ENOTICE
40  */
41
42 ///////////////////////////////////////////////////////////////////////////////
43 //////////////////////////////////////////////////////////////////////////////
44 //         This example code is from the book:
45 //
46 //           Object-Oriented Programming with C++ and OSF/Motif
47 //         by
48 //           Douglas Young
49 //           Prentice Hall, 1992
50 //           ISBN 0-13-630252-1 
51 //
52 //         Copyright 1991 by Prentice Hall
53 //         All Rights Reserved
54 //
55 //  Permission to use, copy, modify, and distribute this software for 
56 //  any purpose except publication and without fee is hereby granted, provided 
57 //  that the above copyright notice appear in all copies of the software.
58 ///////////////////////////////////////////////////////////////////////////////
59 //////////////////////////////////////////////////////////////////////////////
60
61
62 //////////////////////////////////////////////////////////
63 // SelectFileCmd.C: 
64 //////////////////////////////////////////////////////////
65 #include <unistd.h>
66 #include <stdlib.h>
67 #include "SelectFileCmd.h"
68 #include "Application.h"
69 #include <Xm/FileSB.h>
70 #include <X11/Intrinsic.h>
71 #include "Help.hh"
72
73 #include <nl_types.h>
74 extern nl_catd catd;
75
76 #include "NLS.hh"
77
78 extern "C" {
79 #include <Dt/HourGlass.h>
80 }
81
82 extern "C" {
83 extern XtPointer _XmStringUngenerate (
84                                 XmString string,
85                                 XmStringTag tag,
86                                 XmTextType tag_type,
87                                 XmTextType output_type);
88 }
89
90
91 extern forceUpdate( Widget );
92
93 SelectFileCmd::SelectFileCmd (const char * name, 
94                               const char * label,
95                               const char * title,
96                               const char * ok_label,
97                               int          active,
98                               FileCallback ok_callback,
99                               void        *ok_clientData,
100                               Widget       parent) :
101                               NoUndoCmd ((char *)name, (char *)label, active )
102 {
103     _ok_label = (ok_label ? strdup(ok_label) : strdup("OK"));
104     _title = (title ? strdup(title) : strdup(name));
105     _ok_callback       = ok_callback;
106     _ok_clientData     = ok_clientData;
107     _cancel_callback   = NULL;
108     _cancel_clientData = NULL;
109     _fileBrowser       = NULL;
110     _parentWidget      = parent;
111 }
112
113 SelectFileCmd::SelectFileCmd (const char * name, 
114                               const char * label,
115                               const char * title,
116                               const char * ok_label,
117                               int          active,
118                               FileCallback ok_callback,
119                               void        *ok_clientData,
120                               FileCallback cancel_callback,
121                               void        *cancel_clientData,
122                               Widget       parent) :
123                               NoUndoCmd ((char *)name, (char *)label, active )
124 {
125     _ok_label = (ok_label ? strdup(ok_label) : strdup("OK"));
126     _title = (title ? strdup(title) : strdup(name));
127     _ok_callback       = ok_callback;
128     _ok_clientData     = ok_clientData;
129     _cancel_callback   = cancel_callback;
130     _cancel_clientData = cancel_clientData;
131     _fileBrowser       = NULL;
132     _parentWidget      = parent;
133 }
134
135 SelectFileCmd::~SelectFileCmd()
136 {
137     free(_ok_label);
138     free(_title);
139 }
140
141
142 void SelectFileCmd::doit()
143 {
144     // Create a FileSelectionBox widget
145     
146     Arg args[1];
147     Cardinal n = 0;
148     XmString title;
149
150     // If the FSB already exists and is managed, raise it.
151
152     if (_fileBrowser) {
153         XtManageChild ( _fileBrowser );
154         XtPopup (XtParent(_fileBrowser), XtGrabNone );
155         XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
156         forceUpdate(_fileBrowser);
157         return;
158     }
159
160     // If there is no FSB, create it and manage it.
161     // If there is one, just manage it.
162
163     // Creating one is different from the book. cast required.
164     // Also, its the "new CDE" FSB!
165
166     if (!_fileBrowser) {
167         _DtTurnOnHourGlass(_parentWidget);
168
169         _fileBrowser =
170             XmCreateFileSelectionDialog (_parentWidget,
171                                          (char *) name(), 
172                                          args, n );       
173         // Set the title right...
174         title = XmStringCreateLocalized(_title);
175         XmString ok_str = XmStringCreateLocalized(_ok_label);
176         XtVaSetValues(_fileBrowser,
177                       XmNdialogTitle, title,
178                       XmNokLabelString, ok_str,
179                       NULL);
180         XmStringFree(title);
181         XmStringFree(ok_str);
182         XmString hidden_str = XmStringCreateLocalized(
183                         GETMSG(catd, 1, 11, "Show hidden folders and files"));
184         _hidden_button = XtVaCreateManagedWidget(
185                                 "hidden", xmToggleButtonWidgetClass,
186                                  _fileBrowser,
187                                  XmNlabelString, hidden_str,
188                                  XmNalignment, XmALIGNMENT_BEGINNING,
189                                  XmNnavigationType, XmSTICKY_TAB_GROUP,
190                                  XmNsensitive, TRUE,
191                                  NULL);
192         XmStringFree(hidden_str);
193
194         printHelpId("_fileBrowser", _fileBrowser);
195         /* add help callback */
196         // XtAddCallback(_fileBrowser, XmNhelpCallback, HelpCB, helpId);
197         
198         // Set up the callback to be called when the user chooses a file
199             
200         XtAddCallback ( _fileBrowser, 
201             XmNokCallback,
202             &SelectFileCmd::fileSelectedCB, 
203             (XtPointer) this );
204         XtAddCallback ( _fileBrowser,
205             XmNcancelCallback,
206             &SelectFileCmd::fileCanceledCB,
207             (XtPointer) this );
208         XtAddCallback(_hidden_button,
209                       XmNvalueChangedCallback,
210                       &SelectFileCmd::hiddenCB,
211                       this);
212
213         XtSetSensitive(
214             XmFileSelectionBoxGetChild(_fileBrowser, XmDIALOG_HELP_BUTTON),
215             False);
216
217         _DtTurnOffHourGlass(_parentWidget);
218     }
219     
220     // Display the dialog
221     
222     XtManageChild ( _fileBrowser );
223
224     // Raise it, because it might be buried.
225     //
226     XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
227 }
228
229 void SelectFileCmd::fileSelectedCB(
230                                 Widget w,
231                                 XtPointer clientData,
232                                 XtPointer callData)
233 {
234     SelectFileCmd * obj = (SelectFileCmd *) clientData;
235     XmFileSelectionBoxCallbackStruct *cb = 
236         (XmFileSelectionBoxCallbackStruct *) callData;
237     char     *name   = NULL;
238
239     // XtUnmanageChild ( w );   // Bring the file selection dialog down.
240     
241     if (cb->value)
242     {
243         // Extract the first character string matching the default
244         // character set from the compound string
245         name = (char *) _XmStringUngenerate(
246                                         cb->value, NULL,
247                                         XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
248          // if the name is a null string ( no file is selected)
249          // we should pop up an error dialog to let user know
250          // that he/she did not specify file name. But because
251          // the message cat file is frozen, we can not add any
252          // new msg. Just free the name and return.
253          // see aix defect 176761.
254          if(NULL == name) return;
255          if (strlen(name)<1)
256          {
257            free(name);
258            return;
259          }
260
261         // If a string was succesfully extracted, call
262         // fileSelected to handle the file.
263
264         XtUnmanageChild ( w );   // Bring the file selection dialog down.
265         obj->fileSelected ( name );
266     }
267 }
268
269 void SelectFileCmd::fileCanceledCB ( Widget w,
270                                      XtPointer clientData,
271                                      XtPointer )
272 {
273     SelectFileCmd * obj = (SelectFileCmd *) clientData;
274
275     XtUnmanageChild(w);   // Bring the file selection dialog down.
276     obj->fileCanceled();
277 }
278
279 void SelectFileCmd::fileSelected ( char *filename )
280 {
281     if ( _ok_callback )
282         _ok_callback ( _ok_clientData, filename );
283 }
284
285 void SelectFileCmd::fileCanceled ()
286 {
287     if ( _cancel_callback )
288         _cancel_callback ( _cancel_clientData, NULL );
289 }
290
291 void
292 SelectFileCmd::hiddenCB(Widget,
293                         XtPointer client_data,
294                         XtPointer cb_data)
295 {
296     SelectFileCmd * self = (SelectFileCmd *)client_data;
297     XmToggleButtonCallbackStruct *cbs = (XmToggleButtonCallbackStruct*) cb_data;
298
299     self->doHidden(cbs->set);
300 }
301
302 void
303 SelectFileCmd::doHidden(int on)
304 {
305     XtEnum              style;
306     style = (on) ? XmFILTER_NONE : XmFILTER_HIDDEN_FILES;
307     XtVaSetValues(_fileBrowser, XmNfileFilterStyle, style, NULL);
308     XmFileSelectionDoSearch(_fileBrowser, NULL);
309 }
310
311 char *
312 SelectFileCmd::getDirectory()
313 {
314     XmString    directory;
315     char        *path;
316
317     if (NULL == _fileBrowser)
318       return NULL;
319
320     // Get the default selection.
321     XtVaGetValues(_fileBrowser, XmNdirectory, &directory, NULL);
322     path = (char *)
323       _XmStringUngenerate(directory, NULL, XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
324     XmStringFree(directory);
325
326     return path;
327 }
328
329 char *
330 SelectFileCmd::getSelected()
331 {
332     Widget      text;
333     char        *path = NULL;
334
335     if (NULL == _fileBrowser)
336       return NULL;
337
338     // Set the default selection.
339     text = XtNameToWidget(_fileBrowser, "Text");
340     if (NULL != text)
341       XtVaGetValues(text, XmNvalue, &path, NULL);
342
343     return path;
344 }
345
346 int
347 SelectFileCmd::getHidden()
348 {
349     int                 val;
350     unsigned char       current_state;
351
352     if (NULL == _fileBrowser || NULL == _hidden_button)
353       return 0;
354     
355     XtVaGetValues(_hidden_button, XmNset, &current_state, NULL);
356     val = (current_state == XmSET) ? 1 : 0;
357     return val;
358 }
359
360 void
361 SelectFileCmd::setDirectory(char *path)
362 {
363     XmString    directory;
364
365     if (NULL == _fileBrowser)
366       return;
367
368     // Set the default directory where the file selection box points.
369     directory = XmStringCreateLocalized(path);
370     XtVaSetValues(_fileBrowser, XmNdirectory, directory, NULL);
371     XmStringFree(directory);
372 }
373
374 void
375 SelectFileCmd::setSelected(char *path)
376 {
377     Widget      text;
378
379     if (NULL == _fileBrowser)
380       return;
381
382     // Set the default selection.
383     text = XtNameToWidget(_fileBrowser, "Text");
384     if (NULL != text)
385       XtVaSetValues(text, XmNvalue, path, NULL);
386 }
387
388 void
389 SelectFileCmd::setHidden(int on)
390 {
391     unsigned char       current_state;
392     unsigned char       desired_state;
393
394     if (NULL == _fileBrowser || NULL == _hidden_button)
395       return;
396     
397     desired_state = (on) ? XmSET : XmUNSET;
398     XtVaGetValues(_hidden_button, XmNset, &current_state, NULL);
399     if (current_state == desired_state)
400       return;
401
402     XtVaSetValues(_hidden_button, XmNset, desired_state, NULL);
403     doHidden(on);
404 }