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

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

Issue 1363093004: Add BrowserSelectFileDialogTest.OpenCloseFileDialog (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: skip libgtk-x11-2.0 from LSan report 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 <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
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 dialog.
304 bool is_dialog_focused = false;
305 for (auto it = dialogs_.begin();
306 it != dialogs_.end(); ++it) {
msw 2015/10/22 22:23:05 nit: fits on line above now.
joone 2015/10/23 10:51:42 Done.
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 NOTREACHED() << "Cannot find the focused dialog";
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698