| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "views/events/event.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "ui/base/keycodes/keyboard_code_conversion.h" | |
| 9 #include "views/view.h" | |
| 10 #include "views/widget/root_view.h" | |
| 11 | |
| 12 namespace views { | |
| 13 | |
| 14 //////////////////////////////////////////////////////////////////////////////// | |
| 15 // Event, protected: | |
| 16 | |
| 17 Event::Event(ui::EventType type, int flags) | |
| 18 : type_(type), | |
| 19 time_stamp_(base::Time::NowFromSystemTime()), | |
| 20 flags_(flags) { | |
| 21 // Safely initialize the pointer/struct to null/empty. | |
| 22 memset(&native_event_, 0, sizeof(native_event_)); | |
| 23 #if defined(TOOLKIT_USES_GTK) | |
| 24 gdk_event_ = NULL; | |
| 25 #endif | |
| 26 } | |
| 27 | |
| 28 Event::Event(const NativeEvent& native_event, ui::EventType type, int flags) | |
| 29 : native_event_(native_event), | |
| 30 type_(type), | |
| 31 time_stamp_(base::Time::NowFromSystemTime()), | |
| 32 flags_(flags) { | |
| 33 #if defined(TOOLKIT_USES_GTK) | |
| 34 gdk_event_ = NULL; | |
| 35 #endif | |
| 36 } | |
| 37 | |
| 38 //////////////////////////////////////////////////////////////////////////////// | |
| 39 // LocatedEvent, protected: | |
| 40 | |
| 41 #if !defined(USE_AURA) | |
| 42 LocatedEvent::LocatedEvent(const NativeEvent& native_event) | |
| 43 : Event(native_event, | |
| 44 ui::EventTypeFromNative(native_event), | |
| 45 ui::EventFlagsFromNative(native_event)), | |
| 46 location_(ui::EventLocationFromNative(native_event)) { | |
| 47 } | |
| 48 #endif | |
| 49 | |
| 50 // TODO(msw): Kill this legacy constructor when we update uses. | |
| 51 LocatedEvent::LocatedEvent(ui::EventType type, | |
| 52 const gfx::Point& location, | |
| 53 int flags) | |
| 54 : Event(type, flags), | |
| 55 location_(location) { | |
| 56 } | |
| 57 | |
| 58 LocatedEvent::LocatedEvent(const LocatedEvent& model, | |
| 59 View* source, | |
| 60 View* target) | |
| 61 : Event(model), | |
| 62 location_(model.location_) { | |
| 63 if (target && target != source) | |
| 64 View::ConvertPointToView(source, target, &location_); | |
| 65 } | |
| 66 | |
| 67 LocatedEvent::LocatedEvent(const LocatedEvent& model, View* root) | |
| 68 : Event(model), | |
| 69 location_(model.location_) { | |
| 70 View::ConvertPointFromWidget(root, &location_); | |
| 71 } | |
| 72 | |
| 73 //////////////////////////////////////////////////////////////////////////////// | |
| 74 // KeyEvent, public: | |
| 75 | |
| 76 #if !defined(USE_AURA) | |
| 77 KeyEvent::KeyEvent(const NativeEvent& native_event) | |
| 78 : Event(native_event, | |
| 79 ui::EventTypeFromNative(native_event), | |
| 80 ui::EventFlagsFromNative(native_event)), | |
| 81 key_code_(ui::KeyboardCodeFromNative(native_event)), | |
| 82 character_(0), | |
| 83 unmodified_character_(0) { | |
| 84 } | |
| 85 #endif | |
| 86 | |
| 87 KeyEvent::KeyEvent(ui::EventType type, | |
| 88 ui::KeyboardCode key_code, | |
| 89 int event_flags) | |
| 90 : Event(type, event_flags), | |
| 91 key_code_(key_code), | |
| 92 character_(ui::GetCharacterFromKeyCode(key_code, event_flags)), | |
| 93 unmodified_character_(0) { | |
| 94 } | |
| 95 | |
| 96 //////////////////////////////////////////////////////////////////////////////// | |
| 97 // MouseEvent, public: | |
| 98 | |
| 99 MouseEvent::MouseEvent(const NativeEvent& native_event) | |
| 100 : LocatedEvent(native_event) { | |
| 101 } | |
| 102 | |
| 103 MouseEvent::MouseEvent(const MouseEvent& model, View* source, View* target) | |
| 104 : LocatedEvent(model, source, target) { | |
| 105 } | |
| 106 | |
| 107 MouseEvent::MouseEvent(const TouchEvent& touch) | |
| 108 : LocatedEvent(touch.native_event()) { | |
| 109 // The location of the event is correctly extracted from the native event. But | |
| 110 // it is necessary to update the event type. | |
| 111 ui::EventType mtype = ui::ET_UNKNOWN; | |
| 112 switch (touch.type()) { | |
| 113 case ui::ET_TOUCH_RELEASED: | |
| 114 mtype = ui::ET_MOUSE_RELEASED; | |
| 115 break; | |
| 116 case ui::ET_TOUCH_PRESSED: | |
| 117 mtype = ui::ET_MOUSE_PRESSED; | |
| 118 break; | |
| 119 case ui::ET_TOUCH_MOVED: | |
| 120 mtype = ui::ET_MOUSE_MOVED; | |
| 121 break; | |
| 122 default: | |
| 123 NOTREACHED() << "Invalid mouse event."; | |
| 124 } | |
| 125 set_type(mtype); | |
| 126 | |
| 127 // It may not be possible to extract the button-information necessary for a | |
| 128 // MouseEvent from the native event for a TouchEvent, so the flags are | |
| 129 // explicitly updated as well. The button is approximated from the touchpoint | |
| 130 // identity. | |
| 131 int new_flags = flags() & ~(ui::EF_LEFT_BUTTON_DOWN | | |
| 132 ui::EF_RIGHT_BUTTON_DOWN | | |
| 133 ui::EF_MIDDLE_BUTTON_DOWN); | |
| 134 int button = ui::EF_LEFT_BUTTON_DOWN; | |
| 135 if (touch.identity() == 1) | |
| 136 button = ui::EF_RIGHT_BUTTON_DOWN; | |
| 137 else if (touch.identity() == 2) | |
| 138 button = ui::EF_MIDDLE_BUTTON_DOWN; | |
| 139 set_flags(new_flags | button); | |
| 140 } | |
| 141 | |
| 142 //////////////////////////////////////////////////////////////////////////////// | |
| 143 // MouseWheelEvent, public: | |
| 144 | |
| 145 #if !defined(USE_AURA) | |
| 146 MouseWheelEvent::MouseWheelEvent(const NativeEvent& native_event) | |
| 147 : MouseEvent(native_event), | |
| 148 offset_(ui::GetMouseWheelOffset(native_event)) { | |
| 149 } | |
| 150 #endif | |
| 151 | |
| 152 //////////////////////////////////////////////////////////////////////////////// | |
| 153 // TouchEvent, public: | |
| 154 | |
| 155 TouchEvent::TouchEvent(ui::EventType type, | |
| 156 int x, | |
| 157 int y, | |
| 158 int flags, | |
| 159 int touch_id, | |
| 160 float radius_x, | |
| 161 float radius_y, | |
| 162 float angle, | |
| 163 float force) | |
| 164 : LocatedEvent(type, gfx::Point(x, y), flags), | |
| 165 touch_id_(touch_id), | |
| 166 radius_x_(radius_x), | |
| 167 radius_y_(radius_y), | |
| 168 rotation_angle_(angle), | |
| 169 force_(force) { | |
| 170 } | |
| 171 | |
| 172 TouchEvent::TouchEvent(const TouchEvent& model, View* source, View* target) | |
| 173 : LocatedEvent(model, source, target), | |
| 174 touch_id_(model.touch_id_), | |
| 175 radius_x_(model.radius_x_), | |
| 176 radius_y_(model.radius_y_), | |
| 177 rotation_angle_(model.rotation_angle_), | |
| 178 force_(model.force_) { | |
| 179 } | |
| 180 | |
| 181 //////////////////////////////////////////////////////////////////////////////// | |
| 182 // TouchEvent, private: | |
| 183 | |
| 184 TouchEvent::TouchEvent(const TouchEvent& model, View* root) | |
| 185 : LocatedEvent(model, root), | |
| 186 touch_id_(model.touch_id_), | |
| 187 radius_x_(model.radius_x_), | |
| 188 radius_y_(model.radius_y_), | |
| 189 rotation_angle_(model.rotation_angle_), | |
| 190 force_(model.force_) { | |
| 191 } | |
| 192 | |
| 193 //////////////////////////////////////////////////////////////////////////////// | |
| 194 // MouseWheelEvent, public: | |
| 195 | |
| 196 // This value matches windows WHEEL_DELTA. | |
| 197 // static | |
| 198 const int MouseWheelEvent::kWheelDelta = 120; | |
| 199 | |
| 200 } // namespace views | |
| OLD | NEW |