ScriptForge (SFWidgets) new ContextMenu service

Complete a predefined context menu with new items
placed at its bottom.

A context menu is obtained by a right-click
on several areas of a document. Each area has
its own context menu.
Each component model has its own set of context menus.

A context menu is usually predefined at
LibreOffice installation. Customization is done
statically with the Tools + Customize dialog.
The actual new service provides a mean
to make temporary additions at the bottom of a
context menu. Those changes are lost when
the document is closed.

The name of a context menu is the last component
of the resource URL:
   "private:resource/popupmenu/the-name-here"

Context menu items are either usual items or
line separators. Checkboxes or radio buttons
are not supported.
Items run a command or a script when clicked.

The service implements 2 methods:
   AddItem() adds an entry in the menu hierarchy
   Activate() shows or hides the added entries

A context menu can be defined from both Basic
and Python user scripts.

An update of the documentation is required.

Change-Id: Id77f1f2565d75e36c09b13972330d0f83b3f1db4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/172355
Reviewed-by: Jean-Pierre Ledure <jp@ledure.be>
Tested-by: Jenkins
This commit is contained in:
Jean-Pierre Ledure 2024-08-24 15:58:34 +02:00
parent a241f2d96d
commit 8a4ee0fb39
18 changed files with 971 additions and 37 deletions

View file

