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_BASE_EVENTS_EVENT_TARGET_H_
      6 #define UI_BASE_EVENTS_EVENT_TARGET_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/compiler_specific.h"
     10 #include "ui/base/events/event_handler.h"
     11 #include "ui/base/ui_export.h"
     12 
     13 namespace ui {
     14 
     15 class EventDispatcher;
     16 
     17 class UI_EXPORT EventTarget : public EventHandler {
     18  public:
     19   typedef std::vector<EventTarget*> EventTargets;
     20 
     21   class TestApi {
     22    public:
     23     explicit TestApi(EventTarget* target) : target_(target) {}
     24 
     25     const EventHandlerList& pre_target_handlers() {
     26       return target_->pre_target_list_;
     27     }
     28 
     29    private:
     30     TestApi();
     31     EventTarget* target_;
     32 
     33     DISALLOW_COPY_AND_ASSIGN(TestApi);
     34   };
     35 
     36   class DispatcherApi {
     37    public:
     38     explicit DispatcherApi(EventTarget* target) : target_(target) {}
     39 
     40     const EventHandlerList& pre_target_list() const {
     41       return target_->pre_target_list_;
     42     }
     43 
     44    private:
     45     DispatcherApi();
     46     EventTarget* target_;
     47 
     48     DISALLOW_COPY_AND_ASSIGN(DispatcherApi);
     49   };
     50 
     51   EventTarget();
     52   virtual ~EventTarget();
     53 
     54   virtual bool CanAcceptEvent(const ui::Event& event) = 0;
     55   virtual EventTarget* GetParentTarget() = 0;
     56 
     57   // Adds a handler to receive events before the target. The handler must be
     58   // explicitly removed from the target before the handler is destroyed. The
     59   // EventTarget does not take ownership of the handler.
     60   void AddPreTargetHandler(EventHandler* handler);
     61 
     62   // Same as AddPreTargetHandler except that the |handler| is added to the front
     63   // of the list so it is the first one to receive events.
     64   void PrependPreTargetHandler(EventHandler* handler);
     65   void RemovePreTargetHandler(EventHandler* handler);
     66 
     67   // Adds a handler to receive events after the target. The handler must be
     68   // explicitly removed from the target before the handler is destroyed. The
     69   // EventTarget does not take ownership of the handler.
     70   void AddPostTargetHandler(EventHandler* handler);
     71   void RemovePostTargetHandler(EventHandler* handler);
     72 
     73  protected:
     74   void set_target_handler(EventHandler* handler) {
     75     target_handler_ = handler;
     76   }
     77 
     78   // Overridden from EventHandler:
     79   virtual void OnEvent(Event* event) OVERRIDE;
     80   virtual void OnKeyEvent(KeyEvent* event) OVERRIDE;
     81   virtual void OnMouseEvent(MouseEvent* event) OVERRIDE;
     82   virtual void OnScrollEvent(ScrollEvent* event) OVERRIDE;
     83   virtual void OnTouchEvent(TouchEvent* event) OVERRIDE;
     84   virtual void OnGestureEvent(GestureEvent* event) OVERRIDE;
     85 
     86  private:
     87   friend class EventDispatcher;
     88 
     89   // Returns the list of handlers that should receive the event before the
     90   // target. The handlers from the outermost target are first in the list, and
     91   // the handlers on |this| are the last in the list.
     92   void GetPreTargetHandlers(EventHandlerList* list);
     93 
     94   // Returns the list of handlers that should receive the event after the
     95   // target. The handlers from the outermost target are last in the list, and
     96   // the handlers on |this| are the first in the list.
     97   void GetPostTargetHandlers(EventHandlerList* list);
     98 
     99   EventHandlerList pre_target_list_;
    100   EventHandlerList post_target_list_;
    101   EventHandler* target_handler_;
    102 
    103   DISALLOW_COPY_AND_ASSIGN(EventTarget);
    104 };
    105 
    106 }  // namespace ui
    107 
    108 #endif  // UI_BASE_EVENTS_EVENT_TARGET_H_
    109