1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Jordi Mas i Hernandez <jordi@ximian.com>
29 using System.ComponentModel;
31 using System.Runtime.InteropServices;
32 using System.Diagnostics;
33 using System.Collections;
35 namespace System.Windows.Forms
37 public class DataGridBoolColumn : DataGridColumnStyle
40 private enum CheckState {
42 UnChecked = 0x00000002,
47 #region Local Variables
48 private bool allownull;
49 private object falsevalue;
50 private object nullvalue;
51 private object truevalue;
52 private Hashtable checkboxes_state;
53 #endregion // Local Variables
56 public DataGridBoolColumn () : base ()
61 public DataGridBoolColumn (PropertyDescriptor prop) : base (prop)
66 public DataGridBoolColumn (PropertyDescriptor prop, bool isDefault) : base (prop)
69 is_default = isDefault;
72 private void CommonConstructor ()
78 checkboxes_state = new Hashtable ();
83 #region Public Instance Properties
85 public bool AllowNull {
90 if (value != allownull) {
93 if (AllowNullChanged != null) {
94 AllowNullChanged (this, EventArgs.Empty);
100 [TypeConverter(typeof(System.ComponentModel.StringConverter))]
101 public object FalseValue {
106 if (value != falsevalue) {
109 if (FalseValueChanged != null) {
110 FalseValueChanged (this, EventArgs.Empty);
116 [TypeConverter(typeof(System.ComponentModel.StringConverter))]
117 public object NullValue {
122 if (value != nullvalue) {
128 [TypeConverter(typeof(System.ComponentModel.StringConverter))]
129 public object TrueValue {
134 if (value != truevalue) {
137 if (TrueValueChanged != null) {
138 TrueValueChanged (this, EventArgs.Empty);
143 #endregion // Public Instance Properties
145 #region Public Instance Methods
146 protected internal override void Abort (int rowNum)
148 SetState (rowNum, GetState (null, rowNum) & ~CheckState.Selected);
149 grid.Invalidate (grid.GetCurrentCellBounds ());
152 protected internal override bool Commit (CurrencyManager source, int rowNum)
154 SetColumnValueAtRow (source, rowNum, FromStateToValue (GetState (source, rowNum)));
155 SetState (rowNum, GetState (source, rowNum) & ~CheckState.Selected);
156 grid.Invalidate (grid.GetCurrentCellBounds ());
161 protected internal override void ConcedeFocus ()
166 protected internal override void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
168 SetState (rowNum, GetState (source, rowNum) | CheckState.Selected);
169 grid.Invalidate (grid.GetCurrentCellBounds ());
173 protected internal override void EnterNullValue ()
178 protected internal override object GetColumnValueAtRow (CurrencyManager lm, int row)
180 object obj = base.GetColumnValueAtRow (lm, row);
182 if (obj.Equals (nullvalue)) {
183 return Convert.DBNull;
186 if (obj.Equals (truevalue)) {
193 protected internal override int GetMinimumHeight ()
195 return ThemeEngine.Current.DataGridMinimumColumnCheckBoxHeight;
198 protected internal override int GetPreferredHeight (Graphics g, object value)
200 return ThemeEngine.Current.DataGridMinimumColumnCheckBoxHeight;
203 protected internal override Size GetPreferredSize (Graphics g, object value)
205 return new Size (ThemeEngine.Current.DataGridMinimumColumnCheckBoxWidth, ThemeEngine.Current.DataGridMinimumColumnCheckBoxHeight);
208 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum)
210 Paint (g, bounds, source, rowNum, false);
213 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight)
215 Paint (g, bounds, source, rowNum, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.BackColor),
216 ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.ForeColor), alignToRight);
219 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
221 Size chkbox_size = GetPreferredSize (g, null);
222 Rectangle rect = new Rectangle ();
224 chkbox_size.Width -= 2;
225 chkbox_size.Height -= 2;
226 rect.X = bounds.X + ((bounds.Width - chkbox_size.Width) / 2);
227 rect.Y = bounds.Y + ((bounds.Height - chkbox_size.Height) / 2);
228 rect.Width = chkbox_size.Width;
229 rect.Height = chkbox_size.Height;
231 // If the cell is selected
232 if ((GetState (source, rowNum) & CheckState.Selected) == CheckState.Selected) {
233 backBrush = ThemeEngine.Current.ResPool.GetSolidBrush (grid.SelectionBackColor);
236 g.FillRectangle (backBrush, bounds);
238 switch (GetState (source, rowNum) & ~CheckState.Selected) {
239 case CheckState.Checked:
240 state = ButtonState.Checked;
242 case CheckState.Null:
243 state = ButtonState.Inactive;
245 case CheckState.UnChecked:
247 state = ButtonState.Normal;
251 ThemeEngine.Current.CPDrawCheckBox (g, rect, state);
252 PaintGridLine (g, bounds);
255 protected internal override void SetColumnValueAtRow (CurrencyManager lm, int row, object obj)
259 if (obj.Equals (nullvalue)) {
260 value = Convert.DBNull;
262 if (obj.Equals (truevalue)) {
267 base.SetColumnValueAtRow (lm, row, value);
269 #endregion // Public Instance Methods
271 #region Private Instance Methods
272 internal static bool CanRenderType (Type type)
274 return (type == typeof (Boolean));
277 private object FromStateToValue (CheckState state)
279 state = state & ~CheckState.Selected;
281 if ((state & CheckState.Checked) == CheckState.Checked) {
285 if ((state & CheckState.Null) == CheckState.Null) {
292 private CheckState FromValueToState (object obj)
294 if (obj.Equals (truevalue)) {
295 return CheckState.Checked;
298 if (obj.Equals (nullvalue)) {
299 return CheckState.Null;
302 return CheckState.UnChecked;
305 private CheckState GetState (CurrencyManager source, int row)
309 if (checkboxes_state[row] == null) {
310 object value = GetColumnValueAtRow (source, row);
311 state = FromValueToState (value);
312 checkboxes_state.Add (row, state);
314 state = (CheckState) checkboxes_state[row];
320 private CheckState GetNextState (CheckState state)
322 CheckState new_state;
323 bool selected = ((state & CheckState.Selected) == CheckState.Selected);
325 switch (state & ~CheckState.Selected) {
326 case CheckState.Checked:
327 new_state = CheckState.Null;
329 case CheckState.Null:
330 new_state = CheckState.UnChecked;
332 case CheckState.UnChecked:
334 new_state = CheckState.Checked;
339 new_state = new_state | CheckState.Selected;
345 internal override void OnKeyDown (KeyEventArgs ke, int row, int column)
347 CheckState state = GetNextState (GetState (null, row));
349 if (ke.KeyCode == Keys.Space) {
350 grid.is_changing = true;
351 grid.InvalidateCurrentRowHeader ();
352 checkboxes_state[row] = state;
353 grid.Invalidate (grid.GetCellBounds (row, column));
357 internal override void OnMouseDown (MouseEventArgs e, int row, int column)
359 CheckState state = GetNextState (GetState (null, row));
361 grid.is_changing = true;
362 grid.InvalidateCurrentRowHeader ();
363 SetState (row, state);
364 grid.Invalidate (grid.GetCellBounds (row, column));
367 private void SetState (int row, CheckState state)
369 if (checkboxes_state[row] == null) {
370 checkboxes_state.Add (row, state);
372 checkboxes_state[row] = state;
376 #endregion Private Instance Methods
379 public event EventHandler AllowNullChanged;
380 public event EventHandler FalseValueChanged;
381 public event EventHandler TrueValueChanged;