BASIC: Change toolbar button icon ?

View: New views
7 Messages — Rating Filter:   Alert me  

BASIC: Change toolbar button icon ?

by Jan Holst Jensen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

Kind regards
-- Jan Holst Jensen

Sub SetToolbarButtonVisible(ToolbarURL as String, ButtonLabel as String,
Visibility as Boolean)
  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 = "Label" and
ToolbarButtonProps(J).Value = ButtonLabel Then
        ButtonFound = True
      End If
    Next
   
    If ButtonFound Then
      For J = 0 To UBound(ToolbarButtonProps())
        If ToolbarButtonProps(J).Name = "IsVisible" Then
          ToolbarButtonProps(J).Value = Visibility
        End If
      Next
      ' Write properties back - ToolbarButtonProps() is a local copy(!).
      ToolbarSettings.ReplaceByIndex(I, ToolbarButtonProps)
    End If
   
  Next

  Toolbar.SetSettings( ToolbarSettings )
End Sub


Re: BASIC: Change toolbar button icon ?

by Carsten Driesner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

Regards,
Carsten

REM  *****  BASIC  *****

REM *** This example creates a new basic macro toolbar button on
REM *** the Writer standard bar. It doesn't add the button twice.
REM *** It uses the Writer image manager to set an external image
REM *** for the macro toolbar button.

Sub Main
        REM *** String to reference toolbar
        sToolbar = "private:resource/toolbar/standardbar"
        REM *** Macro command to add
        sMyToolbarCmdId = "macro:///Standard.Module1.Test()"
       
        REM *** Retrieve the module configuration manager from central module
configuration manager supplier
        oModuleCfgMgrSupplier =
createUnoService("com.sun.star.ui.ModuleUIConfigurationManagerSupplier")

        REM *** Retrieve the module configuration manager with module identifier
        REM *** See com.sun.star.frame.ModuleManager for more information
        oModuleCfgMgr = oModuleCfgMgrSupplier.getUIConfigurationManager(
"com.sun.star.text.TextDocument" )
        oImageMgr     = oModuleCfgMgr.getImageManager()
       
        oToolbarSettings = oModuleCfgMgr.getSettings( sToolbar, true )
       
        REM *** Look for our button. Can be identified by the CommandURL property.
        bHasAlreadyButton = false
        nCount = oToolbarSettings.getCount()
        for i = 0 to nCount-1
                oToolbarButton() = oToolbarSettings.getByIndex( i )
                nToolbarButtonCount = ubound(oToolbarButton())
                for j = 0 to nToolbarButtonCount
                        if oToolbarButton(j).Name = "CommandURL" then
                                if oToolbarButton(j).Value = sMyToolbarCmdId then
                                        bHasAlreadyButton = true
                                end if
                        endif
                next j
        next i
       
        Dim oImageCmds(0)
        Dim oImages(0)
        REM *** Check if image has already been added
        if not oImageMgr.hasImage( 0, sMyToolbarCmdId ) then
               
                REM *** Try to load the image from the file URL
                oImage = GetImageFromURL( "file:///c:/test.bmp" )
                if not isNull( oImage ) then
               
                    REM *** Insert new image into the Writer image manager
                    oImageCmds(0) = sMyToolbarCmdId
                    oImages(0) = oImage
                        oImageMgr.insertImages( 0, oImageCmds(), oImages() )
                end if
        end if
       
        if not bHasAlreadyButton then
                sString = "My Macro's"
                oToolbarItem = CreateToolbarItem( sMyToolbarCmdId,
"Standard.Module1.Test" )
                oToolbarSettings.insertByIndex( nCount, oToolbarItem )
                oModuleCfgMgr.replaceSettings( sToolbar, oToolbarSettings )
        end if
End Sub

Function GetImageFromURL( URL as String ) as Variant
     Dim oMediaProperties(0) as new com.sun.star.beans.PropertyValue

        REM *** Create graphic provider instance to load images from external files
        oGraphicProvider = createUnoService(
"com.sun.star.graphic.GraphicProvider" )

     REM *** Set URL property so graphic provider is able to load the image
     oMediaProperties(0).Name  = "URL"
     oMediaProperties(0).Value = URL

        REM *** Retrieve the com.sun.star.graphic.XGraphic instance
        GetImageFromURL = oGraphicProvider.queryGraphic( oMediaProperties() )
End Function

