| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/gtk/filedlg.cpp |
| 3 | // Purpose: native implementation of wxFileDialog |
| 4 | // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp |
| 5 | // Id: $Id$ |
| 6 | // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | // For compilers that support precompilation, includes "wx.h". |
| 11 | #include "wx/wxprec.h" |
| 12 | |
| 13 | #if wxUSE_FILEDLG && defined(__WXGTK24__) |
| 14 | |
| 15 | #include "wx/filedlg.h" |
| 16 | |
| 17 | #ifndef WX_PRECOMP |
| 18 | #include "wx/intl.h" |
| 19 | #include "wx/msgdlg.h" |
| 20 | #endif |
| 21 | |
| 22 | #include <gtk/gtk.h> |
| 23 | #include "wx/gtk/private.h" |
| 24 | |
| 25 | #include <unistd.h> // chdir |
| 26 | |
| 27 | #include "wx/filename.h" // wxFilename |
| 28 | #include "wx/tokenzr.h" // wxStringTokenizer |
| 29 | #include "wx/filefn.h" // ::wxGetCwd |
| 30 | |
| 31 | //----------------------------------------------------------------------------- |
| 32 | // "clicked" for OK-button |
| 33 | //----------------------------------------------------------------------------- |
| 34 | |
| 35 | extern "C" { |
| 36 | static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog) |
| 37 | { |
| 38 | int style = dialog->GetWindowStyle(); |
| 39 | wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget))); |
| 40 | |
| 41 | // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation) |
| 42 | #if GTK_CHECK_VERSION(2,7,3) |
| 43 | if(gtk_check_version(2,7,3) != NULL) |
| 44 | #endif |
| 45 | if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT)) |
| 46 | { |
| 47 | if ( g_file_test(filename, G_FILE_TEST_EXISTS) ) |
| 48 | { |
| 49 | wxString msg; |
| 50 | |
| 51 | msg.Printf( |
| 52 | _("File '%s' already exists, do you really want to overwrite it?"), |
| 53 | wxString(wxConvFileName->cMB2WX(filename)).c_str()); |
| 54 | |
| 55 | wxMessageDialog dlg(dialog, msg, _("Confirm"), |
| 56 | wxYES_NO | wxICON_QUESTION); |
| 57 | if (dlg.ShowModal() != wxID_YES) |
| 58 | return; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (style & wxFD_FILE_MUST_EXIST) |
| 63 | { |
| 64 | if ( !g_file_test(filename, G_FILE_TEST_EXISTS) ) |
| 65 | { |
| 66 | wxMessageDialog dlg( dialog, _("Please choose an existing file."), |
| 67 | _("Error"), wxOK| wxICON_ERROR); |
| 68 | dlg.ShowModal(); |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // change to the directory where the user went if asked |
| 74 | if (style & wxFD_CHANGE_DIR) |
| 75 | { |
| 76 | // Use chdir to not care about filename encodings |
| 77 | wxGtkString folder(g_path_get_dirname(filename)); |
| 78 | chdir(folder); |
| 79 | } |
| 80 | |
| 81 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK); |
| 82 | event.SetEventObject(dialog); |
| 83 | dialog->GetEventHandler()->ProcessEvent(event); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | //----------------------------------------------------------------------------- |
| 88 | // "clicked" for Cancel-button |
| 89 | //----------------------------------------------------------------------------- |
| 90 | |
| 91 | extern "C" |
| 92 | { |
| 93 | |
| 94 | static void gtk_filedialog_cancel_callback(GtkWidget *w, wxFileDialog *dialog) |
| 95 | { |
| 96 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
| 97 | event.SetEventObject(dialog); |
| 98 | dialog->GetEventHandler()->ProcessEvent(event); |
| 99 | } |
| 100 | |
| 101 | static void gtk_filedialog_response_callback(GtkWidget *w, |
| 102 | gint response, |
| 103 | wxFileDialog *dialog) |
| 104 | { |
| 105 | if (response == GTK_RESPONSE_ACCEPT) |
| 106 | gtk_filedialog_ok_callback(w, dialog); |
| 107 | else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE |
| 108 | gtk_filedialog_cancel_callback(w, dialog); |
| 109 | } |
| 110 | |
| 111 | static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser, |
| 112 | gpointer user_data) |
| 113 | { |
| 114 | #if GTK_CHECK_VERSION(2,4,0) |
| 115 | GtkWidget *preview = GTK_WIDGET(user_data); |
| 116 | |
| 117 | wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser)); |
| 118 | |
| 119 | if ( !filename ) |
| 120 | return; |
| 121 | |
| 122 | GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL); |
| 123 | gboolean have_preview = pixbuf != NULL; |
| 124 | |
| 125 | gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf); |
| 126 | if ( pixbuf ) |
| 127 | g_object_unref (pixbuf); |
| 128 | |
| 129 | gtk_file_chooser_set_preview_widget_active(chooser, have_preview); |
| 130 | #else |
| 131 | wxUnusedVar(chooser); |
| 132 | wxUnusedVar(user_data); |
| 133 | #endif // GTK+ 2.4+ |
| 134 | } |
| 135 | |
| 136 | } // extern "C" |
| 137 | |
| 138 | |
| 139 | //----------------------------------------------------------------------------- |
| 140 | // wxFileDialog |
| 141 | //----------------------------------------------------------------------------- |
| 142 | |
| 143 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxGenericFileDialog) |
| 144 | |
| 145 | BEGIN_EVENT_TABLE(wxFileDialog,wxGenericFileDialog) |
| 146 | EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk) |
| 147 | END_EVENT_TABLE() |
| 148 | |
| 149 | wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message, |
| 150 | const wxString& defaultDir, |
| 151 | const wxString& defaultFileName, |
| 152 | const wxString& wildCard, |
| 153 | long style, const wxPoint& pos, |
| 154 | const wxSize& sz, |
| 155 | const wxString& name) |
| 156 | : wxGenericFileDialog(parent, message, defaultDir, defaultFileName, |
| 157 | wildCard, style, pos, sz, name, true ) |
| 158 | { |
| 159 | if (gtk_check_version(2,4,0)) |
| 160 | { |
| 161 | wxGenericFileDialog::Create( parent, message, defaultDir, |
| 162 | defaultFileName, wildCard, style, pos ); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | parent = GetParentForModalDialog(parent); |
| 167 | |
| 168 | if (!PreCreation(parent, pos, wxDefaultSize) || |
| 169 | !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style, |
| 170 | wxDefaultValidator, wxT("filedialog"))) |
| 171 | { |
| 172 | wxFAIL_MSG( wxT("wxFileDialog creation failed") ); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | GtkFileChooserAction gtk_action; |
| 177 | GtkWindow* gtk_parent = NULL; |
| 178 | if (parent) |
| 179 | gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) ); |
| 180 | |
| 181 | const gchar* ok_btn_stock; |
| 182 | if ( style & wxFD_SAVE ) |
| 183 | { |
| 184 | gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE; |
| 185 | ok_btn_stock = GTK_STOCK_SAVE; |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN; |
| 190 | ok_btn_stock = GTK_STOCK_OPEN; |
| 191 | } |
| 192 | |
| 193 | m_widget = gtk_file_chooser_dialog_new( |
| 194 | wxGTK_CONV(m_message), |
| 195 | gtk_parent, |
| 196 | gtk_action, |
| 197 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, |
| 198 | ok_btn_stock, GTK_RESPONSE_ACCEPT, |
| 199 | NULL); |
| 200 | |
| 201 | gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT); |
| 202 | |
| 203 | if ( style & wxFD_MULTIPLE ) |
| 204 | gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true); |
| 205 | |
| 206 | // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically |
| 207 | // destroys the dialog when the user press ESC on the dialog: in that case |
| 208 | // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL |
| 209 | // errors... |
| 210 | g_signal_connect (G_OBJECT(m_widget), |
| 211 | "delete_event", |
| 212 | G_CALLBACK (gtk_widget_hide_on_delete), |
| 213 | (gpointer)this); |
| 214 | |
| 215 | // local-only property could be set to false to allow non-local files to be |
| 216 | // loaded. In that case get/set_uri(s) should be used instead of |
| 217 | // get/set_filename(s) everywhere and the GtkFileChooserDialog should |
| 218 | // probably also be created with a backend, e.g "gnome-vfs", "default", ... |
| 219 | // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept |
| 220 | // as the default - true: |
| 221 | // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true); |
| 222 | |
| 223 | g_signal_connect (m_widget, "response", |
| 224 | G_CALLBACK (gtk_filedialog_response_callback), this); |
| 225 | |
| 226 | SetWildcard(wildCard); |
| 227 | |
| 228 | // if defaultDir is specified it should contain the directory and |
| 229 | // defaultFileName should contain the default name of the file, however if |
| 230 | // directory is not given, defaultFileName contains both |
| 231 | wxFileName fn; |
| 232 | if ( defaultDir.empty() ) |
| 233 | fn.Assign(defaultFileName); |
| 234 | else if ( !defaultFileName.empty() ) |
| 235 | fn.Assign(defaultDir, defaultFileName); |
| 236 | else |
| 237 | fn.AssignDir(defaultDir); |
| 238 | |
| 239 | // set the initial file name and/or directory |
| 240 | const wxString dir = fn.GetPath(); |
| 241 | if ( !dir.empty() ) |
| 242 | { |
| 243 | gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), |
| 244 | dir.fn_str()); |
| 245 | } |
| 246 | |
| 247 | const wxString fname = fn.GetFullName(); |
| 248 | if ( style & wxFD_SAVE ) |
| 249 | { |
| 250 | if ( !fname.empty() ) |
| 251 | { |
| 252 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), |
| 253 | fname.fn_str()); |
| 254 | } |
| 255 | |
| 256 | #if GTK_CHECK_VERSION(2,7,3) |
| 257 | if ((style & wxFD_OVERWRITE_PROMPT) && !gtk_check_version(2,7,3)) |
| 258 | gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget), TRUE); |
| 259 | #endif |
| 260 | } |
| 261 | else // wxFD_OPEN |
| 262 | { |
| 263 | if ( !fname.empty() ) |
| 264 | { |
| 265 | gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), |
| 266 | fn.GetFullPath().fn_str()); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | #if GTK_CHECK_VERSION(2,4,0) |
| 271 | if ( style & wxFD_PREVIEW ) |
| 272 | { |
| 273 | GtkWidget *previewImage = gtk_image_new(); |
| 274 | |
| 275 | gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget), |
| 276 | previewImage); |
| 277 | g_signal_connect(m_widget, "update-preview", |
| 278 | G_CALLBACK(gtk_filedialog_update_preview_callback), |
| 279 | previewImage); |
| 280 | } |
| 281 | #endif // GTK+ 2.4+ |
| 282 | } |
| 283 | |
| 284 | void wxFileDialog::OnFakeOk( wxCommandEvent &event ) |
| 285 | { |
| 286 | if (!gtk_check_version(2,4,0)) |
| 287 | EndDialog(wxID_OK); |
| 288 | else |
| 289 | wxGenericFileDialog::OnListOk( event ); |
| 290 | } |
| 291 | |
| 292 | int wxFileDialog::ShowModal() |
| 293 | { |
| 294 | if (!gtk_check_version(2,4,0)) |
| 295 | return wxDialog::ShowModal(); |
| 296 | else |
| 297 | return wxGenericFileDialog::ShowModal(); |
| 298 | } |
| 299 | |
| 300 | bool wxFileDialog::Show( bool show ) |
| 301 | { |
| 302 | if (!gtk_check_version(2,4,0)) |
| 303 | return wxDialog::Show( show ); |
| 304 | else |
| 305 | return wxGenericFileDialog::Show( show ); |
| 306 | } |
| 307 | |
| 308 | void wxFileDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags ) |
| 309 | { |
| 310 | if (!m_wxwindow) |
| 311 | return; |
| 312 | else |
| 313 | wxGenericFileDialog::DoSetSize( x, y, width, height, sizeFlags ); |
| 314 | } |
| 315 | |
| 316 | wxString wxFileDialog::GetPath() const |
| 317 | { |
| 318 | if (!gtk_check_version(2,4,0)) |
| 319 | { |
| 320 | wxGtkString str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget))); |
| 321 | return wxConvFileName->cMB2WX(str); |
| 322 | } |
| 323 | |
| 324 | return wxGenericFileDialog::GetPath(); |
| 325 | } |
| 326 | |
| 327 | void wxFileDialog::GetFilenames(wxArrayString& files) const |
| 328 | { |
| 329 | if (!gtk_check_version(2,4,0)) |
| 330 | { |
| 331 | GetPaths(files); |
| 332 | for (size_t n = 0; n < files.GetCount(); ++n ) |
| 333 | { |
| 334 | wxFileName file(files[n]); |
| 335 | files[n] = file.GetFullName(); |
| 336 | } |
| 337 | } |
| 338 | else |
| 339 | wxGenericFileDialog::GetFilenames( files ); |
| 340 | } |
| 341 | |
| 342 | void wxFileDialog::GetPaths(wxArrayString& paths) const |
| 343 | { |
| 344 | if (!gtk_check_version(2,4,0)) |
| 345 | { |
| 346 | paths.Empty(); |
| 347 | if (gtk_file_chooser_get_select_multiple(GTK_FILE_CHOOSER(m_widget))) |
| 348 | { |
| 349 | GSList *gpathsi = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget)); |
| 350 | GSList *gpaths = gpathsi; |
| 351 | while (gpathsi) |
| 352 | { |
| 353 | wxString file(wxConvFileName->cMB2WX((gchar*) gpathsi->data)); |
| 354 | paths.Add(file); |
| 355 | g_free(gpathsi->data); |
| 356 | gpathsi = gpathsi->next; |
| 357 | } |
| 358 | |
| 359 | g_slist_free(gpaths); |
| 360 | } |
| 361 | else |
| 362 | paths.Add(GetPath()); |
| 363 | } |
| 364 | else |
| 365 | wxGenericFileDialog::GetPaths( paths ); |
| 366 | } |
| 367 | |
| 368 | void wxFileDialog::SetMessage(const wxString& message) |
| 369 | { |
| 370 | if (!gtk_check_version(2,4,0)) |
| 371 | { |
| 372 | m_message = message; |
| 373 | SetTitle(message); |
| 374 | } |
| 375 | else |
| 376 | wxGenericFileDialog::SetMessage( message ); |
| 377 | } |
| 378 | |
| 379 | void wxFileDialog::SetPath(const wxString& path) |
| 380 | { |
| 381 | if (!gtk_check_version(2,4,0)) |
| 382 | { |
| 383 | if (path.empty()) return; |
| 384 | |
| 385 | gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(path)); |
| 386 | } |
| 387 | else |
| 388 | wxGenericFileDialog::SetPath( path ); |
| 389 | } |
| 390 | |
| 391 | void wxFileDialog::SetDirectory(const wxString& dir) |
| 392 | { |
| 393 | if (!gtk_check_version(2,4,0)) |
| 394 | { |
| 395 | if (wxDirExists(dir)) |
| 396 | { |
| 397 | gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(dir)); |
| 398 | } |
| 399 | } |
| 400 | else |
| 401 | wxGenericFileDialog::SetDirectory( dir ); |
| 402 | } |
| 403 | |
| 404 | wxString wxFileDialog::GetDirectory() const |
| 405 | { |
| 406 | if (!gtk_check_version(2,4,0)) |
| 407 | { |
| 408 | wxGtkString str(gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(m_widget))); |
| 409 | return wxConvFileName->cMB2WX(str); |
| 410 | } |
| 411 | |
| 412 | return wxGenericFileDialog::GetDirectory(); |
| 413 | } |
| 414 | |
| 415 | void wxFileDialog::SetFilename(const wxString& name) |
| 416 | { |
| 417 | if (!gtk_check_version(2,4,0)) |
| 418 | { |
| 419 | if (HasFdFlag(wxFD_SAVE)) |
| 420 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name)); |
| 421 | else |
| 422 | SetPath(wxFileName(GetDirectory(), name).GetFullPath()); |
| 423 | } |
| 424 | else |
| 425 | wxGenericFileDialog::SetFilename( name ); |
| 426 | } |
| 427 | |
| 428 | wxString wxFileDialog::GetFilename() const |
| 429 | { |
| 430 | if (!gtk_check_version(2,4,0)) |
| 431 | return wxFileName(GetPath()).GetFullName(); |
| 432 | else |
| 433 | return wxGenericFileDialog::GetFilename(); |
| 434 | } |
| 435 | |
| 436 | void wxFileDialog::SetWildcard(const wxString& wildCard) |
| 437 | { |
| 438 | if (!gtk_check_version(2,4,0)) |
| 439 | { |
| 440 | // parse filters |
| 441 | wxArrayString wildDescriptions, wildFilters; |
| 442 | if (!wxParseCommonDialogsFilter(wildCard, wildDescriptions, wildFilters)) |
| 443 | { |
| 444 | wxFAIL_MSG( wxT("wxFileDialog::SetWildCard - bad wildcard string") ); |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | // Parsing went fine. Set m_wildCard to be returned by wxFileDialogBase::GetWildcard |
| 449 | m_wildCard = wildCard; |
| 450 | |
| 451 | GtkFileChooser* chooser = GTK_FILE_CHOOSER(m_widget); |
| 452 | |
| 453 | // empty current filter list: |
| 454 | GSList* ifilters = gtk_file_chooser_list_filters(chooser); |
| 455 | GSList* filters = ifilters; |
| 456 | |
| 457 | while (ifilters) |
| 458 | { |
| 459 | gtk_file_chooser_remove_filter(chooser,GTK_FILE_FILTER(ifilters->data)); |
| 460 | ifilters = ifilters->next; |
| 461 | } |
| 462 | g_slist_free(filters); |
| 463 | |
| 464 | // add parsed to GtkChooser |
| 465 | for (size_t n = 0; n < wildFilters.GetCount(); ++n) |
| 466 | { |
| 467 | GtkFileFilter* filter = gtk_file_filter_new(); |
| 468 | gtk_file_filter_set_name(filter, wxGTK_CONV(wildDescriptions[n])); |
| 469 | |
| 470 | wxStringTokenizer exttok(wildFilters[n], wxT(";")); |
| 471 | while (exttok.HasMoreTokens()) |
| 472 | { |
| 473 | wxString token = exttok.GetNextToken(); |
| 474 | gtk_file_filter_add_pattern(filter, wxGTK_CONV(token)); |
| 475 | } |
| 476 | |
| 477 | gtk_file_chooser_add_filter(chooser, filter); |
| 478 | } |
| 479 | |
| 480 | // Reset the filter index |
| 481 | SetFilterIndex(0); |
| 482 | } |
| 483 | } |
| 484 | else |
| 485 | wxGenericFileDialog::SetWildcard( wildCard ); |
| 486 | } |
| 487 | |
| 488 | void wxFileDialog::SetFilterIndex(int filterIndex) |
| 489 | { |
| 490 | |
| 491 | if (!gtk_check_version(2,4,0)) |
| 492 | { |
| 493 | gpointer filter; |
| 494 | GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget); |
| 495 | GSList *filters = gtk_file_chooser_list_filters(chooser); |
| 496 | |
| 497 | filter = g_slist_nth_data(filters, filterIndex); |
| 498 | |
| 499 | if (filter != NULL) |
| 500 | { |
| 501 | gtk_file_chooser_set_filter(chooser, GTK_FILE_FILTER(filter)); |
| 502 | } |
| 503 | else |
| 504 | { |
| 505 | wxFAIL_MSG( wxT("wxFileDialog::SetFilterIndex - bad filter index") ); |
| 506 | } |
| 507 | |
| 508 | g_slist_free(filters); |
| 509 | } |
| 510 | else |
| 511 | wxGenericFileDialog::SetFilterIndex( filterIndex ); |
| 512 | } |
| 513 | |
| 514 | int wxFileDialog::GetFilterIndex() const |
| 515 | { |
| 516 | if (!gtk_check_version(2,4,0)) |
| 517 | { |
| 518 | GtkFileChooser *chooser = GTK_FILE_CHOOSER(m_widget); |
| 519 | GtkFileFilter *filter = gtk_file_chooser_get_filter(chooser); |
| 520 | GSList *filters = gtk_file_chooser_list_filters(chooser); |
| 521 | gint index = g_slist_index(filters, filter); |
| 522 | g_slist_free(filters); |
| 523 | |
| 524 | if (index == -1) |
| 525 | { |
| 526 | wxFAIL_MSG( wxT("wxFileDialog::GetFilterIndex - bad filter index returned by gtk+") ); |
| 527 | return 0; |
| 528 | } |
| 529 | else |
| 530 | return index; |
| 531 | } |
| 532 | else |
| 533 | return wxGenericFileDialog::GetFilterIndex(); |
| 534 | } |
| 535 | |
| 536 | #endif // wxUSE_FILEDLG && __WXGTK24__ |