2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $TOG: SelectFileCmd.C /main/9 1998/10/26 17:57:37 mgreess $ */
27 * RESTRICTED CONFIDENTIAL INFORMATION:
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
37 * Copyright 1993, 1994, 1995 Sun Microsystems, Inc. All rights reserved.
42 ///////////////////////////////////////////////////////////////////////////////
43 //////////////////////////////////////////////////////////////////////////////
44 // This example code is from the book:
46 // Object-Oriented Programming with C++ and OSF/Motif
49 // Prentice Hall, 1992
52 // Copyright 1991 by Prentice Hall
53 // All Rights Reserved
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 //////////////////////////////////////////////////////////////////////////////
62 //////////////////////////////////////////////////////////
64 //////////////////////////////////////////////////////////
67 #include "SelectFileCmd.h"
68 #include "Application.h"
69 #include <Xm/FileSB.h>
70 #include <X11/Intrinsic.h>
79 #include <Dt/HourGlass.h>
83 extern XtPointer _XmStringUngenerate (
87 XmTextType output_type);
91 extern void forceUpdate( Widget );
93 SelectFileCmd::SelectFileCmd (const char * name,
96 const char * ok_label,
98 FileCallback ok_callback,
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 = NULL;
108 _cancel_clientData = NULL;
110 _parentWidget = parent;
111 _hidden_button = NULL;
115 SelectFileCmd::SelectFileCmd (const char * name,
118 const char * ok_label,
120 FileCallback ok_callback,
122 FileCallback cancel_callback,
123 void *cancel_clientData,
125 NoUndoCmd ((char *)name, (char *)label, active )
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;
134 _parentWidget = parent;
135 _hidden_button = NULL;
139 SelectFileCmd::~SelectFileCmd()
146 void SelectFileCmd::doit()
148 // Create a FileSelectionBox widget
154 // If the FSB already exists and is managed, raise it.
157 XtManageChild ( _fileBrowser );
158 XtPopup (XtParent(_fileBrowser), XtGrabNone );
159 XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
160 forceUpdate(_fileBrowser);
164 // If there is no FSB, create it and manage it.
165 // If there is one, just manage it.
167 // Creating one is different from the book. cast required.
168 // Also, its the "new CDE" FSB!
171 _DtTurnOnHourGlass(_parentWidget);
174 XmCreateFileSelectionDialog (_parentWidget,
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,
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,
191 XmNlabelString, hidden_str,
192 XmNalignment, XmALIGNMENT_BEGINNING,
193 XmNnavigationType, XmSTICKY_TAB_GROUP,
196 XmStringFree(hidden_str);
198 printHelpId("_fileBrowser", _fileBrowser);
199 /* add help callback */
200 // XtAddCallback(_fileBrowser, XmNhelpCallback, HelpCB, helpId);
202 // Set up the callback to be called when the user chooses a file
204 XtAddCallback ( _fileBrowser,
206 &SelectFileCmd::fileSelectedCB,
208 XtAddCallback ( _fileBrowser,
210 &SelectFileCmd::fileCanceledCB,
212 XtAddCallback(_hidden_button,
213 XmNvalueChangedCallback,
214 &SelectFileCmd::hiddenCB,
218 XmFileSelectionBoxGetChild(_fileBrowser, XmDIALOG_HELP_BUTTON),
221 _DtTurnOffHourGlass(_parentWidget);
224 // Display the dialog
226 XtManageChild ( _fileBrowser );
228 // Raise it, because it might be buried.
230 XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
233 void SelectFileCmd::fileSelectedCB(
235 XtPointer clientData,
238 SelectFileCmd * obj = (SelectFileCmd *) clientData;
239 XmFileSelectionBoxCallbackStruct *cb =
240 (XmFileSelectionBoxCallbackStruct *) callData;
243 // XtUnmanageChild ( w ); // Bring the file selection dialog down.
247 // Extract the first character string matching the default
248 // character set from the compound string
249 name = (char *) _XmStringUngenerate(
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;
265 // If a string was successfully extracted, call
266 // fileSelected to handle the file.
268 XtUnmanageChild ( w ); // Bring the file selection dialog down.
269 obj->fileSelected ( name );
273 void SelectFileCmd::fileCanceledCB ( Widget w,
274 XtPointer clientData,
277 SelectFileCmd * obj = (SelectFileCmd *) clientData;
279 XtUnmanageChild(w); // Bring the file selection dialog down.
283 void SelectFileCmd::fileSelected ( char *filename )
286 _ok_callback ( _ok_clientData, filename );
289 void SelectFileCmd::fileCanceled ()
291 if ( _cancel_callback )
292 _cancel_callback ( _cancel_clientData, NULL );
296 SelectFileCmd::hiddenCB(Widget,
297 XtPointer client_data,
300 SelectFileCmd * self = (SelectFileCmd *)client_data;
301 XmToggleButtonCallbackStruct *cbs = (XmToggleButtonCallbackStruct*) cb_data;
303 self->doHidden(cbs->set);
307 SelectFileCmd::doHidden(int on)
310 style = (on) ? XmFILTER_NONE : XmFILTER_HIDDEN_FILES;
311 XtVaSetValues(_fileBrowser, XmNfileFilterStyle, style, NULL);
312 XmFileSelectionDoSearch(_fileBrowser, NULL);
316 SelectFileCmd::getDirectory()
321 if (NULL == _fileBrowser)
324 // Get the default selection.
325 XtVaGetValues(_fileBrowser, XmNdirectory, &directory, NULL);
327 _XmStringUngenerate(directory, NULL, XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
328 XmStringFree(directory);
334 SelectFileCmd::getSelected()
339 if (NULL == _fileBrowser)
342 // Set the default selection.
343 text = XtNameToWidget(_fileBrowser, "Text");
345 XtVaGetValues(text, XmNvalue, &path, NULL);
351 SelectFileCmd::getHidden()
354 XtArgVal current_state;
356 if (NULL == _fileBrowser || NULL == _hidden_button)
359 XtVaGetValues(_hidden_button, XmNset, ¤t_state, NULL);
360 val = (current_state == XmSET) ? 1 : 0;
365 SelectFileCmd::setDirectory(char *path)
369 if (NULL == _fileBrowser)
372 // Set the default directory where the file selection box points.
373 directory = XmStringCreateLocalized(path);
374 XtVaSetValues(_fileBrowser, XmNdirectory, directory, NULL);
375 XmStringFree(directory);
379 SelectFileCmd::setSelected(char *path)
383 if (NULL == _fileBrowser)
386 // Set the default selection.
387 text = XtNameToWidget(_fileBrowser, "Text");
389 XtVaSetValues(text, XmNvalue, path, NULL);
393 SelectFileCmd::setHidden(int on)
395 XtArgVal current_state;
396 XtArgVal desired_state;
398 if (NULL == _fileBrowser || NULL == _hidden_button)
401 desired_state = (on) ? XmSET : XmUNSET;
402 XtVaGetValues(_hidden_button, XmNset, ¤t_state, NULL);
403 if (current_state == desired_state)
406 XtVaSetValues(_hidden_button, XmNset, desired_state, NULL);