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 librararies 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;
113 SelectFileCmd::SelectFileCmd (const char * name,
116 const char * ok_label,
118 FileCallback ok_callback,
120 FileCallback cancel_callback,
121 void *cancel_clientData,
123 NoUndoCmd ((char *)name, (char *)label, active )
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;
132 _parentWidget = parent;
135 SelectFileCmd::~SelectFileCmd()
142 void SelectFileCmd::doit()
144 // Create a FileSelectionBox widget
150 // If the FSB already exists and is managed, raise it.
153 XtManageChild ( _fileBrowser );
154 XtPopup (XtParent(_fileBrowser), XtGrabNone );
155 XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
156 forceUpdate(_fileBrowser);
160 // If there is no FSB, create it and manage it.
161 // If there is one, just manage it.
163 // Creating one is different from the book. cast required.
164 // Also, its the "new CDE" FSB!
167 _DtTurnOnHourGlass(_parentWidget);
170 XmCreateFileSelectionDialog (_parentWidget,
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,
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,
187 XmNlabelString, hidden_str,
188 XmNalignment, XmALIGNMENT_BEGINNING,
189 XmNnavigationType, XmSTICKY_TAB_GROUP,
192 XmStringFree(hidden_str);
194 printHelpId("_fileBrowser", _fileBrowser);
195 /* add help callback */
196 // XtAddCallback(_fileBrowser, XmNhelpCallback, HelpCB, helpId);
198 // Set up the callback to be called when the user chooses a file
200 XtAddCallback ( _fileBrowser,
202 &SelectFileCmd::fileSelectedCB,
204 XtAddCallback ( _fileBrowser,
206 &SelectFileCmd::fileCanceledCB,
208 XtAddCallback(_hidden_button,
209 XmNvalueChangedCallback,
210 &SelectFileCmd::hiddenCB,
214 XmFileSelectionBoxGetChild(_fileBrowser, XmDIALOG_HELP_BUTTON),
217 _DtTurnOffHourGlass(_parentWidget);
220 // Display the dialog
222 XtManageChild ( _fileBrowser );
224 // Raise it, because it might be buried.
226 XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
229 void SelectFileCmd::fileSelectedCB(
231 XtPointer clientData,
234 SelectFileCmd * obj = (SelectFileCmd *) clientData;
235 XmFileSelectionBoxCallbackStruct *cb =
236 (XmFileSelectionBoxCallbackStruct *) callData;
239 // XtUnmanageChild ( w ); // Bring the file selection dialog down.
243 // Extract the first character string matching the default
244 // character set from the compound string
245 name = (char *) _XmStringUngenerate(
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;
261 // If a string was succesfully extracted, call
262 // fileSelected to handle the file.
264 XtUnmanageChild ( w ); // Bring the file selection dialog down.
265 obj->fileSelected ( name );
269 void SelectFileCmd::fileCanceledCB ( Widget w,
270 XtPointer clientData,
273 SelectFileCmd * obj = (SelectFileCmd *) clientData;
275 XtUnmanageChild(w); // Bring the file selection dialog down.
279 void SelectFileCmd::fileSelected ( char *filename )
282 _ok_callback ( _ok_clientData, filename );
285 void SelectFileCmd::fileCanceled ()
287 if ( _cancel_callback )
288 _cancel_callback ( _cancel_clientData, NULL );
292 SelectFileCmd::hiddenCB(Widget,
293 XtPointer client_data,
296 SelectFileCmd * self = (SelectFileCmd *)client_data;
297 XmToggleButtonCallbackStruct *cbs = (XmToggleButtonCallbackStruct*) cb_data;
299 self->doHidden(cbs->set);
303 SelectFileCmd::doHidden(int on)
306 style = (on) ? XmFILTER_NONE : XmFILTER_HIDDEN_FILES;
307 XtVaSetValues(_fileBrowser, XmNfileFilterStyle, style, NULL);
308 XmFileSelectionDoSearch(_fileBrowser, NULL);
312 SelectFileCmd::getDirectory()
317 if (NULL == _fileBrowser)
320 // Get the default selection.
321 XtVaGetValues(_fileBrowser, XmNdirectory, &directory, NULL);
323 _XmStringUngenerate(directory, NULL, XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
324 XmStringFree(directory);
330 SelectFileCmd::getSelected()
335 if (NULL == _fileBrowser)
338 // Set the default selection.
339 text = XtNameToWidget(_fileBrowser, "Text");
341 XtVaGetValues(text, XmNvalue, &path, NULL);
347 SelectFileCmd::getHidden()
350 XtArgVal current_state;
352 if (NULL == _fileBrowser || NULL == _hidden_button)
355 XtVaGetValues(_hidden_button, XmNset, ¤t_state, NULL);
356 val = (current_state == XmSET) ? 1 : 0;
361 SelectFileCmd::setDirectory(char *path)
365 if (NULL == _fileBrowser)
368 // Set the default directory where the file selection box points.
369 directory = XmStringCreateLocalized(path);
370 XtVaSetValues(_fileBrowser, XmNdirectory, directory, NULL);
371 XmStringFree(directory);
375 SelectFileCmd::setSelected(char *path)
379 if (NULL == _fileBrowser)
382 // Set the default selection.
383 text = XtNameToWidget(_fileBrowser, "Text");
385 XtVaSetValues(text, XmNvalue, path, NULL);
389 SelectFileCmd::setHidden(int on)
391 XtArgVal current_state;
392 XtArgVal desired_state;
394 if (NULL == _fileBrowser || NULL == _hidden_button)
397 desired_state = (on) ? XmSET : XmUNSET;
398 XtVaGetValues(_hidden_button, XmNset, ¤t_state, NULL);
399 if (current_state == desired_state)
402 XtVaSetValues(_hidden_button, XmNset, desired_state, NULL);