| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <gtk/gtk.h> | 5 #include <gtk/gtk.h> |
| 6 #include <map> | 6 #include <map> |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 GtkWidget* CreateMultiFileOpenDialog(const std::string& title, | 76 GtkWidget* CreateMultiFileOpenDialog(const std::string& title, |
| 77 const FilePath& default_path, gfx::NativeWindow parent); | 77 const FilePath& default_path, gfx::NativeWindow parent); |
| 78 | 78 |
| 79 GtkWidget* CreateSaveAsDialog(const std::string& title, | 79 GtkWidget* CreateSaveAsDialog(const std::string& title, |
| 80 const FilePath& default_path, gfx::NativeWindow parent); | 80 const FilePath& default_path, gfx::NativeWindow parent); |
| 81 | 81 |
| 82 // Removes and returns the |params| associated with |dialog| from | 82 // Removes and returns the |params| associated with |dialog| from |
| 83 // |params_map_|. | 83 // |params_map_|. |
| 84 void* PopParamsForDialog(GtkWidget* dialog); | 84 void* PopParamsForDialog(GtkWidget* dialog); |
| 85 | 85 |
| 86 // Take care of internal data structures when a file dialog is destroyed. | |
| 87 void FileDialogDestroyed(GtkWidget* dialog); | |
| 88 | |
| 89 // Check whether response_id corresponds to the user cancelling/closing the | 86 // Check whether response_id corresponds to the user cancelling/closing the |
| 90 // dialog. Used as a helper for the below callbacks. | 87 // dialog. Used as a helper for the below callbacks. |
| 91 bool IsCancelResponse(gint response_id); | 88 bool IsCancelResponse(gint response_id); |
| 92 | 89 |
| 93 // Common function for OnSelectSingleFileDialogResponse and | 90 // Common function for OnSelectSingleFileDialogResponse and |
| 94 // OnSelectSingleFolderDialogResponse. | 91 // OnSelectSingleFolderDialogResponse. |
| 95 void SelectSingleFileHelper(GtkWidget* dialog, | 92 void SelectSingleFileHelper(GtkWidget* dialog, |
| 96 gint response_id, | 93 gint response_id, |
| 97 bool allow_folder); | 94 bool allow_folder); |
| 98 | 95 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 dialog = CreateMultiFileOpenDialog(title_string, default_path, | 224 dialog = CreateMultiFileOpenDialog(title_string, default_path, |
| 228 owning_window); | 225 owning_window); |
| 229 break; | 226 break; |
| 230 case SELECT_SAVEAS_FILE: | 227 case SELECT_SAVEAS_FILE: |
| 231 dialog = CreateSaveAsDialog(title_string, default_path, owning_window); | 228 dialog = CreateSaveAsDialog(title_string, default_path, owning_window); |
| 232 break; | 229 break; |
| 233 default: | 230 default: |
| 234 NOTREACHED(); | 231 NOTREACHED(); |
| 235 return; | 232 return; |
| 236 } | 233 } |
| 234 g_object_ref(dialog); |
| 237 dialogs_.insert(dialog); | 235 dialogs_.insert(dialog); |
| 238 | 236 |
| 239 preview_ = gtk_image_new(); | 237 preview_ = gtk_image_new(); |
| 240 g_signal_connect(dialog, "destroy", | 238 g_signal_connect(dialog, "destroy", |
| 241 G_CALLBACK(OnFileChooserDestroyThunk), this); | 239 G_CALLBACK(OnFileChooserDestroyThunk), this); |
| 242 g_signal_connect(dialog, "update-preview", | 240 g_signal_connect(dialog, "update-preview", |
| 243 G_CALLBACK(OnUpdatePreviewThunk), this); | 241 G_CALLBACK(OnUpdatePreviewThunk), this); |
| 244 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(dialog), preview_); | 242 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(dialog), preview_); |
| 245 | 243 |
| 246 params_map_[dialog] = params; | 244 params_map_[dialog] = params; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 NOTREACHED(); | 312 NOTREACHED(); |
| 315 | 313 |
| 316 if (listener_) { | 314 if (listener_) { |
| 317 GtkFileFilter* selected_filter = | 315 GtkFileFilter* selected_filter = |
| 318 gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog)); | 316 gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog)); |
| 319 GSList* filters = gtk_file_chooser_list_filters(GTK_FILE_CHOOSER(dialog)); | 317 GSList* filters = gtk_file_chooser_list_filters(GTK_FILE_CHOOSER(dialog)); |
| 320 int idx = g_slist_index(filters, selected_filter); | 318 int idx = g_slist_index(filters, selected_filter); |
| 321 g_slist_free(filters); | 319 g_slist_free(filters); |
| 322 listener_->FileSelected(path, idx + 1, PopParamsForDialog(dialog)); | 320 listener_->FileSelected(path, idx + 1, PopParamsForDialog(dialog)); |
| 323 } | 321 } |
| 322 |
| 324 gtk_widget_destroy(dialog); | 323 gtk_widget_destroy(dialog); |
| 325 } | 324 } |
| 326 | 325 |
| 327 void SelectFileDialogImpl::MultiFilesSelected(GtkWidget* dialog, | 326 void SelectFileDialogImpl::MultiFilesSelected(GtkWidget* dialog, |
| 328 const std::vector<FilePath>& files) { | 327 const std::vector<FilePath>& files) { |
| 329 *last_opened_path_ = files[0].DirName(); | 328 *last_opened_path_ = files[0].DirName(); |
| 330 | 329 |
| 331 if (listener_) | 330 if (listener_) |
| 332 listener_->MultiFilesSelected(files, PopParamsForDialog(dialog)); | 331 listener_->MultiFilesSelected(files, PopParamsForDialog(dialog)); |
| 333 gtk_widget_destroy(dialog); | 332 gtk_widget_destroy(dialog); |
| 334 } | 333 } |
| 335 | 334 |
| 336 void SelectFileDialogImpl::FileNotSelected(GtkWidget* dialog) { | 335 void SelectFileDialogImpl::FileNotSelected(GtkWidget* dialog) { |
| 337 void* params = PopParamsForDialog(dialog); | 336 void* params = PopParamsForDialog(dialog); |
| 338 if (listener_) | 337 if (listener_) |
| 339 listener_->FileSelectionCanceled(params); | 338 listener_->FileSelectionCanceled(params); |
| 340 gtk_widget_destroy(dialog); | 339 // In the GTK_RESPONSE_DELETE_EVENT case, the dialog will have already been |
| 340 // destroyed. |
| 341 if (dialogs_.find(dialog) != dialogs_.end()) |
| 342 gtk_widget_destroy(dialog); |
| 341 } | 343 } |
| 342 | 344 |
| 343 bool SelectFileDialogImpl::CallDirectoryExistsOnUIThread(const FilePath& path) { | 345 bool SelectFileDialogImpl::CallDirectoryExistsOnUIThread(const FilePath& path) { |
| 344 base::ThreadRestrictions::ScopedAllowIO allow_io; | 346 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 345 return file_util::DirectoryExists(path); | 347 return file_util::DirectoryExists(path); |
| 346 } | 348 } |
| 347 | 349 |
| 348 GtkWidget* SelectFileDialogImpl::CreateFileOpenHelper( | 350 GtkWidget* SelectFileDialogImpl::CreateFileOpenHelper( |
| 349 const std::string& title, | 351 const std::string& title, |
| 350 const FilePath& default_path, | 352 const FilePath& default_path, |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 } | 463 } |
| 462 | 464 |
| 463 void* SelectFileDialogImpl::PopParamsForDialog(GtkWidget* dialog) { | 465 void* SelectFileDialogImpl::PopParamsForDialog(GtkWidget* dialog) { |
| 464 std::map<GtkWidget*, void*>::iterator iter = params_map_.find(dialog); | 466 std::map<GtkWidget*, void*>::iterator iter = params_map_.find(dialog); |
| 465 DCHECK(iter != params_map_.end()); | 467 DCHECK(iter != params_map_.end()); |
| 466 void* params = iter->second; | 468 void* params = iter->second; |
| 467 params_map_.erase(iter); | 469 params_map_.erase(iter); |
| 468 return params; | 470 return params; |
| 469 } | 471 } |
| 470 | 472 |
| 471 void SelectFileDialogImpl::FileDialogDestroyed(GtkWidget* dialog) { | |
| 472 dialogs_.erase(dialog); | |
| 473 | |
| 474 // Parent may be NULL in a few cases: 1) on shutdown when | |
| 475 // AllBrowsersClosed() trigger this handler after all the browser | |
| 476 // windows got destroyed, or 2) when the parent tab has been opened by | |
| 477 // 'Open Link in New Tab' context menu on a downloadable item and | |
| 478 // the tab has no content (see the comment in SelectFile as well). | |
| 479 GtkWindow* parent = gtk_window_get_transient_for(GTK_WINDOW(dialog)); | |
| 480 if (!parent) | |
| 481 return; | |
| 482 std::set<GtkWindow*>::iterator iter = parents_.find(parent); | |
| 483 if (iter != parents_.end()) | |
| 484 parents_.erase(iter); | |
| 485 else | |
| 486 NOTREACHED(); | |
| 487 } | |
| 488 | |
| 489 bool SelectFileDialogImpl::IsCancelResponse(gint response_id) { | 473 bool SelectFileDialogImpl::IsCancelResponse(gint response_id) { |
| 490 bool is_cancel = response_id == GTK_RESPONSE_CANCEL || | 474 bool is_cancel = response_id == GTK_RESPONSE_CANCEL || |
| 491 response_id == GTK_RESPONSE_DELETE_EVENT; | 475 response_id == GTK_RESPONSE_DELETE_EVENT; |
| 492 if (is_cancel) | 476 if (is_cancel) |
| 493 return true; | 477 return true; |
| 494 | 478 |
| 495 DCHECK(response_id == GTK_RESPONSE_ACCEPT); | 479 DCHECK(response_id == GTK_RESPONSE_ACCEPT); |
| 496 return false; | 480 return false; |
| 497 } | 481 } |
| 498 | 482 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 g_slist_free(filenames); | 542 g_slist_free(filenames); |
| 559 | 543 |
| 560 if (filenames_fp.empty()) { | 544 if (filenames_fp.empty()) { |
| 561 FileNotSelected(dialog); | 545 FileNotSelected(dialog); |
| 562 return; | 546 return; |
| 563 } | 547 } |
| 564 MultiFilesSelected(dialog, filenames_fp); | 548 MultiFilesSelected(dialog, filenames_fp); |
| 565 } | 549 } |
| 566 | 550 |
| 567 void SelectFileDialogImpl::OnFileChooserDestroy(GtkWidget* dialog) { | 551 void SelectFileDialogImpl::OnFileChooserDestroy(GtkWidget* dialog) { |
| 568 FileDialogDestroyed(dialog); | 552 dialogs_.erase(dialog); |
| 553 |
| 554 // Parent may be NULL in a few cases: 1) on shutdown when |
| 555 // AllBrowsersClosed() trigger this handler after all the browser |
| 556 // windows got destroyed, or 2) when the parent tab has been opened by |
| 557 // 'Open Link in New Tab' context menu on a downloadable item and |
| 558 // the tab has no content (see the comment in SelectFile as well). |
| 559 GtkWindow* parent = gtk_window_get_transient_for(GTK_WINDOW(dialog)); |
| 560 if (!parent) |
| 561 return; |
| 562 std::set<GtkWindow*>::iterator iter = parents_.find(parent); |
| 563 if (iter != parents_.end()) |
| 564 parents_.erase(iter); |
| 565 else |
| 566 NOTREACHED(); |
| 569 } | 567 } |
| 570 | 568 |
| 571 void SelectFileDialogImpl::OnUpdatePreview(GtkWidget* chooser) { | 569 void SelectFileDialogImpl::OnUpdatePreview(GtkWidget* chooser) { |
| 572 gchar* filename = gtk_file_chooser_get_preview_filename( | 570 gchar* filename = gtk_file_chooser_get_preview_filename( |
| 573 GTK_FILE_CHOOSER(chooser)); | 571 GTK_FILE_CHOOSER(chooser)); |
| 574 if (!filename) | 572 if (!filename) |
| 575 return; | 573 return; |
| 576 // This will preserve the image's aspect ratio. | 574 // This will preserve the image's aspect ratio. |
| 577 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_size(filename, kPreviewWidth, | 575 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_size(filename, kPreviewWidth, |
| 578 kPreviewHeight, NULL); | 576 kPreviewHeight, NULL); |
| 579 g_free(filename); | 577 g_free(filename); |
| 580 if (pixbuf) { | 578 if (pixbuf) { |
| 581 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf); | 579 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf); |
| 582 g_object_unref(pixbuf); | 580 g_object_unref(pixbuf); |
| 583 } | 581 } |
| 584 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), | 582 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), |
| 585 pixbuf ? TRUE : FALSE); | 583 pixbuf ? TRUE : FALSE); |
| 586 } | 584 } |
| OLD | NEW |