1 /* $TOG: SelectFileCmd.C /main/9 1998/10/26 17:57:37 mgreess $ */
5 * RESTRICTED CONFIDENTIAL INFORMATION:
7 * The information in this document is subject to special
8 * restrictions in a confidential disclosure agreement bertween
9 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
10 * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
11 * Sun's specific written approval. This documment and all copies
12 * and derivative works thereof must be returned or destroyed at
15 * Copyright 1993, 1994, 1995 Sun Microsystems, Inc. All rights reserved.
20 ///////////////////////////////////////////////////////////////////////////////
21 //////////////////////////////////////////////////////////////////////////////
22 // This example code is from the book:
24 // Object-Oriented Programming with C++ and OSF/Motif
27 // Prentice Hall, 1992
30 // Copyright 1991 by Prentice Hall
31 // All Rights Reserved
33 // Permission to use, copy, modify, and distribute this software for
34 // any purpose except publication and without fee is hereby granted, provided
35 // that the above copyright notice appear in all copies of the software.
36 ///////////////////////////////////////////////////////////////////////////////
37 //////////////////////////////////////////////////////////////////////////////
40 //////////////////////////////////////////////////////////
42 //////////////////////////////////////////////////////////
45 #include "SelectFileCmd.h"
46 #include "Application.h"
47 #include <Xm/FileSB.h>
48 #include <X11/Intrinsic.h>
57 #include <Dt/HourGlass.h>
61 extern XtPointer _XmStringUngenerate (
65 XmTextType output_type);
69 extern forceUpdate( Widget );
71 SelectFileCmd::SelectFileCmd (const char * name,
74 const char * ok_label,
76 FileCallback ok_callback,
79 NoUndoCmd ((char *)name, (char *)label, active )
81 _ok_label = (ok_label ? strdup(ok_label) : strdup("OK"));
82 _title = (title ? strdup(title) : strdup(name));
83 _ok_callback = ok_callback;
84 _ok_clientData = ok_clientData;
85 _cancel_callback = NULL;
86 _cancel_clientData = NULL;
88 _parentWidget = parent;
91 SelectFileCmd::SelectFileCmd (const char * name,
94 const char * ok_label,
96 FileCallback ok_callback,
98 FileCallback cancel_callback,
99 void *cancel_clientData,
101 NoUndoCmd ((char *)name, (char *)label, active )
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 = cancel_callback;
108 _cancel_clientData = cancel_clientData;
110 _parentWidget = parent;
113 SelectFileCmd::~SelectFileCmd()
120 void SelectFileCmd::doit()
122 // Create a FileSelectionBox widget
128 // If the FSB already exists and is managed, raise it.
131 XtManageChild ( _fileBrowser );
132 XtPopup (XtParent(_fileBrowser), XtGrabNone );
133 XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
134 forceUpdate(_fileBrowser);
138 // If there is no FSB, create it and manage it.
139 // If there is one, just manage it.
141 // Creating one is different from the book. cast required.
142 // Also, its the "new CDE" FSB!
145 _DtTurnOnHourGlass(_parentWidget);
148 XmCreateFileSelectionDialog (_parentWidget,
151 // Set the title right...
152 title = XmStringCreateLocalized(_title);
153 XmString ok_str = XmStringCreateLocalized(_ok_label);
154 XtVaSetValues(_fileBrowser,
155 XmNdialogTitle, title,
156 XmNokLabelString, ok_str,
159 XmStringFree(ok_str);
160 XmString hidden_str = XmStringCreateLocalized(
161 GETMSG(catd, 1, 11, "Show hidden folders and files"));
162 _hidden_button = XtVaCreateManagedWidget(
163 "hidden", xmToggleButtonWidgetClass,
165 XmNlabelString, hidden_str,
166 XmNalignment, XmALIGNMENT_BEGINNING,
167 XmNnavigationType, XmSTICKY_TAB_GROUP,
170 XmStringFree(hidden_str);
172 printHelpId("_fileBrowser", _fileBrowser);
173 /* add help callback */
174 // XtAddCallback(_fileBrowser, XmNhelpCallback, HelpCB, helpId);
176 // Set up the callback to be called when the user chooses a file
178 XtAddCallback ( _fileBrowser,
180 &SelectFileCmd::fileSelectedCB,
182 XtAddCallback ( _fileBrowser,
184 &SelectFileCmd::fileCanceledCB,
186 XtAddCallback(_hidden_button,
187 XmNvalueChangedCallback,
188 &SelectFileCmd::hiddenCB,
192 XmFileSelectionBoxGetChild(_fileBrowser, XmDIALOG_HELP_BUTTON),
195 _DtTurnOffHourGlass(_parentWidget);
198 // Display the dialog
200 XtManageChild ( _fileBrowser );
202 // Raise it, because it might be buried.
204 XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
207 void SelectFileCmd::fileSelectedCB(
209 XtPointer clientData,
212 SelectFileCmd * obj = (SelectFileCmd *) clientData;
213 XmFileSelectionBoxCallbackStruct *cb =
214 (XmFileSelectionBoxCallbackStruct *) callData;
217 // XtUnmanageChild ( w ); // Bring the file selection dialog down.
221 // Extract the first character string matching the default
222 // character set from the compound string
223 name = (char *) _XmStringUngenerate(
225 XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
226 // if the name is a null string ( no file is selected)
227 // we should pop up an error dialog to let user know
228 // that he/she did not specify file name. But because
229 // the message cat file is frozen, we can not add any
230 // new msg. Just free the name and return.
231 // see aix defect 176761.
232 if(NULL == name) return;
239 // If a string was succesfully extracted, call
240 // fileSelected to handle the file.
242 XtUnmanageChild ( w ); // Bring the file selection dialog down.
243 obj->fileSelected ( name );
247 void SelectFileCmd::fileCanceledCB ( Widget w,
248 XtPointer clientData,
251 SelectFileCmd * obj = (SelectFileCmd *) clientData;
253 XtUnmanageChild(w); // Bring the file selection dialog down.
257 void SelectFileCmd::fileSelected ( char *filename )
260 _ok_callback ( _ok_clientData, filename );
263 void SelectFileCmd::fileCanceled ()
265 if ( _cancel_callback )
266 _cancel_callback ( _cancel_clientData, NULL );
270 SelectFileCmd::hiddenCB(Widget,
271 XtPointer client_data,
274 SelectFileCmd * self = (SelectFileCmd *)client_data;
275 XmToggleButtonCallbackStruct *cbs = (XmToggleButtonCallbackStruct*) cb_data;
277 self->doHidden(cbs->set);
281 SelectFileCmd::doHidden(int on)
284 style = (on) ? XmFILTER_NONE : XmFILTER_HIDDEN_FILES;
285 XtVaSetValues(_fileBrowser, XmNfileFilterStyle, style, NULL);
286 XmFileSelectionDoSearch(_fileBrowser, NULL);
290 SelectFileCmd::getDirectory()
295 if (NULL == _fileBrowser)
298 // Get the default selection.
299 XtVaGetValues(_fileBrowser, XmNdirectory, &directory, NULL);
301 _XmStringUngenerate(directory, NULL, XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
302 XmStringFree(directory);
308 SelectFileCmd::getSelected()
313 if (NULL == _fileBrowser)
316 // Set the default selection.
317 text = XtNameToWidget(_fileBrowser, "Text");
319 XtVaGetValues(text, XmNvalue, &path, NULL);
325 SelectFileCmd::getHidden()
328 unsigned char current_state;
330 if (NULL == _fileBrowser || NULL == _hidden_button)
333 XtVaGetValues(_hidden_button, XmNset, ¤t_state, NULL);
334 val = (current_state == XmSET) ? 1 : 0;
339 SelectFileCmd::setDirectory(char *path)
343 if (NULL == _fileBrowser)
346 // Set the default directory where the file selection box points.
347 directory = XmStringCreateLocalized(path);
348 XtVaSetValues(_fileBrowser, XmNdirectory, directory, NULL);
349 XmStringFree(directory);
353 SelectFileCmd::setSelected(char *path)
357 if (NULL == _fileBrowser)
360 // Set the default selection.
361 text = XtNameToWidget(_fileBrowser, "Text");
363 XtVaSetValues(text, XmNvalue, path, NULL);
367 SelectFileCmd::setHidden(int on)
369 unsigned char current_state;
370 unsigned char desired_state;
372 if (NULL == _fileBrowser || NULL == _hidden_button)
375 desired_state = (on) ? XmSET : XmUNSET;
376 XtVaGetValues(_hidden_button, XmNset, ¤t_state, NULL);
377 if (current_state == desired_state)
380 XtVaSetValues(_hidden_button, XmNset, desired_state, NULL);