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_DISPATCHER_H_
6 #define UI_EVENTS_EVENT_DISPATCHER_H_
8 #include "base/auto_reset.h"
9 #include "ui/events/event.h"
10 #include "ui/events/event_constants.h"
11 #include "ui/events/event_handler.h"
12 #include "ui/events/events_export.h"
16 class EventDispatcher
;
20 struct EventDispatchDetails
{
21 EventDispatchDetails()
22 : dispatcher_destroyed(false),
23 target_destroyed(false) {}
24 bool dispatcher_destroyed
;
25 bool target_destroyed
;
28 class EVENTS_EXPORT EventDispatcherDelegate
{
30 EventDispatcherDelegate();
31 virtual ~EventDispatcherDelegate();
33 // Returns whether an event can still be dispatched to a target. (e.g. during
34 // event dispatch, one of the handlers may have destroyed the target, in which
35 // case the event can no longer be dispatched to the target).
36 virtual bool CanDispatchToTarget(EventTarget
* target
) = 0;
38 // Returns the event being dispatched (or NULL if no event is being
40 Event
* current_event();
42 // Dispatches |event| to |target|. This calls |PreDispatchEvent()| before
43 // dispatching the event, and |PostDispatchEvent()| after the event has been
45 EventDispatchDetails
DispatchEvent(EventTarget
* target
, Event
* event
)
49 // This is called once a target has been determined for an event, right before
50 // the event is dispatched to the target. This function may modify |event| to
51 // prepare it for dispatch (e.g. update event flags, location etc.).
52 virtual EventDispatchDetails
PreDispatchEvent(
54 Event
* event
) WARN_UNUSED_RESULT
;
56 // This is called right after the event dispatch is completed.
57 // |target| is NULL if the target was deleted during dispatch.
58 virtual EventDispatchDetails
PostDispatchEvent(
60 const Event
& event
) WARN_UNUSED_RESULT
;
63 // Dispatches the event to the target.
64 EventDispatchDetails
DispatchEventToTarget(EventTarget
* target
,
65 Event
* event
) WARN_UNUSED_RESULT
;
67 EventDispatcher
* dispatcher_
;
69 DISALLOW_COPY_AND_ASSIGN(EventDispatcherDelegate
);
72 // Dispatches events to appropriate targets.
73 class EVENTS_EXPORT EventDispatcher
{
75 explicit EventDispatcher(EventDispatcherDelegate
* delegate
);
76 virtual ~EventDispatcher();
78 void ProcessEvent(EventTarget
* target
, Event
* event
);
80 const Event
* current_event() const { return current_event_
; }
81 Event
* current_event() { return current_event_
; }
83 bool delegate_destroyed() const { return !delegate_
; }
85 void OnHandlerDestroyed(EventHandler
* handler
);
86 void OnDispatcherDelegateDestroyed();
89 void DispatchEventToEventHandlers(EventHandlerList
* list
, Event
* event
);
91 // Dispatches an event, and makes sure it sets ER_CONSUMED on the
92 // event-handling result if the dispatcher itself has been destroyed during
93 // dispatching the event to the event handler.
94 void DispatchEvent(EventHandler
* handler
, Event
* event
);
96 EventDispatcherDelegate
* delegate_
;
98 Event
* current_event_
;
100 EventHandlerList handler_list_
;
102 DISALLOW_COPY_AND_ASSIGN(EventDispatcher
);
107 #endif // UI_EVENTS_EVENT_DISPATCHER_H_