Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / ui / events / event_utils.cc
blob1b3d7a9301db28bef028a3d0e0a00cbf6ba431f7
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
3 // found in the LICENSE file.
5 #include "ui/events/event_utils.h"
7 #include <vector>
9 #include "ui/events/event.h"
10 #include "ui/gfx/display.h"
11 #include "ui/gfx/screen.h"
13 namespace ui {
15 namespace {
16 int g_custom_event_types = ET_LAST;
17 } // namespace
19 scoped_ptr<Event> EventFromNative(const base::NativeEvent& native_event) {
20 scoped_ptr<Event> event;
21 EventType type = EventTypeFromNative(native_event);
22 switch(type) {
23 case ET_KEY_PRESSED:
24 case ET_KEY_RELEASED:
25 event.reset(new KeyEvent(native_event));
26 break;
28 case ET_MOUSE_PRESSED:
29 case ET_MOUSE_DRAGGED:
30 case ET_MOUSE_RELEASED:
31 case ET_MOUSE_MOVED:
32 case ET_MOUSE_ENTERED:
33 case ET_MOUSE_EXITED:
34 event.reset(new MouseEvent(native_event));
35 break;
37 case ET_MOUSEWHEEL:
38 event.reset(new MouseWheelEvent(native_event));
39 break;
41 case ET_SCROLL_FLING_START:
42 case ET_SCROLL_FLING_CANCEL:
43 case ET_SCROLL:
44 event.reset(new ScrollEvent(native_event));
45 break;
47 case ET_TOUCH_RELEASED:
48 case ET_TOUCH_PRESSED:
49 case ET_TOUCH_MOVED:
50 case ET_TOUCH_CANCELLED:
51 event.reset(new TouchEvent(native_event));
52 break;
54 default:
55 break;
57 return event.Pass();
60 int RegisterCustomEventType() {
61 return ++g_custom_event_types;
64 base::TimeDelta EventTimeForNow() {
65 return base::TimeDelta::FromInternalValue(
66 base::TimeTicks::Now().ToInternalValue());
69 bool ShouldDefaultToNaturalScroll() {
70 return GetInternalDisplayTouchSupport() ==
71 gfx::Display::TOUCH_SUPPORT_AVAILABLE;
74 gfx::Display::TouchSupport GetInternalDisplayTouchSupport() {
75 gfx::Screen* screen = gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE);
76 // No screen in some unit tests.
77 if (!screen)
78 return gfx::Display::TOUCH_SUPPORT_UNKNOWN;
79 const std::vector<gfx::Display>& displays = screen->GetAllDisplays();
80 for (std::vector<gfx::Display>::const_iterator it = displays.begin();
81 it != displays.end(); ++it) {
82 if (it->IsInternal())
83 return it->touch_support();
85 return gfx::Display::TOUCH_SUPPORT_UNAVAILABLE;
88 } // namespace ui