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 public 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 if (table_style.CurrentGridLineStyle == DataGridLineStyle.Solid) {
239 g.DrawRectangle (ThemeEngine.Current.ResPool.GetPen (table_style.CurrentGridLineColor), bounds);
242 switch (GetState (source, rowNum) & ~CheckState.Selected) {
243 case CheckState.Checked:
244 state = ButtonState.Checked;
246 case CheckState.Null:
247 state = ButtonState.Inactive;
249 case CheckState.UnChecked:
251 state = ButtonState.Normal;
255 ThemeEngine.Current.CPDrawCheckBox (g, rect, state);
258 protected internal override void SetColumnValueAtRow (CurrencyManager lm, int row, object obj)
262 if (obj.Equals (nullvalue)) {
263 value = Convert.DBNull;
265 if (obj.Equals (truevalue)) {
270 base.SetColumnValueAtRow (lm, row, value);
272 #endregion // Public Instance Methods
274 #region Private Instance Methods
275 internal static bool CanRenderType (Type type)
277 return (type == typeof (Boolean));
280 private object FromStateToValue (CheckState state)
282 state = state & ~CheckState.Selected;
284 if ((state & CheckState.Checked) == CheckState.Checked) {
288 if ((state & CheckState.Null) == CheckState.Null) {
295 private CheckState FromValueToState (object obj)
297 if (obj.Equals (truevalue)) {
298 return CheckState.Checked;
301 if (obj.Equals (nullvalue)) {
302 return CheckState.Null;
305 return CheckState.UnChecked;
308 private CheckState GetState (CurrencyManager source, int row)
312 if (checkboxes_state[row] == null) {
313 object value = GetColumnValueAtRow (source, row);
314 state = FromValueToState (value);
315 checkboxes_state.Add (row, state);
317 state = (CheckState) checkboxes_state[row];
323 private CheckState GetNextState (CheckState state)
325 CheckState new_state;
326 bool selected = ((state & CheckState.Selected) == CheckState.Selected);
328 switch (state & ~CheckState.Selected) {
329 case CheckState.Checked:
330 new_state = CheckState.Null;
332 case CheckState.Null:
333 new_state = CheckState.UnChecked;
335 case CheckState.UnChecked:
337 new_state = CheckState.Checked;
342 new_state = new_state | CheckState.Selected;
348 internal override void OnKeyDown (KeyEventArgs ke, int row, int column)
350 CheckState state = GetNextState (GetState (null, row));
352 if (ke.KeyCode == Keys.Space) {
353 grid.is_changing = true;
354 grid.InvalidateCurrentRowHeader ();
355 checkboxes_state[row] = state;
356 grid.Invalidate (grid.GetCellBounds (row, column));
360 internal override void OnMouseDown (MouseEventArgs e, int row, int column)
362 CheckState state = GetNextState (GetState (null, row));
364 grid.is_changing = true;
365 grid.InvalidateCurrentRowHeader ();
366 SetState (row, state);
367 grid.Invalidate (grid.GetCellBounds (row, column));
370 private void SetState (int row, CheckState state)
372 if (checkboxes_state[row] == null) {
373 checkboxes_state.Add (row, state);
375 checkboxes_state[row] = state;
379 #endregion Private Instance Methods
382 public event EventHandler AllowNullChanged;
383 public event EventHandler FalseValueChanged;
384 public event EventHandler TrueValueChanged;