Home > Plug-in Development > DXCore adornments creation and wizards

DXCore adornments creation and wizards

October 6th, 2010

There are several project item wizards made for simplifying the DXCore visual adornments creation process:

  • DXCoreAdornment – widespread adornments project item which consists of two main adornment objects bound to text coordinates: TextDocumentAdornment and VisualObjectAdornments.
  • ViewPortAdornment – contains of EditorAdornment and ViewPortAdornment which can be added to a TextDocument or TextView and bound to screen or text view coordinates.
  • TileVisual – contains of two DXCore tile adornment objects: TextDocumentTile and TileVisual to create tiles in the code editor. Tile adornments can react on mouse events.

Add new project item DXCore templates

Here’s the code of these project items:

DXCoreAdornment:

using System;
using System.Collections.Generic;
using DevExpress.DXCore.Adornments;
using DevExpress.DXCore.Platform.Drawing;
using DevExpress.CodeRush.Core;
using DevExpress.CodeRush.StructuralParser;

namespace StandardPlugIn1
{
  class DXCoreAdornment1DocumentAdornment : TextDocumentAdornment
  {
    public DXCoreAdornment1DocumentAdornment(SourceRange range)
      : base(range) { }
    protected override TextViewAdornment NewAdornment(string feature, IElementFrame frame)
    {
      return new DXCoreAdornment1ViewAdornment(feature, frame);
    }
  }
  class DXCoreAdornment1ViewAdornment : VisualObjectAdornment
  {
    public DXCoreAdornment1ViewAdornment(string feature, IElementFrame frame)
      : base(feature, frame) { }
    public override void Render(IDrawingSurface context, ElementFrameGeometry geometry)
    {
      // TODO: Add adornment painting logic
      context.DrawRectangle(null, Colors.Red, geometry.StartRect);
      context.DrawRectangle(null, Colors.Green, geometry.EndRect);
      context.DrawRectangle(null, Colors.Blue, geometry.Bounds);
    }
  }
}

ViewPortAdornment:

using System;
using System.Collections.Generic;
using DevExpress.DXCore.Adornments;
using DevExpress.DXCore.Platform.Drawing;
using DevExpress.CodeRush.Core;
using DevExpress.CodeRush.StructuralParser;

namespace StandardPlugIn1
{
  class ViewPortAdornment1DocumentAdornment : EditorAdornment
  {
    public ViewPortAdornment1DocumentAdornment() { }
    protected override TextViewAdornment GetTextViewAdornment(TextView textView)
    {
      return new ViewPortAdornment1Adornment(textView);
    }
  }
  class ViewPortAdornment1Adornment : ViewPortAdornment
  {
    public ViewPortAdornment1Adornment(TextView textView)
      : base(textView, Point.Zero, false) { }
    public override void Render(IDrawingSurface context, ElementFrameGeometry geometry)
    {
      // TODO: Add adornment painting logic
    }
  }
}

TileVisual:

using System;
using System.Collections.Generic;
using DevExpress.DXCore.Adornments;
using DevExpress.DXCore.Platform.Drawing;
using DevExpress.CodeRush.Core;
using DevExpress.CodeRush.StructuralParser;

namespace StandardPlugIn1
{
  class TileVisual1DocumentAdornment : TextDocumentTile
  {
    public TileVisual1DocumentAdornment(DocPoint start, DocPoint end, CoreEventHub master, object obj)
      : base(start, end, master, obj) { }
    protected override TextViewAdornment NewAdornment(string feature, IElementFrame binding)
    {
      TileVisual1Adornment newAdornment = new TileVisual1Adornment(binding);
      newAdornment.Cursor = Cursor.Arrow;
      return newAdornment;
    }
  }
  class TileVisual1Adornment : TileVisual
  {
    public TileVisual1Adornment(IElementFrame binding)
      : base(binding) { }
    public override void Render(IDrawingSurface context, ElementFrameGeometry geometry)
    {
      // TODO: Add adornment painting logic
    }
  }
}

After a file with adornment objects is created, we need to add it to a TextDocument to make the adornments work. Here are a few ways to achieve this:

1) Simply add it to an active document anytime you want like this:

TextDocument document = CodeRush.Documents.ActiveTextDocument;
SourcePoint carePosition = CodeRush.Caret.SourcePoint;
DXCoreAdornment1DocumentAdornment docAdornment = new DXCoreAdornment1DocumentAdornment(carePosition);
document.AddAdornment(docAdornment);

This code gets the active text document, the current text caret position and adds the newly created adornment to the text document. To remove an adornment, call the RemoveAdornments on the TextDocument object:

document.RemoveAdornment(docAdornment);

2) The second and the preferred way is to add an adornment at the time the code editor is going to paint language elements. For this purpose, the EventNexus.DecorateLanguageElement event should be used. Inside the InitializePlugIn overridden method of your StandardPlugIn instance, subscribe to the DecorateLanguageElement event:

public override void InitializePlugIn()
{
  base.InitializePlugIn();
  EventNexus.DecorateLanguageElement += ehDecorateLanguageElement;
}

Don’t forget to unsubscribe from the event inside the FinalizePlugIn:

public override void FinalizePlugIn()
{
  EventNexus.DecorateLanguageElement -= ehDecorateLanguageElement;
  base.FinalizePlugIn();
}

Inside the ehDecorateLanguageElement event handler, create an instance of an adornment and add it to the DecorateLanguageElementEventArgs:

void ehDecorateLanguageElement(object sender, DecorateLanguageElementEventArgs args)
{
  LanguageElement activeElement = args.LanguageElement;
  if (CanDrawLanguageElement(activeElement))
  {
    DXCoreAdornment1DocumentAdornment docAdornment = new DXCoreAdornment1DocumentAdornment(activeElement);
    args.AddAdornment(docAdornment);
  }
}

In the CanDrawLanguageElement method, you may perform some requirements, e.g. filter language elements you want to be painted. There’s no need for manual removal of adornments in this case, because if the requirements are not met (e.g. no language elements to paint) then adornments won’t be added at all.

—–
Products: DXCore
Versions: 10.1 and up
VS IDEs: any
Updated: Oct/06/2010
ID: D022

Similar Posts: