home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWLINC.PAK / EVENTHAN.H < prev    next >
C/C++ Source or Header  |  1995-08-29  |  6KB  |  179 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1992, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   TEventHandler and related classes & macros
  6. //----------------------------------------------------------------------------
  7. #if !defined(OWL_EVENTHAN_H)
  8. #define OWL_EVENTHAN_H
  9.  
  10. #if !defined(OWL_SIGNATUR_H)
  11. # include <owl/signatur.h>
  12. #endif
  13. #if !defined(OWL_DISPATCH_H)
  14. # include <owl/dispatch.h>
  15. #endif
  16.  
  17. #if defined(__TRACE) || defined(__WARN)
  18.   //
  19.   // class/operator which converts a Windows message to its string equivalent
  20.   //
  21.   class _EXPCLASS ostream;
  22.   class _OWLCLASS MsgName {
  23.     public:
  24.       friend ostream& operator <<(ostream& os, const MsgName& msg);
  25.       MsgName(uint msg) : Message(msg) {}
  26.     private:
  27.       uint Message;
  28.   };
  29. #endif
  30.  
  31. #if defined(OWLRTFAR)
  32. # define __RTFAR  __far
  33. #else
  34. # define __RTFAR
  35. #endif
  36.  
  37. //
  38. // messages defined for OWL use - the top of the user range of ids is reserved
  39. //
  40. #define WM_OWLLAST        0x7FFF
  41. #define WM_OWLFIRST       (WM_OWLLAST - 0x03FF)
  42.  
  43. #define WM_COMMAND_ENABLE (WM_OWLLAST - 0)
  44. #define WM_CHILDINVALID   (WM_OWLLAST - 1)
  45. #define WM_OWLDOCUMENT    (WM_OWLLAST - 2)
  46. #define WM_OWLVIEW        (WM_OWLLAST - 3)
  47. #define WM_OWLNOTIFY      (WM_OWLLAST - 4)
  48. #define WM_OWLPREPROCMENU (WM_OWLLAST - 5)
  49. #define WM_OWLCANCLOSE    (WM_OWLLAST - 6)
  50. #define WM_VBXINITFORM    (WM_OWLLAST - 7)
  51. #define WM_VBXNAME        (WM_OWLLAST - 8)
  52. #define WM_VBXBASE        (WM_OWLLAST - 8 - 256)
  53. #define WM_OWLWAKEUP      (WM_VBXBASE - 1)
  54.  
  55. template <class T> class TResponseTableEntry;  // forward declaration
  56. typedef TResponseTableEntry<GENERIC>  TGenericTableEntry;
  57.  
  58. //
  59. //  class TEventHandler
  60. //  ----- -------------
  61. //  base class from which to derive classes that can handle events
  62. //
  63. class _OWLCLASS _RTTI TEventHandler {
  64.   public:
  65.     class TEventInfo {
  66.       public:
  67.         const uint                  Msg;
  68.         const uint                  Id;
  69.         GENERIC*                    Object;
  70.         TGenericTableEntry __RTFAR* Entry;
  71.  
  72.         TEventInfo(uint msg, uint id = 0) : Msg(msg), Id(id) {Entry = 0;}
  73.     };
  74.     typedef bool(*TEqualOperator)(TGenericTableEntry __RTFAR&, TEventInfo&);
  75.  
  76.     //
  77.     // searches the list of response table entries looking for a match
  78.     //
  79.     // since class TEventHandler doesn't have any entries, this routine just
  80.     // returns false
  81.     //
  82.     virtual bool     Find(TEventInfo& info, TEqualOperator op = 0);
  83.  
  84.     virtual LRESULT  Dispatch(TEventInfo& info, WPARAM wp, LPARAM lp = 0);
  85.  
  86.   protected:
  87.     bool             SearchEntries(TGenericTableEntry __RTFAR* entries,
  88.                                    TEventInfo& info,
  89.                                    TEqualOperator op);
  90. };
  91.  
  92. //
  93. //  template class TResponseTableEntry
  94. //  -------- ----- -------------------
  95. //  entries into a response table
  96. //
  97. template <class T> class TResponseTableEntry {
  98.   public:
  99.     typedef void (T::*PMF)();
  100.  
  101.     union {
  102.       uint          Msg;
  103.       uint          NotifyCode;
  104.     };
  105.     uint            Id;
  106.     TAnyDispatcher  Dispatcher;
  107.     PMF             Pmf;
  108. };
  109.  
  110. //
  111. // macros to declare a response table
  112. //
  113. #define DECLARE_RESPONSE_TABLE(cls)\
  114.   private:\
  115.     static TResponseTableEntry< cls > __RTFAR __entries[];\
  116.     typedef TResponseTableEntry< cls >::PMF   TMyPMF;\
  117.     typedef cls                               TMyClass;\
  118.   public:\
  119.     bool  Find(TEventInfo&, TEqualOperator = 0)
  120.  
  121. #define END_RESPONSE_TABLE\
  122.   {0, 0, 0, 0}}
  123.  
  124. #define DEFINE_RESPONSE_TABLE_ENTRIES(cls)\
  125.   TResponseTableEntry< cls > __RTFAR  cls::__entries[] = {
  126.  
  127. //
  128. // macro to define a response table for a class with no base response tables
  129. //
  130. // you use it like this:
  131. //    DEFINE_RESPONSE_TABLE(cls)
  132. //      EV_WM_PAINT,
  133. //      EV_WM_LBUTTONDOWN,
  134. //    END_RESPONSE_TABLE;
  135. //
  136. #define DEFINE_RESPONSE_TABLE(cls)\
  137.   bool  cls::Find(TEventInfo& eventInfo, TEqualOperator equal)\
  138.       {eventInfo.Object = (GENERIC*)this;\
  139.        return SearchEntries((TGenericTableEntry __RTFAR*)__entries, eventInfo, equal);}\
  140.   DEFINE_RESPONSE_TABLE_ENTRIES(cls)
  141.  
  142. //
  143. // macro to define a response table for a class with one base. use this macro
  144. // exactly like macro DEFINE_RESPONSE_TABLE
  145. //
  146. #define DEFINE_RESPONSE_TABLE1(cls, base)\
  147.   bool  cls::Find(TEventInfo& eventInfo, TEqualOperator equal)\
  148.       {eventInfo.Object = (GENERIC*)this;\
  149.        return SearchEntries((TGenericTableEntry __RTFAR*)__entries, eventInfo, equal) ||\
  150.               base::Find(eventInfo, equal);}\
  151.   DEFINE_RESPONSE_TABLE_ENTRIES(cls)
  152.  
  153. //
  154. // macro to define a response table for a class with two bases. use this macro
  155. // exactly like macro DEFINE_RESPONSE_TABLE
  156. //
  157. #define DEFINE_RESPONSE_TABLE2(cls, base1, base2)\
  158.   bool  cls::Find(TEventInfo& eventInfo, TEqualOperator equal)\
  159.       {eventInfo.Object = (GENERIC*)this;\
  160.        return SearchEntries((TGenericTableEntry __RTFAR*)__entries, eventInfo, equal) ||\
  161.               base1::Find(eventInfo, equal) ||\
  162.               base2::Find(eventInfo, equal);}\
  163.   DEFINE_RESPONSE_TABLE_ENTRIES(cls)
  164.  
  165. //
  166. // macro to define a response table for a class with three bases. use this macro
  167. // exactly like macro DEFINE_RESPONSE_TABLE
  168. //
  169. #define DEFINE_RESPONSE_TABLE3(cls, base1, base2, base3)\
  170.   bool  cls::Find(TEventInfo& eventInfo, TEqualOperator equal)\
  171.       {eventInfo.Object = (GENERIC*)this;\
  172.        return SearchEntries((TGenericTableEntry __RTFAR*)__entries, eventInfo, equal) ||\
  173.               base1::Find(eventInfo, equal) ||\
  174.               base2::Find(eventInfo, equal) ||\
  175.               base3::Find(eventInfo, equal);}\
  176.   DEFINE_RESPONSE_TABLE_ENTRIES(cls)
  177.  
  178. #endif  // OWL_EVENTHAN_H
  179.