Click here to Skip to main content
15,895,084 members
Articles / Desktop Programming / WPF

Code InfoBox Visual Studio Extension (VSX) 2010

Rate me:
Please Sign up or sign in to vote.
4.97/5 (42 votes)
31 Jan 2010CPOL10 min read 121.1K   1.7K   67  
A WPF Infowindow placed on the right hand corner of the Code Canvas which informs you about the code statistics
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Formatting;
using Microsoft.VisualStudio.Text;
using System.Text;
using System.Globalization;
using System.Windows;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.IO;
using System.CodeDom;
using Microsoft.VisualStudio.Utilities;

namespace ViewportAdornment1
{

    /// <summary>
    /// Adornment class that draws a square box in the top right hand corner of the viewport
    /// </summary>
    public class AlwaysVisibleInfoBox
    {

        private ucInfoBox infobox;
        private IWpfTextView _view;
        private IAdornmentLayer _adornmentLayer;
        private CodeInfoTracker _info;
        //private CodeInfoTracker.Calculator s
        /// <summary>
        /// Creates a square image and attaches an event handler to the layout changed event that
        /// adds the the square in the upper right-hand corner of the TextView via the adornment layer
        /// </summary>
        /// <param name="view">The <see cref="IWpfTextView"/> upon which the adornment will be drawn</param>
        public AlwaysVisibleInfoBox(IWpfTextView view)
        {
            _view = view;
            this._info = new CodeInfoTracker(view);
            //CodeInfoTracker.Calculators calc = this._cTracker.PerFormCalculate();
            _view.LayoutChanged += this.OnLayoutChanged;
            this.PlaceUserControl();
        }

        private void PlaceUserControl()
        {
            infobox = new ucInfoBox(this._info);
            _adornmentLayer = this._view.GetAdornmentLayer("AlwaysVisibleInfoBox");

            _view.ViewportHeightChanged += delegate { this.onSizeChange(); };
            _view.ViewportWidthChanged += delegate { this.onSizeChange(); };
        }

        //private void CreateBox()
        //{
        //    //Set the fill color of the square
        //    Brush brush = new LinearGradientBrush(Colors.WhiteSmoke, Colors.AliceBlue, 0.0);
        //    //Brush brush = new SolidColorBrush(Colors.BlueViolet);
        //    brush.Freeze();
        //    //Set the outline color of the square
        //    Brush penBrush = new SolidColorBrush(Colors.Red);
        //    penBrush.Freeze();
        //    Pen pen = new Pen(penBrush, 0.5);
        //    pen.Freeze();

        //    FormattedText formattedText = new FormattedText(string.Format("No of Lines:{0}", this.no_of_lines),
        //                                            CultureInfo.GetCultureInfo("en-us"),
        //                                            FlowDirection.LeftToRight,
        //                                            new Typeface("Verdana"),
        //                                            8,
        //                                            Brushes.Black);

        //    int digitcnt = this.no_of_lines.ToString().Length + 12;
            
        //    formattedText.MaxTextWidth = 300;
        //    formattedText.MaxTextHeight = 240;

        //    formattedText.SetFontSize(8 * (96.0 / 72.0), 0, digitcnt);
        //    formattedText.SetFontWeight(FontWeights.Bold, 0, digitcnt);
        //    formattedText.SetForegroundBrush(
        //                                new LinearGradientBrush(
        //                                Colors.Orange,
        //                                Colors.Teal,
        //                                90.0),
        //                                0, digitcnt);
        //    //formattedText.SetForegroundBrush(
        //    //                            new LinearGradientBrush(
        //    //                            Colors.Gold,
        //    //                            Colors.Violet,
        //    //                            90.0),
        //    //                            digitcnt + 1, charcnt);
        //    formattedText.SetFontStyle(FontStyles.Italic, 0, digitcnt);
        //    formattedText.SetForegroundBrush(brush);

