Contenido

# Servicio SFWidgets.ToolbarButton

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.

## Invocación del servicio

Before using the ToolbarButton service the ScriptForge library needs to be loaded or imported:

• Para cargar la biblioteca ScriptForge que necesitan las macros de Basic se debe usar la siguiente declaración:
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
• Los scripts de Python necesitan importar el módulo scriptforge:
from scriptforge import CreateScriptService

The ToolbarButton service is invoked using the ToolbarButtons method from the Toolbar service.

##### En BASIC

The example below retrieves the names of all buttons available in the Standard 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 Print button in the Standard toolbar:

        oDoc = CreateScriptService("Document", ThisComponent)
        oToolbar = oDoc.Toolbars("standardbar")
        oToolbarButton = oToolbar.ToolbarButtons("Print")
        oToolbarButton.Visible = Not oToolbarButton.Visible

El nombre del botón que se pasa como argumento al método ToolbarButtons es el nombre regionalizado del botón definido en el diálogo Herramientas ▸ Personalizar ▸ Barras de herramientas.

Inactive toolbars do not have buttons. Therefore, calling the ToolbarButtons method will make the toolbar visible.

##### En Python

        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.Visible

## Propiedades

Nombre | De solo lectura | Tipo | Descripción
--- | --- | --- | ---
Caption | Sí | String | Devuelve el nombre del botón.
Height | Sí | Long | Devuelve la altura del botón, en píxeles.
Index | Sí | Long | Devuelve el índice del botón en su barra de herramientas contenedora.
OnClick | No | String | The 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.
Parent | Sí | Servicio Toolbar | Returns a Toolbar service instance corresponding to the parent toolbar of the current toolbar button.
TipText | No | String | Specifies the tooltip text shown when the user hovers over the toolbar button.
Visible | No | Boolean | Specifies whether the toolbar button is visible or not.
Width | Sí | Long | Devuelve la anchura del botón, en píxeles.
X | Sí | Long | Returns the X (horizontal) coordinate of the top-left corner of the button, in pixels.
Y | Sí | Long | Returns 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.

##### En BASIC

        Sub OpenPopupMenu()
            GlobalScope.BasicLibraries.LoadLibrary("ScriptForge")
            oDoc = CreateScriptService("Document", ThisComponent)
            oToolbar = oDoc.Toolbars("standardbar")
            oButton = oToolbar.ToolbarButtons("Mi botón")
            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: " & strResponse
        End Sub

##### En Python

        def open_popup_menu(args=None):
            bas = CreateScriptService("Basic")
            doc = CreateScriptService("Document", bas.ThisComponent)
            toolbar = doc.Toolbars("standardbar")
            toolbutton = toolbar.ToolbarButtons("Mi botón")
            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"Su elección: {response}")

Lista de métodos en el servicio ToolbarButton
Execute |  |

## Execute

Ejecuta la orden o secuencia de órdenes asociada con el botón de la barra de herramientas.

Este método devuelve el valor devuelto por la orden o la secuencia que se ejecutó.

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.

### Sintaxis:

svc.Execute(): any

### Ejemplo:

The example below executes the Print button from the Standard toolbar:

##### En BASIC

          oDoc = CreateScriptService("Document", ThisComponent)
          oToolbar = oDoc.Toolbars("standardbar")
          oToolbarButton = oToolbar.ToolbarButtons("Print")
          oToolbarButton.Execute()

##### En Python

          bas = CreateScriptService("Basic")
        doc = CreateScriptService("Document", bas.ThisComponent)
        toolbar = doc.Toolbars("standardbar")
        toolbar_button = toolbar.ToolbarButtons("Print")
        toolbar_button.Execute()

Todas las rutinas o identificadores BASIC de ScriptForge precedidas por guion bajo «_» están reservadas para uso interno. No deben utilizarse en macros BASIC o secuencias Python.

Temas relacionados

Servicio SFWidgets.Toolbar

¡Necesitamos su ayuda!