Function CreateToolbarItem( Command as String, Label as String ) as Variant
        Dim aToolbarItem(3) as new com.sun.star.beans.PropertyValue

        aToolbarItem(0).Name = "CommandURL"
        aToolbarItem(0).Value = Command
        aToolbarItem(1).Name = "Label"
        aToolbarItem(1).Value = Label
        aToolbarItem(2).Name = "Type"
        aToolbarItem(2).Value = 0
        aToolbarItem(3).Name = "Visible"
        aToolbarItem(3).Value = true

    CreateToolbarItem = aToolbarItem()
End Function

Sub Test
        MsgBox "Test"
End Sub

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: BASIC: Change toolbar button icon ?

by Paolo Mantovani :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Carsten,
Just for let you know that almost all your posts in dev@api are labeled
as "important" in my local archive :-)
Thanks for sharing.
Keep it up!!

ciao
Paolo M


Carsten Driesner ha scritto:

> 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.
>
> Regards,
> Carsten
>
> REM  *****  BASIC  *****
>
> REM *** This example creates a new basic macro toolbar button on
> REM *** the Writer standard bar. It doesn't add the button twice.
> REM *** It uses the Writer image manager to set an external image
> REM *** for the macro toolbar button.
>
> Sub Main
>     REM *** String to reference toolbar
>     sToolbar = "private:resource/toolbar/standardbar"
>     REM *** Macro command to add
>     sMyToolbarCmdId = "macro:///Standard.Module1.Test()"
>    
>     REM *** Retrieve the module configuration manager from central
> module configuration manager supplier
>     oModuleCfgMgrSupplier =
> createUnoService("com.sun.star.ui.ModuleUIConfigurationManagerSupplier")
>
>     REM *** Retrieve the module configuration manager with module
> identifier
>     REM *** See com.sun.star.frame.ModuleManager for more information
>     oModuleCfgMgr = oModuleCfgMgrSupplier.getUIConfigurationManager(
> "com.sun.star.text.TextDocument" )
>     oImageMgr     = oModuleCfgMgr.getImageManager()
>    
>     oToolbarSettings = oModuleCfgMgr.getSettings( sToolbar, true )
>    
>     REM *** Look for our button. Can be identified by the CommandURL
> property.
>     bHasAlreadyButton = false
>     nCount = oToolbarSettings.getCount()
>     for i = 0 to nCount-1
>         oToolbarButton() = oToolbarSettings.getByIndex( i )
>         nToolbarButtonCount = ubound(oToolbarButton())
>         for j = 0 to nToolbarButtonCount
>             if oToolbarButton(j).Name = "CommandURL" then
>                 if oToolbarButton(j).Value = sMyToolbarCmdId then
>                     bHasAlreadyButton = true
>                 end if
>             endif
>         next j
>     next i
>    
>     Dim oImageCmds(0)
>     Dim oImages(0)
>     REM *** Check if image has already been added
>     if not oImageMgr.hasImage( 0, sMyToolbarCmdId ) then
>        
>         REM *** Try to load the image from the file URL
>         oImage = GetImageFromURL( "file:///c:/test.bmp" )
>         if not isNull( oImage ) then
>        
>             REM *** Insert new image into the Writer image manager
>             oImageCmds(0) = sMyToolbarCmdId
>             oImages(0) = oImage
>             oImageMgr.insertImages( 0, oImageCmds(), oImages() )
>         end if
>     end if
>    
>     if not bHasAlreadyButton then
>         sString = "My Macro's"
>         oToolbarItem = CreateToolbarItem( sMyToolbarCmdId,
> "Standard.Module1.Test" )
>         oToolbarSettings.insertByIndex( nCount, oToolbarItem )
>         oModuleCfgMgr.replaceSettings( sToolbar, oToolbarSettings )
>     end if
> End Sub
>
> Function GetImageFromURL( URL as String ) as Variant
>     Dim oMediaProperties(0) as new com.sun.star.beans.PropertyValue
>
>     REM *** Create graphic provider instance to load images from
> external files
>     oGraphicProvider = createUnoService(
> "com.sun.star.graphic.GraphicProvider" )
>
>     REM *** Set URL property so graphic provider is able to load the image
>     oMediaProperties(0).Name  = "URL"
>     oMediaProperties(0).Value = URL
>
>     REM *** Retrieve the com.sun.star.graphic.XGraphic instance
>     GetImageFromURL = oGraphicProvider.queryGraphic( oMediaProperties() )
> End Function
>
> Function CreateToolbarItem( Command as String, Label as String ) as Variant
>     Dim aToolbarItem(3) as new com.sun.star.beans.PropertyValue
>
>     aToolbarItem(0).Name = "CommandURL"
>     aToolbarItem(0).Value = Command
>     aToolbarItem(1).Name = "Label"
>     aToolbarItem(1).Value = Label
>     aToolbarItem(2).Name = "Type"
>     aToolbarItem(2).Value = 0
>     aToolbarItem(3).Name = "Visible"
>     aToolbarItem(3).Value = true
>
>       CreateToolbarItem = aToolbarItem()
> End Function
>
> Sub Test
>     MsgBox "Test"
> End Sub
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@...
> For additional commands, e-mail: dev-help@...
>
> .
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: BASIC: Change toolbar button icon ?

