filedlg.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: src/gtk/filedlg.cpp
  3. // Purpose: native implementation of wxFileDialog
  4. // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp
  5. // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. // For compilers that support precompilation, includes "wx.h".
  9. #include "wx/wxprec.h"
  10. #if wxUSE_FILEDLG
  11. #include "wx/filedlg.h"
  12. #ifndef WX_PRECOMP
  13. #include "wx/intl.h"
  14. #include "wx/msgdlg.h"
  15. #endif
  16. #include <gtk/gtk.h>
  17. #include "wx/gtk/private.h"
  18. #ifdef __UNIX__
  19. #include <unistd.h> // chdir
  20. #endif
  21. #include "wx/filename.h" // wxFilename
  22. #include "wx/tokenzr.h" // wxStringTokenizer
  23. #include "wx/filefn.h" // ::wxGetCwd
  24. #include "wx/modalhook.h"
  25. //-----------------------------------------------------------------------------
  26. // "clicked" for OK-button
  27. //-----------------------------------------------------------------------------
  28. extern "C" {
  29. static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
  30. {
  31. int style = dialog->GetWindowStyle();
  32. wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)));
  33. // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
  34. #ifndef __WXGTK3__
  35. #if GTK_CHECK_VERSION(2,7,3)
  36. if (gtk_check_version(2, 7, 3) != NULL)
  37. #endif
  38. {
  39. if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT))
  40. {
  41. if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
  42. {
  43. wxString msg;
  44. msg.Printf(
  45. _("File '%s' already exists, do you really want to overwrite it?"),
  46. wxString::FromUTF8(filename));
  47. wxMessageDialog dlg(dialog, msg, _("Confirm"),
  48. wxYES_NO | wxICON_QUESTION);
  49. if (dlg.ShowModal() != wxID_YES)
  50. return;
  51. }
  52. }
  53. }
  54. #endif
  55. if (style & wxFD_FILE_MUST_EXIST)
  56. {
  57. if ( !g_file_test(filename, G_FILE_TEST_EXISTS) )
  58. {
  59. wxMessageDialog dlg( dialog, _("Please choose an existing file."),
  60. _("Error"), wxOK| wxICON_ERROR);
  61. dlg.ShowModal();
  62. return;
  63. }
  64. }
  65. // change to the directory where the user went if asked
  66. if (style & wxFD_CHANGE_DIR)
  67. {
  68. // Use chdir to not care about filename encodings
  69. wxGtkString folder(g_path_get_dirname(filename));
  70. chdir(folder);
  71. }
  72. wxCommandEvent event(wxEVT_BUTTON, wxID_OK);
  73. event.SetEventObject(dialog);
  74. dialog->HandleWindowEvent(event);
  75. }
  76. }
  77. //-----------------------------------------------------------------------------
  78. // "clicked" for Cancel-button
  79. //-----------------------------------------------------------------------------
  80. extern "C"
  81. {
  82. static void
  83. gtk_filedialog_cancel_callback(GtkWidget * WXUNUSED(w), wxFileDialog *dialog)
  84. {
  85. wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
  86. event.SetEventObject(dialog);
  87. dialog->HandleWindowEvent(event);
  88. }
  89. static void gtk_filedialog_response_callback(GtkWidget *w,
  90. gint response,
  91. wxFileDialog *dialog)
  92. {
  93. if (response == GTK_RESPONSE_ACCEPT)
  94. gtk_filedialog_ok_callback(w, dialog);
  95. else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
  96. gtk_filedialog_cancel_callback(w, dialog);
  97. }
  98. static void gtk_filedialog_selchanged_callback(GtkFileChooser *chooser,
  99. wxFileDialog *dialog)
  100. {
  101. wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
  102. dialog->GTKSelectionChanged(wxString::FromUTF8(filename));
  103. }
  104. static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser,
  105. gpointer user_data)
  106. {
  107. GtkWidget *preview = GTK_WIDGET(user_data);
  108. wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
  109. if ( !filename )
  110. return;
  111. GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL);
  112. gboolean have_preview = pixbuf != NULL;
  113. gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf);
  114. if ( pixbuf )
  115. g_object_unref (pixbuf);
  116. gtk_file_chooser_set_preview_widget_active(chooser, have_preview);
  117. }
  118. } // extern "C"
  119. void wxFileDialog::AddChildGTK(wxWindowGTK* child)
  120. {
  121. // allow dialog to be resized smaller horizontally
  122. gtk_widget_set_size_request(
  123. child->m_widget, child->GetMinWidth(), child->m_height);
  124. gtk_file_chooser_set_extra_widget(
  125. GTK_FILE_CHOOSER(m_widget), child->m_widget);
  126. }
  127. //-----------------------------------------------------------------------------
  128. // wxFileDialog
  129. //-----------------------------------------------------------------------------
  130. IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase)
  131. BEGIN_EVENT_TABLE(wxFileDialog,wxFileDialogBase)
  132. EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
  133. EVT_SIZE(wxFileDialog::OnSize)
  134. END_EVENT_TABLE()
  135. wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
  136. const wxString& defaultDir,
  137. const wxString& defaultFileName,
  138. const wxString& wildCard,
  139. long style, const wxPoint& pos,
  140. const wxSize& sz,
  141. const wxString& name)
  142. : wxFileDialogBase()
  143. {
  144. Create(parent, message, defaultDir, defaultFileName, wildCard, style, pos, sz, name);
  145. }
  146. bool wxFileDialog::Create(wxWindow *parent, const wxString& message,
  147. const wxString& defaultDir,
  148. const wxString& defaultFileName,
  149. const wxString& wildCard,
  150. long style, const wxPoint& pos,
  151. const wxSize& sz,
  152. const wxString& name)
  153. {
  154. parent = GetParentForModalDialog(parent, style);
  155. if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName,
  156. wildCard, style, pos, sz, name))
  157. {
  158. return false;
  159. }
  160. if (!PreCreation(parent, pos, wxDefaultSize) ||
  161. !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
  162. wxDefaultValidator, wxT("filedialog")))
  163. {
  164. wxFAIL_MSG( wxT("wxFileDialog creation failed") );
  165. return false;
  166. }
  167. GtkFileChooserAction gtk_action;
  168. GtkWindow* gtk_parent = NULL;
  169. if (parent)
  170. gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
  171. const gchar* ok_btn_stock;
  172. if ( style & wxFD_SAVE )
  173. {
  174. gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
  175. ok_btn_stock = GTK_STOCK_SAVE;
  176. }
  177. else
  178. {
  179. gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
  180. ok_btn_stock = GTK_STOCK_OPEN;
  181. }
  182. m_widget = gtk_file_chooser_dialog_new(
  183. wxGTK_CONV(m_message),
  184. gtk_parent,
  185. gtk_action,
  186. GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
  187. ok_btn_stock, GTK_RESPONSE_ACCEPT,
  188. NULL);
  189. g_object_ref(m_widget);
  190. GtkFileChooser* file_chooser = GTK_FILE_CHOOSER(m_widget);
  191. m_fc.SetWidget(file_chooser);
  192. gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
  193. if ( style & wxFD_MULTIPLE )
  194. gtk_file_chooser_set_select_multiple(file_chooser, true);
  195. // local-only property could be set to false to allow non-local files to be
  196. // loaded. In that case get/set_uri(s) should be used instead of
  197. // get/set_filename(s) everywhere and the GtkFileChooserDialog should
  198. // probably also be created with a backend, e.g. "gnome-vfs", "default", ...
  199. // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
  200. // as the default - true:
  201. // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
  202. g_signal_connect (m_widget, "response",
  203. G_CALLBACK (gtk_filedialog_response_callback), this);
  204. g_signal_connect (m_widget, "selection-changed",
  205. G_CALLBACK (gtk_filedialog_selchanged_callback), this);
  206. // deal with extensions/filters
  207. SetWildcard(wildCard);
  208. wxString defaultFileNameWithExt = defaultFileName;
  209. if ( !wildCard.empty() && !defaultFileName.empty() &&
  210. !wxFileName(defaultFileName).HasExt() )
  211. {
  212. // append the default extension, if any, to the initial file name: GTK
  213. // won't do it for us by default (unlike e.g. MSW)
  214. const wxFileName fnWC(m_fc.GetCurrentWildCard());
  215. if ( fnWC.HasExt() )
  216. {
  217. // Notice that we shouldn't append the extension if it's a wildcard
  218. // because this is not useful: the user would need to change it to use
  219. // some fixed extension anyhow.
  220. const wxString& ext = fnWC.GetExt();
  221. if ( ext.find_first_of("?*") == wxString::npos )
  222. defaultFileNameWithExt << "." << ext;
  223. }
  224. }
  225. // if defaultDir is specified it should contain the directory and
  226. // defaultFileName should contain the default name of the file, however if
  227. // directory is not given, defaultFileName contains both
  228. wxFileName fn;
  229. if ( defaultDir.empty() )
  230. fn.Assign(defaultFileNameWithExt);
  231. else if ( !defaultFileNameWithExt.empty() )
  232. fn.Assign(defaultDir, defaultFileNameWithExt);
  233. else
  234. fn.AssignDir(defaultDir);
  235. // set the initial file name and/or directory
  236. fn.MakeAbsolute(); // GTK+ needs absolute path
  237. const wxString dir = fn.GetPath();
  238. if ( !dir.empty() )
  239. {
  240. gtk_file_chooser_set_current_folder(file_chooser, wxGTK_CONV_FN(dir));
  241. }
  242. const wxString fname = fn.GetFullName();
  243. if ( style & wxFD_SAVE )
  244. {
  245. if ( !fname.empty() )
  246. {
  247. gtk_file_chooser_set_current_name(file_chooser, wxGTK_CONV_FN(fname));
  248. }
  249. #if GTK_CHECK_VERSION(2,7,3)
  250. if ((style & wxFD_OVERWRITE_PROMPT)
  251. #ifndef __WXGTK3__
  252. && gtk_check_version(2,7,3) == NULL
  253. #endif
  254. )
  255. {
  256. gtk_file_chooser_set_do_overwrite_confirmation(file_chooser, true);
  257. }
  258. #endif
  259. }
  260. else // wxFD_OPEN
  261. {
  262. if ( !fname.empty() )
  263. {
  264. gtk_file_chooser_set_filename(file_chooser,
  265. wxGTK_CONV_FN(fn.GetFullPath()));
  266. }
  267. }
  268. if ( style & wxFD_PREVIEW )
  269. {
  270. GtkWidget *previewImage = gtk_image_new();
  271. gtk_file_chooser_set_preview_widget(file_chooser, previewImage);
  272. g_signal_connect(m_widget, "update-preview",
  273. G_CALLBACK(gtk_filedialog_update_preview_callback),
  274. previewImage);
  275. }
  276. return true;
  277. }
  278. wxFileDialog::~wxFileDialog()
  279. {
  280. if (m_extraControl)
  281. {
  282. // get chooser to drop its reference right now, allowing wxWindow dtor
  283. // to verify that ref count drops to zero
  284. gtk_file_chooser_set_extra_widget(
  285. GTK_FILE_CHOOSER(m_widget), NULL);
  286. }
  287. }
  288. void wxFileDialog::OnFakeOk(wxCommandEvent& WXUNUSED(event))
  289. {
  290. // Update the current directory from here, accessing it later may not work
  291. // due to the strange way GtkFileChooser works.
  292. wxGtkString
  293. str(gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(m_widget)));
  294. m_dir = wxString::FromUTF8(str);
  295. EndDialog(wxID_OK);
  296. }
  297. int wxFileDialog::ShowModal()
  298. {
  299. WX_HOOK_MODAL_DIALOG();
  300. CreateExtraControl();
  301. return wxDialog::ShowModal();
  302. }
  303. void wxFileDialog::DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
  304. int WXUNUSED(width), int WXUNUSED(height),
  305. int WXUNUSED(sizeFlags))
  306. {
  307. }
  308. void wxFileDialog::OnSize(wxSizeEvent&)
  309. {
  310. // avoid calling DoLayout(), which will set the (wrong) size of
  311. // m_extraControl, its size is managed by GtkFileChooser
  312. }
  313. wxString wxFileDialog::GetPath() const
  314. {
  315. return m_fc.GetPath();
  316. }
  317. void wxFileDialog::GetFilenames(wxArrayString& files) const
  318. {
  319. m_fc.GetFilenames( files );
  320. }
  321. void wxFileDialog::GetPaths(wxArrayString& paths) const
  322. {
  323. m_fc.GetPaths( paths );
  324. }
  325. void wxFileDialog::SetMessage(const wxString& message)
  326. {
  327. m_message = message;
  328. SetTitle(message);
  329. }
  330. void wxFileDialog::SetPath(const wxString& path)
  331. {
  332. wxFileDialogBase::SetPath(path);
  333. // Don't do anything if no path is specified, in particular don't set the
  334. // path to m_dir below as this would result in opening the dialog in the
  335. // parent directory of this one instead of m_dir itself.
  336. if ( path.empty() )
  337. return;
  338. // we need an absolute path for GTK native chooser so ensure that we have
  339. // it: use the initial directory if it was set or just CWD otherwise (this
  340. // is the default behaviour if m_dir is empty)
  341. wxFileName fn(path);
  342. fn.MakeAbsolute(m_dir);
  343. m_fc.SetPath(fn.GetFullPath());
  344. }
  345. void wxFileDialog::SetDirectory(const wxString& dir)
  346. {
  347. wxFileDialogBase::SetDirectory(dir);
  348. m_fc.SetDirectory(dir);
  349. }
  350. void wxFileDialog::SetFilename(const wxString& name)
  351. {
  352. wxFileDialogBase::SetFilename(name);
  353. if (HasFdFlag(wxFD_SAVE))
  354. {
  355. gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
  356. }
  357. else
  358. {
  359. wxString path( GetDirectory() );
  360. if (path.empty())
  361. {
  362. // SetPath() fires an assert if fed other than filepaths
  363. return;
  364. }
  365. SetPath(wxFileName(path, name).GetFullPath());
  366. }
  367. }
  368. wxString wxFileDialog::GetFilename() const
  369. {
  370. wxString currentFilename( m_fc.GetFilename() );
  371. if (currentFilename.empty())
  372. {
  373. // m_fc.GetFilename() will return empty until the dialog has been shown
  374. // in which case use any previously provided value
  375. currentFilename = m_fileName;
  376. }
  377. return currentFilename;
  378. }
  379. void wxFileDialog::SetWildcard(const wxString& wildCard)
  380. {
  381. wxFileDialogBase::SetWildcard(wildCard);
  382. m_fc.SetWildcard( GetWildcard() );
  383. }
  384. void wxFileDialog::SetFilterIndex(int filterIndex)
  385. {
  386. m_fc.SetFilterIndex( filterIndex);
  387. }
  388. int wxFileDialog::GetFilterIndex() const
  389. {
  390. return m_fc.GetFilterIndex();
  391. }
  392. void wxFileDialog::GTKSelectionChanged(const wxString& filename)
  393. {
  394. m_currentlySelectedFilename = filename;
  395. if (m_extraControl)
  396. m_extraControl->UpdateWindowUI(wxUPDATE_UI_RECURSE);
  397. }
  398. #endif // wxUSE_FILEDLG