views:

578

answers:

1

Hi All,

I have a Label in AS3 that I want to be selectable (.selectable = true) AND I want the clipboardMenu to show up on right-click ALONG with custom menu items.

If I do THIS:

var label:Label = new Label();
label.text = "test";
label.selectable = false;

var contextMenu = new ContextMenu();
contextMenu.clipboardMenu = true;
contextMenu.clipboardItems.copy = true;
contextMenu.clipboardItems.selectAll = true;
contextMenu.addItem(new ContextMenuItem("Test Item"));
contextMenu.addItem(new ContextMenuItem("Test Item 2"));

label.contextMenu = contextMenu;

thing.addChild(label);

This ALMOST works - in that when I right-click on the label I get a menu that has the clipboard items AND my custom items.... HOWEVER, the text in the label IS NOT SELECTABLE due to the .selectable = false. This of makes the clipboard menu items useless.

IF I change the line:

label.selectable = false;

to:

label.selectable = true;

The label IS selectable - however ONLY the clipboard menu items are present.

How can I both make the label selectable AND have a menu with my custom items + standard clipboard items?

Thanks for your help.

BTW - this is with an AIR application using the 3.2 SDK.

+1  A: 

Try using UITextField insted of label:

var label:UITextField = new UITextField();
label.text = "test";
label.selectable = true;

var contextMenu:ContextMenu = new ContextMenu();
contextMenu.clipboardMenu = true;
contextMenu.clipboardItems.copy = true;
contextMenu.clipboardItems.selectAll = true;
contextMenu.customItems = [new ContextMenuItem("Test Item"), new ContextMenuItem("Test Item 2")];

label.contextMenu = contextMenu;

addChild(label);

Also, you can extend Label to apply context menu directly to the inner UITextField (this solution works only in AIR, though you can use ContextMenu istead of NativeMenu and it'll work both in AIR and in flash player):

package test
{
import mx.controls.Label;
import flash.display.NativeMenu;
import mx.core.UITextField;
import mx.core.mx_internal;

use namespace mx_internal;
public class LabelWithContextMenu extends Label
{
    public function LabelWithContextMenu()
    {
     super();
    }

    override public function get contextMenu():NativeMenu
    {
     if (textField is UITextField)
      return UITextField(textField).contextMenu;
     else
      return super.contextMenu;
    }

    override public function set contextMenu(cm:NativeMenu):void
    {
     if (textField == null)
      createTextField(-1);
     if (textField is UITextField)
      UITextField(textField).contextMenu = cm;
     else
      super.contextMenu = cm;
    }

}
}
Hrundik
Very nice example - great solution. Thank you very much. +1 WITH the check box. Much Appreciated.
Gabriel