Exemple #1
0
        private static void OnShowLineNumbersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TextEditor editor      = (TextEditor)d;
            var        leftMargins = editor.TextArea.LeftMargins;

            if ((bool)e.NewValue)
            {
                LineNumberMargin lineNumbers = new LineNumberMargin();
                Line             line        = (Line)DottedLineMargin.Create();
                leftMargins.Insert(0, lineNumbers);
                leftMargins.Insert(1, line);
                var lineNumbersForeground = new Binding("LineNumbersForeground")
                {
                    Source = editor
                };
                line.SetBinding(Line.StrokeProperty, lineNumbersForeground);
                lineNumbers.SetBinding(Control.ForegroundProperty, lineNumbersForeground);
            }
            else
            {
                for (int i = 0; i < leftMargins.Count; i++)
                {
                    if (leftMargins[i] is LineNumberMargin)
                    {
                        leftMargins.RemoveAt(i);
                        if (i < leftMargins.Count && DottedLineMargin.IsDottedLineMargin(leftMargins[i]))
                        {
                            leftMargins.RemoveAt(i);
                        }
                        break;
                    }
                }
            }
        }
Exemple #2
0
        public CodeTextEditor()
        {
            Options = new TextEditorOptions
            {
                ConvertTabsToSpaces      = true,
                AllowScrollBelowDocument = true,
                IndentationSize          = 4,
                EnableEmailHyperlinks    = false,
            };

            // TODO: remove this after bug fix
#if AVALONIA
            var lineMargin = new LineNumberMargin {
                Margin = new Thickness(0, 0, 10, 0)
            };
            lineMargin[~TextBlock.ForegroundProperty] = this[~LineNumbersForegroundProperty];
            TextArea.LeftMargins.Insert(0, lineMargin);
#else
            ShowLineNumbers = true;
#endif

            TextArea.TextView.VisualLinesChanged += OnVisualLinesChanged;
            TextArea.TextEntering += OnTextEntering;
            TextArea.TextEntered  += OnTextEntered;

#if AVALONIA
            PointerHover        += OnMouseHover;
            PointerHoverStopped += OnMouseHoverStopped;
#else
            MouseHover        += OnMouseHover;
            MouseHoverStopped += OnMouseHoverStopped;

            ToolTipService.SetInitialShowDelay(this, 0);
            SearchReplacePanel.Install(this);
#endif

            var commandBindings   = TextArea.CommandBindings;
            var deleteLineCommand = commandBindings.OfType <CommandBinding>().FirstOrDefault(x =>
                                                                                             x.Command == AvalonEditCommands.DeleteLine);
            if (deleteLineCommand != null)
            {
                commandBindings.Remove(deleteLineCommand);
            }

            var contextMenu = new ContextMenu();
            contextMenu.SetItems(new[]
            {
                new MenuItem {
                    Command = ApplicationCommands.Cut
                },
                new MenuItem {
                    Command = ApplicationCommands.Copy
                },
                new MenuItem {
                    Command = ApplicationCommands.Paste
                }
            });
            ContextMenu = contextMenu;
        }
 // using a method to ensure the local variables can be garbage collected after the method returns
 void DocumentDoesNotHoldReferenceToLineMargin_CreateMargin(TextDocument textDocument, Action finalizeAction)
 {
     TextView textView = new TextViewWithGCCallback(finalizeAction)
     {
         Document = textDocument
     };
     LineNumberMargin margin = new LineNumberMargin()
     {
         TextView = textView
     };
 }
        partial void Initialize()
        {
            var lineMargin = new LineNumberMargin {
                Margin = new Thickness(0, 0, 10, 0)
            };

            lineMargin[~TextBlock.ForegroundProperty] = this[~LineNumbersForegroundProperty];
            TextArea.LeftMargins.Insert(0, lineMargin);

            PointerHover        += OnMouseHover;
            PointerHoverStopped += OnMouseHoverStopped;
        }
        WeakReference DocumentDoesNotHoldReferenceToLineMargin_CreateMargin(TextDocument textDocument)
        {
            TextView textView = new TextView()
            {
                Document = textDocument
            };
            LineNumberMargin margin = new LineNumberMargin()
            {
                TextView = textView
            };

            return(new WeakReference(textView));
        }
        private static void addLineNumberMarching(TextEditor _editor, LineNumberMargin lineNumbers)
        {
            var leftMargins = _editor.TextArea.LeftMargins;

            Line line = (Line)DottedLineMargin.Create();

            leftMargins.Insert(0, lineNumbers);
            leftMargins.Insert(1, line);
            var lineNumbersForeground = new Binding("LineNumbersForeground")
            {
                Source = _editor
            };

            line.SetBinding(Line.StrokeProperty, lineNumbersForeground);
            lineNumbers.SetBinding(Control.ForegroundProperty, lineNumbersForeground);
        }
        public MainWindow()
        {
            InitializeComponent();
            this.AttachDevTools();

            _textEditor = this.FindControl <TextEditor>("Editor");
            _textEditor.SyntaxHighlighting     = HighlightingManager.Instance.GetDefinition("C#");
            _textEditor.TextArea.TextEntering += textEditor_TextArea_TextEntering;
            _textEditor.TextArea.TextEntered  += textEditor_TextArea_TextEntered;
            var lineNumberMargin = new LineNumberMargin {
                Margin = new Thickness(0, 0, 10, 0)
            };

            TextBlock.SetForeground(lineNumberMargin, Brushes.Gray);
            _textEditor.TextArea.LeftMargins.Add(lineNumberMargin);
            _textEditor.TextArea.IndentationStrategy = new Indentation.CSharp.CSharpIndentationStrategy( );
        }
        public TextViewerControl()
        {
            InitializeComponent();

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                textEditor.FontFamily = "Consolas";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                textEditor.FontFamily = "Menlo";
            }
            else
            {
                textEditor.FontFamily = "Monospace";
            }

            // TOOD: add
            //SearchPanel.Install(textEditor.TextArea);

            // TODO: remove this after bug fix
            var lineMargin = new LineNumberMargin {
                Margin = new Thickness(0, 0, 10, 0)
            };

            lineMargin[~TextBlock.ForegroundProperty] = textEditor[~TextEditor.LineNumbersForegroundProperty];
            textEditor.TextArea.LeftMargins.Insert(0, lineMargin);

            textEditor.TextArea.PointerPressed += TextAreaMouseRightButtonDown;

            var textView = textEditor.TextArea.TextView;

            textView.CurrentLineBackground = new SolidColorBrush(Color.FromRgb(224, 224, 224));
            textView.CurrentLineBorder     = new Pen(Brushes.Transparent, 0);
            //textView.Options.HighlightCurrentLine = true;
            textView.Options.EnableEmailHyperlinks = false;
            textView.Options.EnableHyperlinks      = false;
            textEditor.IsReadOnly = true;
        }
        public TextArea(TextEditor editor)
        {
            this.editor = editor;

            CanGetFocus = true;

            lineNumberMargin = new LineNumberMargin(editor);
            paddingMargin    = new PaddingMargin(5);
            textViewMargin   = new TextViewMargin(editor);

            margins.Add(lineNumberMargin);
            margins.Add(paddingMargin);
            margins.Add(textViewMargin);

            contextMenu = new Menu();

            cutMenuItem          = new MenuItem("Cut");
            cutMenuItem.Clicked += (sender, e) => Cut();
            contextMenu.Items.Add(cutMenuItem);

            copyMenuItem          = new MenuItem("Copy");
            copyMenuItem.Clicked += (sender, e) => Copy();
            contextMenu.Items.Add(copyMenuItem);

            pasteMenuItem          = new MenuItem("Paste");
            pasteMenuItem.Clicked += (sender, e) => Paste();
            contextMenu.Items.Add(pasteMenuItem);

            contextMenu.Items.Add(new SeparatorMenuItem());

            selectallMenuItem          = new MenuItem("Select All");
            selectallMenuItem.Clicked += (sender, e) => SelectAll();
            contextMenu.Items.Add(selectallMenuItem);

            ButtonPressed += HandleButtonPressed;
        }