home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / NotificationCenter.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.3 KB  |  157 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* 
  19.    NotificationCenter.h -- class definition for NotificationCenters
  20.    Created: Chris Toshok <toshok@netscape.com>, 7-Aug-96.
  21.  */
  22.  
  23.  
  24.  
  25. #ifndef _xfe_notificationcenter_h
  26. #define _xfe_notificationcenter_h
  27.  
  28.  
  29. #include "plhash.h"
  30.  
  31. #include "xp_core.h"
  32.  
  33. class XFE_NotificationCenter; /* must be defined for the callback stuff. */
  34.  
  35. typedef void (*XFE_FunctionNotification)(XFE_NotificationCenter *n_obj,
  36.                      XFE_NotificationCenter *obj,
  37.                      void *clientData,
  38.                      void *callData);
  39.  
  40. // These macros are needed because some non GNU/SGI compilers didn't
  41. // like us throwing method pointers around.
  42. #define XFE_CALLBACK_DECL(name) \
  43.     static void name##_cb(XFE_NotificationCenter *n_obj, XFE_NotificationCenter *obj, void *clientData, void *callData); \
  44.     virtual void name(XFE_NotificationCenter *n_obj, void *clientData, void *callData);
  45.  
  46. #define XFE_CALLBACK_DEFN(classname, name) \
  47. void classname::name##_cb(XFE_NotificationCenter *n_obj, XFE_NotificationCenter *obj, void *clientData, void *callData) \
  48. { \
  49.   ((classname*)obj)->name(n_obj, clientData, callData); \
  50. } \
  51. void classname::name
  52.  
  53. typedef struct {
  54.   XFE_NotificationCenter *obj;
  55.   XFE_FunctionNotification callbackFunction;
  56.   void *clientData;
  57. } XFE_CallbackElement;
  58.  
  59. typedef struct {
  60.   char *notification_type;
  61.   
  62.   int num_interested;
  63.   int num_alloced;
  64.   XFE_CallbackElement *callbacks;
  65. } XFE_NotificationList;
  66.  
  67. // Notification Mechanism class.
  68. // Subclass from here if you want this functionality.
  69. //
  70. // If object A wants to be notified when event e happens
  71. // for object B, A needs to ask B to be put on the notification
  72. // list for event e:
  73. //
  74. //   B->registerInterest(e,   // const char*
  75. //                       A,   
  76. //                       A::<callback>,
  77. //                       <clientData>);
  78. //
  79. // then when the event e happens for B, B broadcasts this message:
  80. //
  81. //   B->notifyInterested(e);
  82. //
  83. // and A recieves the message in the form of a call to <callback>
  84. // with <clientData>.  <clientData> is NULL by default.
  85. // <callback> needs to be declared and implemented in object A:
  86. //
  87. //   // Public member function in A.h
  88. //   XFE_CALLBACK_DECL(e)
  89. //
  90. //   // Anywhere in A.cpp:
  91. //   XFE_CALLBACK_DEFN(A, e)(XFE_NotificationCenter */*obj*/, 
  92. //                           void */*clientData*/, 
  93. //                           void *callData)
  94. //
  95. // The event e is represented by a const char* stored in:
  96. //   1) object B, or 
  97. //   2) a third-party object that knows about A & B
  98. //      if A & B don't know about each other, or
  99. //   3) the superclass shared by A & B if there
  100. //      is no third-party object that knows about A & B.
  101. //
  102. // Note that object A does not need to do the registering, a third-party
  103. // object can do this (e.g. a XFE_Frame instance can set up a notification
  104. // between the menubar and the dashboard since it knows about both).   CCM
  105. class XFE_NotificationCenter
  106. {
  107. public:
  108.     XFE_NotificationCenter();
  109.     ~XFE_NotificationCenter();
  110.     
  111.     // These two deal with static member function callbacks
  112.     void registerInterest(const char *notification_name,
  113.                           XFE_NotificationCenter *obj,
  114.                           XFE_FunctionNotification notification_func,
  115.                           void *clientData = NULL);
  116.     
  117.     void unregisterInterest(const char *notification_name,
  118.                             XFE_NotificationCenter *obj,
  119.                             XFE_FunctionNotification notification_func,
  120.                             void *clientData = NULL);
  121.     
  122.     void removeAllInterest(void); // removes everyone's registered interest.
  123.     void removeAllInterest(XFE_NotificationCenter *obj); // remove all of obj's interest
  124.  
  125.     /* whether or not there is anyone that has registered interest in this
  126.        notification */
  127.     XP_Bool hasInterested(const char *notification_name);
  128.  
  129.     /* This next method really should be protected, since 
  130.        we only want the objects themselves to invoke their
  131.        own callbacks.  But, I for one don't want to have 
  132.        to write upteen million XtCallback pairs (one static,
  133.        one non-static) to invoke a callback, so we expose this
  134.        function. */
  135.     // Notify those that are interested
  136.     void notifyInterested(const char *notification_name,
  137.                           void *callData = NULL);
  138.     
  139.     void setForwarder(XFE_NotificationCenter *obj);
  140.     XFE_NotificationCenter *getForwarder();
  141.     
  142. private:
  143.     XFE_NotificationCenter *m_forwarder;
  144.     
  145.     PRHashTable *m_hashtable;
  146.     
  147.     int m_numlists;
  148.     
  149.     XFE_NotificationList *getNotificationListForName(const char *name);
  150.     XFE_NotificationList *addNewNotificationList(const char *name);
  151.  
  152.     static int destroyHashEnumerator(PRHashEntry *he, int i, void *arg);
  153. };
  154.  
  155. #endif /* _xfe_notificationcenter_h */
  156.  
  157.