SFWidgets.Menu service

A Menu szolgáltatással menük hozhatók létre és távolíthatók el a LibreOffice dokumentumablak menüsorából. Minden egyes menübejegyzés egy parancsfájlhoz vagy egy UNO-parancshoz társítható. Ez a szolgáltatás a következő képességeket biztosítja:

note

Az ezzel a szolgáltatással létrehozott menük csak a megadott dokumentumablakhoz érhetők el. Nem kerülnek mentésre a dokumentumba vagy az alkalmazás beállításai közé. A dokumentum bezárása és megnyitása visszaállítja az alapértelmezett menüsorbeállításokat.


warning

Amikor OLE-objektumokat, például matematikai képleteket vagy Calc-diagramokat szerkeszt a dokumentumon belül, a LibreOffice az objektumnak megfelelően átkonfigurálja a menüsort. Ilyenkor a Menu szolgáltatással létrehozott menük eltávolításra kerülnek, és az OLE-objektum szerkesztése után nem állíthatók vissza.


A szolgáltatás igénybevétele

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

note

• Basic macros require to load ScriptForge library using the following statement:
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")

• Python scripts require an import from scriptforge module:
from scriptforge import CreateScriptService


A Basic nyelvben

A Menu szolgáltatást a Document szolgáltatás CreateMenu metódusának meghívásával lehet létrehozni. Az alábbi kódrészlet létrehoz egy Saját menü nevű menüt az aktuális dokumentumablakban két bejegyzéssel A elem és B elem.


    Sub CreateMenu()
        GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
        Dim oDoc as Object, oMenu as Object
        Set oDoc = CreateScriptService("Document")
        Set oMenu = oDoc.CreateMenu("My Menu")
        With oMenu
            .AddItem("Item A", Command := "About")
            .AddItem("Item B", Script := "vnd.sun.star.script:Standard.Module1.ItemB_Listener?language=Basic&location=application")
            .Dispose()
        End With
    End Sub
  
note

After creating the menu, it is recommended to call the Dispose method to free the resources used by the Menu service instance.


A fenti példában az A elem az .uno:About UNO parancshoz kapcsolódik, míg a B elem az ItemB_Listener parancsfájlhoz, amely a Standard könyvtár Module1 könyvtárában a My Macros tárolóban van definiálva.

The following example defines ItemB_Listener that will be called when Item B is clicked. This listener simply splits the argument string passed to the Sub and shows them in a message box.


    Sub ItemB_Listener(args As String)
        ' Process the argument string passed to the listener
        Dim sArgs as Object
        sArgs = Split(args, ",")
        MsgBox "Menu name: "   & sArgs(0) & Chr(13) & _
               "Menu item: "   & sArgs(1) & Chr(13) & _
               "Item ID: "     & sArgs(2) & Chr(13) & _
               "Item status: " & sArgs(3)
    End Sub
  

As shown in the example above, menu entries associated with a script receive a comma-separated string argument with the following values:

A Python nyelvben

The examples above can be written in Python as follows:


    from scriptforge import CreateScriptService
    
    def create_menu(args=None):
        oDoc = CreateScriptService("Document")
        oMenu = oDoc.CreateMenu("My Menu")
        oMenu.AddItem("Item A", command="About")
        oMenu.AddItem("Item B", script="vnd.sun.star.script:my_macros.py$item_b_listener?language=Python&location=user")
        oMenu.Dispose()
  

    def item_b_listener(args):
        bas = CreateScriptService("Basic")
        s_args = args.split(",")
        msg = f"Menu name: {s_args[0]}\n"
        msg += f"Menu item: {s_args[1]}\n"
        msg += f"Item ID: {s_args[2]}\n"
        msg += f"Item status: {s_args[3]}"
        bas.MsgBox(msg)
  

Tulajdonságok

Név

Írásvédett

Típus

Leírás

ShortcutCharacter

Nem

String

