| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 | |
| 6 #include "views/controls/menu/menu_host_gtk.h" | 5 #include "views/controls/menu/menu_host_gtk.h" |
| 7 | 6 |
| 8 #include <gdk/gdk.h> | 7 #include <gdk/gdk.h> |
| 9 | 8 |
| 10 #include "views/controls/menu/menu_controller.h" | 9 #include "views/controls/menu/menu_controller.h" |
| 11 #include "views/controls/menu/menu_host_root_view.h" | 10 #include "views/controls/menu/menu_host_root_view.h" |
| 12 #include "views/controls/menu/menu_item_view.h" | 11 #include "views/controls/menu/menu_item_view.h" |
| 13 #include "views/controls/menu/submenu_view.h" | 12 #include "views/controls/menu/submenu_view.h" |
| 14 | 13 |
| 15 namespace views { | 14 namespace views { |
| 16 | 15 |
| 17 MenuHost::MenuHost(SubmenuView* submenu) | 16 // static |
| 17 MenuHost* MenuHost::Create(SubmenuView* submenu_view) { |
| 18 return new MenuHostGtk(submenu_view); |
| 19 } |
| 20 |
| 21 MenuHostGtk::MenuHostGtk(SubmenuView* submenu) |
| 18 : WidgetGtk(WidgetGtk::TYPE_POPUP), | 22 : WidgetGtk(WidgetGtk::TYPE_POPUP), |
| 19 closed_(false), | 23 destroying_(false), |
| 20 submenu_(submenu), | 24 submenu_(submenu), |
| 21 did_pointer_grab_(false) { | 25 did_pointer_grab_(false) { |
| 22 GdkEvent* event = gtk_get_current_event(); | 26 GdkEvent* event = gtk_get_current_event(); |
| 23 if (event) { | 27 if (event) { |
| 24 if (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS || | 28 if (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS || |
| 25 event->type == GDK_3BUTTON_PRESS) { | 29 event->type == GDK_3BUTTON_PRESS) { |
| 26 set_mouse_down(true); | 30 set_mouse_down(true); |
| 27 } | 31 } |
| 28 gdk_event_free(event); | 32 gdk_event_free(event); |
| 29 } | 33 } |
| 30 } | 34 } |
| 31 | 35 |
| 32 void MenuHost::Init(gfx::NativeWindow parent, | 36 MenuHostGtk::~MenuHostGtk() { |
| 37 } |
| 38 |
| 39 void MenuHostGtk::Init(gfx::NativeWindow parent, |
| 33 const gfx::Rect& bounds, | 40 const gfx::Rect& bounds, |
| 34 View* contents_view, | 41 View* contents_view, |
| 35 bool do_capture) { | 42 bool do_capture) { |
| 43 make_transient_to_parent(); |
| 36 WidgetGtk::Init(GTK_WIDGET(parent), bounds); | 44 WidgetGtk::Init(GTK_WIDGET(parent), bounds); |
| 45 // Make sure we get destroyed when the parent is destroyed. |
| 46 gtk_window_set_destroy_with_parent(GTK_WINDOW(GetNativeView()), TRUE); |
| 37 gtk_window_set_type_hint(GTK_WINDOW(GetNativeView()), | 47 gtk_window_set_type_hint(GTK_WINDOW(GetNativeView()), |
| 38 GDK_WINDOW_TYPE_HINT_MENU); | 48 GDK_WINDOW_TYPE_HINT_MENU); |
| 39 SetContentsView(contents_view); | 49 SetContentsView(contents_view); |
| 40 Show(); | 50 ShowMenuHost(do_capture); |
| 51 } |
| 52 |
| 53 bool MenuHostGtk::IsMenuHostVisible() { |
| 54 return IsVisible(); |
| 55 } |
| 56 |
| 57 void MenuHostGtk::ShowMenuHost(bool do_capture) { |
| 58 WidgetGtk::Show(); |
| 41 if (do_capture) | 59 if (do_capture) |
| 42 DoCapture(); | 60 DoCapture(); |
| 43 } | 61 } |
| 44 | 62 |
| 45 gfx::NativeWindow MenuHost::GetNativeWindow() { | 63 void MenuHostGtk::HideMenuHost() { |
| 64 // Make sure we release capture before hiding. |
| 65 ReleaseMenuHostCapture(); |
| 66 |
| 67 WidgetGtk::Hide(); |
| 68 } |
| 69 |
| 70 void MenuHostGtk::DestroyMenuHost() { |
| 71 HideMenuHost(); |
| 72 destroying_ = true; |
| 73 CloseNow(); |
| 74 } |
| 75 |
| 76 void MenuHostGtk::SetMenuHostBounds(const gfx::Rect& bounds) { |
| 77 SetBounds(bounds); |
| 78 } |
| 79 |
| 80 void MenuHostGtk::ReleaseMenuHostCapture() { |
| 81 ReleaseGrab(); |
| 82 } |
| 83 |
| 84 gfx::NativeWindow MenuHostGtk::GetMenuHostWindow() { |
| 46 return GTK_WINDOW(GetNativeView()); | 85 return GTK_WINDOW(GetNativeView()); |
| 47 } | 86 } |
| 48 | 87 |
| 49 void MenuHost::Show() { | 88 RootView* MenuHostGtk::CreateRootView() { |
| 50 WidgetGtk::Show(); | 89 return new MenuHostRootView(this, submenu_); |
| 51 } | 90 } |
| 52 | 91 |
| 53 void MenuHost::Hide() { | 92 bool MenuHostGtk::ReleaseCaptureOnMouseReleased() { |
| 54 if (closed_) { | 93 return false; |
| 55 // We're already closed, nothing to do. | |
| 56 // This is invoked twice if the first time just hid us, and the second | |
| 57 // time deleted Closed (deleted) us. | |
| 58 return; | |
| 59 } | |
| 60 // The menus are freed separately, and possibly before the window is closed, | |
| 61 // remove them so that View doesn't try to access deleted objects. | |
| 62 static_cast<MenuHostRootView*>(GetRootView())->suspend_events(); | |
| 63 GetRootView()->RemoveAllChildViews(false); | |
| 64 ReleaseGrab(); | |
| 65 closed_ = true; | |
| 66 WidgetGtk::Hide(); | |
| 67 } | 94 } |
| 68 | 95 |
| 69 void MenuHost::HideWindow() { | 96 void MenuHostGtk::ReleaseGrab() { |
| 70 // Make sure we release capture before hiding. | 97 WidgetGtk::ReleaseGrab(); |
| 71 ReleaseGrab(); | 98 if (did_pointer_grab_) { |
| 72 WidgetGtk::Hide(); | 99 did_pointer_grab_ = false; |
| 100 gdk_pointer_ungrab(GDK_CURRENT_TIME); |
| 101 } |
| 73 } | 102 } |
| 74 | 103 |
| 75 void MenuHost::DoCapture() { | 104 void MenuHostGtk::OnDestroy(GtkWidget* object) { |
| 105 if (!destroying_) { |
| 106 // We weren't explicitly told to destroy ourselves, which means the menu was |
| 107 // deleted out from under us (the window we're parented to was closed). Tell |
| 108 // the SubmenuView to drop references to us. |
| 109 submenu_->MenuHostDestroyed(); |
| 110 } |
| 111 WidgetGtk::OnDestroy(object); |
| 112 } |
| 113 |
| 114 gboolean MenuHostGtk::OnGrabBrokeEvent(GtkWidget* widget, GdkEvent* event) { |
| 115 // Grab breaking only happens when drag and drop starts. So, we don't try |
| 116 // and ungrab or cancel the menu. |
| 117 did_pointer_grab_ = false; |
| 118 return WidgetGtk::OnGrabBrokeEvent(widget, event); |
| 119 } |
| 120 |
| 121 void MenuHostGtk::DoCapture() { |
| 76 // Release the current grab. | 122 // Release the current grab. |
| 77 GtkWidget* current_grab_window = gtk_grab_get_current(); | 123 GtkWidget* current_grab_window = gtk_grab_get_current(); |
| 78 if (current_grab_window) | 124 if (current_grab_window) |
| 79 gtk_grab_remove(current_grab_window); | 125 gtk_grab_remove(current_grab_window); |
| 80 | 126 |
| 81 // Make sure all app mouse events are targetted at us only. | 127 // Make sure all app mouse events are targetted at us only. |
| 82 DoGrab(); | 128 DoGrab(); |
| 83 | 129 |
| 84 // And do a grab. | 130 // And do a grab. |
| 85 // NOTE: we do this to ensure we get mouse events from other apps, a grab | 131 // NOTE: we do this to ensure we get mouse events from other apps, a grab |
| 86 // done with gtk_grab_add doesn't get events from other apps. | 132 // done with gtk_grab_add doesn't get events from other apps. |
| 87 GdkGrabStatus grab_status = | 133 GdkGrabStatus grab_status = |
| 88 gdk_pointer_grab(window_contents()->window, FALSE, | 134 gdk_pointer_grab(window_contents()->window, FALSE, |
| 89 static_cast<GdkEventMask>( | 135 static_cast<GdkEventMask>( |
| 90 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | | 136 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | |
| 91 GDK_POINTER_MOTION_MASK), | 137 GDK_POINTER_MOTION_MASK), |
| 92 NULL, NULL, GDK_CURRENT_TIME); | 138 NULL, NULL, GDK_CURRENT_TIME); |
| 93 did_pointer_grab_ = (grab_status == GDK_GRAB_SUCCESS); | 139 did_pointer_grab_ = (grab_status == GDK_GRAB_SUCCESS); |
| 94 DCHECK(did_pointer_grab_); | 140 DCHECK(did_pointer_grab_); |
| 95 // need keyboard grab. | 141 // need keyboard grab. |
| 96 #ifdef DEBUG_MENU | |
| 97 DLOG(INFO) << "Doing capture"; | |
| 98 #endif | |
| 99 } | |
| 100 | |
| 101 void MenuHost::ReleaseCapture() { | |
| 102 ReleaseGrab(); | |
| 103 } | |
| 104 | |
| 105 RootView* MenuHost::CreateRootView() { | |
| 106 return new MenuHostRootView(this, submenu_); | |
| 107 } | |
| 108 | |
| 109 gboolean MenuHost::OnGrabBrokeEvent(GtkWidget* widget, GdkEvent* event) { | |
| 110 // Grab breaking only happens when drag and drop starts. So, we don't try | |
| 111 // and ungrab or cancel the menu. | |
| 112 did_pointer_grab_ = false; | |
| 113 return WidgetGtk::OnGrabBrokeEvent(widget, event); | |
| 114 } | |
| 115 | |
| 116 // Overriden to return false, we do NOT want to release capture on mouse | |
| 117 // release. | |
| 118 bool MenuHost::ReleaseCaptureOnMouseReleased() { | |
| 119 return false; | |
| 120 } | |
| 121 | |
| 122 void MenuHost::ReleaseGrab() { | |
| 123 WidgetGtk::ReleaseGrab(); | |
| 124 if (did_pointer_grab_) { | |
| 125 did_pointer_grab_ = false; | |
| 126 gdk_pointer_ungrab(GDK_CURRENT_TIME); | |
| 127 } | |
| 128 } | 142 } |
| 129 | 143 |
| 130 } // namespace views | 144 } // namespace views |
| OLD | NEW |