Home | History | Annotate | Download | only in events
      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_EVENT_TARGET_H_
      6 #define UI_EVENTS_EVENT_TARGET_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "ui/events/event_handler.h"
     14 #include "ui/events/events_export.h"
     15 
     16 namespace ui {
     17 
     18 class EventDispatcher;
     19 class EventTargeter;
     20 class EventTargetIterator;
     21 class LocatedEvent;
     22 
     23 class EVENTS_EXPORT EventTarget : public EventHandler {
     24  public:
     25   class DispatcherApi {
     26    public:
     27     explicit DispatcherApi(EventTarget* target) : target_(target) {}
     28 
     29     const EventHandlerList& pre_target_list() const {
     30       return target_->pre_target_list_;
     31     }
     32 
     33    private:
     34     DispatcherApi();
     35     EventTarget* target_;
     36 
     37     DISALLOW_COPY_AND_ASSIGN(DispatcherApi);
     38   };
     39 
     40   EventTarget();
     41   virtual ~EventTarget();
     42 
     43   virtual bool CanAcceptEvent(const Event& event) = 0;
     44 
     45   // Returns the parent EventTarget in the event-target tree.
     46   virtual EventTarget* GetParentTarget() = 0;
     47 
     48   // Returns an iterator an EventTargeter can use to iterate over the list of
     49   // child EventTargets.
     50   virtual scoped_ptr<EventTargetIterator> GetChildIterator() const = 0;
     51 
     52   // Returns the EventTargeter that should be used to find the target for an
     53   // event in the subtree rooted at this EventTarget.
     54   virtual EventTargeter* GetEventTargeter() = 0;
     55 
     56   // Updates the states in |event| (e.g. location) to be suitable for |target|,
     57   // so that |event| can be dispatched to |target|.
     58   virtual void ConvertEventToTarget(EventTarget* target,
     59                                     LocatedEvent* event);
     60 
     61   // Adds a handler to receive events before the target. The handler must be
     62   // explicitly removed from the target before the handler is destroyed. The
     63   // EventTarget does not take ownership of the handler.
     64   void AddPreTargetHandler(EventHandler* handler);
     65 
     66   // Same as AddPreTargetHandler except that the |handler| is added to the front
     67   // of the list so it is the first one to receive events.
     68   void PrependPreTargetHandler(EventHandler* handler);
     69   void RemovePreTargetHandler(EventHandler* handler);
     70 
     71   // Adds a handler to receive events after the target. The handler must be
     72   // explicitly removed from the target before the handler is destroyed. The
     73   // EventTarget does not take ownership of the handler.
     74   void AddPostTargetHandler(EventHandler* handler);
     75   void RemovePostTargetHandler(EventHandler* handler);
     76 
     77   // Returns true if the event pre target list is empty.
     78   bool IsPreTargetListEmpty() const;
     79 
     80  protected:
     81   void set_target_handler(EventHandler* handler) {
     82     target_handler_ = handler;
     83   }
     84 
     85   // Overridden from EventHandler:
     86   virtual void OnEvent(Event* event) OVERRIDE;
     87   virtual void OnKeyEvent(KeyEvent* event) OVERRIDE;
     88   virtual void OnMouseEvent(MouseEvent* event) OVERRIDE;
     89   virtual void OnScrollEvent(ScrollEvent* event) OVERRIDE;
     90   virtual void OnTouchEvent(TouchEvent* event) OVERRIDE;
     91   virtual void OnGestureEvent(GestureEvent* event) OVERRIDE;
     92 
     93  private:
     94   friend class EventDispatcher;
     95   friend class EventTargetTestApi;
     96 
     97   // Returns the list of handlers that should receive the event before the
     98   // target. The handlers from the outermost target are first in the list, and
     99   // the handlers on |this| are the last in the list.
    100   void GetPreTargetHandlers(EventHandlerList* list);
    101 
    102   // Returns the list of handlers that should receive the event after the
    103   // target. The handlers from the outermost target are last in the list, and
    104   // the handlers on |this| are the first in the list.
    105   void GetPostTargetHandlers(EventHandlerList* list);
    106 
    107   EventHandlerList pre_target_list_;
    108   EventHandlerList post_target_list_;
    109   EventHandler* target_handler_;
    110 
    111   DISALLOW_COPY_AND_ASSIGN(EventTarget);
    112 };
    113 
    114 }  // namespace ui
    115 
    116 #endif  // UI_EVENTS_EVENT_TARGET_H_
    117