Character used to define the access key of a menu item. The default character is "~".

SubmenuCharacter

Nem

String

Character or string that defines how menu items are nested. The default character is ">".


Menu and Submenus

Almenüvel rendelkező menü létrehozásához a menübejegyzés létrehozásakor használja a SubmenuCharacter tulajdonságban meghatározott karaktert, hogy meghatározza, hova kerüljön. Vegyük például a következő menü/almenü hierarchiát.


    ' Item A
    ' Item B > Item B.1
    '          Item B.2
    ' ------ (line separator)
    ' Item C > Item C.1 > Item C.1.1
    '                     Item C.1.2
    ' Item C > Item C.2 > Item C.2.1
    '                     Item C.2.2
    '                     ------ (line separator)
    '                     Item C.2.3
    '                     Item C.2.4
  

The code below uses the default submenu character ">" to create the menu/submenu hierarchy defined above:


    oMenu.AddItem("Item A")
    oMenu.AddItem("Item B>Item B.1")
    oMenu.AddItem("Item B>Item B.2")
    oMenu.AddItem("---")
    oMenu.AddItem("Item C>Item C.1>Item C.1.1")
    oMenu.AddItem("Item C>Item C.1>Item C.1.2")
    oMenu.AddItem("Item C>Item C.2>Item C.2.1")
    oMenu.AddItem("Item C>Item C.2>Item C.2.2")
    oMenu.AddItem("Item C>Item C.2>---")
    oMenu.AddItem("Item C>Item C.2>Item C.2.3")
    oMenu.AddItem("Item C>Item C.2>Item C.2.4")
  
note

The string --- is used to define separator lines in menus or submenus.


Ikonok használata

Items in the menu can have icons, which are specified as arguments in the AddCheckBox, AddItem and AddRadioButton methods.

A LibreOffice mappában elérhető összes ikon használható, ha megadjuk az ikonfájlok telepítési mappájában található ikonfájlok mappájához viszonyított elérési útjukat. Az ikonok a következő mappában találhatók:

INSTALLDIR/share/config

tip

Use the InstallFolder property of the FileSystem service to determine where LibreOffice is installed in your system.


Ez a mappa egy sor ZIP-fájlt tartalmaz, amelyek az egyes elérhető ikonkészletek képfájljait tartalmazzák. A ZIP-fájlokban lévő képek mappákba vannak rendezve. Egy ikon használatához adja meg az ikonfájlt a ZIP-fájlon belüli helyének elérési útvonalával együtt.

The example below uses the icon "sc_newdoc.svg" that is located inside the "cmd" folder. The forward slash "/" character is used as the path separator regardless of the operating system.

A Basic nyelvben

      myMenu.AddItem("Item A", Icon := "cmd/sc_newdoc.svg")
    
A Python nyelvben

      myMenu.AddItem("Item A", icon="cmd/sc_newdoc.svg")
    
note

All icon sets have the same internal structure. The actual icon displayed depends on the icon set currently in use.


Metódusok

List of Methods in the Menu Service

AddCheckBox

AddItem

AddRadioButton


AddCheckBox

Inserts a check box in the menu. Returns an integer value that identifies the inserted item.

Szintaxis:

svc.AddCheckBox(menuitem: str, opt name: str, opt status: bool, opt icon: str, opt tooltip: str, opt command: str, opt script: str): int

Paraméterek:

menuitem: Defines the text to be displayed in the menu. This argument also defines the hierarchy of the item inside the menu by using the submenu character.

name: String value used to identify the menu item. By default, the last component of the menu hierarchy is used.

status: Defines whether the item is selected when the menu is created (Default = False).

icon: Path and name of the icon to be displayed without the leading path separator. The actual icon shown depends on the icon set being used.

tooltip: Text to be displayed as tooltip.

command: The name of a UNO command without the .uno: prefix. If the command name does not exist, nothing happens.

script: The URI for a Basic or Python script that will be executed when the item is clicked.

