1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"SF_MenuListener" script:
language=
"StarBasic" script:
moduleType=
"normal">REM =======================================================================================================================
4 REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
5 REM === The SFWidgets library is one of the associated libraries. ===
6 REM === Full documentation is available on https://help.libreoffice.org/ ===
7 REM =======================================================================================================================
12 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
13 ''' SF_MenuListener
14 ''' ===============
15 ''' The current module is dedicated to the management of menu events + listeners, triggered by user actions,
16 ''' which cannot be defined with the Basic IDE
18 ''' Concerned listeners:
19 ''' com.sun.star.awt.XMenuListener
20 ''' allowing a user to select a menu command in user menus preset in the menubar
22 ''' The described events/listeners are processed by UNO listeners
24 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
26 REM ============================================================= PRIVATE MEMBERS
28 Dim MenuListener As Object
' com.sun.star.awt.XMenuListener
30 REM =========================================================== PRIVATE CONSTANTS
32 Private Const _MenuListenerPrefix =
"_SFMENU_
"
33 Private Const _MenuListener =
"com.sun.star.awt.XMenuListener
"
34 Private Const cstUnoPrefix =
".uno:
"
35 Private Const cstScriptArg =
":::
"
37 REM ================================================================== EXCEPTIONS
39 REM ============================================================== PUBLIC METHODS
41 REM -----------------------------------------------------------------------------
42 Public Sub SetMenuListener(poSubmenu As Object)
43 ''' Arm a menu listener on a submenu
44 ''' Args:
45 ''' poSubmenu: the targeted submenu
48 If IsNull(MenuListener) Then Set MenuListener = CreateUnoListener(_MenuListenerPrefix, _MenuListener)
49 poSubmenu.addMenuListener(MenuListener)
53 End Sub
' SFWidgets.SF_MenuListener.SetMenuListener
55 REM ============================================================= PRIVATE METHODS
57 REM -----------------------------------------------------------------------------
58 Sub _SFMENU_itemSelected(Optional poEvent As Object)
' com.sun.star.awt.MenuEvent
59 ''' Execute the command or the script associated with the actually selected item
60 ''' When a script, next argument is provided:
61 ''' a comma-separated string with
4 components
62 ''' - the menu header
63 ''' - the name of the selected menu entry (without tilde
"~
")
64 ''' - the numeric identifier of the selected menu entry
65 ''' - the new status of the selected menu entry (
"0" or
"1"). Always
"0" for usual items.
67 Dim iMenuId As Integer
68 Dim oMenu As Object
' stardiv.Toolkit.VCLXPopupMenu
69 Dim sCommand As String
' Command associated with menu entry
70 Dim bType As Boolean
' True when status is meaningful: item is radio button or checkbox
71 Dim bStatus As Boolean
' Status of the menu item, always False for normal items
72 Dim oFrame As Object
' com.sun.star.comp.framework.Frame
73 Dim oDispatcher As Object
' com.sun.star.frame.DispatchHelper
74 Dim vScript As Variant
' Split command in script/argument
75 Dim oSession As Object : Set oSession = ScriptForge.SF_Services.CreateScriptService(
"ScriptForge.Session
")
76 Dim oArgs() As new com.sun.star.beans.PropertyValue
78 On Local Error GoTo Catch
' Avoid stopping event scripts
81 iMenuId = poEvent.MenuId
82 oMenu = poEvent.Source
85 ' Collect command (script or menu command) and status radiobuttons and checkboxes
86 sCommand = .getCommand(iMenuId)
87 bStatus = .isItemChecked(iMenuId)
90 If Len(sCommand)
> 0 Then
91 ' A menu has been clicked necessarily in the current window (Document) or one of its subcomponents (FormDocument)
92 Set oFrame = StarDesktop.ActiveFrame
93 If oFrame.Frames.Count
> 0 Then Set oFrame = oFrame.getActiveFrame()
94 ' Command or script ?
95 If Left(sCommand, Len(cstUnoPrefix)) = cstUnoPrefix Then
96 ' Execute uno command
97 Set oDispatcher = ScriptForge.SF_Utils._GetUNOService(
"DispatchHelper
")
98 oDispatcher.executeDispatch(oFrame, sCommand,
"",
0, oArgs())
101 ' Execute script
102 vScript = Split(sCommand, cstScriptArg)
103 oSession._ExecuteScript(vScript(
0), vScript(
1)
& ",
" & Iif(bStatus,
"1",
"0"))
' Return value is ignored
111 End Sub
' SFWidgets.SF_MenuListener._SFMENU_itemSelected
113 REM -----------------------------------------------------------------------------
114 Sub _SFMENU_itemHighlighted(Optional poEvent As Object)
' com.sun.star.awt.MenuEvent
116 End Sub
' SFWidgets.SF_MenuListener._SFMENU_itemHighlighted
118 Sub _SFMENU_itemActivated(Optional poEvent As Object)
' com.sun.star.awt.MenuEvent
120 End Sub
' SFWidgets.SF_MenuListener._SFMENU_itemActivated
122 Sub _SFMENU_itemDeactivated(Optional poEvent As Object)
' com.sun.star.awt.MenuEvent
124 End Sub
' SFWidgets.SF_MenuListener._SFMENU_itemDeactivated
126 Sub _SFMENU_disposing(Optional poEvent As Object)
' com.sun.star.awt.MenuEvent
128 End Sub
' SFWidgets.SF_MenuListener._SFMENU_disposing
130 REM ============================================ END OF SFWIDGETS.SF_MENULISTENER