List Info

Thread: Refreshing a modified menu bar




Refreshing a modified menu bar
user name
2006-10-20 13:34:11
Le 19 oct. 06, à 14:52, Carsten Driesner a écrit :

> Julien Galand wrote:
>> Thanks for this link.
>> I had tried this previously and it didn't work, but
I think I have  
>> now found the cause (there is apparently a bug in
the C++ UNO binding  
>> about the awt::KeyEvent structure, for which I have
just posted a  
>> mail in this mailing list), and I could make it
work.
>> But the real question in my previous mail was to
display the shortcut  
>> in the menu line. The code snippet above doesn't
change anything in  
>> the menu added by the "transient change"
method (I have set the same  
>> command URL on the shortcut and the menu line,
hoping that OO would  
>> associate both automatically).
>> Is there a way to display for example
"Ctrl+F12" on the right of a  
>> menu line (and right-justified) ?
> Hi Julien,
>
> OpenOffice.org should display all associated keyboard
shortcuts  
> automatically in the menu. If it doesn't work for your
example, then I  
> guess that it's a bug. So could you write me a bug for
this issue and  
> add a small example to reproduce it?
>

Hi Carsten,

I have spent several hours to boil down the phenomenon.
 From my tests, I have concluded that this "display
connection" of the  
shortcut with its menu line doesn't work when the menu line
is in a  
submenu (i.e. not a line in a menu of the menu bar, but in a
submenu of  
a menu).
When it is simply a menu line of a menu of the menu bar, it
works.

It looks like a bug. I use OO 2.0.3.

With regards to a sample code, I have got one, but only in
JScript, as  
I am much more used to it than Basic.
Copy the sample below in a text file, say  
"Insert_new_menu_and_shortcut.js".
I launch the script in Windows, with a simple command line
(OO should  
be running) :

cscript "Insert_new_menu_and_shortcut.js"

I hope JScript won't be a burden for you. If it were, tell
me and I  
will try to translate it in OO Basic with my little
knowledge of it.
Here is the sample :

(you can try both cases, with a simple menu line, or with a
submenu, by  
assigning the 3rd parameter of 'InsertMenu')
____________________________________________________________
____________ 
__________________________

var ooServMgr =
WScript.CreateObject("com.sun.star.ServiceManager"
);

var mycommand =
"macro:///Standard.Module1.Test()";

// Set the 3rd parameter to 'true' to insert a submenu in
the "File"  
menu.
// This submenu has only one line.
// Or 'false' to insert only the menu line in the
"File" menu.
//
InsertMenu(ooServMgr, mycommand, true);

InsertShortcut(ooServMgr, mycommand);

function InsertMenu(ooServMgr, myCommand, bWithSubmenu) {
	var ooDesktop =
ooServMgr.createInstance("com.sun.star.frame.Desktop&qu
ot;);
	var curFrame  = ooDesktop.getCurrentFrame();
	var layoutMgr = curFrame.LayoutManager;

	var menuElementName =
"private:resource/menubar/menubar";
	var menuBar         =
layoutMgr.getElement(menuElementName);
	var menuBarSettings = menuBar.getSettings(true);

	menuBar.Persistent = false;

	// Find the "File" popup menu.
	//
	var fileMenuIndex = FindCommand(".uno:PickList",
menuBarSettings);

	if (fileMenuIndex >= 0) {
		var popupMenuItem =  
menuBarSettings.getByIndex(fileMenuIndex).toArray();
		var popupMenu =
GetProperty("ItemDescriptorContainer",
popupMenuItem);
	
		//if (FindCommand(myCommand, popupMenu)==-1)
		{
			var menuItem;
			menuItem = CreateMenuItem(ooServMgr, myCommand,
"Test shortcut",  
null);
			
			var menuItemPassed = ooServMgr.Bridge_GetValueObject();
			menuItemPassed.Set("[]com.sun.star.beans.PropertyVal
ue", menuItem);
			
			if (bWithSubmenu) {
				var submenu;
				submenu =  
menuBarSettings.createInstanceWithContext(ooServMgr.DefaultC
ontext);
				submenu.insertByIndex(0, menuItemPassed);
				
				menuItem = CreateMenuItem(ooServMgr, "",
"Test shortcut submenu",  
submenu);
				menuItemPassed.Set("[]com.sun.star.beans.PropertyVa
lue", menuItem);
			}
			
			popupMenu.insertByIndex(popupMenu.getCount(),
menuItemPassed);
		}
	}
	menuBar.setSettings(menuBarSettings);
}

function FindCommand(command, menuBarSettings) {
	for (var i=0; i<menuBarSettings.getCount(); i++) {
		var popupMenu = menuBarSettings.getByIndex(i).toArray();
		
		for (var j=0; j<popupMenu.length; j++) {
			if (popupMenu[j].Name=="CommandURL" &&
popupMenu[j].Value==command)  
return i;
		}
	}
	return -1;
}

function GetProperty(propertyName, properties) {
	for (var i = 0; i<properties.length; i++) {
		if (properties[i].Name==propertyName) return
properties[i].Value;
	}
	return null;
}
	
function CreateMenuItem(ooServMgr, command, label, submenu)
{
	var menuItem = new Array(3 + (submenu != null));
	
	menuItem[0] =  
ooServMgr.Bridge_GetStruct("com.sun.star.beans.Property
Value");
	menuItem[1] =  
ooServMgr.Bridge_GetStruct("com.sun.star.beans.Property
Value");
	menuItem[2] =  
ooServMgr.Bridge_GetStruct("com.sun.star.beans.Property
Value");
	
	menuItem[0].Name  = "CommandURL";
	menuItem[0].Value = command;
	menuItem[1].Name  = "Label";
	menuItem[1].Value = label;
	menuItem[2].Name  = "Type";
	menuItem[2].Value = 0;
	
	if (submenu != null) {
		menuItem[3] =  
ooServMgr.Bridge_GetStruct("com.sun.star.beans.Property
Value");
		menuItem[3].Name  = "ItemDescriptorContainer";
		menuItem[3].Value = submenu;
	}

	return menuItem;
}

function InsertShortcut(ooServMgr, command) {
	var xUICfgSupp  =  
ooServMgr.createInstance("com.sun.star.ui.ModuleUIConfi
gurationManagerSu 
pplier");
	var xUICfgMgr   =  
xUICfgSupp.getUIConfigurationManager("com.sun.star.text
.TextDocument");
	var shortcutMgr = xUICfgMgr.getShortCutManager();

	var keyEvent =
ooServMgr.Bridge_GetStruct("com.sun.star.awt.KeyEvent&q
uot;);

	keyEvent.KeyCode = 778; // F11
	keyEvent.Modifiers = 2; // Shift==1; Ctrl==2

	try {
		if (shortcutMgr.getCommandByKeyEvent(keyEvent)==command) {
			shortcutMgr.removeKeyEvent(keyEvent);
		}
	}
	catch(e) {
		var type = typeof e;
	}

	shortcutMgr.setKeyEvent(keyEvent, command);
	shortcutMgr.store();
}

____________________________________________________________
____________ 
__________________________

Julien Galand
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )