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