Chromium Code Reviews| 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 <gdk/gdkx.h> | |
| 5 #include <gtk/gtk.h> | 6 #include <gtk/gtk.h> |
| 6 #include <sys/stat.h> | 7 #include <sys/stat.h> |
| 7 #include <sys/types.h> | 8 #include <sys/types.h> |
| 8 #include <unistd.h> | 9 #include <unistd.h> |
| 9 | 10 |
| 10 #include <map> | 11 #include <map> |
| 11 #include <set> | 12 #include <set> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 14 // Xlib defines RootWindow | 15 // Xlib defines RootWindow |
| 15 #undef RootWindow | 16 #undef RootWindow |
| 16 | 17 |
| 17 #include "base/logging.h" | 18 #include "base/logging.h" |
| 18 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
| 20 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 21 #include "base/strings/sys_string_conversions.h" | 22 #include "base/strings/sys_string_conversions.h" |
| 22 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/threading/thread.h" | 24 #include "base/threading/thread.h" |
| 24 #include "base/threading/thread_restrictions.h" | 25 #include "base/threading/thread_restrictions.h" |
| 25 #include "chrome/browser/ui/libgtk2ui/gtk2_signal.h" | 26 #include "chrome/browser/ui/libgtk2ui/gtk2_signal.h" |
| 26 #include "chrome/browser/ui/libgtk2ui/gtk2_util.h" | 27 #include "chrome/browser/ui/libgtk2ui/gtk2_util.h" |
| 27 #include "chrome/browser/ui/libgtk2ui/select_file_dialog_impl.h" | 28 #include "chrome/browser/ui/libgtk2ui/select_file_dialog_impl.h" |
| 28 #include "ui/aura/window_observer.h" | 29 #include "ui/aura/window_observer.h" |
| 29 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
| 30 #include "ui/shell_dialogs/select_file_dialog.h" | 31 #include "ui/shell_dialogs/select_file_dialog.h" |
| 31 #include "ui/strings/grit/ui_strings.h" | 32 #include "ui/strings/grit/ui_strings.h" |
| 33 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h" | |
| 32 #include "ui/views/widget/desktop_aura/x11_desktop_handler.h" | 34 #include "ui/views/widget/desktop_aura/x11_desktop_handler.h" |
| 33 | 35 |
| 34 namespace { | 36 namespace { |
| 35 | 37 |
| 36 // Makes sure that .jpg also shows .JPG. | 38 // Makes sure that .jpg also shows .JPG. |
| 37 gboolean FileFilterCaseInsensitive(const GtkFileFilterInfo* file_info, | 39 gboolean FileFilterCaseInsensitive(const GtkFileFilterInfo* file_info, |
| 38 std::string* file_extension) { | 40 std::string* file_extension) { |
| 39 return base::EndsWith(file_info->filename, *file_extension, | 41 return base::EndsWith(file_info->filename, *file_extension, |
| 40 base::CompareCase::INSENSITIVE_ASCII); | 42 base::CompareCase::INSENSITIVE_ASCII); |
| 41 } | 43 } |
| 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 | 54 // Implementation of SelectFileDialog that shows a Gtk common dialog for |
| 53 // choosing a file or folder. This acts as a modal dialog. | 55 // choosing a file or folder. This acts as a modal dialog. |
| 54 class SelectFileDialogImplGTK : public SelectFileDialogImpl, | 56 class SelectFileDialogImplGTK : public SelectFileDialogImpl, |
| 55 public aura::WindowObserver { | 57 public aura::WindowObserver { |
| 56 public: | 58 public: |
| 57 explicit SelectFileDialogImplGTK(Listener* listener, | 59 explicit SelectFileDialogImplGTK(Listener* listener, |
| 58 ui::SelectFilePolicy* policy); | 60 ui::SelectFilePolicy* policy); |
| 61 // Close the file dialog. | |
| 62 void Close() override; | |
| 59 | 63 |
| 60 protected: | 64 protected: |
| 61 ~SelectFileDialogImplGTK() override; | 65 ~SelectFileDialogImplGTK() override; |
| 62 | 66 |
| 63 // BaseShellDialog implementation: | 67 // BaseShellDialog implementation: |
| 64 bool IsRunning(gfx::NativeWindow parent_window) const override; | 68 bool IsRunning(gfx::NativeWindow parent_window) const override; |
| 65 | 69 |
| 66 // SelectFileDialog implementation. | 70 // SelectFileDialog implementation. |
| 67 // |params| is user data we pass back via the Listener interface. | 71 // |params| is user data we pass back via the Listener interface. |
| 68 void SelectFileImpl(Type type, | 72 void SelectFileImpl(Type type, |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); | 286 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); |
| 283 | 287 |
| 284 gtk_widget_show_all(dialog); | 288 gtk_widget_show_all(dialog); |
| 285 | 289 |
| 286 // We need to call gtk_window_present after making the widgets visible to make | 290 // We need to call gtk_window_present after making the widgets visible to make |
| 287 // sure window gets correctly raised and gets focus. | 291 // sure window gets correctly raised and gets focus. |
| 288 int time = views::X11DesktopHandler::get()->wm_user_time_ms(); | 292 int time = views::X11DesktopHandler::get()->wm_user_time_ms(); |
| 289 gtk_window_present_with_time(GTK_WINDOW(dialog), time); | 293 gtk_window_present_with_time(GTK_WINDOW(dialog), time); |
| 290 } | 294 } |
| 291 | 295 |
| 296 // Close the file-picker by sending an ESC key event to it. | |
| 297 void SelectFileDialogImplGTK::Close() { | |
| 298 // Get the focused window. | |
| 299 XID focused_window; | |
| 300 int revert; | |
| 301 XGetInputFocus(gfx::GetXDisplay(), &focused_window, &revert); | |
| 302 | |
| 303 // Check if the focused window is the file-picker dailog. | |
|
msw
2015/10/20 23:18:14
nit: 'dialog'
joone
2015/10/22 02:14:47
Done.
| |
| 304 bool is_dialog_focused = false; | |
| 305 for (std::set<GtkWidget*>::iterator it = dialogs_.begin(); | |
|
msw
2015/10/20 23:18:14
nit: "auto it"
joone
2015/10/22 02:14:47
Done.
| |
| 306 it != dialogs_.end(); ++it) { | |
| 307 if (focused_window == GDK_WINDOW_XID(gtk_widget_get_window(*it))) { | |
| 308 is_dialog_focused = true; | |
| 309 break; | |
| 310 } | |
| 311 } | |
| 312 | |
| 313 if (!is_dialog_focused) { | |
| 314 LOG(ERROR) << "Cannot find the focused dialog"; | |
|
msw
2015/10/20 23:18:14
nit: make this a "NOTREACHED() << ...;" to crash i
joone
2015/10/22 02:14:47
Done.
| |
| 315 return; | |
| 316 } | |
| 317 | |
| 318 // Send an ESC key event to the focused_window. | |
| 319 XKeyEvent event; | |
| 320 memset(&event, 0, sizeof(event)); | |
| 321 event.display = gfx::GetXDisplay(); | |
| 322 event.window = focused_window; | |
| 323 event.keycode = XKeysymToKeycode(gfx::GetXDisplay(), XK_Escape); | |
| 324 event.type = KeyPress; | |
| 325 | |
| 326 XSendEvent(gfx::GetXDisplay(), focused_window, True, KeyPressMask, | |
| 327 reinterpret_cast<XEvent*>(&event)); | |
| 328 } | |
| 329 | |
| 292 void SelectFileDialogImplGTK::AddFilters(GtkFileChooser* chooser) { | 330 void SelectFileDialogImplGTK::AddFilters(GtkFileChooser* chooser) { |
| 293 for (size_t i = 0; i < file_types_.extensions.size(); ++i) { | 331 for (size_t i = 0; i < file_types_.extensions.size(); ++i) { |
| 294 GtkFileFilter* filter = NULL; | 332 GtkFileFilter* filter = NULL; |
| 295 std::set<std::string> fallback_labels; | 333 std::set<std::string> fallback_labels; |
| 296 | 334 |
| 297 for (size_t j = 0; j < file_types_.extensions[i].size(); ++j) { | 335 for (size_t j = 0; j < file_types_.extensions[i].size(); ++j) { |
| 298 const std::string& current_extension = file_types_.extensions[i][j]; | 336 const std::string& current_extension = file_types_.extensions[i][j]; |
| 299 if (!current_extension.empty()) { | 337 if (!current_extension.empty()) { |
| 300 if (!filter) | 338 if (!filter) |
| 301 filter = gtk_file_filter_new(); | 339 filter = gtk_file_filter_new(); |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 647 g_free(filename); | 685 g_free(filename); |
| 648 if (pixbuf) { | 686 if (pixbuf) { |
| 649 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf); | 687 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf); |
| 650 g_object_unref(pixbuf); | 688 g_object_unref(pixbuf); |
| 651 } | 689 } |
| 652 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), | 690 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), |
| 653 pixbuf ? TRUE : FALSE); | 691 pixbuf ? TRUE : FALSE); |
| 654 } | 692 } |
| 655 | 693 |
| 656 } // namespace libgtk2ui | 694 } // namespace libgtk2ui |
| OLD | NEW |