
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();
}
}