Added the source code of Aga.Controls (TreeViewAdv for .Net) version 1.7.0.0.
2 using System.Collections.Generic;
5 using Aga.Controls.Properties;
6 using System.Reflection;
7 using System.Windows.Forms;
8 using System.Windows.Forms.VisualStyles;
9 using System.ComponentModel;
11 namespace Aga.Controls.Tree.NodeControls
13 public class NodeCheckBox : InteractiveControl
15 public const int ImageSize = 13;
17 private Bitmap _check;
18 private Bitmap _uncheck;
19 private Bitmap _unknown;
23 private bool _threeState;
25 public bool ThreeState
27 get { return _threeState; }
28 set { _threeState = value; }
38 public NodeCheckBox(string propertyName)
40 _check = Resources.check;
41 _uncheck = Resources.uncheck;
42 _unknown = Resources.unknown;
43 DataPropertyName = propertyName;
47 public override Size MeasureSize(TreeNodeAdv node, DrawContext context)
49 return new Size(ImageSize, ImageSize);
52 public override void Draw(TreeNodeAdv node, DrawContext context)
54 Rectangle bounds = GetBounds(node, context);
55 CheckState state = GetCheckState(node);
56 if (Application.RenderWithVisualStyles)
58 VisualStyleRenderer renderer;
59 if (state == CheckState.Indeterminate)
60 renderer = new VisualStyleRenderer(VisualStyleElement.Button.CheckBox.MixedNormal);
61 else if (state == CheckState.Checked)
62 renderer = new VisualStyleRenderer(VisualStyleElement.Button.CheckBox.CheckedNormal);
64 renderer = new VisualStyleRenderer(VisualStyleElement.Button.CheckBox.UncheckedNormal);
65 renderer.DrawBackground(context.Graphics, new Rectangle(bounds.X, bounds.Y, ImageSize, ImageSize));
70 if (state == CheckState.Indeterminate)
72 else if (state == CheckState.Checked)
76 context.Graphics.DrawImage(img, bounds.Location);
80 protected virtual CheckState GetCheckState(TreeNodeAdv node)
82 object obj = GetValue(node);
83 if (obj is CheckState)
84 return (CheckState)obj;
86 return (bool)obj ? CheckState.Checked : CheckState.Unchecked;
88 return CheckState.Unchecked;
91 protected virtual void SetCheckState(TreeNodeAdv node, CheckState value)
95 SetValue(node, value);
96 OnCheckStateChanged(node);
100 Type type = GetPropertyType(node);
101 if (type == typeof(CheckState))
103 SetValue(node, value);
104 OnCheckStateChanged(node);
106 else if (type == typeof(bool))
108 SetValue(node, value != CheckState.Unchecked);
109 OnCheckStateChanged(node);
114 public override void MouseDown(TreeNodeAdvMouseEventArgs args)
116 if (args.Button == MouseButtons.Left && IsEditEnabled(args.Node))
118 DrawContext context = new DrawContext();
119 context.Bounds = args.ControlBounds;
120 Rectangle rect = GetBounds(args.Node, context);
121 if (rect.Contains(args.ViewLocation))
123 CheckState state = GetCheckState(args.Node);
124 state = GetNewState(state);
125 SetCheckState(args.Node, state);
132 public override void MouseDoubleClick(TreeNodeAdvMouseEventArgs args)
137 private CheckState GetNewState(CheckState state)
139 if (state == CheckState.Indeterminate)
140 return CheckState.Unchecked;
141 else if(state == CheckState.Unchecked)
142 return CheckState.Checked;
144 return ThreeState ? CheckState.Indeterminate : CheckState.Unchecked;
147 public override void KeyDown(KeyEventArgs args)
149 if (args.KeyCode == Keys.Space && EditEnabled)
151 Parent.BeginUpdate();
154 if (Parent.CurrentNode != null)
156 CheckState value = GetNewState(GetCheckState(Parent.CurrentNode));
157 foreach (TreeNodeAdv node in Parent.Selection)
158 if (IsEditEnabled(node))
159 SetCheckState(node, value);
170 public event EventHandler<TreePathEventArgs> CheckStateChanged;
171 protected void OnCheckStateChanged(TreePathEventArgs args)
173 if (CheckStateChanged != null)
174 CheckStateChanged(this, args);
177 protected void OnCheckStateChanged(TreeNodeAdv node)
179 TreePath path = this.Parent.GetPath(node);
180 OnCheckStateChanged(new TreePathEventArgs(path));