Using a clipboard menu in Flex with Flash Player 10

by Peter deHaan on August 27, 2008

in ActionScript, ContextMenu, ContextMenuClipboardItems, Image

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>

{ 3 comments… read them below or add one }

1 flash designer October 23, 2008 at 1:22 am

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

Reply

2 Daniel November 26, 2008 at 7:11 am

Hi,

I have a weird problem with the context menu. I`m trying to hide the clipboardItems (cut, copy…) from a textArea`s context menu. And it still appears. I`d appreciate any answers that might help. My code is below:

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

    var m1:ContextMenuItem = new ContextMenuItem("Mark as Company Name" );
    var m2:ContextMenuItem = new ContextMenuItem("Mark as Industry" );
    var m3:ContextMenuItem = new ContextMenuItem("Mark as Location" );
    var m4:ContextMenuItem = new ContextMenuItem("Mark as Cost" );
    var m5:ContextMenuItem = new ContextMenuItem("Mark as Market Value" );

    m1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, mark);
    m2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, mark);
    m3.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, mark);
    m4.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, mark);
    m5.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, mark);
    cm.customItems.push(m1, m2, m3, m4, m5);

    panelParsedText.contextMenu = cm;
}

Reply

3 peterd November 26, 2008 at 1:52 pm

Daniel,

Good timing! I was actually trying to do this on an FxTextArea control in Flex Gumbo the other day and it looks like it may not be possible. I was also able to reproduce the issue with a standard Flash Player TextField instance, so filed a bug report in the public Flash Player bug base:
http://bugs.adobe.com/jira/browse/FP-1094

Peter

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: