OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/thread.h" | 13 #include "base/thread.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/sys_string_conversions.h" | 15 #include "base/sys_string_conversions.h" |
16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/shell_dialogs.h" | 17 #include "chrome/browser/shell_dialogs.h" |
18 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
19 | 19 |
20 // The size of the preview we display for selected image files. We set height | |
21 // larger than width because generally there is more free space vertically | |
22 // than horiztonally (setting the preview image will alway expand the width of | |
23 // the dialog, but usually not the height). The image's aspect ratio will always | |
24 // be preserved. | |
25 static const int kPreviewWidth = 256; | |
26 static const int kPreviewHeight = 512; | |
27 | |
20 // Implementation of SelectFileDialog that shows a Gtk common dialog for | 28 // Implementation of SelectFileDialog that shows a Gtk common dialog for |
21 // choosing a file or folder. | 29 // choosing a file or folder. |
22 // This acts as a modal dialog. Ideally we want to only act modally for the | 30 // This acts as a modal dialog. Ideally we want to only act modally for the |
23 // parent window and allow other toplevel chrome windows to still function while | 31 // parent window and allow other toplevel chrome windows to still function while |
24 // the dialog is showing, but we need the GtkWindowGroup or something similar to | 32 // the dialog is showing, but we need the GtkWindowGroup or something similar to |
25 // get that, and that API is only available in more recent versions of GTK. | 33 // get that, and that API is only available in more recent versions of GTK. |
26 // TODO(port): fix modality: crbug.com/8727 | 34 // TODO(port): fix modality: crbug.com/8727 |
27 class SelectFileDialogImpl : public SelectFileDialog { | 35 class SelectFileDialogImpl : public SelectFileDialog { |
28 public: | 36 public: |
29 explicit SelectFileDialogImpl(Listener* listener); | 37 explicit SelectFileDialogImpl(Listener* listener); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 static bool IsCancelResponse(gint response_id); | 89 static bool IsCancelResponse(gint response_id); |
82 | 90 |
83 // Callback for when the user responds to a Save As or Open File dialog. | 91 // Callback for when the user responds to a Save As or Open File dialog. |
84 static void OnSelectSingleFileDialogResponse( | 92 static void OnSelectSingleFileDialogResponse( |
85 GtkWidget* dialog, gint response_id, SelectFileDialogImpl* dialog_impl); | 93 GtkWidget* dialog, gint response_id, SelectFileDialogImpl* dialog_impl); |
86 | 94 |
87 // Callback for when the user responds to a Open Multiple Files dialog. | 95 // Callback for when the user responds to a Open Multiple Files dialog. |
88 static void OnSelectMultiFileDialogResponse( | 96 static void OnSelectMultiFileDialogResponse( |
89 GtkWidget* dialog, gint response_id, SelectFileDialogImpl* dialog_impl); | 97 GtkWidget* dialog, gint response_id, SelectFileDialogImpl* dialog_impl); |
90 | 98 |
99 // Callback for when we update the preview for the selection. | |
100 static void OnUpdatePreview(GtkFileChooser* chooser, | |
101 SelectFileDialogImpl* dialog); | |
102 | |
91 // The listener to be notified of selection completion. | 103 // The listener to be notified of selection completion. |
92 Listener* listener_; | 104 Listener* listener_; |
93 | 105 |
94 // A map from dialog windows to the |params| user data associated with them. | 106 // A map from dialog windows to the |params| user data associated with them. |
95 std::map<GtkWidget*, void*> params_map_; | 107 std::map<GtkWidget*, void*> params_map_; |
96 | 108 |
97 // The file filters. | 109 // The file filters. |
98 FileTypeInfo file_types_; | 110 FileTypeInfo file_types_; |
99 | 111 |
100 // The index of the default selected file filter. | 112 // The index of the default selected file filter. |
101 // Note: This starts from 1, not 0. | 113 // Note: This starts from 1, not 0. |
102 size_t file_type_index_; | 114 size_t file_type_index_; |
103 | 115 |
104 // The set of all parent windows for which we are currently running dialogs. | 116 // The set of all parent windows for which we are currently running dialogs. |
105 std::set<GtkWindow*> parents_; | 117 std::set<GtkWindow*> parents_; |
106 | 118 |
107 // The type of dialog we are showing the user. | 119 // The type of dialog we are showing the user. |
108 Type type_; | 120 Type type_; |
109 | 121 |
110 // These two variables track where the user last saved a file or opened a | 122 // These two variables track where the user last saved a file or opened a |
111 // file so that we can display future dialogs with the same starting path. | 123 // file so that we can display future dialogs with the same starting path. |
112 static FilePath* last_saved_path_; | 124 static FilePath* last_saved_path_; |
113 static FilePath* last_opened_path_; | 125 static FilePath* last_opened_path_; |
114 | 126 |
127 // The GtkImage widget for showing previews of selected images. | |
128 GtkWidget* preview_; | |
129 | |
115 DISALLOW_COPY_AND_ASSIGN(SelectFileDialogImpl); | 130 DISALLOW_COPY_AND_ASSIGN(SelectFileDialogImpl); |
116 }; | 131 }; |
117 | 132 |
118 FilePath* SelectFileDialogImpl::last_saved_path_ = NULL; | 133 FilePath* SelectFileDialogImpl::last_saved_path_ = NULL; |
119 FilePath* SelectFileDialogImpl::last_opened_path_ = NULL; | 134 FilePath* SelectFileDialogImpl::last_opened_path_ = NULL; |
120 | 135 |
121 // static | 136 // static |
122 SelectFileDialog* SelectFileDialog::Create(Listener* listener) { | 137 SelectFileDialog* SelectFileDialog::Create(Listener* listener) { |
123 DCHECK(MessageLoop::current() != | 138 DCHECK(MessageLoop::current() != |
124 g_browser_process->io_thread()->message_loop()); | 139 g_browser_process->io_thread()->message_loop()); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 dialog = CreateMultiFileOpenDialog(title_string, owning_window); | 196 dialog = CreateMultiFileOpenDialog(title_string, owning_window); |
182 break; | 197 break; |
183 case SELECT_SAVEAS_FILE: | 198 case SELECT_SAVEAS_FILE: |
184 dialog = CreateSaveAsDialog(title_string, default_path, owning_window); | 199 dialog = CreateSaveAsDialog(title_string, default_path, owning_window); |
185 break; | 200 break; |
186 default: | 201 default: |
187 NOTIMPLEMENTED() << "Dialog type " << type << " not implemented."; | 202 NOTIMPLEMENTED() << "Dialog type " << type << " not implemented."; |
188 return; | 203 return; |
189 } | 204 } |
190 | 205 |
206 preview_ = gtk_image_new(); | |
207 g_signal_connect(dialog, "update-preview", G_CALLBACK(OnUpdatePreview), this); | |
208 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(dialog), preview_); | |
209 | |
191 params_map_[dialog] = params; | 210 params_map_[dialog] = params; |
192 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); | 211 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); |
193 gtk_widget_show_all(dialog); | 212 gtk_widget_show_all(dialog); |
194 } | 213 } |
195 | 214 |
196 void SelectFileDialogImpl::AddFilters(GtkFileChooser* chooser) { | 215 void SelectFileDialogImpl::AddFilters(GtkFileChooser* chooser) { |
197 for (size_t i = 0; i < file_types_.extensions.size(); ++i) { | 216 for (size_t i = 0; i < file_types_.extensions.size(); ++i) { |
198 GtkFileFilter* filter = NULL; | 217 GtkFileFilter* filter = NULL; |
199 for (size_t j = 0; j < file_types_.extensions[i].size(); ++j) { | 218 for (size_t j = 0; j < file_types_.extensions[i].size(); ++j) { |
200 // TODO(estade): it's probably preferable to use mime types, but we are | 219 // TODO(estade): it's probably preferable to use mime types, but we are |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
402 GSList* filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog)); | 421 GSList* filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog)); |
403 std::vector<FilePath> filenames_fp; | 422 std::vector<FilePath> filenames_fp; |
404 for (GSList* iter = filenames; iter != NULL; iter = g_slist_next(iter)) { | 423 for (GSList* iter = filenames; iter != NULL; iter = g_slist_next(iter)) { |
405 filenames_fp.push_back(FilePath(static_cast<char*>(iter->data))); | 424 filenames_fp.push_back(FilePath(static_cast<char*>(iter->data))); |
406 g_free(iter->data); | 425 g_free(iter->data); |
407 } | 426 } |
408 | 427 |
409 g_slist_free(filenames); | 428 g_slist_free(filenames); |
410 dialog_impl->MultiFilesSelected(dialog, filenames_fp); | 429 dialog_impl->MultiFilesSelected(dialog, filenames_fp); |
411 } | 430 } |
431 | |
432 // static | |
433 void SelectFileDialogImpl::OnUpdatePreview(GtkFileChooser* chooser, | |
434 SelectFileDialogImpl* dialog) { | |
435 gchar* filename = gtk_file_chooser_get_preview_filename(chooser); | |
436 if (!filename) | |
437 return; | |
438 // This will preserve the image's aspect ratio. | |
439 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_size(filename, kPreviewWidth, | |
440 kPreviewHeight, NULL); | |
441 g_free(filename); | |
442 if (pixbuf) { | |
443 gtk_image_set_from_pixbuf(GTK_IMAGE(dialog->preview_), pixbuf); | |
444 g_object_unref(pixbuf); | |
445 } | |
446 gtk_file_chooser_set_preview_widget_active(chooser, pixbuf ? true : false); | |
Evan Martin
2009/06/30 20:16:58
maybe TRUE and FALSE here
| |
447 } | |
OLD | NEW |