@ -22,6 +22,7 @@ $(eval $(call gb_Package_Package,wizards_basicsrvsfwidgets,$(SRCDIR)/wizards/sou
$(eval $(call gb_Package_add_files,wizards_basicsrvsfwidgets,$(LIBO_SHARE_FOLDER)/basic/SFWidgets,\
SF_Menu.xba \
SF_MenuListener.xba \
SF_ContextMenu.xba \
SF_PopupMenu.xba \
SF_Register.xba \
SF_Toolbar.xba \

View file

@ -784,12 +784,14 @@ Try:
If Script = &quot;SetTableData&quot; Then vReturn = vBasicObject.SetTableData(vArgs(0), vArgs(1), vArgs(2))
Case &quot;SFDocuments.Document&quot;
Select Case Script
Case &quot;ContextMenus&quot; : vReturn = vBasicObject.ContextMenus(vArgs(0), vArgs(1))
Case &quot;Forms&quot; : vReturn = vBasicObject.Forms(vArgs(0))
Case &quot;Styles&quot; : vReturn = vBasicObject.Styles(vArgs(0), vArgs(1), vArgs(2), vArgs(3), vArgs(4), vArgs(5))
Case &quot;Toolbars&quot; : vReturn = vBasicObject.Toolbars(vArgs(0))
End Select
Case &quot;SFDocuments.Base&quot;
Select Case Script
Case &quot;ContextMenus&quot; : vReturn = vBasicObject.ContextMenus(vArgs(0), vArgs(1))
Case &quot;FormDocuments&quot; : vReturn = vBasicObject.FormDocuments()
Case &quot;Forms&quot; : vReturn = vBasicObject.Forms(vArgs(0), vArgs(1))
Case &quot;Toolbars&quot; : vReturn = vBasicObject.Toolbars(vArgs(0))
@ -797,6 +799,7 @@ Try:
Case &quot;SFDocuments.Calc&quot;
Select Case Script
Case &quot;Charts&quot; : vReturn = vBasicObject.Charts(vArgs(0), vArgs(1))
Case &quot;ContextMenus&quot; : vReturn = vBasicObject.ContextMenus(vArgs(0), vArgs(1))
Case &quot;Forms&quot; : vReturn = vBasicObject.Forms(vArgs(0), vArgs(1))
Case &quot;GetFormula&quot; : vReturn = vBasicObject.GetFormula(vArgs(0))
Case &quot;GetValue&quot; : vReturn = vBasicObject.GetValue(vArgs(0))
@ -815,11 +818,13 @@ Try:
If Script = &quot;Controls&quot; Then vReturn = vBasicObject.Controls(vArgs(0))
Case &quot;SFDocuments.FormDocument&quot;
Select Case Script
Case &quot;ContextMenus&quot; : vReturn = vBasicObject.ContextMenus(vArgs(0), vArgs(1))
Case &quot;Forms&quot; : vReturn = vBasicObject.Forms(vArgs(0))
Case &quot;Toolbars&quot; : vReturn = vBasicObject.Toolbars(vArgs(0))
End Select
Case &quot;SFDocuments.Writer&quot;
Select Case Script
Case &quot;ContextMenus&quot; : vReturn = vBasicObject.ContextMenus(vArgs(0), vArgs(1))
Case &quot;Forms&quot; : vReturn = vBasicObject.Forms(vArgs(0))
Case &quot;Styles&quot; : vReturn = vBasicObject.Styles(vArgs(0), vArgs(1), vArgs(2), vArgs(3), vArgs(4), vArgs(5))
Case &quot;Toolbars&quot; : vReturn = vBasicObject.Toolbars(vArgs(0))

View file

@ -134,7 +134,7 @@ Try:
sLibrary = &quot;SFDialogs&quot;
Case &quot;database&quot;, &quot;datasheet&quot; : sLibrary = &quot;SFDatabases&quot;
Case &quot;unittest&quot; : sLibrary = &quot;SFUnitTests&quot;
Case &quot;menu&quot;, &quot;popupmenu&quot;, &quot;toolbar&quot;, &quot;toolbarbutton&quot;
Case &quot;contextmenu&quot;, &quot;menu&quot;, &quot;popupmenu&quot;, &quot;toolbar&quot;, &quot;toolbarbutton&quot;
sLibrary = &quot;SFWidgets&quot;
Case Else
End Select

View file

@ -1255,6 +1255,53 @@ Public Sub _CloseProgressBar(Optional ByRef poEvent As Object)
End Sub &apos; ScriptForge.SF_UI._CloseProgressBar
REM -----------------------------------------------------------------------------
Function _GetConfigurationManager(ByRef poComponent) As String
&apos;&apos;&apos; Derives the argument to be passed to a configuration manager
&apos;&apos;&apos; (managing the user interface elements like menus, toolbars, ...)
&apos;&apos;&apos; from the nature of the component
&apos;&apos;&apos; Args:
&apos;&apos;&apos; poComponent: any component in desktop, typically a document but not only
Dim sConfigurationManager As String &apos; Return value
Dim oWindow As Object &apos; Window type
Check:
&apos; On Local Error GoTo Catch
If IsNull(poComponent) Then GoTo Catch
Try:
Set oWindow = _IdentifyWindow(poComponent)
&apos; Derive the name of the UI configuration manager from the component type
With oWindow
Select Case .WindowName
Case BASICIDE : sConfigurationManager = &quot;com.sun.star.script.BasicIDE&quot;
Case WELCOMESCREEN : sConfigurationManager = &quot;com.sun.star.frame.StartModule&quot;
Case Else
Select Case .DocumentType
Case BASEDOCUMENT : sConfigurationManager = &quot;com.sun.star.sdb.OfficeDatabaseDocument&quot;
Case CALCDOCUMENT : sConfigurationManager = &quot;com.sun.star.sheet.SpreadsheetDocument&quot;
Case DRAWDOCUMENT : sConfigurationManager = &quot;com.sun.star.drawing.DrawingDocument&quot;
Case FORMDOCUMENT : sConfigurationManager = &quot;com.sun.star.sdb.FormDesign&quot;
Case IMPRESSDOCUMENT : sConfigurationManager = &quot;com.sun.star.presentation.PresentationDocument&quot;
Case MATHDOCUMENT : sConfigurationManager = &quot;com.sun.star.formula.FormulaProperties&quot;
Case WRITERDOCUMENT : sConfigurationManager = &quot;com.sun.star.text.TextDocument&quot;
Case TABLEDATA, QUERYDATA, SQLDATA
sConfigurationManager = &quot;com.sun.star.sdb.DataSourceBrowser&quot;
Case Else : sConfigurationManager = &quot;&quot;
End Select
End Select
End With
Finally:
_GetConfigurationManager = sConfigurationManager
Exit Function
Catch:
On Local Error GoTo 0
GoTo Finally
End Function &apos; ScriptForge.SF_UI._GetConfigurationManager
REM -----------------------------------------------------------------------------
Public Function _IdentifyWindow(ByRef poComponent As Object) As Object
&apos;&apos;&apos; Return a Window object (definition on top of module) based on component given as argument
@ -1362,7 +1409,6 @@ Public Function _ListToolbars(ByRef poComponent As Object) As Object
&apos;&apos;&apos; poComponent: any component in desktop, typically a document but not only
Dim oToolbarsDict As Object &apos; Return value
Dim oWindow As Object &apos; Window type
Dim oConfigMgr As Object &apos; com.sun.star.ui.ModuleUIConfigurationManagerSupplier
Dim sConfigurationManager As String &apos; Derived from the component&apos;s type
Dim oUIConfigMgr As Object &apos; com.sun.star.comp.framework.ModuleUIConfigurationManager
@ -1382,30 +1428,12 @@ Check:
Try:
Set oToolbarsDict = CreateScriptService(&quot;Dictionary&quot;)
Set oWindow = _IdentifyWindow(poComponent)
&apos; 1. Collect all builtin and custom toolbars stored in the LibreOffice configuration files
&apos; Derive the name of the UI configuration manager from the component type
With oWindow
Select Case .WindowName
Case BASICIDE : sConfigurationManager = &quot;com.sun.star.script.BasicIDE&quot;
Case WELCOMESCREEN : sConfigurationManager = &quot;com.sun.star.frame.StartModule&quot;
Case Else
Select Case .DocumentType
Case BASEDOCUMENT : sConfigurationManager = &quot;com.sun.star.sdb.OfficeDatabaseDocument&quot;
Case CALCDOCUMENT : sConfigurationManager = &quot;com.sun.star.sheet.SpreadsheetDocument&quot;
Case DRAWDOCUMENT : sConfigurationManager = &quot;com.sun.star.drawing.DrawingDocument&quot;
Case FORMDOCUMENT : sConfigurationManager = &quot;com.sun.star.sdb.FormDesign&quot;
Case IMPRESSDOCUMENT : sConfigurationManager = &quot;com.sun.star.presentation.PresentationDocument&quot;
Case MATHDOCUMENT : sConfigurationManager = &quot;com.sun.star.formula.FormulaProperties&quot;
Case WRITERDOCUMENT : sConfigurationManager = &quot;com.sun.star.text.TextDocument&quot;
Case TABLEDATA, QUERYDATA, SQLDATA
sConfigurationManager = &quot;com.sun.star.sdb.DataSourceBrowser&quot;
Case Else : sConfigurationManager = &quot;&quot;
End Select
End Select
End With
sConfigurationManager = _GetConfigurationManager(poComponent)
Set oConfigMgr = SF_Utils._GetUNOService(&quot;ModuleUIConfigurationManagerSupplier&quot;)
Set oUIConfigMgr = oConfigMgr.getUIConfigurationManager(sConfigurationManager)
vCommandBars = oUIConfigMgr.getUIElementsInfo(com.sun.star.ui.UIElementType.TOOLBAR)

View file

@ -2293,6 +2293,9 @@ class SFDocuments:
def CloseDocument(self, saveask = True):
return self.ExecMethod(self.vbMethod, 'CloseDocument', saveask)
def ContextMenus(self, contextmenuname = '', submenuchar = '>'):
return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'ContextMenus', contextmenuname, submenuchar)
def CreateMenu(self, menuheader, before = '', submenuchar = '>'):
return self.ExecMethod(self.vbMethod, 'CreateMenu', menuheader, before, submenuchar)
@ -2894,6 +2897,32 @@ class SFWidgets:
return self.ExecMethod(self.vbMethod, 'AddRadioButton', menuitem, name, status, icon, tooltip,
command, script)
# #########################################################################
# SF_ContextMenu CLASS
# #########################################################################
class SF_ContextMenu(SFServices):
"""
A context menu is obtained by a right-click on several areas of a document.
Each component model has its own set of context menus.
A context menu is usually predefined at LibreOffice installation.
Customization is done statically with the Tools + Customize dialog.
The actual service provides a mean to make temporary additions at
the bottom of a context menu. Those changes are lost when the document is closed.
"""
# Mandatory class properties for service registration
serviceimplementation = 'basic'
servicename = 'SFWidgets.ContextMenu'
servicesynonyms = ('contextmenu', 'sfwidgets.contextmenu')
serviceproperties = dict(ShortcutCharacter = False, SubmenuCharacter = False)
def Activate(self, enable = True):
return self.ExecMethod(self.vbMethod, 'Activate', enable)
def AddItem(self, menuitem, command = '', script = ''):
return self.ExecMethod(self.vbMethod, 'AddItem', menuitem, command, script)
# #########################################################################
# SF_PopupMenu CLASS
# #########################################################################
@ -3082,6 +3111,7 @@ FORMDOCUMENT = SFDocuments.SF_FormDocument
WRITER = SFDocuments.SF_Writer
# SFWidgets
MENU = SFWidgets.SF_Menu
CONTEXTMENU = SFWidgets.SF_ContextMenu
POPUPMENU = SFWidgets.SF_PopupMenu
TOOLBAR = SFWidgets.SF_Toolbar
TOOLBARBUTTON = SFWidgets.SF_ToolbarButton

View file

@ -86,6 +86,7 @@ FORMDOCUMENT = SFDocuments.SF_FormDocument
WRITER = SFDocuments.SF_Writer
# SFWidgets
MENU = SFWidgets.SF_Menu
CONTEXTMENU = SFWidgets.SF_ContextMenu
POPUPMENU = SFWidgets.SF_PopupMenu
TOOLBAR = SFWidgets.SF_Toolbar
TOOLBARBUTTON = SFWidgets.SF_ToolbarButton
@ -95,7 +96,7 @@ SERVICE = Union[ARRAY, BASIC, DICTIONARY, EXCEPTION, FILESYSTEM, L10N, PLATFORM,
DATABASE, DATASET, DATASHEET,
DIALOG, DIALOGCONTROL,
DOCUMENT, BASE, CALC, CALCREFERENCE, CHART, FORM, FORMCONTROL, FORMDOCUMENT, WRITER,
MENU, POPUPMENU, TOOLBAR, TOOLBARBUTTON]
MENU, CONTEXTMENU, POPUPMENU, TOOLBAR, TOOLBARBUTTON]
# UNO
UNO = TypeVar('UNO')
# Other
@ -4486,12 +4487,28 @@ class SFDocuments:
"""
...
def ContextMenus(self, contextmenuname: str = ..., submenuchar: str = ...
) -> Union[CONTEXTMENU, Tuple[str, ...]]:
"""
Returns either a list of the available ContextMenu names in the actual document
or a ``SFWidgets.SF_ContextMenu`` object instance.
Args
``contextmenuname``: the usual name of one of the available context menus.
``submenuchar``: the delimiter used to create menu trees when calling
the ``AddItem()`` method from the ``ContextMenu`` service. The default value is ">".
Returns
The list of available context menu names as a tuple of strings when ``contextmenuname``
is absent, a ``ContextMenu`` class instance otherwise.
"""
...
def CreateMenu(self, menuheader: str, before: Union[str, int] = ..., submenuchar: str = ...
) -> MENU:
"""
Creates a new menu entry in the menubar of a given document window.
The menu created is only available during the current LibreOffice session and is not saved neither
The created menu is only available during the current LibreOffice session and is not saved neither
in the document nor in the global application settings.
Hence, closing the document window will make the menu disappear.
It will only reappear when the macro that creates the menu is executed again.
@ -6864,7 +6881,97 @@ class SFWidgets:
...
# #########################################################################
# SF_Menu CLASS
# SF_ContextMenu CLASS
# #########################################################################
class SF_ContextMenu(SFServices):
"""
Complete a predefined context menu with new items.
A context menu is obtained by a right-click on several areas of a document.
Each area determines its own context menu.
(Consider right-clicking on a cell or on a sheet tab in a Calc document).
Each component model has its own set of context menus.
A context menu is usually predefined at LibreOffice installation.
Customization is done statically with the Tools + Customize dialog.
The actual service provides a mean to make temporary additions at
the bottom of a context menu in an active document. Those changes are lost when the document is closed.
The name of a context menu is the last component of the resource URL:
"private:resource/popupmenu/the-name-here"
Context menu items are either usual items or line separators. Checkboxes or radio buttons are not supported.
"""
ShortcutCharacter: str
""" Character used to define the access key of a menu item. The default character is "~" (tilde). """
SubmenuCharacter: str
""" Character or string that defines how menu items are nested. The default character is ">".
Example
``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")``
"""
def Activate(self, enable: bool = ...) -> None:
"""
Make the added items of the context menu available for execution, or, at the opposite,
disable them, depending on the argument.
Args
``enable``: when ``True`` (default), the new items of the context menu are made visible.
When ``False``, they are suppressed.
Returns
None
"""
...
def AddItem(self,
menuitem: str,
command: str = ...,
script: str = ...,
) -> None:
"""
Inserts a labeled entry or a line separator in the menu.
Args
``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. If the last component is equal to "---", a line separator is inserted
and all other arguments are ignored.
``command``: a menu command like ".uno:About". The validity of the command is not checked.
``script``: a Basic or Python script (determined by its URI notation) to be run when
the item is clicked.
Read https://wiki.documentfoundation.org/Documentation/DevGuide/Scripting_Framework#Scripting_Framework_URI_Specification
No argument will be passed to the called script.
Note
Arguments ``Command`` and ``Script`` are mutually exclusive.
Returns
None
"""
...
# #########################################################################
# SF_PopupMenu CLASS
# #########################################################################
class SF_PopupMenu(SFServices):
"""
@ -6948,11 +7055,12 @@ class SFWidgets:
tooltip: str = ...,
) -> int:
"""
Inserts a label entry in the menu.
Inserts a labeled entry or a line separator in the menu.
Args
``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.
character. If the last component is equal to "---", a line separator is inserted
and all other arguments are ignored.
``name``: the string value used to identify the menu item.
By default, the last component of the menu hierarchy is used.

View file

@ -912,6 +912,13 @@ Public Function Activate() As Boolean
Activate = [_Super].Activate()
End Function &apos; SFDocuments.SF_Base.Activate
REM -----------------------------------------------------------------------------
Public Function ContextMenus(Optional ByVal ContextMenuName As Variant _
, Optional ByVal SubmenuChar As Variant _
) As Variant
ContextMenus = [_Super].ContextMenus(ContextMenuName, SubmenuChar)
End Function &apos; SFDocuments.SF_Base.ContextMenus
REM -----------------------------------------------------------------------------
Public Function CreateMenu(Optional ByVal MenuHeader As Variant _
, Optional ByVal Before As Variant _

View file

@ -3728,6 +3728,13 @@ Public Function CloseDocument(Optional ByVal SaveAsk As Variant) As Boolean
CloseDocument = [_Super].CloseDocument(SaveAsk)
End Function &apos; SFDocuments.SF_Calc.CloseDocument
REM -----------------------------------------------------------------------------
Public Function ContextMenus(Optional ByVal ContextMenuName As Variant _
, Optional ByVal SubmenuChar As Variant _
) As Variant
ContextMenus = [_Super].ContextMenus(ContextMenuName, SubmenuChar)
End Function &apos; SFDocuments.SF_Calc.ContextMenus
REM -----------------------------------------------------------------------------
Public Function CreateMenu(Optional ByVal MenuHeader As Variant _
, Optional ByVal Before As Variant _

View file

@ -86,6 +86,9 @@ Private _CustomProperties As Object &apos; Dictionary of custom properties
&apos; Cache for static toolbar descriptions
Private _Toolbars As Object &apos; SF_Dictionary instance to hold toolbars stored in application or in document
&apos; List of standard context menus
Private _ContextMenus As Variant &apos; Array of ResourceURL strings
&apos; Style descriptor
Type StyleDescriptor
Family As Object
@ -123,6 +126,7 @@ Private Sub Class_Initialize()
Set _DocumentProperties = Nothing
Set _CustomProperties = Nothing
Set _Toolbars = Nothing
_ContextMenus = Array()
_StyleFamilies = Array()
End Sub &apos; SFDocuments.SF_Document Constructor
@ -530,6 +534,57 @@ Catch:
GoTo Finally
End Function &apos; SFDocuments.SF_Document.CloseDocument
REM -----------------------------------------------------------------------------
Public Function ContextMenus(Optional ByVal ContextMenuName As Variant _
, Optional ByVal SubmenuChar As Variant _
) As Variant
&apos;&apos;&apos; Returns either a list of the available ContextMenu names in the actual document
&apos;&apos;&apos; or a SFWidgets.ContextMenu object instance.
&apos;&apos;&apos; Args:
&apos;&apos;&apos; ContextMenuName: the usual name of one of the available ContextMenus
&apos;&apos;&apos; SubmenuChar: Delimiter used in menu trees
&apos;&apos;&apos; Returns:
&apos;&apos;&apos; A zero-based array of ContextMenu names when there is no argument,
&apos;&apos;&apos; or a new ContextMenu object instance from the SFWidgets library.
Const cstThisSub = &quot;SFDocuments.Document.ContextMenus&quot;
Const cstSubArgs = &quot;[ContextMenuName=&quot;&quot;&quot;&quot;], [SubmenuChar=&quot;&quot;&gt;&quot;&quot;]&quot;
If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
Check:
If IsMissing(ContextMenuName) Or IsEmpty(ContextMenuName) Then ContextMenuName = &quot;&quot;
If IsMissing(SubmenuChar) Or IsEmpty(SubmenuChar) Then SubmenuChar = &quot;&gt;&quot;
If UBound(_ContextMenus) &lt; 0 Then _ContextMenus = _ListContextMenus()
If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
If Not _IsStillAlive() Then GoTo Finally
If VarType(ContextMenuName) = V_STRING Then
If Len(ContextMenuName) &gt; 0 Then
If Not ScriptForge.SF_Utils._Validate(ContextMenuName, &quot;ContextMenuName&quot;, V_STRING, _ContextMenus) Then GoTo Finally
End If
Else
If Not ScriptForge.SF_Utils._Validate(ContextMenuName, &quot;ContextMenuName&quot;, V_STRING) Then GoTo Finally &apos; Manage here the VarType error
End If
If Not ScriptForge.SF_Utils._Validate(SubmenuChar, &quot;SubmenuChar&quot;, V_STRING) Then GoTo Finally
End If
Try:
If Len(ContextMenuName) = 0 Then
ContextMenus = _ContextMenus
Else
ContextMenus = CreateScriptService(&quot;SFWidgets.ContextMenu&quot; _
, _Component _
, &quot;private:resource/popupmenu/&quot; &amp; LCase(ContextMenuName) _
, SubmenuChar)
End If
Finally:
ScriptForge.SF_Utils._ExitFunction(cstThisSub)
Exit Function
Catch:
GoTo Finally
End Function &apos; SFDocuments.SF_Document.ContextMenus
REM -----------------------------------------------------------------------------
Public Function CreateMenu(Optional ByVal MenuHeader As Variant _
, Optional ByVal Before As Variant _
@ -855,6 +910,7 @@ Public Function Methods() As Variant
Methods = Array( _
&quot;Activate&quot; _
, &quot;CloseDocument&quot; _
, &quot;ContextMenus&quot; _
, &quot;CreateMenu&quot; _
, &quot;Echo&quot; _
, &quot;DeleteStyles&quot; _
@ -2000,6 +2056,43 @@ CatchReadonly:
GoTo Finally
End Function &apos; SFDocuments.SF_Document._IsStillAlive
REM -----------------------------------------------------------------------------
Private Function _ListContextMenus() As Variant
&apos;&apos;&apos; Returns an array of the usual names of the context menus available in the current document
Dim vMenus As Variant &apos; Return value
Dim vMenusObj As Variant &apos; Array of arrays of property values
Dim oSupplier As Object &apos; /singletons/com.sun.star.ui.theModuleUIConfigurationManagerSupplier
Dim sComponentType As String &apos; Argument to determine the system config manager, ex. &quot;com.sun.star.text.TextDocument&quot;
Dim oUIConf As Object &apos; com.sun.star.ui.XUIConfigurationManager
Dim i As Long
On Local Error GoTo Catch
vMenus = Array()
Try:
Set oSupplier = ScriptForge.SF_Utils._GetUNOService(&quot;ModuleUIConfigurationManagerSupplier&quot;)
sComponentType = ScriptForge.SF_UI._GetConfigurationManager(_Component)
Set oUIConf = oSupplier.getUIConfigurationManager(sComponentType)
&apos; Discard menubar, statusbar, ...
vMenusObj = oUIConf.getUIElementsInfo(com.sun.star.ui.UIElementType.POPUPMENU)
&apos; Extract and sort the names
ReDim vMenus(0 To UBound(vMenusObj))
For i = 0 To UBound(vMenusObj)
vMenus(i) = Mid(vMenusObj(i)(0).Value, Len(&quot;private:resource/popupmenu/&quot;) + 1)
Next i
vMenus = ScriptForge.SF_Array.Unique(vMenus, CaseSensitive := True)
Finally:
_ListContextMenus = vMenus
Exit Function
Catch:
On Local Error GoTo 0
GoTo Finally
End Function &apos; SFDocuments.SF_Document._ListContextMenus
REM -----------------------------------------------------------------------------
Private Sub _LoadDocumentProperties()
&apos;&apos;&apos; Create dictionary with document properties as entries/ Custom properties are excluded

View file

@ -501,6 +501,13 @@ Public Function Activate() As Boolean
Activate = [_Super].Activate()
End Function &apos; SFDocuments.SF_FormDocument.Activate
REM -----------------------------------------------------------------------------
Public Function ContextMenus(Optional ByVal ContextMenuName As Variant _
, Optional ByVal SubmenuChar As Variant _
) As Variant
ContextMenus = [_Super].ContextMenus(ContextMenuName, SubmenuChar)
End Function &apos; SFDocuments.SF_FormDocument.ContextMenus
REM -----------------------------------------------------------------------------
Public Function CreateMenu(Optional ByVal MenuHeader As Variant _
, Optional ByVal Before As Variant _

View file

@ -654,6 +654,13 @@ Public Function CloseDocument(Optional ByVal SaveAsk As Variant) As Boolean
CloseDocument = [_Super].CloseDocument(SaveAsk)
End Function &apos; SFDocuments.SF_Writer.CloseDocument
REM -----------------------------------------------------------------------------
Public Function ContextMenus(Optional ByVal ContextMenuName As Variant _
, Optional ByVal SubmenuChar As Variant _
) As Variant
ContextMenus = [_Super].ContextMenus(ContextMenuName, SubmenuChar)
End Function &apos; SFDocuments.SF_Writer.ContextMenus
REM -----------------------------------------------------------------------------
Public Function CreateMenu(Optional ByVal MenuHeader As Variant _
, Optional ByVal Before As Variant _
@ -1166,4 +1173,4 @@ Private Function _Repr() As String
End Function &apos; SFDocuments.SF_Writer._Repr
REM ============================================ END OF SFDOCUMENTS.SF_WRITER
</script:module>
</script:module>

View file

@ -0,0 +1,574 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="SF_ContextMenu" script:language="StarBasic" script:moduleType="normal">REM =======================================================================================================================
REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
REM === The SFWidgets library is one of the associated libraries. ===
REM === Full documentation is available on https://help.libreoffice.org/ ===
REM =======================================================================================================================
Option Compatible
Option ClassModule
Option Explicit
&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
&apos;&apos;&apos; SF_ContextMenu
&apos;&apos;&apos; ==============
&apos;&apos;&apos; Complete a predefined context menu with new items.
&apos;&apos;&apos;
&apos;&apos;&apos; A context menu is obtained by a right-click on several areas of a document.
&apos;&apos;&apos; Each area determines its own context menu.
&apos;&apos;&apos; (Consider right-clicking on a cell or on a sheet tab in a Calc document).
&apos;&apos;&apos; Each component model has its own set of context menus.
&apos;&apos;&apos;
&apos;&apos;&apos; A context menu is usually predefined at LibreOffice installation.
&apos;&apos;&apos; Customization is done statically with the Tools + Customize dialog.
&apos;&apos;&apos; The actual service provides a mean to make temporary additions at
&apos;&apos;&apos; the bottom of a context menu. Those changes are lost when the document is closed.
&apos;&apos;&apos;
&apos;&apos;&apos; The name of a context menu is the last component of the resource URL:
&apos;&apos;&apos; &quot;private:resource/popupmenu/the-name-here&quot;
&apos;&apos;&apos;
&apos;&apos;&apos; Context menu items are either:
&apos;&apos;&apos; - usual items
&apos;&apos;&apos; - line separators
&apos;&apos;&apos; Checkboxes or radio buttons are not supported.
&apos;&apos;&apos;
&apos;&apos;&apos; A context menu is setup in next sequence:
&apos;&apos;&apos; 1. Define each menu item, submenu or line separator with AddItem(...)
&apos;&apos;&apos; 2. Activate() the menu to make it visible and active
&apos;&apos;&apos; 3. If meaningful, it can be temporarily deactivate with Activate(False)
&apos;&apos;&apos; 4. Items can be optionally added, and the menu might be made active again
&apos;&apos;&apos; 5. Dispose() cleans the memory resources
&apos;&apos;&apos;
&apos;&apos;&apos; Definitions:
&apos;&apos;&apos; SubmenuCharacter: the character or the character string that identifies how menus are cascading
&apos;&apos;&apos; Default = &quot;&gt;&quot;
&apos;&apos;&apos; Can be set when invoking the ContextMenu service
&apos;&apos;&apos; ShortcutCharacter: the underline access key character
&apos;&apos;&apos; Default = &quot;~&quot;
&apos;&apos;&apos;
&apos;&apos;&apos; Service invocation:
&apos;&apos;&apos; From a document, calc, writer, formdocument or datasheet service:
&apos;&apos;&apos; Dim MenusList As Variant, oCtxMenu As Object
&apos;&apos;&apos; MenusList = doc.ContextMenus()
&apos;&apos;&apos; &apos; Returns a list of available context menus as strings
&apos;&apos;&apos; Set oCtxMenu = doc.ContextMenus(ContextMenuName, SubmenuChar = &quot;&gt;&gt;&quot;)
&apos;&apos;&apos; &apos; Returns a context menu service instance
&apos;&apos;&apos;
&apos;&apos;&apos; Menus and submenus
&apos;&apos;&apos; To create a context menu with submenus, use the character defined in the
&apos;&apos;&apos; SubmenuCharacter property while creating the menu entry to define where it will be
&apos;&apos;&apos; placed. For instance, consider the following menu/submenu hierarchy.
&apos;&apos;&apos; Item A
&apos;&apos;&apos; Item B &gt; Item B.1
&apos;&apos;&apos; Item B.2
&apos;&apos;&apos; ------ (line separator)
&apos;&apos;&apos; Item C &gt; Item C.1 &gt; Item C.1.1
&apos;&apos;&apos; Item C.1.2
&apos;&apos;&apos; Item C &gt; ------ (line separator)
&apos;&apos;&apos; Item C &gt; Item C.2 &gt; Item C.2.1
&apos;&apos;&apos; Item C.2.2
&apos;&apos;&apos; Next code will create the menu/submenu hierarchy
&apos;&apos;&apos; With myMenu
&apos;&apos;&apos; .AddItem(&quot;Item A&quot;, Command := ...)
&apos;&apos;&apos; .AddItem(&quot;Item B&gt;Item B.1&quot;, Script := ...)
&apos;&apos;&apos; .AddItem(&quot;Item B&gt;Item B.2&quot;, ...)
&apos;&apos;&apos; .AddItem(&quot;---&quot;)
&apos;&apos;&apos; .AddItem(&quot;Item C&gt;Item C.1&gt;Item C.1.1&quot;, ...)
&apos;&apos;&apos; .AddItem(&quot;Item C&gt;Item C.1&gt;Item C.1.2&quot;, ...)
&apos;&apos;&apos; .AddItem(&quot;Item C&gt;---&quot;)
&apos;&apos;&apos; .AddItem(&quot;Item C&gt;Item C.2&gt;Item C.2.1&quot;, ...)
&apos;&apos;&apos; .AddItem(&quot;Item C&gt;Item C.2&gt;Item C.2.2&quot;, ...)
&apos;&apos;&apos; End With
&apos;&apos;&apos;
&apos;&apos;&apos; Example: Add 2 items to the general context menu of a Writer document
&apos;&apos;&apos; Sub SetContextMenu(oWriter As Object)
&apos;&apos;&apos; Dim oMenu As Object
&apos;&apos;&apos; Set oMenu = oWriter.ContextMenus(&quot;text&quot;)
&apos;&apos;&apos; With oMenu
&apos;&apos;&apos; .AddItem(&quot;About&quot;, Command := &quot;.uno:About&quot;)
&apos;&apos;&apos; .AddItem(&quot;Run&quot;, Script := &quot;vnd.sun.star.script:Standard.Module1.MyFunction?language=Basic&amp;location=document&quot;)
&apos;&apos;&apos; &apos; MyFunction is a Sub without argument
&apos;&apos;&apos; .Activate()
&apos;&apos;&apos; .Dispose()
&apos;&apos;&apos; End With
&apos;&apos;&apos; End Sub
&apos;&apos;&apos;
&apos;&apos;&apos;
&apos;&apos;&apos; Detailed user documentation:
&apos;&apos;&apos; https://help.libreoffice.org/latest/en-US/text/sbasic/shared/03/sf_contextmenu.html?DbPAR=BASIC
&apos;&apos;&apos;
&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
REM ================================================================== EXCEPTIONS
REM ============================================================= PRIVATE MEMBERS
Private [Me] As Object
Private ObjectType As String &apos; Must be ContextMenu
Private ServiceName As String
&apos; Menu descriptors
Private Component As Object &apos; com.sun.star.lang.XComponent
Private ResourceURL As String &apos; private:resource/popupmenu/...
Private ConfigManager As Object &apos; com.sun.star.ui.XUIConfigurationManager
Private MenuTree As Variant &apos; Dictionary nodename - com.sun.star.ui.ItemDescriptor pair
Private SubmenuChar As String &apos; Delimiter in menu trees
Private MenuContainer As Object &apos; com.sun.star.container.XIndexAccess
REM ============================================================ MODULE CONSTANTS
Private Const _UnderlineAccessKeyChar = &quot;~&quot;
Private Const _DefaultSubmenuChar = &quot;&gt;&quot;
Private Const _SeparatorChar = &quot;---&quot;
Private Const cstUnoPrefix = &quot;.uno:&quot;
REM ====================================================== CONSTRUCTOR/DESTRUCTOR
REM -----------------------------------------------------------------------------
Private Sub Class_Initialize()
Set [Me] = Nothing
ObjectType = &quot;ContextMenu&quot;
ServiceName = &quot;SFWidgets.ContextMenu&quot;
Set Component = Nothing
Set ConfigManager = Nothing
ResourceURL = &quot;&quot;
Set MenuTree = Nothing
SubmenuChar = _DefaultSubmenuChar
Set MenuContainer = Nothing
End Sub &apos; SFWidgets.SF_ContextMenu Constructor
REM -----------------------------------------------------------------------------
Private Sub Class_Terminate()
Call Class_Initialize()
End Sub &apos; SFWidgets.SF_ContextMenu Destructor
REM -----------------------------------------------------------------------------
Public Function Dispose() As Variant
If Not IsNull(MenuTree) Then Set MenuTree = MenuTree.Dispose()
Call Class_Terminate()
Set Dispose = Nothing
End Function &apos; SFWidgets.SF_ContextMenu Explicit Destructor
REM ================================================================== PROPERTIES
REM -----------------------------------------------------------------------------
Property Get ShortcutCharacter() As Variant
&apos;&apos;&apos; The ShortcutCharacter property specifies character preceding the underline access key
ShortcutCharacter = _PropertyGet(&quot;ShortcutCharacter&quot;)
End Property &apos; SFWidgets.SF_ContextMenu.ShortcutCharacter (get)
REM -----------------------------------------------------------------------------
Property Get SubmenuCharacter() As Variant
&apos;&apos;&apos; The SubmenuCharacter property specifies the character string indicating
&apos;&apos;&apos; a sub-menu in a popup menu item
SubmenuCharacter = _PropertyGet(&quot;SubmenuCharacter&quot;)
End Property &apos; SFWidgets.SF_ContextMenu.SubmenuCharacter (get)
REM ===================================================================== METHODS
REM -----------------------------------------------------------------------------
Public Sub Activate(Optional ByVal Enable As Variant) As Variant
&apos;&apos;&apos; Make the added items of the context menu available for execution,
&apos;&apos;&apos; or, at the opposite, disable them, depending on the argument.
&apos;&apos;&apos; Args:
&apos;&apos;&apos; Enable: When True (default), the new items of the context menu are made visible.
&apos;&apos;&apos; When False, they are suppressed.
&apos;&apos;&apos; Returns:
&apos;&apos;&apos; None
&apos;&apos;&apos; Examples:
&apos;&apos;&apos; Sub SetContextMenu(oWriter As Object) &apos; oWriter is a SFDocuments.SF_Writer service instance
&apos;&apos;&apos; Dim oMenu As Object
&apos;&apos;&apos; Set oMenu = oWriter.ContextMenus(&quot;text&quot;)
&apos;&apos;&apos; With oMenu
&apos;&apos;&apos; .AddItem(&quot;About&quot;, Command := &quot;.uno:About&quot;)
&apos;&apos;&apos; .AddItem(&quot;Run&quot;, Script := &quot;vnd.sun.star.script:Standard.Module1.MyFunction?language=Basic&amp;location=document&quot;)
&apos;&apos;&apos; &apos; MyFunction is a Sub without argument
&apos;&apos;&apos; .Activate()
&apos;&apos;&apos; .Dispose() &apos; Do not dispose() if you plan later on to Activate(Enable := False) the context menu
&apos;&apos;&apos; End With
&apos;&apos;&apos; End Sub
Dim bSettings As Boolean &apos; When True, the menu is already stored
Const cstThisSub = &quot;SFWidgets.ContextMenu.Activate&quot;
Const cstSubArgs = &quot;[Enable=True]&quot;
If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
Check:
If IsMissing(Enable) Or IsEmpty(Enable) Then Enable = True
If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
If Not ScriptForge.SF_Utils._Validate(Enable, &quot;Enable&quot;, ScriptForge.V_BOOLEAN) Then GoTo Catch
End If
If IsNull(ConfigManager) Or IsNull(MenuContainer) Then GoTo Finally
Try:
With ConfigManager
bSettings = .hasSettings(ResourceURL)
If Enable And bSettings Then
.replaceSettings(ResourceURL, MenuContainer)
ElseIf Enable And Not bSettings Then
.insertSettings(ResourceURL, MenuContainer)
ElseIf Not Enable And bSettings Then
.removeSettings(ResourceURL)
Else
&apos; Nothing to deactivate
End If
End With
Finally:
ScriptForge.SF_Utils._ExitFunction(cstThisSub)
Exit Sub
Catch:
GoTo Finally
End Sub &apos; SFWidgets.SF_ContextMenu.Activate
REM -----------------------------------------------------------------------------
Public Sub AddItem(Optional ByVal MenuItem As Variant _
, Optional ByVal Command As Variant _
, Optional ByVal Script As Variant _
) As Integer
&apos;&apos;&apos; Insert in the context menu a new entry
&apos;&apos;&apos; Args:
&apos;&apos;&apos; MenuItem: The text to be displayed in the menu entry.
&apos;&apos;&apos; It determines also the hierarchy of the popup menu
&apos;&apos;&apos; It is made up of all the components (separated by the &quot;SubmenuCharacter&quot;) of the menu branch
&apos;&apos;&apos; Example: A&gt;B&gt;C means &quot;C&quot; is a new entry in submenu &quot;A =&gt; B =&gt;&quot;
&apos;&apos;&apos; If the last component is equal to &quot;---&quot;, a line separator is inserted and all other arguments are ignored
&apos;&apos;&apos; Command: A menu command like &quot;.uno:About&quot;. The validity of the command is not checked.
&apos;&apos;&apos; Script: a Basic or Python script (determined by its URI notation) to be run when the item is clicked
&apos;&apos;&apos; Read https://wiki.documentfoundation.org/Documentation/DevGuide/Scripting_Framework#Scripting_Framework_URI_Specification
&apos;&apos;&apos; No argument will be passed to the called script.
&apos;&apos;&apos; Arguments Command and Script are mutually exclusive.
&apos;&apos;&apos; Returns:
&apos;&apos;&apos; None
&apos;&apos;&apos; Examples:
&apos;&apos;&apos; myMenu.AddItem(&quot;Menu top&gt;Item 1&quot;, Command := &quot;About&quot;)
&apos;&apos;&apos; myMenu.AddItem(&quot;Menu top&gt;Item 2&quot;, Script := &quot;vnd.sun.star.script:myLib.Module1.ThisSub?language=Basic&amp;location=document&quot;)
Dim sCommand As String &apos; Alias of either Command or Script
Dim vSplit As Variant &apos; Split menu item
Dim sMenu As String &apos; Submenu where to attach the new item, as a string
Dim oMenu As Object &apos; Submenu where to attach the new item, as an object
Dim sName As String &apos; The text displayed in the menu box
Dim vEntry As Variant &apos; com.sun.star.ui.ItemDescriptor
Const cstThisSub = &quot;SFWidgets.ContextMenu.AddItem&quot;
Const cstSubArgs = &quot;MenuItem, [Command=&quot;&quot;&quot;&quot;], [Script=&quot;&quot;&quot;&quot;]&quot;
If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
Check:
If IsMissing(Command) Or IsEmpty(Command) Then Command = &quot;&quot;
If IsMissing(Script) Or IsEmpty(Script) Then Script = &quot;&quot;
If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
If Not ScriptForge.SF_Utils._Validate(MenuItem, &quot;MenuItem&quot;, V_STRING) Then GoTo Catch
If Not ScriptForge.SF_Utils._Validate(Command, &quot;Command&quot;, V_STRING) Then GoTo Catch
If Not ScriptForge.SF_Utils._Validate(Script, &quot;Script&quot;, V_STRING) Then GoTo Catch
End If
If Len(Command) &gt; 0 Then
If Left(Command, Len(cstUnoPrefix)) = cstUnoPrefix Then sCommand = Command Else sCommand = cstUnoPrefix &amp; Command
Else
sCommand = Script
End If
Try:
&apos; Run through the upper menu tree
vSplit = _SplitMenuItem(MenuItem)
&apos; Create and determine the menu to which to attach the new item
sMenu = vSplit(0)
Set oMenu = _GetContextMenu(sMenu) &apos; Run through the upper menu tree and retain the last branch
&apos; Insert the new item
sName = vSplit(1)
With ScriptForge.SF_Utils
vEntry = Array( ._MakePropertyValue(&quot;Type&quot;, Iif(sName = _SeparatorChar, _
com.sun.star.ui.ItemType.SEPARATOR_LINE, _
com.sun.star.ui.ItemType.DEFAULT)) _
, ._MakePropertyValue(&quot;Label&quot;, Iif(sName = _SeparatorChar, &quot;&quot;, sName)) _
, ._MakePropertyValue(&quot;CommandURL&quot;, sCommand) _
, ._MakePropertyValue(&quot;HelpURL&quot;, &quot;&quot;) _
, ._MakePropertyValue(&quot;Style&quot;, _
Iif(Len(Script) &gt; 0, 0, com.sun.star.ui.ItemStyle.ICON)) _
, ._MakePropertyValue(&quot;ItemDescriptorContainer&quot;, Null) _
)
oMenu.insertByIndex(oMenu.Count, vEntry)
End With
Finally:
ScriptForge.SF_Utils._ExitFunction(cstThisSub)
Exit Sub
Catch:
GoTo Finally
End Sub &apos; SFWidgets.SF_ContextMenu.AddItem
REM -----------------------------------------------------------------------------
Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
&apos;&apos;&apos; Return the actual value of the given property
&apos;&apos;&apos; Args:
&apos;&apos;&apos; PropertyName: the name of the property as a string
&apos;&apos;&apos; Returns:
&apos;&apos;&apos; The actual value of the property
&apos;&apos;&apos; If the property does not exist, returns Null
&apos;&apos;&apos; Exceptions:
&apos;&apos;&apos; see the exceptions of the individual properties
&apos;&apos;&apos; Examples:
&apos;&apos;&apos; myModel.GetProperty(&quot;MyProperty&quot;)
Const cstThisSub = &quot;SFWidgets.ContextMenu.GetProperty&quot;
Const cstSubArgs = &quot;&quot;
If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
GetProperty = Null
Check:
If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
If Not ScriptForge.SF_Utils._Validate(PropertyName, &quot;PropertyName&quot;, V_STRING, Properties()) Then GoTo Catch
End If
Try:
GetProperty = _PropertyGet(PropertyName)
Finally:
ScriptForge.SF_Utils._ExitFunction(cstThisSub)
Exit Function
Catch:
GoTo Finally
End Function &apos; SFWidgets.SF_ContextMenu.GetProperty
REM -----------------------------------------------------------------------------
Public Function Methods() As Variant
&apos;&apos;&apos; Return the list of public methods of the Model service as an array
Methods = Array( _
&quot;AddItem&quot; _
, &quot;Execute&quot; _
)
End Function &apos; SFWidgets.SF_ContextMenu.Methods
REM -----------------------------------------------------------------------------
Public Function Properties() As Variant
&apos;&apos;&apos; Return the list or properties of the Timer a.AddItem(&quot;B&gt;B1&quot;)class as an array
Properties = Array( _
&quot;ShortcutCharacter&quot; _
, &quot;SubmenuCharacter&quot; _
)
End Function &apos; SFWidgets.SF_ContextMenu.Properties
REM -----------------------------------------------------------------------------
Public Function SetProperty(Optional ByVal PropertyName As Variant _
, Optional ByRef Value As Variant _
) As Boolean
&apos;&apos;&apos; Set a new value to the given property
&apos;&apos;&apos; Args:
&apos;&apos;&apos; PropertyName: the name of the property as a string
&apos;&apos;&apos; Value: its new value
&apos;&apos;&apos; Exceptions
&apos;&apos;&apos; ARGUMENTERROR The property does not exist
Const cstThisSub = &quot;SFWidgets.ContextMenu.SetProperty&quot;
Const cstSubArgs = &quot;PropertyName, Value&quot;
If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
SetProperty = False
Check:
If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
If Not SF_Utils._Validate(PropertyName, &quot;PropertyName&quot;, V_STRING, Properties()) Then GoTo Catch
End If
Try:
SetProperty = _PropertySet(PropertyName, Value)
Finally:
SF_Utils._ExitFunction(cstThisSub)
Exit Function
Catch:
GoTo Finally
End Function &apos; SFWidgets.SF_ContextMenu.SetProperty
REM =========================================================== PRIVATE FUNCTIONS
REM -----------------------------------------------------------------------------
Private Function _GetContextMenu(ByVal psSubmenu As String) As Object
&apos;&apos;&apos; Get the context menu node corresponding with the string in argument
&apos;&apos;&apos; A context menu entry is represented by a set of property values
&apos;&apos;&apos; grouped as a ItemDescriptor entry in above array.
&apos;&apos;&apos; The menu node is stored in a com.cun.star.ItemDescriptor service
&apos;&apos;&apos; If the menu entry exists, it is found in the MenuTree dictionary
&apos;&apos;&apos; If it does not exist, it is created recursively.
&apos;&apos;&apos; Args:
&apos;&apos;&apos; psSubmenu: a string like &quot;A&gt;B&quot;
&apos;&apos;&apos; Returns
&apos;&apos;&apos; A com.sun.star.container.XIndexAccess object
&apos;&apos;&apos; Example
&apos;&apos;&apos; If psSubmenu = &quot;A&gt;B&gt;C&gt;D&quot;, and the initial menu container is empty,
&apos;&apos;&apos; - &quot;A&quot;, &quot;A&gt;B&quot;, &quot;A&gt;B&gt;C&quot;, &quot;A&gt;B&gt;C&gt;D&quot; should be created
&apos;&apos;&apos; - the popup menu corresponding with &quot;A&gt;B&gt;C&gt;D&quot; should be returned
Dim oContext As Object &apos; Return value
Dim vSplit As Variant &apos; An array as returned by Split()
Dim sMenu As String &apos; The left part of psSubmenu
Dim vMenu As Variant &apos; Array of property values =&gt; com.sun.star.ui.ItemDescriptor
Dim oLastMenu As Object &apos; com.sun.star.ui.ItemDescriptor.ItemDescriptorContainer
Dim oNewMenu As Object &apos; com.sun.star.ui.ItemDescriptor.ItemDescriptorContainer
Dim i As Long
Set oContext = Nothing
Try:
With ScriptForge.SF_Utils
If Len(psSubmenu) = 0 Then &apos; Menu starts at the root
Set oContext = MenuContainer
ElseIf MenuTree.Exists(psSubmenu) Then &apos; Shortcut: if the submenu exists, get it directly
Set oContext = ._GetPropertyValue(MenuTree.Item(psSubmenu), &quot;ItemDescriptorContainer&quot;)
Else &apos; Build the tree
vSplit = Split(psSubmenu, SubmenuChar)
&apos; Search the successive submenus in the MenuTree dictionary, If not found, create a new entry
Set oLastMenu = MenuContainer
For i = 0 To UBound(vSplit)
sMenu = Join(ScriptForge.SF_Array.Slice(vSplit, 0, i), SubmenuChar)
If MenuTree.Exists(sMenu) Then
Set oNewMenu = ._GetPropertyValue(MenuTree.Item(sMenu), &quot;ItemDescriptorContainer&quot;)
Else
&apos; Insert the new menu tree item at the bottom of the tree branch
Set oNewMenu = MenuContainer.createInstanceWithContext(GetDefaultContext())
vMenu = Array( ._MakePropertyValue(&quot;Type&quot;, com.sun.star.ui.ItemType.DEFAULT) _
, ._MakePropertyValue(&quot;Label&quot;, vSplit(i)) _
, ._MakePropertyValue(&quot;CommandURL&quot;, &quot;&quot;) _
, ._MakePropertyValue(&quot;HelpURL&quot;, &quot;&quot;) _
, ._MakePropertyValue(&quot;Style&quot;, 0) _
, ._MakePropertyValue(&quot;ItemDescriptorContainer&quot;, oNewMenu) _
)
oLastMenu.insertByIndex(oLastMenu.Count, vMenu)
MenuTree.Add(sMenu, vMenu)
End If
Set oLastMenu = oNewMenu
Next i
Set oContext = oLastMenu
End If
End With
Finally:
Set _GetContextMenu = oContext
Exit Function
End Function &apos; SFWidgets.SF_ContextMenu._GetContextMenu
REM -----------------------------------------------------------------------------
Public Sub _Initialize(ByRef poComponent As Object _
, ByVal psContextMenuName As String _
, ByVal psSubmenuChar As String _
)
&apos;&apos;&apos; Complete the object creation process:
&apos;&apos;&apos; - Initialize the dictionary
&apos;&apos;&apos; - initialize the root popup menu
&apos;&apos;&apos; - store the arguments for later use
&apos;&apos;&apos; Args:
&apos;&apos;&apos; Component: the document&apos;s component requesting a context menu
&apos;&apos;&apos; ContextMenuName: a private:resource/popupmenu/... reference
&apos;&apos;&apos; SubmenuChar: Delimiter used in menu trees
Dim oSupplier As Object &apos; /singletons/com.sun.star.ui.theModuleUIConfigurationManagerSupplier
Dim sComponentType As String &apos; Argument to determine the system config manager, ex. &quot;com.sun.star.text.TextDocument&quot;
Dim MainConfigManager As Object &apos; com.sun.star.ui.XUIConfigurationManager
Try:
&apos; Initialize the dictionary
Set MenuTree = ScriptForge.SF_Services.CreateScriptService(&quot;Dictionary&quot;)
&apos; Identify the container of the menu tree
&apos; The container is taken either from the system configuration manager of from the local (= in document) one
&apos; It is saved in the local version when the menu is Executed()
Set oSupplier = ScriptForge.SF_Utils._GetUNOService(&quot;ModuleUIConfigurationManagerSupplier&quot;)
sComponentType = ScriptForge.SF_UI._GetConfigurationManager(poComponent)
Set MainConfigManager = oSupplier.getUIConfigurationManager(sComponentType)
Set ConfigManager = poComponent.getUIConfigurationManager(sComponentType)
&apos; Copy from system config manager if not found in local (= in document) one
If ConfigManager.hasSettings(psContextMenuName) Then
Set MenuContainer = ConfigManager.getSettings(psContextMenuName, true)
Else
Set MenuContainer = MainConfigManager.getSettings(psContextMenuName, true)
End If
&apos; Store the private instance properties
Set Component = poComponent
ResourceURL = psContextMenuName
If Len(psSubmenuChar) &gt; 0 Then SubmenuChar = psSubmenuChar
Finally:
Exit Sub
End Sub &apos; SFWidgets.SF_ContextMenu._Initialize
REM -----------------------------------------------------------------------------
Private Function _PropertyGet(Optional ByVal psProperty As String) As Variant
&apos;&apos;&apos; Return the value of the named property
&apos;&apos;&apos; Args:
&apos;&apos;&apos; psProperty: the name of the property
Dim vGet As Variant &apos; Return value
Dim cstThisSub As String
Const cstSubArgs = &quot;&quot;
cstThisSub = &quot;SFWidgets.ContextMenu.get&quot; &amp; psProperty
If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
_PropertyGet = Null
Select Case UCase(psProperty)
Case UCase(&quot;ShortcutCharacter&quot;)
_PropertyGet = _UnderlineAccessKeyChar
Case UCase(&quot;SubmenuCharacter&quot;)
_PropertyGet = SubmenuChar
Case Else
_PropertyGet = Null
End Select
Finally:
ScriptForge.SF_Utils._ExitFunction(cstThisSub)
Exit Function
Catch:
GoTo Finally
End Function &apos; SFWidgets.SF_ContextMenu._PropertyGet
REM -----------------------------------------------------------------------------
Private Function _Repr() As String
&apos;&apos;&apos; Convert the SF_ContextMenu instance to a readable string, typically for debugging purposes (DebugPrint ...)
&apos;&apos;&apos; Args:
&apos;&apos;&apos; Return:
&apos;&apos;&apos; &quot;[ContextMenu]: Name, Menu entries
_Repr = &quot;[ContextMenu]: &quot; &amp; ResourceURL &amp; &quot;, &quot; &amp; SF_String.Represent(MenuTree.Keys())
End Function &apos; SFWidgets.SF_ContextMenu._Repr
REM -----------------------------------------------------------------------------
Private Function _SplitMenuItem(ByVal psMenuItem As String ) As Variant
&apos;&apos;&apos; Split a menu item given as a string and delimited by the submenu character
&apos;&apos;&apos; Args:
&apos;&apos;&apos; psMenuItem: a string like &quot;A&gt;B&gt;C&quot;
&apos;&apos;&apos; Returns:
&apos;&apos;&apos; An array: [0] = &quot;A&gt;B&quot;
&apos;&apos;&apos; [1] = &quot;C&quot;
Dim vReturn(0 To 1) As String &apos; Return value
Dim vMenus() As Variant &apos; Array of menus
Try:
vMenus = Split(psMenuItem, SubmenuChar)
vReturn(1) = vMenus(UBound(vMenus))
vReturn(0) = Left(psMenuItem, Len(psMenuItem) - Iif(UBound(vMenus) &gt; 0, Len(SubmenuChar), 0) - Len(vReturn(1)))
Finally:
_SplitMenuItem = vReturn
End Function &apos; SFWidgets.SF_ContextMenu._SplitMenuItem
REM ============================================ END OF SFWIDGETS.SF_CONTEXTMENU
</script:module>

View file

@ -13,7 +13,7 @@ Option Explicit
&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
&apos;&apos;&apos; SF_Menu
&apos;&apos;&apos; ============
&apos;&apos;&apos; =======
&apos;&apos;&apos; Display a menu in the menubar of a document or a form document.
&apos;&apos;&apos; After use, the menu will not be saved neither in the application settings, nor in the document.
&apos;&apos;&apos;
@ -69,8 +69,9 @@ Option Explicit
&apos;&apos;&apos; Set oDoc = ui.GetDocument(ThisComponent)
&apos;&apos;&apos; Set myMenu = oDoc.CreateMenu(&quot;My own menu&quot;)
&apos;&apos;&apos;
&apos;&apos;&apos;
&apos;&apos;&apos; Detailed user documentation:
&apos;&apos;&apos; https://help.libreoffice.org/latest/en-US/text/sbasic/shared/03/SF_Menu.html?DbPAR=BASIC
&apos;&apos;&apos; https://help.libreoffice.org/latest/en-US/text/sbasic/shared/03/sf_menu.html?DbPAR=BASIC
&apos;&apos;&apos;
&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;

View file

@ -378,8 +378,8 @@ Public Function Execute(Optional ByVal ReturnId As Variant) As Variant
&apos;&apos;&apos; Set myMenu = CreateScriptService(&quot;SFWidgets.PopupMenu&quot;, poMouseEvent)
&apos;&apos;&apos; With myMenu
&apos;&apos;&apos; .AddCheckBox(&quot;View&gt;Toolbars&gt;Dialog&quot;)
&apos;&apos;&apos; .AddCheckBox(&quot;View&gt;Toolbars&gt;Find&quot;, STatus := True)
&apos;&apos;&apos; .AddCheckBox(&quot;View&gt;Status Bar&quot;, STatus := True)
&apos;&apos;&apos; .AddCheckBox(&quot;View&gt;Toolbars&gt;Find&quot;, Status := True)
&apos;&apos;&apos; .AddCheckBox(&quot;View&gt;Status Bar&quot;, Status := True)
&apos;&apos;&apos; .AddItem(&quot;View&gt;Full Screen&quot;, Name := &quot;FULLSCREEN&quot;)
&apos;&apos;&apos; vChoice = .Execute(False) &apos; When 1st checkbox is clicked, return &quot;Dialog&quot;
&apos;&apos;&apos; &apos; When last item is clicked, return &quot;FULLSCREEN&quot;
@ -531,7 +531,7 @@ Public Function _AddItem(ByVal MenuItem As String _
&apos;&apos;&apos; Example: &quot;cmd/sc_cut.png&quot;
&apos;&apos;&apos; Tooltip: The help text to be displayed as a tooltip
&apos;&apos;&apos; Command: only for menubar menus
&apos;&apos;&apos; Either a uo command like &quot;.uno:About&quot;
&apos;&apos;&apos; Either a UNO command like &quot;.uno:About&quot;
&apos;&apos;&apos; or a script to be run: script URI ::: string argument to be passed to the script
&apos;&apos;&apos; Returns:
&apos;&apos;&apos; The numeric identification of the newly inserted item
@ -655,7 +655,7 @@ Private Function _GetPopupMenu(ByVal psSubmenu As String) As Object
&apos;&apos;&apos; - the popup menu corresponding with &quot;A&gt;B&gt;C&gt;D&quot; should be returned
Dim oPopup As Object &apos; Return value
Dim vSplit As Variant &apos; An array as returned by _SplitMenuItem()
Dim vSplit As Variant &apos; An array as returned by Split()
Dim sMenu As String &apos; The left part of psSubmenu
Dim oMenu As Object &apos; com.sun.star.awt.XpopupMenu
Dim oLastMenu As Object &apos; com.sun.star.awt.XpopupMenu

View file

@ -22,10 +22,19 @@ Option Explicit
&apos;&apos;&apos; Register the list of services implemented by the current library
&apos;&apos;&apos; - _NewMenu
&apos;&apos;&apos; Create a new menu service instance.
&apos;&apos;&apos; Called from SFDocuments services with CreateMenu()
&apos;&apos;&apos; Called from SFDocuments services with doc.CreateMenu(...)
&apos;&apos;&apos; - _NewContextMenu
&apos;&apos;&apos; Create a new context menu service instance.
&apos;&apos;&apos; Called from SFDocuments services with doc.ContextMenus(...)
&apos;&apos;&apos; - _NewPopupMenu
&apos;&apos;&apos; Create a new popup menu service instance.
&apos;&apos;&apos; Called from CreateScriptService()
&apos;&apos;&apos; Called from CreateScriptService(&quot;PopupMenu, ...)
&apos;&apos;&apos; - _NewToolbar
&apos;&apos;&apos; Create a new toolbar service instance.
&apos;&apos;&apos; Called from SFDocuments services with doc.Toolbars(...)
&apos;&apos;&apos; - _NewToolbarButton
&apos;&apos;&apos; Create a new toolbarbutton service instance.
&apos;&apos;&apos; Called from a Toolbar service with toolbar.ToolbarButtons(...)
&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
REM ================================================================== EXCEPTIONS
@ -51,15 +60,59 @@ Public Sub RegisterScriptServices() As Variant
With GlobalScope.ScriptForge.SF_Services
.RegisterService(&quot;Menu&quot;, &quot;SFWidgets.SF_Register._NewMenu&quot;) &apos; Reference to the function initializing the service
.RegisterService(&quot;ContextMenu&quot;, &quot;SFWidgets.SF_Register._NewContextMenu&quot;) &apos; id.
.RegisterService(&quot;PopupMenu&quot;, &quot;SFWidgets.SF_Register._NewPopupMenu&quot;) &apos; id.
.RegisterService(&quot;Toolbar&quot;, &quot;SFWidgets.SF_Register._NewToolbar&quot;) &apos; id.
.RegisterService(&quot;ToolbarButton&quot;, &quot;SFWidgets.SF_Register._NewToolbarButton&quot;) &apos; id.
.RegisterService(&quot;ToolbarButton&quot;, &quot;SFWidgets.SF_Register._NewToolbarButton&quot;) &apos; id.
End With
End Sub &apos; SFWidgets.SF_Register.RegisterScriptServices
REM =========================================================== PRIVATE FUNCTIONS
REM -----------------------------------------------------------------------------
Public Function _NewContextMenu(Optional ByVal pvArgs As Variant) As Object
&apos;&apos;&apos; Create a new instance of the SF_ContextMenu class
&apos;&apos;&apos; Args:
&apos;&apos;&apos; Component: the document&apos;s component requesting a context menu
&apos;&apos;&apos; ContextMenuName: a private:resource/popupmenu/... reference
&apos;&apos;&apos; SubmenuChar: Delimiter used in menu trees
&apos;&apos;&apos; Returns: the instance or Nothing
Dim oMenu As Object &apos; Return value
Dim Component As Object &apos; The document&apos;s component requesting a context menu
Dim ContextMenuName As String &apos; A &quot;private:resource/popupmenu/...&quot; reference
Dim SubmenuChar As String &apos; Delimiter in menu trees
If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
Set oMenu = Nothing
Check:
&apos; Get arguments, their check has been done upstream
If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
If Not IsArray(pvArgs) Then pvArgs = Array(pvArgs)
If UBound(pvArgs) &gt;= 0 Then Set Component = pvArgs(0) Else Set Component = Nothing
If UBound(pvArgs) &gt;= 1 Then ContextMenuName = pvArgs(1) Else ContextMenuName = &quot;&quot;
If UBound(pvArgs) &gt;= 2 Then SubmenuChar = pvArgs(2) Else SubmenuChar = &quot;&gt;&quot;
Try:
If Not IsNull(Component) Then
Set oMenu = New SF_ContextMenu
With oMenu
Set .[Me] = oMenu
._Initialize(Component, ContextMenuName, SubmenuChar)
End With
Else
Set oMenu = Nothing
End If
Finally:
Set _NewContextMenu = oMenu
Exit Function
Catch:
GoTo Finally
End Function &apos; SFWidgets.SF_Register._NewContextMenu
REM -----------------------------------------------------------------------------
Public Function _NewMenu(Optional ByVal pvArgs As Variant) As Object
&apos;&apos;&apos; Create a new instance of the SF_Menu class

View file

@ -48,6 +48,12 @@ Option Explicit
&apos;&apos;&apos; Dim oCalc As Object, oToolbar As Object
&apos;&apos;&apos; Set oCalc = CreateScriptService(&quot;Calc&quot;, &quot;myFile.ods&quot;)
&apos;&apos;&apos; Set oToolbar = oCalc.Toolbars(&quot;findbar&quot;)
&apos;&apos;&apos;
&apos;&apos;&apos;
&apos;&apos;&apos; Detailed user documentation:
&apos;&apos;&apos; https://help.libreoffice.org/latest/en-US/text/sbasic/shared/03/toolbar.html?DbPAR=BASIC
&apos;&apos;&apos;
&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
REM ================================================================== EXCEPTIONS

View file

@ -34,6 +34,12 @@ Option Explicit
&apos;&apos;&apos; Set oCalc = CreateScriptService(&quot;Calc&quot;, &quot;myFile.ods&quot;)
&apos;&apos;&apos; Set oToolbar = oCalc.Toolbars(&quot;findbar&quot;)
&apos;&apos;&apos; Set oToolbarButton = oToolbar.ToolbarButtons(&quot;Find Next&quot;)
&apos;&apos;&apos;
&apos;&apos;&apos;
&apos;&apos;&apos; Detailed user documentation:
&apos;&apos;&apos; https://help.libreoffice.org/latest/en-US/text/sbasic/shared/03/toolbarcontrol.html?DbPAR=BASIC
&apos;&apos;&apos;
&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
REM ================================================================== EXCEPTIONS

View file

@ -8,4 +8,5 @@
<library:element library:name="SF_MenuListener"/>
<library:element library:name="SF_Toolbar"/>
<library:element library:name="SF_ToolbarButton"/>
<library:element library:name="SF_ContextMenu"/>
</library:library>