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);
}
}
}