OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Detecting the state of dispatches

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1569
Location: Frankfurt, Germany

PostPosted: Sun Nov 07, 2004 12:10 am    Post subject: Detecting the state of dispatches Reply with quote

Sometimes triggering and switching things were tidious to do and were seen as almost unsolveable with Starbasic. By this I mean hiding toolbars when they're visible or switching the control design mode for Impress/Dawing component.
Those things had one drawback: They didn't have any parameters that we could pass to set the behavior to a specific state. The problem always was: When we make the dispatch the object gets switched to the opposite state it was before.
I have found a way around this with the XStatusListener.
Here is the example code that can detect if the DesignMode is currently on in a document (I wish this would be possible with the API not with a dispatch, but it's not possible currently so only this dispatch mechanism will work). The http://www.oooforum.org/forum/viewtopic.php?t=8030 thread gives you the development of the discussion how to detect the designmode.

Code:

Sub setDesignModeOn( ) 'sets the DesignMode in the current Document ON
RequestCurrentStatus(ThisComponent.getCurrentController().Frame, ".uno:SwitchControlDesignMode") 'you can pass any Dispatchobj that has a boolean state (good alternatives you can find here: http://www.oooforum.org/forum/viewtopic.php?t=6890)

'also set DesignMode ON for the next time the document is opened!
Stardesktop.getCurrentComponent().ApplyFormDesignMode = True
   'msgbox "Macro executed" '[dbg]
End Sub

'~~~~~~~~~
'~~~~~~~~~

REM This function is only called when the DesignMode is off
Sub SwitchDesignMode( )
Dim oFrame, dispatcher
Dim noArgs1()

oFrame = ThisComponent.getCurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")   
dispatcher.executeDispatch(oFrame, ".uno:SwitchControlDesignMode", "", 0, noArgs1())

End Sub

'~~~~~~~~~
'~~~~~~~~~

Sub RequestCurrentStatus(frame As Object, DispatchcmdName as String)
  Dim oParser
  Dim oUrl as New com.sun.star.util.URL
 
  oUrl.Complete = DispatchcmdName

  REM Parse the URL as required
  oParser = createUnoService("com.sun.star.util.URLTransformer")
  oParser.parseStrict(oUrl)
  repository(0, oUrl) 'dump it into the repository!

  REM See if the current Frame supports this UNO command
  oDisp = frame.queryDispatch(oUrl,"",0)
  repository(1, oDisp)
   
  If (Not IsNull(oDisp)) Then
      CreateStatusListener(oDisp, oUrl)
  End If   
 
End Sub

'~~~~~~~~~
'~~~~~~~~~

Function CreateStatusListener(oDisp ,oUrl)
statusListener = CreateUnoListener("Status_", "com.sun.star.frame.XStatusListener")
repository(2, statusListener)

oDisp.addStatusListener(statusListener, oURL)

End Function

'~~~~~~~~~
'~~~~~~~~~

Function Status_disposing(oEvt) 'we need this, although it's useless!
End Function

'~~~~~~~~~
'~~~~~~~~~

Function Status_statusChanged(oEvt)
If oEvt.State = False Then
   removeStatusListener(true)
End If   
removeStatusListener(false)
End Function

'~~~~~~~~~
'~~~~~~~~~

Function removeStatusListener(bTrigger) 'bTrigger = true when we need to trigger the DesignMode to on (because it is off)

oUrl = repository(0)
oDisp = repository(1)
statusListener = repository(2)

oDisp.removeStatusListener(statusListener, oURL)
If bTrigger Then
   SwitchDesignMode()
End If   
End Function

'~~~~~~~~~
'~~~~~~~~~

Function repository(where as Integer, Optional oURL) 'use this function so we don't need too many global variables!
Static returnObj(5) as new com.sun.star.beans.PropertyValue

If Not IsMissing(oUrl) Then
   returnObj(where).Value = oURL
End If
   
repository = returnObj(where).Value
End Function


For other dispatches for which you can make use of with this example that you can adjust to your situation, please download the slots.sxc in the "download files" section of http://api.openoffice.org

I appreciate any kind of feedback (I kind of posted this in a hurry so maybe something doesn't work as quite expected or I told something wrong).
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1569
Location: Frankfurt, Germany

PostPosted: Fri Nov 19, 2004 9:43 pm    Post subject: Reply with quote

As a notice:
Don't use the example given when loading a document hidden.
As stated by many people before many uno dispatches won't work for documents that are loaded hidden, that is because not all uno services are instantiated properly.
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group