blob: 01d4f7a252f677d2e2aff2e45560f6332a6d991f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkEventSink_DEFINED
11#define SkEventSink_DEFINED
12
13#include "SkRefCnt.h"
14#include "SkEvent.h"
15
16struct SkTagList;
17
18/** \class SkEventSink
19
20 SkEventSink is the base class for all objects that receive SkEvents.
21*/
22class SkEventSink : public SkRefCnt {
23public:
robertphillips@google.coma22e2112012-08-16 14:58:06 +000024 SK_DECLARE_INST_COUNT(SkEventSink)
25
26 SkEventSink();
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 virtual ~SkEventSink();
28
reed@google.com87fac4a2011-08-04 13:50:17 +000029 /**
30 * Returns this eventsink's unique ID. Use this to post SkEvents to
31 * this eventsink.
32 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 SkEventSinkID getSinkID() const { return fID; }
34
reed@google.com87fac4a2011-08-04 13:50:17 +000035 /**
36 * Call this to pass an event to this object for processing. Returns true if the
37 * event was handled.
38 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 bool doEvent(const SkEvent&);
reed@google.com87fac4a2011-08-04 13:50:17 +000040
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 /** Returns true if the sink (or one of its subclasses) understands the event as a query.
42 If so, the sink may modify the event to communicate its "answer".
43 */
44 bool doQuery(SkEvent* query);
45
reed@google.com87fac4a2011-08-04 13:50:17 +000046 /**
47 * Add sinkID to the list of listeners, to receive events from calls to sendToListeners()
48 * and postToListeners(). If sinkID already exists in the listener list, no change is made.
49 */
50 void addListenerID(SkEventSinkID sinkID);
51
52 /**
53 * Copy listeners from one event sink to another, typically from parent to child.
54 * @param from the event sink to copy the listeners from
55 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 void copyListeners(const SkEventSink& from);
reed@google.com87fac4a2011-08-04 13:50:17 +000057
58 /**
59 * Remove sinkID from the list of listeners. If sinkID does not appear in the list,
60 * no change is made.
61 */
62 void removeListenerID(SkEventSinkID);
63
64 /**
65 * Returns true if there are 1 or more listeners attached to this eventsink
66 */
67 bool hasListeners() const;
68
69 /**
70 * Posts a copy of evt to each of the eventsinks in the lisener list.
71 * This ignores the targetID and target proc in evt.
72 */
73 void postToListeners(const SkEvent& evt, SkMSec delay = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
75 enum EventResult {
76 kHandled_EventResult, //!< the eventsink returned true from its doEvent method
77 kNotHandled_EventResult, //!< the eventsink returned false from its doEvent method
78 kSinkNotFound_EventResult //!< no matching eventsink was found for the event's getSink().
79 };
reed@android.com8a1c16f2008-12-17 15:59:43 +000080
reed@google.com87fac4a2011-08-04 13:50:17 +000081 /**
82 * DoEvent handles dispatching the event to its target ID or proc.
83 */
84 static EventResult DoEvent(const SkEvent&);
85
86 /**
87 * Returns the matching eventsink, or null if not found
88 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 static SkEventSink* FindSink(SkEventSinkID);
90
91protected:
92 /** Override this to handle events in your subclass. Be sure to call the inherited version
93 for events that you don't handle.
94 */
95 virtual bool onEvent(const SkEvent&);
96 virtual bool onQuery(SkEvent*);
97
98 SkTagList* findTagList(U8CPU tag) const;
99 void addTagList(SkTagList*);
100 void removeTagList(U8CPU tag);
101
102private:
103 SkEventSinkID fID;
104 SkTagList* fTagHead;
105
106 // for our private link-list
107 SkEventSink* fNextSink;
robertphillips@google.coma22e2112012-08-16 14:58:06 +0000108
109 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110};
111
112#endif