**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / CheckedListBox.cs
blobbb1e1f114b4cf13c5f4bae54da139d1d81fac46b
1 //
2 // System.Windows.Forms.CheckedListBox.cs
3 //
4 // Author:
5 // stubbed out by Jaak Simm (jaaksimm@firm.ee)
6 // Denis hayes (dennish@raytek.com)
7 // Alexandre Pigolkine (pigolkine@gmx.de)
8 //
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:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
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.
33 using System.Drawing;
34 using System.Collections;
36 namespace System.Windows.Forms {
38 /// <summary>
39 /// Displays a ListBox in which a check box is displayed to the left of each item.
40 /// </summary>
42 [MonoTODO]
43 public class CheckedListBox : ListBox {
45 // private fields
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()
54 checkOnClick = false;
55 threeDCheckBoxes = true;
56 DrawMode_ = DrawMode.Normal;
59 internal override void OnObjectCollectionChanged() {
60 CheckedIndices_ = null;
61 CheckedItems_ = null;
62 base.OnObjectCollectionChanged();
65 // --- CheckedListBox Properties ---
66 [MonoTODO]
67 public CheckedListBox.CheckedIndexCollection CheckedIndices {
68 get {
69 if( CheckedIndices_ == null) {
70 CheckedIndices_ = new CheckedListBox.CheckedIndexCollection(this);
72 return CheckedIndices_;
76 [MonoTODO]
77 public CheckedListBox.CheckedItemCollection CheckedItems {
78 get {
79 if( CheckedItems_ == null) {
80 CheckedItems_ = new CheckedListBox.CheckedItemCollection(this);
82 return CheckedItems_;
86 public bool CheckOnClick {
87 get {
88 return checkOnClick;
90 set {
91 checkOnClick = value;
95 [MonoTODO]
96 protected override CreateParams CreateParams {
97 get {
98 CreateParams createParams = base.CreateParams;
99 // set ownerDraw flag to be able to paint check-boxes
100 createParams.Style |= (int)ListBoxStyles.LBS_OWNERDRAWFIXED;
101 return createParams;
105 [MonoTODO]
106 public override DrawMode DrawMode {
107 get {
108 return DrawMode.Normal;
110 set {
111 // always DrawMode.Normal
115 [MonoTODO]
116 public override int ItemHeight {
117 get {
118 //FIXME
119 return base.ItemHeight;
121 set {
122 //FIXME
123 base.ItemHeight = value;
127 [MonoTODO]
128 public new CheckedListBox.ObjectCollection Items {
129 get {
130 return (CheckedListBox.ObjectCollection)base.Items;
134 [MonoTODO]
135 public new object DataSource { // .NET V1.1 Beta. needs implmented
136 get { return base.DataSource; }
137 set { base.DataSource = value; }
140 [MonoTODO]
141 public new string DisplayMember { // .NET V1.1 Beta. needs implmented
142 get { return base.DisplayMember; }
143 set { base.DisplayMember = value; }
146 [MonoTODO]
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 {
153 get {
154 return base.SelectionMode;
156 set {
157 if (value!=SelectionMode.One && value!=SelectionMode.None)
158 throw new ArgumentException();
159 base.SelectionMode=value;
163 public bool ThreeDCheckBoxes {
164 get { return threeDCheckBoxes; }
165 set {
166 if( threeDCheckBoxes != value) {
167 threeDCheckBoxes = value;
168 Invalidate();
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 ();
188 [MonoTODO]
189 protected override ListBox.ObjectCollection CreateItemCollection() {
190 return (ListBox.ObjectCollection)(new CheckedListBox.ObjectCollection( this));
193 [MonoTODO]
194 public bool GetItemChecked(int index)
196 return CheckedIndices.Contains(index);
199 [MonoTODO]
200 public CheckState GetItemCheckState(int index)
202 return CheckedIndices.Contains(index) ? CheckState.Checked : CheckState.Unchecked;
205 // [event methods]
206 [MonoTODO]
207 protected override void OnBackColorChanged(EventArgs e)
209 //FIXME
210 base.OnBackColorChanged(e);
213 [MonoTODO]
214 protected override void OnClick(EventArgs e)
216 //FIXME
217 base.OnClick(e);
220 [MonoTODO]
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);
237 else {
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);
256 paintOn.Dispose ();
257 bmp.Dispose();
260 [MonoTODO]
261 protected override void OnFontChanged(EventArgs e)
263 //FIXME
264 base.OnFontChanged(e);
267 [MonoTODO]
268 protected override void OnHandleCreated(EventArgs e)
270 //FIXME
271 base.OnHandleCreated(e);
274 // only supports .NET framework, thus is not stubbed out
276 [MonoTODO]
277 protected virtual void OnItemCheck(ItemCheckEventArgs ice)
279 throw new NotImplementedException ();
283 [MonoTODO]
284 protected override void OnKeyPress(KeyPressEventArgs e)
286 //FIXME
287 base.OnKeyPress(e);
290 [MonoTODO]
291 protected override void OnMeasureItem(MeasureItemEventArgs e)
293 //FIXME
294 base.OnMeasureItem(e);
297 [MonoTODO]
298 protected override void OnSelectedIndexChanged(EventArgs e)
300 //FIXME
301 base.OnSelectedIndexChanged(e);
303 // end of [event methods]
305 [MonoTODO]
306 public void SetItemChecked(int index,bool value)
308 SetItemCheckState(index, value ? CheckState.Checked : CheckState.Unchecked);
311 [MonoTODO]
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 ?
325 Invalidate();
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;
341 [MonoTODO]
342 protected override void WndProc(ref Message m)
345 //FIXME
346 switch ((Msg) m.Msg) {
347 case Msg.WM_COMMAND:
348 switch(m.HiWordWParam) {
349 case (uint)ListBoxNotifications.LBN_SELCHANGE:
350 listboxSelChange();
351 m.Result = IntPtr.Zero;
352 break;
353 case (uint)ListBoxNotifications.LBN_DBLCLK:
354 listboxSelChange();
355 m.Result = IntPtr.Zero;
356 break;
357 default:
358 base.WndProc(ref m);
359 break;
361 break;
362 default:
363 base.WndProc(ref m);
364 break;
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
379 /// <summary>
380 /// Encapsulates the collection of indexes of checked items (including items in an indeterminate state) in a CheckedListBox.
381 /// </summary>
382 [MonoTODO]
383 public class CheckedIndexCollection : IList, ICollection, IEnumerable {
384 CheckedListBox owner_;
385 ArrayList collection_;
387 internal CheckedIndexCollection(CheckedListBox owner)
389 owner_ = owner;
390 collection_ = owner_.Items.CreateCheckedIndexList();
393 /// --- CheckedIndexCollection Properties ---
394 [MonoTODO]
395 public int Count {
396 get { return collection_.Count; }
399 [MonoTODO]
400 public bool IsReadOnly {
401 get { return true; }
404 [MonoTODO]
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(); }
417 [MonoTODO] set { ; }
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
433 [MonoTODO]
434 public bool Contains(int index)
436 return collection_.Contains(index);
439 [MonoTODO]
440 public void CopyTo(Array dest,int index)
442 collection_.CopyTo(dest, index);
445 [MonoTODO]
446 public IEnumerator GetEnumerator()
448 return collection_.GetEnumerator();
451 [MonoTODO]
452 public int IndexOf(int index)
454 return collection_.IndexOf(index);
457 /// --- CheckedIndexCollection.IList methods ---
458 [MonoTODO]
459 int IList.Add(object value)
461 throw new NotImplementedException ();
464 [MonoTODO]
465 void IList.Clear()
467 throw new NotImplementedException ();
470 [MonoTODO]
471 bool IList.Contains(object index)
473 throw new NotImplementedException ();
476 [MonoTODO]
477 int IList.IndexOf(object index)
479 throw new NotImplementedException ();
482 [MonoTODO]
483 void IList.Insert(int index,object value)
485 throw new NotImplementedException ();
488 [MonoTODO]
489 void IList.Remove(object value)
491 throw new NotImplementedException ();
494 [MonoTODO]
495 void IList.RemoveAt(int index)
497 throw new NotImplementedException ();
499 } // --- end of CheckedListBox.CheckedIndexCollection ---
504 /// sub-class: CheckedListBox.CheckedItemCollection
505 /// <summary>
506 /// Encapsulates the collection of checked items (including items in an indeterminate state) in a CheckedListBox control.
507 /// </summary>
508 [MonoTODO]
509 public class CheckedItemCollection : IList, ICollection, IEnumerable {
511 CheckedListBox owner_;
512 ArrayList collection_;
514 internal CheckedItemCollection(CheckedListBox owner)
516 owner_ = owner;
517 collection_ = owner_.Items.CreateCheckedItemList();
520 /// --- CheckedItemCollection Properties ---
521 [MonoTODO]
522 public int Count {
523 get { return collection_.Count; }
526 [MonoTODO]
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 (); }
541 [MonoTODO]
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
561 [MonoTODO]
562 public bool Contains(object item)
564 return collection_.Contains(item);
567 [MonoTODO]
568 public void CopyTo(Array dest,int index)
570 collection_.CopyTo(dest,index);
573 [MonoTODO]
574 public IEnumerator GetEnumerator()
576 return collection_.GetEnumerator();
579 [MonoTODO]
580 public int IndexOf(object item)
582 return collection_.IndexOf(item);
585 /// --- CheckedItemCollection.IList methods ---
586 [MonoTODO]
587 int IList.Add(object value)
589 throw new NotImplementedException ();
592 [MonoTODO]
593 void IList.Clear()
595 throw new NotImplementedException ();
598 [MonoTODO]
599 bool IList.Contains(object index)
601 throw new NotImplementedException ();
604 [MonoTODO]
605 int IList.IndexOf(object index)
607 throw new NotImplementedException ();
610 [MonoTODO]
611 void IList.Insert(int index,object value)
613 throw new NotImplementedException ();
616 [MonoTODO]
617 void IList.Remove(object value)
619 throw new NotImplementedException ();
622 [MonoTODO]
623 void IList.RemoveAt(int index)
625 throw new NotImplementedException ();
627 } // --- end of CheckedListBox.CheckedItemCollection ---
629 /// sub-class: CheckedListBox.ObjectCollection
630 /// <summary>
631 /// Represents the collection of items in a CheckedListBox.
632 /// </summary>
634 [MonoTODO]
635 public new class ObjectCollection : ListBox.ObjectCollection {
637 /// --- ObjectCollection.constructor ---
638 [MonoTODO]
639 public ObjectCollection(CheckedListBox owner) :base(owner)
644 /// --- methods ---
645 [MonoTODO]
646 public int Add(object item,bool isChecked)
648 throw new NotImplementedException ();
651 [MonoTODO]
652 public int Add(object item,CheckState check)
654 throw new NotImplementedException ();