1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
<?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_MenuListener" 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 Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' SF_MenuListener
''' ===============
''' The current module is dedicated to the management of menu events + listeners, triggered by user actions,
''' which cannot be defined with the Basic IDE
'''
''' Concerned listeners:
''' com.sun.star.awt.XMenuListener
''' allowing a user to select a menu command in user menus preset in the menubar
'''
''' The described events/listeners are processed by UNO listeners
'''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
REM ============================================================= PRIVATE MEMBERS
Dim MenuListener As Object ' com.sun.star.awt.XMenuListener
REM =========================================================== PRIVATE CONSTANTS
Private Const _MenuListenerPrefix = "_SFMENU_"
Private Const _MenuListener = "com.sun.star.awt.XMenuListener"
Private Const cstUnoPrefix = ".uno:"
Private Const cstScriptArg = ":::"
REM ================================================================== EXCEPTIONS
REM ============================================================== PUBLIC METHODS
REM -----------------------------------------------------------------------------
Public Sub SetMenuListener(poSubmenu As Object)
''' Arm a menu listener on a submenu
''' Args:
''' poSubmenu: the targeted submenu
Try:
If IsNull(MenuListener) Then Set MenuListener = CreateUnoListener(_MenuListenerPrefix, _MenuListener)
poSubmenu.addMenuListener(MenuListener)
Finally:
Exit Sub
End Sub ' SFWidgets.SF_MenuListener.SetMenuListener
REM ============================================================= PRIVATE METHODS
REM -----------------------------------------------------------------------------
Sub _SFMENU_itemSelected(Optional poEvent As Object) ' com.sun.star.awt.MenuEvent
''' Execute the command or the script associated with the actually selected item
''' When a script, next argument is provided:
''' a comma-separated string with 4 components
''' - the menu header
''' - the name of the selected menu entry (without tilde "~")
''' - the numeric identifier of the selected menu entry
''' - the new status of the selected menu entry ("0" or "1"). Always "0" for usual items.
Dim iMenuId As Integer
Dim oMenu As Object ' stardiv.Toolkit.VCLXPopupMenu
Dim sCommand As String ' Command associated with menu entry
Dim bType As Boolean ' True when status is meaningful: item is radio button or checkbox
Dim bStatus As Boolean ' Status of the menu item, always False for normal items
Dim oFrame As Object ' com.sun.star.comp.framework.Frame
Dim oDispatcher As Object ' com.sun.star.frame.DispatchHelper
Dim oSession As Object ' SF_Session service
Dim vScript As Variant ' Split command in script/argument
Dim oArgs() As new com.sun.star.beans.PropertyValue
On Local Error GoTo Catch ' Avoid stopping event scripts
Try:
iMenuId = poEvent.MenuId
oMenu = poEvent.Source
With oMenu
' Collect command (script or menu command) and status radiobuttons and checkboxes
sCommand = .getCommand(iMenuId)
bStatus = .isItemChecked(iMenuId)
End With
If Len(sCommand) > 0 Then
If Left(sCommand, Len(cstUnoPrefix)) = cstUnoPrefix Then
' Execute uno command
Set oFrame = StarDesktop.CurrentComponent.CurrentController.Frame ' A menu has been clicked necessarily in the current window
Set oDispatcher = ScriptForge.SF_Utils._GetUNOService("DispatchHelper")
oDispatcher.executeDispatch(oFrame, sCommand, "", 0, oArgs())
oFrame.activate()
Else
' Execute script
Set oSession = ScriptForge.SF_Services.CreateScriptService("Session")
vScript = Split(sCommand, cstScriptArg)
oSession._ExecuteScript(vScript(0), vScript(1) & "," & Iif(bStatus, "1", "0")) ' Return value is ignored
End If
End If
Finally:
Exit Sub
Catch:
GoTo Finally
End Sub ' SFWidgets.SF_MenuListener._SFMENU_itemSelected
REM -----------------------------------------------------------------------------
Sub _SFMENU_itemHighlighted(Optional poEvent As Object) ' com.sun.star.awt.MenuEvent
Exit Sub
End Sub ' SFWidgets.SF_MenuListener._SFMENU_itemHighlighted
Sub _SFMENU_itemActivated(Optional poEvent As Object) ' com.sun.star.awt.MenuEvent
Exit Sub
End Sub ' SFWidgets.SF_MenuListener._SFMENU_itemActivated
Sub _SFMENU_itemDeactivated(Optional poEvent As Object) ' com.sun.star.awt.MenuEvent
Exit Sub
End Sub ' SFWidgets.SF_MenuListener._SFMENU_itemDeactivated
Sub _SFMENU_disposing(Optional poEvent As Object) ' com.sun.star.awt.MenuEvent
Exit Sub
End Sub ' SFWidgets.SF_MenuListener._SFMENU_disposing
REM ============================================ END OF SFDIALOGS.SF_DIALOGLISTENER
</script:module>
|