NShape Programmer Tasks
Define shape specific commands

<< Click to Display Table of Contents >>

Navigation:  Programmer Tasks > Developing a New Shape Class >

NShape Programmer Tasks
Define shape specific commands

Previous pageReturn to chapter overviewNext page

In case your shape should provide commands (e.g. for a context menu), you have to override the Shape.GetMenuItemDefs method which returns an IEnumerable<MenuItemDef> that can be used for constructing (context) menus, tool bars or other command based user interface elements.

As an example, we derive a shape from LabelBase and add a command that toggles the LabelBase.MaintainOrientation property:

First of all, the declaration of the derived shape:

public class MyLabel : LabelBase {
 /// <override></override>
 public override Shape Clone() {
         Shape result = new MyLabel(Type, this.Template);
         return result;
 protected internal MyLabel(ShapeType shapeType, Template template)
         : base(shapeType, template) {

Afterwards, add the override for the Shape.GetMenuItemDef method:

 /// <override></override>
 public override IEnumerable<MenuItemDef> GetMenuItemDefs(int mouseX, int mouseY, int range) {
         // Return menu item defs provided by the base class
         foreach (MenuItemDef menuItemDef in base.GetMenuItemDefs(mouseX, mouseY, range))
                 yield return menuItemDef;
         // Prepare parameters for menu item definition construction:
         // Menu item's state 
         bool isFeasible = true;
         bool isChecked = this.MaintainOrientation;
         // Menu item's name, title and description (usually displayed as tool tip)
         string title = "Maintain Orientation";
         string name = "MaintainLabelOrientationMenuItemDef";
         // Build descriptive text: What will happen when clicking this menu item? / Why is this menu item disabled?
         string description = isFeasible
                 ? "If enabled, the angle of the label will not change when rotating its partner shape."
                 : "Here comes a description why this menu item is disabled.";
         // Construct and return the menu item definition
         yield return new DelegateMenuItemDef(
                 title, null, Color.Empty, name, description, isChecked, isFeasible, Permission.Layout, SecurityDomainName,
                 (i, p) => { this.MaintainOrientation = !this.MaintainOrientation; }

That's all. After rebuilding your shape library, you can check the result:
A label (rotated by 90°) is attached to a shape.
The default value of the shape's MaintainOrientation property is true, which is reflected by the menu item's checked state.
Clicking the "Maintain Orientation context menu item will toggle the shape's property value to false.
Rotating the partner shape will also rotate the label now that MaintainOrientation is disabled.
Clicking the "Maintain Orientation" context menu item will toggle the shape's property value again.
With MaintainOrientation set to true, rotating the partner shape will only rotate the label's position, the orientation of the label will be unchanged.
The context menu item once again reflects the state of the shape's property value.

See Also

Programmer Tasks: Developing a new shape class

Programmer Tasks: Customizing Context Menus