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) 2004-2005 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Reflection;
35 namespace System.Windows.Forms
38 public class CheckedListBox : ListBox
40 private CheckedIndexCollection checked_indices;
41 private CheckedItemCollection checked_items;
42 private bool check_onclick;
43 private bool three_dcheckboxes;
45 public CheckedListBox ()
47 items = new CheckedListBox.ObjectCollection (this);
48 checked_indices = new CheckedIndexCollection (this);
49 checked_items = new CheckedItemCollection (this);
50 check_onclick = false;
51 three_dcheckboxes = false;
52 listbox_info.item_height = FontHeight + 2;
57 [EditorBrowsable (EditorBrowsableState.Never)]
58 public new event EventHandler Click;
61 [EditorBrowsable (EditorBrowsableState.Never)]
62 public new event EventHandler DataSourceChanged;
65 [EditorBrowsable (EditorBrowsableState.Never)]
66 public new event EventHandler DisplayMemberChanged;
69 [EditorBrowsable (EditorBrowsableState.Never)]
70 public new event DrawItemEventHandler DrawItem;
71 public event ItemCheckEventHandler ItemCheck;
74 [EditorBrowsable (EditorBrowsableState.Never)]
75 public new event MeasureItemEventHandler MeasureItem;
78 [EditorBrowsable (EditorBrowsableState.Never)]
79 public new event EventHandler ValueMemberChanged;
82 #region Public Properties
85 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
\r
86 public CheckedListBox.CheckedIndexCollection CheckedIndices {
87 get {return checked_indices; }
91 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
\r
92 public CheckedListBox.CheckedItemCollection CheckedItems {
93 get {return checked_items; }
96 [DefaultValue (false)]
97 public bool CheckOnClick {
98 get { return check_onclick; }
99 set { check_onclick = value; }
102 protected override CreateParams CreateParams {
103 get { return base.CreateParams;}
106 [EditorBrowsable (EditorBrowsableState.Never)]
107 [Browsable (false)]
\r
108 public new object DataSource {
109 get { return base.DataSource; }
110 set { DataSource = value; }
113 [EditorBrowsable (EditorBrowsableState.Never)]
115 public new string DisplayMember {
116 get { throw new NotImplementedException (); }
117 set { throw new NotImplementedException (); }
121 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
122 [EditorBrowsable (EditorBrowsableState.Never)]
123 public override DrawMode DrawMode {
124 get { return DrawMode.Normal; }
125 set { /* Not possible */ }
129 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
130 [EditorBrowsable (EditorBrowsableState.Never)]
131 public override int ItemHeight {
132 get { return listbox_info.item_height; }
133 set { /* Not possible */ }
136 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
138 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
139 public new CheckedListBox.ObjectCollection Items {
140 get { return (CheckedListBox.ObjectCollection) base.Items; }
143 public override SelectionMode SelectionMode {
144 get { return base.SelectionMode; }
146 if (value == SelectionMode.MultiSimple || value == SelectionMode.MultiExtended)
147 throw new InvalidEnumArgumentException ("Multi selection modes not supported");
149 base.SelectionMode = value;
153 [DefaultValue (false)]
154 public bool ThreeDCheckBoxes {
155 get { return three_dcheckboxes; }
157 if (three_dcheckboxes == value)
160 three_dcheckboxes = value;
166 [EditorBrowsable (EditorBrowsableState.Never)]
167 public new string ValueMember {
168 get { return base.ValueMember; }
169 set { base.ValueMember = value; }
172 #endregion Public Properties
174 #region Public Methods
176 protected override AccessibleObject CreateAccessibilityInstance ()
178 throw new NotImplementedException ();
181 protected override ListBox.ObjectCollection CreateItemCollection ()
183 return new ObjectCollection (this);
186 public bool GetItemChecked (int index)
188 return (GetItemCheckState (index) == CheckState.Checked);
191 public CheckState GetItemCheckState (int index)
193 if (index < 0 || index >= Items.Count)
194 throw new ArgumentOutOfRangeException ("Index of out range");
196 return (Items.GetListBoxItem (index)).State;
199 protected override void OnBackColorChanged (EventArgs e)
201 base.OnBackColorChanged (e);
204 protected override void OnClick (EventArgs e)
209 if (focused_item != -1) {
210 SetItemChecked (focused_item, !GetItemChecked (focused_item));
215 Click (this, EventArgs.Empty);
218 protected override void OnDrawItem (DrawItemEventArgs e)
220 ThemeEngine.Current.DrawCheckedListBoxItem (this, e);
223 protected override void OnFontChanged (EventArgs e)
225 base.OnFontChanged (e);
228 protected override void OnHandleCreated (EventArgs e)
230 base.OnHandleCreated (e);
233 protected virtual void OnItemCheck (ItemCheckEventArgs ice)
235 if (ItemCheck != null)
236 ItemCheck (this, ice);
239 protected override void OnKeyPress (KeyPressEventArgs e)
243 if (e.KeyChar == ' ') { // Space should check focused item
244 if (focused_item != -1) {
245 SetItemChecked (focused_item, !GetItemChecked (focused_item));
250 protected override void OnMeasureItem (MeasureItemEventArgs e)
252 if (MeasureItem != null)
253 MeasureItem (this, e);
256 protected override void OnSelectedIndexChanged (EventArgs e)
258 base.OnSelectedIndexChanged (e);
261 public void SetItemChecked (int index, bool value)
263 SetItemCheckState (index, value ? CheckState.Checked : CheckState.Unchecked);
266 public void SetItemCheckState (int index, CheckState value)
268 if (index < 0 || index >= Items.Count)
269 throw new ArgumentOutOfRangeException ("Index of out range");
271 if (!Enum.IsDefined (typeof (CheckState), value))
272 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for CheckState", value));
274 CheckState old_value = (Items.GetListBoxItem (index)).State;
276 if (old_value == value)
279 (Items.GetListBoxItem (index)).State = value;
281 Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
284 case CheckState.Checked:
285 checked_indices.AddIndex (index);
286 checked_items.AddObject (Items[index]);
288 case CheckState.Unchecked:
289 checked_indices.RemoveIndex (index);
290 checked_items.RemoveObject (Items[index]);
292 case CheckState.Indeterminate:
297 OnItemCheck (new ItemCheckEventArgs (index, value, old_value));
299 if (ClientRectangle.Contains (invalidate))
300 Invalidate (invalidate);
303 protected override void WmReflectCommand (ref Message m)
305 base.WmReflectCommand (ref m);
308 protected override void WndProc (ref Message m)
310 base.WndProc (ref m);
313 #endregion Public Methods
315 #region Private Methods
317 internal override void OnMouseDownLB (object sender, MouseEventArgs e)
319 Rectangle hit_rect, item_rect;
320 CheckState value = CheckState.Checked;
321 bool set_value = false;
322 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
328 hit_rect = item_rect = GetItemDisplayRectangle (index, LBoxInfo.top_item); // Full item rect
329 hit_rect.X += ThemeEngine.Current.CheckedListBoxCheckRectangle().X;
330 hit_rect.Y += ThemeEngine.Current.CheckedListBoxCheckRectangle().Y;
331 hit_rect.Width = ThemeEngine.Current.CheckedListBoxCheckRectangle().Width;
332 hit_rect.Height = ThemeEngine.Current.CheckedListBoxCheckRectangle().Height;
334 if ((Items.GetListBoxItem (index)).State == CheckState.Checked)
335 value = CheckState.Unchecked;
337 if (hit_rect.Contains (e.X, e.Y) == true) {
341 if (item_rect.Contains (e.X, e.Y) == true) {
342 if (!check_onclick) {
343 if ((Items.GetListBoxItem (index)).Selected == true)
350 SetItemCheckState (index, value);
352 base.OnMouseDownLB (sender, e);
355 internal override void UpdateItemInfo (UpdateOperation operation, int first, int last)
357 base.UpdateItemInfo (operation, first, last);
358 CheckedItems.ReCreate ();
359 CheckedIndices.ReCreate ();
362 #endregion Private Methods
364 public class ObjectCollection : ListBox.ObjectCollection
366 public ObjectCollection (CheckedListBox owner) : base (owner)
371 public int Add (object item, bool isChecked)
374 return Add (item, CheckState.Checked);
376 return Add (item, CheckState.Unchecked);
380 public int Add (object item, CheckState check)
382 int cnt = object_items.Count;
383 ListBox.ListBoxItem box_item = new ListBox.ListBoxItem (cnt);
384 box_item.State = check;
385 object_items.Add (item);
386 listbox_items.Add (box_item);
392 CheckedListBox.CheckedIndexCollection
394 public class CheckedIndexCollection : IList, ICollection, IEnumerable
396 private CheckedListBox owner;
397 private ArrayList indices = new ArrayList ();
399 internal CheckedIndexCollection (CheckedListBox owner)
404 #region Public Properties
405 public virtual int Count {
406 get { return indices.Count; }
409 public virtual bool IsReadOnly {
413 bool ICollection.IsSynchronized {
414 get { return false; }
417 bool IList.IsFixedSize{
421 object ICollection.SyncRoot {
426 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
427 public int this[int index] {
429 if (index < 0 || index >= Count)
430 throw new ArgumentOutOfRangeException ("Index of out range");
432 return (int) indices[index];
435 #endregion Public Properties
437 public bool Contains (int index)
439 return indices.Contains (index);
443 public virtual void CopyTo (Array dest, int index)
445 indices.CopyTo (dest, index);
448 public virtual IEnumerator GetEnumerator ()
450 return indices.GetEnumerator ();
453 int IList.Add (object value)
455 throw new NotSupportedException ();
460 throw new NotSupportedException ();
463 bool IList.Contains (object index)
465 return Contains ((int)index);
468 int IList.IndexOf (object index)
470 return IndexOf ((int) index);
473 void IList.Insert (int index, object value)
475 throw new NotSupportedException ();
478 void IList.Remove (object value)
480 throw new NotSupportedException ();
483 void IList.RemoveAt (int index)
485 throw new NotSupportedException ();
488 object IList.this[int index]{
489 get {return indices[index]; }
490 set {throw new NotImplementedException (); }
493 public int IndexOf (int index)
495 return indices.IndexOf (index);
498 #region Private Methods
500 internal void AddIndex (int index)
505 internal void ClearIndices ()
510 internal void RemoveIndex (int index)
512 indices.Remove (index);
515 internal void ReCreate ()
519 for (int i = 0; i < owner.Items.Count; i++) {
520 ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
522 if (item.State == CheckState.Checked)
523 indices.Add (item.Index);
527 #endregion Private Methods
531 CheckedItemCollection
533 public class CheckedItemCollection : IList, ICollection, IEnumerable
535 private CheckedListBox owner;
536 private ArrayList object_items = new ArrayList ();
538 internal CheckedItemCollection (CheckedListBox owner)
543 #region Public Properties
544 public virtual int Count {
545 get { return object_items.Count; }
548 public virtual bool IsReadOnly {
553 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
554 public virtual object this [int index] {
556 if (index < 0 || index >= Count)
557 throw new ArgumentOutOfRangeException ("Index of out range");
559 return object_items[index];
561 set {throw new NotSupportedException ();}
564 bool ICollection.IsSynchronized {
568 object ICollection.SyncRoot {
572 bool IList.IsFixedSize {
576 object IList.this[int index] {
577 get { return object_items[index]; }
578 set { throw new NotSupportedException (); }
581 #endregion Public Properties
583 #region Public Methods
584 public virtual bool Contains (object selectedObject)
586 return object_items.Contains (selectedObject);
589 public virtual void CopyTo (Array dest, int index)
591 object_items.CopyTo (dest, index);
594 int IList.Add (object value)
596 throw new NotSupportedException ();
601 throw new NotSupportedException ();
604 bool IList.Contains (object selectedIndex)
606 throw new NotImplementedException ();
609 void IList.Insert (int index, object value)
611 throw new NotSupportedException ();
614 void IList.Remove (object value)
616 throw new NotSupportedException ();
619 void IList.RemoveAt (int index)
621 throw new NotSupportedException ();
624 public int IndexOf (object item)
626 return object_items.IndexOf (item);
629 public virtual IEnumerator GetEnumerator ()
631 return object_items.GetEnumerator ();
634 #endregion Public Methods
636 #region Private Methods
637 internal void AddObject (object obj)
639 object_items.Add (obj);
642 internal void ClearObjects ()
644 object_items.Clear ();
647 internal void ReCreate ()
649 object_items.Clear ();
651 for (int i = 0; i < owner.Items.Count; i++) {
652 ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
654 if (item.State == CheckState.Checked)
655 object_items.Add (owner.Items[item.Index]);
659 internal void RemoveObject (object obj)
661 object_items.Remove (obj);
663 #endregion Private Methods