2 // System.Windows.Forms.CheckedListBox.cs
5 // stubbed out by Jaak Simm (jaaksimm@firm.ee)
6 // Denis hayes (dennish@raytek.com)
7 // Alexandre Pigolkine (pigolkine@gmx.de)
9 // (C) Ximian, Inc., 2002/3
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.Collections
;
36 namespace System
.Windows
.Forms
{
39 /// Displays a ListBox in which a check box is displayed to the left of each item.
43 public class CheckedListBox
: ListBox
{
46 private bool checkOnClick
;
47 private bool threeDCheckBoxes
;
48 private CheckedListBox
.CheckedIndexCollection CheckedIndices_
= null;
49 private CheckedListBox
.CheckedItemCollection CheckedItems_
= null;
51 // --- Constructor ---
52 public CheckedListBox() : base()
55 threeDCheckBoxes
= true;
56 DrawMode_
= DrawMode
.Normal
;
59 internal override void OnObjectCollectionChanged() {
60 CheckedIndices_
= null;
62 base.OnObjectCollectionChanged();
65 // --- CheckedListBox Properties ---
67 public CheckedListBox
.CheckedIndexCollection CheckedIndices
{
69 if( CheckedIndices_
== null) {
70 CheckedIndices_
= new CheckedListBox
.CheckedIndexCollection(this);
72 return CheckedIndices_
;
77 public CheckedListBox
.CheckedItemCollection CheckedItems
{
79 if( CheckedItems_
== null) {
80 CheckedItems_
= new CheckedListBox
.CheckedItemCollection(this);
86 public bool CheckOnClick
{
96 protected override CreateParams CreateParams
{
98 CreateParams createParams
= base.CreateParams
;
99 // set ownerDraw flag to be able to paint check-boxes
100 createParams
.Style
|= (int)ListBoxStyles
.LBS_OWNERDRAWFIXED
;
106 public override DrawMode DrawMode
{
108 return DrawMode
.Normal
;
111 // always DrawMode.Normal
116 public override int ItemHeight
{
119 return base.ItemHeight
;
123 base.ItemHeight
= value;
128 public new CheckedListBox
.ObjectCollection Items
{
130 return (CheckedListBox
.ObjectCollection
)base.Items
;
135 public new object DataSource
{ // .NET V1.1 Beta. needs implmented
136 get { return base.DataSource; }
137 set { base.DataSource = value; }
141 public new string DisplayMember
{ // .NET V1.1 Beta. needs implmented
142 get { return base.DisplayMember; }
143 set { base.DisplayMember = value; }
147 public new string ValueMember
{ // .NET V1.1 Beta. needs implmented
148 get { return base.DisplayMember; }
149 set { base.DisplayMember = value; }
152 public override SelectionMode SelectionMode
{
154 return base.SelectionMode
;
157 if (value!=SelectionMode
.One
&& value!=SelectionMode
.None
)
158 throw new ArgumentException();
159 base.SelectionMode
=value;
163 public bool ThreeDCheckBoxes
{
164 get { return threeDCheckBoxes; }
166 if( threeDCheckBoxes
!= value) {
167 threeDCheckBoxes
= value;
173 // --- CheckedListBox methods ---
174 // following methods only support .NET framework:
175 protected virtual void OnItemCheck(ItemCheckEventArgs ice
){
176 throw new NotImplementedException ();
179 protected override void WmReflectCommand(ref Message m
){
180 throw new NotImplementedException ();
183 protected override AccessibleObject
CreateAccessibilityInstance()
185 throw new NotImplementedException ();
189 protected override ListBox
.ObjectCollection
CreateItemCollection() {
190 return (ListBox
.ObjectCollection
)(new CheckedListBox
.ObjectCollection( this));
194 public bool GetItemChecked(int index
)
196 return CheckedIndices
.Contains(index
);
200 public CheckState
GetItemCheckState(int index
)
202 return CheckedIndices
.Contains(index
) ? CheckState
.Checked
: CheckState
.Unchecked
;
207 protected override void OnBackColorChanged(EventArgs e
)
210 base.OnBackColorChanged(e
);
214 protected override void OnClick(EventArgs e
)
221 protected override void OnDrawItem(DrawItemEventArgs e
)
223 Rectangle paintBounds
= new Rectangle (0, 0, e
.Bounds
.Width
, e
.Bounds
.Height
);
224 Bitmap bmp
= new Bitmap( paintBounds
.Width
, paintBounds
.Height
,e
.Graphics
);
225 Graphics paintOn
= Graphics
.FromImage(bmp
);
227 paintOn
.FillRectangle(SystemBrushes
.Window
, paintBounds
);
229 Rectangle checkRect
= new Rectangle( 0, 0, paintBounds
.Height
, paintBounds
.Height
);
230 checkRect
.Inflate(-1,-1);
231 Rectangle textRect
= new Rectangle( checkRect
.Right
, 0, paintBounds
.Width
- checkRect
.Width
- 1, paintBounds
.Height
);
233 if( (e
.State
& DrawItemState
.Selected
) != 0) {
234 paintOn
.FillRectangle(SystemBrushes
.Highlight
, textRect
);
235 paintOn
.DrawString(Items_
[e
.Index
].ToString(), Font
, SystemBrushes
.HighlightText
, textRect
.X
, textRect
.Y
);
238 paintOn
.DrawString(Items_
[e
.Index
].ToString(), Font
, SystemBrushes
.ControlText
, textRect
.X
, textRect
.Y
);
241 ButtonState state
= ButtonState
.Normal
;
242 if( !threeDCheckBoxes
) {
243 state
|= ButtonState
.Flat
;
246 if( CheckedIndices
.Contains(e
.Index
)) {
247 state
|= ButtonState
.Checked
;
250 ControlPaint
.DrawCheckBox (paintOn
, checkRect
, state
);
252 if( 0 != (DrawItemState
.Focus
& e
.State
)) {
253 ControlPaint
.DrawFocusRectangle (paintOn
, textRect
);
255 e
.Graphics
.DrawImage(bmp
, e
.Bounds
.Left
, e
.Bounds
.Top
, e
.Bounds
.Width
, e
.Bounds
.Height
);
261 protected override void OnFontChanged(EventArgs e
)
264 base.OnFontChanged(e
);
268 protected override void OnHandleCreated(EventArgs e
)
271 base.OnHandleCreated(e
);
274 // only supports .NET framework, thus is not stubbed out
277 protected virtual void OnItemCheck(ItemCheckEventArgs ice)
279 throw new NotImplementedException ();
284 protected override void OnKeyPress(KeyPressEventArgs e
)
291 protected override void OnMeasureItem(MeasureItemEventArgs e
)
294 base.OnMeasureItem(e
);
298 protected override void OnSelectedIndexChanged(EventArgs e
)
301 base.OnSelectedIndexChanged(e
);
303 // end of [event methods]
306 public void SetItemChecked(int index
,bool value)
308 SetItemCheckState(index
, value ? CheckState
.Checked
: CheckState
.Unchecked
);
312 public void SetItemCheckState(int index
, CheckState
value)
314 if( index
< 0 || index
> Items
.Count
) {
315 // FIXME: Set exception properties
316 throw new ArgumentException();
319 //bool invalidateControl = false;
320 ListBox
.ObjectCollection
.ListBoxItem item
= Items_
.getItemAt(index
);
321 item
.Checked_
= value == CheckState
.Checked
? true : false;
322 CheckedIndices_
= null;
323 CheckedItems_
= null;
324 // FIXME: Minimize repainting here, invalidate only part on the control ?
328 internal void listboxSelChange()
330 int curSel
= Win32
.SendMessage(Handle
, (int)ListBoxMessages
.LB_GETCURSEL
, 0, 0);
331 //Console.WriteLine("ListBoxNotifications.LBN_SELCHANGE. {0} item is active", curSel);
332 // CHECKME: the things work nice w/out call to control, but may be this will be needed.
333 //CallControlWndProc(ref m);
334 if(checkOnClick
|| prevSelectedIndex
== curSel
) {
335 SelectedIndex
= curSel
;
336 SetItemChecked(SelectedIndex
, !CheckedIndices
.Contains(SelectedIndex
));
338 prevSelectedIndex
= curSel
;
342 protected override void WndProc(ref Message m
)
346 switch ((Msg
) m
.Msg
) {
348 switch(m
.HiWordWParam
) {
349 case (uint)ListBoxNotifications
.LBN_SELCHANGE
:
351 m
.Result
= IntPtr
.Zero
;
353 case (uint)ListBoxNotifications
.LBN_DBLCLK
:
355 m
.Result
= IntPtr
.Zero
;
371 /// --- CheckedListBox events ---
372 /// following events are not stubbed out, because they only support .NET framework:
373 /// - public new event EventHandler Click;
374 /// - public new event DrawItemEventHandler DrawItem;
375 /// - public new event MeasureItemEventHandler MeasureItem;
376 public event ItemCheckEventHandler ItemCheck
;
378 /// sub-class: CheckedListBox.CheckedIndexCollection
380 /// Encapsulates the collection of indexes of checked items (including items in an indeterminate state) in a CheckedListBox.
383 public class CheckedIndexCollection
: IList
, ICollection
, IEnumerable
{
384 CheckedListBox owner_
;
385 ArrayList collection_
;
387 internal CheckedIndexCollection(CheckedListBox owner
)
390 collection_
= owner_
.Items
.CreateCheckedIndexList();
393 /// --- CheckedIndexCollection Properties ---
396 get { return collection_.Count; }
400 public bool IsReadOnly
{
405 public int this[int index
] {
406 get { return (int)collection_[index]; }
409 /// --- ICollection properties ---
410 bool IList
.IsFixedSize
{
411 [MonoTODO
] get { throw new NotImplementedException(); }
414 object IList
.this[int index
] {
416 [MonoTODO
] get { throw new NotImplementedException(); }
420 object ICollection
.SyncRoot
{
422 [MonoTODO
] get { throw new NotImplementedException(); }
425 bool ICollection
.IsSynchronized
{
427 [MonoTODO
] get { throw new NotImplementedException(); }
431 /// --- CheckedIndexCollection Methods ---
432 /// Note: IList methods are stubbed out, otherwise does not IList interface cannot be implemented
434 public bool Contains(int index
)
436 return collection_
.Contains(index
);
440 public void CopyTo(Array dest
,int index
)
442 collection_
.CopyTo(dest
, index
);
446 public IEnumerator
GetEnumerator()
448 return collection_
.GetEnumerator();
452 public int IndexOf(int index
)
454 return collection_
.IndexOf(index
);
457 /// --- CheckedIndexCollection.IList methods ---
459 int IList
.Add(object value)
461 throw new NotImplementedException ();
467 throw new NotImplementedException ();
471 bool IList
.Contains(object index
)
473 throw new NotImplementedException ();
477 int IList
.IndexOf(object index
)
479 throw new NotImplementedException ();
483 void IList
.Insert(int index
,object value)
485 throw new NotImplementedException ();
489 void IList
.Remove(object value)
491 throw new NotImplementedException ();
495 void IList
.RemoveAt(int index
)
497 throw new NotImplementedException ();
499 } // --- end of CheckedListBox.CheckedIndexCollection ---
504 /// sub-class: CheckedListBox.CheckedItemCollection
506 /// Encapsulates the collection of checked items (including items in an indeterminate state) in a CheckedListBox control.
509 public class CheckedItemCollection
: IList
, ICollection
, IEnumerable
{
511 CheckedListBox owner_
;
512 ArrayList collection_
;
514 internal CheckedItemCollection(CheckedListBox owner
)
517 collection_
= owner_
.Items
.CreateCheckedItemList();
520 /// --- CheckedItemCollection Properties ---
523 get { return collection_.Count; }
527 public bool IsReadOnly
{
528 get { return collection_.IsReadOnly; }
531 /// --- ICollection properties ---
532 bool IList
.IsFixedSize
{
533 [MonoTODO
] get { throw new NotImplementedException (); }
536 object IList
.this[int index
] {
537 [MonoTODO
] get { throw new NotImplementedException (); }
538 [MonoTODO
] set { throw new NotImplementedException (); }
542 public object this[int index
] {
543 get { return collection_[index]; }
544 set { throw new NotImplementedException (); }
547 object ICollection
.SyncRoot
{
549 [MonoTODO
] get { throw new NotImplementedException (); }
552 bool ICollection
.IsSynchronized
{
554 [MonoTODO
] get { throw new NotImplementedException (); }
559 /// --- CheckedItemCollection Methods ---
560 /// Note: IList methods are stubbed out, otherwise IList interface cannot be implemented
562 public bool Contains(object item
)
564 return collection_
.Contains(item
);
568 public void CopyTo(Array dest
,int index
)
570 collection_
.CopyTo(dest
,index
);
574 public IEnumerator
GetEnumerator()
576 return collection_
.GetEnumerator();
580 public int IndexOf(object item
)
582 return collection_
.IndexOf(item
);
585 /// --- CheckedItemCollection.IList methods ---
587 int IList
.Add(object value)
589 throw new NotImplementedException ();
595 throw new NotImplementedException ();
599 bool IList
.Contains(object index
)
601 throw new NotImplementedException ();
605 int IList
.IndexOf(object index
)
607 throw new NotImplementedException ();
611 void IList
.Insert(int index
,object value)
613 throw new NotImplementedException ();
617 void IList
.Remove(object value)
619 throw new NotImplementedException ();
623 void IList
.RemoveAt(int index
)
625 throw new NotImplementedException ();
627 } // --- end of CheckedListBox.CheckedItemCollection ---
629 /// sub-class: CheckedListBox.ObjectCollection
631 /// Represents the collection of items in a CheckedListBox.
635 public new class ObjectCollection
: ListBox
.ObjectCollection
{
637 /// --- ObjectCollection.constructor ---
639 public ObjectCollection(CheckedListBox owner
) :base(owner
)
646 public int Add(object item
,bool isChecked
)
648 throw new NotImplementedException ();
652 public int Add(object item
,CheckState check
)
654 throw new NotImplementedException ();