home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / hpp.z / WEVENT.HPP < prev    next >
C/C++ Source or Header  |  1996-11-20  |  7KB  |  212 lines

  1. /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2.    %     Copyright (C) 1994, by Watcom International Inc.  All rights    %
  3.    %     reserved.  No part of this software may be reproduced or        %
  4.    %     used in any form or by any means - graphic, electronic or       %
  5.    %     mechanical, including photocopying, recording, taping or        %
  6.    %     information storage and retrieval systems - except with the     %
  7.    %     written permission of Watcom International Inc.                 %
  8.    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  9. */
  10.  
  11. /*************************************************************************
  12.  *
  13.  * WEventGenerator -- Base Event class
  14.  *
  15.  *************************************************************************/
  16. #ifndef _WEVENT_HPP_INCLUDED
  17. #define _WEVENT_HPP_INCLUDED
  18.  
  19. #pragma warning 877 9
  20.  
  21. #ifndef _WNO_PRAGMA_PUSH
  22. #pragma pack(push,8);
  23. #pragma enum int;
  24. #endif
  25.  
  26. #ifndef _WLLIST_HPP_INCLUDED
  27. #include "wllist.hpp"
  28. #endif
  29.  
  30. #undef WEVENT_DEF
  31. #define WEVENT_DEF( name ) W ## name
  32.  
  33. enum WEventID {
  34.     #include "weventl.hpp"
  35.  
  36.     WEVENT_DEF( MarkerEvent ),
  37.     WLastEvent = WMarkerEvent - 1
  38. };
  39.  
  40. #undef WEVENT_DEF
  41.  
  42. class  WObject;
  43. struct WEventData;
  44.  
  45. /* Define a generic callback type */
  46.  
  47. typedef WBool WCMDEF (WObject::*WEventHandler)( WObject *source,
  48.                                                 WEventData *event );
  49. #define WEventHandlerCast( __cls,__hdlr ) (WEventHandler)((char (__cls::*)(WObject *,WEventData *))&__cls::__hdlr)
  50.  
  51. /* Define a struct for setting multiple events */
  52.  
  53. struct WEventHandlerList {
  54.     WEventID        eventID;
  55.     WEventHandler   eventHandler;
  56.     WObject        *object;
  57.     void           *userData;
  58. };
  59.  
  60. /* The event class -- an object must inherit from this class
  61.    to be able to set and invoke events */
  62.  
  63. class WCMCLASS WEventGenerator : public WObject {
  64.  
  65.     WDeclareSubclass( WEventGenerator, WObject );
  66.  
  67.     public:
  68.  
  69.         struct event_data {
  70.             wllist_link         link;
  71.             WEventID            id;
  72.             WObject *           object;
  73.             WEventHandler       handler;
  74.             WLong               returns;
  75.             void *              userdata;
  76.             WBool               reserved;
  77.         };
  78.  
  79.     protected:
  80.  
  81.         /**************************************************************
  82.          * Constructors and destructors
  83.          **************************************************************/
  84.  
  85.         WEventGenerator();
  86.  
  87.         virtual ~WEventGenerator();
  88.  
  89.         /**************************************************************
  90.          * Properties
  91.          **************************************************************/
  92.  
  93.     public:
  94.  
  95.         /**************************************************************
  96.          * Methods
  97.          **************************************************************/
  98.  
  99.         // CallEventHandler
  100.  
  101.         virtual WBool CallEventHandler( WEventID id,
  102.                                         WObject * object,
  103.                                         WEventData * eventData=NULL,
  104.                                         WLong * retval=NULL,
  105.                                         WInt indexToStartAt=0 ) const;
  106.  
  107.         // EventID
  108.  
  109.         static WEventID EventID( const WChar * name );
  110.  
  111.         // HasEventHandler
  112.  
  113.         virtual WBool HasEventHandler( WEventID id ) const;
  114.  
  115.         // RemoveAllEventHandlers
  116.  
  117.         virtual WBool RemoveAllEventHandlers();
  118.  
  119.         // RemoveEventHandler
  120.  
  121.         virtual WBool RemoveEventHandler( WEventID id,
  122.                                           WBool removeAll=FALSE,
  123.                                           WInt * index=NULL );
  124.         virtual WBool RemoveEventHandler( WEventID id,
  125.                                           WObject * object,
  126.                                           WEventHandler handler,
  127.                                           WInt * index=NULL );
  128.  
  129.         // RemoveEventHandlerList
  130.         //
  131.         //    Remove several events at once from a null-terminated
  132.         //    array of events.  If no object is passed, uses "this"
  133.         //    by default.
  134.  
  135.         WBool RemoveEventHandlerList( WEventHandlerList * list,
  136.                                       WObject * object=NULL );
  137.  
  138.         // SetEventHandlerList
  139.         //
  140.         //    Set several events at once from a null-terminated
  141.         //    array of events.  If no object is passed, uses "this"
  142.         //    by default.
  143.  
  144.         WBool SetEventHandlerList( WEventHandlerList * list,
  145.                                    WObject * object=NULL );
  146.  
  147.         /**************************************************************
  148.          * Item Properties
  149.          **************************************************************/
  150.  
  151.         virtual WBool SetEventHandler( WEventID id, WObject * object,
  152.                                        WEventHandler handler,
  153.                                        void * userdata=NULL,
  154.                                        WInt * index=NULL );
  155.  
  156.         virtual WBool GetEventHandler( WEventID id, WObject ** object,
  157.                                        WEventHandler * handler,
  158.                                        void ** userdata=NULL,
  159.                                        WEventHandler current=NULL ) const;
  160.         /**************************************************************
  161.          * Cache
  162.          **************************************************************/
  163.  
  164.         // FlushCache
  165.  
  166.         static void FlushCache();
  167.  
  168.         // CacheSize
  169.  
  170.         static WULong GetCacheSize();
  171.         static WBool SetCacheSize( WULong newSize );
  172.  
  173.         /**************************************************************
  174.          * Others
  175.          **************************************************************/
  176.  
  177.     protected:
  178.  
  179.         virtual event_data * FindEvent( WEventID id ) const;
  180.  
  181.         virtual WBool FastCallEventHandler( event_data *first,
  182.                                             WObject * object,
  183.                                             WEventData * eventData=NULL,
  184.                                             WLong * retval=NULL,
  185.                                             WInt indexToStartAt=0 ) const;
  186.  
  187.         enum WEventNotice {
  188.             WEventNoticeAdd,
  189.             WEventNoticeRemove,
  190.             WEventNoticeRemoveAll
  191.         };
  192.  
  193.         virtual void EventNotice( WEventID id, WEventNotice type );
  194.  
  195.         /**************************************************************
  196.          * Data members
  197.          **************************************************************/
  198.  
  199.     private:
  200.  
  201.         WULong                  _bitfield : 32;
  202.         wllist_header           _events;
  203.         WEventHandler           _currHandler;
  204. };
  205.  
  206. #ifndef _WNO_PRAGMA_PUSH
  207. #pragma enum pop;
  208. #pragma pack(pop);
  209. #endif
  210.  
  211. #endif // _WEVENT_HPP_INCLUDED
  212.