1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/External/Aga.Controls/Tree/NodeControls/BaseTextControl.cs Sun May 27 15:16:19 2012 +0000
1.3 @@ -0,0 +1,314 @@
1.4 +using System;
1.5 +using System.Collections.Generic;
1.6 +using System.Text;
1.7 +using System.Drawing;
1.8 +using System.Windows.Forms;
1.9 +using System.Reflection;
1.10 +using System.ComponentModel;
1.11 +
1.12 +namespace Aga.Controls.Tree.NodeControls
1.13 +{
1.14 + public abstract class BaseTextControl : EditableControl
1.15 + {
1.16 + private TextFormatFlags _baseFormatFlags;
1.17 + private TextFormatFlags _formatFlags;
1.18 + private Pen _focusPen;
1.19 + private StringFormat _format;
1.20 +
1.21 + #region Properties
1.22 +
1.23 + private Font _font = null;
1.24 + public Font Font
1.25 + {
1.26 + get
1.27 + {
1.28 + if (_font == null)
1.29 + return Control.DefaultFont;
1.30 + else
1.31 + return _font;
1.32 + }
1.33 + set
1.34 + {
1.35 + if (value == Control.DefaultFont)
1.36 + _font = null;
1.37 + else
1.38 + _font = value;
1.39 + }
1.40 + }
1.41 +
1.42 + protected bool ShouldSerializeFont()
1.43 + {
1.44 + return (_font != null);
1.45 + }
1.46 +
1.47 + private HorizontalAlignment _textAlign = HorizontalAlignment.Left;
1.48 + [DefaultValue(HorizontalAlignment.Left)]
1.49 + public HorizontalAlignment TextAlign
1.50 + {
1.51 + get { return _textAlign; }
1.52 + set
1.53 + {
1.54 + _textAlign = value;
1.55 + SetFormatFlags();
1.56 + }
1.57 + }
1.58 +
1.59 + private StringTrimming _trimming = StringTrimming.None;
1.60 + [DefaultValue(StringTrimming.None)]
1.61 + public StringTrimming Trimming
1.62 + {
1.63 + get { return _trimming; }
1.64 + set
1.65 + {
1.66 + _trimming = value;
1.67 + SetFormatFlags();
1.68 + }
1.69 + }
1.70 +
1.71 + private bool _displayHiddenContentInToolTip = true;
1.72 + [DefaultValue(true)]
1.73 + public bool DisplayHiddenContentInToolTip
1.74 + {
1.75 + get { return _displayHiddenContentInToolTip; }
1.76 + set { _displayHiddenContentInToolTip = value; }
1.77 + }
1.78 +
1.79 + private bool _useCompatibleTextRendering = false;
1.80 + [DefaultValue(false)]
1.81 + public bool UseCompatibleTextRendering
1.82 + {
1.83 + get { return _useCompatibleTextRendering; }
1.84 + set { _useCompatibleTextRendering = value; }
1.85 + }
1.86 +
1.87 + [DefaultValue(false)]
1.88 + public bool TrimMultiLine
1.89 + {
1.90 + get;
1.91 + set;
1.92 + }
1.93 +
1.94 + #endregion
1.95 +
1.96 + protected BaseTextControl()
1.97 + {
1.98 + IncrementalSearchEnabled = true;
1.99 + _focusPen = new Pen(Color.Black);
1.100 + _focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
1.101 +
1.102 + _format = new StringFormat(StringFormatFlags.LineLimit | StringFormatFlags.NoClip | StringFormatFlags.FitBlackBox | StringFormatFlags.MeasureTrailingSpaces);
1.103 + _baseFormatFlags = TextFormatFlags.PreserveGraphicsClipping |
1.104 + TextFormatFlags.PreserveGraphicsTranslateTransform;
1.105 + SetFormatFlags();
1.106 + LeftMargin = 3;
1.107 + }
1.108 +
1.109 + private void SetFormatFlags()
1.110 + {
1.111 + _format.Alignment = TextHelper.TranslateAligment(TextAlign);
1.112 + _format.Trimming = Trimming;
1.113 +
1.114 + _formatFlags = _baseFormatFlags | TextHelper.TranslateAligmentToFlag(TextAlign)
1.115 + | TextHelper.TranslateTrimmingToFlag(Trimming);
1.116 + }
1.117 +
1.118 + public override Size MeasureSize(TreeNodeAdv node, DrawContext context)
1.119 + {
1.120 + return GetLabelSize(node, context);
1.121 + }
1.122 +
1.123 + protected Size GetLabelSize(TreeNodeAdv node, DrawContext context)
1.124 + {
1.125 + return GetLabelSize(node, context, GetLabel(node));
1.126 + }
1.127 +
1.128 + protected Size GetLabelSize(TreeNodeAdv node, DrawContext context, string label)
1.129 + {
1.130 + PerformanceAnalyzer.Start("GetLabelSize");
1.131 + CheckThread();
1.132 + Font font = GetDrawingFont(node, context, label);
1.133 + Size s = Size.Empty;
1.134 + if (UseCompatibleTextRendering)
1.135 + s = TextRenderer.MeasureText(label, font);
1.136 + else
1.137 + {
1.138 + SizeF sf = context.Graphics.MeasureString(label, font);
1.139 + s = new Size((int)Math.Ceiling(sf.Width), (int)Math.Ceiling(sf.Height));
1.140 + }
1.141 + PerformanceAnalyzer.Finish("GetLabelSize");
1.142 +
1.143 + if (!s.IsEmpty)
1.144 + return s;
1.145 + else
1.146 + return new Size(10, Font.Height);
1.147 + }
1.148 +
1.149 + protected Font GetDrawingFont(TreeNodeAdv node, DrawContext context, string label)
1.150 + {
1.151 + Font font = context.Font;
1.152 + if (DrawTextMustBeFired(node))
1.153 + {
1.154 + DrawEventArgs args = new DrawEventArgs(node, this, context, label);
1.155 + args.Font = context.Font;
1.156 + OnDrawText(args);
1.157 + font = args.Font;
1.158 + }
1.159 + return font;
1.160 + }
1.161 +
1.162 + protected void SetEditControlProperties(Control control, TreeNodeAdv node)
1.163 + {
1.164 + string label = GetLabel(node);
1.165 + DrawContext context = new DrawContext();
1.166 + context.Font = control.Font;
1.167 + control.Font = GetDrawingFont(node, context, label);
1.168 + }
1.169 +
1.170 + public override void Draw(TreeNodeAdv node, DrawContext context)
1.171 + {
1.172 + if (context.CurrentEditorOwner == this && node == Parent.CurrentNode)
1.173 + return;
1.174 +
1.175 + PerformanceAnalyzer.Start("BaseTextControl.Draw");
1.176 + string label = GetLabel(node);
1.177 + Rectangle bounds = GetBounds(node, context);
1.178 + Rectangle focusRect = new Rectangle(bounds.X, context.Bounds.Y,
1.179 + bounds.Width, context.Bounds.Height);
1.180 +
1.181 + Brush backgroundBrush;
1.182 + Color textColor;
1.183 + Font font;
1.184 + CreateBrushes(node, context, label, out backgroundBrush, out textColor, out font, ref label);
1.185 +
1.186 + if (backgroundBrush != null)
1.187 + context.Graphics.FillRectangle(backgroundBrush, focusRect);
1.188 + if (context.DrawFocus)
1.189 + {
1.190 + focusRect.Width--;
1.191 + focusRect.Height--;
1.192 + if (context.DrawSelection == DrawSelectionMode.None)
1.193 + _focusPen.Color = SystemColors.ControlText;
1.194 + else
1.195 + _focusPen.Color = SystemColors.InactiveCaption;
1.196 + context.Graphics.DrawRectangle(_focusPen, focusRect);
1.197 + }
1.198 +
1.199 + PerformanceAnalyzer.Start("BaseTextControl.DrawText");
1.200 + if (UseCompatibleTextRendering)
1.201 + TextRenderer.DrawText(context.Graphics, label, font, bounds, textColor, _formatFlags);
1.202 + else
1.203 + context.Graphics.DrawString(label, font, GetFrush(textColor), bounds, _format);
1.204 + PerformanceAnalyzer.Finish("BaseTextControl.DrawText");
1.205 +
1.206 + PerformanceAnalyzer.Finish("BaseTextControl.Draw");
1.207 + }
1.208 +
1.209 + private static Dictionary<Color, Brush> _brushes = new Dictionary<Color,Brush>();
1.210 + private static Brush GetFrush(Color color)
1.211 + {
1.212 + Brush br;
1.213 + if (_brushes.ContainsKey(color))
1.214 + br = _brushes[color];
1.215 + else
1.216 + {
1.217 + br = new SolidBrush(color);
1.218 + _brushes.Add(color, br);
1.219 + }
1.220 + return br;
1.221 + }
1.222 +
1.223 + private void CreateBrushes(TreeNodeAdv node, DrawContext context, string text, out Brush backgroundBrush, out Color textColor, out Font font, ref string label)
1.224 + {
1.225 + textColor = SystemColors.ControlText;
1.226 + backgroundBrush = null;
1.227 + font = context.Font;
1.228 + if (context.DrawSelection == DrawSelectionMode.Active)
1.229 + {
1.230 + textColor = SystemColors.HighlightText;
1.231 + backgroundBrush = SystemBrushes.Highlight;
1.232 + }
1.233 + else if (context.DrawSelection == DrawSelectionMode.Inactive)
1.234 + {
1.235 + textColor = SystemColors.ControlText;
1.236 + backgroundBrush = SystemBrushes.InactiveBorder;
1.237 + }
1.238 + else if (context.DrawSelection == DrawSelectionMode.FullRowSelect)
1.239 + textColor = SystemColors.HighlightText;
1.240 +
1.241 + if (!context.Enabled)
1.242 + textColor = SystemColors.GrayText;
1.243 +
1.244 + if (DrawTextMustBeFired(node))
1.245 + {
1.246 + DrawEventArgs args = new DrawEventArgs(node, this, context, text);
1.247 + args.Text = label;
1.248 + args.TextColor = textColor;
1.249 + args.BackgroundBrush = backgroundBrush;
1.250 + args.Font = font;
1.251 +
1.252 + OnDrawText(args);
1.253 +
1.254 + textColor = args.TextColor;
1.255 + backgroundBrush = args.BackgroundBrush;
1.256 + font = args.Font;
1.257 + label = args.Text;
1.258 + }
1.259 + }
1.260 +
1.261 + public string GetLabel(TreeNodeAdv node)
1.262 + {
1.263 + if (node != null && node.Tag != null)
1.264 + {
1.265 + object obj = GetValue(node);
1.266 + if (obj != null)
1.267 + return FormatLabel(obj);
1.268 + }
1.269 + return string.Empty;
1.270 + }
1.271 +
1.272 + protected virtual string FormatLabel(object obj)
1.273 + {
1.274 + var res = obj.ToString();
1.275 + if (TrimMultiLine && res != null)
1.276 + {
1.277 + string[] parts = res.Split('\n');
1.278 + if (parts.Length > 1)
1.279 + return parts[0] + "...";
1.280 + }
1.281 + return res;
1.282 + }
1.283 +
1.284 + public void SetLabel(TreeNodeAdv node, string value)
1.285 + {
1.286 + SetValue(node, value);
1.287 + }
1.288 +
1.289 + protected override void Dispose(bool disposing)
1.290 + {
1.291 + base.Dispose(disposing);
1.292 + if (disposing)
1.293 + {
1.294 + _focusPen.Dispose();
1.295 + _format.Dispose();
1.296 + }
1.297 + }
1.298 +
1.299 + /// <summary>
1.300 + /// Fires when control is going to draw a text. Can be used to change text or back color
1.301 + /// </summary>
1.302 + public event EventHandler<DrawEventArgs> DrawText;
1.303 + protected virtual void OnDrawText(DrawEventArgs args)
1.304 + {
1.305 + TreeViewAdv tree = args.Node.Tree;
1.306 + if (tree != null)
1.307 + tree.FireDrawControl(args);
1.308 + if (DrawText != null)
1.309 + DrawText(this, args);
1.310 + }
1.311 +
1.312 + protected virtual bool DrawTextMustBeFired(TreeNodeAdv node)
1.313 + {
1.314 + return DrawText != null || (node.Tree != null && node.Tree.DrawControlMustBeFired());
1.315 + }
1.316 + }
1.317 +}