Last of the spelling fixed
[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 libraries 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 void 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     _hidden_button     = NULL;
112     _directory         = NULL;
113 }
114
115 SelectFileCmd::SelectFileCmd (const char * name, 
116                               const char * label,
117                               const char * title,
118                               const char * ok_label,
119                               int          active,
120                               FileCallback ok_callback,
121                               void        *ok_clientData,
122                               FileCallback cancel_callback,
123                               void        *cancel_clientData,
124                               Widget       parent) :
125                               NoUndoCmd ((char *)name, (char *)label, active )
126 {
127     _ok_label = (ok_label ? strdup(ok_label) : strdup("OK"));
128     _title = (title ? strdup(title) : strdup(name));
129     _ok_callback       = ok_callback;
130     _ok_clientData     = ok_clientData;
131     _cancel_callback   = cancel_callback;
132     _cancel_clientData = cancel_clientData;
133     _fileBrowser       = NULL;
134     _parentWidget      = parent;
135     _hidden_button     = NULL;
136     _directory         = NULL;
137 }
138
139 SelectFileCmd::~SelectFileCmd()
140 {
141     free(_ok_label);
142     free(_title);
143 }
144
145
146 void SelectFileCmd::doit()
147 {
148     // Create a FileSelectionBox widget
149     
150     Arg args[1];
151     Cardinal n = 0;
152     XmString title;
153
154     // If the FSB already exists and is managed, raise it.
155
156     if (_fileBrowser) {
157         XtManageChild ( _fileBrowser );
158         XtPopup (XtParent(_fileBrowser), XtGrabNone );
159         XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
160         forceUpdate(_fileBrowser);
161         return;
162     }
163
164     // If there is no FSB, create it and manage it.
165     // If there is one, just manage it.
166
167     // Creating one is different from the book. cast required.
168     // Also, its the "new CDE" FSB!
169
170     if (!_fileBrowser) {
171         _DtTurnOnHourGlass(_parentWidget);
172
173         _fileBrowser =
174             XmCreateFileSelectionDialog (_parentWidget,
175                                          (char *) name(), 
176                                          args, n );       
177         // Set the title right...
178         title = XmStringCreateLocalized(_title);
179         XmString ok_str = XmStringCreateLocalized(_ok_label);
180         XtVaSetValues(_fileBrowser,
181                       XmNdialogTitle, title,
182                       XmNokLabelString, ok_str,
183                       NULL);
184         XmStringFree(title);
185         XmStringFree(ok_str);
186         XmString hidden_str = XmStringCreateLocalized(
187                         GETMSG(catd, 1, 11, "Show hidden folders and files"));
188         _hidden_button = XtVaCreateManagedWidget(
189                                 "hidden", xmToggleButtonWidgetClass,
190                                  _fileBrowser,
191                                  XmNlabelString, hidden_str,
192                                  XmNalignment, XmALIGNMENT_BEGINNING,
193                                  XmNnavigationType, XmSTICKY_TAB_GROUP,
194                                  XmNsensitive, TRUE,
195                                  NULL);
196         XmStringFree(hidden_str);
197
198         printHelpId("_fileBrowser", _fileBrowser);
199         /* add help callback */
200         // XtAddCallback(_fileBrowser, XmNhelpCallback, HelpCB, helpId);
201         
202         // Set up the callback to be called when the user chooses a file
203             
204         XtAddCallback ( _fileBrowser, 
205             XmNokCallback,
206             &SelectFileCmd::fileSelectedCB, 
207             (XtPointer) this );
208         XtAddCallback ( _fileBrowser,
209             XmNcancelCallback,
210             &SelectFileCmd::fileCanceledCB,
211             (XtPointer) this );
212         XtAddCallback(_hidden_button,
213                       XmNvalueChangedCallback,
214                       &SelectFileCmd::hiddenCB,
215                       this);
216
217         XtSetSensitive(
218             XmFileSelectionBoxGetChild(_fileBrowser, XmDIALOG_HELP_BUTTON),
219             False);
220
221         _DtTurnOffHourGlass(_parentWidget);
222     }
223     
224     // Display the dialog
225     
226     XtManageChild ( _fileBrowser );
227
228     // Raise it, because it might be buried.
229     //
230     XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
231 }
232
233 void SelectFileCmd::fileSelectedCB(
234                                 Widget w,
235                                 XtPointer clientData,
236                                 XtPointer callData)
237 {
238     SelectFileCmd * obj = (SelectFileCmd *) clientData;
239     XmFileSelectionBoxCallbackStruct *cb = 
240         (XmFileSelectionBoxCallbackStruct *) callData;
241     char     *name   = NULL;
242
243     // XtUnmanageChild ( w );   // Bring the file selection dialog down.
244     
245     if (cb->value)
246     {
247         // Extract the first character string matching the default
248         // character set from the compound string
249         name = (char *) _XmStringUngenerate(
250                                         cb->value, NULL,
251                                         XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
252          // if the name is a null string ( no file is selected)
253          // we should pop up an error dialog to let user know
254          // that he/she did not specify file name. But because
255          // the message cat file is frozen, we can not add any
256          // new msg. Just free the name and return.
257          // see aix defect 176761.
258          if(NULL == name) return;
259          if (strlen(name)<1)
260          {
261            free(name);
262            return;
263          }
264
265         // If a string was successfully extracted, call
266         // fileSelected to handle the file.
267
268         XtUnmanageChild ( w );   // Bring the file selection dialog down.
269         obj->fileSelected ( name );
270     }
271 }
272
273 void SelectFileCmd::fileCanceledCB ( Widget w,
274                                      XtPointer clientData,
275                                      XtPointer )
276 {
277     SelectFileCmd * obj = (SelectFileCmd *) clientData;
278
279     XtUnmanageChild(w);   // Bring the file selection dialog down.
280     obj->fileCanceled();
281 }
282
283 void SelectFileCmd::fileSelected ( char *filename )
284 {
285     if ( _ok_callback )
286         _ok_callback ( _ok_clientData, filename );
287 }
288
289 void SelectFileCmd::fileCanceled ()
290 {
291     if ( _cancel_callback )
292         _cancel_callback ( _cancel_clientData, NULL );
293 }
294
295 void
296 SelectFileCmd::hiddenCB(Widget,
297                         XtPointer client_data,
298                         XtPointer cb_data)
299 {
300     SelectFileCmd * self = (SelectFileCmd *)client_data;
301     XmToggleButtonCallbackStruct *cbs = (XmToggleButtonCallbackStruct*) cb_data;
302
303     self->doHidden(cbs->set);
304 }
305
306 void
307 SelectFileCmd::doHidden(int on)
308 {
309     XtEnum              style;
310     style = (on) ? XmFILTER_NONE : XmFILTER_HIDDEN_FILES;
311     XtVaSetValues(_fileBrowser, XmNfileFilterStyle, style, NULL);
312     XmFileSelectionDoSearch(_fileBrowser, NULL);
313 }
314
315 char *
316 SelectFileCmd::getDirectory()
317 {
318     XmString    directory;
319     char        *path;
320
321     if (NULL == _fileBrowser)
322       return NULL;
323
324     // Get the default selection.
325     XtVaGetValues(_fileBrowser, XmNdirectory, &directory, NULL);
326     path = (char *)
327       _XmStringUngenerate(directory, NULL, XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
328     XmStringFree(directory);
329
330     return path;
331 }
332
333 char *
334 SelectFileCmd::getSelected()
335 {
336     Widget      text;
337     char        *path = NULL;
338
339     if (NULL == _fileBrowser)
340       return NULL;
341
342     // Set the default selection.
343     text = XtNameToWidget(_fileBrowser, "Text");
344     if (NULL != text)
345       XtVaGetValues(text, XmNvalue, &path, NULL);
346
347     return path;
348 }
349
350 int
351 SelectFileCmd::getHidden()
352 {
353     int                 val;
354     XtArgVal            current_state;
355
356     if (NULL == _fileBrowser || NULL == _hidden_button)
357       return 0;
358     
359     XtVaGetValues(_hidden_button, XmNset, &current_state, NULL);
360     val = (current_state == XmSET) ? 1 : 0;
361     return val;
362 }
363
364 void
365 SelectFileCmd::setDirectory(char *path)
366 {
367     XmString    directory;
368
369     if (NULL == _fileBrowser)
370       return;
371
372     // Set the default directory where the file selection box points.
373     directory = XmStringCreateLocalized(path);
374     XtVaSetValues(_fileBrowser, XmNdirectory, directory, NULL);
375     XmStringFree(directory);
376 }
377
378 void
379 SelectFileCmd::setSelected(char *path)
380 {
381     Widget      text;
382
383     if (NULL == _fileBrowser)
384       return;
385
386     // Set the default selection.
387     text = XtNameToWidget(_fileBrowser, "Text");
388     if (NULL != text)
389       XtVaSetValues(text, XmNvalue, path, NULL);
390 }
391
392 void
393 SelectFileCmd::setHidden(int on)
394 {
395     XtArgVal            current_state;
396     XtArgVal            desired_state;
397
398     if (NULL == _fileBrowser || NULL == _hidden_button)
399       return;
400     
401     desired_state = (on) ? XmSET : XmUNSET;
402     XtVaGetValues(_hidden_button, XmNset, &current_state, NULL);
403     if (current_state == desired_state)
404       return;
405
406     XtVaSetValues(_hidden_button, XmNset, desired_state, NULL);
407     doHidden(on);
408 }