public Paper() { // paper properties Background = new SolidColorBrush(Colors.White); Cursor = Cursors.IBeam; // setup caret caret = new Rectangle(); caret.Fill = new SolidColorBrush(Colors.Black); caret.Height = 12; caret.Width = 1; caret.Margin = new Thickness(-1, 0, 0, 0); // setup caret timer caretTimer = new DispatcherTimer(); caretTimer.Interval = TimeSpan.FromSeconds(0.5); caretTimer.Tick += delegate { caret.Visibility = (caret.Visibility == Visibility.Visible || !ShowCaret) ? Visibility.Collapsed : Visibility.Visible; }; caretTimer.Start(); // setup highlighting indicies HighlightedBlocks = new List<Character>(); HighlightFromIndex = new Index(0, 0); HighlightToIndex = new Index(0, 0); // events MouseLeftButtonDown += Paper_MouseLeftButtonDown; MouseLeftButtonUp += Paper_MouseLeftButtonUp; }
public void HighlightUpto(int line, int pos) { var newBlocks = new List<Character>(); if (HighlightToIndex != null && HighlightToIndex.Line == line && HighlightToIndex.Position == pos) return; // get blocks int fromLine = 0, fromPos = 0, toLine = 0, toPos = 0; PaperLine pLine; if (HighlightFromIndex.Line == line) { pLine = (PaperLine)Children[line]; fromPos = Math.Min(HighlightFromIndex.Position, pos); toPos = Math.Max(HighlightFromIndex.Position, pos); newBlocks.AddRange(pLine.GetCharacters(fromPos, toPos)); } else if (HighlightFromIndex.Line > line) { fromLine = line; toLine = HighlightFromIndex.Line; for (; fromLine <= toLine; fromLine++) { pLine = (PaperLine)Children[fromLine]; fromPos = (fromLine == line) ? pos : 0; toPos = (fromLine == HighlightFromIndex.Line) ? HighlightFromIndex.Position - 1 : pLine.LastIndex; newBlocks.AddRange(pLine.GetCharacters(fromPos, toPos)); } } else if (HighlightFromIndex.Line < line) { fromLine = HighlightFromIndex.Line; toLine = line; for (; fromLine <= toLine; fromLine++) { pLine = (PaperLine)Children[fromLine]; fromPos = (fromLine == HighlightFromIndex.Line) ? HighlightFromIndex.Position : 0; toPos = (fromLine == line) ? pos : pLine.LastIndex; newBlocks.AddRange(pLine.GetCharacters(fromPos, toPos)); } } // set CaretPosition UpdateCaret(line, pos); // the blocks that are already highlighted, but now need to be unhighlighted HighlightedBlocks.Except(newBlocks).ToList().ForEach(b => b.Unhighlight()); // highlight the rest HighlightedBlocks = newBlocks; foreach (var block in HighlightedBlocks) { if (block.CurrentBackgroundState != Character.BackgroundState.Blue) block.HighlightBlue(); } // keep track to be used in RemoveHighlights HighlightToIndex = new Index(line, pos); }
public void HighlightFrom(int line, int pos) { ClearHighlights(); UpdateCaret(line, pos); HighlightFromIndex = new Index(line, pos); }