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-unsubscr...@api.openoffice.org
For additional commands, e-mail: dev-h...@api.openoffice.org

Reply via email to