| View previous topic :: View next topic |
| Author |
Message |
apeters General User

Joined: 04 Apr 2005 Posts: 47
|
Posted: Mon Apr 18, 2005 11:18 pm Post subject: dynamic menu´s in Writer |
|
|
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
|
 |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Tue Apr 19, 2005 6:47 am Post subject: |
|
|
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 |
|
 |
apeters General User

Joined: 04 Apr 2005 Posts: 47
|
Posted: Tue Apr 19, 2005 7:01 am Post subject: |
|
|
| thats ugly |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
apeters General User

Joined: 04 Apr 2005 Posts: 47
|
Posted: Tue Apr 19, 2005 7:17 am Post subject: |
|
|
| 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 |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
apeters General User

Joined: 04 Apr 2005 Posts: 47
|
Posted: Wed Apr 20, 2005 1:54 am Post subject: |
|
|
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 |
|
 |
Pash General User

Joined: 08 Sep 2005 Posts: 15
|
Posted: Fri Dec 02, 2005 1:39 am Post subject: |
|
|
| Is there any example to remove menu item from context menu? |
|
| Back to top |
|
 |
apeters General User

Joined: 04 Apr 2005 Posts: 47
|
Posted: Fri Dec 02, 2005 1:43 am Post subject: |
|
|
| you can only add Menus, and when the last instance is closed the menu is removed |
|
| Back to top |
|
 |
jruget General User


Joined: 27 Oct 2006 Posts: 39
|
Posted: Mon Jan 15, 2007 8:01 am Post subject: |
|
|
| 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 |
|
 |
|