180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2006 The Android Open Source Project
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef SkEventSink_DEFINED
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkEventSink_DEFINED
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkRefCnt.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkEvent.h"
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustruct SkTagList;
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** \class SkEventSink
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkEventSink is the base class for all objects that receive SkEvents.
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkEventSink : public SkRefCnt {
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_DECLARE_INST_COUNT(SkEventSink)
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru             SkEventSink();
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual ~SkEventSink();
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Returns this eventsink's unique ID. Use this to post SkEvents to
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  this eventsink.
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkEventSinkID getSinkID() const { return fID; }
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Call this to pass an event to this object for processing. Returns true if the
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  event was handled.
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool doEvent(const SkEvent&);
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /** Returns true if the sink (or one of its subclasses) understands the event as a query.
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        If so, the sink may modify the event to communicate its "answer".
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    */
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool doQuery(SkEvent* query);
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Add sinkID to the list of listeners, to receive events from calls to sendToListeners()
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  and postToListeners(). If sinkID already exists in the listener list, no change is made.
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void addListenerID(SkEventSinkID sinkID);
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Copy listeners from one event sink to another, typically from parent to child.
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  @param from the event sink to copy the listeners from
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void copyListeners(const SkEventSink& from);
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Remove sinkID from the list of listeners. If sinkID does not appear in the list,
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  no change is made.
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void removeListenerID(SkEventSinkID);
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Returns true if there are 1 or more listeners attached to this eventsink
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool hasListeners() const;
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Posts a copy of evt to each of the eventsinks in the lisener list.
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  This ignores the targetID and target proc in evt.
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void postToListeners(const SkEvent& evt, SkMSec delay = 0);
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    enum EventResult {
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kHandled_EventResult,       //!< the eventsink returned true from its doEvent method
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kNotHandled_EventResult,    //!< the eventsink returned false from its doEvent method
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kSinkNotFound_EventResult   //!< no matching eventsink was found for the event's getSink().
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  DoEvent handles dispatching the event to its target ID or proc.
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static EventResult DoEvent(const SkEvent&);
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Returns the matching eventsink, or null if not found
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static SkEventSink* FindSink(SkEventSinkID);
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /** Override this to handle events in your subclass. Be sure to call the inherited version
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        for events that you don't handle.
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    */
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual bool onEvent(const SkEvent&);
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual bool onQuery(SkEvent*);
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkTagList*  findTagList(U8CPU tag) const;
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void        addTagList(SkTagList*);
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void        removeTagList(U8CPU tag);
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkEventSinkID   fID;
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkTagList*      fTagHead;
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // for our private link-list
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkEventSink*    fNextSink;
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef SkRefCnt INHERITED;
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
113