Context Sensitive Menus

This sample shows how to create context sensitive menus.

The basic idea is to set the menu for the context on the fly in the movieclip's onRollover event. The code below is a snippet from the online sample. This code first defines the "menu create" functions for each context including the Windows System Tray. Then the onRollover events are defined for each context (movieclip). And lastly, to sort out and handle menu clicks for each context, the mMenu.onCommand function is defined .

// application context menu -- this function will be called by the application window's 
// onRollover event to set the application right-click menu
function setDefaultApplicationRightClickMenu() {
	mMenu.removeAllItems("Right Click Menu");
	mMenu.addItem("application 1", "Right Click Menu");
	mMenu.addItem("application 2", "Right Click Menu");
	mMenu.addItem("application checkable item", "Right Click Menu");
	mMenu.addItem("application 4", "Right Click Menu");
	mMenu.addItem("application 5", "Right Click Menu");
	mMenu.addItem("application 6", "Right Click Menu");
	mMenu.addItem("application 7", "Right Click Menu");
	mMenu.addItem("application 8", "Right Click Menu");
	mMenu.addItem("application 9", "Right Click Menu");
	mMenu.addItem("application 10", "Right Click Menu");
	mMenu.checkItem("application checkable item", applicationCheckItemState, "Right Click Menu");
}
// circle context menu -- this function will be called by the circle's onRollover event
// to set the circle's right-click menu
function setCircleRightClickMenu() {
	var menuName:String = "Right Click Menu";
	mMenu.removeAllItems(menuName);
	mMenu.addItem("circle 1", menuName);
	mMenu.addItem("circle 2", menuName);
	mMenu.addItem("circle 3", menuName);
	mMenu.addItem("circle 4", menuName);
	mMenu.addItem("circle 5", menuName);
	mMenu.addItem("circle 6", menuName);
	mMenu.addItem("circle 7", menuName);
	mMenu.addItem("circle 8", menuName);
	mMenu.addItem("circle checkable item", menuName);
	mMenu.addItem("circle 10", menuName);
	mMenu.addItem("circle 11", menuName);
	mMenu.removeItem("circle 11", menuName);
	mMenu.checkItem("circle checkable item", circleCheckItemState, menuName);
}
// square context menu -- this function will be called by the square's onRollover event
// to set the square's right-click menu
function setSquareRightClickMenu() {
	mMenu.removeAllItems();
	mMenu.addItem("square 1");
	mMenu.addItem("square 2");
	mMenu.addItem("square 3");
	mMenu.addItem("square 4");
	mMenu.addItem("square checkable item");
	mMenu.addItem("square 6");
	mMenu.addItem("square 7");
	mMenu.addItem("square 8");
	mMenu.addItem("square 9");
	mMenu.addItem("square 10");
	mMenu.addItem("square 11");
	mMenu.removeItem("square 11");
	mMenu.checkItem("square checkable item", squareCheckItemState);
}
//
// create context menu for right-clicks on the Windows system tray icon
function createASystemTrayMenu() {
	mMenu.removeAllItems("System Tray Menu");
	mMenu.addItem("system tray checkable item", "System Tray Menu");
	mMenu.addItem("system tray item 2", "System Tray Menu");
	mMenu.addSeparator("System Tray Menu");
	mMenu.addItem("system tray item 3", "System Tray Menu");
	mMenu.addItem("system tray item 4", "System Tray Menu");
	mMenu.checkItem("system tray item 4", true, "System Tray Menu");
	mMenu.checkItem("system tray checkable item", systemTrayCheckItemState, "System Tray Menu");
}
createASystemTrayMenu();
//
// Define Rollover events
//
circle_mc.onRollOver = function() {
	setDefaultApplicationRightClickMenu();
};
square_mc.onRollOver = function() {
	setDefaultApplicationRightClickMenu();
};
application_mc.onRollOver = function() {
	setDefaultApplicationRightClickMenu();
};
//
// handle menu clicks
//
mMenu.onCommand = function(menuName:String, itemName:String) {
	mApplication.trace("inside mMenu.onCommand event handler ("+menuName+","+itemName+")");
	if (itemName.indexOf("checkable")>=0) {
		mApplication.trace("is checkable item in "+menuName);
		if (itemName.indexOf("application")>=0) {
			applicationCheckItemState = !applicationCheckItemState;
		} else if (itemName.indexOf("square")>=0) {
			squareCheckItemState = !squareCheckItemState;
		} else if (itemName.indexOf("circle")>=0) {
			circleCheckItemState = !_root.circleCheckItemState;
		} else if (itemName.indexOf("system")>=0) {
			systemTrayCheckItemState = !_root.systemTrayCheckItemState;
			mMenu.checkItem("system tray checkable item", systemTrayCheckItemState, "System Tray Menu");
		}
	}
};

You can download this sample, "contextMenu", from the mProjector Samples page -- http://www.screentime.com/software/mprojector/flas.html#contextMenu.