How can I change a macro button's color dynamically?

In Calc, I write a macro to set the current cell’s format to text,
so any number inside will not be interpreted as date or somewhat.
Then I add a button which binds to this macro to toolbar,
and I use it like format as currency or other built-in formatting button.

But these built-in buttons’ color will change if the current cell is that kind of format.
How can I change my macro button’s color like that?

Following is my macro code:

REM bound as vnd.sun.star.script:Standard.Module1.format_as_text?language=Basic&location=application
sub format_as_text
	dim oDoc
	dim oFormats
	dim oRange
	oDoc = ThisComponent
	oRange = oDoc.CurrentSelection
	oFormats = oDoc.NumberFormats
	
	Dim aLocale As New com.sun.star.lang.Locale
	oRange.NumberFormat = oFormats.getStandardFormat(_
		com.sun.star.util.NumberFormat.TEXT, aLocale)
end sub

I don’t think it’s possible. If it does work, please let us know how.

I think i found a solution:

sub format_as_text
	dim oDoc
	dim oFormats
	dim oRange
	oDoc = ThisComponent
	oRange = oDoc.CurrentSelection
	oFormats = oDoc.NumberFormats
	Dim aLocale As New com.sun.star.lang.Locale
	oRange.NumberFormat = oFormats.getStandardFormat(	com.sun.star.util.NumberFormat.TEXT, aLocale)
end sub


sub highlight_unhighlight_Icon (event)
	dim oDoc
	dim oFormats
	dim oRange
	oDoc = ThisComponent
	oFormats = oDoc.NumberFormats
    Dim aLocale As New com.sun.star.lang.Locale
	if event.NumberFormat = oFormats.getStandardFormat(com.sun.star.util.NumberFormat.TEXT, aLocale) then
        S_get_Icon
    endif
end sub


Sub S_get_Icon
	on error goto errorhandler
	odoc 	= thiscomponent
	oframe 	= odoc.CurrentController.Frame
   	olayout 	= oframe.LayoutManager
    stoolbar 	= 	"private:resource/toolbar/formatobjectbar"
    atoolbar = oLayout.getElement(stoolbar)
    oAcc =  aToolbar.realinterface.AccessibleContext
    for i = 0 to oAcc.AccessibleChildCount- 1
         ochild = oAcc.getAccessibleChild(i)
         if oChild.AccessibleName= "format_as_text" then
             oChild.grabFocus
          endif
    next i 
    errorhandler:
End Sub

find attached an example
NF.ods (11.2 KB)
The macro highlight_unhighlight_Icon is bound to the sheetevent Selection changed

3 Likes

Nice work! but there are some little differences between
the highlight of *.grabFocus and built-in’s format highlight.

The grabFocus method cause the toolbar button little lighter than the built-in format highlight.
The grabFocus will switch to highlight other toolbar button
if user hover the mouse pointer on the other button,
and the highlight will not fade out the last hovered button after the mouse left the toolbar.

Would you like to share where can I find the docs about the grabFocus method
and other docs about the toolbar? Maybe there are more APIs suit this situation.

For me, it looks as you where a Korinthenkacker

1 Like

Because the differences seem to indicate that the grabFocus method is not expected to use in this case.

https://api.libreoffice.org/

1 Like

@gholk full control of toolbar is more complicated Toolbar with functional ComboBox, DropdownBox etc

1 Like