by Fernand Vanrie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Carsten,

Thanks for this cool peace of code, very usefull !

But :-)

My button installs fine but the macro behind the button (located in a
document) is not working, i supose there is something wrong with the
commandUIRL

The test() macro is located in a document in a "Toolbarlib" Library, in
a "ToolbarButtonsMod" module

is use sMyToolbarCmdId = "macro:///Toolbarlib.ToolbarButtonsMod.Test()"

any idea ?

Thanks

Fernand


> Hello Carsten,
> Just for let you know that almost all your posts in dev@api are labeled
> as "important" in my local archive :-)
> Thanks for sharing.
> Keep it up!!
>
> ciao
> Paolo M
>
>
> Carsten Driesner ha scritto:
>  
>> 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.
>>
>> Regards,
>> Carsten
>>
>> REM  *****  BASIC  *****
>>
>> REM *** This example creates a new basic macro toolbar button on
>> REM *** the Writer standard bar. It doesn't add the button twice.
>> REM *** It uses the Writer image manager to set an external image
>> REM *** for the macro toolbar button.
>>
>> Sub Main
>>     REM *** String to reference toolbar
>>     sToolbar = "private:resource/toolbar/standardbar"
>>     REM *** Macro command to add
>>     sMyToolbarCmdId = "macro:///Standard.Module1.Test()"
>>    
>>     REM *** Retrieve the module configuration manager from central
>> module configuration manager supplier
>>     oModuleCfgMgrSupplier =
>> createUnoService("com.sun.star.ui.ModuleUIConfigurationManagerSupplier")
>>
>>     REM *** Retrieve the module configuration manager with module
>> identifier
>>     REM *** See com.sun.star.frame.ModuleManager for more information
>>     oModuleCfgMgr = oModuleCfgMgrSupplier.getUIConfigurationManager(
>> "com.sun.star.text.TextDocument" )
>>     oImageMgr     = oModuleCfgMgr.getImageManager()
>>    
>>     oToolbarSettings = oModuleCfgMgr.getSettings( sToolbar, true )
>>    
>>     REM *** Look for our button. Can be identified by the CommandURL
>> property.
>>     bHasAlreadyButton = false
>>     nCount = oToolbarSettings.getCount()
>>     for i = 0 to nCount-1
>>         oToolbarButton() = oToolbarSettings.getByIndex( i )
>>         nToolbarButtonCount = ubound(oToolbarButton())
>>         for j = 0 to nToolbarButtonCount
>>             if oToolbarButton(j).Name = "CommandURL" then
>>                 if oToolbarButton(j).Value = sMyToolbarCmdId then
>>                     bHasAlreadyButton = true
>>                 end if
>>             endif
>>         next j
>>     next i
>>    
>>     Dim oImageCmds(0)
>>     Dim oImages(0)
>>     REM *** Check if image has already been added
>>     if not oImageMgr.hasImage( 0, sMyToolbarCmdId ) then
>>        
>>         REM *** Try to load the image from the file URL
>>         oImage = GetImageFromURL( "file:///c:/test.bmp" )
>>         if not isNull( oImage ) then
>>        
>>             REM *** Insert new image into the Writer image manager
>>             oImageCmds(0) = sMyToolbarCmdId
>>             oImages(0) = oImage
>>             oImageMgr.insertImages( 0, oImageCmds(), oImages() )
>>         end if
>>     end if
>>    
>>     if not bHasAlreadyButton then
>>         sString = "My Macro's"
>>         oToolbarItem = CreateToolbarItem( sMyToolbarCmdId,
>> "Standard.Module1.Test" )
>>         oToolbarSettings.insertByIndex( nCount, oToolbarItem )
>>         oModuleCfgMgr.replaceSettings( sToolbar, oToolbarSettings )
>>     end if
>> End Sub
>>
>> Function GetImageFromURL( URL as String ) as Variant
>>     Dim oMediaProperties(0) as new com.sun.star.beans.PropertyValue
>>
>>     REM *** Create graphic provider instance to load images from
>> external files
>>     oGraphicProvider = createUnoService(
>> "com.sun.star.graphic.GraphicProvider" )
>>
>>     REM *** Set URL property so graphic provider is able to load the image
>>     oMediaProperties(0).Name  = "URL"
>>     oMediaProperties(0).Value = URL
>>
>>     REM *** Retrieve the com.sun.star.graphic.XGraphic instance
>>     GetImageFromURL = oGraphicProvider.queryGraphic( oMediaProperties() )
>> End Function
>>
>> Function CreateToolbarItem( Command as String, Label as String ) as Variant
>>     Dim aToolbarItem(3) as new com.sun.star.beans.PropertyValue
>>
>>     aToolbarItem(0).Name = "CommandURL"
>>     aToolbarItem(0).Value = Command
>>     aToolbarItem(1).Name = "Label"
>>     aToolbarItem(1).Value = Label
>>     aToolbarItem(2).Name = "Type"
>>     aToolbarItem(2).Value = 0
>>     aToolbarItem(3).Name = "Visible"
>>     aToolbarItem(3).Value = true
>>
>>       CreateToolbarItem = aToolbarItem()
>> End Function
>>
>> Sub Test
>>     MsgBox "Test"
>> End Sub
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@...
>> For additional commands, e-mail: dev-help@...
>>
>> .
>>
>>    
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@...
> For additional commands, e-mail: dev-help@...
>  


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: BASIC: Change toolbar button icon ?

