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.
5 #ifndef UI_EVENTS_EVENT_TARGET_H_
6 #define UI_EVENTS_EVENT_TARGET_H_
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"
18 class EventDispatcher
;
20 class EventTargetIterator
;
23 class EVENTS_EXPORT EventTarget
: public EventHandler
{
27 explicit DispatcherApi(EventTarget
* target
) : target_(target
) {}
29 const EventHandlerList
& pre_target_list() const {
30 return target_
->pre_target_list_
;
37 DISALLOW_COPY_AND_ASSIGN(DispatcherApi
);
41 virtual ~EventTarget();
43 virtual bool CanAcceptEvent(const Event
& event
) = 0;
45 // Returns the parent EventTarget in the event-target tree.
46 virtual EventTarget
* GetParentTarget() = 0;
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;
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;
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
,
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
);
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
);
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
);
77 // Returns true if the event pre target list is empty.
78 bool IsPreTargetListEmpty() const;
81 void set_target_handler(EventHandler
* handler
) {
82 target_handler_
= handler
;
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
;
94 friend class EventDispatcher
;
95 friend class EventTargetTestApi
;
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
);
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
);
107 EventHandlerList pre_target_list_
;
108 EventHandlerList post_target_list_
;
109 EventHandler
* target_handler_
;
111 DISALLOW_COPY_AND_ASSIGN(EventTarget
);
116 #endif // UI_EVENTS_EVENT_TARGET_H_