123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- /*
- * CDE - Common Desktop Environment
- *
- * Copyright (c) 1993-2012, The Open Group. All rights reserved.
- *
- * These libraries and programs are free software; you can
- * redistribute them and/or modify them under the terms of the GNU
- * Lesser General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * These libraries and programs are distributed in the hope that
- * they will be useful, but WITHOUT ANY WARRANTY; without even the
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Lesser General Public License for more
- * details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with these libraries and programs; if not, write
- * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
- * Floor, Boston, MA 02110-1301 USA
- */
- /* $TOG: SelectFileCmd.C /main/9 1998/10/26 17:57:37 mgreess $ */
- /*
- *+SNOTICE
- *
- * RESTRICTED CONFIDENTIAL INFORMATION:
- *
- * The information in this document is subject to special
- * restrictions in a confidential disclosure agreement bertween
- * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
- * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
- * Sun's specific written approval. This documment and all copies
- * and derivative works thereof must be returned or destroyed at
- * Sun's request.
- *
- * Copyright 1993, 1994, 1995 Sun Microsystems, Inc. All rights reserved.
- *
- *+ENOTICE
- */
- ///////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////
- // This example code is from the book:
- //
- // Object-Oriented Programming with C++ and OSF/Motif
- // by
- // Douglas Young
- // Prentice Hall, 1992
- // ISBN 0-13-630252-1
- //
- // Copyright 1991 by Prentice Hall
- // All Rights Reserved
- //
- // Permission to use, copy, modify, and distribute this software for
- // any purpose except publication and without fee is hereby granted, provided
- // that the above copyright notice appear in all copies of the software.
- ///////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////
- // SelectFileCmd.C:
- //////////////////////////////////////////////////////////
- #include <unistd.h>
- #include <stdlib.h>
- #include "SelectFileCmd.h"
- #include "Application.h"
- #include <Xm/FileSB.h>
- #include <X11/Intrinsic.h>
- #include "Help.hh"
- #include <nl_types.h>
- extern nl_catd catd;
- #include "NLS.hh"
- extern "C" {
- #include <Dt/HourGlass.h>
- }
- extern "C" {
- extern XtPointer _XmStringUngenerate (
- XmString string,
- XmStringTag tag,
- XmTextType tag_type,
- XmTextType output_type);
- }
- extern void forceUpdate( Widget );
- SelectFileCmd::SelectFileCmd (const char * name,
- const char * label,
- const char * title,
- const char * ok_label,
- int active,
- FileCallback ok_callback,
- void *ok_clientData,
- Widget parent) :
- NoUndoCmd ((char *)name, (char *)label, active )
- {
- _ok_label = (ok_label ? strdup(ok_label) : strdup("OK"));
- _title = (title ? strdup(title) : strdup(name));
- _ok_callback = ok_callback;
- _ok_clientData = ok_clientData;
- _cancel_callback = NULL;
- _cancel_clientData = NULL;
- _fileBrowser = NULL;
- _parentWidget = parent;
- _hidden_button = NULL;
- _directory = NULL;
- }
- SelectFileCmd::SelectFileCmd (const char * name,
- const char * label,
- const char * title,
- const char * ok_label,
- int active,
- FileCallback ok_callback,
- void *ok_clientData,
- FileCallback cancel_callback,
- void *cancel_clientData,
- Widget parent) :
- NoUndoCmd ((char *)name, (char *)label, active )
- {
- _ok_label = (ok_label ? strdup(ok_label) : strdup("OK"));
- _title = (title ? strdup(title) : strdup(name));
- _ok_callback = ok_callback;
- _ok_clientData = ok_clientData;
- _cancel_callback = cancel_callback;
- _cancel_clientData = cancel_clientData;
- _fileBrowser = NULL;
- _parentWidget = parent;
- _hidden_button = NULL;
- _directory = NULL;
- }
- SelectFileCmd::~SelectFileCmd()
- {
- free(_ok_label);
- free(_title);
- }
- void SelectFileCmd::doit()
- {
- // Create a FileSelectionBox widget
-
- Arg args[1];
- Cardinal n = 0;
- XmString title;
- // If the FSB already exists and is managed, raise it.
- if (_fileBrowser) {
- XtManageChild ( _fileBrowser );
- XtPopup (XtParent(_fileBrowser), XtGrabNone );
- XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
- forceUpdate(_fileBrowser);
- return;
- }
- // If there is no FSB, create it and manage it.
- // If there is one, just manage it.
- // Creating one is different from the book. cast required.
- // Also, its the "new CDE" FSB!
- if (!_fileBrowser) {
- _DtTurnOnHourGlass(_parentWidget);
- _fileBrowser =
- XmCreateFileSelectionDialog (_parentWidget,
- (char *) name(),
- args, n );
- // Set the title right...
- title = XmStringCreateLocalized(_title);
- XmString ok_str = XmStringCreateLocalized(_ok_label);
- XtVaSetValues(_fileBrowser,
- XmNdialogTitle, title,
- XmNokLabelString, ok_str,
- NULL);
- XmStringFree(title);
- XmStringFree(ok_str);
- XmString hidden_str = XmStringCreateLocalized(
- GETMSG(catd, 1, 11, "Show hidden folders and files"));
- _hidden_button = XtVaCreateManagedWidget(
- "hidden", xmToggleButtonWidgetClass,
- _fileBrowser,
- XmNlabelString, hidden_str,
- XmNalignment, XmALIGNMENT_BEGINNING,
- XmNnavigationType, XmSTICKY_TAB_GROUP,
- XmNsensitive, TRUE,
- NULL);
- XmStringFree(hidden_str);
- printHelpId("_fileBrowser", _fileBrowser);
- /* add help callback */
- // XtAddCallback(_fileBrowser, XmNhelpCallback, HelpCB, helpId);
-
- // Set up the callback to be called when the user chooses a file
-
- XtAddCallback ( _fileBrowser,
- XmNokCallback,
- &SelectFileCmd::fileSelectedCB,
- (XtPointer) this );
- XtAddCallback ( _fileBrowser,
- XmNcancelCallback,
- &SelectFileCmd::fileCanceledCB,
- (XtPointer) this );
- XtAddCallback(_hidden_button,
- XmNvalueChangedCallback,
- &SelectFileCmd::hiddenCB,
- this);
- XtSetSensitive(
- XmFileSelectionBoxGetChild(_fileBrowser, XmDIALOG_HELP_BUTTON),
- False);
- _DtTurnOffHourGlass(_parentWidget);
- }
-
- // Display the dialog
-
- XtManageChild ( _fileBrowser );
- // Raise it, because it might be buried.
- //
- XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
- }
- void SelectFileCmd::fileSelectedCB(
- Widget w,
- XtPointer clientData,
- XtPointer callData)
- {
- SelectFileCmd * obj = (SelectFileCmd *) clientData;
- XmFileSelectionBoxCallbackStruct *cb =
- (XmFileSelectionBoxCallbackStruct *) callData;
- char *name = NULL;
- // XtUnmanageChild ( w ); // Bring the file selection dialog down.
-
- if (cb->value)
- {
- // Extract the first character string matching the default
- // character set from the compound string
- name = (char *) _XmStringUngenerate(
- cb->value, NULL,
- XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
- // if the name is a null string ( no file is selected)
- // we should pop up an error dialog to let user know
- // that he/she did not specify file name. But because
- // the message cat file is frozen, we can not add any
- // new msg. Just free the name and return.
- // see aix defect 176761.
- if(NULL == name) return;
- if (strlen(name)<1)
- {
- free(name);
- return;
- }
- // If a string was successfully extracted, call
- // fileSelected to handle the file.
- XtUnmanageChild ( w ); // Bring the file selection dialog down.
- obj->fileSelected ( name );
- }
- }
- void SelectFileCmd::fileCanceledCB ( Widget w,
- XtPointer clientData,
- XtPointer )
- {
- SelectFileCmd * obj = (SelectFileCmd *) clientData;
- XtUnmanageChild(w); // Bring the file selection dialog down.
- obj->fileCanceled();
- }
- void SelectFileCmd::fileSelected ( char *filename )
- {
- if ( _ok_callback )
- _ok_callback ( _ok_clientData, filename );
- }
- void SelectFileCmd::fileCanceled ()
- {
- if ( _cancel_callback )
- _cancel_callback ( _cancel_clientData, NULL );
- }
- void
- SelectFileCmd::hiddenCB(Widget,
- XtPointer client_data,
- XtPointer cb_data)
- {
- SelectFileCmd * self = (SelectFileCmd *)client_data;
- XmToggleButtonCallbackStruct *cbs = (XmToggleButtonCallbackStruct*) cb_data;
- self->doHidden(cbs->set);
- }
- void
- SelectFileCmd::doHidden(int on)
- {
- XtEnum style;
- style = (on) ? XmFILTER_NONE : XmFILTER_HIDDEN_FILES;
- XtVaSetValues(_fileBrowser, XmNfileFilterStyle, style, NULL);
- XmFileSelectionDoSearch(_fileBrowser, NULL);
- }
- char *
- SelectFileCmd::getDirectory()
- {
- XmString directory;
- char *path;
- if (NULL == _fileBrowser)
- return NULL;
- // Get the default selection.
- XtVaGetValues(_fileBrowser, XmNdirectory, &directory, NULL);
- path = (char *)
- _XmStringUngenerate(directory, NULL, XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
- XmStringFree(directory);
- return path;
- }
- char *
- SelectFileCmd::getSelected()
- {
- Widget text;
- char *path = NULL;
- if (NULL == _fileBrowser)
- return NULL;
- // Set the default selection.
- text = XtNameToWidget(_fileBrowser, "Text");
- if (NULL != text)
- XtVaGetValues(text, XmNvalue, &path, NULL);
- return path;
- }
- int
- SelectFileCmd::getHidden()
- {
- int val;
- XtArgVal current_state;
- if (NULL == _fileBrowser || NULL == _hidden_button)
- return 0;
-
- XtVaGetValues(_hidden_button, XmNset, ¤t_state, NULL);
- val = (current_state == XmSET) ? 1 : 0;
- return val;
- }
- void
- SelectFileCmd::setDirectory(char *path)
- {
- XmString directory;
- if (NULL == _fileBrowser)
- return;
- // Set the default directory where the file selection box points.
- directory = XmStringCreateLocalized(path);
- XtVaSetValues(_fileBrowser, XmNdirectory, directory, NULL);
- XmStringFree(directory);
- }
- void
- SelectFileCmd::setSelected(char *path)
- {
- Widget text;
- if (NULL == _fileBrowser)
- return;
- // Set the default selection.
- text = XtNameToWidget(_fileBrowser, "Text");
- if (NULL != text)
- XtVaSetValues(text, XmNvalue, path, NULL);
- }
- void
- SelectFileCmd::setHidden(int on)
- {
- XtArgVal current_state;
- XtArgVal desired_state;
- if (NULL == _fileBrowser || NULL == _hidden_button)
- return;
-
- desired_state = (on) ? XmSET : XmUNSET;
- XtVaGetValues(_hidden_button, XmNset, ¤t_state, NULL);
- if (current_state == desired_state)
- return;
- XtVaSetValues(_hidden_button, XmNset, desired_state, NULL);
- doHidden(on);
- }
|