widget/TouchEvents.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/TouchEvents.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,212 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef mozilla_TouchEvents_h__
    1.10 +#define mozilla_TouchEvents_h__
    1.11 +
    1.12 +#include <stdint.h>
    1.13 +
    1.14 +#include "mozilla/dom/Touch.h"
    1.15 +#include "mozilla/MouseEvents.h"
    1.16 +#include "nsAutoPtr.h"
    1.17 +#include "nsIDOMSimpleGestureEvent.h"
    1.18 +#include "nsTArray.h"
    1.19 +
    1.20 +namespace mozilla {
    1.21 +
    1.22 +/******************************************************************************
    1.23 + * mozilla::WidgetGestureNotifyEvent
    1.24 + *
    1.25 + * This event is the first event generated when the user touches
    1.26 + * the screen with a finger, and it's meant to decide what kind
    1.27 + * of action we'll use for that touch interaction.
    1.28 + *
    1.29 + * The event is dispatched to the layout and based on what is underneath
    1.30 + * the initial contact point it's then decided if we should pan
    1.31 + * (finger scrolling) or drag the target element.
    1.32 + ******************************************************************************/
    1.33 +
    1.34 +class WidgetGestureNotifyEvent : public WidgetGUIEvent
    1.35 +{
    1.36 +public:
    1.37 +  virtual WidgetGestureNotifyEvent* AsGestureNotifyEvent() MOZ_OVERRIDE
    1.38 +  {
    1.39 +    return this;
    1.40 +  }
    1.41 +
    1.42 +  WidgetGestureNotifyEvent(bool aIsTrusted, uint32_t aMessage,
    1.43 +                           nsIWidget *aWidget) :
    1.44 +    WidgetGUIEvent(aIsTrusted, aMessage, aWidget, NS_GESTURENOTIFY_EVENT),
    1.45 +    panDirection(ePanNone), displayPanFeedback(false)
    1.46 +  {
    1.47 +  }
    1.48 +
    1.49 +  virtual WidgetEvent* Duplicate() const MOZ_OVERRIDE
    1.50 +  {
    1.51 +    // XXX Looks like this event is handled only in PostHandleEvent() of
    1.52 +    //     EventStateManager.  Therefore, it might be possible to handle this
    1.53 +    //     in PreHandleEvent() and not to dispatch as a DOM event into the DOM
    1.54 +    //     tree like ContentQueryEvent.  Then, this event doesn't need to
    1.55 +    //     support Duplicate().
    1.56 +    MOZ_ASSERT(eventStructType == NS_GESTURENOTIFY_EVENT,
    1.57 +               "Duplicate() must be overridden by sub class");
    1.58 +    // Not copying widget, it is a weak reference.
    1.59 +    WidgetGestureNotifyEvent* result =
    1.60 +      new WidgetGestureNotifyEvent(false, message, nullptr);
    1.61 +    result->AssignGestureNotifyEventData(*this, true);
    1.62 +    result->mFlags = mFlags;
    1.63 +    return result;
    1.64 +  }
    1.65 +
    1.66 +  enum ePanDirection
    1.67 +  {
    1.68 +    ePanNone,
    1.69 +    ePanVertical,
    1.70 +    ePanHorizontal,
    1.71 +    ePanBoth
    1.72 +  };
    1.73 +
    1.74 +  ePanDirection panDirection;
    1.75 +  bool displayPanFeedback;
    1.76 +
    1.77 +  void AssignGestureNotifyEventData(const WidgetGestureNotifyEvent& aEvent,
    1.78 +                                    bool aCopyTargets)
    1.79 +  {
    1.80 +    AssignGUIEventData(aEvent, aCopyTargets);
    1.81 +
    1.82 +    panDirection = aEvent.panDirection;
    1.83 +    displayPanFeedback = aEvent.displayPanFeedback;
    1.84 +  }
    1.85 +};
    1.86 +
    1.87 +/******************************************************************************
    1.88 + * mozilla::WidgetTouchEvent
    1.89 + ******************************************************************************/
    1.90 +
    1.91 +class WidgetSimpleGestureEvent : public WidgetMouseEventBase
    1.92 +{
    1.93 +public:
    1.94 +  virtual WidgetSimpleGestureEvent* AsSimpleGestureEvent() MOZ_OVERRIDE
    1.95 +  {
    1.96 +    return this;
    1.97 +  }
    1.98 +
    1.99 +  WidgetSimpleGestureEvent(bool aIsTrusted, uint32_t aMessage,
   1.100 +                           nsIWidget* aWidget)
   1.101 +    : WidgetMouseEventBase(aIsTrusted, aMessage, aWidget,
   1.102 +                           NS_SIMPLE_GESTURE_EVENT)
   1.103 +    , allowedDirections(0)
   1.104 +    , direction(0)
   1.105 +    , delta(0.0)
   1.106 +    , clickCount(0)
   1.107 +  {
   1.108 +  }
   1.109 +
   1.110 +  WidgetSimpleGestureEvent(const WidgetSimpleGestureEvent& aOther)
   1.111 +    : WidgetMouseEventBase(aOther.mFlags.mIsTrusted, aOther.message,
   1.112 +                           aOther.widget, NS_SIMPLE_GESTURE_EVENT)
   1.113 +    , allowedDirections(aOther.allowedDirections)
   1.114 +    , direction(aOther.direction)
   1.115 +    , delta(aOther.delta)
   1.116 +    , clickCount(0)
   1.117 +  {
   1.118 +  }
   1.119 +
   1.120 +  virtual WidgetEvent* Duplicate() const MOZ_OVERRIDE
   1.121 +  {
   1.122 +    MOZ_ASSERT(eventStructType == NS_SIMPLE_GESTURE_EVENT,
   1.123 +               "Duplicate() must be overridden by sub class");
   1.124 +    // Not copying widget, it is a weak reference.
   1.125 +    WidgetSimpleGestureEvent* result =
   1.126 +      new WidgetSimpleGestureEvent(false, message, nullptr);
   1.127 +    result->AssignSimpleGestureEventData(*this, true);
   1.128 +    result->mFlags = mFlags;
   1.129 +    return result;
   1.130 +  }
   1.131 +
   1.132 +  // See nsIDOMSimpleGestureEvent for values
   1.133 +  uint32_t allowedDirections;
   1.134 +  // See nsIDOMSimpleGestureEvent for values
   1.135 +  uint32_t direction;
   1.136 +  // Delta for magnify and rotate events
   1.137 +  double delta;
   1.138 +  // The number of taps for tap events
   1.139 +  uint32_t clickCount;
   1.140 +
   1.141 +  // XXX Not tested by test_assign_event_data.html
   1.142 +  void AssignSimpleGestureEventData(const WidgetSimpleGestureEvent& aEvent,
   1.143 +                                    bool aCopyTargets)
   1.144 +  {
   1.145 +    AssignMouseEventBaseData(aEvent, aCopyTargets);
   1.146 +
   1.147 +    // allowedDirections isn't copied
   1.148 +    direction = aEvent.direction;
   1.149 +    delta = aEvent.delta;
   1.150 +    clickCount = aEvent.clickCount;
   1.151 +  }
   1.152 +};
   1.153 +
   1.154 +/******************************************************************************
   1.155 + * mozilla::WidgetTouchEvent
   1.156 + ******************************************************************************/
   1.157 +
   1.158 +class WidgetTouchEvent : public WidgetInputEvent
   1.159 +{
   1.160 +public:
   1.161 +  virtual WidgetTouchEvent* AsTouchEvent() MOZ_OVERRIDE { return this; }
   1.162 +
   1.163 +  WidgetTouchEvent()
   1.164 +  {
   1.165 +  }
   1.166 +
   1.167 +  WidgetTouchEvent(const WidgetTouchEvent& aOther) :
   1.168 +    WidgetInputEvent(aOther.mFlags.mIsTrusted, aOther.message, aOther.widget,
   1.169 +                     NS_TOUCH_EVENT)
   1.170 +  {
   1.171 +    modifiers = aOther.modifiers;
   1.172 +    time = aOther.time;
   1.173 +    touches.AppendElements(aOther.touches);
   1.174 +    mFlags.mCancelable = message != NS_TOUCH_CANCEL;
   1.175 +    MOZ_COUNT_CTOR(WidgetTouchEvent);
   1.176 +  }
   1.177 +
   1.178 +  WidgetTouchEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget) :
   1.179 +    WidgetInputEvent(aIsTrusted, aMessage, aWidget, NS_TOUCH_EVENT)
   1.180 +  {
   1.181 +    MOZ_COUNT_CTOR(WidgetTouchEvent);
   1.182 +    mFlags.mCancelable = message != NS_TOUCH_CANCEL;
   1.183 +  }
   1.184 +
   1.185 +  virtual ~WidgetTouchEvent()
   1.186 +  {
   1.187 +    MOZ_COUNT_DTOR(WidgetTouchEvent);
   1.188 +  }
   1.189 +
   1.190 +  virtual WidgetEvent* Duplicate() const MOZ_OVERRIDE
   1.191 +  {
   1.192 +    MOZ_ASSERT(eventStructType == NS_TOUCH_EVENT,
   1.193 +               "Duplicate() must be overridden by sub class");
   1.194 +    // Not copying widget, it is a weak reference.
   1.195 +    WidgetTouchEvent* result = new WidgetTouchEvent(false, message, nullptr);
   1.196 +    result->AssignTouchEventData(*this, true);
   1.197 +    result->mFlags = mFlags;
   1.198 +    return result;
   1.199 +  }
   1.200 +
   1.201 +  nsTArray<nsRefPtr<mozilla::dom::Touch>> touches;
   1.202 +
   1.203 +  void AssignTouchEventData(const WidgetTouchEvent& aEvent, bool aCopyTargets)
   1.204 +  {
   1.205 +    AssignInputEventData(aEvent, aCopyTargets);
   1.206 +
   1.207 +    // Assign*EventData() assume that they're called only new instance.
   1.208 +    MOZ_ASSERT(touches.IsEmpty());
   1.209 +    touches.AppendElements(aEvent.touches);
   1.210 +  }
   1.211 +};
   1.212 +
   1.213 +} // namespace mozilla
   1.214 +
   1.215 +#endif // mozilla_TouchEvents_h__

mercurial