| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/ui/libgtkui/select_file_dialog_impl_gtk.h" | 5 #include "chrome/browser/ui/libgtkui/select_file_dialog_impl_gtk.h" |
| 6 | 6 |
| 7 #include <gdk/gdkx.h> | 7 #include <gdk/gdkx.h> |
| 8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 static const int kPreviewHeight = 512; | 69 static const int kPreviewHeight = 512; |
| 70 | 70 |
| 71 SelectFileDialogImpl* SelectFileDialogImpl::NewSelectFileDialogImplGTK( | 71 SelectFileDialogImpl* SelectFileDialogImpl::NewSelectFileDialogImplGTK( |
| 72 Listener* listener, | 72 Listener* listener, |
| 73 ui::SelectFilePolicy* policy) { | 73 ui::SelectFilePolicy* policy) { |
| 74 return new SelectFileDialogImplGTK(listener, policy); | 74 return new SelectFileDialogImplGTK(listener, policy); |
| 75 } | 75 } |
| 76 | 76 |
| 77 SelectFileDialogImplGTK::SelectFileDialogImplGTK(Listener* listener, | 77 SelectFileDialogImplGTK::SelectFileDialogImplGTK(Listener* listener, |
| 78 ui::SelectFilePolicy* policy) | 78 ui::SelectFilePolicy* policy) |
| 79 : SelectFileDialogImpl(listener, policy), preview_(NULL) {} | 79 : SelectFileDialogImpl(listener, policy), preview_(nullptr) {} |
| 80 | 80 |
| 81 SelectFileDialogImplGTK::~SelectFileDialogImplGTK() { | 81 SelectFileDialogImplGTK::~SelectFileDialogImplGTK() { |
| 82 for (std::set<aura::Window*>::iterator iter = parents_.begin(); | 82 for (std::set<aura::Window*>::iterator iter = parents_.begin(); |
| 83 iter != parents_.end(); ++iter) { | 83 iter != parents_.end(); ++iter) { |
| 84 (*iter)->RemoveObserver(this); | 84 (*iter)->RemoveObserver(this); |
| 85 } | 85 } |
| 86 while (dialogs_.begin() != dialogs_.end()) { | 86 while (dialogs_.begin() != dialogs_.end()) { |
| 87 gtk_widget_destroy(*(dialogs_.begin())); | 87 gtk_widget_destroy(*(dialogs_.begin())); |
| 88 } | 88 } |
| 89 } | 89 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 owning_window->AddObserver(this); | 127 owning_window->AddObserver(this); |
| 128 parents_.insert(owning_window); | 128 parents_.insert(owning_window); |
| 129 } | 129 } |
| 130 | 130 |
| 131 std::string title_string = base::UTF16ToUTF8(title); | 131 std::string title_string = base::UTF16ToUTF8(title); |
| 132 | 132 |
| 133 file_type_index_ = file_type_index; | 133 file_type_index_ = file_type_index; |
| 134 if (file_types) | 134 if (file_types) |
| 135 file_types_ = *file_types; | 135 file_types_ = *file_types; |
| 136 | 136 |
| 137 GtkWidget* dialog = NULL; | 137 GtkWidget* dialog = nullptr; |
| 138 switch (type) { | 138 switch (type) { |
| 139 case SELECT_FOLDER: | 139 case SELECT_FOLDER: |
| 140 case SELECT_UPLOAD_FOLDER: | 140 case SELECT_UPLOAD_FOLDER: |
| 141 dialog = CreateSelectFolderDialog(type, title_string, default_path, | 141 dialog = CreateSelectFolderDialog(type, title_string, default_path, |
| 142 owning_window); | 142 owning_window); |
| 143 break; | 143 break; |
| 144 case SELECT_OPEN_FILE: | 144 case SELECT_OPEN_FILE: |
| 145 dialog = CreateFileOpenDialog(title_string, default_path, owning_window); | 145 dialog = CreateFileOpenDialog(title_string, default_path, owning_window); |
| 146 break; | 146 break; |
| 147 case SELECT_OPEN_MULTI_FILE: | 147 case SELECT_OPEN_MULTI_FILE: |
| 148 dialog = | 148 dialog = |
| 149 CreateMultiFileOpenDialog(title_string, default_path, owning_window); | 149 CreateMultiFileOpenDialog(title_string, default_path, owning_window); |
| 150 break; | 150 break; |
| 151 case SELECT_SAVEAS_FILE: | 151 case SELECT_SAVEAS_FILE: |
| 152 dialog = CreateSaveAsDialog(title_string, default_path, owning_window); | 152 dialog = CreateSaveAsDialog(title_string, default_path, owning_window); |
| 153 break; | 153 break; |
| 154 default: | 154 default: |
| 155 NOTREACHED(); | 155 NOTREACHED(); |
| 156 return; | 156 return; |
| 157 } | 157 } |
| 158 g_signal_connect(dialog, "delete-event", | 158 g_signal_connect(dialog, "delete-event", |
| 159 G_CALLBACK(gtk_widget_hide_on_delete), NULL); | 159 G_CALLBACK(gtk_widget_hide_on_delete), nullptr); |
| 160 dialogs_.insert(dialog); | 160 dialogs_.insert(dialog); |
| 161 | 161 |
| 162 preview_ = gtk_image_new(); | 162 preview_ = gtk_image_new(); |
| 163 g_signal_connect(dialog, "destroy", G_CALLBACK(OnFileChooserDestroyThunk), | 163 g_signal_connect(dialog, "destroy", G_CALLBACK(OnFileChooserDestroyThunk), |
| 164 this); | 164 this); |
| 165 g_signal_connect(dialog, "update-preview", G_CALLBACK(OnUpdatePreviewThunk), | 165 g_signal_connect(dialog, "update-preview", G_CALLBACK(OnUpdatePreviewThunk), |
| 166 this); | 166 this); |
| 167 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(dialog), preview_); | 167 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(dialog), preview_); |
| 168 | 168 |
| 169 params_map_[dialog] = params; | 169 params_map_[dialog] = params; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 190 gtk_widget_show_all(dialog); | 190 gtk_widget_show_all(dialog); |
| 191 | 191 |
| 192 // We need to call gtk_window_present after making the widgets visible to make | 192 // We need to call gtk_window_present after making the widgets visible to make |
| 193 // sure window gets correctly raised and gets focus. | 193 // sure window gets correctly raised and gets focus. |
| 194 gtk_window_present_with_time( | 194 gtk_window_present_with_time( |
| 195 GTK_WINDOW(dialog), ui::X11EventSource::GetInstance()->GetTimestamp()); | 195 GTK_WINDOW(dialog), ui::X11EventSource::GetInstance()->GetTimestamp()); |
| 196 } | 196 } |
| 197 | 197 |
| 198 void SelectFileDialogImplGTK::AddFilters(GtkFileChooser* chooser) { | 198 void SelectFileDialogImplGTK::AddFilters(GtkFileChooser* chooser) { |
| 199 for (size_t i = 0; i < file_types_.extensions.size(); ++i) { | 199 for (size_t i = 0; i < file_types_.extensions.size(); ++i) { |
| 200 GtkFileFilter* filter = NULL; | 200 GtkFileFilter* filter = nullptr; |
| 201 std::set<std::string> fallback_labels; | 201 std::set<std::string> fallback_labels; |
| 202 | 202 |
| 203 for (size_t j = 0; j < file_types_.extensions[i].size(); ++j) { | 203 for (size_t j = 0; j < file_types_.extensions[i].size(); ++j) { |
| 204 const std::string& current_extension = file_types_.extensions[i][j]; | 204 const std::string& current_extension = file_types_.extensions[i][j]; |
| 205 if (!current_extension.empty()) { | 205 if (!current_extension.empty()) { |
| 206 if (!filter) | 206 if (!filter) |
| 207 filter = gtk_file_filter_new(); | 207 filter = gtk_file_filter_new(); |
| 208 std::unique_ptr<std::string> file_extension( | 208 std::unique_ptr<std::string> file_extension( |
| 209 new std::string("." + current_extension)); | 209 new std::string("." + current_extension)); |
| 210 fallback_labels.insert(std::string("*").append(*file_extension)); | 210 fallback_labels.insert(std::string("*").append(*file_extension)); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 if (listener_) | 289 if (listener_) |
| 290 listener_->FileSelectionCanceled(params); | 290 listener_->FileSelectionCanceled(params); |
| 291 gtk_widget_destroy(dialog); | 291 gtk_widget_destroy(dialog); |
| 292 } | 292 } |
| 293 | 293 |
| 294 GtkWidget* SelectFileDialogImplGTK::CreateFileOpenHelper( | 294 GtkWidget* SelectFileDialogImplGTK::CreateFileOpenHelper( |
| 295 const std::string& title, | 295 const std::string& title, |
| 296 const base::FilePath& default_path, | 296 const base::FilePath& default_path, |
| 297 gfx::NativeWindow parent) { | 297 gfx::NativeWindow parent) { |
| 298 GtkWidget* dialog = gtk_file_chooser_dialog_new( | 298 GtkWidget* dialog = gtk_file_chooser_dialog_new( |
| 299 title.c_str(), NULL, GTK_FILE_CHOOSER_ACTION_OPEN, "_Cancel", | 299 title.c_str(), nullptr, GTK_FILE_CHOOSER_ACTION_OPEN, "_Cancel", |
| 300 GTK_RESPONSE_CANCEL, "_Open", GTK_RESPONSE_ACCEPT, nullptr); | 300 GTK_RESPONSE_CANCEL, "_Open", GTK_RESPONSE_ACCEPT, nullptr); |
| 301 SetGtkTransientForAura(dialog, parent); | 301 SetGtkTransientForAura(dialog, parent); |
| 302 AddFilters(GTK_FILE_CHOOSER(dialog)); | 302 AddFilters(GTK_FILE_CHOOSER(dialog)); |
| 303 | 303 |
| 304 if (!default_path.empty()) { | 304 if (!default_path.empty()) { |
| 305 if (CallDirectoryExistsOnUIThread(default_path)) { | 305 if (CallDirectoryExistsOnUIThread(default_path)) { |
| 306 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), | 306 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), |
| 307 default_path.value().c_str()); | 307 default_path.value().c_str()); |
| 308 } else { | 308 } else { |
| 309 // If the file doesn't exist, this will just switch to the correct | 309 // If the file doesn't exist, this will just switch to the correct |
| (...skipping 20 matching lines...) Expand all Loading... |
| 330 ? l10n_util::GetStringUTF8(IDS_SELECT_UPLOAD_FOLDER_DIALOG_TITLE) | 330 ? l10n_util::GetStringUTF8(IDS_SELECT_UPLOAD_FOLDER_DIALOG_TITLE) |
| 331 : l10n_util::GetStringUTF8(IDS_SELECT_FOLDER_DIALOG_TITLE); | 331 : l10n_util::GetStringUTF8(IDS_SELECT_FOLDER_DIALOG_TITLE); |
| 332 } | 332 } |
| 333 std::string accept_button_label = | 333 std::string accept_button_label = |
| 334 (type == SELECT_UPLOAD_FOLDER) | 334 (type == SELECT_UPLOAD_FOLDER) |
| 335 ? l10n_util::GetStringUTF8( | 335 ? l10n_util::GetStringUTF8( |
| 336 IDS_SELECT_UPLOAD_FOLDER_DIALOG_UPLOAD_BUTTON) | 336 IDS_SELECT_UPLOAD_FOLDER_DIALOG_UPLOAD_BUTTON) |
| 337 : "_Open"; | 337 : "_Open"; |
| 338 | 338 |
| 339 GtkWidget* dialog = gtk_file_chooser_dialog_new( | 339 GtkWidget* dialog = gtk_file_chooser_dialog_new( |
| 340 title_string.c_str(), NULL, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, | 340 title_string.c_str(), nullptr, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, |
| 341 "_Cancel", GTK_RESPONSE_CANCEL, accept_button_label.c_str(), | 341 "_Cancel", GTK_RESPONSE_CANCEL, accept_button_label.c_str(), |
| 342 GTK_RESPONSE_ACCEPT, nullptr); | 342 GTK_RESPONSE_ACCEPT, nullptr); |
| 343 SetGtkTransientForAura(dialog, parent); | 343 SetGtkTransientForAura(dialog, parent); |
| 344 | 344 |
| 345 if (!default_path.empty()) { | 345 if (!default_path.empty()) { |
| 346 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), | 346 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), |
| 347 default_path.value().c_str()); | 347 default_path.value().c_str()); |
| 348 } else if (!last_opened_path_->empty()) { | 348 } else if (!last_opened_path_->empty()) { |
| 349 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), | 349 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), |
| 350 last_opened_path_->value().c_str()); | 350 last_opened_path_->value().c_str()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 | 387 |
| 388 GtkWidget* SelectFileDialogImplGTK::CreateSaveAsDialog( | 388 GtkWidget* SelectFileDialogImplGTK::CreateSaveAsDialog( |
| 389 const std::string& title, | 389 const std::string& title, |
| 390 const base::FilePath& default_path, | 390 const base::FilePath& default_path, |
| 391 gfx::NativeWindow parent) { | 391 gfx::NativeWindow parent) { |
| 392 std::string title_string = | 392 std::string title_string = |
| 393 !title.empty() ? title | 393 !title.empty() ? title |
| 394 : l10n_util::GetStringUTF8(IDS_SAVE_AS_DIALOG_TITLE); | 394 : l10n_util::GetStringUTF8(IDS_SAVE_AS_DIALOG_TITLE); |
| 395 | 395 |
| 396 GtkWidget* dialog = gtk_file_chooser_dialog_new( | 396 GtkWidget* dialog = gtk_file_chooser_dialog_new( |
| 397 title_string.c_str(), NULL, GTK_FILE_CHOOSER_ACTION_SAVE, "_Cancel", | 397 title_string.c_str(), nullptr, GTK_FILE_CHOOSER_ACTION_SAVE, "_Cancel", |
| 398 GTK_RESPONSE_CANCEL, "_Save", GTK_RESPONSE_ACCEPT, nullptr); | 398 GTK_RESPONSE_CANCEL, "_Save", GTK_RESPONSE_ACCEPT, nullptr); |
| 399 SetGtkTransientForAura(dialog, parent); | 399 SetGtkTransientForAura(dialog, parent); |
| 400 | 400 |
| 401 AddFilters(GTK_FILE_CHOOSER(dialog)); | 401 AddFilters(GTK_FILE_CHOOSER(dialog)); |
| 402 if (!default_path.empty()) { | 402 if (!default_path.empty()) { |
| 403 if (CallDirectoryExistsOnUIThread(default_path)) { | 403 if (CallDirectoryExistsOnUIThread(default_path)) { |
| 404 // If this is an existing directory, navigate to that directory, with no | 404 // If this is an existing directory, navigate to that directory, with no |
| 405 // filename. | 405 // filename. |
| 406 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), | 406 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), |
| 407 default_path.value().c_str()); | 407 default_path.value().c_str()); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 return; | 491 return; |
| 492 } | 492 } |
| 493 | 493 |
| 494 GSList* filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog)); | 494 GSList* filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog)); |
| 495 if (!filenames) { | 495 if (!filenames) { |
| 496 FileNotSelected(dialog); | 496 FileNotSelected(dialog); |
| 497 return; | 497 return; |
| 498 } | 498 } |
| 499 | 499 |
| 500 std::vector<base::FilePath> filenames_fp; | 500 std::vector<base::FilePath> filenames_fp; |
| 501 for (GSList* iter = filenames; iter != NULL; iter = g_slist_next(iter)) { | 501 for (GSList* iter = filenames; iter != nullptr; iter = g_slist_next(iter)) { |
| 502 base::FilePath path(static_cast<char*>(iter->data)); | 502 base::FilePath path(static_cast<char*>(iter->data)); |
| 503 g_free(iter->data); | 503 g_free(iter->data); |
| 504 if (CallDirectoryExistsOnUIThread(path)) | 504 if (CallDirectoryExistsOnUIThread(path)) |
| 505 continue; | 505 continue; |
| 506 filenames_fp.push_back(path); | 506 filenames_fp.push_back(path); |
| 507 } | 507 } |
| 508 g_slist_free(filenames); | 508 g_slist_free(filenames); |
| 509 | 509 |
| 510 if (filenames_fp.empty()) { | 510 if (filenames_fp.empty()) { |
| 511 FileNotSelected(dialog); | 511 FileNotSelected(dialog); |
| 512 return; | 512 return; |
| 513 } | 513 } |
| 514 MultiFilesSelected(dialog, filenames_fp); | 514 MultiFilesSelected(dialog, filenames_fp); |
| 515 } | 515 } |
| 516 | 516 |
| 517 void SelectFileDialogImplGTK::OnFileChooserDestroy(GtkWidget* dialog) { | 517 void SelectFileDialogImplGTK::OnFileChooserDestroy(GtkWidget* dialog) { |
| 518 dialogs_.erase(dialog); | 518 dialogs_.erase(dialog); |
| 519 | 519 |
| 520 // |parent| can be NULL when closing the host window | 520 // |parent| can be nullptr when closing the host window |
| 521 // while opening the file-picker. | 521 // while opening the file-picker. |
| 522 aura::Window* parent = GetAuraTransientParent(dialog); | 522 aura::Window* parent = GetAuraTransientParent(dialog); |
| 523 if (!parent) | 523 if (!parent) |
| 524 return; | 524 return; |
| 525 std::set<aura::Window*>::iterator iter = parents_.find(parent); | 525 std::set<aura::Window*>::iterator iter = parents_.find(parent); |
| 526 if (iter != parents_.end()) { | 526 if (iter != parents_.end()) { |
| 527 (*iter)->RemoveObserver(this); | 527 (*iter)->RemoveObserver(this); |
| 528 parents_.erase(iter); | 528 parents_.erase(iter); |
| 529 } else { | 529 } else { |
| 530 NOTREACHED(); | 530 NOTREACHED(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 545 struct stat stat_buf; | 545 struct stat stat_buf; |
| 546 if (stat(filename, &stat_buf) != 0 || !S_ISREG(stat_buf.st_mode)) { | 546 if (stat(filename, &stat_buf) != 0 || !S_ISREG(stat_buf.st_mode)) { |
| 547 g_free(filename); | 547 g_free(filename); |
| 548 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), | 548 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), |
| 549 FALSE); | 549 FALSE); |
| 550 return; | 550 return; |
| 551 } | 551 } |
| 552 | 552 |
| 553 // This will preserve the image's aspect ratio. | 553 // This will preserve the image's aspect ratio. |
| 554 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_size(filename, kPreviewWidth, | 554 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_size(filename, kPreviewWidth, |
| 555 kPreviewHeight, NULL); | 555 kPreviewHeight, nullptr); |
| 556 g_free(filename); | 556 g_free(filename); |
| 557 if (pixbuf) { | 557 if (pixbuf) { |
| 558 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf); | 558 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf); |
| 559 g_object_unref(pixbuf); | 559 g_object_unref(pixbuf); |
| 560 } | 560 } |
| 561 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), | 561 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), |
| 562 pixbuf ? TRUE : FALSE); | 562 pixbuf ? TRUE : FALSE); |
| 563 } | 563 } |
| 564 | 564 |
| 565 } // namespace libgtkui | 565 } // namespace libgtkui |
| OLD | NEW |