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 "ui/ui_controls/ui_controls.h" | 5 #include "ui/ui_controls/ui_controls.h" |
| 6 | 6 |
| 7 #include <gdk/gdkkeysyms.h> | 7 #include <gdk/gdkkeysyms.h> |
| 8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 event->type = GDK_MOTION_NOTIFY; | 87 event->type = GDK_MOTION_NOTIFY; |
| 88 | 88 |
| 89 gdk_event_put(event); | 89 gdk_event_put(event); |
| 90 gdk_event_free(event); | 90 gdk_event_free(event); |
| 91 } | 91 } |
| 92 | 92 |
| 93 } // namespace | 93 } // namespace |
| 94 | 94 |
| 95 namespace ui_controls { | 95 namespace ui_controls { |
| 96 | 96 |
| 97 bool SendKeyPress(gfx::NativeWindow window, | 97 namespace { |
| 98 ui::KeyboardCode key, | 98 |
| 99 bool control, | 99 class UIControlsGtk : public UIControls { |
| 100 bool shift, | 100 |
| 101 bool alt, | 101 bool SendKeyPress(gfx::NativeWindow window, |
| 102 bool command) { | 102 ui::KeyboardCode key, |
| 103 DCHECK(!command); // No command key on Linux | 103 bool control, |
| 104 GdkWindow* event_window = NULL; | 104 bool shift, |
| 105 GtkWidget* grab_widget = gtk_grab_get_current(); | 105 bool alt, |
| 106 if (grab_widget) { | 106 bool command) OVERRIDE { |
| 107 // If there is a grab, send all events to the grabbed widget. | 107 DCHECK(!command); // No command key on Linux |
| 108 event_window = gtk_widget_get_window(grab_widget); | 108 GdkWindow* event_window = NULL; |
| 109 } else if (window) { | 109 GtkWidget* grab_widget = gtk_grab_get_current(); |
| 110 event_window = gtk_widget_get_window(GTK_WIDGET(window)); | 110 if (grab_widget) { |
| 111 } else { | 111 // If there is a grab, send all events to the grabbed widget. |
| 112 // No target was specified. Send the events to the active toplevel. | 112 event_window = gtk_widget_get_window(grab_widget); |
| 113 GList* windows = gtk_window_list_toplevels(); | 113 } else if (window) { |
| 114 for (GList* element = windows; element; element = g_list_next(element)) { | 114 event_window = gtk_widget_get_window(GTK_WIDGET(window)); |
| 115 GtkWindow* this_window = GTK_WINDOW(element->data); | 115 } else { |
| 116 if (gtk_window_is_active(this_window)) { | 116 // No target was specified. Send the events to the active toplevel. |
| 117 event_window = gtk_widget_get_window(GTK_WIDGET(this_window)); | 117 GList* windows = gtk_window_list_toplevels(); |
| 118 break; | 118 for (GList* element = windows; element; element = g_list_next(element)) { |
| 119 GtkWindow* this_window = GTK_WINDOW(element->data); | |
| 120 if (gtk_window_is_active(this_window)) { | |
| 121 event_window = gtk_widget_get_window(GTK_WIDGET(this_window)); | |
| 122 break; | |
| 123 } | |
| 119 } | 124 } |
| 125 g_list_free(windows); | |
| 120 } | 126 } |
| 121 g_list_free(windows); | 127 if (!event_window) { |
| 128 NOTREACHED() << "Window not specified and none is active"; | |
| 129 return false; | |
| 130 } | |
| 131 | |
| 132 std::vector<GdkEvent*> events; | |
| 133 ui::SynthesizeKeyPressEvents( | |
| 134 event_window, key, control, shift, alt, &events); | |
| 135 for (std::vector<GdkEvent*>::iterator iter = events.begin(); | |
| 136 iter != events.end(); ++iter) { | |
| 137 gdk_event_put(*iter); | |
| 138 // gdk_event_put appends a copy of the event. | |
| 139 gdk_event_free(*iter); | |
| 140 } | |
| 141 | |
| 142 return true; | |
| 122 } | 143 } |
| 123 if (!event_window) { | 144 |
| 124 NOTREACHED() << "Window not specified and none is active"; | 145 bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window, |
| 146 ui::KeyboardCode key, | |
| 147 bool control, | |
| 148 bool shift, | |
| 149 bool alt, | |
| 150 bool command, | |
| 151 const base::Closure& task) OVERRIDE { | |
| 152 DCHECK(!command); // No command key on Linux | |
| 153 int release_count = 1; | |
| 154 if (control) | |
| 155 release_count++; | |
| 156 if (shift) | |
| 157 release_count++; | |
| 158 if (alt) | |
| 159 release_count++; | |
| 160 // This object will delete itself after running |task|. | |
| 161 new EventWaiter(task, GDK_KEY_RELEASE, release_count); | |
| 162 return SendKeyPress(window, key, control, shift, alt, command); | |
| 163 } | |
| 164 | |
| 165 bool SendMouseMove(long x, long y) OVERRIDE { | |
| 166 gdk_display_warp_pointer( | |
| 167 gdk_display_get_default(), gdk_screen_get_default(), x, y); | |
| 168 // Sometimes gdk_display_warp_pointer fails to send back any indication of | |
| 169 // the move, even though it succesfully moves the server cursor. We fake | |
| 170 // it in order to get drags to work. | |
| 171 FakeAMouseMotionEvent(x, y); | |
| 172 return true; | |
| 173 } | |
| 174 | |
| 175 bool SendMouseMoveNotifyWhenDone( | |
| 176 long x, long y, const base::Closure& task) OVERRIDE { | |
| 177 bool rv = SendMouseMove(x, y); | |
| 178 new EventWaiter(task, GDK_MOTION_NOTIFY, 1); | |
| 179 return rv; | |
| 180 } | |
| 181 | |
| 182 bool SendMouseEvents(MouseButton type, int state) OVERRIDE { | |
| 183 GdkEvent* event = gdk_event_new(GDK_BUTTON_PRESS); | |
| 184 | |
| 185 event->button.send_event = false; | |
| 186 event->button.time = XTimeNow(); | |
| 187 | |
| 188 gint x, y; | |
| 189 GtkWidget* grab_widget = gtk_grab_get_current(); | |
| 190 if (grab_widget) { | |
| 191 // If there is a grab, we need to target all events at it regardless of | |
| 192 // what widget the mouse is over. | |
| 193 event->button.window = gtk_widget_get_window(grab_widget); | |
| 194 gdk_window_get_pointer(event->button.window, &x, &y, NULL); | |
| 195 } else { | |
| 196 event->button.window = gdk_window_at_pointer(&x, &y); | |
| 197 CHECK(event->button.window); | |
| 198 } | |
| 199 | |
| 200 g_object_ref(event->button.window); | |
| 201 event->button.x = x; | |
| 202 event->button.y = y; | |
| 203 gint origin_x, origin_y; | |
| 204 gdk_window_get_origin(event->button.window, &origin_x, &origin_y); | |
| 205 event->button.x_root = x + origin_x; | |
| 206 event->button.y_root = y + origin_y; | |
| 207 | |
| 208 event->button.axes = NULL; | |
| 209 GdkModifierType modifier; | |
| 210 gdk_window_get_pointer(event->button.window, NULL, NULL, &modifier); | |
| 211 event->button.state = modifier; | |
| 212 event->button.button = type == LEFT ? 1 : (type == MIDDLE ? 2 : 3); | |
| 213 event->button.device = gdk_device_get_core_pointer(); | |
| 214 | |
| 215 event->button.type = GDK_BUTTON_PRESS; | |
| 216 if (state & DOWN) | |
| 217 gdk_event_put(event); | |
| 218 | |
| 219 // Also send a release event. | |
| 220 GdkEvent* release_event = gdk_event_copy(event); | |
| 221 release_event->button.type = GDK_BUTTON_RELEASE; | |
| 222 release_event->button.time++; | |
| 223 if (state & UP) | |
| 224 gdk_event_put(release_event); | |
| 225 | |
| 226 gdk_event_free(event); | |
| 227 gdk_event_free(release_event); | |
| 228 | |
| 125 return false; | 229 return false; |
| 126 } | 230 } |
| 127 | 231 |
| 128 std::vector<GdkEvent*> events; | 232 bool SendMouseEventsNotifyWhenDone(MouseButton type, |
| 129 ui::SynthesizeKeyPressEvents(event_window, key, control, shift, alt, &events); | 233 int state, |
| 130 for (std::vector<GdkEvent*>::iterator iter = events.begin(); | 234 const base::Closure& task) OVERRIDE { |
| 131 iter != events.end(); ++iter) { | 235 bool rv = SendMouseEvents(type, state); |
| 132 gdk_event_put(*iter); | 236 GdkEventType wait_type; |
| 133 // gdk_event_put appends a copy of the event. | 237 if (state & UP) { |
| 134 gdk_event_free(*iter); | 238 wait_type = GDK_BUTTON_RELEASE; |
| 239 } else { | |
| 240 if (type == LEFT) | |
| 241 wait_type = GDK_BUTTON_PRESS; | |
| 242 else if (type == MIDDLE) | |
| 243 wait_type = GDK_2BUTTON_PRESS; | |
| 244 else | |
| 245 wait_type = GDK_3BUTTON_PRESS; | |
| 246 } | |
| 247 new EventWaiter(task, wait_type, 1); | |
| 248 return rv; | |
| 135 } | 249 } |
| 136 | 250 |
| 137 return true; | 251 bool SendMouseClick(MouseButton type) { |
| 138 } | 252 return SendMouseEvents(type, UP | DOWN); |
| 253 } | |
|
oshima
2012/11/15 23:37:41
DISALLOW_COPY_AND_ASSIGN
scottmg
2012/11/16 17:58:54
Done.
| |
| 254 }; | |
| 139 | 255 |
| 140 bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window, | 256 } // namespace |
| 141 ui::KeyboardCode key, | |
| 142 bool control, | |
| 143 bool shift, | |
| 144 bool alt, | |
| 145 bool command, | |
| 146 const base::Closure& task) { | |
| 147 DCHECK(!command); // No command key on Linux | |
| 148 int release_count = 1; | |
| 149 if (control) | |
| 150 release_count++; | |
| 151 if (shift) | |
| 152 release_count++; | |
| 153 if (alt) | |
| 154 release_count++; | |
| 155 // This object will delete itself after running |task|. | |
| 156 new EventWaiter(task, GDK_KEY_RELEASE, release_count); | |
| 157 return SendKeyPress(window, key, control, shift, alt, command); | |
| 158 } | |
| 159 | 257 |
| 160 bool SendMouseMove(long x, long y) { | 258 UIControls* CreateNativeUIControls() { |
| 161 gdk_display_warp_pointer(gdk_display_get_default(), gdk_screen_get_default(), | 259 return new UIControlsGtk; |
| 162 x, y); | |
| 163 // Sometimes gdk_display_warp_pointer fails to send back any indication of | |
| 164 // the move, even though it succesfully moves the server cursor. We fake it in | |
| 165 // order to get drags to work. | |
| 166 FakeAMouseMotionEvent(x, y); | |
| 167 return true; | |
| 168 } | |
| 169 | |
| 170 bool SendMouseMoveNotifyWhenDone(long x, long y, const base::Closure& task) { | |
| 171 bool rv = SendMouseMove(x, y); | |
| 172 new EventWaiter(task, GDK_MOTION_NOTIFY, 1); | |
| 173 return rv; | |
| 174 } | |
| 175 | |
| 176 bool SendMouseEvents(MouseButton type, int state) { | |
| 177 GdkEvent* event = gdk_event_new(GDK_BUTTON_PRESS); | |
| 178 | |
| 179 event->button.send_event = false; | |
| 180 event->button.time = XTimeNow(); | |
| 181 | |
| 182 gint x, y; | |
| 183 GtkWidget* grab_widget = gtk_grab_get_current(); | |
| 184 if (grab_widget) { | |
| 185 // If there is a grab, we need to target all events at it regardless of | |
| 186 // what widget the mouse is over. | |
| 187 event->button.window = gtk_widget_get_window(grab_widget); | |
| 188 gdk_window_get_pointer(event->button.window, &x, &y, NULL); | |
| 189 } else { | |
| 190 event->button.window = gdk_window_at_pointer(&x, &y); | |
| 191 CHECK(event->button.window); | |
| 192 } | |
| 193 | |
| 194 g_object_ref(event->button.window); | |
| 195 event->button.x = x; | |
| 196 event->button.y = y; | |
| 197 gint origin_x, origin_y; | |
| 198 gdk_window_get_origin(event->button.window, &origin_x, &origin_y); | |
| 199 event->button.x_root = x + origin_x; | |
| 200 event->button.y_root = y + origin_y; | |
| 201 | |
| 202 event->button.axes = NULL; | |
| 203 GdkModifierType modifier; | |
| 204 gdk_window_get_pointer(event->button.window, NULL, NULL, &modifier); | |
| 205 event->button.state = modifier; | |
| 206 event->button.button = type == LEFT ? 1 : (type == MIDDLE ? 2 : 3); | |
| 207 event->button.device = gdk_device_get_core_pointer(); | |
| 208 | |
| 209 event->button.type = GDK_BUTTON_PRESS; | |
| 210 if (state & DOWN) | |
| 211 gdk_event_put(event); | |
| 212 | |
| 213 // Also send a release event. | |
| 214 GdkEvent* release_event = gdk_event_copy(event); | |
| 215 release_event->button.type = GDK_BUTTON_RELEASE; | |
| 216 release_event->button.time++; | |
| 217 if (state & UP) | |
| 218 gdk_event_put(release_event); | |
| 219 | |
| 220 gdk_event_free(event); | |
| 221 gdk_event_free(release_event); | |
| 222 | |
| 223 return false; | |
| 224 } | |
| 225 | |
| 226 bool SendMouseEventsNotifyWhenDone(MouseButton type, | |
| 227 int state, | |
| 228 const base::Closure& task) { | |
| 229 bool rv = SendMouseEvents(type, state); | |
| 230 GdkEventType wait_type; | |
| 231 if (state & UP) { | |
| 232 wait_type = GDK_BUTTON_RELEASE; | |
| 233 } else { | |
| 234 if (type == LEFT) | |
| 235 wait_type = GDK_BUTTON_PRESS; | |
| 236 else if (type == MIDDLE) | |
| 237 wait_type = GDK_2BUTTON_PRESS; | |
| 238 else | |
| 239 wait_type = GDK_3BUTTON_PRESS; | |
| 240 } | |
| 241 new EventWaiter(task, wait_type, 1); | |
| 242 return rv; | |
| 243 } | |
| 244 | |
| 245 bool SendMouseClick(MouseButton type) { | |
| 246 return SendMouseEvents(type, UP | DOWN); | |
| 247 } | 260 } |
| 248 | 261 |
| 249 } // namespace ui_controls | 262 } // namespace ui_controls |
| OLD | NEW |