Carsten Driesner wrote:
Jan Holst Jensen wrote:
Hi all.

I am trying to emulate a togglebutton/checkbox in a custom toolbar. Right now I have settled for having two toolbar buttons with different icons. Only one of them is visible and when it's pressed it toggles the visibility of both buttons using the code below (which sets visibility of a single button with a given label).

It works OK but only as long as the user doesn't fiddle with button visibility :-). Is there a way of changing a toolbar button's icon directly from Basic code ? I could not figure out how to do it.
Hi Jan,

Your solution looks it a little bit strange. You can set the image of a toolbar button with the help of an image manager. Look at the following Basic code which uses the image manager to set an image for a button that references a Basic macro. I am sure you can adapt the code to your needs.

Hi Carsten.

Thanks for this. I was looking for something resembling Excel VBA where you just change the .FaceId property of a toolbar button which I can now see is definitely not the way to do it in OpenOffice.

As far as I can understand icons are instead associated with a command URL and not the user interface element. So if I change the command URL of a toolbar button the icon changes with it. This actually makes good sense for my usage scenario.

I have a toolbar button that is used to turn a feature on or off. When the button is pressed to turn the feature ON I change its command URL to point to the Sub that turns the feature OFF. This also changes the icon so the end user can see the current state of the feature (is it ON or OFF ?). As illustrated by the code below.

Cheers
-- Jan


' The Toolbar button with label "ToggleStuff" is initially set to point to
' the ToggleStuffOn macro and the icon for that macro URL shows that the feature is off. ' The icon associated with the ToggleStuffOff macro will show that the feature is on.

Sub ToggleStuffOn
ChangeToolbarButtonCommandURL("private:resource/toolbar/custom_toolbar_1a0e", "vnd.sun.star.script:Standard.Test.ToggleStuffOn?language=Basic&location=document", "vnd.sun.star.script:Standard.Test.ToggleStuffOff?language=Basic&location=document")
End Sub

Sub ToggleStuffOff
ChangeToolbarButtonCommandURL("private:resource/toolbar/custom_toolbar_1a0e", "vnd.sun.star.script:Standard.Test.ToggleStuffOff?language=Basic&location=document", "vnd.sun.star.script:Standard.Test.ToggleStuffOn?language=Basic&location=document")
End Sub

Sub ChangeToolbarButtonCommandURL(ToolbarURL as String, OldURL as String, NewURL as String)
 Dim LayoutMgr as Variant
 Dim Toolbar as Variant
 Dim ToolbarSettings as Variant

 LayoutMgr = ThisComponent.GetCurrentController().GetFrame().LayoutManager
 Toolbar = LayoutMgr.GetElement(ToolbarURL)
 Toolbar.Persistent = False
 ToolbarSettings = Toolbar.GetSettings(True)

 Dim I, J as Integer
 Dim ButtonCount as Integer
 Dim ButtonFound as Boolean

 ButtonCount = ToolbarSettings.GetCount()
 For I = 0 To ButtonCount - 1
   ButtonFound = False

   Dim ToolbarButtonProps()
   ToolbarButtonProps() = ToolbarSettings.GetByIndex(I)
   For J = 0 To UBound(ToolbarButtonProps())
If ToolbarButtonProps(J).Name = "CommandURL" and ToolbarButtonProps(J).Value = OldURL Then
       ButtonFound = True
     End If
   Next
If ButtonFound Then
     For J = 0 To UBound(ToolbarButtonProps())
       If ToolbarButtonProps(J).Name = "CommandURL" Then
         ToolbarButtonProps(J).Value = NewURL
       End If
     Next
     ' Write properties back - ToolbarButtonProps() is a local copy(!).
     ToolbarSettings.ReplaceByIndex(I, ToolbarButtonProps)
   End If
Next

 Toolbar.SetSettings( ToolbarSettings )
End Sub



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@api.openoffice.org
For additional commands, e-mail: dev-h...@api.openoffice.org

Reply via email to