| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "views/controls/native_control_gtk.h" | 5 #include "views/controls/native_control_gtk.h" |
| 6 | 6 |
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "ui/base/accessibility/accessibility_types.h" | 10 #include "ui/base/accessibility/accessibility_types.h" |
| 11 #include "ui/views/focus/focus_manager.h" | 11 #include "ui/views/focus/focus_manager.h" |
| 12 #include "ui/views/widget/widget.h" | 12 #include "ui/views/widget/widget.h" |
| 13 | 13 |
| 14 #if defined(TOUCH_UI) | |
| 15 namespace { | |
| 16 | |
| 17 GdkEvent* MouseButtonEvent(const views::MouseEvent& mouseev, | |
| 18 const views::NativeViewHost* source) { | |
| 19 GdkEvent* event = NULL; | |
| 20 switch (mouseev.type()) { | |
| 21 case ui::ET_MOUSE_PRESSED: | |
| 22 event = gdk_event_new(GDK_BUTTON_PRESS); | |
| 23 break; | |
| 24 case ui::ET_MOUSE_RELEASED: | |
| 25 event = gdk_event_new(GDK_BUTTON_RELEASE); | |
| 26 break; | |
| 27 default: | |
| 28 NOTREACHED(); | |
| 29 return NULL; | |
| 30 } | |
| 31 event->button.send_event = false; | |
| 32 event->button.time = GDK_CURRENT_TIME; | |
| 33 | |
| 34 // Ideally using native_view()->window should work, but it doesn't. | |
| 35 event->button.window = gdk_window_at_pointer(NULL, NULL); | |
| 36 g_object_ref(event->button.window); | |
| 37 event->button.x = mouseev.location().x(); | |
| 38 event->button.y = mouseev.location().y(); | |
| 39 | |
| 40 gfx::Point point = mouseev.location(); | |
| 41 views::View::ConvertPointToScreen(source, &point); | |
| 42 event->button.x_root = point.x(); | |
| 43 event->button.y_root = point.y(); | |
| 44 | |
| 45 event->button.axes = NULL; | |
| 46 | |
| 47 // Ideally, this should reconstruct the state from mouseev.flags(). | |
| 48 GdkModifierType modifier; | |
| 49 gdk_window_get_pointer(event->button.window, NULL, NULL, &modifier); | |
| 50 event->button.state = modifier; | |
| 51 | |
| 52 event->button.button = (mouseev.flags() & ui::EF_LEFT_BUTTON_DOWN) ? 1 : | |
| 53 (mouseev.flags() & ui::EF_RIGHT_BUTTON_DOWN) ? 3 : | |
| 54 2; | |
| 55 event->button.device = gdk_device_get_core_pointer(); | |
| 56 return event; | |
| 57 } | |
| 58 | |
| 59 GdkEvent* MouseMoveEvent(const views::MouseEvent& mouseev, | |
| 60 const views::NativeViewHost* source) { | |
| 61 GdkEvent* event = gdk_event_new(GDK_MOTION_NOTIFY); | |
| 62 event->motion.send_event = false; | |
| 63 event->motion.time = GDK_CURRENT_TIME; | |
| 64 | |
| 65 event->motion.window = source->native_view()->window; | |
| 66 g_object_ref(event->motion.window); | |
| 67 event->motion.x = mouseev.location().x(); | |
| 68 event->motion.y = mouseev.location().y(); | |
| 69 | |
| 70 gfx::Point point = mouseev.location(); | |
| 71 views::View::ConvertPointToScreen(source, &point); | |
| 72 event->motion.x_root = point.x(); | |
| 73 event->motion.y_root = point.y(); | |
| 74 | |
| 75 event->motion.device = gdk_device_get_core_pointer(); | |
| 76 return event; | |
| 77 } | |
| 78 | |
| 79 GdkEvent* MouseCrossEvent(const views::MouseEvent& mouseev, | |
| 80 const views::NativeViewHost* source) { | |
| 81 GdkEvent* event = NULL; | |
| 82 switch (mouseev.type()) { | |
| 83 case ui::ET_MOUSE_ENTERED: | |
| 84 event = gdk_event_new(GDK_ENTER_NOTIFY); | |
| 85 break; | |
| 86 case ui::ET_MOUSE_EXITED: | |
| 87 event = gdk_event_new(GDK_LEAVE_NOTIFY); | |
| 88 break; | |
| 89 default: | |
| 90 NOTREACHED(); | |
| 91 return NULL; | |
| 92 } | |
| 93 event->crossing.send_event = false; | |
| 94 event->crossing.time = GDK_CURRENT_TIME; | |
| 95 | |
| 96 event->crossing.window = source->native_view()->window; | |
| 97 g_object_ref(event->crossing.window); | |
| 98 | |
| 99 event->crossing.x = event->crossing.y = 0; | |
| 100 event->crossing.x_root = event->crossing.y_root = 0; | |
| 101 event->crossing.mode = GDK_CROSSING_NORMAL; | |
| 102 event->crossing.detail = GDK_NOTIFY_VIRTUAL; | |
| 103 event->crossing.focus = false; | |
| 104 event->crossing.state = 0; | |
| 105 | |
| 106 return event; | |
| 107 } | |
| 108 | |
| 109 } // namespace | |
| 110 #endif // defined(TOUCH_UI) | |
| 111 | |
| 112 namespace views { | 14 namespace views { |
| 113 | 15 |
| 114 #if defined(TOUCH_UI) | |
| 115 void NativeControlGtk::FakeNativeMouseEvent(const MouseEvent& mouseev) { | |
| 116 GdkEvent* event = NULL; | |
| 117 switch (mouseev.type()) { | |
| 118 case ui::ET_MOUSE_PRESSED: | |
| 119 case ui::ET_MOUSE_RELEASED: | |
| 120 event = MouseButtonEvent(mouseev, this); | |
| 121 break; | |
| 122 case ui::ET_MOUSE_MOVED: | |
| 123 event = MouseMoveEvent(mouseev, this); | |
| 124 break; | |
| 125 case ui::ET_MOUSE_ENTERED: | |
| 126 case ui::ET_MOUSE_EXITED: | |
| 127 event = MouseCrossEvent(mouseev, this); | |
| 128 break; | |
| 129 default: | |
| 130 NOTREACHED(); | |
| 131 return; | |
| 132 } | |
| 133 | |
| 134 if (event) { | |
| 135 // Do not gdk_event_put, since that will end up going through the | |
| 136 // message-loop and turn into an infinite loop. | |
| 137 gtk_widget_event(native_view(), event); | |
| 138 gdk_event_free(event); | |
| 139 } | |
| 140 } | |
| 141 #endif // defined(TOUCH_UI) | |
| 142 | |
| 143 NativeControlGtk::NativeControlGtk() { | 16 NativeControlGtk::NativeControlGtk() { |
| 144 } | 17 } |
| 145 | 18 |
| 146 NativeControlGtk::~NativeControlGtk() { | 19 NativeControlGtk::~NativeControlGtk() { |
| 147 if (native_view()) | 20 if (native_view()) |
| 148 gtk_widget_destroy(native_view()); | 21 gtk_widget_destroy(native_view()); |
| 149 } | 22 } |
| 150 | 23 |
| 151 //////////////////////////////////////////////////////////////////////////////// | 24 //////////////////////////////////////////////////////////////////////////////// |
| 152 // NativeControlGtk, View overrides: | 25 // NativeControlGtk, View overrides: |
| (...skipping 29 matching lines...) Expand all Loading... |
| 182 } | 55 } |
| 183 } | 56 } |
| 184 | 57 |
| 185 void NativeControlGtk::OnFocus() { | 58 void NativeControlGtk::OnFocus() { |
| 186 DCHECK(native_view()); | 59 DCHECK(native_view()); |
| 187 gtk_widget_grab_focus(native_view()); | 60 gtk_widget_grab_focus(native_view()); |
| 188 GetWidget()->NotifyAccessibilityEvent( | 61 GetWidget()->NotifyAccessibilityEvent( |
| 189 parent(), ui::AccessibilityTypes::EVENT_FOCUS, true); | 62 parent(), ui::AccessibilityTypes::EVENT_FOCUS, true); |
| 190 } | 63 } |
| 191 | 64 |
| 192 #if defined(TOUCH_UI) | |
| 193 bool NativeControlGtk::OnMousePressed(const MouseEvent& mouseev) { | |
| 194 FakeNativeMouseEvent(mouseev); | |
| 195 return true; | |
| 196 } | |
| 197 | |
| 198 void NativeControlGtk::OnMouseReleased(const MouseEvent& mouseev) { | |
| 199 FakeNativeMouseEvent(mouseev); | |
| 200 } | |
| 201 | |
| 202 void NativeControlGtk::OnMouseMoved(const MouseEvent& mouseev) { | |
| 203 FakeNativeMouseEvent(mouseev); | |
| 204 } | |
| 205 | |
| 206 void NativeControlGtk::OnMouseEntered(const MouseEvent& mouseev) { | |
| 207 FakeNativeMouseEvent(mouseev); | |
| 208 } | |
| 209 | |
| 210 void NativeControlGtk::OnMouseExited(const MouseEvent& mouseev) { | |
| 211 FakeNativeMouseEvent(mouseev); | |
| 212 } | |
| 213 #endif // defined(TOUCH_UI) | |
| 214 | |
| 215 void NativeControlGtk::NativeControlCreated(GtkWidget* native_control) { | 65 void NativeControlGtk::NativeControlCreated(GtkWidget* native_control) { |
| 216 Attach(native_control); | 66 Attach(native_control); |
| 217 | 67 |
| 218 // Update the newly created GtkWidget with any resident enabled state. | 68 // Update the newly created GtkWidget with any resident enabled state. |
| 219 gtk_widget_set_sensitive(native_view(), IsEnabled()); | 69 gtk_widget_set_sensitive(native_view(), IsEnabled()); |
| 220 | 70 |
| 221 // Listen for focus change event to update the FocusManager focused view. | 71 // Listen for focus change event to update the FocusManager focused view. |
| 222 g_signal_connect(native_control, "focus-in-event", | 72 g_signal_connect(native_control, "focus-in-event", |
| 223 G_CALLBACK(CallFocusIn), this); | 73 G_CALLBACK(CallFocusIn), this); |
| 224 } | 74 } |
| 225 | 75 |
| 226 // static | 76 // static |
| 227 gboolean NativeControlGtk::CallFocusIn(GtkWidget* gtk_widget, | 77 gboolean NativeControlGtk::CallFocusIn(GtkWidget* gtk_widget, |
| 228 GdkEventFocus* event, | 78 GdkEventFocus* event, |
| 229 NativeControlGtk* control) { | 79 NativeControlGtk* control) { |
| 230 Widget* widget = Widget::GetTopLevelWidgetForNativeView(gtk_widget); | 80 Widget* widget = Widget::GetTopLevelWidgetForNativeView(gtk_widget); |
| 231 FocusManager* focus_manager = widget ? widget->GetFocusManager() : NULL; | 81 FocusManager* focus_manager = widget ? widget->GetFocusManager() : NULL; |
| 232 if (!focus_manager) { | 82 if (!focus_manager) { |
| 233 // TODO(jcampan): http://crbug.com/21378 Reenable this NOTREACHED() when the | 83 // TODO(jcampan): http://crbug.com/21378 Reenable this NOTREACHED() when the |
| 234 // options page is only based on views. | 84 // options page is only based on views. |
| 235 // NOTREACHED(); | 85 // NOTREACHED(); |
| 236 NOTIMPLEMENTED(); | 86 NOTIMPLEMENTED(); |
| 237 return false; | 87 return false; |
| 238 } | 88 } |
| 239 focus_manager->SetFocusedView(control->focus_view()); | 89 focus_manager->SetFocusedView(control->focus_view()); |
| 240 return false; | 90 return false; |
| 241 } | 91 } |
| 242 | 92 |
| 243 } // namespace views | 93 } // namespace views |
| OLD | NEW |