home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Power Pack / Visual_Basic4_Power_Pack.bin / vb4files / vbof / vbofemgr.cls < prev    next >
Encoding:
Text File  |  1996-11-20  |  13.0 KB  |  390 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "VBOFEventManager"
  6. Attribute VB_Creatable = True
  7. Attribute VB_Exposed = True
  8. Option Explicit
  9.  
  10. ' (c) Copyright 1995 Ken Fitzpatrick
  11. '     All Rights Reserved
  12. '     Cannot be distributed or sold without permission
  13. '
  14. ' VBObjectFrameworkEventManager is a supplemental
  15. '   Event Manager for Microsoft Visual Basic 4.0.
  16. '   It is valid only in conjunction with the
  17. '   following Classes Modules:
  18. '       VBOFCollection
  19. '       VBOFObjectLink
  20. '       VBOFObjectManager
  21. '       VBOFEventObject
  22. '
  23. ' The VBOFEventManager interface, while
  24. '   public, is not for public use.
  25. '   VBOFEventManager is fully
  26. '   encapsulated by VBOFObjectManager
  27. '   and applications should use that interface
  28. '   for all VBOFEventManager activity.
  29. '
  30. ' See Class Module "VBOFObjectManager" for
  31. '     documentation details
  32.  
  33. Private pvtObjectEvents As New Collection
  34. Private pvtCollectionEvents As New Collection
  35. Private pvtVBOFObjectManager As VBOFObjectManager
  36.  
  37. Private Const pvtReceiverDoesNotSupportThisMethod = 438
  38.  
  39. Public Property Get ObjectManager() As VBOFObjectManager
  40. Attribute ObjectManager.VB_Description = "Private"
  41.     Set ObjectManager = pvtVBOFObjectManager
  42. End Property
  43.  
  44.  
  45. Public Property Set ObjectManager(anObjectManager As VBOFObjectManager)
  46.     Set pvtVBOFObjectManager = anObjectManager
  47. End Property
  48.  
  49.  
  50.  
  51. Public Function ObjectID() As Long
  52. Attribute ObjectID.VB_Description = "Private"
  53.     ObjectID = -1
  54. End Function
  55.  
  56.  
  57.  
  58. Public Function RegisterForObjectEvent(Optional TriggerObject As Variant, Optional TriggerObjectType As Variant, Optional TriggerEvent As Variant, Optional RegisterObject As Variant, Optional RegisterType As Variant, Optional SkipTriggerObject As Variant, Optional NewEventObject As Variant) As Boolean
  59. Attribute RegisterForObjectEvent.VB_Description = "Private"
  60. ' Register the specified RegisterObject or
  61. '   RegisterType to receive notification upon the
  62. '   posting of the specified TriggerEvent by the
  63. '   specified TriggerObject or TriggerObjectType
  64.  
  65. #If NoEventMgr = False Then
  66.     Dim tempNewEventObject As New VBOFEventObject
  67.     Dim tempEventObject As VBOFEventObject
  68.     
  69.     On Local Error Resume Next
  70.     
  71. ' bullet-proofing
  72.     If IsMissing(RegisterObject) And IsMissing(RegisterType) Then
  73.         pvtErrorMessage TypeName(Me) & " cannot process the '.RegisterForObjectEvent' method for this object because the 'RegisterObject:=' and the 'RegisterType:=' parameters are missing.  At least one of these must be specified."
  74.         RegisterForObjectEvent = False
  75.         Exit Function
  76.     End If
  77.     If IsMissing(TriggerObject) And IsMissing(TriggerObjectType) Then
  78.         If IsMissing(SkipTriggerObject) Or Not SkipTriggerObject Then
  79.             pvtErrorMessage TypeName(Me) & " cannot process the '.RegisterForObjectEvent' method for this object because the 'TriggerObject:=' and the 'TriggerObjectType:=' parameters are missing.  At least one of these must be specified."
  80.             RegisterForObjectEvent = False
  81.             Exit Function
  82.         End If
  83.     End If
  84.  
  85. ' initialize the EventObject
  86.     If IsMissing(NewEventObject) Then
  87.         Set tempEventObject = tempNewEventObject
  88.     Else
  89.         Set tempEventObject = NewEventObject
  90.     End If
  91.     Set tempEventObject.ObjectManager = pvtVBOFObjectManager
  92.  
  93.     If Not IsMissing(RegisterObject) Then
  94.         Set tempEventObject.RegisterObject = _
  95.             RegisterObject
  96.     End If
  97.  
  98.     If Not IsMissing(RegisterType) Then
  99.         tempEventObject.RegisterType = _
  100.             RegisterType
  101.     End If
  102.  
  103.     If Not IsMissing(TriggerObject) Then
  104.         Set tempEventObject.TriggerObject = _
  105.             TriggerObject
  106.     End If
  107.  
  108.     If Not IsMissing(TriggerObjectType) Then
  109.         tempEventObject.TriggerObjectType = _
  110.             TriggerObjectType
  111.     End If
  112.     
  113.     If Not IsMissing(TriggerEvent) Then
  114.         tempEventObject.TriggerEvent = _
  115.             TriggerEvent
  116.     End If
  117.     
  118. ' for internal re-use
  119.     If Not IsMissing(SkipTriggerObject) Then
  120.         If SkipTriggerObject = True Then    ' doesn't work when And-ed to above line
  121.             Exit Function
  122.         End If
  123.     End If
  124.  
  125.     pvtObjectEvents.Add _
  126.         tempEventObject
  127. #End If
  128.  
  129.     RegisterForObjectEvent = True
  130. End Function
  131.  
  132. Public Function RegisterForCollectionEvent(Optional Collection As Variant, Optional TriggerObject As Variant, Optional TriggerObjectType As Variant, Optional TriggerEvent As Variant, Optional RegisterObject As Variant, Optional RegisterType As Variant) As Boolean
  133. Attribute RegisterForCollectionEvent.VB_Description = "Private"
  134. ' Register the specified RegisterObject or
  135. '   RegisterType to receive notification upon the
  136. '   posting of the specified TriggerEvent by the
  137. '   specified TriggerObject or TriggerObjectType
  138.  
  139. #If NoEventMgr = False Then
  140.     Dim tempNewEventObject As New VBOFEventObject
  141.     
  142.     On Local Error Resume Next
  143.     
  144. ' bullet-proofing
  145.     If IsMissing(RegisterObject) And IsMissing(RegisterType) Then
  146.         pvtErrorMessage TypeName(Me) & " cannot process the '.RegisterForCollectionEvent' method for this object because the 'RegisterObject:=' and the 'RegisterType:=' parameters are missing.  At least one of these must be specified."
  147.         RegisterForCollectionEvent = False
  148.         Exit Function
  149.     End If
  150.  
  151. ' internal re-use
  152.     RegisterForObjectEvent _
  153.         TriggerObject:=TriggerObject, _
  154.         TriggerObjectType:=TriggerObjectType, _
  155.         TriggerEvent:=TriggerEvent, _
  156.         RegisterObject:=RegisterObject, _
  157.         RegisterType:=RegisterType, _
  158.         NewEventObject:=tempNewEventObject, _
  159.         SkipTriggerObject:=True
  160.  
  161.     If Not IsMissing(Collection) Then
  162.         Set tempNewEventObject.Collection = _
  163.             Collection
  164.     End If
  165.  
  166.     pvtCollectionEvents.Add _
  167.         tempNewEventObject
  168. #End If
  169.  
  170.     RegisterForCollectionEvent = True
  171. End Function
  172.  
  173. Public Function TriggerObjectEvent(Optional Event As Variant, Optional Object As Variant, Optional Verbose As Variant) As Boolean
  174. Attribute TriggerObjectEvent.VB_Description = "Private"
  175. ' Trigger the specified Object Event.
  176. ' Post ObjectEventCallBack messages to each of the
  177. '   registered objects
  178.  
  179. #If NoEventMgr = False Then
  180.     Dim tempEventObject As Object
  181.     Dim tempUCaseEvent As String
  182.     
  183.     On Local Error Resume Next
  184.     
  185.     TriggerObjectEvent = True
  186.     tempUCaseEvent = UCase$(Event)
  187.  
  188. ' check for the "Instantated" event, which should
  189. '   be directed towards the new object
  190.     If tempUCaseEvent = "INSTANTIATED" Then
  191.         Object. _
  192.             ObjectEventCallBack _
  193.                 Event:=Event, _
  194.                 Object:=Object
  195.         Exit Function
  196.     End If
  197.     
  198. ' process each of the EventObjects
  199.     For Each tempEventObject In pvtObjectEvents
  200.  
  201. ' if the event pertains to this EventObject
  202.         If tempEventObject. _
  203.             IsRegisteredForEvent( _
  204.                 Event:=Event, _
  205.                 Object:=Object) _
  206.         Then
  207.         
  208. ' notify the registered object.
  209.             tempEventObject. _
  210.                 RegisterObject. _
  211.                 ObjectEventCallBack _
  212.                     Event:=Event, _
  213.                     Object:=Object
  214.  
  215. ' notify of the "missing method" condition
  216.             If Err = pvtReceiverDoesNotSupportThisMethod Then
  217.                 If Not IsMissing(Verbose) Then
  218.                     If Verbose <> False Then
  219.                         pvtErrorMessage "Class Module '" & TypeName(tempEventObject.RegisterObject) & "' does not support the method 'ObjectEventCallBack'." & vbCrLf & "Object cannot be supported by " & TypeName(Me) & " without this method." & vbCrLf & "ObjectType=" & TypeName(tempEventObject.RegisterObject) & ", ObjectID=" & tempEventObject.RegisterObject.ObjectID
  220.                     End If
  221.                 End If
  222.             End If
  223.         End If
  224.     
  225.     Next tempEventObject
  226. #End If
  227.  
  228.     Set tempEventObject = Nothing
  229. End Function
  230.  
  231. Public Function TriggerCollectionEvent(Optional Event As Variant, Optional Object As Variant, Optional Collection As Variant, Optional Verbose As Variant, Optional NoDelete As Variant) As Boolean
  232. Attribute TriggerCollectionEvent.VB_Description = "Private"
  233. ' Trigger the specified Collection Event.
  234. ' Post ObjectEventCallBack messages to each of the
  235. '   registered Collections
  236.  
  237. #If NoEventMgr = False Then
  238.     Dim tempEventObject As Object
  239.     Dim tempNoDelete As Boolean
  240.     Dim I As Long
  241.     
  242.     On Local Error Resume Next
  243.  
  244. ' bullet-proofing
  245. '>>    If TypeName(Object) = "Error" Then
  246. '        Exit Function
  247. '    End If
  248.     tempNoDelete = False
  249.     If Not IsMissing(NoDelete) Then
  250.         tempNoDelete = NoDelete
  251.     End If
  252.  
  253. ' process each of the EventObjects
  254.     I = 1
  255.     For Each tempEventObject In pvtCollectionEvents
  256.  
  257. ' if the event pertains to this EventObject
  258.         If tempEventObject. _
  259.             IsRegisteredForEvent( _
  260.                 Event:=Event, _
  261.                 Object:=Object, _
  262.                 Collection:=Collection, _
  263.                 IsCollectionEvent:=True _
  264.         ) Then
  265.         
  266. ' notify the registered object.
  267.             tempEventObject. _
  268.                 RegisterObject. _
  269.                 ObjectEventCallBack _
  270.                     Event:=Event, _
  271.                     Object:=Object, _
  272.                     NoDelete:=tempNoDelete
  273.  
  274. ' notify of the "missing method" condition
  275.             If Err = pvtReceiverDoesNotSupportThisMethod Then
  276.                 If Not IsMissing(Verbose) Then
  277.                     If Verbose <> False Then
  278.                         pvtErrorMessage "Class Module '" & TypeName(tempEventObject.RegisterObject) & "' does not support the method 'ObjectEventCallBack'." & vbCrLf & "Object cannot be supported by " & TypeName(Me) & " without this method." & vbCrLf & "ObjectType=" & TypeName(tempEventObject.RegisterObject) & ", ObjectID=" & tempEventObject.RegisterObject.ObjectID
  279.                     End If
  280.                 End If
  281.             End If
  282.         End If
  283.     
  284.         I = I + 1
  285.     Next tempEventObject
  286. #End If
  287.  
  288.     Set tempEventObject = Nothing
  289. End Function
  290.  
  291.  
  292.  
  293. Private Function pvtErrorMessage(Optional ErrorMessage As Variant) As Long
  294.     pvtErrorMessage = _
  295.         pvtVBOFObjectManager.DisplayErrorMessage _
  296.             (ErrorMessage)
  297. End Function
  298.  
  299.  
  300. Public Function UnRegisterForCollectionEvent(Optional RegisterObject As Variant, Optional CleanUpMode As Variant) As Boolean
  301. Attribute UnRegisterForCollectionEvent.VB_Description = "Private"
  302. ' UnRegister the specified RegisterObject
  303.  
  304.     Dim tempEventObject As Object
  305.     Dim tempObjectID As Long
  306.     Dim I As Long
  307.     
  308.     On Local Error Resume Next
  309.  
  310.     If Not IsMissing(CleanUpMode) Then
  311.         If CleanUpMode Then
  312.             UnRegisterForCollectionEvent = True
  313.             Exit Function
  314.         End If
  315.     End If
  316.     
  317.     I = 1
  318.     For Each tempEventObject In pvtCollectionEvents
  319.         
  320. ' if the RegisterObject doesn't have an ObjectID,
  321. '   assume it's a Form and force the UnRegister to continue
  322.         tempObjectID = tempEventObject.RegisterObject.ObjectID
  323.         If Err = pvtReceiverDoesNotSupportThisMethod Then
  324.             tempObjectID = RegisterObject.ObjectID
  325.         End If
  326.         
  327.         If tempObjectID = RegisterObject.ObjectID Then
  328.             If TypeName(tempEventObject.RegisterObject) = TypeName(RegisterObject) Then
  329.     
  330.                 pvtCollectionEvents.Remove I
  331.                 Set tempEventObject = Nothing
  332.                 I = I - 1
  333.             End If
  334.         End If
  335.         
  336.         I = I + 1
  337.     Next tempEventObject
  338.  
  339.     UnRegisterForCollectionEvent = True
  340.     Set tempEventObject = Nothing
  341. End Function
  342.  
  343.  
  344. Public Function UnRegisterForObjectEvent(Optional RegisterObject As Variant, Optional CleanUpMode As Variant) As Boolean
  345. Attribute UnRegisterForObjectEvent.VB_Description = "Private"
  346. ' UnRegister the specified RegisterObject
  347.  
  348.     Dim tempEventObject As Object
  349.     Dim tempObjectID As Long
  350.     Dim I As Long
  351.     
  352.     On Local Error Resume Next
  353.     
  354. ' don't bother doing this during "CleanUpMode"
  355.     If Not IsMissing(CleanUpMode) Then
  356.         If CleanUpMode Then
  357.             UnRegisterForObjectEvent = True
  358.             Exit Function
  359.         End If
  360.     End If
  361.  
  362.     I = 1
  363.     For Each tempEventObject In pvtObjectEvents
  364.         
  365. ' if the RegisterObject doesn't have an ObjectID,
  366. '   assume it's a Form and force the UnRegister to continue
  367.         tempObjectID = tempEventObject.RegisterObject.ObjectID
  368.         If Err = pvtReceiverDoesNotSupportThisMethod Then
  369.             tempObjectID = RegisterObject.ObjectID
  370.         End If
  371.         
  372.         If tempObjectID = RegisterObject.ObjectID Then
  373.             If TypeName(tempEventObject.RegisterObject) = TypeName(RegisterObject) Then
  374.                 pvtObjectEvents.Remove I
  375.                 Set tempEventObject = Nothing
  376.                 I = I - 1
  377. '               Exit Function
  378.             End If
  379.         End If
  380.         
  381.         I = I + 1
  382.     Next tempEventObject
  383.  
  384.     UnRegisterForObjectEvent = True
  385.     Set tempEventObject = Nothing
  386. End Function
  387.  
  388.  
  389.  
  390.