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); |
| 59 | 61 |
| 62 // Close the file dialog. | |
| 63 void Close() override; | |
| 64 | |
| 60 protected: | 65 protected: |
| 61 ~SelectFileDialogImplGTK() override; | 66 ~SelectFileDialogImplGTK() override; |
| 62 | 67 |
| 63 // BaseShellDialog implementation: | 68 // BaseShellDialog implementation: |
| 64 bool IsRunning(gfx::NativeWindow parent_window) const override; | 69 bool IsRunning(gfx::NativeWindow parent_window) const override; |
| 65 | 70 |
| 66 // SelectFileDialog implementation. | 71 // SelectFileDialog implementation. |
| 67 // |params| is user data we pass back via the Listener interface. | 72 // |params| is user data we pass back via the Listener interface. |
| 68 void SelectFileImpl(Type type, | 73 void SelectFileImpl(Type type, |
| 69 const base::string16& title, | 74 const base::string16& title, |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); | 287 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); |
| 283 | 288 |
| 284 gtk_widget_show_all(dialog); | 289 gtk_widget_show_all(dialog); |
| 285 | 290 |
| 286 // We need to call gtk_window_present after making the widgets visible to make | 291 // We need to call gtk_window_present after making the widgets visible to make |
| 287 // sure window gets correctly raised and gets focus. | 292 // sure window gets correctly raised and gets focus. |
| 288 int time = views::X11DesktopHandler::get()->wm_user_time_ms(); | 293 int time = views::X11DesktopHandler::get()->wm_user_time_ms(); |
| 289 gtk_window_present_with_time(GTK_WINDOW(dialog), time); | 294 gtk_window_present_with_time(GTK_WINDOW(dialog), time); |
| 290 } | 295 } |
| 291 | 296 |
| 297 void SelectFileDialogImplGTK::Close() { | |
| 298 // Close the file-picker by sending an ESC key event to it. | |
| 299 | |
| 300 // Get the focused dialog. | |
| 301 XID focused_window = 0; | |
| 302 int revert = 0; | |
| 303 XGetInputFocus(gfx::GetXDisplay(), &focused_window, &revert); | |
| 304 | |
| 305 // Check if the focused window is the file-picker dialog. | |
| 306 bool is_dialog_focused = false; | |
| 307 for (auto it = dialogs_.begin(); it != dialogs_.end(); ++it) { | |
| 308 if (focused_window == GDK_WINDOW_XID(gtk_widget_get_window(*it))) { | |
| 309 is_dialog_focused = true; | |
| 310 break; | |
| 311 } | |
| 312 } | |
| 313 | |
| 314 if (!is_dialog_focused) { | |
| 315 NOTREACHED() << "Cannot find the focused dialog"; | |
| 316 return; | |
| 317 } | |
| 318 | |
| 319 // Send an ESC key event to the focused dialog. | |
|
sadrul
2015/10/31 16:36:17
Whoa .. not lgtm
You should close the dialog usin
| |
| 320 XKeyEvent event = {0}; | |
| 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 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 654 g_free(filename); | 692 g_free(filename); |
| 655 if (pixbuf) { | 693 if (pixbuf) { |
| 656 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf); | 694 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf); |
| 657 g_object_unref(pixbuf); | 695 g_object_unref(pixbuf); |
| 658 } | 696 } |
| 659 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), | 697 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), |
| 660 pixbuf ? TRUE : FALSE); | 698 pixbuf ? TRUE : FALSE); |
| 661 } | 699 } |
| 662 | 700 |
| 663 } // namespace libgtk2ui | 701 } // namespace libgtk2ui |
| OLD | NEW |