context: using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Drawing;
  using System.Windows.Forms;
  using System.Reflection;
  using System.ComponentModel;
  
  namespace Aga.Controls.Tree.NodeControls
  {
  	public abstract class BaseTextControl : EditableControl
  	{
  		private TextFormatFlags _baseFormatFlags;
          private TextFormatFlags _formatFlags;
          private Pen _focusPen;
  		private StringFormat _format;
  
  		#region Properties
  
  		private Font _font = null;
  		public Font Font
  		{
  			get
  			{
  				if (_font == null)
  					return Control.DefaultFont;
  				else
  					return _font;
  			}
  			set
  			{
  				if (value == Control.DefaultFont)
  					_font = null;
  				else
  					_font = value;
  			}
  		}
  
  		protected bool ShouldSerializeFont()
  		{
  			return (_font != null);
  		}
  
  		private HorizontalAlignment _textAlign = HorizontalAlignment.Left;
  		[DefaultValue(HorizontalAlignment.Left)]
  		public HorizontalAlignment TextAlign
  		{
  			get { return _textAlign; }
  			set 
  			{ 
  				_textAlign = value;
  				SetFormatFlags();
  			}
  		}
  
  		private StringTrimming _trimming = StringTrimming.None;
  		[DefaultValue(StringTrimming.None)]
  		public StringTrimming Trimming
  		{
  			get { return _trimming; }
  			set 
  			{ 
  				_trimming = value;
  				SetFormatFlags();
  			}
  		}
  
  		private bool _displayHiddenContentInToolTip = true;
  		[DefaultValue(true)]
  		public bool DisplayHiddenContentInToolTip
  		{
  			get { return _displayHiddenContentInToolTip; }
  			set { _displayHiddenContentInToolTip = value; }
  		}
  
  		private bool _useCompatibleTextRendering = false;
  		[DefaultValue(false)]
  		public bool UseCompatibleTextRendering
  		{
  			get { return _useCompatibleTextRendering; }
  			set { _useCompatibleTextRendering = value; }
  		}
  
  		[DefaultValue(false)]
  		public bool TrimMultiLine
  		{
  			get;
  			set;
  		}
  
  		#endregion
  
  		protected BaseTextControl()
  		{
  			IncrementalSearchEnabled = true;
  			_focusPen = new Pen(Color.Black);
  			_focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
  
  			_format = new StringFormat(StringFormatFlags.LineLimit | StringFormatFlags.NoClip | StringFormatFlags.FitBlackBox | StringFormatFlags.MeasureTrailingSpaces);
  			_baseFormatFlags = TextFormatFlags.PreserveGraphicsClipping |
  						   TextFormatFlags.PreserveGraphicsTranslateTransform;
  			SetFormatFlags();
  			LeftMargin = 3;
  		}
  
  		private void SetFormatFlags()
  		{
  			_format.Alignment = TextHelper.TranslateAligment(TextAlign);
  			_format.Trimming = Trimming;
  
  			_formatFlags = _baseFormatFlags | TextHelper.TranslateAligmentToFlag(TextAlign)
  				| TextHelper.TranslateTrimmingToFlag(Trimming);
  		}
  
  		public override Size MeasureSize(TreeNodeAdv node, DrawContext context)
  		{
  			return GetLabelSize(node, context);
  		}
  
  		protected Size GetLabelSize(TreeNodeAdv node, DrawContext context)
  		{
  			return GetLabelSize(node, context, GetLabel(node));
  		}
  
  		protected Size GetLabelSize(TreeNodeAdv node, DrawContext context, string label)
  		{
  			PerformanceAnalyzer.Start("GetLabelSize");
  			CheckThread();
  			Font font = GetDrawingFont(node, context, label);
  			Size s = Size.Empty;
  			if (!UseCompatibleTextRendering)
  				s = TextRenderer.MeasureText(label, font);
  			else
  			{
  				SizeF sf = context.Graphics.MeasureString(label, font);
  				s = new Size((int)Math.Ceiling(sf.Width), (int)Math.Ceiling(sf.Height));
  			}
  			PerformanceAnalyzer.Finish("GetLabelSize");
  
  			if (!s.IsEmpty)
  				return s;
  			else
  				return new Size(10, Font.Height);
  		}
  
  		protected Font GetDrawingFont(TreeNodeAdv node, DrawContext context, string label)
  		{
  			Font font = context.Font;
  			if (DrawTextMustBeFired(node))
  			{
  				DrawEventArgs args = new DrawEventArgs(node, this, context, label);
  				args.Font = context.Font;
  				OnDrawText(args);
  				font = args.Font;
  			}
  			return font;
  		}
  
  		protected void SetEditControlProperties(Control control, TreeNodeAdv node)
  		{
  			string label = GetLabel(node);
  			DrawContext context = new DrawContext();
  			context.Font = control.Font;
  			control.Font = GetDrawingFont(node, context, label);
  		}
  
  		public override void Draw(TreeNodeAdv node, DrawContext context)
  		{
  			if (context.CurrentEditorOwner == this && node == Parent.CurrentNode)
  				return;
  
  			PerformanceAnalyzer.Start("BaseTextControl.Draw");
  
  			Rectangle bounds = GetBounds(node, context);
  			Rectangle focusRect = new Rectangle(bounds.X, context.Bounds.Y,	
  				bounds.Width, context.Bounds.Height);
              
              DrawEventArgs args;
  			CreateBrushes(node, context, out args);
  
  			if (args.BackgroundBrush != null)
  				context.Graphics.FillRectangle(args.BackgroundBrush, focusRect);
  			if (context.DrawFocus)
  			{
  				focusRect.Width--;
  				focusRect.Height--;
  				if (context.DrawSelection == DrawSelectionMode.None)
  					_focusPen.Color = SystemColors.ControlText;
  				else
  					_focusPen.Color = SystemColors.InactiveCaption;
  				context.Graphics.DrawRectangle(_focusPen, focusRect);
  			}
  			
  			PerformanceAnalyzer.Start("BaseTextControl.DrawText");
              
              DrawHighLight(context, bounds, args);
              
              if (!UseCompatibleTextRendering)
                  TextRenderer.DrawText(context.Graphics, args.Text, args.Font, bounds, args.TextColor, _formatFlags);
              else
                 context.Graphics.DrawString(args.Text, args.Font, GetFrush(args.TextColor), bounds, _format);
  
  
  			PerformanceAnalyzer.Finish("BaseTextControl.DrawText");
  			PerformanceAnalyzer.Finish("BaseTextControl.Draw");
  		}
  
          private void DrawHighLight(DrawContext context, Rectangle bounds, DrawEventArgs args)
          {
              string label = args.Text;
              if (!String.IsNullOrEmpty(args.HighLightToken) && label.ToLower().Contains(args.HighLightToken.ToLower()))
              {
                  Graphics g = context.Graphics;
                  int numChars = label.Length;
                  CharacterRange[] characterRanges = new CharacterRange[1];
                  int idx = label.ToLower().IndexOf(args.HighLightToken.ToLower());
                  //Get the max width.. for the complete length
  
gt:                 SizeF size = g.MeasureString(label, args.Font);