Example #1
0
        /// <summary>
        /// Adds the scarlet box behind the 'a' characters within the given line
        /// </summary>
        /// <param name="line">Line to add the adornments</param>

        /*
         * private void CreateVisuals(ITextViewLine line)
         * {
         *  IWpfTextViewLineCollection textViewLines = this.view.TextViewLines;
         *
         *  // Loop through each character, and place a box around any 'a'
         *  for (int charIndex = line.Start; charIndex < line.End; charIndex++)
         *  {
         *      if (this.view.TextSnapshot[charIndex] == 'a')
         *      {
         *          SnapshotSpan span = new SnapshotSpan(this.view.TextSnapshot, Span.FromBounds(charIndex, charIndex + 1));
         *          Geometry geometry = textViewLines.GetMarkerGeometry(span);
         *          if (geometry != null)
         *          {
         *              var drawing = new GeometryDrawing(this.brush, this.pen, geometry);
         *              drawing.Freeze();
         *
         *              var drawingImage = new DrawingImage(drawing);
         *              drawingImage.Freeze();
         *
         *              var image = new Image
         *              {
         *                  Source = drawingImage,
         *              };
         *
         *              // Align the image with the top of the bounds of the text geometry
         *              Canvas.SetLeft(image, geometry.Bounds.Left);
         *              Canvas.SetTop(image, geometry.Bounds.Top);
         *
         *              this.layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, span, null, image, null);
         *          }
         *      }
         *  }
         * }
         */
        private void HighlightCoverage(CoverageState[] coverdata, ProfileVector profiledata, ITextViewLine line)
        {
            if (view == null || profiledata == null || line == null || view.TextSnapshot == null)
            {
                return;
            }

            IWpfTextViewLineCollection textViewLines = view.TextViewLines;

            if (textViewLines == null || line.Extent == null)
            {
                return;
            }

            int lineno = 1 + view.TextSnapshot.GetLineNumberFromPosition(line.Extent.Start);

            CoverageState covered = lineno < coverdata.Length ? coverdata[lineno] : CoverageState.Irrelevant;

            if (covered != CoverageState.Irrelevant)
            {
                SnapshotSpan span = new SnapshotSpan(view.TextSnapshot, Span.FromBounds(line.Start, line.End));
                Geometry     g    = textViewLines.GetMarkerGeometry(span);

                if (g != null)
                {
                    g = new RectangleGeometry(new Rect(g.Bounds.X, g.Bounds.Y, view.ViewportWidth, g.Bounds.Height));

                    GeometryDrawing drawing = (covered == CoverageState.Covered) ?
                                              new GeometryDrawing(coveredBrush, coveredPen, g) :
                                              new GeometryDrawing(uncoveredBrush, uncoveredPen, g);

                    drawing.Freeze();

                    DrawingImage drawingImage = new DrawingImage(drawing);
                    drawingImage.Freeze();

                    Image image = new Image();
                    image.Source = drawingImage;

                    //Align the image with the top of the bounds of the text geometry
                    Canvas.SetLeft(image, g.Bounds.Left);
                    Canvas.SetTop(image, g.Bounds.Top);

                    layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, span, null, image, null);
                }
            }

            var profile = profiledata.Get(lineno);

            if (profile != null && profile.Item1 != 0 || profile.Item2 != 0)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append(profile.Item1);
                sb.Append("%/");
                sb.Append(profile.Item2);
                sb.Append("%");

                SnapshotSpan span = new SnapshotSpan(view.TextSnapshot, Span.FromBounds(line.Start, line.End));
                Geometry     g    = textViewLines.GetMarkerGeometry(span);

                if (g != null) // Yes, this apparently happens...
                {
                    double x = g.Bounds.X + g.Bounds.Width + 20;
                    if (x < view.ViewportWidth / 2)
                    {
                        x = view.ViewportWidth / 2;
                    }
                    g = new RectangleGeometry(new Rect(x, g.Bounds.Y, 30, g.Bounds.Height));

                    Label lbl = new Label();
                    lbl.FontSize   = 7;
                    lbl.Foreground = Brushes.Black;
                    lbl.Background = Brushes.Transparent;
                    lbl.FontFamily = new FontFamily("Verdana");
                    lbl.Content    = sb.ToString();

                    Canvas.SetLeft(lbl, g.Bounds.Left);
                    Canvas.SetTop(lbl, g.Bounds.Top);

                    layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, span, null, lbl, null);
                }
            }
        }