        //    int charcnt = this.no_of_characters.ToString().Length + 16;
        //    FormattedText formattedText1 = new FormattedText(string.Format("No of Characters:{0}", this.no_of_characters),
        //                                CultureInfo.GetCultureInfo("en-us"),
        //                                FlowDirection.LeftToRight,
        //                                new Typeface("Verdana"),
        //                                12,
        //                                Brushes.Black);
                       
        //    formattedText1.MaxTextWidth = 300;
        //    formattedText1.MaxTextHeight = 240;

        //    formattedText1.SetFontSize(8 * (96.0 / 72.0), 0, charcnt);
        //    formattedText1.SetFontWeight(FontWeights.Bold, 0, charcnt);
        //    formattedText1.SetForegroundBrush(
        //                                new LinearGradientBrush(
        //                                Colors.Orange,
        //                                Colors.Teal,
        //                                90.0),
        //                                0, charcnt);
        //    //formattedText.SetForegroundBrush(
        //    //                            new LinearGradientBrush(
        //    //                            Colors.Gold,
        //    //                            Colors.Violet,
        //    //                            90.0),
        //    //                            digitcnt + 1, charcnt);
        //    formattedText1.SetFontStyle(FontStyles.Italic, 0, charcnt);
        //    formattedText1.SetForegroundBrush(brush);

        //    GeometryGroup group = new GeometryGroup();
        //    Geometry formattedTextGroup = formattedText.BuildGeometry(new Point(20, 10));
        //    Geometry formattedTextGroup1 = formattedText1.BuildGeometry(new Point(20, 30));

        //    group.Children.Add(formattedTextGroup);
        //    group.Children.Add(formattedTextGroup1);

        //    //draw a square with the created brush and pen
        //    System.Windows.Rect r = new System.Windows.Rect(0, 0, 150, 50);
        //    Geometry g = new RectangleGeometry(r);
        //    group.Children.Add(g);

        //    GeometryDrawing drawing = new GeometryDrawing();
        //    drawing.Geometry = group;
        //    drawing.Brush = brush;
        //    drawing.Pen = pen;
        //    drawing.Freeze();

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

        //    _image = new Image();
        //    _image.Source = drawingImage;

        //    //Grab a reference to the adornment layer that this adornment should be added to
        //    _adornmentLayer = this._view.GetAdornmentLayer("AlwaysVisibleInfoBox");

        //    _view.ViewportHeightChanged += delegate { this.onSizeChange(); };
        //    _view.ViewportWidthChanged += delegate { this.onSizeChange(); };
        //}

        /// <summary>
        /// On layout change add the adornment to any reformated lines
        /// </summary>
        private void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
        {
            string filestring = _view.TextSnapshot.GetText();
            //this.PlaceUserControl();
            this._info = new CodeInfoTracker(_view);
            this.infobox.UpdateInfo(this._info);
        }
             
        /// <summary>
        /// Within the given line add the scarlet box behind the a
        /// </summary>
        private void CalculateDocument(ITextViewLine line)
        {
            //grab a reference to the lines in the current TextView 
            IWpfTextViewLineCollection textViewLines = _view.TextViewLines;
            int start = line.Start;
            int end = line.End;

        }

        public void onSizeChange()
        {
            //clear the adornment layer of previous adornments
            _adornmentLayer.RemoveAllAdornments();

            //Place the image in the top right hand corner of the Viewport
            Canvas.SetLeft(infobox, _view.ViewportRight - 255);
            Canvas.SetTop(infobox, _view.ViewportTop + 10);
            
            //add the image to the adornment layer and make it relative to the viewport
            _adornmentLayer.AddAdornment(AdornmentPositioningBehavior.ViewportRelative, null, null, infobox, null);
        }


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President
India India
Did you like his post?

Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook

Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.

Working as a VP product of APPSeCONNECT, an integration platform of future, he does all sort of innovation around the product.

Have any problem? Write to him in his Forum.

You can also mail him directly to abhi2434@yahoo.com

Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com

Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing

Comments and Discussions