Events

There are three basic kinds of events that GoDiagram deals with: DiagramEvents, InputEvents, and ChangedEvents. This page discusses the first two; see Changed Events for the last kind of event.

Diagram Events

DiagramEvents represent general user-initiated changes to a diagram. You can register one or more diagram event handlers by subscribing to the event on the diagram. Each kind of diagram event is distinguished by its name.

Currently defined diagram event names can be found in the DiagramEventName enum.

DiagramEvents do not necessarily correspond to mouse events or keyboard events or touch events. Nor do they necessarily correspond to changes to the diagram's model -- for tracking such changes, use Model.Changed or Diagram.ModelChanged. DiagramEvents only occur because the user did something, perhaps indirectly.

In addition to the DiagramEvent listeners there are also circumstances where detecting such changes is common enough to warrant having properties that are event handlers. Because these events do not necessarily correspond to any particular input or diagram event, these event handlers have custom arguments that are specific to the situation.

One very common such event property is GraphObject.Click, which if non-null is a function that is called whenever the user clicks on that object. This is most commonly used to specify behavior for "Button"s, but it and the other "Click" event properties, "DoubleClick" and "ContextClick", can be useful on any GraphObject.

Another common event property is Part.SelectionChanged, which (if non-null) is called whenever Part.IsSelected changes. In this case the event hander function is passed a single argument, the Part. There is no need for additional arguments because the function can check the current value of Part.IsSelected to decide what to do.

Model ChangedEvents are more complete and reliable than depending on DiagramEvents. For example, the "LinkDrawn" DiagramEvent is not raised when code adds a link to a diagram. That DiagramEvent is only raised when the user draws a new link using the LinkingTool. Furthermore the link has not yet been routed, so Link.Points will not have been computed. In fact, creating a new link may invalidate a Layout, so all of the nodes may be moved in the near future.

Sometimes you want to update a database as the user makes changes to a diagram. Usually you will want to implement a Model ChangedEvent listener, by registering a Model.Changed or Diagram.ModelChanged listener, that notices the changes to the model and decides what to record in the database. See the discussion of Changed Events and the Update Demo.

This example demonstrates handling several diagram events: Diagram.ElementSingleClicked, Diagram.BackgroundDoubleClicked, and Diagram.ClipboardPasted.


  diagram.ElementSingleClicked += (s, e) => {
    var part = (e.Subject as GraphObject).Part;
    if (!(part is Link)) Console.WriteLine("Clicked on " + part.Key);
  };

  diagram.BackgroundDoubleClicked += (s, e) => {
    Console.WriteLine("Double-clicked at " + e.Diagram.LastInput.DocumentPoint);
  };

  diagram.ClipboardPasted += (s, e) => {
    Console.WriteLine("Pasted " + e.Diagram.Selection.Count + " parts");
  };

  ...

Input Events

When a low-level event occurs, GoDiagram canonicalizes the keyboard/mouse/touch event information into a new InputEvent that can be passed to various event-handling methods and saved for later examination.

An InputEvent keeps the InputEvent.Key for keyboard events, the InputEvent.Button for mouse events, the InputEvent.ViewPoint for mouse and touch events, and InputEvent.Modifiers for keyboard and mouse events.

The diagram's event handlers also record the InputEvent.DocumentPoint, which is the InputEvent.ViewPoint in document coordinates at the time of the mouse event, and the InputEvent.Timestamp, which records the time that the event occurred in milliseconds.

The InputEvent class also provides many handy properties for particular kinds of events. Examples include InputEvent.Control (if the control key had been pressed) and InputEvent.Left (if the left/primary mouse button was pressed).

Some tools find the "current" GraphObject at the mouse point. This is remembered as the InputEvent.TargetElement.

Higher-level input events

Some tools detect a sequence of input events to compose somewhat more abstract user events. Examples include "click" (mouse-down-and-up very close to each other) and "hover" (motionless mouse for some time). The tools will call an event handler (if there is any) for the current GraphObject at the mouse point. The event handler is held as the value of a property on the object. It then also "bubbles" the event up the chain of GraphObject.Panels until it ends with a Part. This allows a "click" event handler to be declared on a Panel and have it apply even if the click actually happens on an element deep inside the panel. If there is no object at the mouse point, the event occurs on the diagram.

Click-like event properties include GraphObject.Click, GraphObject.DoubleClick, and GraphObject.ContextClick. They also occur when there is no GraphObject -- the event happened in the diagram's background: Diagram.Click, Diagram.DoubleClick, and Diagram.ContextClick. These are all properties that you can set to a function that is the event handler. These events are caused by both mouse events and touch events.

Mouse-over-like event properties include GraphObject.MouseEnter, GraphObject.MouseOver, and GraphObject.MouseLeave. But only Diagram.MouseOver applies to the diagram.

Hover-like event properties include GraphObject.MouseHover and GraphObject.MouseHold. The equivalent diagram properties are Diagram.MouseHover and Diagram.MouseHold.

There are also event properties for dragging operations: GraphObject.MouseDragEnter, GraphObject.MouseDragLeave, and GraphObject.MouseDrop. These apply to stationary objects, not the objects being dragged. And they also occur when dragging by touch events, not just mouse events.

This example demonstrates handling three higher-level input events: clicking on nodes and entering/leaving groups.


  diagram.NodeTemplate =
    new Node("Auto") {
        Click = (e, obj) => { Console.WriteLine("Clicked on " + obj.Part.Key); }
      }
      .Add(
        new Shape("Ellipse") { Fill = "white" },
        new TextBlock().Bind("Text", "Key")
      );

  diagram.GroupTemplate =
    new Group("Vertical") {
        MouseEnter = (e, obj, prev) => {
          if (obj.Part.FindElement("SHAPE") is Shape shape) shape.Fill = "red";
        },
        MouseLeave = (e, obj, prev) => {
          if (obj.Part.FindElement("SHAPE") is Shape shape) shape.Fill = "rgba(128,128,128,0.33)";
        }
      }
      .Add(
        new TextBlock { Alignment = Spot.Left, Font = new Font("Segoe UI", 15, FontWeight.Bold) }
          .Bind("Text", "Key"),
        new Panel("Auto")
          .Add(
            new Shape("RoundedRectangle") {
                Name = "SHAPE",
                Parameter1 = 14,
                Fill = "rgba(128,128,128,0.33)"
              },
            new Placeholder { Padding = 5 }
          )
      );

  ...

Clicking and Selecting

This example demonstrates both the "Click" and the "SelectionChanged" events:


  diagram.NodeTemplate =
    new Node("Auto") {
        SelectionAdorned = false,
        Click = (e, obj) => { Console.WriteLine("Clicked on " + obj.Part.Key); },
        SelectionChanged = (part) => {
          var shape = part.Elt(0) as Shape;
          shape.Fill = part.IsSelected ? "red" : "white";
        }
      }
      .Add(
        new Shape("Ellipse") { Fill = "white" },
        new TextBlock().Bind("Text", "Key")
      );

  ...

Note the distinction between the GraphObject.Click event property and the Part.SelectionChanged event property. Both are methods that get called when something has happened to the node. The GraphObject.Click occurs when the user clicks on the node, which happens to select the node. But the Part.SelectionChanged occurs even when there is no click event or even any mouse event -- it was due to a property change to the node.