SelectFileCmd.C 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. // This example code is from the book:
  44. //
  45. // Object-Oriented Programming with C++ and OSF/Motif
  46. // by
  47. // Douglas Young
  48. // Prentice Hall, 1992
  49. // ISBN 0-13-630252-1
  50. //
  51. // Copyright 1991 by Prentice Hall
  52. // All Rights Reserved
  53. //
  54. // Permission to use, copy, modify, and distribute this software for
  55. // any purpose except publication and without fee is hereby granted, provided
  56. // that the above copyright notice appear in all copies of the software.
  57. ///////////////////////////////////////////////////////////////////////////////
  58. //////////////////////////////////////////////////////////////////////////////
  59. //////////////////////////////////////////////////////////
  60. // SelectFileCmd.C:
  61. //////////////////////////////////////////////////////////
  62. #include <unistd.h>
  63. #include <stdlib.h>
  64. #include "SelectFileCmd.h"
  65. #include "Application.h"
  66. #include <Xm/FileSB.h>
  67. #include <X11/Intrinsic.h>
  68. #include "Help.hh"
  69. #include <nl_types.h>
  70. extern nl_catd catd;
  71. #include "NLS.hh"
  72. extern "C" {
  73. #include <Dt/HourGlass.h>
  74. }
  75. extern "C" {
  76. extern XtPointer _XmStringUngenerate (
  77. XmString string,
  78. XmStringTag tag,
  79. XmTextType tag_type,
  80. XmTextType output_type);
  81. }
  82. extern void forceUpdate( Widget );
  83. SelectFileCmd::SelectFileCmd (const char * name,
  84. const char * label,
  85. const char * title,
  86. const char * ok_label,
  87. int active,
  88. FileCallback ok_callback,
  89. void *ok_clientData,
  90. Widget parent) :
  91. NoUndoCmd ((char *)name, (char *)label, active )
  92. {
  93. _ok_label = (ok_label ? strdup(ok_label) : strdup("OK"));
  94. _title = (title ? strdup(title) : strdup(name));
  95. _ok_callback = ok_callback;
  96. _ok_clientData = ok_clientData;
  97. _cancel_callback = NULL;
  98. _cancel_clientData = NULL;
  99. _fileBrowser = NULL;
  100. _parentWidget = parent;
  101. _hidden_button = NULL;
  102. _directory = NULL;
  103. }
  104. SelectFileCmd::SelectFileCmd (const char * name,
  105. const char * label,
  106. const char * title,
  107. const char * ok_label,
  108. int active,
  109. FileCallback ok_callback,
  110. void *ok_clientData,
  111. FileCallback cancel_callback,
  112. void *cancel_clientData,
  113. Widget parent) :
  114. NoUndoCmd ((char *)name, (char *)label, active )
  115. {
  116. _ok_label = (ok_label ? strdup(ok_label) : strdup("OK"));
  117. _title = (title ? strdup(title) : strdup(name));
  118. _ok_callback = ok_callback;
  119. _ok_clientData = ok_clientData;
  120. _cancel_callback = cancel_callback;
  121. _cancel_clientData = cancel_clientData;
  122. _fileBrowser = NULL;
  123. _parentWidget = parent;
  124. _hidden_button = NULL;
  125. _directory = NULL;
  126. }
  127. SelectFileCmd::~SelectFileCmd()
  128. {
  129. free(_ok_label);
  130. free(_title);
  131. }
  132. void SelectFileCmd::doit()
  133. {
  134. // Create a FileSelectionBox widget
  135. Arg args[1];
  136. Cardinal n = 0;
  137. XmString title;
  138. // If the FSB already exists and is managed, raise it.
  139. if (_fileBrowser) {
  140. XtManageChild ( _fileBrowser );
  141. XtPopup (XtParent(_fileBrowser), XtGrabNone );
  142. XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
  143. forceUpdate(_fileBrowser);
  144. return;
  145. }
  146. // If there is no FSB, create it and manage it.
  147. // If there is one, just manage it.
  148. // Creating one is different from the book. cast required.
  149. // Also, its the "new CDE" FSB!
  150. if (!_fileBrowser) {
  151. _DtTurnOnHourGlass(_parentWidget);
  152. _fileBrowser =
  153. XmCreateFileSelectionDialog (_parentWidget,
  154. (char *) name(),
  155. args, n );
  156. // Set the title right...
  157. title = XmStringCreateLocalized(_title);
  158. XmString ok_str = XmStringCreateLocalized(_ok_label);
  159. XtVaSetValues(_fileBrowser,
  160. XmNdialogTitle, title,
  161. XmNokLabelString, ok_str,
  162. NULL);
  163. XmStringFree(title);
  164. XmStringFree(ok_str);
  165. XmString hidden_str = XmStringCreateLocalized(
  166. GETMSG(catd, 1, 11, "Show hidden folders and files"));
  167. _hidden_button = XtVaCreateManagedWidget(
  168. "hidden", xmToggleButtonWidgetClass,
  169. _fileBrowser,
  170. XmNlabelString, hidden_str,
  171. XmNalignment, XmALIGNMENT_BEGINNING,
  172. XmNnavigationType, XmSTICKY_TAB_GROUP,
  173. XmNsensitive, TRUE,
  174. NULL);
  175. XmStringFree(hidden_str);
  176. printHelpId("_fileBrowser", _fileBrowser);
  177. /* add help callback */
  178. // XtAddCallback(_fileBrowser, XmNhelpCallback, HelpCB, helpId);
  179. // Set up the callback to be called when the user chooses a file
  180. XtAddCallback ( _fileBrowser,
  181. XmNokCallback,
  182. &SelectFileCmd::fileSelectedCB,
  183. (XtPointer) this );
  184. XtAddCallback ( _fileBrowser,
  185. XmNcancelCallback,
  186. &SelectFileCmd::fileCanceledCB,
  187. (XtPointer) this );
  188. XtAddCallback(_hidden_button,
  189. XmNvalueChangedCallback,
  190. &SelectFileCmd::hiddenCB,
  191. this);
  192. XtSetSensitive(
  193. XmFileSelectionBoxGetChild(_fileBrowser, XmDIALOG_HELP_BUTTON),
  194. False);
  195. _DtTurnOffHourGlass(_parentWidget);
  196. }
  197. // Display the dialog
  198. XtManageChild ( _fileBrowser );
  199. // Raise it, because it might be buried.
  200. //
  201. XRaiseWindow(XtDisplay(_fileBrowser), XtWindow(XtParent(_fileBrowser)));
  202. }
  203. void SelectFileCmd::fileSelectedCB(
  204. Widget w,
  205. XtPointer clientData,
  206. XtPointer callData)
  207. {
  208. SelectFileCmd * obj = (SelectFileCmd *) clientData;
  209. XmFileSelectionBoxCallbackStruct *cb =
  210. (XmFileSelectionBoxCallbackStruct *) callData;
  211. char *name = NULL;
  212. // XtUnmanageChild ( w ); // Bring the file selection dialog down.
  213. if (cb->value)
  214. {
  215. // Extract the first character string matching the default
  216. // character set from the compound string
  217. name = (char *) _XmStringUngenerate(
  218. cb->value, NULL,
  219. XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
  220. // if the name is a null string ( no file is selected)
  221. // we should pop up an error dialog to let user know
  222. // that he/she did not specify file name. But because
  223. // the message cat file is frozen, we can not add any
  224. // new msg. Just free the name and return.
  225. // see aix defect 176761.
  226. if(NULL == name) return;
  227. if (strlen(name)<1)
  228. {
  229. free(name);
  230. return;
  231. }
  232. // If a string was successfully extracted, call
  233. // fileSelected to handle the file.
  234. XtUnmanageChild ( w ); // Bring the file selection dialog down.
  235. obj->fileSelected ( name );
  236. }
  237. }
  238. void SelectFileCmd::fileCanceledCB ( Widget w,
  239. XtPointer clientData,
  240. XtPointer )
  241. {
  242. SelectFileCmd * obj = (SelectFileCmd *) clientData;
  243. XtUnmanageChild(w); // Bring the file selection dialog down.
  244. obj->fileCanceled();
  245. }
  246. void SelectFileCmd::fileSelected ( char *filename )
  247. {
  248. if ( _ok_callback )
  249. _ok_callback ( _ok_clientData, filename );
  250. }
  251. void SelectFileCmd::fileCanceled ()
  252. {
  253. if ( _cancel_callback )
  254. _cancel_callback ( _cancel_clientData, NULL );
  255. }
  256. void
  257. SelectFileCmd::hiddenCB(Widget,
  258. XtPointer client_data,
  259. XtPointer cb_data)
  260. {
  261. SelectFileCmd * self = (SelectFileCmd *)client_data;
  262. XmToggleButtonCallbackStruct *cbs = (XmToggleButtonCallbackStruct*) cb_data;
  263. self->doHidden(cbs->set);
  264. }
  265. void
  266. SelectFileCmd::doHidden(int on)
  267. {
  268. XtEnum style;
  269. style = (on) ? XmFILTER_NONE : XmFILTER_HIDDEN_FILES;
  270. XtVaSetValues(_fileBrowser, XmNfileFilterStyle, style, NULL);
  271. XmFileSelectionDoSearch(_fileBrowser, NULL);
  272. }
  273. char *
  274. SelectFileCmd::getDirectory()
  275. {
  276. XmString directory;
  277. char *path;
  278. if (NULL == _fileBrowser)
  279. return NULL;
  280. // Get the default selection.
  281. XtVaGetValues(_fileBrowser, XmNdirectory, &directory, NULL);
  282. path = (char *)
  283. _XmStringUngenerate(directory, NULL, XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
  284. XmStringFree(directory);
  285. return path;
  286. }
  287. char *
  288. SelectFileCmd::getSelected()
  289. {
  290. Widget text;
  291. char *path = NULL;
  292. if (NULL == _fileBrowser)
  293. return NULL;
  294. // Set the default selection.
  295. text = XtNameToWidget(_fileBrowser, "Text");
  296. if (NULL != text)
  297. XtVaGetValues(text, XmNvalue, &path, NULL);
  298. return path;
  299. }
  300. int
  301. SelectFileCmd::getHidden()
  302. {
  303. int val;
  304. XtArgVal current_state;
  305. if (NULL == _fileBrowser || NULL == _hidden_button)
  306. return 0;
  307. XtVaGetValues(_hidden_button, XmNset, &current_state, NULL);
  308. val = (current_state == XmSET) ? 1 : 0;
  309. return val;
  310. }
  311. void
  312. SelectFileCmd::setDirectory(char *path)
  313. {
  314. XmString directory;
  315. if (NULL == _fileBrowser)
  316. return;
  317. // Set the default directory where the file selection box points.
  318. directory = XmStringCreateLocalized(path);
  319. XtVaSetValues(_fileBrowser, XmNdirectory, directory, NULL);
  320. XmStringFree(directory);
  321. }
  322. void
  323. SelectFileCmd::setSelected(char *path)
  324. {
  325. Widget text;
  326. if (NULL == _fileBrowser)
  327. return;
  328. // Set the default selection.
  329. text = XtNameToWidget(_fileBrowser, "Text");
  330. if (NULL != text)
  331. XtVaSetValues(text, XmNvalue, path, NULL);
  332. }
  333. void
  334. SelectFileCmd::setHidden(int on)
  335. {
  336. XtArgVal current_state;
  337. XtArgVal desired_state;
  338. if (NULL == _fileBrowser || NULL == _hidden_button)
  339. return;
  340. desired_state = (on) ? XmSET : XmUNSET;
  341. XtVaGetValues(_hidden_button, XmNset, &current_state, NULL);
  342. if (current_state == desired_state)
  343. return;
  344. XtVaSetValues(_hidden_button, XmNset, desired_state, NULL);
  345. doHidden(on);
  346. }