65
128
HTML DEVELOPER’S GUIDE FOR ADOBE AIR
Working with menus
Last updated 9/28/2011
For details of the specific menu structure that the MenuBuilder framework accepts, see “Defining MenuBuilder menu
structure” on page 129.
Creating an application or window menu
Adobe AIR 1.0 and later
When you create an application or window menu using the MenuBuilder framework, the top-level objects or nodes in
the menu data structure correspond to the items that show up in the menu bar. Items nested inside one of those top-
level items define the individual menu commands. Likewise, those menu items can contain other items. In that case
the menu item is a submenu rather than a command. When the user selects the menu item it expands its own menu
of items.
You use the
Menu.setAsMenu()
method to set a menu as the application menu or window menu for the window in
which the call executes. The
setAsMenu()
method takes one parameter: the NativeMenu object to use. The following
example loads an XML file and sets the generated menu as the application or window menu:
var windowMenu = air.ui.Menu.createFromXML("windowMenu.xml");
air.ui.Menu.setAsMenu(windowMenu);
On an operating system that supports window menus, the
setAsMenu()
call sets the menu as the window menu for
the current window (the window that’s represented as
window.nativeWindow
). On an operating system that supports
an application menu, the menu is used as the application menu.
Mac OS X defines a set of standard menus as the default application menu, with the same set of menu items for every
application. These menus include an application menu whose name matches the application name, an Edit menu, and
a Window menu. When you assign a NativeMenu object as the application menu by calling the
Menu.setAsMenu()
method, the items in the NativeMenu are inserted into the standard menu structure between the Edit and Window
menus. The standard menus are not modified or replaced.
You can replace the standard menus rather than supplement them if you prefer. To replace the existing menu, pass a
second argument with the value
true
to the
setAsMenu()
call, as in this example:
air.ui.Menu.setAsMenu(windowMenu, true);
Creating a DOM element context menu
Adobe AIR 1.0 and later
Creating a context menu for a DOM element using the MenuBuilder framework involves two steps. First you create
the NativeMenu instance that defines the menu structure using the
Menu.createFromXML()
or
Menu.createFromJSON()
method. You then assign that menu as the context menu for a DOM element by calling the
Menu.setAsContextMenu()
method. Because a context menu consists of a single menu, the top-level menu items in
the menu data structure serve as the items in the single menu. Any menu item that contains child menu items defines
a submenu. To assign a NativeMenu as the context menu for a DOM element, call the
Menu.setAsContextMenu()
method. This method requires two parameters: the NativeMenu to set as the context menu, and the id (a string) of the
DOM element to which it is assigned:
var treeContextMenu = air.ui.Menu.createFromXML("treeContextMenu.xml");
air.ui.Menu.setAsContextMenu(treeContextMenu, "navTree");
If you omit the DOM element parameter, the method uses the HTML document from which the method is called as
the default value. In other words, the menu is set as the context menu for the HTML document’s entire window. This
technique is convenient for removing the default context menu from an entire HTML window by passing
null
for the
first parameter, as in this example:
How to C#: Basic SDK Concept of XDoc.PDF for .NET XDoc.PDF for .NET allows C# developers to edit hyperlink of PDF document, including editing PDF url links and quick navigation link in bookmark/outline.
export pdf bookmarks to excel; create bookmarks pdf files
54
129
HTML DEVELOPER’S GUIDE FOR ADOBE AIR
Working with menus
Last updated 9/28/2011
air.ui.Menu.setAsContextMenu(null);
You can also remove an assigned context menu from any DOM element. Call the
setAsContextMenu()
method and
pass
null
and the element id as the two arguments.
Creating an icon context menu
Adobe AIR 1.0 and later
In addition to context menus for DOM elements within an application window, an Adobe AIR application supports
two other special context menus: dock icon menus for operating systems that support a dock, and system tray icon
menus for operating systems that use a system tray. To set either of these menus, you first create a NativeMenu using
the
Menu.createFromXML()
or
Menu.createFromJSON()
method. Then you assign the NativeMenu as the dock or
system tray icon menu by calling the
Menu.setAsIconMenu()
method.
This method accepts two arguments. The first argument, which is required, is the NativeMenu to use as the icon menu.
The second argument is an Array containing strings that are file paths to images to use as the icon, or BitmapData
objects containing image data for the icon. This argument is required unless default icons are specified in the
application.xml file. If default icons are specified in the application.xml file, those icons are used by default for the
system tray icon.
The following example demonstrates loading menu data and assigning the menu as the dock or system tray icon
context menu:
// Assumes that icons are specified in the application.xml file.
// Otherwise the icons would need to be specified using a second
// parameter to the setAsIconMenu() function.
var iconMenu = air.ui.Menu.createFromXML("iconMenu.xml");
air.ui.Menu.setAsIconMenu(iconMenu);
Note: Mac OS X defines a standard context menu for the application dock icon. When you assign a menu as the dock icon
context menu, the items in the menu are displayed above the standard OS menu items. You cannot remove, access, or
modify the standard menu items.
Defining MenuBuilder menu structure
Adobe AIR 1.0 and later
When you create a NativeMenu object using the
Menu.createFromXML()
or
Menu.createFromJSON()
method, the
structure of XML elements or objects defines the structure of the resulting menu. Once the menu is created, you can
change its structure or properties at run time. To change a menu item at run time you access the NativeMenuItem
object by navigating through the NativeMenu object’s hierarchy.
The MenuBuilder framework looks for certain XML attributes or object properties as it parses through the menu data
source. The presence and value of those attributes or properties determines the structure of the menu that’s created.
When you use XML for the menu structure, the XML file must contain a root node. The child nodes of the root node are
used as the top-level menu item nodes. The XML nodes can have any name. The names of the XML nodes don’t affect
the menu structure. Only the hierarchical structure of the nodes and their attribute values are used to define the menu.
VB.NET PDF - WPF PDF Viewer for VB.NET Program C#.NET PDF file & pages edit, C#.NET PDF pages extract, copy, paste, C#.NET rotate PDF pages, C#.NET search text in PDF, C#.NET edit PDF bookmark, C#.NET edit
convert word pdf bookmarks; how to add a bookmark in pdf
63
130
HTML DEVELOPER’S GUIDE FOR ADOBE AIR
Working with menus
Last updated 9/28/2011
Menu item types
Adobe AIR 1.0 and later
Each entry in the menu data source (each XML element or JSON object) can specify an item type and type-specific
information about the menu item it represents. Adobe AIR supports the following menu item types, which can be set
as the values of the
type
attribute or property in the data source:
A normal menu item is treated as a submenu if it has children. With an XML data source, this means that the menu
item element contains other XML elements. For a JSON data source, give the object representing the menu item a
property named
items
containing an array of other objects.
Menu data source attributes or properties
Adobe AIR 1.0 and later
Items in the menu data source can specify several XML attributes or object properties that determine how the item is
displayed and behaves. The following table lists the attributes you can specify, their data types, their purposes, and how
the data source must represent them:
Menu item type Description
normal
The default type. Selecting an item with the normal type triggers a select event and
calls the function specified in the onSelect field of the data source. Alternatively, if
the item has children, the menu item dispatches a preparing event, then a
displaying event and then opens the submenu.
check
Selecting an item with the check type toggles the NativeMenuItem’s checked
property between true and false values, triggers a select event, and calls the
function specified in the onSelect field of the data source. When the menu item is
in the true state, it displays a check mark in the menu next to the item’s label.
separator
Items with the separator type provide a simple horizontal line that divides the
items in the menu into different visual groups.
Attribute or property
Type
Description
altKey
Boolean
Specifies whether the Alt key is required as
part of the key equivalent for the item.
cmdKey
Boolean
Specifies whether the Command key is
required as part of the key equivalent for
the item. The
defaultKeyEquivalentModifiers
field also affects this value.
ctrlKey
Boolean
Specifies whether the Control key is
required as part of the key equivalent for
the item. The
defaultKeyEquivalentModifiers
field also affects this value.
defaultKeyEquivalentModifiers Boolean
Specifies whether the operating system
default modifier key (Command for Mac OS
X and Control for Windows) is required as
part of the key equivalent for the item. If
not specified, the MenuBuilder framework
treats the item as if the value was true.
74
131
HTML DEVELOPER’S GUIDE FOR ADOBE AIR
Working with menus
Last updated 9/28/2011
The MenuBuilder framework ignores all other object properties or XML attributes.
enabled
Boolean
Specifies whether the user can select the
menu item (true), or not (false). If not
specified, the MenuBuilder framework
treats the item as if the value was true.
items
Array
(JSON only) specifies that the menu item is
itself a menu. The objects in the array are
the child menu items contained in the
menu.
keyEquivalent
String
Specifies a keyboard character which, when
pressed, triggers an event as though the
menu item was selected.
If this value is an uppercase character, the
shift key is required as part of the key
equivalent of the item.
label
String
Specifies the text that appears in the
control. This item is used for all menu item
types except separator.
mnemonicIndex
Integer
Specifies the index position of the
character in the label that is used as the
mnemonic for the menu item.
Alternatively, you can indicate that a
character in the label is the menu item's
mnemonic by including an underscore
immediately to the left of that character.
onSelect
String or Function
Specifies the name of a function (a String)
or a reference to the function (a Function
object). The specified function is called as
an event listener when the user selects the
menu item. For more information see
“Handling MenuBuilder menu events” on
page
136.
shiftKey
String
Specifies whether the Shift key is required
as part of the key equivalent for the item.
Alternatively, the keyEquivalent value
specifies this value as well. If the
keyEquivalent value is an uppercase
letter, the shift key is required as part of the
key equivalent.
toggled
Boolean
Specifies whether a check item is selected.
If not specified, the MenuBuilder
framework treats the item as if the value
was false and the item is not selected.
type
String
Specifies the type of menu item.
Meaningful values are separator and
check. The MenuBuilder framework treats
all other values, or elements or objects with
no type entry, as normal menu entries.
Attribute or property
Type
Description
27
132
HTML DEVELOPER’S GUIDE FOR ADOBE AIR
Working with menus
Last updated 9/28/2011
Example: An XML MenuBuilder data source
Adobe AIR 1.0 and later
The following example uses the MenuBuilder framework to define a context menu for a region of text. It shows how
to define the menu structure using XML as the data source. For an application that specifies an identical menu
structure using a JSON array, see “Example: A JSON MenuBuilder data source” on page 133.
The application consists of two files.
The first file is the menu data source, in a file named “textContextMenu.xml.” While this example uses menu item
nodes named “menuitem,” the actual name of the XML nodes doesn’t matter. As described previously, only the
structure of the XML and the attribute values affect the structure of the generated menu.
<?xml version="1.0" encoding="utf-8" ?>
<root>
<menuitem label="MenuItem A"/>
<menuitem label="MenuItem B" type="check" toggled="true"/>
<menuitem label="MenuItem C" enabled="false"/>
<menuitem type="separator"/>
<menuitem label="MenuItem D">
<menuitem label="SubMenuItem D-1"/>
<menuitem label="SubMenuItem D-2"/>
<menuitem label="SubMenuItem D-3"/>
</menuitem>
</root>
The second file is the source code for the application user interface (the HTML file specified as the initial window in
the application.xml file:
52
133
HTML DEVELOPER’S GUIDE FOR ADOBE AIR
Working with menus
Last updated 9/28/2011
<html>
<head>
<title>XML-based menu data source example</title>
<script type="text/javascript" src="AIRAliases.js"></script>
<script type="text/javascript" src="AIRMenuBuilder.js"></script>
<style type="text/css">
#contextEnabledText
{
margin-left: auto;
margin-right: auto;
margin-top: 100px;
width: 50%
}
</style>
</head>
<body>
<div id="contextEnabledText">This block of text is context menu enabled. Right click
or Command-click on the text to view the context menu.</div>
<script type="text/javascript">
// Create a NativeMenu from "textContextMenu.xml" and set it
// as context menu for the "contextEnabledText" DOM element:
var textMenu = air.ui.Menu.createFromXML("textContextMenu.xml");
air.ui.Menu.setAsContextMenu(textMenu, "contextEnabledText");
// Remove the default context menu from the page:
air.ui.Menu.setAsContextMenu(null);
</script>
</body>
</html>
Example: A JSON MenuBuilder data source
Adobe AIR 1.0 and later
The following example uses the MenuBuilder framework to define a context menu for a region of text using a JSON
array as the data source. For an application that specifies an identical menu structure in XML, see “Example: An XML
MenuBuilder data source” on page 132.
The application consists of two files.
The first file is the menu data source, in a file named “textContextMenu.js.”
[
{label: "MenuItem A"},
{label: "MenuItem B", type: "check", toggled: "true"},
{label: "MenuItem C", enabled: "false"},
{type: "separator"},
{label: "MenuItem D", items:
[
{label: "SubMenuItem D-1"},
{label: "SubMenuItem D-2"},
{label: "SubMenuItem D-3"}
]
}
]
55
134
HTML DEVELOPER’S GUIDE FOR ADOBE AIR
Working with menus
Last updated 9/28/2011
The second file is the source code for the application user interface (the HTML file specified as the initial window in
the application.xml file:
<html>
<head>
<title>JSON-based menu data source example</title>
<script type="text/javascript" src="AIRAliases.js"></script>
<script type="text/javascript" src="AIRMenuBuilder.js"></script>
<style type="text/css">
#contextEnabledText
{
margin-left: auto;
margin-right: auto;
margin-top: 100px;
width: 50%
}
</style>
</head>
<body>
<div id="contextEnabledText">This block of text is context menu enabled. Right click
or Command-click on the text to view the context menu.</div>
<script type="text/javascript">
// Create a NativeMenu from "textContextMenu.js" and set it
// as context menu for the "contextEnabledText" DOM element:
var textMenu = air.ui.Menu.createFromJSON("textContextMenu.js");
air.ui.Menu.setAsContextMenu(textMenu, "contextEnabledText");
// Remove the default context menu from the page:
air.ui.Menu.setAsContextMenu(null);
</script>
</body>
</html>
Adding menu keyboard features with MenuBuilder
Adobe AIR 1.0 and later
Operating system native menus support the use of keyboard shortcuts, and these shortcuts are also available in Adobe
AIR. Two of the types of keyboard shortcuts that can be specified in a menu data source are keyboard equivalents for
menu commands and mnemonics.
Specifying menu keyboard equivalents
Adobe AIR 1.0 and later
You can specify a key equivalent (sometimes called an accelerator) for a window or application menu command.
When the key or key combination is pressed the NativeMenuItem dispatches a
select
event and any
onSelect
event
handler specified in the data source is called. The behavior is the same as though the user had selected the menu item.
For complete details about menu keyboard equivalents, see “Key equivalents for native menu commands (AIR)” on
page 116.
Using the MenuBuilder framework, you can specify a keyboard equivalent for a menu item in its corresponding node
in the data source. If the data source has a
keyEquivalent
field, the MenuBuilder framework uses that value as the
key equivalent character.
Documents you may be interested
Documents you may be interested