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_PopupMenu" 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 =======================================================================================================================
14 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
15 ''' SF_PopupMenu
16 ''' ============
17 ''' Display a popup menu anywhere and any time
19 ''' A popup menu is usually triggered by a mouse action (typically a right-click) on a dialog, a form
20 ''' or one of their controls. In this case the menu will be displayed below the clicked area.
21 ''' When triggered by other events, including in the normal flow of a user script, the script should
22 ''' provide the coordinates of the topleft edge of the menu versus the actual component.
24 ''' The menu is described from top to bottom. Each menu item receives a numeric and a string identifier.
25 ''' The Execute() method returns the item selected by the user.
27 ''' Menu items are either:
28 ''' - usual items
29 ''' - checkboxes
30 ''' - radio buttons
31 ''' - a menu separator
32 ''' Menu items can be decorated with icons and tooltips.
34 ''' Definitions:
35 ''' SubmenuCharacter: the character or the character string that identifies how menus are cascading
36 ''' Default =
">"
37 ''' Can be set when invoking the PopupMenu service
38 ''' ShortcutCharacter: the underline access key character
39 ''' Default =
"~
"
41 ''' Service invocation:
42 ''' Sub OpenMenu(Optional poMouseEvent As Object)
43 ''' Dim myMenu As Object
44 ''' Set myMenu = CreateScriptService(
"SFWidgets.PopupMenu
", poMouseEvent, , ,
">>")
' Usual case
45 ''' ' or
46 ''' Set myMenu = CreateScriptService(
"SFWidgets.PopupMenu
", , X, Y,
" |
")
' Use X and Y coordinates to place the menu
48 ''' Menus and submenus
49 ''' To create a popup menu with submenus, use the character defined in the
50 ''' SubmenuCharacter property while creating the menu entry to define where it will be
51 ''' placed. For instance, consider the following menu/submenu hierarchy.
52 ''' Item A
53 ''' Item B
> Item B
.1
54 ''' Item B
.2
55 ''' ------ (line separator)
56 ''' Item C
> Item C
.1 > Item C
.1.1
57 ''' Item C
.1.2
58 ''' Item C
> Item C
.2 > Item C
.2.1
59 ''' Item C
.2.2
60 ''' Next code will create the menu/submenu hierarchy
61 ''' With myMenu
62 ''' .AddItem(
"Item A
")
63 ''' .AddItem(
"Item B
>Item B
.1")
64 ''' .AddItem(
"Item B
>Item B
.2")
65 ''' .AddItem(
"---
")
66 ''' .AddItem(
"Item C
>Item C
.1>Item C
.1.1")
67 ''' .AddItem(
"Item C
>Item C
.1>Item C
.1.2")
68 ''' .AddItem(
"Item C
>Item C
.2>Item C
.2.1")
69 ''' .AddItem(
"Item C
>Item C
.2>Item C
.2.2")
70 ''' End With
72 ''' Example
1: simulate a subset of the View menu in the menubar of the Basic IDE
73 ''' Sub OpenMenu(Optional poMouseEvent As Object)
74 ''' Dim myMenu As Object, vChoice As Variant
75 ''' Set myMenu = CreateScriptService(
"SFWidgets.PopupMenu
", poMouseEvent)
76 ''' With myMenu
77 ''' .AddCheckBox(
"View
>Toolbars
>Dialog
")
78 ''' .AddCheckBox(
"View
>Toolbars
>Find
", Status := True)
79 ''' .AddCheckBox(
"View
>Status Bar
", Status := True)
80 ''' .AddItem(
"View
>Full Screen
", Name :=
"FULLSCREEN
")
81 ''' vChoice = .Execute(False)
' When
1st checkbox is clicked, return
"Dialog
"
82 ''' ' When last item is clicked, return
"FULLSCREEN
"
83 ''' .Dispose()
84 ''' End With
86 ''' Example
2: jump to another sheet of a Calc document
87 ''' ' Link next Sub to the
"Mouse button released
" event of a form control of a Calc sheet
88 ''' Sub JumpToSheet(Optional poEvent As Object)
89 ''' Dim myMenu As Object, sChoice As String, myDoc As Object, vSheets As Variant, sSheet As String
90 ''' Set myMenu = CreateScriptService(
"SFWidgets.PopupMenu
", poEvent)
91 ''' Set myDoc = CreateScriptService(
"Calc
", ThisComponent)
92 ''' vSheets = myDoc.Sheets
93 ''' For Each sSheet In vSheets
94 ''' myMenu.AddItem(sSheet)
95 ''' Next sSheet
96 ''' sChoice = myMenu.Execute(False)
' Return sheet name, not sheet index
97 ''' If sChoice
<> "" Then myDoc.Activate(sChoice)
98 ''' myDoc.Dispose()
99 ''' myMenu.Dispose()
100 ''' End Sub
103 ''' Detailed user documentation:
104 ''' https://help.libreoffice.org/latest/en-US/text/sbasic/shared/
03/sf_popupmenu.html?DbPAR=BASIC
106 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
108 REM ================================================================== EXCEPTIONS
110 REM ============================================================= PRIVATE MEMBERS
112 Private [Me] As Object
113 Private ObjectType As String
' Must be POPUPMENU
114 Private ServiceName As String
117 ' Menu descriptors
118 Private MenuTree As Variant
' Dictionary treename - XPopupMenu pair
119 Private MenuIdentification As Variant
' Dictionary item ID - item name
120 Private SubmenuChar As String
' Delimiter in menu trees
121 Private MenuRoot As Object
' stardiv.vcl.PopupMenu or com.sun.star.awt.XPopupMenu
122 Private LastItem As Integer
' Every item has its entry number. This is the last one
123 Private Rectangle As Object
' com.sun.star.awt.Rectangle
124 Private PeerWindow As Object
' com.sun.star.awt.XWindowPeer
125 Private MenubarMenu As Boolean
' When True, the actual popup menu depends on a menubar item
127 REM ============================================================ MODULE CONSTANTS
129 Private Const _UnderlineAccessKeyChar =
"~
"
130 Private Const _DefaultSubmenuChar =
">"
131 Private Const _SeparatorChar =
"---
"
132 Private Const _IconsDirectory =
"private:graphicrepository/
" ' Refers to
<install folder
>/share/config/images_*.zip.
133 Private Const cstUnoPrefix =
".uno:
"
134 Private Const cstNormal =
"N
"
135 Private Const cstCheck =
"C
"
136 Private Const cstRadio =
"R
"
138 REM ====================================================== CONSTRUCTOR/DESTRUCTOR
140 REM -----------------------------------------------------------------------------
141 Private Sub Class_Initialize()
143 ObjectType =
"POPUPMENU
"
144 ServiceName =
"SFWidgets.PopupMenu
"
145 Set MenuTree = Nothing
146 Set MenuIdentification = Nothing
147 SubmenuChar = _DefaultSubmenuChar
148 Set MenuRoot = Nothing
150 Set Rectangle = Nothing
151 Set PeerWindow = Nothing
153 End Sub
' SFWidgets.SF_PopupMenu Constructor
155 REM -----------------------------------------------------------------------------
156 Private Sub Class_Terminate()
157 Call Class_Initialize()
158 End Sub
' SFWidgets.SF_PopupMenu Destructor
160 REM -----------------------------------------------------------------------------
161 Public Function Dispose() As Variant
162 If Not IsNull(MenuTree) Then Set MenuTree = MenuTree.Dispose()
163 If Not IsNull(MenuIdentification) Then Set MenuIdentification = MenuIdentification.Dispose()
164 Call Class_Terminate()
165 Set Dispose = Nothing
166 End Function
' SFWidgets.SF_PopupMenu Explicit Destructor
168 REM ================================================================== PROPERTIES
170 REM -----------------------------------------------------------------------------
171 Property Get ShortcutCharacter() As Variant
172 ''' The ShortcutCharacter property specifies character preceding the underline access key
173 ShortcutCharacter = _PropertyGet(
"ShortcutCharacter
")
174 End Property
' SFWidgets.SF_PopupMenu.ShortcutCharacter (get)
176 REM -----------------------------------------------------------------------------
177 Property Get SubmenuCharacter() As Variant
178 ''' The SubmenuCharacter property specifies the character string indicating
179 ''' a sub-menu in a popup menu item
180 SubmenuCharacter = _PropertyGet(
"SubmenuCharacter
")
181 End Property
' SFWidgets.SF_PopupMenu.SubmenuCharacter (get)
183 REM ===================================================================== METHODS
185 REM -----------------------------------------------------------------------------
186 Public Function AddCheckBox(Optional ByVal MenuItem As Variant _
187 , Optional ByVal Name As Variant _
188 , Optional ByVal Status As Variant _
189 , Optional ByVal Icon As Variant _
190 , Optional ByVal Tooltip As Variant _
192 ''' Insert in the popup menu a new entry
193 ''' Args:
194 ''' MenuItem: The text to be displayed in the menu entry.
195 ''' It determines also the hierarchy of the popup menu
196 ''' It is made up of all the components (separated by the
"SubmenuCharacter
") of the menu branch
197 ''' Example: A
>B
>C means
"C
" is a new entry in submenu
"A =
> B =
>"
198 ''' If the last component is equal to the
"SeparatorCharacter
", a line separator is inserted
199 ''' Name: The name to be returned by the Execute() method if this item is clicked
200 ''' Default = the last component of MenuItem
201 ''' Status: when True the item is selected. Default = False
202 ''' Icon: The path name of the icon to be displayed, without leading path separator
203 ''' The icons are stored in one of the
<install folder
>/share/config/images_*.zip files
204 ''' The exact file depends on the user options about the current icon set
205 ''' Use the (normal) slash
"/
" as path separator
206 ''' Example:
"cmd/sc_cut.png
"
207 ''' Tooltip: The help text to be displayed as a tooltip
208 ''' Returns:
209 ''' The numeric identification of the newly inserted item
210 ''' Examples:
211 ''' Dim myMenu As Object, iId As Integer
212 ''' Set myMenu = CreateScriptService(
"SFWidgets.PopupMenu
", poEvent)
213 ''' iId = myMenu.AddCheckBox(
"Menu top
>Checkbox item
", Status := True)
215 Dim iId As Integer
' Return value
217 Const cstThisSub =
"SFWidgets.PopupMenu.AddCheckBox
"
218 Const cstSubArgs =
"MenuItem, [Name=
""""], [Status=False], [Icon=
""""], [Tooltip=
""""]
"
220 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
224 If IsMissing(Name) Or IsEmpty(Name) Then Name =
""
225 If IsMissing(Status) Or IsEmpty(Status) Then Status = False
226 If IsMissing(Icon) Or IsEmpty(Icon) Then Icon =
""
227 If IsMissing(Tooltip) Or IsEmpty(Tooltip) Then Tooltip =
""
228 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
229 If Not ScriptForge.SF_Utils._Validate(MenuItem,
"MenuItem
", V_STRING) Then GoTo Catch
230 If Not ScriptForge.SF_Utils._Validate(Name,
"Name
", V_STRING) Then GoTo Catch
231 If Not ScriptForge.SF_Utils._Validate(Status,
"Status
", ScriptForge.V_BOOLEAN) Then GoTo Catch
232 If Not ScriptForge.SF_Utils._Validate(Icon,
"Icon
", V_STRING) Then GoTo Catch
233 If Not ScriptForge.SF_Utils._Validate(Tooltip,
"Tooltip
", V_STRING) Then GoTo Catch
237 iId = _AddItem(MenuItem, Name, cstCheck, Status, Icon, Tooltip)
241 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
245 End Function
' SFWidgets.SF_PopupMenu.AddCheckBox
247 REM -----------------------------------------------------------------------------
248 Public Function AddItem(Optional ByVal MenuItem As Variant _
249 , Optional ByVal Name As Variant _
250 , Optional ByVal Icon As Variant _
251 , Optional ByVal Tooltip As Variant _
253 ''' Insert in the popup menu a new entry
254 ''' Args:
255 ''' MenuItem: The text to be displayed in the menu entry.
256 ''' It determines also the hierarchy of the popup menu
257 ''' It is made up of all the components (separated by the
"SubmenuCharacter
") of the menu branch
258 ''' Example: A
>B
>C means
"C
" is a new entry in submenu
"A =
> B =
>"
259 ''' If the last component is equal to
"---
", a line separator is inserted and all other arguments are ignored
260 ''' Name: The name to be returned by the Execute() method if this item is clicked
261 ''' Default = the last component of MenuItem
262 ''' Icon: The path name of the icon to be displayed, without leading path separator
263 ''' The icons are stored in one of the
<install folder
>/share/config/images_*.zip files
264 ''' The exact file depends on the user options about the current icon set
265 ''' Use the (normal) slash
"/
" as path separator
266 ''' Example:
"cmd/sc_cut.png
"
267 ''' Tooltip: The help text to be displayed as a tooltip
268 ''' Returns:
269 ''' The numeric identification of the newly inserted item
270 ''' Examples:
271 ''' Dim myMenu As Object, iId As Integer
272 ''' Set myMenu = CreateScriptService(
"SFWidgets.PopupMenu
", poEvent)
273 ''' iId = myMenu.AddItem(
"Menu top
>Normal item
", Icon :=
"cmd.sc_cut.png
")
275 Dim iId As Integer
' Return value
277 Const cstThisSub =
"SFWidgets.PopupMenu.AddItem
"
278 Const cstSubArgs =
"MenuItem, [Name=
""""], [Icon=
""""], [Tooltip=
""""]
"
280 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
284 If IsMissing(Name) Or IsEmpty(Name) Then Name =
""
285 If IsMissing(Icon) Or IsEmpty(Icon) Then Icon =
""
286 If IsMissing(Tooltip) Or IsEmpty(Tooltip) Then Tooltip =
""
287 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
288 If Not ScriptForge.SF_Utils._Validate(MenuItem,
"MenuItem
", V_STRING) Then GoTo Catch
289 If Not ScriptForge.SF_Utils._Validate(Name,
"Name
", V_STRING) Then GoTo Catch
290 If Not ScriptForge.SF_Utils._Validate(Icon,
"Icon
", V_STRING) Then GoTo Catch
291 If Not ScriptForge.SF_Utils._Validate(Tooltip,
"Tooltip
", V_STRING) Then GoTo Catch
295 iId = _AddItem(MenuItem, Name, cstNormal, False, Icon, Tooltip)
299 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
303 End Function
' SFWidgets.SF_PopupMenu.AddItem
305 REM -----------------------------------------------------------------------------
306 Public Function AddRadioButton(Optional ByVal MenuItem As Variant _
307 , Optional ByVal Name As Variant _
308 , Optional ByVal Status As Variant _
309 , Optional ByVal Icon As Variant _
310 , Optional ByVal Tooltip As Variant _
312 ''' Insert in the popup menu a new entry as a radio button
313 ''' Args:
314 ''' MenuItem: The text to be displayed in the menu entry.
315 ''' It determines also the hieAddCheckBoxrarchy of the popup menu
316 ''' It is made up of all the components (separated by the
"SubmenuCharacter
") of the menu branch
317 ''' Example: A
>B
>C means
"C
" is a new entry in submenu
"A =
> B =
>"
318 ''' If the last component is equal to the
"SeparatorCharacter
", a line separator is inserted
319 ''' Name: The name to be returned by the Execute() method if this item is clicked
320 ''' Default = the last component of MenuItem
321 ''' Status: when True the item is selected. Default = False
322 ''' Icon: The path name of the icon to be displayed, without leading path separator
323 ''' The icons are stored in one of the
<install folder
>/share/config/images_*.zip files
324 ''' The exact file depends on the user options about the current icon set
325 ''' Use the (normal) slash
"/
" as path separator
326 ''' Example:
"cmd/sc_cut.png
"
327 ''' Tooltip: The help text to be displayed as a tooltip
328 ''' Returns:
329 ''' The numeric identification of the newly inserted item
330 ''' Examples:
331 ''' Dim myMenu As Object, iId As Integer
332 ''' Set myMenu = CreateScriptService(
"SFWidgets.PopupMenu
", poEvent)
333 ''' iId = myMenu.AddRadioButton(
"Menu top
>Radio item
", Status := True)
335 Dim iId As Integer
' Return value
337 Const cstThisSub =
"SFWidgets.PopupMenu.AddRadioButton
"
338 Const cstSubArgs =
"MenuItem, [Name=
""""], [Status=False], [Icon=
""""], [Tooltip=
""""]
"
340 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
344 If IsMissing(Name) Or IsEmpty(Name) Then Name =
""
345 If IsMissing(Status) Or IsEmpty(Status) Then Status = False
346 If IsMissing(Icon) Or IsEmpty(Icon) Then Icon =
""
347 If IsMissing(Tooltip) Or IsEmpty(Tooltip) Then Tooltip =
""
348 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
349 If Not ScriptForge.SF_Utils._Validate(MenuItem,
"MenuItem
", V_STRING) Then GoTo Catch
350 If Not ScriptForge.SF_Utils._Validate(Name,
"Name
", V_STRING) Then GoTo Catch
351 If Not ScriptForge.SF_Utils._Validate(Status,
"Status
", ScriptForge.V_BOOLEAN) Then GoTo Catch
352 If Not ScriptForge.SF_Utils._Validate(Icon,
"Icon
", V_STRING) Then GoTo Catch
353 If Not ScriptForge.SF_Utils._Validate(Tooltip,
"Tooltip
", V_STRING) Then GoTo Catch
357 iId = _AddItem(MenuItem, Name, cstRadio, Status, Icon, Tooltip)
361 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
365 End Function
' SFWidgets.SF_PopupMenu.AddRadioButton
367 REM -----------------------------------------------------------------------------
368 Public Function Execute(Optional ByVal ReturnId As Variant) As Variant
369 ''' Display the popup menu and return the menu item clicked by the user
370 ''' Args:
371 ''' ReturnId: When True (default), return the unique ID of the clicked item, otherwise return its name
372 ''' Returns:
373 ''' The numeric identification of clicked item or its name
374 ''' The returned value is
0 or
"" (depending on ReturnId) when the menu is cancelled
375 ''' Examples:
376 ''' Sub OpenMenu(Optional poMouseEvent As Object)
377 ''' Dim myMenu As Object, vChoice As Variant
378 ''' Set myMenu = CreateScriptService(
"SFWidgets.PopupMenu
", poMouseEvent)
379 ''' With myMenu
380 ''' .AddCheckBox(
"View
>Toolbars
>Dialog
")
381 ''' .AddCheckBox(
"View
>Toolbars
>Find
", STatus := True)
382 ''' .AddCheckBox(
"View
>Status Bar
", STatus := True)
383 ''' .AddItem(
"View
>Full Screen
", Name :=
"FULLSCREEN
")
384 ''' vChoice = .Execute(False)
' When
1st checkbox is clicked, return
"Dialog
"
385 ''' ' When last item is clicked, return
"FULLSCREEN
"
386 ''' End With
388 Dim vMenuItem As Variant
' Return value
390 Const cstThisSub =
"SFWidgets.PopupMenu.Execute
"
391 Const cstSubArgs =
"[ReturnId=True]
"
393 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
397 If IsMissing(ReturnId) Or IsEmpty(ReturnId) Then ReturnId = True
398 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
399 If Not ScriptForge.SF_Utils._Validate(ReturnId,
"ReturnId
", ScriptForge.V_BOOLEAN) Then GoTo Catch
401 If Not ReturnId Then vMenuItem =
""
404 vMenuItem = MenuRoot.Execute(PeerWindow, Rectangle, com.sun.star.awt.PopupMenuDirection.EXECUTE_DEFAULT)
405 If Not ReturnId Then vMenuItem = MenuIdentification.Item(CStr(vMenuItem))
409 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
413 End Function
' SFWidgets.SF_PopupMenu.Execute
415 REM -----------------------------------------------------------------------------
416 Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
417 ''' Return the actual value of the given property
418 ''' Args:
419 ''' PropertyName: the name of the property as a string
420 ''' Returns:
421 ''' The actual value of the property
422 ''' If the property does not exist, returns Null
423 ''' Exceptions:
424 ''' see the exceptions of the individual properties
425 ''' Examples:
426 ''' myModel.GetProperty(
"MyProperty
")
428 Const cstThisSub =
"SFWidgets.PopupMenu.GetProperty
"
429 Const cstSubArgs =
""
431 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
435 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
436 If Not ScriptForge.SF_Utils._Validate(PropertyName,
"PropertyName
", V_STRING, Properties()) Then GoTo Catch
440 GetProperty = _PropertyGet(PropertyName)
443 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
447 End Function
' SFWidgets.SF_PopupMenu.GetProperty
449 REM -----------------------------------------------------------------------------
450 Public Function Methods() As Variant
451 ''' Return the list of public methods of the Model service as an array
454 "AddCheckBox
" _
455 ,
"AddItem
" _
456 ,
"AddRadioButton
" _
457 ,
"Execute
" _
460 End Function
' SFWidgets.SF_PopupMenu.Methods
462 REM -----------------------------------------------------------------------------
463 Public Function Properties() As Variant
464 ''' Return the list or properties of the Timer a.AddItem(
"B
>B1
")class as an array
466 Properties = Array( _
467 "ShortcutCharacter
" _
468 ,
"SubmenuCharacter
" _
471 End Function
' SFWidgets.SF_PopupMenu.Properties
473 REM -----------------------------------------------------------------------------
474 Public Function SetProperty(Optional ByVal PropertyName As Variant _
475 , Optional ByRef Value As Variant _
477 ''' Set a new value to the given property
478 ''' Args:
479 ''' PropertyName: the name of the property as a string
480 ''' Value: its new value
481 ''' Exceptions
482 ''' ARGUMENTERROR The property does not exist
484 Const cstThisSub =
"SFWidgets.PopupMenu.SetProperty
"
485 Const cstSubArgs =
"PropertyName, Value
"
487 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
491 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
492 If Not SF_Utils._Validate(PropertyName,
"PropertyName
", V_STRING, Properties()) Then GoTo Catch
496 SetProperty = _PropertySet(PropertyName, Value)
499 SF_Utils._ExitFunction(cstThisSub)
503 End Function
' SFWidgets.SF_PopupMenu.SetProperty
505 REM =========================================================== PRIVATE FUNCTIONS
507 REM -----------------------------------------------------------------------------
508 Public Function _AddItem(ByVal MenuItem As String _
509 , ByVal Name As String _
510 , ByVal ItemType As String _
511 , ByVal Status As Boolean _
512 , ByVal Icon As String _
513 , ByVal Tooltip As String _
514 , Optional ByVal Command As String _
516 ''' Insert in the popup menu a new entry
517 ''' Args:
518 ''' MenuItem: The text to be displayed in the menu entry.
519 ''' It determines also the hierarchy of the popup menu
520 ''' It is made up of all the components (separated by the
"SubmenuCharacter
") of the menu branch
521 ''' Example: A
>B
>C means
"C
" is a new entry in submenu
"A =
> B =
>"
522 ''' If the last component is equal to the
"SeparatorCharacter
", a line separator is inserted
523 ''' Name: The name to be returned by the Execute() method if this item is clicked
524 ''' Default = the last component of MenuItem
525 ''' ItemType:
"N
"(ormal,
"C
"(heck) or
"R
"(adio)
526 ''' Status: when True the item is selected
527 ''' Icon: The path name of the icon to be displayed, without leading path separator
528 ''' The icons are stored in one of the
<install folder
>/share/config/images_*.zip files
529 ''' The exact file depends on the user options about the current icon set
530 ''' Use the (normal) slash
"/
" as path separator
531 ''' Example:
"cmd/sc_cut.png
"
532 ''' Tooltip: The help text to be displayed as a tooltip
533 ''' Command: only for menubar menus
534 ''' Either a uo command like
".uno:About
"
535 ''' or a script to be run: script URI ::: string argument to be passed to the script
536 ''' Returns:
537 ''' The numeric identification of the newly inserted item
539 Dim iId As Integer
' Return value
540 Dim vSplit As Variant
' Split menu item
541 Dim sMenu As String
' Submenu where to attach the new item, as a string
542 Dim oMenu As Object
' Submenu where to attach the new item, as an object
543 Dim sName As String
' The text displayed in the menu box
544 Dim oImage As Object
' com.sun.star.graphic.XGraphic
545 Dim sCommand As String
' Alias of Command completed with arguments
546 Const cstCommandSep =
",
"
548 On Local Error GoTo Catch
550 If IsMissing(Command) Then Command =
""
553 ' Run through the upper menu tree
554 vSplit = _SplitMenuItem(MenuItem)
556 ' Create and determine the menu to which to attach the new item
558 Set oMenu = _GetPopupMenu(sMenu)
' Run through the upper menu tree and retain the last branch
560 ' Insert the new item
561 LastItem = LastItem +
1
565 If sName = _SeparatorChar Then
570 .insertItem(LastItem, sName,
0, -
1)
572 .insertItem(LastItem, sName, com.sun.star.awt.MenuItemStyle.CHECKABLE + com.sun.star.awt.MenuItemStyle.AUTOCHECK, -
1)
573 .checkItem(LastItem, Status)
575 .insertItem(LastItem, sName, com.sun.star.awt.MenuItemStyle.RADIOCHECK + com.sun.star.awt.MenuItemStyle.AUTOCHECK, -
1)
576 .checkItem(LastItem, Status)
579 ' Store the ID - Name relation
580 If Len(Name) =
0 Then Name = Replace(sName, _UnderlineAccessKeyChar,
"")
581 MenuIdentification.Add(CStr(LastItem), Name)
583 ' Add the icon when relevant
584 If Len(Icon)
> 0 Then
585 Set oImage = _GetImageFromUrl(_IconsDirectory
& Icon)
586 If Not IsNull(oImage) Then .setItemImage(LastItem, oImage, False)
589 ' Add the tooltip when relevant
590 If Len(Tooltip)
> 0 Then .setTipHelpText(LastItem, Tooltip)
592 ' Add the command: UNO command or script to run - menubar menus only
593 If Len(Command)
> 0 Then
594 If Left(Command, Len(cstUnoPrefix)) = cstUnoPrefix Then
597 sCommand = Command
& cstCommandSep
& Name
& cstCommandSep
& CStr(LastItem)
599 .setCommand(LastItem, sCommand)
611 End Function
' SFWidgets.SF_PopupMenu._AddItem
613 REM -----------------------------------------------------------------------------
614 Private Function _GetImageFromURL(ByVal psUrl as String) As Object
615 ''' Returns a com.sun.star.graphic.XGraphic instance based on the given URL
616 ''' The returned object is intended to be inserted as an icon in the popup menu
617 ''' Derived from
"Useful Macro Information For OpenOffice
" By Andrew Pitonyak
619 Dim vMediaProperties As Variant
' Array of com.sun.star.beans.PropertyValue
620 Dim oGraphicProvider As Object
' com.sun.star.graphic.GraphicProvider
621 Dim oImage As Object
' Return value
623 On Local Error GoTo Catch
' Ignore errors
627 ' Create graphic provider instance to load images from files.
628 Set oGraphicProvider = CreateUnoService(
"com.sun.star.graphic.GraphicProvider
")
630 ' Set the URL property so graphic provider is able to load the image
631 Set vMediaProperties = Array(ScriptForge.SF_Utils._MakePropertyValue(
"URL
", psURL))
633 ' Retrieve the com.sun.star.graphic.XGraphic instance
634 Set oImage = oGraphicProvider.queryGraphic(vMediaProperties)
637 Set _GetImageFromUrl = oImage
641 End Function
' SFWidgets.SF_PopupMenu._GetImageFromUrl
643 REM -----------------------------------------------------------------------------
644 Private Function _GetPopupMenu(ByVal psSubmenu As String) As Object
645 ''' Get the com.sun.star.awt.XPopupMenu object corresponding with the string in argument
646 ''' If the menu exists, it is found in the MenuTree dictionary
647 ''' If it does not exist, it is created recursively.
648 ''' Args:
649 ''' psSubmenu: a string like
"A
>B
"
650 ''' Returns
651 ''' A com.sun.star.awt.XpopupMenu object
652 ''' Example
653 ''' If psSubmenu =
"A
>B
>C
>D
", and only the root menu exists,
654 ''' -
"A
",
"A
>B
",
"A
>B
>C
",
"A
>B
>C
>D
" should be created
655 ''' - the popup menu corresponding with
"A
>B
>C
>D
" should be returned
657 Dim oPopup As Object
' Return value
658 Dim vSplit As Variant
' An array as returned by _SplitMenuItem()
659 Dim sMenu As String
' The left part of psSubmenu
660 Dim oMenu As Object
' com.sun.star.awt.XpopupMenu
661 Dim oLastMenu As Object
' com.sun.star.awt.XpopupMenu
665 Set oLastMenu = MenuRoot
667 If Len(psSubmenu) =
0 Then
' Menu starts at the root
668 Set oPopup = MenuRoot
669 ElseIf MenuTree.Exists(psSubmenu) Then
' Shortcut: if the submenu exists, get it directly
670 Set oPopup = MenuTree.Item(psSubmenu)
671 Else
' Build the tree
672 vSplit = Split(psSubmenu, SubmenuChar)
673 ' Search the successive submenus in the MenuTree dictionary, If not found, create a new entry
674 For i =
0 To UBound(vSplit)
675 sMenu = Join(ScriptForge.SF_Array.Slice(vSplit,
0, i), SubmenuChar)
676 If MenuTree.Exists(sMenu) Then
677 Set oLastMenu = MenuTree.Item(sMenu)
679 ' Insert the new menu tree item
680 LastItem = LastItem +
1
681 oLastMenu.insertItem(LastItem, vSplit(i),
0, -
1)
682 Set oMenu = CreateUnoService(
"stardiv.vcl.PopupMenu
")
683 If MenubarMenu Then SFWidgets.SF_MenuListener.SetMenuListener(oMenu)
684 MenuTree.Add(sMenu, oMenu)
685 oLastMenu.setPopupMenu(LastItem, oMenu)
686 Set oLastMenu = oMenu
689 Set oPopup = oLastMenu
693 Set _GetPopupMenu = oPopup
695 End Function
' SFWidgets.SF_PopupMenu._GetPopupMenu
697 REM -----------------------------------------------------------------------------
698 Public Sub _Initialize(ByRef poPeer As Object _
701 , psSubmenuChar As String _
703 ''' Complete the object creation process:
704 ''' - Initialize the dictionaries
705 ''' - initialize the root popup menu
706 ''' - initialize the display area
707 ''' - store the arguments for later use
708 ''' Args:
709 ''' poPeer: a peer window
710 ''' plXPos, plYPos: the coordinates
713 ' Initialize the dictionaries
714 With ScriptForge.SF_Services
715 Set MenuTree = .CreateScriptService(
"Dictionary
")
716 Set MenuIdentification = .CreateScriptService(
"Dictionary
")
719 ' Initialize the root of the menu tree
720 Set MenuRoot = CreateUnoService(
"stardiv.vcl.PopupMenu
")
722 ' Setup the display area
723 Set Rectangle = New com.sun.star.awt.Rectangle
727 ' Keep the targeted window
728 Set PeerWindow = poPeer
730 ' Store the submenu character
731 If Len(psSubmenuChar)
> 0 Then SubmenuChar = psSubmenuChar
735 End Sub
' SFWidgets.SF_PopupMenu._Initialize
737 REM -----------------------------------------------------------------------------
738 Private Function _PropertyGet(Optional ByVal psProperty As String) As Variant
739 ''' Return the value of the named property
740 ''' Args:
741 ''' psProperty: the name of the property
743 Dim vGet As Variant
' Return value
744 Dim cstThisSub As String
745 Const cstSubArgs =
""
747 cstThisSub =
"SFWidgets.PopupMenu.get
" & psProperty
748 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
750 ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
753 Select Case UCase(psProperty)
754 Case UCase(
"ShortcutCharacter
")
755 _PropertyGet = _UnderlineAccessKeyChar
756 Case UCase(
"SubmenuCharacter
")
757 _PropertyGet = SubmenuChar
763 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
767 End Function
' SFWidgets.SF_PopupMenu._PropertyGet
769 REM -----------------------------------------------------------------------------
770 Private Function _Repr() As String
771 ''' Convert the SF_PopupMenu instance to a readable string, typically for debugging purposes (DebugPrint ...)
772 ''' Args:
773 ''' Return:
774 ''' "[PopupMenu]: Name, Type (dialogname)
775 _Repr =
"[PopupMenu]:
" & SF_String.Represent(MenuTree.Keys())
& ",
" & SF_String.Represent(MenuIdentification.Items())
777 End Function
' SFWidgets.SF_PopupMenu._Repr
779 REM -----------------------------------------------------------------------------
780 Private Function _SplitMenuItem(ByVal psMenuItem As String ) As Variant
781 ''' Split a menu item given as a string and delimited by the submenu character
782 ''' Args:
783 ''' psMenuItem: a string like
"A
>B
>C
"
784 ''' Returns:
785 ''' An array: [
0] =
"A
>B
"
786 ''' [
1] =
"C
"
788 Dim vReturn(
0 To
1) As String
' Return value
789 Dim vMenus() As Variant
' Array of menus
792 vMenus = Split(psMenuItem, SubmenuChar)
793 vReturn(
1) = vMenus(UBound(vMenus))
794 vReturn(
0) = Left(psMenuItem, Len(psMenuItem) - Iif(UBound(vMenus)
> 0, Len(SubmenuChar),
0) - Len(vReturn(
1)))
797 _SplitMenuItem = vReturn
798 End Function
' SFWidgets.SF_PopupMenu._SplitMenuItem
800 REM ============================================ END OF SFWIDGETS.SF_POPUPMENU