home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / hpp.z / WEVENT.HPP < prev    next >
C/C++ Source or Header  |  1996-12-13  |  7KB  |  215 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 )                                  \
  50.         static_cast< WBool (WObject::*)( WObject *, WEventData * ) >(       \
  51.           reinterpret_cast< WBool (__cls::*)( WObject *, WEventData * ) >(  \
  52.             &__cls::__hdlr ) )
  53.  
  54. /* Define a struct for setting multiple events */
  55.  
  56. struct WEventHandlerList {
  57.     WEventID        eventID;
  58.     WEventHandler   eventHandler;
  59.     WObject        *object;
  60.     void           *userData;
  61. };
  62.  
  63. /* The event class -- an object must inherit from this class
  64.    to be able to set and invoke events */
  65.  
  66. class WCMCLASS WEventGenerator : public WObject {
  67.  
  68.     WDeclareSubclass( WEventGenerator, WObject );
  69.  
  70.     public:
  71.  
  72.         struct event_data {
  73.             wllist_link         link;
  74.             WEventID            id;
  75.             WObject *           object;
  76.             WEventHandler       handler;
  77.             WLong               returns;
  78.             void *              userdata;
  79.             WBool               reserved;
  80.         };
  81.  
  82.     protected:
  83.  
  84.         /**************************************************************
  85.          * Constructors and destructors
  86.          **************************************************************/
  87.  
  88.         WEventGenerator();
  89.  
  90.         virtual ~WEventGenerator();
  91.  
  92.         /**************************************************************
  93.          * Properties
  94.          **************************************************************/
  95.  
  96.     public:
  97.  
  98.         /**************************************************************
  99.          * Methods
  100.          **************************************************************/
  101.  
  102.         // CallEventHandler
  103.  
  104.         virtual WBool CallEventHandler( WEventID id,
  105.                                         WObject * object,
  106.                                         WEventData * eventData=NULL,
  107.                                         WLong * retval=NULL,
  108.                                         WInt indexToStartAt=0 ) const;
  109.  
  110.         // EventID
  111.  
  112.         static WEventID EventID( const WChar * name );
  113.  
  114.         // HasEventHandler
  115.  
  116.         virtual WBool HasEventHandler( WEventID id ) const;
  117.  
  118.         // RemoveAllEventHandlers
  119.  
  120.         virtual WBool RemoveAllEventHandlers();
  121.  
  122.         // RemoveEventHandler
  123.  
  124.         virtual WBool RemoveEventHandler( WEventID id,
  125.                                           WBool removeAll=FALSE,
  126.                                           WInt * index=NULL );
  127.         virtual WBool RemoveEventHandler( WEventID id,
  128.                                           WObject * object,
  129.                                           WEventHandler handler,
  130.                                           WInt * index=NULL );
  131.  
  132.         // RemoveEventHandlerList
  133.         //
  134.         //    Remove several events at once from a null-terminated
  135.         //    array of events.  If no object is passed, uses "this"
  136.         //    by default.
  137.  
  138.         WBool RemoveEventHandlerList( WEventHandlerList * list,
  139.                                       WObject * object=NULL );
  140.  
  141.         // SetEventHandlerList
  142.         //
  143.         //    Set several events at once from a null-terminated
  144.         //    array of events.  If no object is passed, uses "this"
  145.         //    by default.
  146.  
  147.         WBool SetEventHandlerList( WEventHandlerList * list,
  148.                                    WObject * object=NULL );
  149.  
  150.         /**************************************************************
  151.          * Item Properties
  152.          **************************************************************/
  153.  
  154.         virtual WBool SetEventHandler( WEventID id, WObject * object,
  155.                                        WEventHandler handler,
  156.                                        void * userdata=NULL,
  157.                                        WInt * index=NULL );
  158.  
  159.         virtual WBool GetEventHandler( WEventID id, WObject ** object,
  160.                                        WEventHandler * handler,
  161.                                        void ** userdata=NULL,
  162.                                        WEventHandler current=NULL ) const;
  163.         /**************************************************************
  164.          * Cache
  165.          **************************************************************/
  166.  
  167.         // FlushCache
  168.  
  169.         static void FlushCache();
  170.  
  171.         // CacheSize
  172.  
  173.         static WULong GetCacheSize();
  174.         static WBool SetCacheSize( WULong newSize );
  175.  
  176.         /**************************************************************
  177.          * Others
  178.          **************************************************************/
  179.  
  180.     protected:
  181.  
  182.         virtual event_data * FindEvent( WEventID id ) const;
  183.  
  184.         virtual WBool FastCallEventHandler( event_data *first,
  185.                                             WObject * object,
  186.                                             WEventData * eventData=NULL,
  187.                                             WLong * retval=NULL,
  188.                                             WInt indexToStartAt=0 ) const;
  189.  
  190.         enum WEventNotice {
  191.             WEventNoticeAdd,
  192.             WEventNoticeRemove,
  193.             WEventNoticeRemoveAll
  194.         };
  195.  
  196.         virtual void EventNotice( WEventID id, WEventNotice type );
  197.  
  198.         /**************************************************************
  199.          * Data members
  200.          **************************************************************/
  201.  
  202.     private:
  203.  
  204.         WULong                  _bitfield : 32;
  205.         wllist_header           _events;
  206.         WEventHandler           _currHandler;
  207. };
  208.  
  209. #ifndef _WNO_PRAGMA_PUSH
  210. #pragma enum pop;
  211. #pragma pack(pop);
  212. #endif
  213.  
  214. #endif // _WEVENT_HPP_INCLUDED
  215.