| 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/libgtk2ui/select_file_dialog_impl_gtk2.h" |
| 6 |
| 5 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 6 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 7 #include <sys/types.h> | 9 #include <sys/types.h> |
| 8 #include <unistd.h> | 10 #include <unistd.h> |
| 9 | 11 |
| 10 #include <map> | 12 #include <map> |
| 11 #include <set> | 13 #include <set> |
| 12 #include <vector> | 14 #include <vector> |
| 13 | 15 |
| 14 // Xlib defines RootWindow | 16 // Xlib defines RootWindow |
| (...skipping 27 matching lines...) Expand all Loading... |
| 42 | 44 |
| 43 // Deletes |data| when gtk_file_filter_add_custom() is done with it. | 45 // Deletes |data| when gtk_file_filter_add_custom() is done with it. |
| 44 void OnFileFilterDataDestroyed(std::string* file_extension) { | 46 void OnFileFilterDataDestroyed(std::string* file_extension) { |
| 45 delete file_extension; | 47 delete file_extension; |
| 46 } | 48 } |
| 47 | 49 |
| 48 } // namespace | 50 } // namespace |
| 49 | 51 |
| 50 namespace libgtk2ui { | 52 namespace libgtk2ui { |
| 51 | 53 |
| 52 // Implementation of SelectFileDialog that shows a Gtk common dialog for | |
| 53 // choosing a file or folder. This acts as a modal dialog. | |
| 54 class SelectFileDialogImplGTK : public SelectFileDialogImpl, | |
| 55 public aura::WindowObserver { | |
| 56 public: | |
| 57 explicit SelectFileDialogImplGTK(Listener* listener, | |
| 58 ui::SelectFilePolicy* policy); | |
| 59 | |
| 60 protected: | |
| 61 ~SelectFileDialogImplGTK() override; | |
| 62 | |
| 63 // BaseShellDialog implementation: | |
| 64 bool IsRunning(gfx::NativeWindow parent_window) const override; | |
| 65 | |
| 66 // SelectFileDialog implementation. | |
| 67 // |params| is user data we pass back via the Listener interface. | |
| 68 void SelectFileImpl(Type type, | |
| 69 const base::string16& title, | |
| 70 const base::FilePath& default_path, | |
| 71 const FileTypeInfo* file_types, | |
| 72 int file_type_index, | |
| 73 const base::FilePath::StringType& default_extension, | |
| 74 gfx::NativeWindow owning_window, | |
| 75 void* params) override; | |
| 76 | |
| 77 private: | |
| 78 bool HasMultipleFileTypeChoicesImpl() override; | |
| 79 | |
| 80 // Overridden from aura::WindowObserver: | |
| 81 void OnWindowDestroying(aura::Window* window) override; | |
| 82 | |
| 83 // Add the filters from |file_types_| to |chooser|. | |
| 84 void AddFilters(GtkFileChooser* chooser); | |
| 85 | |
| 86 // Notifies the listener that a single file was chosen. | |
| 87 void FileSelected(GtkWidget* dialog, const base::FilePath& path); | |
| 88 | |
| 89 // Notifies the listener that multiple files were chosen. | |
| 90 void MultiFilesSelected(GtkWidget* dialog, | |
| 91 const std::vector<base::FilePath>& files); | |
| 92 | |
| 93 // Notifies the listener that no file was chosen (the action was canceled). | |
| 94 // Dialog is passed so we can find that |params| pointer that was passed to | |
| 95 // us when we were told to show the dialog. | |
| 96 void FileNotSelected(GtkWidget* dialog); | |
| 97 | |
| 98 GtkWidget* CreateSelectFolderDialog( | |
| 99 Type type, | |
| 100 const std::string& title, | |
| 101 const base::FilePath& default_path, | |
| 102 gfx::NativeWindow parent); | |
| 103 | |
| 104 GtkWidget* CreateFileOpenDialog(const std::string& title, | |
| 105 const base::FilePath& default_path, gfx::NativeWindow parent); | |
| 106 | |
| 107 GtkWidget* CreateMultiFileOpenDialog(const std::string& title, | |
| 108 const base::FilePath& default_path, gfx::NativeWindow parent); | |
| 109 | |
| 110 GtkWidget* CreateSaveAsDialog(const std::string& title, | |
| 111 const base::FilePath& default_path, gfx::NativeWindow parent); | |
| 112 | |
| 113 // Removes and returns the |params| associated with |dialog| from | |
| 114 // |params_map_|. | |
| 115 void* PopParamsForDialog(GtkWidget* dialog); | |
| 116 | |
| 117 // Take care of internal data structures when a file dialog is destroyed. | |
| 118 void FileDialogDestroyed(GtkWidget* dialog); | |
| 119 | |
| 120 // Check whether response_id corresponds to the user cancelling/closing the | |
| 121 // dialog. Used as a helper for the below callbacks. | |
| 122 bool IsCancelResponse(gint response_id); | |
| 123 | |
| 124 // Common function for OnSelectSingleFileDialogResponse and | |
| 125 // OnSelectSingleFolderDialogResponse. | |
| 126 void SelectSingleFileHelper(GtkWidget* dialog, | |
| 127 gint response_id, | |
| 128 bool allow_folder); | |
| 129 | |
| 130 // Common function for CreateFileOpenDialog and CreateMultiFileOpenDialog. | |
| 131 GtkWidget* CreateFileOpenHelper(const std::string& title, | |
| 132 const base::FilePath& default_path, | |
| 133 gfx::NativeWindow parent); | |
| 134 | |
| 135 // Callback for when the user responds to a Save As or Open File dialog. | |
| 136 CHROMEGTK_CALLBACK_1(SelectFileDialogImplGTK, void, | |
| 137 OnSelectSingleFileDialogResponse, int); | |
| 138 | |
| 139 // Callback for when the user responds to a Select Folder dialog. | |
| 140 CHROMEGTK_CALLBACK_1(SelectFileDialogImplGTK, void, | |
| 141 OnSelectSingleFolderDialogResponse, int); | |
| 142 | |
| 143 // Callback for when the user responds to a Open Multiple Files dialog. | |
| 144 CHROMEGTK_CALLBACK_1(SelectFileDialogImplGTK, void, | |
| 145 OnSelectMultiFileDialogResponse, int); | |
| 146 | |
| 147 // Callback for when the file chooser gets destroyed. | |
| 148 CHROMEGTK_CALLBACK_0(SelectFileDialogImplGTK, void, OnFileChooserDestroy); | |
| 149 | |
| 150 // Callback for when we update the preview for the selection. | |
| 151 CHROMEGTK_CALLBACK_0(SelectFileDialogImplGTK, void, OnUpdatePreview); | |
| 152 | |
| 153 // A map from dialog windows to the |params| user data associated with them. | |
| 154 std::map<GtkWidget*, void*> params_map_; | |
| 155 | |
| 156 // The GtkImage widget for showing previews of selected images. | |
| 157 GtkWidget* preview_; | |
| 158 | |
| 159 // All our dialogs. | |
| 160 std::set<GtkWidget*> dialogs_; | |
| 161 | |
| 162 // The set of all parent windows for which we are currently running dialogs. | |
| 163 std::set<aura::Window*> parents_; | |
| 164 | |
| 165 DISALLOW_COPY_AND_ASSIGN(SelectFileDialogImplGTK); | |
| 166 }; | |
| 167 | |
| 168 // The size of the preview we display for selected image files. We set height | 54 // The size of the preview we display for selected image files. We set height |
| 169 // larger than width because generally there is more free space vertically | 55 // larger than width because generally there is more free space vertically |
| 170 // than horiztonally (setting the preview image will alway expand the width of | 56 // than horiztonally (setting the preview image will alway expand the width of |
| 171 // the dialog, but usually not the height). The image's aspect ratio will always | 57 // the dialog, but usually not the height). The image's aspect ratio will always |
| 172 // be preserved. | 58 // be preserved. |
| 173 static const int kPreviewWidth = 256; | 59 static const int kPreviewWidth = 256; |
| 174 static const int kPreviewHeight = 512; | 60 static const int kPreviewHeight = 512; |
| 175 | 61 |
| 176 SelectFileDialogImpl* SelectFileDialogImpl::NewSelectFileDialogImplGTK( | 62 SelectFileDialogImpl* SelectFileDialogImpl::NewSelectFileDialogImplGTK( |
| 177 Listener* listener, ui::SelectFilePolicy* policy) { | 63 Listener* listener, ui::SelectFilePolicy* policy) { |
| (...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 654 g_free(filename); | 540 g_free(filename); |
| 655 if (pixbuf) { | 541 if (pixbuf) { |
| 656 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf); | 542 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf); |
| 657 g_object_unref(pixbuf); | 543 g_object_unref(pixbuf); |
| 658 } | 544 } |
| 659 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), | 545 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), |
| 660 pixbuf ? TRUE : FALSE); | 546 pixbuf ? TRUE : FALSE); |
| 661 } | 547 } |
| 662 | 548 |
| 663 } // namespace libgtk2ui | 549 } // namespace libgtk2ui |
| OLD | NEW |