Pages: 1
Print
Author Topic: 2 level entries for right click menu  (Read 2454 times)
Brian Wong
Member

Posts: 18


« on: February 15, 2009, 07:30:10 PM »

Hi all,

        Is it any way to create 2 level entries for right click menu? at the moment, I can only create 1 level entry for right click menu by using Application.AppendMacro API, however, I cannot think of any way to create 2 level entries.



-Brian
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 1548



WWW
« Reply #1 on: February 16, 2009, 02:41:26 PM »

You will need to use one of the following events:
On_Context_Menu (document level event you would place in your dtdname.mcr file)
On_Application_Document_Context_Menu (application level event to modify the context menu for all document types)

There is currently no way to modify the Context Menu displayed when no documents are open as the events above only fire when you right click in an open document (or Shift+F10 or on a Windows-savvy keyboard the Context Menu key).

You use the Application.ActiveContextMenu object to modify what the Context Menu contains (for details on each object, property and method refer to the XMetaL Developer Programmer's Guide). These properties and methods are essentially the same as those used with the CommandBars object (used for manipulating menus and toolbars).

The following example demonstrates the basics of extending the Context Menu.

Code:
<MACRO name="On_Context_Menu" hide="true" lang="JScript"><![CDATA[
if ((ActiveDocument.ViewType == sqViewNormal)||(ActiveDocument.ViewType == sqViewTagsOn)) {
//Extend standard context menu to add an item.
//Adding items to the 'newMenu' object makes 'newMenu' into a container.
        //Doing so means the OnAction property no longer takes effect so that property is not set here.
var newMenu        = Application.ActiveContextMenu.Controls.Add(variantType=5);
newMenu.Caption    = "Top Level Menu Item";
newMenu.BeginGroup = false; //set to true to add separator between this and rest of menu.
newMenu.FaceId     = 0; //FaceId has no effect when assigned to this type of item.

//Add two items to the object called "newMenu" that we created above.
var newOption      = newMenu.Controls.Add(variantType=5);
newOption.Caption  = "Menu Item 1";
newOption.OnAction = "Your Macro Name 1";
newOption.BeginGroup = false; //default
newOption.FaceId     = Application.MakeFaceId("Structure (Custom)",4,1);

var newOption      = newMenu.Controls.Add(variantType=5);
newOption.Caption  = "Menu Item 2";
newOption.OnAction = "Your Macro Name 2";
newOption.BeginGroup = false; //default
newOption.FaceId     = Application.MakeFaceId("Structure (Custom)",4,2);
}
]]></MACRO>

The previous example adds a new menu item containing two items whenever the context menu is displayed in TagsOn or Normal view. You could remove the if statement or alter it to meet any other requirement(s) you might have. For example, you may wish to add certain items when Selection.ContainerName="p" but other items when the user is not in that type of element, or you may wish to actually add items when the user is in PlainText view, provided you have a script that runs there (perhaps some basic text manipulation or something).
« Last Edit: February 16, 2009, 02:46:59 PM by Derek Read » Logged
mag3737
XMetaL Evangelist
Administrator
Member

Posts: 100


I even use XMetaL to write my business letters.


« Reply #2 on: February 16, 2009, 08:05:26 PM »

(Just a little extra information.) Derek's sample code shows one example of the "manual" way to build up menus (and toolbars) using script in XMetaL. For multi-level menus, it is necessary to follow a procedure like this. The "AppendMacro" is essentially a shortcut for some of this code, but as discovered by Brian it is limited to adding single menu items.
Logged

Tom Magliery
JustSystems Canada, Inc.
Brian Wong
Member

Posts: 18


« Reply #3 on: February 20, 2009, 12:15:10 PM »

Hi All,

        Thanks for your information, Derek. I followed your example and have successfully created a new appendMacros function which allow me to create multi-levels "right click menu". It is a lot better than the original AppendMacro function. Now i can rearrange my right click menu which a better a way.

 Regards,

-Brian
Logged
Pages: 1
Print
Jump to:  

email us