OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

dynamic menu´s in Writer

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
apeters
General User
General User


Joined: 04 Apr 2005
Posts: 47

PostPosted: Mon Apr 18, 2005 11:18 pm    Post subject: dynamic menu´s in Writer Reply with quote

Hello! For my external application that works with OpenOffice 2.0 BETA i want to add dynamic menus. The menus are only visible if my application is active. So i habe the following idea.

My application starts Writer and execute a macro that add a menu with two entrys. Every entry calls a other macro. And when my application close he call a other macro that makes the menus invidible. Thats the idea.

Now to the real part. i started the macro recorder and add the menus und klick on stop recording. then i saved the macro. And what he recorded was only this

Code:

sub Record
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
rem dispatcher.executeDispatch(document, ".uno:ConfigureDialog", "", 0, Array())


end sub

Question
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Apr 19, 2005 6:47 am    Post subject: Reply with quote

As you have discovered, the macro recorder next to useless. It has been this way since it was introduced.

The macro recorder only records dispatch operations. This means several things.
1. It is NOT a good example of how to learn to write macros, as it never uses the API, only the dispatch mechanism.
2. Since it only records dispatches, it only records that you want to invoke a certian dialog box, but not what you do inside of that dialog box.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
apeters
General User
General User


Joined: 04 Apr 2005
Posts: 47

PostPosted: Tue Apr 19, 2005 7:01 am    Post subject: Reply with quote

thats ugly
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Apr 19, 2005 7:10 am    Post subject: Reply with quote

My understanding is that OOo 2.0 has a new API to dynamically manipulate the menus and toolbars. I haven't looked into it yet.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
apeters
General User
General User


Joined: 04 Apr 2005
Posts: 47

PostPosted: Tue Apr 19, 2005 7:17 am    Post subject: Reply with quote

thats right, now you can add dynamicly Toolbars and Menus like MS Office. If you find related material for this please post here.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Apr 19, 2005 7:21 am    Post subject: Reply with quote

I haven't looked into it yet in the documentation. But you could consult the documentation. I do plan to look this up sometime, but there are always so many other things to do.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
apeters
General User
General User


Joined: 04 Apr 2005
Posts: 47

PostPosted: Wed Apr 20, 2005 1:54 am    Post subject: Reply with quote

Hello! I get an example from the Mailinglist.

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 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!! Is fixed on build
     REM *** SRC680m91 or newer.
     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

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 Test
  MsgBox "Test"
End Sub
Back to top
View user's profile Send private message
Pash
General User
General User


Joined: 08 Sep 2005
Posts: 15

PostPosted: Fri Dec 02, 2005 1:39 am    Post subject: Reply with quote

Is there any example to remove menu item from context menu?
Back to top
View user's profile Send private message
apeters
General User
General User


Joined: 04 Apr 2005
Posts: 47

PostPosted: Fri Dec 02, 2005 1:43 am    Post subject: Reply with quote

you can only add Menus, and when the last instance is closed the menu is removed
Back to top
View user's profile Send private message
jruget
General User
General User


Joined: 27 Oct 2006
Posts: 39

PostPosted: Mon Jan 15, 2007 8:01 am    Post subject: Reply with quote

Quote:
Is there any example to remove menu item from context menu?


Here is a solution to remove a menu added with the previous code :

Code:
Function RemoveMenu(sMyPopupMenuCmdId, sCommandeURL, sMenuBar)

   oModel = ThisComponent   
   oDocCfgMgr = oModel.getUIConfigurationManager
   oFrame = oModel.getCurrentController().getFrame()
   oLayoutManager = oFrame.LayoutManager()
   oMenuBar = oLayoutManager.getElement( sMenuBar )
   oMenuBarSettings = oMenuBar.getSettings( true )
   oMenuBar.Persistent = false // It shouln't be a problem with 000 2.0

   itemIndex = FindPopupMenu(sMyPopupMenuCmdId, oMenuBar)
   If not ( itemIndex < 0 ) Then
   print "remove in menu bar"
      oMenuBarSettings.removeByIndex(  oMenuBarSettings.getCount() -1)
   EndIf
 
   oMenuBar.setSettings( oMenuBarSettings )

end function

_________________
OpenOffice.org 2.1 / Windows XP
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group