WebKit merge 94748:94770
[chromium-blink-merge.git] / views / events / event_gtk.cc
blob13c8a8d67c2f0b1669102d3402756159a0109422
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
3 // found in the LICENSE file.
5 #include "views/events/event.h"
7 #include <gdk/gdk.h>
9 #include "base/logging.h"
10 #include "ui/base/keycodes/keyboard_code_conversion_gtk.h"
12 namespace views {
14 namespace {
16 ui::EventType EventTypeFromNative(NativeEvent native_event) {
17 // Add new event types as necessary.
18 switch (native_event->type) {
19 case GDK_2BUTTON_PRESS:
20 case GDK_3BUTTON_PRESS:
21 case GDK_BUTTON_PRESS:
22 return ui::ET_MOUSE_PRESSED;
23 case GDK_BUTTON_RELEASE:
24 return ui::ET_MOUSE_RELEASED;
25 case GDK_DRAG_MOTION:
26 return ui::ET_MOUSE_DRAGGED;
27 case GDK_ENTER_NOTIFY:
28 return ui::ET_MOUSE_ENTERED;
29 case GDK_KEY_PRESS:
30 return ui::ET_KEY_PRESSED;
31 case GDK_KEY_RELEASE:
32 return ui::ET_KEY_RELEASED;
33 case GDK_LEAVE_NOTIFY:
34 return ui::ET_MOUSE_EXITED;
35 case GDK_MOTION_NOTIFY:
36 if (native_event->motion.state & GDK_BUTTON1_MASK ||
37 native_event->motion.state & GDK_BUTTON2_MASK ||
38 native_event->motion.state & GDK_BUTTON3_MASK ||
39 native_event->motion.state & GDK_BUTTON4_MASK ||
40 native_event->motion.state & GDK_BUTTON5_MASK) {
41 return ui::ET_MOUSE_DRAGGED;
43 return ui::ET_MOUSE_MOVED;
44 case GDK_SCROLL:
45 return ui::ET_MOUSEWHEEL;
46 default:
47 NOTREACHED();
48 break;
50 return ui::ET_UNKNOWN;
53 GdkEventKey* GetGdkEventKeyFromNative(NativeEvent native_event) {
54 DCHECK(native_event->type == GDK_KEY_PRESS ||
55 native_event->type == GDK_KEY_RELEASE);
56 return &native_event->key;
59 gfx::Point GetMouseEventLocation(NativeEvent native_event) {
60 double x = 0, y = 0;
61 if (gdk_event_get_coords(native_event, &x, &y))
62 return gfx::Point(static_cast<int>(x), static_cast<int>(y));
63 return gfx::Point();
66 int GetMouseWheelOffset(NativeEvent native_event) {
67 DCHECK(native_event->type == GDK_SCROLL);
68 int offset = (native_event->scroll.direction == GDK_SCROLL_UP ||
69 native_event->scroll.direction == GDK_SCROLL_LEFT) ? 1 : -1;
70 return MouseWheelEvent::kWheelDelta * offset;
73 unsigned int GetGdkStateFromNative(NativeEvent native_event) {
74 switch (native_event->type) {
75 case GDK_KEY_PRESS:
76 case GDK_KEY_RELEASE:
77 return native_event->key.state;
78 case GDK_BUTTON_PRESS:
79 case GDK_2BUTTON_PRESS:
80 case GDK_3BUTTON_PRESS:
81 case GDK_BUTTON_RELEASE:
82 return native_event->button.state;
83 case GDK_SCROLL:
84 return native_event->scroll.state;
85 case GDK_MOTION_NOTIFY:
86 return native_event->motion.state;
87 case GDK_ENTER_NOTIFY:
88 case GDK_LEAVE_NOTIFY:
89 return native_event->crossing.state;
90 default:
91 NOTREACHED();
92 break;
94 return 0;
97 int GetFlagsFromGdkState(unsigned int state) {
98 int flags = 0;
99 flags |= (state & GDK_LOCK_MASK) ? ui::EF_CAPS_LOCK_DOWN : 0;
100 flags |= (state & GDK_CONTROL_MASK) ? ui::EF_CONTROL_DOWN : 0;
101 flags |= (state & GDK_SHIFT_MASK) ? ui::EF_SHIFT_DOWN : 0;
102 flags |= (state & GDK_MOD1_MASK) ? ui::EF_ALT_DOWN : 0;
103 flags |= (state & GDK_BUTTON1_MASK) ? ui::EF_LEFT_BUTTON_DOWN : 0;
104 flags |= (state & GDK_BUTTON2_MASK) ? ui::EF_MIDDLE_BUTTON_DOWN : 0;
105 flags |= (state & GDK_BUTTON3_MASK) ? ui::EF_RIGHT_BUTTON_DOWN : 0;
106 return flags;
109 } // namespace
111 ////////////////////////////////////////////////////////////////////////////////
112 // Event, public:
114 // static
115 int Event::GetFlagsFromGdkEvent(NativeEvent native_event) {
116 int flags = GetFlagsFromGdkState(GetGdkStateFromNative(native_event));
117 if (native_event->type == GDK_2BUTTON_PRESS)
118 flags |= ui::EF_IS_DOUBLE_CLICK;
119 if (native_event->type == GDK_BUTTON_PRESS ||
120 native_event->type == GDK_2BUTTON_PRESS ||
121 native_event->type == GDK_3BUTTON_PRESS ||
122 native_event->type == GDK_BUTTON_RELEASE) {
123 switch (native_event->button.button) {
124 case 1:
125 return flags | ui::EF_LEFT_BUTTON_DOWN;
126 case 2:
127 return flags | ui::EF_MIDDLE_BUTTON_DOWN;
128 case 3:
129 return flags | ui::EF_RIGHT_BUTTON_DOWN;
132 return flags;
135 ////////////////////////////////////////////////////////////////////////////////
136 // Event, private:
138 void Event::Init() {
139 native_event_ = NULL;
140 native_event_2_ = NULL;
143 void Event::InitWithNativeEvent(NativeEvent native_event) {
144 native_event_ = native_event;
145 // TODO(beng): remove once we rid views of Gtk/Gdk.
146 native_event_2_ = NULL;
149 ////////////////////////////////////////////////////////////////////////////////
150 // LocatedEvent, protected:
152 LocatedEvent::LocatedEvent(NativeEvent native_event)
153 : Event(native_event, EventTypeFromNative(native_event),
154 GetFlagsFromGdkEvent(native_event)),
155 location_(GetMouseEventLocation(native_event)) {
158 ////////////////////////////////////////////////////////////////////////////////
159 // MouseEvent, public:
161 MouseEvent::MouseEvent(NativeEvent native_event)
162 : LocatedEvent(native_event) {
165 ////////////////////////////////////////////////////////////////////////////////
166 // KeyEvent, public:
168 KeyEvent::KeyEvent(NativeEvent native_event)
169 : Event(native_event, EventTypeFromNative(native_event),
170 GetFlagsFromGdkEvent(native_event)),
171 key_code_(ui::KeyboardCodeFromGdkEventKey(
172 GetGdkEventKeyFromNative(native_event))),
173 character_(0),
174 unmodified_character_(0) {
177 ////////////////////////////////////////////////////////////////////////////////
178 // MouseWheelEvent, public:
180 MouseWheelEvent::MouseWheelEvent(NativeEvent native_event)
181 : MouseEvent(native_event),
182 offset_(GetMouseWheelOffset(native_event)) {
185 } // namespace views