epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkEventSink_DEFINED |
| 11 | #define SkEventSink_DEFINED |
| 12 | |
| 13 | #include "SkRefCnt.h" |
| 14 | #include "SkEvent.h" |
| 15 | |
| 16 | struct SkTagList; |
| 17 | |
| 18 | /** \class SkEventSink |
| 19 | |
| 20 | SkEventSink is the base class for all objects that receive SkEvents. |
| 21 | */ |
| 22 | class SkEventSink : public SkRefCnt { |
| 23 | public: |
| 24 | SkEventSink(); |
| 25 | virtual ~SkEventSink(); |
| 26 | |
| 27 | /** Returns this eventsink's unique ID. Use this to post SkEvents to |
| 28 | this eventsink. |
| 29 | */ |
| 30 | SkEventSinkID getSinkID() const { return fID; } |
| 31 | |
| 32 | /** Call this to pass an event to this object for processing. Returns true if the |
| 33 | event was handled. |
| 34 | */ |
| 35 | bool doEvent(const SkEvent&); |
| 36 | /** Returns true if the sink (or one of its subclasses) understands the event as a query. |
| 37 | If so, the sink may modify the event to communicate its "answer". |
| 38 | */ |
| 39 | bool doQuery(SkEvent* query); |
| 40 | |
| 41 | /** Add sinkID to the list of listeners, to receive events from calls to sendToListeners() |
| 42 | and postToListeners(). If sinkID already exists in the listener list, no change is made. |
| 43 | */ |
| 44 | void addListenerID(SkEventSinkID sinkID); |
| 45 | /** Copy listeners from one event sink to another, typically from parent to child. |
| 46 | @param from the event sink to copy the listeners from |
| 47 | */ |
| 48 | void copyListeners(const SkEventSink& from); |
| 49 | /** Remove sinkID from the list of listeners. If sinkID does not appear in the list, |
| 50 | no change is made. |
| 51 | */ |
| 52 | void removeListenerID(SkEventSinkID); |
| 53 | /** Returns true if there are 1 or more listeners attached to this eventsink |
| 54 | */ |
| 55 | bool hasListeners() const; |
| 56 | /** Posts a copy of evt to each of the eventsinks in the lisener list. |
| 57 | */ |
| 58 | void postToListeners(const SkEvent& evt, SkMSec delay = 0); |
| 59 | |
| 60 | enum EventResult { |
| 61 | kHandled_EventResult, //!< the eventsink returned true from its doEvent method |
| 62 | kNotHandled_EventResult, //!< the eventsink returned false from its doEvent method |
| 63 | kSinkNotFound_EventResult //!< no matching eventsink was found for the event's getSink(). |
| 64 | }; |
| 65 | /** DoEvent handles searching for an eventsink object that matches the targetID. |
| 66 | If one is found, it calls the sink's doEvent method, returning |
| 67 | either kHandled_EventResult or kNotHandled_EventResult. If no matching |
| 68 | eventsink is found, kSinkNotFound_EventResult is returned. |
| 69 | */ |
| 70 | static EventResult DoEvent(const SkEvent&, SkEventSinkID targetID); |
| 71 | |
| 72 | /** Returns the matching eventsink, or null if not found |
| 73 | */ |
| 74 | static SkEventSink* FindSink(SkEventSinkID); |
| 75 | |
| 76 | protected: |
| 77 | /** Override this to handle events in your subclass. Be sure to call the inherited version |
| 78 | for events that you don't handle. |
| 79 | */ |
| 80 | virtual bool onEvent(const SkEvent&); |
| 81 | virtual bool onQuery(SkEvent*); |
| 82 | |
| 83 | SkTagList* findTagList(U8CPU tag) const; |
| 84 | void addTagList(SkTagList*); |
| 85 | void removeTagList(U8CPU tag); |
| 86 | |
| 87 | private: |
| 88 | SkEventSinkID fID; |
| 89 | SkTagList* fTagHead; |
| 90 | |
| 91 | // for our private link-list |
| 92 | SkEventSink* fNextSink; |
| 93 | }; |
| 94 | |
| 95 | #endif |
| 96 | |