Added the source code of Aga.Controls (TreeViewAdv for .Net) version 1.7.0.0.
2 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Windows.Forms;
7 using System.Windows.Forms.VisualStyles;
8 using System.Drawing.Imaging;
10 namespace Aga.Controls.Tree
12 [TypeConverter(typeof(TreeColumn.TreeColumnConverter)), DesignTimeVisible(false), ToolboxItem(false)]
13 public class TreeColumn : Component
15 private class TreeColumnConverter : ComponentConverter
17 public TreeColumnConverter()
18 : base(typeof(TreeColumn))
22 public override bool GetPropertiesSupported(ITypeDescriptorContext context)
28 private const int HeaderLeftMargin = 5;
29 private const int HeaderRightMargin = 5;
30 private const int SortOrderMarkMargin = 8;
32 private TextFormatFlags _headerFlags;
33 private TextFormatFlags _baseHeaderFlags = TextFormatFlags.NoPadding |
34 TextFormatFlags.EndEllipsis |
35 TextFormatFlags.VerticalCenter |
36 TextFormatFlags.PreserveGraphicsTranslateTransform;
40 private TreeColumnCollection _owner;
41 internal TreeColumnCollection Owner
43 get { return _owner; }
44 set { _owner = value; }
53 return Owner.IndexOf(this);
59 private string _header;
63 get { return _header; }
71 private string _tooltipText;
73 public string TooltipText
75 get { return _tooltipText; }
76 set { _tooltipText = value; }
80 [DefaultValue(50), Localizable(true)]
91 _width = Math.Max(MinColumnWidth, value);
92 if (_maxColumnWidth > 0)
94 _width = Math.Min(_width, MaxColumnWidth);
101 private int _minColumnWidth;
103 public int MinColumnWidth
105 get { return _minColumnWidth; }
109 throw new ArgumentOutOfRangeException("value");
111 _minColumnWidth = value;
112 Width = Math.Max(value, Width);
116 private int _maxColumnWidth;
118 public int MaxColumnWidth
120 get { return _maxColumnWidth; }
124 throw new ArgumentOutOfRangeException("value");
126 _maxColumnWidth = value;
128 Width = Math.Min(value, _width);
132 private bool _visible = true;
134 public bool IsVisible
136 get { return _visible; }
140 OnIsVisibleChanged();
144 private HorizontalAlignment _textAlign = HorizontalAlignment.Left;
145 [DefaultValue(HorizontalAlignment.Left)]
146 public HorizontalAlignment TextAlign
148 get { return _textAlign; }
151 if (value != _textAlign)
154 _headerFlags = _baseHeaderFlags | TextHelper.TranslateAligmentToFlag(value);
160 private bool _sortable = false;
161 [DefaultValue(false)]
164 get { return _sortable; }
165 set { _sortable = value; }
168 private SortOrder _sort_order = SortOrder.None;
169 public SortOrder SortOrder
171 get { return _sort_order; }
174 if (value == _sort_order)
177 OnSortOrderChanged();
181 public Size SortMarkSize
185 if (Application.RenderWithVisualStyles)
186 return new Size(9, 5);
188 return new Size(7, 4);
194 this(string.Empty, 50)
198 public TreeColumn(string header, int width)
202 _headerFlags = _baseHeaderFlags | TextFormatFlags.Left;
205 public override string ToString()
207 if (string.IsNullOrEmpty(Header))
208 return GetType().Name;
213 protected override void Dispose(bool disposing)
215 base.Dispose(disposing);
220 private static VisualStyleRenderer _normalRenderer;
221 private static VisualStyleRenderer _pressedRenderer;
222 private static VisualStyleRenderer _hotRenderer;
224 private static void CreateRenderers()
226 if (Application.RenderWithVisualStyles && _normalRenderer == null)
228 _normalRenderer = new VisualStyleRenderer(VisualStyleElement.Header.Item.Normal);
229 _pressedRenderer = new VisualStyleRenderer(VisualStyleElement.Header.Item.Pressed);
230 _hotRenderer = new VisualStyleRenderer(VisualStyleElement.Header.Item.Hot);
234 internal Bitmap CreateGhostImage(Rectangle bounds, Font font)
236 Bitmap b = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
237 Graphics gr = Graphics.FromImage(b);
238 gr.FillRectangle(SystemBrushes.ControlDark, bounds);
239 DrawContent(gr, bounds, font);
240 BitmapHelper.SetAlphaChanelValue(b, 150);
244 internal void Draw(Graphics gr, Rectangle bounds, Font font, bool pressed, bool hot)
246 DrawBackground(gr, bounds, pressed, hot);
247 DrawContent(gr, bounds, font);
250 private void DrawContent(Graphics gr, Rectangle bounds, Font font)
252 Rectangle innerBounds = new Rectangle(bounds.X + HeaderLeftMargin, bounds.Y,
253 bounds.Width - HeaderLeftMargin - HeaderRightMargin,
256 if (SortOrder != SortOrder.None)
257 innerBounds.Width -= (SortMarkSize.Width + SortOrderMarkMargin);
259 Size maxTextSize = TextRenderer.MeasureText(gr, Header, font, innerBounds.Size, TextFormatFlags.NoPadding);
260 Size textSize = TextRenderer.MeasureText(gr, Header, font, innerBounds.Size, _baseHeaderFlags);
262 if (SortOrder != SortOrder.None)
264 int tw = Math.Min(textSize.Width, innerBounds.Size.Width);
267 if (TextAlign == HorizontalAlignment.Left)
268 x = innerBounds.X + tw + SortOrderMarkMargin;
269 else if (TextAlign == HorizontalAlignment.Right)
270 x = innerBounds.Right + SortOrderMarkMargin;
272 x = innerBounds.X + tw + (innerBounds.Width - tw) / 2 + SortOrderMarkMargin;
273 DrawSortMark(gr, bounds, x);
276 if (textSize.Width < maxTextSize.Width)
277 TextRenderer.DrawText(gr, Header, font, innerBounds, SystemColors.ControlText, _baseHeaderFlags | TextFormatFlags.Left);
279 TextRenderer.DrawText(gr, Header, font, innerBounds, SystemColors.ControlText, _headerFlags);
282 private void DrawSortMark(Graphics gr, Rectangle bounds, int x)
284 int y = bounds.Y + bounds.Height / 2 - 2;
285 x = Math.Max(x, bounds.X + SortOrderMarkMargin);
287 int w2 = SortMarkSize.Width / 2;
288 if (SortOrder == SortOrder.Ascending)
290 Point[] points = new Point[] { new Point(x, y), new Point(x + SortMarkSize.Width, y), new Point(x + w2, y + SortMarkSize.Height) };
291 gr.FillPolygon(SystemBrushes.ControlDark, points);
293 else if (SortOrder == SortOrder.Descending)
295 Point[] points = new Point[] { new Point(x - 1, y + SortMarkSize.Height), new Point(x + SortMarkSize.Width, y + SortMarkSize.Height), new Point(x + w2, y - 1) };
296 gr.FillPolygon(SystemBrushes.ControlDark, points);
300 internal static void DrawDropMark(Graphics gr, Rectangle rect)
302 gr.FillRectangle(SystemBrushes.HotTrack, rect.X-1, rect.Y, 2, rect.Height);
305 internal static void DrawBackground(Graphics gr, Rectangle bounds, bool pressed, bool hot)
307 if (Application.RenderWithVisualStyles)
311 _pressedRenderer.DrawBackground(gr, bounds);
313 _hotRenderer.DrawBackground(gr, bounds);
315 _normalRenderer.DrawBackground(gr, bounds);
319 gr.FillRectangle(SystemBrushes.Control, bounds);
320 Pen p1 = SystemPens.ControlLightLight;
321 Pen p2 = SystemPens.ControlDark;
322 Pen p3 = SystemPens.ControlDarkDark;
324 gr.DrawRectangle(p2, bounds.X, bounds.Y, bounds.Width, bounds.Height);
327 gr.DrawLine(p1, bounds.X, bounds.Y, bounds.Right, bounds.Y);
328 gr.DrawLine(p3, bounds.X, bounds.Bottom, bounds.Right, bounds.Bottom);
329 gr.DrawLine(p3, bounds.Right - 1, bounds.Y, bounds.Right - 1, bounds.Bottom - 1);
330 gr.DrawLine(p1, bounds.Left, bounds.Y + 1, bounds.Left, bounds.Bottom - 2);
331 gr.DrawLine(p2, bounds.Right - 2, bounds.Y + 1, bounds.Right - 2, bounds.Bottom - 2);
332 gr.DrawLine(p2, bounds.X, bounds.Bottom - 1, bounds.Right - 2, bounds.Bottom - 1);
341 public event EventHandler HeaderChanged;
342 private void OnHeaderChanged()
344 if (HeaderChanged != null)
345 HeaderChanged(this, EventArgs.Empty);
348 public event EventHandler SortOrderChanged;
349 private void OnSortOrderChanged()
351 if (SortOrderChanged != null)
352 SortOrderChanged(this, EventArgs.Empty);
355 public event EventHandler IsVisibleChanged;
356 private void OnIsVisibleChanged()
358 if (IsVisibleChanged != null)
359 IsVisibleChanged(this, EventArgs.Empty);
362 public event EventHandler WidthChanged;
363 private void OnWidthChanged()
365 if (WidthChanged != null)
366 WidthChanged(this, EventArgs.Empty);