SFWidgets.ToolbarButton service/text/sbasic/shared/03/sf_toolbarbutton.xhpToolbarButton service
SFWidgets.ToolbarButton service
The ToolbarButton service allows to retrieve information related to the toolbar buttons available in a given toolbar. With this service it is possible to:Toggle the visibility of toolbar elements.Execute the command associated with a given toolbar button.
Service invocation
Before using the ToolbarButton service the ScriptForge library needs to be loaded or imported:The ToolbarButton service is invoked using the ToolbarButtons method from the Toolbar service.The example below retrieves the names of all buttons available in the toolbar.oDoc = CreateScriptService("Document", ThisComponent)oToolbar = oDoc.Toolbars("standardbar")arrToolbarButtons = oToolbar.ToolbarButtons()MsgBox SF_String.Represent(arrToolbarButtons)Use the ToolbarButtons method without arguments to retrieve an array with all available toolbar button names.The example below toggles the visibility of the button in the toolbar:oDoc = CreateScriptService("Document", ThisComponent)oToolbar = oDoc.Toolbars("standardbar")oToolbarButton = oToolbar.ToolbarButtons("Print")oToolbarButton.Visible = Not oToolbarButton.VisibleThe button name passed as argument to the ToolbarButtons method is the localized button name defined in the dialog.Inactive toolbars do not have buttons. Therefore, calling the ToolbarButtons method will make the toolbar visible.bas = CreateScriptService("Basic")doc = CreateScriptService("Document", bas.ThisComponent)toolbar = doc.Toolbars("standardbar")arr_toolbar_buttons = toolbar.ToolbarButtons()bas.MsgBox(repr(arr_toolbar_buttons))bas = CreateScriptService("Basic")doc = CreateScriptService("Document", bas.ThisComponent)toolbar = doc.Toolbars("standardbar")toolbar_button = toolbar.ToolbarButtons("Print")toolbar_button.Visible = not toolbar_button.VisibleToolbarButton service;Caption propertyToolbarButton service;Height propertyToolbarButton service;Index propertyToolbarButton service;OnClick propertyToolbarButton service;Parent propertyToolbarButton service;TipText propertyToolbarButton service;Visible propertyToolbarButton service;Width propertyToolbarButton service;X propertyToolbarButton service;Y property
Properties
NameReadonlyTypeDescriptionCaptionYesStringReturns the name of the button.HeightYesLongReturns the height of the button, in pixels.IndexYesLongReturns the index of the button in its parent toolbar.OnClickNoStringThe UNO command or script executed when the button is pressed. Read the Wiki page Scripting Framework URI Specification to learn more on how to define a URI string.ParentYesToolbar serviceReturns a Toolbar service instance corresponding to the parent toolbar of the current toolbar button.TipTextNoStringSpecifies the tooltip text shown when the user hovers over the toolbar button.VisibleNoBooleanSpecifies whether the toolbar button is visible or not.WidthYesLongReturns the width of the button, in pixels.XYesLongReturns the X (horizontal) coordinate of the top-left corner of the button, in pixels.YYesLongReturns the Y (vertical) coordinate of the top-left corner of the button, in pixels.
Use of ToolbarButton alongside the PopupMenu service
A common use case of the properties X and Y described above is to open a popup menu in the position where the toolbar button is located.Suppose you create the script below and associate it with a button named "My Button" in the standardbar. When it is clicked, a popup menu will be shown with 3 options for the user to select.Sub OpenPopupMenu() GlobalScope.BasicLibraries.LoadLibrary("ScriptForge") oDoc = CreateScriptService("Document", ThisComponent) oToolbar = oDoc.Toolbars("standardbar") oButton = oToolbar.ToolbarButtons("My Button") oPopup = CreateScriptService("SFWidgets.PopupMenu", , oButton.X, oButton.Y + oButton.Height) oPopup.AddItem("Item A", "A") oPopup.AddItem("Item B", "B") oPopup.AddItem("Item C", "C") strResponse = oPopup.Execute(False) MsgBox "Your choice: " & strResponseEnd Subdef open_popup_menu(args=None): bas = CreateScriptService("Basic") doc = CreateScriptService("Document", bas.ThisComponent) toolbar = doc.Toolbars("standardbar") toolbutton = toolbar.ToolbarButtons("My Button") popup = CreateScriptService("PopupMenu", None, toolbutton.X, toolbutton.Y + toolbutton.Height) popup.AddItem("Item A", "A") popup.AddItem("Item B", "B") popup.AddItem("Item C", "C") response = popup.Execute(False) bas.MsgBox(f"Your choice: {response}")
List of Methods in the ToolbarButton Service
Execute
Executes the command or script associated with the toolbar button.This method returns the value returned by the command or script executed.Use the OnClick property to determine the command or script that shall be executed. If the command/script does not return any value, then Null is returned.
svc.Execute(): any
The example below executes the button from the toolbar:oDoc = CreateScriptService("Document", ThisComponent)oToolbar = oDoc.Toolbars("standardbar")oToolbarButton = oToolbar.ToolbarButtons("Print")oToolbarButton.Execute()bas = CreateScriptService("Basic")doc = CreateScriptService("Document", bas.ThisComponent)toolbar = doc.Toolbars("standardbar")toolbar_button = toolbar.ToolbarButtons("Print")toolbar_button.Execute()