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