Brian Raymond wrote:

Mathias Bauer wrote:


This requires the new menu configuration API of OOo2.0.


I took some sample code from an earlier mailing list post that was pointed
out to me on the forums (thanks DannyB) and modified it to add something to
the file menu.

I tried with both m79 and m87 and have some strange results. Since I don't
have a lot of data to reference I wanted to send this code out to see if
maybe I'm doing something wrong.

The desired result is for a single command to be added to the File menu

Actual result is the command is added however every time it is done another
complete file menu is added. So after 1 run there are two file menus, after
2 runs three etc. The command doesn't currently execute either.

Hi Brian,

Yes, the code works exactly how you have changed it. Your code changes the changed configuration again and again, that's why you can see more than one file menu (see the two insertByIndex).

The example you found and adapted is not the way you should change the menu bar in a context of an Add-on. An Add-on can be uninstalled and you have no chance to remove your menu bar changes! Currently there is a bug in our API that prevent one to change a menu bar non-persistent (see 46194), but I it is fixed now and will be in one of the next builds.

The example below uses therefor the persistent way, but can be easily changed when 46194 is fixed. Please keep in mind that the macro doesn't work directly in the Basic IDE due to oModel.getCurrentController(). You can add a button to a toolbar and start the macro with it.
Please use this example as a template for your add-on.


--- example code ---

REM  *****  BASIC  *****

Sub Main
  REM *** Adds a item to the File Menu
  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 uninstalled by a user without any chance to
REM *** undo its configuration changes!
REM *** ATTENTION:
REM *** Due to bug 46194 "oMenuBar.Persistent = false" doesn't
REM *** work. To have a working example we now use oMenuBar.Persistent = true.
REM *** This should be changed to false when 46194 is fixed!!!
oMenuBar.Persistent = true


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

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to