by Jan Holst Jensen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: BASIC: Change toolbar button icon ?

by Fernand Vanrie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> Carsten, Jan
found the solution in Jan's post

sMyToolbarCmdId =
"vnd.sun.star.script:Toolbar.ToolbarButtons.Test?language=Basic&location=document"

thanks
fernand

>
> Thanks for this cool peace of code, very usefull !
>
> But :-)
>
> My button installs fine but the macro behind the button (located in a
> document) is not working, i supose there is something wrong with the
> commandUIRL
>
> The test() macro is located in a document in a "Toolbarlib" Library,
> in a "ToolbarButtonsMod" module
>
> is use sMyToolbarCmdId = "macro:///Toolbarlib.ToolbarButtonsMod.Test()"
> any idea ?
>
> Thanks
>
> Fernand
>
>
>> Hello Carsten,
>> Just for let you know that almost all your posts in dev@api are labeled
>> as "important" in my local archive :-)
>> Thanks for sharing.
>> Keep it up!!
>>
>> ciao
>> Paolo M
>>
>>
>> Carsten Driesner ha scritto:
>>  
>>> 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.
>>>
>>> Regards,
>>> Carsten
>>>
>>> REM  *****  BASIC  *****
>>>
>>> REM *** This example creates a new basic macro toolbar button on
>>> REM *** the Writer standard bar. It doesn't add the button twice.
>>> REM *** It uses the Writer image manager to set an external image
>>> REM *** for the macro toolbar button.
>>>
>>> Sub Main
>>>     REM *** String to reference toolbar
>>>     sToolbar = "private:resource/toolbar/standardbar"
>>>     REM *** Macro command to add
>>>     sMyToolbarCmdId = "macro:///Standard.Module1.Test()"
>>>         REM *** Retrieve the module configuration manager from central
>>> module configuration manager supplier
>>>     oModuleCfgMgrSupplier =
>>> createUnoService("com.sun.star.ui.ModuleUIConfigurationManagerSupplier")
>>>
>>>
>>>     REM *** Retrieve the module configuration manager with module
>>> identifier
>>>     REM *** See com.sun.star.frame.ModuleManager for more information
>>>     oModuleCfgMgr = oModuleCfgMgrSupplier.getUIConfigurationManager(
>>> "com.sun.star.text.TextDocument" )
>>>     oImageMgr     = oModuleCfgMgr.getImageManager()
>>>         oToolbarSettings = oModuleCfgMgr.getSettings( sToolbar, true )
>>>         REM *** Look for our button. Can be identified by the
>>> CommandURL
>>> property.
>>>     bHasAlreadyButton = false
>>>     nCount = oToolbarSettings.getCount()
>>>     for i = 0 to nCount-1
>>>         oToolbarButton() = oToolbarSettings.getByIndex( i )
>>>         nToolbarButtonCount = ubound(oToolbarButton())
>>>         for j = 0 to nToolbarButtonCount
>>>             if oToolbarButton(j).Name = "CommandURL" then
>>>                 if oToolbarButton(j).Value = sMyToolbarCmdId then
>>>                     bHasAlreadyButton = true
>>>                 end if
>>>             endif
>>>         next j
>>>     next i
>>>         Dim oImageCmds(0)
>>>     Dim oImages(0)
>>>     REM *** Check if image has already been added
>>>     if not oImageMgr.hasImage( 0, sMyToolbarCmdId ) then
>>>                REM *** Try to load the image from the file URL
>>>         oImage = GetImageFromURL( "file:///c:/test.bmp" )
>>>         if not isNull( oImage ) then
>>>                    REM *** Insert new image into the Writer image
>>> manager
>>>             oImageCmds(0) = sMyToolbarCmdId
>>>             oImages(0) = oImage
>>>             oImageMgr.insertImages( 0, oImageCmds(), oImages() )
>>>         end if
>>>     end if
>>>         if not bHasAlreadyButton then
>>>         sString = "My Macro's"
>>>         oToolbarItem = CreateToolbarItem( sMyToolbarCmdId,
>>> "Standard.Module1.Test" )
>>>         oToolbarSettings.insertByIndex( nCount, oToolbarItem )
>>>         oModuleCfgMgr.replaceSettings( sToolbar, oToolbarSettings )
>>>     end if
>>> End Sub
>>>
>>> Function GetImageFromURL( URL as String ) as Variant
>>>     Dim oMediaProperties(0) as new com.sun.star.beans.PropertyValue
>>>
>>>     REM *** Create graphic provider instance to load images from
>>> external files
>>>     oGraphicProvider = createUnoService(
>>> "com.sun.star.graphic.GraphicProvider" )
>>>
>>>     REM *** Set URL property so graphic provider is able to load the
>>> image
>>>     oMediaProperties(0).Name  = "URL"
>>>     oMediaProperties(0).Value = URL
>>>
>>>     REM *** Retrieve the com.sun.star.graphic.XGraphic instance
>>>     GetImageFromURL = oGraphicProvider.queryGraphic(
>>> oMediaProperties() )
>>> End Function
>>>
>>> Function CreateToolbarItem( Command as String, Label as String ) as
>>> Variant
>>>     Dim aToolbarItem(3) as new com.sun.star.beans.PropertyValue
>>>
>>>     aToolbarItem(0).Name = "CommandURL"
>>>     aToolbarItem(0).Value = Command
>>>     aToolbarItem(1).Name = "Label"
>>>     aToolbarItem(1).Value = Label
>>>     aToolbarItem(2).Name = "Type"
>>>     aToolbarItem(2).Value = 0
>>>     aToolbarItem(3).Name = "Visible"
>>>     aToolbarItem(3).Value = true
>>>
>>>       CreateToolbarItem = aToolbarItem()
>>> End Function
>>>
>>> Sub Test
>>>     MsgBox "Test"
>>> End Sub
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@...
>>> For additional commands, e-mail: dev-help@...
>>>
>>> .
>>>
>>>    
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@...
>> For additional commands, e-mail: dev-help@...
>>  
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: BASIC: Change toolbar button icon ?

by Carsten Driesner-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jan Holst Jensen schrieb:

> 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.
Hi Jan,

Yes, that's right. OpenOffice.org associates command URL and icon. The
idea behind this:  A command which can reside in a menu, toolbar  or
context menu should always be represented by the same icon. As the
command URL reflects the action behind a user interface element it's
logical to use it as the primary key to accessthe icon.
>
> 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.
The code you attached to this mail looks much better now. Normally the
state of a command in OpenOffice.org is defined by a part of  the
controller implementation. It can use a
com.sun.star.frame.FeatureStateEvent to update the state of a command.
There you can set the state of a command to on or off. For example the
Gallery button on the standard bar has a on/off state. Implementing a
controller (Dispatch Provider) is, as far as I know, not possible with
Basic. Therefore your solution is the best way and also much easier.

Regards,
Carsten

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...