View comments | RSS feed

ContextMenu class

Availability

Flash Player 7.

Description

The ContextMenu class provides runtime control over the items in the Flash Player context menu, which appears when a user right-clicks (Windows) or Control-clicks (Macintosh) on Flash Player. You can use the methods and properties of the ContextMenu class to add custom menu items, control the display of the built-in context menu items (for example, Zoom In and Print), or create copies of menus.

You can attach a ContextMenu object to a specific button, movie clip, or text field object, or to an entire movie level. You use the menu property of the Button, MovieClip, or TextField classes to do this. For more information about the menu property, see Button.menu, MovieClip.menu, and TextField.menu.

To add new items to a ContextMenu object, you create a ContextMenuItem object, and then add that object to the ContextMenu.customItems array. For more information about creating context menu items, see the ContextMenuItem class entry.

Flash Player has three types of context menus: the standard menu (which appears when you right-click in Flash Player), the edit menu (which appears when you right-click over a selectable or editable text field), and an error menu (which appears when a SWF file has failed to load into Flash Player.) Only the standard and edit menus can be modified with the ContextMenu class.

Custom menu items always appear at the top of the Flash Player context menu, above any visible built-in menu items; a separator bar distinguishes built-in and custom menu items. You can add no more than 15 custom items to a context menu. You cannot remove the Settings menu item from the context menu. The Settings menu item is required in Flash so users can access the settings that affect privacy and storage on their computers. You also cannot remove the About menu item from the context menu, which is required so users can find out what version of Flash Player they are using.

You must use the constructor new ContextMenu() to create a ContextMenu object before calling its methods.

Method summary for the ContextMenu class

Method Description
ContextMenu.copy()

Returns a copy of the specified ContextMenu object.

ContextMenu.hideBuiltInItems()

Hides most built-in items in the Flash Player context menu.

Property summary for the ContextMenu class

Property Description
ContextMenu.builtInItems

An object whose members correspond to built-in context menu items.

ContextMenu.customItems

An array, undefined by default, that contains ContextMenuItem objects.

Event handler summary for the ContextMenu class

Property Description
ContextMenu.onSelect

Invoked before the menu is displayed.

Constructor for the ContextMenu class

Availability

Flash Player 7.

Usage

new ContextMenu ([callBackFunction])

Parameters

callBackFunction A reference to a function that is called when the user right-clicks or Control-clicks, before the menu is displayed. This parameter is optional.

Returns

A reference to a ContextMenu object.

Description

Constructor; creates a new ContextMenu object. You can optionally specify an identifier for an event handler when you create the object. The specified function is called when the user invokes the context menu, but before the menu is actually displayed. This is useful for customizing menu contents based on application state or based on the type of object (movie clip, text field, or button) or the Timeline that the user right-clicks or Control-clicks. (For an example of creating an event handler, see ContextMenu.onSelect.)

Example

The following example hides all the built-in objects in the Context menu. (However, the Settings and About items still appear, because they cannot be disabled.)

var newMenu:ContextMenu = new ContextMenu();
newMenu.hideBuiltInItems();
this.menu = newMenu;

In this example, the specified event handler, menuHandler, enables or disables a custom menu item (using the ContextMenu.customItems array) based on the value of a Boolean variable named showItem. If false, the custom menu item is disabled; otherwise, it's enabled.

var showItem = true;  // Change this to false to remove
var my_cm:ContextMenu = new ContextMenu(menuHandler);
my_cm.customItems.push(new ContextMenuItem("Hello", itemHandler));
function menuHandler(obj, menuObj) {
   if (showItem == false) {
      menuObj.customItems[0].enabled = false;
   } else {
      menuObj.customItems[0].enabled = true;
   }
}
function itemHandler(obj, item) {
   //...put code here...
   trace("selected!");
}
this.menu = my_cm;

When the user right-clicks or Control-clicks the Stage, the custom menu is displayed.

See also

Button.menu, ContextMenu.onSelect, ContextMenu.customItems, ContextMenu.hideBuiltInItems(), MovieClip.menu, TextField.menu

Comments


recoveredfromflashMX2004 said on Aug 4, 2004 at 2:30 PM :
prochimpo said on Feb 9, 2004 at 5:58 AM :

Is it possible to remove the settings and about options in the context menu? I am building applications using flash as a front end and need to employ the right click context menu. Seeing settings and about flash in the context menu makes it unusable for my application. I have to use work arounds, but it would be nice to use the context menu as it should be used without the extra unremoveable settings and about flash.
Friendly Voice said on Feb 9, 2004 at 9:54 AM :
For now, both of those options need to be there. Settings is required for legal reasons - we have to provide folks with access to settings that affect privacy and storage on their computer. And info like that in "about" is in pretty much every product, so users can tell which version they are using. If you want to make a formal request (livedocs can address only doc comments) you can send email to wish-flash@macromedia.com.


nwebb[co.uk] said on Feb 15, 2004 at 7:21 AM :

It doesn't appear possible to assign an instance of the ContextMenu class to any MovieClip, Button or TextField instance that does not reside directly on the main timeline.
For example, the third line of script below will fail.

mc1.btn1.menu = cm1; //works
mc1.txt1.menu = cm1; //works
mc1.mc2.menu = cm1; //fails

note: Although the Button and TextField object work when nested inside mc1, neither object possesses a timeline and so their parent timeline is that of mc1 (which resides on the root).
I couldn't see any mention of this in the dictionary, so thought I'd post here to save others some time.


Aries said on Feb 18, 2004 at 7:52 PM :

Hello,

