Posted 11 years ago by Nick
Version: 12.2.0571
Avatar

Hello,

I've read the previous post (3 years old now) regarding the difficulties in providing an 'autosizeheight' option on the syntaxeditor, as the lines are virtualized. For those of us using the syntax editor inside a grid (as a one-line syntax editor, for example), the availability of an 'autosizeheight' property would be very useful. For me, I know that there will never be more than one or two lines of code in this editor, so virtualization isn't a requirement.

We've had a messy hack in place for a few years now which dynamically sizes the editor correctly, but it sure would be nice to have this option baked right into the editor itself.

Thanks.

Comments (8)

Posted 11 years ago by Nick
Avatar

Or perhaps you might be willing to post a code sample of how you would add this logic?

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

Hi Nick,

I'll log your request with the related TODO item.  You'd probably need to use something like a FormattedText object with the font that you are using in SyntaxEditor.  In a SyntaxEditor.MeasureOverride override, measure the document text with that and append it onto the size the SyntaxEditor currently measures at (via the base method).


Actipro Software Support

Posted 11 years ago by Nick
Avatar

Sounds reasonable. I'll give that a whirl. Thx.

Posted 11 years ago by Nick
Avatar

Any pointers on how to measure the line height (to match the LineHeight parameter of the FormattedText type)?

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

Hi Nick,

I don't think FormattedTextTakes a line height parameter.  Just use whatever font family and size you use on SyntaxEditor.  Then we may also add a pixel or two of padding on each line.


Actipro Software Support

Posted 11 years ago by Nick
Avatar
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActiproSoftware.Windows.Controls.SyntaxEditor;
using System.Windows.Media;
using System.Globalization;
using System.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using Connexion.Core;
using ActiproSoftware.Text;

namespace Connexion.Device
{
  public class AutoHeightSyntaxEditor : SyntaxEditor
  {
    public AutoHeightSyntaxEditor()
    {
      VerticalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Disabled;
      HorizontalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Disabled;

      DocumentTextChanged += (o, e) => PerformAutoSizeHeight();
      LayoutUpdated += (o, e) => PerformAutoSizeHeight();
      
      PreviewMouseWheel += (o, e) =>
        {
          e.Handled = true;

          var e2 = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
          e2.RoutedEvent = UIElement.MouseWheelEvent;
          e2.Source = o;

          var parent = WPFHelpers.GetParentOfType(this, typeof(ScrollViewer)) as ScrollViewer;
          if (parent != null)
            parent.RaiseEvent(e2);
        };
    }

    public void PerformAutoSizeHeight()
    {
      if (!string.IsNullOrEmpty(Text))
      {
        ActiveView.Scroller.ScrollToDocumentStart();

        // calc margin widths
        double marginWidth = 0;
        foreach (var margin in ActiveView.Margins)
        {
          marginWidth += margin.VisualElement.ActualWidth;
        }

        var collapsedRegions = ActiveView.CollapsedRegionManager.GetCollapsedRanges(new TextSnapshotRange(ActiveView.CurrentSnapshot, 0, ActiveView.CurrentSnapshot.Length));
        StringBuilder text = null;
        bool endsInNewLine = false;

        if (collapsedRegions != null && collapsedRegions.Count > 0)
        {
          text = new StringBuilder(ActiveView.CurrentSnapshot.GetText(LineTerminator.CarriageReturn));
          foreach (var collapsedRgn in collapsedRegions.OrderByDescending(c => c.StartOffset))
          {
            text.Remove(collapsedRgn.StartOffset, collapsedRgn.Length);
          }

          endsInNewLine = text[text.Length - 1] == '\r';
        }
        else
        {
          endsInNewLine = Text.EndsWith("\r") || Text.EndsWith("\n");
        }

        Typeface typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
        FormattedText formattedText = new FormattedText(text == null ? Text : text.ToString(), CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, typeface, FontSize, Brushes.Black)
        {
          MaxTextWidth = Math.Max(0, ActualWidth - (Margin.Left + Margin.Right + Padding.Left + Padding.Right + marginWidth)),
          LineHeight = 16, // <-- This should be based off the current font + some padding value. Actipro property?
          Trimming = TextTrimming.None,
        };

        Height = formattedText.Height + (endsInNewLine ? formattedText.LineHeight : 0) + (Padding.Top + Padding.Bottom);
      }
    }

  }
}
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Nick,

We have been working on this a bit as a possible feature addition for the upcoming 2014.1 version. If you would like to help test this with us, please write our support address and reference this thread. Thanks!


Actipro Software Support

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

We've announced this feature on our blog as being included for the 2014.1 version:

http://blog.actiprosoftware.com/post/2014/02/26/SyntaxEditor-Auto-Sizing


Actipro Software Support

The latest build of this product (v24.1.1) 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.