Be very careful while writing listeners and intercepting dispatches. Assume that something will change, or that you did something wrong and that OOo will crash; you have been warned.
Global oDispatchInterceptor
Global oSlaveDispatchProvider
Global oMasterDispatchProvider
Global oFrame
Global bDebug As Boolean
Dim oCopyDispatch
Sub RegisterInterceptor
oFrame = ThisComponent.currentController.Frame
oDispatchInterceptor = CreateUnoListener("ThisFrame_", _
"com.sun.star.frame.XDispatchProviderInterceptor")
oFrame.registerDispatchProviderInterceptor(oDispatchInterceptor)
End Sub
Sub ReleaseInterceptor()
On Error Resume Next
oFrame.releaseDispatchProviderInterceptor(oDispatchInterceptor)
End Sub
Function ThisFrame_queryDispatch ( oUrl, _
sTargetFrameName As String, lSearchFlags As Long ) As Variant
302
Dim oDisp
Dim sUrl As String
'slot protocol causes OOo crash...
if oUrl.Protocol = "slot:" Then
Exit Function
End If
If bDebug Then
Print oUrl.complete
End If
'do your dispatch management here:
Select Case oUrl.complete
'Case ".uno:Copy"
'oDisp = GetCopyDispatch 'replace the original dispatch
'Case ".uno:Paste"
'oDisp = GetCopyDispatch 'replace the original dispatch
'Case ".uno:Save"
'oDisp = GetCopyDispatch 'replace the original dispatch
'Case ".uno:Undo"
'oDisp = GetCopyDispatch 'replace the original dispatch
'Case ".uno:blabla"
'do something
Case ".uno:Cut"
oDisp = GetCopyDispatch 'replace the original dispatch
Case Else
oDisp = _
oSlaveDispatchProvider.queryDispatch( oUrl, sTargetFrameName, lSearchFlags )
End Select
ThisFrame_queryDispatch = oDisp
End Function
Function ThisFrame_queryDispatches ( mDispatches ) As Variant
'ThisFrame_queryDispatches = mDispatches()
End Function
Function ThisFrame_getSlaveDispatchProvider ( ) As Variant
ThisFrame_getSlaveDispatchProvider = oSlaveDispatchProvider
End Function
Sub ThisFrame_setSlaveDispatchProvider ( oSDP )
oSlaveDispatchProvider = oSDP
End Sub
Function ThisFrame_getMasterDispatchProvider ( ) As Variant
ThisFrame_getMasterDispatchProvider = oMasterDispatchProvider
End Function
Sub ThisFrame_setMasterDispatchProvider ( oMDP )
303
oMasterDispatchProvider = oMDP
End Sub
Sub toggleDebug
bDebug = Not bDebug
End Sub
Function GetCopyDispatch()
If Not IsNull(oCopyDispatch) Then
oCopyDispatch = _
CreateUnoListener("MyCustom_", "com.sun.star.frame.XDispatch")
End If
GetCopyDispatch = oCopyDispatch
End Function
Sub MyCustom_dispatch(URL, Arguments)
Select Case URL.complete
Case ".uno:Cut"
MsgBox "cut"
'Case ".uno:Copy"
'MsgBox "Sorry, the original " & URL.complete & _
'" dispatch was stolen from Paolo M.", 48
'Case ".uno:Paste"
'ThisComponent.CurrentSelection(0).String = _
'"**** ARBITRARY CLIPBOARD CONTENT PROVIDED BY PAOLO M. ****"
'Case ".uno:Save"
'MsgBox "Sorry, the original " & URL.complete & _
'" dispatch was stolen from Paolo M.", 48
'Case ".uno:Undo"
'MsgBox "Undo What????!!!???", 16
'Case Else
End Select
End Sub
Sub MyCustom_addStatusListener(Control, URL)
End sub
Option Explicit
global oXKeyHandler as object
sub sStartXKeyHandler
if isNull(oXKeyHandler) then 'only if not jet running
oXKeyHandler = CreateUnoListener("KeyHandler_", "com.sun.star.awt.XKeyHandler")
ThisComponent.CurrentController.AddKeyHandler(oXKeyHandler)
endif
end sub
sub sStopXKeyHandler
if not IsNull(oXKeyHandler) then 'only if still running
ThisComponent.CurrentController.removeKeyHandler(oXKeyHandler)
oXKeyHandler = Nothing 'To know later this handler has stopped.
end if
end sub
sub XKeyHandler_Disposing(oEvent)
end sub
function KeyHandler_KeyPressed(oEvent) as boolean
' MsgBox(oEvent.KeyCode)
End function
function KeyHandler_KeyReleased(oEvent) as boolean
KeyHandler_KeyReleased = False 'cancel KeyReleased
if oevent.modifiers = 2 and oevent.keycode =535 then
msgbox "cut"
end if
end function
Option Explicit
global oXKeyHandler as object
sub sStartXKeyHandler
if isNull(oXKeyHandler) then 'only if not jet running
oXKeyHandler = CreateUnoListener("KeyHandler_", "com.sun.star.awt.XKeyHandler")
ThisComponent.CurrentController.AddKeyHandler(oXKeyHandler)
endif
end sub
sub sStopXKeyHandler
if not IsNull(oXKeyHandler) then 'only if still running
ThisComponent.CurrentController.removeKeyHandler(oXKeyHandler)
oXKeyHandler = Nothing 'To know later this handler has stopped.
end if
end sub
sub XKeyHandler_Disposing(oEvent)
end sub
function KeyHandler_KeyPressed(oEvent) as boolean
KeyHandler_KeyPressed = False 'cancel KeyPressed
End function
function KeyHandler_KeyReleased(oEvent) as boolean
KeyHandler_KeyReleased = False 'cancel KeyReleased
if oevent.keyfunc = 8 then 'cut function called
msgbox "cut"
end if
end function
Sub addListener()
oModifyListener = createUnoListener("my_", "com.sun.star.util.XModifyListener")
ThisComponent.CurrentController.ActiveSheet.addModifyListener(oModifyListener)
End Sub
Sub my_modified(e)
msgbox "Yep"
End Sub
Sub my_disposing(e)
REM required dummy routine called by
REM parent interface c.s.s.lang.XEventListener
End Sub
Sub my_modified(e)
oMRI = CreateUnoService( "mytools.Mri" )
oMRI.inspect(e)
End Sub
Villeroy wrote:The Source element is always the object which got the listener attached.
Users browsing this forum: No registered users and 2 guests