Home | History | Annotate | Download | only in gestures
      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.
      4 
      5 #ifndef UI_EVENTS_GESTURES_GESTURE_RECOGNIZER_IMPL_H_
      6 #define UI_EVENTS_GESTURES_GESTURE_RECOGNIZER_IMPL_H_
      7 
      8 #include <map>
      9 #include <vector>
     10 
     11 #include "base/memory/linked_ptr.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "ui/events/event_constants.h"
     14 #include "ui/events/events_export.h"
     15 #include "ui/events/gestures/gesture_recognizer.h"
     16 #include "ui/events/gestures/gesture_sequence.h"
     17 #include "ui/gfx/point.h"
     18 
     19 namespace ui {
     20 class GestureConsumer;
     21 class GestureEvent;
     22 class GestureEventHelper;
     23 class GestureSequence;
     24 class TouchEvent;
     25 
     26 class EVENTS_EXPORT GestureRecognizerImpl : public GestureRecognizer,
     27                                             public GestureSequenceDelegate {
     28  public:
     29   typedef std::map<int, GestureConsumer*> TouchIdToConsumerMap;
     30 
     31   GestureRecognizerImpl();
     32   virtual ~GestureRecognizerImpl();
     33 
     34   std::vector<GestureEventHelper*>& helpers() { return helpers_; }
     35 
     36   // Overridden from GestureRecognizer
     37   virtual GestureConsumer* GetTouchLockedTarget(
     38       const TouchEvent& event) OVERRIDE;
     39   virtual GestureConsumer* GetTargetForGestureEvent(
     40       const GestureEvent& event) OVERRIDE;
     41   virtual GestureConsumer* GetTargetForLocation(
     42       const gfx::Point& location) OVERRIDE;
     43   virtual void TransferEventsTo(GestureConsumer* current_consumer,
     44                                 GestureConsumer* new_consumer) OVERRIDE;
     45   virtual bool GetLastTouchPointForTarget(GestureConsumer* consumer,
     46                                           gfx::Point* point) OVERRIDE;
     47   virtual void CancelActiveTouches(GestureConsumer* consumer) OVERRIDE;
     48 
     49  protected:
     50   virtual GestureSequence* CreateSequence(GestureSequenceDelegate* delegate);
     51   virtual GestureSequence* GetGestureSequenceForConsumer(GestureConsumer* c);
     52 
     53  private:
     54   // Sets up the target consumer for gestures based on the touch-event.
     55   void SetupTargets(const TouchEvent& event, GestureConsumer* consumer);
     56   void CancelTouches(std::vector<std::pair<int, GestureConsumer*> >* touches);
     57 
     58   // Overridden from GestureRecognizer
     59   virtual Gestures* ProcessTouchEventForGesture(
     60       const TouchEvent& event,
     61       ui::EventResult result,
     62       GestureConsumer* target) OVERRIDE;
     63   virtual void CleanupStateForConsumer(GestureConsumer* consumer) OVERRIDE;
     64   virtual void AddGestureEventHelper(GestureEventHelper* helper) OVERRIDE;
     65   virtual void RemoveGestureEventHelper(GestureEventHelper* helper) OVERRIDE;
     66 
     67   // Overridden from ui::GestureSequenceDelegate.
     68   virtual void DispatchPostponedGestureEvent(GestureEvent* event) OVERRIDE;
     69 
     70   // Convenience method to find the GestureEventHelper that can dispatch events
     71   // to a specific |consumer|.
     72   GestureEventHelper* FindDispatchHelperForConsumer(GestureConsumer* consumer);
     73 
     74   std::map<GestureConsumer*, GestureSequence*> consumer_sequence_;
     75 
     76   // Both |touch_id_target_| and |touch_id_target_for_gestures_| map a touch-id
     77   // to its target window.  touch-ids are removed from |touch_id_target_| on
     78   // ET_TOUCH_RELEASE and ET_TOUCH_CANCEL. |touch_id_target_for_gestures_| are
     79   // removed in ConsumerDestroyed().
     80   TouchIdToConsumerMap touch_id_target_;
     81   TouchIdToConsumerMap touch_id_target_for_gestures_;
     82 
     83   std::vector<GestureEventHelper*> helpers_;
     84 
     85   DISALLOW_COPY_AND_ASSIGN(GestureRecognizerImpl);
     86 };
     87 
     88 // Provided only for testing:
     89 EVENTS_EXPORT void SetGestureRecognizerForTesting(
     90     GestureRecognizer* gesture_recognizer);
     91 
     92 }  // namespace ui
     93 
     94 #endif  // UI_EVENTS_GESTURES_GESTURE_RECOGNIZER_IMPL_H_
     95