note

The arguments command and script are mutually exclusive, hence only one of them can be set for each menu item.


tip

Read Scripting Framework URI Specification to learn more about the URI syntax used in the script argument.


Példa:

A Basic nyelvben

      ' Menu entry associated with the .uno:Paste command
      oMenu.AddCheckBox("Item A", Status := True, ToolTip := "Paste values", Command := "Paste")
      ' Runs the Basic script Standard.Module1.MyListener stored in the document
      oMenu.AddCheckBox("Item B", Status := False, Script := "vnd.sun.star.script:Standard.Module1.MyListener?language=Basic&location=document")
      ' Runs the Python script MyListener located in file myScripts.py in the user scripts folder
      oMenu.AddCheckBox("Item C", Status := True, Script := "vnd.sun.star.script:myScripts.py$MyListener?language=Python&location=user")
    
A Python nyelvben

      oMenu.AddCheckBox("Item A", status=True, tooltip="Paste values", command="Paste")
      oMenu.AddCheckBox("Item B", status=False, script="vnd.sun.star.script:Standard.Module1.MyListener?language=Basic&location=document")
      oMenu.AddCheckBox("Item C", Status=True, Script="vnd.sun.star.script:myScripts.py$MyListener?language=Python&location=user")
    

AddItem

Inserts a label entry in the menu. Returns an integer value that identifies the inserted item.

Szintaxis:

svc.AddItem(menuitem: str, opt name: str, opt icon: str, opt tooltip: str, opt command: str, opt script: str): int

Paraméterek:

menuitem: Defines the text to be displayed in the menu. This argument also defines the hierarchy of the item inside the menu by using the submenu character.

name: String value to be returned when the item is clicked. By default, the last component of the menu hierarchy is used.

icon: Path and name of the icon to be displayed without the leading path separator. The actual icon shown depends on the icon set being used.

tooltip: Text to be displayed as tooltip.

command: The name of a UNO command without the .uno: prefix. If the command name does not exist, nothing happens.

script: The URI for a Basic or Python script that will be executed when the item is clicked.

note

The arguments command and script are mutually exclusive, hence only one of them can be set for each menu item.


tip

Read Scripting Framework URI Specification to learn more about the URI syntax used in the script argument.


Példa:

A Basic nyelvben

      oMenu.AddItem("Item A", Tooltip := "A descriptive message")
    
A Python nyelvben

      oMenu.AddItem("Item A", tooltip = "A descriptive message")
    

AddRadioButton

Inserts a radio button entry in the menu. Returns an integer value that identifies the inserted item.

Szintaxis:

svc.AddRadioButton(menuitem: str, opt name: str, opt status: str, opt icon: str, opt tooltip: str, opt command: str, opt script: str): int

Paraméterek:

menuitem: Defines the text to be displayed in the menu. This argument also defines the hierarchy of the item inside the menu by using the submenu character.

name: String value to be returned when the item is clicked. By default, the last component of the menu hierarchy is used.

status: Defines whether the item is selected when the menu is created (Default = False).

icon: Path and name of the icon to be displayed without the leading path separator. The actual icon shown depends on the icon set being used.

tooltip: Text to be displayed as tooltip.

command: The name of a UNO command without the .uno: prefix. If the command name does not exist, nothing happens.

script: The URI for a Basic or Python script that will be executed when the item is clicked.

note

The arguments command and script are mutually exclusive, hence only one of them can be set for each menu item.


tip

Read Scripting Framework URI Specification to learn more about the URI syntax used in the script argument.


Példa:

A Basic nyelvben

      oMenu.AddRadioButton("Item A", Name := "A", Status := True)
    
A Python nyelvben

      oMenu.AddRadioButton("Item A", name="A", status=True)
    
warning

All ScriptForge Basic routines or identifiers that are prefixed with an underscore character "_" are reserved for internal use. They are not meant be used in Basic macros or Python scripts.


Támogasson minket!