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   void set_target_handler(EventHandler* handler) {
     81     target_handler_ = handler;
     82   }
     83 
     84  protected:
     85   EventHandler* target_handler() { return target_handler_; }
     86 
     87   // Overridden from EventHandler:
     88   virtual void OnEvent(Event* event) OVERRIDE;
     89   virtual void OnKeyEvent(KeyEvent* event) OVERRIDE;
     90   virtual void OnMouseEvent(MouseEvent* event) OVERRIDE;
     91   virtual void OnScrollEvent(ScrollEvent* event) OVERRIDE;
     92   virtual void OnTouchEvent(TouchEvent* event) OVERRIDE;
     93   virtual void OnGestureEvent(GestureEvent* event) OVERRIDE;
     94 
     95  private:
     96   friend class EventDispatcher;
     97   friend class EventTargetTestApi;
     98 
     99   // Returns the list of handlers that should receive the event before the
    100   // target. The handlers from the outermost target are first in the list, and
    101   // the handlers on |this| are the last in the list.
    102   void GetPreTargetHandlers(EventHandlerList* list);
    103 
    104   // Returns the list of handlers that should receive the event after the
    105   // target. The handlers from the outermost target are last in the list, and
    106   // the handlers on |this| are the first in the list.
    107   void GetPostTargetHandlers(EventHandlerList* list);
    108 
    109   EventHandlerList pre_target_list_;
    110   EventHandlerList post_target_list_;
    111   EventHandler* target_handler_;
    112 
    113   DISALLOW_COPY_AND_ASSIGN(EventTarget);
    114 };
    115 
    116 }  // namespace ui
    117 
    118 #endif  // UI_EVENTS_EVENT_TARGET_H_
    119