How can I add watermark on SyntaxEditor? ( using JavaScriptSyntaxLanguage )

SyntaxEditor Web Languages Add-on for WPF Forum

Posted 6 years ago by Hyundong Hwang
Version: 14.1.0600
Avatar

Hi, I am using SyntaxEditor with JavaScriptSyntaxLanguage, and also want to add watermark on it.

Delightly I found a watermark sample code in the installed samples "Adornment 7 - watermark".

So I applied on my project but it didn't work. :(

I wonder that the watermark function is only worked on custom language not JavaScriptSyntaxLanguage.

Is my wondering right?

 

My sample codes follows,

this.SyntaxEditorObj.Document.Language = new JavaScriptSyntaxLanguage();
this.SyntaxEditorObj.Document.Language.RegisterService(new AdornmentManagerProvider<VictoriaWatermarkAdornmentManager>(typeof(VictoriaWatermarkAdornmentManager)));

 

VictoriaWatermarkAdornmentManager class is almost same as the installed sample.

public class VictoriaWatermarkAdornmentManager : AdornmentManagerBase<IEditorView>
{
	private static AdornmentLayerDefinition layerDefinition = new AdornmentLayerDefinition("Watermark", new Ordering(AdornmentLayerDefinitions.Selection.Key, OrderPlacement.After));
	private IAdornment watermarkAdornment;

	/////////////////////////////////////////////////////////////////////////////////////////////////////
	// OBJECT
	/////////////////////////////////////////////////////////////////////////////////////////////////////

	/// <summary>
	/// Initializes a new instance of the <c>WatermarkAdornmentManager</c> class.
	/// </summary>
	/// <param name="view">The view to which this manager is attached.</param>
	public VictoriaWatermarkAdornmentManager(IEditorView view)
		: base(view, layerDefinition)
	{
		// Only let this manager be active when the view has focus
		this.IsActive = view.HasFocus;

		// Create the watermark adornment
		this.watermarkAdornment = this.CreateWatermarkAdornment();

		// Attach to events
		view.TextAreaLayout += OnViewTextAreaLayout;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////////////
	// NON-PUBLIC PROCEDURES
	/////////////////////////////////////////////////////////////////////////////////////////////////////

	/// <summary>
	/// Called when the <c>ViewTextAreaLayout</c> event occurs.
	/// </summary>
	/// <param name="sender">The sender of the event.</param>
	/// <param name="e">The <see cref="ActiproSoftware.Windows.Controls.SyntaxEditor.TextViewTextAreaLayoutEventArgs"/> 
	/// instance containing the event data.</param>
	private void OnViewTextAreaLayout(object sender, TextViewTextAreaLayoutEventArgs e)
	{
		// Determine the center of the text area viewport
		Rect textAreaViewportBounds = e.View.TextAreaViewportBounds;
		Point center = new Point(textAreaViewportBounds.Width / 2, textAreaViewportBounds.Height / 2);

		// Determine the center of the watermark element
		UIElement element = this.watermarkAdornment.VisualElement;
		TextBlock textBlock = element as TextBlock;
		if (textBlock == null)
			return;
		Point textBlockCenter = new Point(textBlock.ActualWidth / 2, textBlock.ActualHeight / 2);

		// Determine the watermark location
		Point watermarkLocation = new Point(center.X - textBlockCenter.X, center.Y - textBlockCenter.Y);

		// Set the watermark location
		watermarkAdornment.Location = watermarkLocation;
	}

	/// <summary>
	/// Creates the watermark adornment.
	/// </summary>
	/// <returns>The watermark adornment.</returns>
	private IAdornment CreateWatermarkAdornment()
	{
		// Create a UIElement for the watermark
		TextBlock textBlock = new TextBlock()
		{
			Text = "powered by TradeJs™",
			FontSize = 72,
			Foreground = new SolidColorBrush(Color.FromArgb(0x20, 0xff, 0x0, 0x0)),
		};

		// Add the UIElement to the adornment layer as an adornment
		return this.AdornmentLayer.AddAdornment(textBlock, new Point(), null, null);
	}

	/////////////////////////////////////////////////////////////////////////////////////////////////////
	// PUBLIC PROCEDURES
	/////////////////////////////////////////////////////////////////////////////////////////////////////

	/// <summary>
	/// Occurs when the manager is closed and detached from the view.
	/// </summary>
	/// <remarks>
	/// Overrides should release any event handlers set up in the manager's constructor.
	/// </remarks>
	protected override void OnClosed()
	{
		// Detach from events
		this.View.TextAreaLayout -= OnViewTextAreaLayout;

		// Remove any remaining adornments
		this.AdornmentLayer.RemoveAllAdornments();

		// Call the base method
		base.OnClosed();
	}
}

Comments (2)

Answer - Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Hyundong,

The problem here is that you register the service after the language has been assigned to the document which is attached on the SyntaxEditor.  You should do these steps instead:

  1. Create language.
  2. Register extra services.
  3. Set language to document.

If you do those things, it will work.


Actipro Software Support

Posted 6 years ago by Hyundong Hwang
Avatar

Thank you very much, It works greatly :)

This is the fixed codes : 

 

var language = new JavaScriptSyntaxLanguage();
language.RegisterService(new AdornmentManagerProvider<VictoriaWatermarkAdornmentManager>(typeof(VictoriaWatermarkAdornmentManager)));
language.RegisterService(new CodeDocumentTaggerProvider<VictoriaClassificationTagger>(typeof(VictoriaClassificationTagger)));
language.RegisterService(new VictoriaMouseEventSink());

...

this.SyntaxEditorObj.Document.Language = language;
The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.