Kohei Yoshida wrote:
Hi Carsten,

On 10/16/06, Carsten Driesner <[EMAIL PROTECTED]> wrote:

Hi Kohei,

The following example code adds menu items to a Writer application
module persistently. You only have to adapt the code to use the Calc
module and translate it to your target language.
The example code adds a top-level popup menu to the Writer persistently.
[snip the code and other stuff]

Thanks for the code.  I just tested it to see how it works, and I see
that it indeed adds a persistent top-level menu.

Unfortunately that is not what I had in mind.  What I'd like to have
is a menu item that goes under Calc's top-level "Tools" menu just
below the "Goal Seek..." entry.  That's how it is integrated in the
cws (and ooo-build) at the moment, and it seems "right" to put it
where it is.

It's much easier to show a screenshot than describing it verbally, so
let me just post this link:

http://wiki.services.openoffice.org/wiki/Image:ScSolverMenu.png (just added)

As I understand it, it's not possible to insert such non-top-level
menu item via UNO call...(or is it?)   Would it be easy to extend UNO
to support this?

Hi Kohei,

It's possible to add your menu items anywhere you want. My example just adds a top-level menu, but you are free to add your menu items where you want. You have to adapt my example code, it just show you how you can use the API not a full solution. The following code adds a Basic macro below "Goal Seek..." in the tools menu. Please adapt it to your needs.

Sub AddMenuItemToToolsMenuPersistently
REM *** Creates a menu item inside the Tools popup menu on the Calc menu bar persistently.
        REM *** It checks if the menu item has already been added to the menu 
bar.

        REM *** Initialize strings
        sMenuBar = "private:resource/menubar/menubar"
        sToolsPopupMenuId = ".uno:ToolsMenu"
        sToolsMenuItemAddAfter = ".uno:GoalSeekDialog"
        sMyCommand = "macro:///Standard.Module1.Test()"
        sMyCommandLabel = "Basic Test"
        
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.sheet.SpreadsheetDocument" )
        oMenuBarSettings = oModuleCfgMgr.getSettings( sMenuBar, true )
        
    REM *** Look for the "Tools" popup menu and add our command
    REM *** We must look if we haven't added our command already!
    bMenuItemInserted = FALSE
    toolsMenuIndex = FindPopupMenu( sToolsPopupMenuId, oMenuBarSettings )
    if toolsMenuIndex >= 0 then
      oPopupMenuItem() = oMenuBarSettings.getByIndex(toolsMenuIndex)
oPopupMenu = GetProperty( "ItemDescriptorContainer", oPopupMenuItem() )
      if not isNull( oPopupMenu ) then
        REM *** Find Goal Seek... menu item as we want to add our menu item
        REM *** below it.
nGoalSeekMenuItemIndex = FindCommand( sToolsMenuItemAddAfter, oPopupMenu )
        if nGoalSeekMenuItemIndex >= 0 then
          REM *** Check that we don't add our command more than once
          nMyCmdIndex = FindCommand( sMyCommand, oPopupMenu )
          if nMyCmdIndex = -1 then
            oMenuItem = CreateMenuItem( sMyCommand, sMyCommandLabel )
            oPopupMenu.insertByIndex( nGoalSeekMenuItemIndex+1, oMenuItem )
            bMenuItemInserted = TRUE
          endif
        else
          msgbox "No goal seek menu item found!"
        endif
      endif
    else
      msgbox "No tools menu found!"
    endif

    if bMenuItemInserted then
      oModuleCfgMgr.replaceSettings( sMenuBar, oMenuBarSettings )
      oModuleCfgMgr.store()
    endif
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

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

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 = i
          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 = i
           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

Sub Test
        MsgBox "Test"
End Sub

Regards,
Carsten

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

Reply via email to