Roll src/third_party/WebKit 7f556d1:d75145d (svn 195932:195940)
[chromium-blink-merge.git] / ui / events / event_utils.cc
blob021ac278708168b8344b3f1ca5364c239ee0a27a
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_TRANSLATED_KEY_PRESS:
29 case ET_TRANSLATED_KEY_RELEASE:
30 // These should not be generated by native events.
31 NOTREACHED();
32 break;
34 case ET_MOUSE_PRESSED:
35 case ET_MOUSE_DRAGGED:
36 case ET_MOUSE_RELEASED:
37 case ET_MOUSE_MOVED:
38 case ET_MOUSE_ENTERED:
39 case ET_MOUSE_EXITED:
40 event.reset(new MouseEvent(native_event));
41 break;
43 case ET_MOUSEWHEEL:
44 event.reset(new MouseWheelEvent(native_event));
45 break;
47 case ET_SCROLL_FLING_START:
48 case ET_SCROLL_FLING_CANCEL:
49 case ET_SCROLL:
50 event.reset(new ScrollEvent(native_event));
51 break;
53 case ET_TOUCH_RELEASED:
54 case ET_TOUCH_PRESSED:
55 case ET_TOUCH_MOVED:
56 case ET_TOUCH_CANCELLED:
57 event.reset(new TouchEvent(native_event));
58 break;
60 default:
61 break;
63 return event.Pass();
66 int RegisterCustomEventType() {
67 return ++g_custom_event_types;
70 base::TimeDelta EventTimeForNow() {
71 return base::TimeDelta::FromInternalValue(
72 base::TimeTicks::Now().ToInternalValue());
75 bool ShouldDefaultToNaturalScroll() {
76 return GetInternalDisplayTouchSupport() ==
77 gfx::Display::TOUCH_SUPPORT_AVAILABLE;
80 gfx::Display::TouchSupport GetInternalDisplayTouchSupport() {
81 gfx::Screen* screen = gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE);
82 // No screen in some unit tests.
83 if (!screen)
84 return gfx::Display::TOUCH_SUPPORT_UNKNOWN;
85 const std::vector<gfx::Display>& displays = screen->GetAllDisplays();
86 for (std::vector<gfx::Display>::const_iterator it = displays.begin();
87 it != displays.end(); ++it) {
88 if (it->IsInternal())
89 return it->touch_support();
91 return gfx::Display::TOUCH_SUPPORT_UNAVAILABLE;
94 } // namespace ui