Tobias Krais wrote:
Hi together,
since some weeks I'm developing a little OpenOffice connector for a
program we develeped (called JUDAS: www.judas.de). At the moment I try
to add a menu called JUDAS to the menu bar at runtime. I found how to
disable menus at runtime, but not how to add them.
You may ask why is it necessary that the menus should be added at
runtime. The menus should appear only if OpenOffice was started by
JUDAS. If the user starts OpenOffice by itself, the menu should not appear.
My question is: is it possible to generate menus at runtime? We are
using OOo2.
If yes, do you have examples or know where I should start studying?
Hi Tobias,
I think I answered this question on the mailing list api.openoffice.org
several times. I attached two small examples which modifies the menu bar
of an application module. If you only want to do temporary changes,
please use the transient example.
Regards,
Carsten
REM ***** BASIC *****
Sub Main
REM *** Adds a item to the File Menu only transient. Means
REM *** that this menu item only exists for and during the
REM *** lifetime of the current frame.
REM *** Initialize strings
sMenuBar = "private:resource/menubar/menubar"
sMyPopupMenuCmdId = ".uno:PickList"
sMyCommand = "macro:///Standard.Module1.Test()"
REM *** Retrieve the current frame of my model
oModel = ThisComponent
if not isNull( oModel ) then
REM *** Retrieve frame from current controller
oFrame = oModel.getCurrentController().getFrame()
REM *** Retrieve the layout manager of my current frame
oLayoutManager = oFrame.LayoutManager()
REM *** Retrieve the menu bar from the layout manager
oMenuBar = oLayoutManager.getElement( sMenuBar )
REM *** Retrieve writable configuration settings from menu bar
oMenuBarSettings = oMenuBar.getSettings( true )
REM *** Make our changes only transient. An Add-on should
REM *** never change configuration persistently as it can
REM *** be deinstalled by a user without any chance to
REM *** undo its configuration changes!
REM *** Please look for bug #i46194 which prevents using
REM *** oMenuBar.Persistent = false!!
oMenuBar.Persistent = false
REM *** Look for the "File" popup menu and add our command
REM *** We must look if we haven't added our command already!
fileMenuIndex = FindPopupMenu( sMyPopupMenuCmdId, oMenuBarSettings )
if fileMenuIndex >= 0 then
oPopupMenuItem() = oMenuBarSettings.getByIndex(fileMenuIndex)
oPopupMenu = GetProperty( "ItemDescriptorContainer", oPopupMenuItem() )
if not isNull( oPopupMenu ) then
if FindCommand( sMyCommand, oPopupMenu ) = -1 then
oMenuItem = CreateMenuItem( sMyCommand, "Standard.Module1.Test" )
nCount = oPopupMenu.getCount()
oPopupMenu.insertByIndex( nCount, oMenuItem )
endif
endif
else
msgbox "No file menu found!"
endif
oMenuBar.setSettings( oMenuBarSettings )
endif
End Sub
Function FindCommand( Command as String, oPopupMenu as Object ) as Integer
nCount = oPopupMenu.getCount()-1
for i = 0 to nCount
oMenuItem() = oPopupMenu.getByIndex(i)
nPropertyCount = ubound(oMenuItem())
for j = 0 to nPropertyCount
if oMenuItem(j).Name = "CommandURL" then
if oMenuItem(j).Value = Command then
FindCommand = j
exit function
endif
endif
next j
next i
FindCommand = -1
End Function
Function FindPopupMenu( Command as String, oMenuBarSettings as Object ) as
Integer
for i = 0 to oMenuBarSettings.getCount()-1
oPopupMenu() = oMenuBarSettings.getByIndex(i)
nPopupMenuCount = ubound(oPopupMenu())
for j = 0 to nPopupMenuCount
if oPopupMenu(j).Name = "CommandURL" then
if oPopupMenu(j).Value = Command then
FindPopupMenu = j
exit function
endif
endif
next j
next i
FindPopupMenu = -1
End Function
Function GetProperty( PropertyName as String, properties() as Variant ) as
Variant
for j = lbound( properties() ) to ubound( properties() )
oPropertyValue = properties(j)
if oPropertyValue.Name = PropertyName then
GetProperty = oPropertyValue.Value
exit function
endif
next j
GetProperty = null
end function
Function CreateMenuItem( Command as String, Label as String ) as Variant
Dim aMenuItem(2) as new com.sun.star.beans.PropertyValue
aMenuItem(0).Name = "CommandURL"
aMenuItem(0).Value = Command
aMenuItem(1).Name = "Label"
aMenuItem(1).Value = Label
aMenuItem(2).Name = "Type"
aMenuItem(2).Value = 0
CreateMenuItem = aMenuItem()
End Function
Sub Test
MsgBox "Test"
End Sub
REM ***** BASIC *****
Sub Main
REM *** Creates a top-level popup menu on the Writer menu bar
persistently.
REM *** It checks if its popup menu has already been added to the menu
bar
REM *** and does nothing.
REM *** The popup menu contains one menu item with a
REM *** Initialize strings
sMenuBar = "private:resource/menubar/menubar"
sMyPopupMenuCmdId = "vnd.openoffice.org:MyMenu"
REM *** Retrieve the module configuration manager from central module
configuration manager supplier
oModuleCfgMgrSupplier =
createUnoService("com.sun.star.ui.ModuleUIConfigurationManagerSupplier")
REM *** Retrieve the module configuration manager with module identifier
REM *** See com.sun.star.frame.ModuleManager for more information
oModuleCfgMgr = oModuleCfgMgrSupplier.getUIConfigurationManager(
"com.sun.star.text.TextDocument" )
oMenuBarSettings = oModuleCfgMgr.getSettings( sMenuBar, true )
REM *** Look for our top-level popup menu. Can be identified by the
CommandURL property.
bHasAlreadyPopupMenu = false
nCount = oMenuBarSettings.getCount()
for i = 0 to nCount-1
oPopupMenu() = oMenuBarSettings.getByIndex( i )
nPopupMenuCount = ubound(oPopupMenu())
for j = 0 to nPopupMenuCount
if oPopupMenu(j).Name = "CommandURL" then
if oPopupMenu(j).Value = sMyPopupMenuCmdId then
bHasAlreadyPopupMenu = true
end if
endif
next j
next i
MsgBox bHasAlreadyPopupMenu
if not bHasAlreadyPopupMenu then
sString = "My Macro's"
oPopupMenu = CreatePopupMenu( sMyPopupMenuCmdId, sString,
oMenuBarSettings )
oPopupMenuContainer = oPopupMenu(3).Value
oMenuItem = CreateMenuItem( "macro:///Standard.Module1.Test()",
"Standard.Module1.Test" )
oPopupMenuContainer.insertByIndex( 0, oMenuItem )
oMenuBarSettings.insertByIndex( nCount, oPopupMenu )
oModuleCfgMgr.replaceSettings( sMenuBar, oMenuBarSettings )
end if
End Sub
Function CreatePopupMenu( CommandId, Label, Factory ) as Variant
Dim aPopupMenu(3) as new com.sun.star.beans.PropertyValue
aPopupMenu(0).Name = "CommandURL"
aPopupMenu(0).Value = CommandId
aPopupMenu(1).Name = "Label"
aPopupMenu(1).Value = Label
aPopupMenu(2).Name = "Type"
aPopupMenu(2).Value = 0
aPopupMenu(3).Name = "ItemDescriptorContainer"
aPopupMenu(3).Value = Factory.createInstanceWithContext(
GetDefaultContext() )
CreatePopupMenu = aPopupMenu()
End Function
Sub ResetMenuBar
sMenuBar = "private:resource/menubar/menubar"
REM *** Retrieve the module configuration manager from central module
configuration manager supplier
oModuleCfgMgrSupplier =
createUnoService("com.sun.star.ui.ModuleUIConfigurationManagerSupplier")
REM *** Retrieve the module configuration manager with module identifier
REM *** See com.sun.star.frame.ModuleManager for more information
oModuleCfgMgr = oModuleCfgMgrSupplier.getUIConfigurationManager(
"com.sun.star.text.TextDocument" )
REM *** Reset default settings
oMenuBarSettings = oModuleCfgMgr.getDefaultSettings( sMenuBar )
oModuleCfgMgr.replaceSettings( oMenuBarSettings )
End Sub
Function CreateMenuItem( Command as String, Label as String ) as Variant
Dim aMenuItem(2) as new com.sun.star.beans.PropertyValue
aMenuItem(0).Name = "CommandURL"
aMenuItem(0).Value = Command
aMenuItem(1).Name = "Label"
aMenuItem(1).Value = Label
aMenuItem(2).Name = "Type"
aMenuItem(2).Value = 0
CreateMenuItem = aMenuItem()
End Function
Sub Test
MsgBox "Test"
End Sub
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]