Added the source code of Aga.Controls (TreeViewAdv for .Net) version 1.7.0.0.
2 using System.Collections.Generic;
5 using System.Windows.Forms;
6 using System.Reflection;
7 using System.ComponentModel;
9 namespace Aga.Controls.Tree.NodeControls
11 public abstract class BaseTextControl : EditableControl
13 private TextFormatFlags _baseFormatFlags;
14 private TextFormatFlags _formatFlags;
15 private Pen _focusPen;
16 private StringFormat _format;
20 private Font _font = null;
26 return Control.DefaultFont;
32 if (value == Control.DefaultFont)
39 protected bool ShouldSerializeFont()
41 return (_font != null);
44 private HorizontalAlignment _textAlign = HorizontalAlignment.Left;
45 [DefaultValue(HorizontalAlignment.Left)]
46 public HorizontalAlignment TextAlign
48 get { return _textAlign; }
56 private StringTrimming _trimming = StringTrimming.None;
57 [DefaultValue(StringTrimming.None)]
58 public StringTrimming Trimming
60 get { return _trimming; }
68 private bool _displayHiddenContentInToolTip = true;
70 public bool DisplayHiddenContentInToolTip
72 get { return _displayHiddenContentInToolTip; }
73 set { _displayHiddenContentInToolTip = value; }
76 private bool _useCompatibleTextRendering = false;
78 public bool UseCompatibleTextRendering
80 get { return _useCompatibleTextRendering; }
81 set { _useCompatibleTextRendering = value; }
85 public bool TrimMultiLine
93 protected BaseTextControl()
95 IncrementalSearchEnabled = true;
96 _focusPen = new Pen(Color.Black);
97 _focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
99 _format = new StringFormat(StringFormatFlags.LineLimit | StringFormatFlags.NoClip | StringFormatFlags.FitBlackBox | StringFormatFlags.MeasureTrailingSpaces);
100 _baseFormatFlags = TextFormatFlags.PreserveGraphicsClipping |
101 TextFormatFlags.PreserveGraphicsTranslateTransform;
106 private void SetFormatFlags()
108 _format.Alignment = TextHelper.TranslateAligment(TextAlign);
109 _format.Trimming = Trimming;
111 _formatFlags = _baseFormatFlags | TextHelper.TranslateAligmentToFlag(TextAlign)
112 | TextHelper.TranslateTrimmingToFlag(Trimming);
115 public override Size MeasureSize(TreeNodeAdv node, DrawContext context)
117 return GetLabelSize(node, context);
120 protected Size GetLabelSize(TreeNodeAdv node, DrawContext context)
122 return GetLabelSize(node, context, GetLabel(node));
125 protected Size GetLabelSize(TreeNodeAdv node, DrawContext context, string label)
127 PerformanceAnalyzer.Start("GetLabelSize");
129 Font font = GetDrawingFont(node, context, label);
131 if (UseCompatibleTextRendering)
132 s = TextRenderer.MeasureText(label, font);
135 SizeF sf = context.Graphics.MeasureString(label, font);
136 s = new Size((int)Math.Ceiling(sf.Width), (int)Math.Ceiling(sf.Height));
138 PerformanceAnalyzer.Finish("GetLabelSize");
143 return new Size(10, Font.Height);
146 protected Font GetDrawingFont(TreeNodeAdv node, DrawContext context, string label)
148 Font font = context.Font;
149 if (DrawTextMustBeFired(node))
151 DrawEventArgs args = new DrawEventArgs(node, this, context, label);
152 args.Font = context.Font;
159 protected void SetEditControlProperties(Control control, TreeNodeAdv node)
161 string label = GetLabel(node);
162 DrawContext context = new DrawContext();
163 context.Font = control.Font;
164 control.Font = GetDrawingFont(node, context, label);
167 public override void Draw(TreeNodeAdv node, DrawContext context)
169 if (context.CurrentEditorOwner == this && node == Parent.CurrentNode)
172 PerformanceAnalyzer.Start("BaseTextControl.Draw");
173 string label = GetLabel(node);
174 Rectangle bounds = GetBounds(node, context);
175 Rectangle focusRect = new Rectangle(bounds.X, context.Bounds.Y,
176 bounds.Width, context.Bounds.Height);
178 Brush backgroundBrush;
181 CreateBrushes(node, context, label, out backgroundBrush, out textColor, out font, ref label);
183 if (backgroundBrush != null)
184 context.Graphics.FillRectangle(backgroundBrush, focusRect);
185 if (context.DrawFocus)
189 if (context.DrawSelection == DrawSelectionMode.None)
190 _focusPen.Color = SystemColors.ControlText;
192 _focusPen.Color = SystemColors.InactiveCaption;
193 context.Graphics.DrawRectangle(_focusPen, focusRect);
196 PerformanceAnalyzer.Start("BaseTextControl.DrawText");
197 if (UseCompatibleTextRendering)
198 TextRenderer.DrawText(context.Graphics, label, font, bounds, textColor, _formatFlags);
200 context.Graphics.DrawString(label, font, GetFrush(textColor), bounds, _format);
201 PerformanceAnalyzer.Finish("BaseTextControl.DrawText");
203 PerformanceAnalyzer.Finish("BaseTextControl.Draw");
206 private static Dictionary<Color, Brush> _brushes = new Dictionary<Color,Brush>();
207 private static Brush GetFrush(Color color)
210 if (_brushes.ContainsKey(color))
211 br = _brushes[color];
214 br = new SolidBrush(color);
215 _brushes.Add(color, br);
220 private void CreateBrushes(TreeNodeAdv node, DrawContext context, string text, out Brush backgroundBrush, out Color textColor, out Font font, ref string label)
222 textColor = SystemColors.ControlText;
223 backgroundBrush = null;
225 if (context.DrawSelection == DrawSelectionMode.Active)
227 textColor = SystemColors.HighlightText;
228 backgroundBrush = SystemBrushes.Highlight;
230 else if (context.DrawSelection == DrawSelectionMode.Inactive)
232 textColor = SystemColors.ControlText;
233 backgroundBrush = SystemBrushes.InactiveBorder;
235 else if (context.DrawSelection == DrawSelectionMode.FullRowSelect)
236 textColor = SystemColors.HighlightText;
238 if (!context.Enabled)
239 textColor = SystemColors.GrayText;
241 if (DrawTextMustBeFired(node))
243 DrawEventArgs args = new DrawEventArgs(node, this, context, text);
245 args.TextColor = textColor;
246 args.BackgroundBrush = backgroundBrush;
251 textColor = args.TextColor;
252 backgroundBrush = args.BackgroundBrush;
258 public string GetLabel(TreeNodeAdv node)
260 if (node != null && node.Tag != null)
262 object obj = GetValue(node);
264 return FormatLabel(obj);
269 protected virtual string FormatLabel(object obj)
271 var res = obj.ToString();
272 if (TrimMultiLine && res != null)
274 string[] parts = res.Split('\n');
275 if (parts.Length > 1)
276 return parts[0] + "...";
281 public void SetLabel(TreeNodeAdv node, string value)
283 SetValue(node, value);
286 protected override void Dispose(bool disposing)
288 base.Dispose(disposing);
297 /// Fires when control is going to draw a text. Can be used to change text or back color
299 public event EventHandler<DrawEventArgs> DrawText;
300 protected virtual void OnDrawText(DrawEventArgs args)
302 TreeViewAdv tree = args.Node.Tree;
304 tree.FireDrawControl(args);
305 if (DrawText != null)
306 DrawText(this, args);
309 protected virtual bool DrawTextMustBeFired(TreeNodeAdv node)
311 return DrawText != null || (node.Tree != null && node.Tree.DrawControlMustBeFired());