27
Aug
08

Using a clipboard menu in Flex with Flash Player 10

The following example shows how you can set a clipboard menu and specify custom clipboard items on a context menu by setting the contextMenu and clipboardItems properties.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see Using the beta Gumbo SDK in Flex Builder 3″.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/27/using-a-clipboard-menu-in-flex-with-flash-player-10/ -->
<Application name="ContextMenu_clipboardMenu_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        xmlns:mx="library:adobe/flex/halo"
        xmlns:ui="flash.ui.*"
        layout="flex.layout.BasicLayout">

    <Script>
        <![CDATA[
            import mx.controls.Alert;

            private function img_onEvent(evt:Event):void {
                Alert.show(evt.type);
            }

            private function img_menuSelect(evt:ContextMenuEvent):void {
                img.contextMenu.hideBuiltInItems();
            }
        ]]>
    </Script>

    <mx:CheckBox id="checkBox"
            label="clipboardMenu"
            left="10"
            top="10" />

    <mx:Image id="img"
            source="@Embed('assets/flashplayer_icon.jpg')"
            copy="img_onEvent(event);"
            cut="img_onEvent(event);"
            paste="img_onEvent(event);"
            horizontalCenter="0"
            verticalCenter="0">
        <mx:contextMenu>
            <ui:ContextMenu clipboardMenu="{checkBox.selected}"
                    menuSelect="img_menuSelect(event);">
                <ui:clipboardItems>
                    <ui:ContextMenuClipboardItems
                            copy="true"
                            cut="true"
                            paste="true"
                            selectAll="false" />
                </ui:clipboardItems>
            </ui:ContextMenu>
        </mx:contextMenu>
    </mx:Image>

</Application>

View source is enabled in the following example.

You can also specify custom clupboard items using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/27/using-a-clipboard-menu-in-flex-with-flash-player-10/ -->
<Application name="ContextMenu_clipboardMenu_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        xmlns:mx="library:adobe/flex/halo"
        layout="flex.layout.BasicLayout"
        initialize="init();">

    <Script>
        <![CDATA[
            import mx.controls.Alert;

            [Embed("assets/flashplayer_icon.jpg")]
            private var flashPlayerLogo:Class;

            private var cm:ContextMenu;

            private function init():void {
                cm = new ContextMenu();
                cm.hideBuiltInItems();
                cm.clipboardMenu = checkBox.selected;
                cm.clipboardItems.copy = true;
                cm.clipboardItems.cut = true;
                cm.clipboardItems.paste = true;
                cm.clipboardItems.selectAll = false;

                checkBox.label = "clipboardMenu";
                checkBox.addEventListener(Event.CHANGE, checkBox_change);
                checkBox.setStyle("left", 10);
                checkBox.setStyle("top", 10);

                img.contextMenu = cm;
                img.source = flashPlayerLogo;
                img.addEventListener(Event.COPY, img_onEvent);
                img.addEventListener(Event.CUT, img_onEvent);
                img.addEventListener(Event.PASTE, img_onEvent);
                img.setStyle("horizontalCenter", 0);
                img.setStyle("verticalCenter", 0);
            }

            private function checkBox_change(evt:Event):void {
                cm.clipboardMenu = checkBox.selected;
            }

            private function img_onEvent(evt:Event):void {
                Alert.show(evt.type);
            }
        ]]>
    </Script>

    <mx:CheckBox id="checkBox" />
    <mx:Image id="img" />

</Application>

1 Response to “Using a clipboard menu in Flex with Flash Player 10”


  1. 1 flash designer Oct 23rd, 2008 at 1:22 am

    guys will this alos work with menus that retrieve their content from an external xml file?

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".