public ITextPosition GetTextPosition(Point p) { int l = 0, r = textline.Length; TextBounds bound = null; double ml = TextCore.View.MarginLeft; if (p.X < ml) { return(null); } while (r - l > 1) { int mid = (l + r) >> 1; bound = textline.GetTextBounds(l, mid - l).FirstOrDefault(); if (p.X - ml >= bound.Rectangle.Right) { l = mid; } else { r = mid; } } if (l == r) { return(GetTextPosition(l + 1)); } l = Math.Min(l, r); bound = textline.GetTextBounds(l, 1).FirstOrDefault(); return((p.X - ml >= bound.Rectangle.Right) ? GetTextPosition(l + 2) : GetTextPosition(l + 1)); }
protected void DrawTextRectangle(DrawingContext ctx, Brush brush, Pen pen, int start, int count) { if (count <= 0) { return; } WinTextLine text = ViewParent?.TextLine; TextBounds bound = text.GetTextBounds(start, count).FirstOrDefault(); Rect rect = bound.Rectangle; rect.X += ViewParent.TextCore.View.MarginLeft; ctx.DrawRectangle(brush, null, rect); }
protected override void Render(DrawingContext ctx) { if (Core == null || Core.IsDisposed) { return; } base.Render(ctx); IMRATextItemInfo item = ViewParent?.Core; ITextBoxCore core = ViewParent?.TextCore; WinTextLine text = ViewParent?.TextLine; if (core == null) { return; } ITextPosition start = core.SelectedStart; ITextPosition end = core.SelectedEnd; if (start.CompareTo(end) == 0) { ITextPosition pos = start.NextSeek(); RenderBracket(ctx, pos); RenderDefaultMatch(ctx, pos); pos = pos.PrevSeek(); RenderBracket(ctx, pos); RenderDefaultMatch(ctx, pos); } if (start.Line == end.Line && start.Column == end.Column) { status = start.Line == item.Line ? SelectionStatus.Caret : SelectionStatus.None; } else if (item.Line >= start.Line && item.Line <= end.Line) { status = SelectionStatus.Range; } else { status = SelectionStatus.None; } switch (status) { case SelectionStatus.None: break; case SelectionStatus.Caret: { if (!blinkshow && !forceshow) { break; } int index = start.Column - 1; TextBounds bound = text.GetTextBounds(index, 1).FirstOrDefault(); Brush brush = null; double thickness = 1.0; ViewParent.TextCore.DictBrush.TryGetValue("foreground_rawtext_caret", out brush); ViewParent.TextCore.DictValue.TryGetValue("rawtext_caret_thickness", out thickness); Pen pen = new Pen(brush, thickness); Point p1 = bound.Rectangle.TopLeft; Point p2 = bound.Rectangle.BottomLeft; p1.Y -= 2; p1.X += ViewParent.TextCore.View.MarginLeft; p2.X += ViewParent.TextCore.View.MarginLeft; ctx.DrawLine(pen, p1, p2); } break; case SelectionStatus.Range: { //int left = start.Column - 1; //int right = end.Column - 1; if (start.Line < item.Line) { start = Core.Start; } if (end.Line > item.Line) { end = Core.End; } //if (right - left <= 0) break; //TextBounds bound = text.GetTextBounds(left, right - left).FirstOrDefault(); Brush brush = null; //Rect rect = bound.Rectangle; ViewParent.TextCore.DictBrush.TryGetValue("background_rawtext_selected", out brush); //rect.X += ViewParent.TextCore.View.MarginLeft; //ctx.DrawRectangle(brush, null, rect); DrawTextRectangle(ctx, brush, null, start, end); } break; } }