Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Side by Side Diff: chrome/browser/ui/libgtk2ui/select_file_dialog_impl_gtk2.cc

Issue 1233913009: Make File-Picker modal on Linux (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: close the file-picker when the test is done. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <map> 7 #include <map>
7 #include <set> 8 #include <set>
8 #include <vector> 9 #include <vector>
9 10
10 // Xlib defines RootWindow 11 // Xlib defines RootWindow
11 #undef RootWindow 12 #undef RootWindow
12 13
13 #include "base/logging.h" 14 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h" 16 #include "base/message_loop/message_loop.h"
16 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
17 #include "base/strings/sys_string_conversions.h" 18 #include "base/strings/sys_string_conversions.h"
18 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
19 #include "base/threading/thread.h" 20 #include "base/threading/thread.h"
20 #include "base/threading/thread_restrictions.h" 21 #include "base/threading/thread_restrictions.h"
21 #include "chrome/browser/ui/libgtk2ui/gtk2_signal.h" 22 #include "chrome/browser/ui/libgtk2ui/gtk2_signal.h"
22 #include "chrome/browser/ui/libgtk2ui/gtk2_util.h" 23 #include "chrome/browser/ui/libgtk2ui/gtk2_util.h"
23 #include "chrome/browser/ui/libgtk2ui/select_file_dialog_impl.h" 24 #include "chrome/browser/ui/libgtk2ui/select_file_dialog_impl.h"
24 #include "ui/aura/window_observer.h" 25 #include "ui/aura/window_observer.h"
25 #include "ui/base/l10n/l10n_util.h" 26 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/shell_dialogs/select_file_dialog.h" 27 #include "ui/shell_dialogs/select_file_dialog.h"
27 #include "ui/strings/grit/ui_strings.h" 28 #include "ui/strings/grit/ui_strings.h"
29 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
28 #include "ui/views/widget/desktop_aura/x11_desktop_handler.h" 30 #include "ui/views/widget/desktop_aura/x11_desktop_handler.h"
29 31
30 namespace { 32 namespace {
31 33
32 // Makes sure that .jpg also shows .JPG. 34 // Makes sure that .jpg also shows .JPG.
33 gboolean FileFilterCaseInsensitive(const GtkFileFilterInfo* file_info, 35 gboolean FileFilterCaseInsensitive(const GtkFileFilterInfo* file_info,
34 std::string* file_extension) { 36 std::string* file_extension) {
35 return base::EndsWith(file_info->filename, *file_extension, 37 return base::EndsWith(file_info->filename, *file_extension,
36 base::CompareCase::INSENSITIVE_ASCII); 38 base::CompareCase::INSENSITIVE_ASCII);
37 } 39 }
(...skipping 24 matching lines...) Expand all
62 // SelectFileDialog implementation. 64 // SelectFileDialog implementation.
63 // |params| is user data we pass back via the Listener interface. 65 // |params| is user data we pass back via the Listener interface.
64 void SelectFileImpl(Type type, 66 void SelectFileImpl(Type type,
65 const base::string16& title, 67 const base::string16& title,
66 const base::FilePath& default_path, 68 const base::FilePath& default_path,
67 const FileTypeInfo* file_types, 69 const FileTypeInfo* file_types,
68 int file_type_index, 70 int file_type_index,
69 const base::FilePath::StringType& default_extension, 71 const base::FilePath::StringType& default_extension,
70 gfx::NativeWindow owning_window, 72 gfx::NativeWindow owning_window,
71 void* params) override; 73 void* params) override;
74 // Close the file dialog.
75 void CloseImpl() override;
72 76
73 private: 77 private:
74 bool HasMultipleFileTypeChoicesImpl() override; 78 bool HasMultipleFileTypeChoicesImpl() override;
75 79
76 // Overridden from aura::WindowObserver: 80 // Overridden from aura::WindowObserver:
77 void OnWindowDestroying(aura::Window* window) override; 81 void OnWindowDestroying(aura::Window* window) override;
78 82
79 // Add the filters from |file_types_| to |chooser|. 83 // Add the filters from |file_types_| to |chooser|.
80 void AddFilters(GtkFileChooser* chooser); 84 void AddFilters(GtkFileChooser* chooser);
81 85
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 270
267 preview_ = gtk_image_new(); 271 preview_ = gtk_image_new();
268 g_signal_connect(dialog, "destroy", 272 g_signal_connect(dialog, "destroy",
269 G_CALLBACK(OnFileChooserDestroyThunk), this); 273 G_CALLBACK(OnFileChooserDestroyThunk), this);
270 g_signal_connect(dialog, "update-preview", 274 g_signal_connect(dialog, "update-preview",
271 G_CALLBACK(OnUpdatePreviewThunk), this); 275 G_CALLBACK(OnUpdatePreviewThunk), this);
272 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(dialog), preview_); 276 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(dialog), preview_);
273 277
274 params_map_[dialog] = params; 278 params_map_[dialog] = params;
275 279
276 // TODO(erg): Figure out how to fake GTK window-to-parent modality without 280 // Disable input events handling in the host window to make this dialog modal.
277 // having the parent be a real GtkWindow. 281 views::DesktopWindowTreeHostX11* host =
278 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); 282 views::DesktopWindowTreeHostX11::GetHostForXID(
283 owning_window->GetHost()->GetAcceleratedWidget());
284 host->DisableEventListening(GDK_WINDOW_XID(gtk_widget_get_window(dialog)));
279 285
280 gtk_widget_show_all(dialog); 286 gtk_widget_show_all(dialog);
281 287
282 // We need to call gtk_window_present after making the widgets visible to make 288 // We need to call gtk_window_present after making the widgets visible to make
283 // sure window gets correctly raised and gets focus. 289 // sure window gets correctly raised and gets focus.
284 int time = views::X11DesktopHandler::get()->wm_user_time_ms(); 290 int time = views::X11DesktopHandler::get()->wm_user_time_ms();
285 gtk_window_present_with_time(GTK_WINDOW(dialog), time); 291 gtk_window_present_with_time(GTK_WINDOW(dialog), time);
286 } 292 }
287 293
294 // Close the file-picker by sending an ESC key event to it.
295 void SelectFileDialogImplGTK::CloseImpl() {
296 // Get the focused window.
297 XID focused_window;
298 int revert;
299 XGetInputFocus(gfx::GetXDisplay(), &focused_window, &revert);
300
301 // Send an ESC key event to the focused_window.
302 XKeyEvent event;
303 memset(&event, 0, sizeof(event));
304 event.display = gfx::GetXDisplay();
305 event.window = focused_window;
306 event.keycode = XKeysymToKeycode(gfx::GetXDisplay(), XK_Escape);
307 event.type = KeyPress;
308
309 XSendEvent(gfx::GetXDisplay(), focused_window, True, KeyPressMask,
310 reinterpret_cast<XEvent*>(&event));
311 }
312
288 void SelectFileDialogImplGTK::AddFilters(GtkFileChooser* chooser) { 313 void SelectFileDialogImplGTK::AddFilters(GtkFileChooser* chooser) {
289 for (size_t i = 0; i < file_types_.extensions.size(); ++i) { 314 for (size_t i = 0; i < file_types_.extensions.size(); ++i) {
290 GtkFileFilter* filter = NULL; 315 GtkFileFilter* filter = NULL;
291 std::set<std::string> fallback_labels; 316 std::set<std::string> fallback_labels;
292 317
293 for (size_t j = 0; j < file_types_.extensions[i].size(); ++j) { 318 for (size_t j = 0; j < file_types_.extensions[i].size(); ++j) {
294 const std::string& current_extension = file_types_.extensions[i][j]; 319 const std::string& current_extension = file_types_.extensions[i][j];
295 if (!current_extension.empty()) { 320 if (!current_extension.empty()) {
296 if (!filter) 321 if (!filter)
297 filter = gtk_file_filter_new(); 322 filter = gtk_file_filter_new();
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 dialogs_.erase(dialog); 545 dialogs_.erase(dialog);
521 546
522 // Parent may be NULL in a few cases: 1) on shutdown when 547 // Parent may be NULL in a few cases: 1) on shutdown when
523 // AllBrowsersClosed() trigger this handler after all the browser 548 // AllBrowsersClosed() trigger this handler after all the browser
524 // windows got destroyed, or 2) when the parent tab has been opened by 549 // windows got destroyed, or 2) when the parent tab has been opened by
525 // 'Open Link in New Tab' context menu on a downloadable item and 550 // 'Open Link in New Tab' context menu on a downloadable item and
526 // the tab has no content (see the comment in SelectFile as well). 551 // the tab has no content (see the comment in SelectFile as well).
527 aura::Window* parent = GetAuraTransientParent(dialog); 552 aura::Window* parent = GetAuraTransientParent(dialog);
528 if (!parent) 553 if (!parent)
529 return; 554 return;
555
556 views::DesktopWindowTreeHostX11* host =
557 views::DesktopWindowTreeHostX11::GetHostForXID(
558 parent->GetHost()->GetAcceleratedWidget());
559 host->EnableEventListening();
560
530 std::set<aura::Window*>::iterator iter = parents_.find(parent); 561 std::set<aura::Window*>::iterator iter = parents_.find(parent);
531 if (iter != parents_.end()) { 562 if (iter != parents_.end()) {
532 (*iter)->RemoveObserver(this); 563 (*iter)->RemoveObserver(this);
533 parents_.erase(iter); 564 parents_.erase(iter);
534 } else { 565 } else {
535 NOTREACHED(); 566 NOTREACHED();
536 } 567 }
537 } 568 }
538 569
539 bool SelectFileDialogImplGTK::IsCancelResponse(gint response_id) { 570 bool SelectFileDialogImplGTK::IsCancelResponse(gint response_id) {
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 g_free(filename); 660 g_free(filename);
630 if (pixbuf) { 661 if (pixbuf) {
631 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf); 662 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf);
632 g_object_unref(pixbuf); 663 g_object_unref(pixbuf);
633 } 664 }
634 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), 665 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser),
635 pixbuf ? TRUE : FALSE); 666 pixbuf ? TRUE : FALSE);
636 } 667 }
637 668
638 } // namespace libgtk2ui 669 } // namespace libgtk2ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698