1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/External/Aga.Controls/Tree/NodeControls/NodeCheckBox.cs Sun May 27 15:16:19 2012 +0000
1.3 @@ -0,0 +1,184 @@
1.4 +using System;
1.5 +using System.Collections.Generic;
1.6 +using System.Text;
1.7 +using System.Drawing;
1.8 +using Aga.Controls.Properties;
1.9 +using System.Reflection;
1.10 +using System.Windows.Forms;
1.11 +using System.Windows.Forms.VisualStyles;
1.12 +using System.ComponentModel;
1.13 +
1.14 +namespace Aga.Controls.Tree.NodeControls
1.15 +{
1.16 + public class NodeCheckBox : InteractiveControl
1.17 + {
1.18 + public const int ImageSize = 13;
1.19 +
1.20 + private Bitmap _check;
1.21 + private Bitmap _uncheck;
1.22 + private Bitmap _unknown;
1.23 +
1.24 + #region Properties
1.25 +
1.26 + private bool _threeState;
1.27 + [DefaultValue(false)]
1.28 + public bool ThreeState
1.29 + {
1.30 + get { return _threeState; }
1.31 + set { _threeState = value; }
1.32 + }
1.33 +
1.34 + #endregion
1.35 +
1.36 + public NodeCheckBox()
1.37 + : this(string.Empty)
1.38 + {
1.39 + }
1.40 +
1.41 + public NodeCheckBox(string propertyName)
1.42 + {
1.43 + _check = Resources.check;
1.44 + _uncheck = Resources.uncheck;
1.45 + _unknown = Resources.unknown;
1.46 + DataPropertyName = propertyName;
1.47 + LeftMargin = 0;
1.48 + }
1.49 +
1.50 + public override Size MeasureSize(TreeNodeAdv node, DrawContext context)
1.51 + {
1.52 + return new Size(ImageSize, ImageSize);
1.53 + }
1.54 +
1.55 + public override void Draw(TreeNodeAdv node, DrawContext context)
1.56 + {
1.57 + Rectangle bounds = GetBounds(node, context);
1.58 + CheckState state = GetCheckState(node);
1.59 + if (Application.RenderWithVisualStyles)
1.60 + {
1.61 + VisualStyleRenderer renderer;
1.62 + if (state == CheckState.Indeterminate)
1.63 + renderer = new VisualStyleRenderer(VisualStyleElement.Button.CheckBox.MixedNormal);
1.64 + else if (state == CheckState.Checked)
1.65 + renderer = new VisualStyleRenderer(VisualStyleElement.Button.CheckBox.CheckedNormal);
1.66 + else
1.67 + renderer = new VisualStyleRenderer(VisualStyleElement.Button.CheckBox.UncheckedNormal);
1.68 + renderer.DrawBackground(context.Graphics, new Rectangle(bounds.X, bounds.Y, ImageSize, ImageSize));
1.69 + }
1.70 + else
1.71 + {
1.72 + Image img;
1.73 + if (state == CheckState.Indeterminate)
1.74 + img = _unknown;
1.75 + else if (state == CheckState.Checked)
1.76 + img = _check;
1.77 + else
1.78 + img = _uncheck;
1.79 + context.Graphics.DrawImage(img, bounds.Location);
1.80 + }
1.81 + }
1.82 +
1.83 + protected virtual CheckState GetCheckState(TreeNodeAdv node)
1.84 + {
1.85 + object obj = GetValue(node);
1.86 + if (obj is CheckState)
1.87 + return (CheckState)obj;
1.88 + else if (obj is bool)
1.89 + return (bool)obj ? CheckState.Checked : CheckState.Unchecked;
1.90 + else
1.91 + return CheckState.Unchecked;
1.92 + }
1.93 +
1.94 + protected virtual void SetCheckState(TreeNodeAdv node, CheckState value)
1.95 + {
1.96 + if (VirtualMode)
1.97 + {
1.98 + SetValue(node, value);
1.99 + OnCheckStateChanged(node);
1.100 + }
1.101 + else
1.102 + {
1.103 + Type type = GetPropertyType(node);
1.104 + if (type == typeof(CheckState))
1.105 + {
1.106 + SetValue(node, value);
1.107 + OnCheckStateChanged(node);
1.108 + }
1.109 + else if (type == typeof(bool))
1.110 + {
1.111 + SetValue(node, value != CheckState.Unchecked);
1.112 + OnCheckStateChanged(node);
1.113 + }
1.114 + }
1.115 + }
1.116 +
1.117 + public override void MouseDown(TreeNodeAdvMouseEventArgs args)
1.118 + {
1.119 + if (args.Button == MouseButtons.Left && IsEditEnabled(args.Node))
1.120 + {
1.121 + DrawContext context = new DrawContext();
1.122 + context.Bounds = args.ControlBounds;
1.123 + Rectangle rect = GetBounds(args.Node, context);
1.124 + if (rect.Contains(args.ViewLocation))
1.125 + {
1.126 + CheckState state = GetCheckState(args.Node);
1.127 + state = GetNewState(state);
1.128 + SetCheckState(args.Node, state);
1.129 + Parent.UpdateView();
1.130 + args.Handled = true;
1.131 + }
1.132 + }
1.133 + }
1.134 +
1.135 + public override void MouseDoubleClick(TreeNodeAdvMouseEventArgs args)
1.136 + {
1.137 + args.Handled = true;
1.138 + }
1.139 +
1.140 + private CheckState GetNewState(CheckState state)
1.141 + {
1.142 + if (state == CheckState.Indeterminate)
1.143 + return CheckState.Unchecked;
1.144 + else if(state == CheckState.Unchecked)
1.145 + return CheckState.Checked;
1.146 + else
1.147 + return ThreeState ? CheckState.Indeterminate : CheckState.Unchecked;
1.148 + }
1.149 +
1.150 + public override void KeyDown(KeyEventArgs args)
1.151 + {
1.152 + if (args.KeyCode == Keys.Space && EditEnabled)
1.153 + {
1.154 + Parent.BeginUpdate();
1.155 + try
1.156 + {
1.157 + if (Parent.CurrentNode != null)
1.158 + {
1.159 + CheckState value = GetNewState(GetCheckState(Parent.CurrentNode));
1.160 + foreach (TreeNodeAdv node in Parent.Selection)
1.161 + if (IsEditEnabled(node))
1.162 + SetCheckState(node, value);
1.163 + }
1.164 + }
1.165 + finally
1.166 + {
1.167 + Parent.EndUpdate();
1.168 + }
1.169 + args.Handled = true;
1.170 + }
1.171 + }
1.172 +
1.173 + public event EventHandler<TreePathEventArgs> CheckStateChanged;
1.174 + protected void OnCheckStateChanged(TreePathEventArgs args)
1.175 + {
1.176 + if (CheckStateChanged != null)
1.177 + CheckStateChanged(this, args);
1.178 + }
1.179 +
1.180 + protected void OnCheckStateChanged(TreeNodeAdv node)
1.181 + {
1.182 + TreePath path = this.Parent.GetPath(node);
1.183 + OnCheckStateChanged(new TreePathEventArgs(path));
1.184 + }
1.185 +
1.186 + }
1.187 +}