I also tried implementing the ContextMenu in a new component and I can't get it to recongnize the MovieClip.prototype.menu. I don't know if this has to do with root, but I have it right clicking and recongnizing my clicks. The point the script fails is at the moment the menu is to be populated with the custom Items.

I am developing a Flash mySQL Desktop and I am using icons for say, tables. I wanted the user to right click and get some specail info on my custom icon component.... dosn't work... I don't understand, I have tried about every angle possible and I can't get it to work in an external class file....

Oh well.....

Mike
mike@tgdd.com


No screen name said on Feb 26, 2004 at 5:45 PM :

So how do you get rid of the Debugger menu? It shows up as greyed out, but I don't seem to be able to get rid of that. Any help appreciated.


No screen name said on Feb 26, 2004 at 5:54 PM :

I forgot to mention that while I see it in my flash files, I don't see it in Macromedia's homepage. So I wonder if I actually have the Flash Debug player or not. Is there a way to find out (in case that is why I'm seeing the menu)?
recoveredfromflashMX2004 said on Aug 4, 2004 at 2:32 PM :
dirx said on Mar 31, 2004 at 8:10 AM :

a workaround to assign context menus to nested movieclips (flash player 7.0.19.0):

attach a library symbol (should be exported for actionscript) with button behavior to this movieclip and assign a contextmenu to this button.

to me it appears to be a player or even compiler bug, that it is not possible to assign a context menu to nested movieclips directly.


jepo said on Apr 1, 2004 at 1:59 PM :

The functionality does seem to warrant unexpected results. A bug has been filed regarding this. Thanks!


ctvrtlik said on Apr 22, 2004 at 3:11 PM :

do context menu's not work on elements/clips in a forms application? are there any documented workarounds?


stash999 said on Apr 26, 2004 at 7:46 AM :

I feel Macromedia have an excellent product however 2 main areas are of a concern when WANTING to develop an application style project.

1. Right mouse click Context menu (Settings, about....).
- I know about the reasons (Legal etc), but I think I speak for a lot of developers when I say "get rid of it please".
- Putting the Legal reason on the right mouse click is not acceptable. Put it somewhere else. This is possibly the only application that does this and a unfortunate reason of why I cannot use it to develop applications using flash.

Let the user include it as a standard button etc.

2. Custom cursors having to be designed for other than Hand or pointer.
- Painful area. Want a sizer cursor, so I have to design one???.

Good effort. Must do better. You have almost got it.


TroyWorks said on May 31, 2004 at 9:00 PM :

Note that if the function reference passed to the new ContextMenuItem is invalid the menu won't show up.

case1: no options the full menu (e.g. zoom, 100%, settings, about, etc) shows up

case2:
var trialNavigation = new ContextMenu();
trialNavigation.hideBuiltInItems();
// Applying this line new menu without this line the menu won't take effect.
_level0.menu = trialNavigation;

this will show the context menu with only two options 'settings... ' and 'debugger'.

case 3 - invalid function passed in. In this case the item will be completely ignored , in the case of only a single item it might be the equalent of case 2.

e.g.

var testFunction1 = function () {
gotoAndPlay(2);
};
var trialNavigation = new ContextMenu();
trialNavigation.hideBuiltInItems();
var newItem01 = new ContextMenuItem("My Copy", testFunction1);
var newItem02 = new ContextMenuItem("My Paste", invalidFunction); //won't show up!!!!
trialNavigation.customItems.push(newItem01);
trialNavigation.customItems.push(newItem02);

// Applying the new menu
_level0.menu = trialNavigation;


No screen name said on Jun 29, 2004 at 9:30 AM :

Unfortunately, when you pass a handler into the constructor you can't also pass in an object to scope to. The handler will be called but it has no scope. This holds true for onSelect as well. If you try to extend the class and have an onSelect function it is not recognized oddly enough. Therefore, it makes it impossible to use with AS2. The same holds true with the ContextMenuItem. Are there any workarounds if not, can this be filed as a bug.

Kenny Bunch


ctvrtlik said on Jul 26, 2004 at 1:43 PM :

why does a movie stop playing in mac's when you use the right context but in pc's it keeps playing? do i have separate versions of the flash player on my mac and pc machine?
ctvrtlik said on Oct 1, 2004 at 11:22 AM :
i think it would be useful if there was an ability to catch an event if the user clicks outside the context menu. for example, they right click an object then decide they don't want to select any of the items, it would be nice if we could catch an "onMouseOutside" event or something of that nature.
M.Q said on Nov 16, 2004 at 11:37 PM :
How can I customize the contextmenu items, like change the textAlign to right and also change the font color, etc...?

thanks,
Munir Qasem
tectonik said on Aug 26, 2005 at 4:15 AM :
/*
contextMenu and unicodes charsets
Is is not possible to display ContextMenuItems captions in special charsets.
*/
createTextField("tf1", getNextHighestDepth(), 0, 0, 150, 30);
var aLangs:Array = ["français", "english", "deutsch", "español", "한국어", "日本語", "中文", "türkçe", "italiano", "nederlands", "português", "中文繁體", "ไทย", "русский", "عربي", "עברית", "हिन्दी"];
var contextMenu1:ContextMenu = new ContextMenu();
menu = contextMenu1;
for (var i = 0; i<aLangs.length; i++) {
contextMenu1.customItems.push(new ContextMenuItem(aLangs[i], function (obj:ContextMenu, menuItem:ContextMenuItem) {
tf1.text = menuItem.caption;
}));
}
pixeldrew said on Jan 22, 2006 at 3:19 AM :
The function that is passed to the ContextMenuItem is not passed by reference but seems to be copied. So beware that the scope of the function passed is not what you may think it is.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001276.html