CheckedListBox.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  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.
  19. //
  20. // Copyright (c) 2004-2005 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. //
  25. //
  26. // NOT COMPLETE
  27. using System;
  28. using System.Drawing;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.Reflection;
  32. namespace System.Windows.Forms
  33. {
  34. public class CheckedListBox : ListBox
  35. {
  36. private CheckedIndexCollection checked_indices;
  37. private CheckedItemCollection checked_items;
  38. private bool check_onclick;
  39. private bool three_dcheckboxes;
  40. private static readonly Rectangle checkbox_rect = new Rectangle (2, 2, 11,11); // Position of the checkbox relative to the item
  41. public CheckedListBox ()
  42. {
  43. items = new CheckedListBox.ObjectCollection (this);
  44. checked_indices = new CheckedIndexCollection (this);
  45. checked_items = new CheckedItemCollection (this);
  46. check_onclick = false;
  47. three_dcheckboxes = false;
  48. listbox_info.item_height = FontHeight + 2;
  49. }
  50. #region events
  51. [Browsable (false)]
  52. [EditorBrowsable (EditorBrowsableState.Never)]
  53. public new event EventHandler Click;
  54. [Browsable (false)]
  55. [EditorBrowsable (EditorBrowsableState.Never)]
  56. public new event EventHandler DataSourceChanged;
  57. [Browsable (false)]
  58. [EditorBrowsable (EditorBrowsableState.Never)]
  59. public new event EventHandler DisplayMemberChanged;
  60. [Browsable (false)]
  61. [EditorBrowsable (EditorBrowsableState.Never)]
  62. public new event DrawItemEventHandler DrawItem;
  63. public event ItemCheckEventHandler ItemCheck;
  64. [Browsable (false)]
  65. [EditorBrowsable (EditorBrowsableState.Never)]
  66. public new event MeasureItemEventHandler MeasureItem;
  67. [Browsable (false)]
  68. [EditorBrowsable (EditorBrowsableState.Never)]
  69. public new event EventHandler ValueMemberChanged;
  70. #endregion Events
  71. #region Public Properties
  72. [Browsable (false)]
  73. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  74. public CheckedListBox.CheckedIndexCollection CheckedIndices {
  75. get {return checked_indices; }
  76. }
  77. [Browsable (false)]
  78. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  79. public CheckedListBox.CheckedItemCollection CheckedItems {
  80. get {return checked_items; }
  81. }
  82. [DefaultValue (false)]
  83. public bool CheckOnClick {
  84. get { return check_onclick; }
  85. set { check_onclick = value; }
  86. }
  87. protected override CreateParams CreateParams {
  88. get { return base.CreateParams;}
  89. }
  90. [EditorBrowsable (EditorBrowsableState.Never)]
  91. [Browsable (false)]
  92. public new object DataSource {
  93. get { return base.DataSource; }
  94. set { DataSource = value; }
  95. }
  96. [EditorBrowsable (EditorBrowsableState.Never)]
  97. [Browsable (false)]
  98. public new string DisplayMember {
  99. get { throw new NotImplementedException (); }
  100. set { throw new NotImplementedException (); }
  101. }
  102. [Browsable (false)]
  103. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  104. [EditorBrowsable (EditorBrowsableState.Never)]
  105. public override DrawMode DrawMode {
  106. get { return DrawMode.Normal; }
  107. set { /* Not possible */ }
  108. }
  109. [Browsable (false)]
  110. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  111. [EditorBrowsable (EditorBrowsableState.Never)]
  112. public override int ItemHeight {
  113. get { return listbox_info.item_height; }
  114. set { /* Not possible */ }
  115. }
  116. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  117. [Localizable (true)]
  118. [Editor ("System.Windows.Forms.Design.ListContolStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  119. public new CheckedListBox.ObjectCollection Items {
  120. get { return (CheckedListBox.ObjectCollection) base.Items; }
  121. }
  122. public override SelectionMode SelectionMode {
  123. get { return base.SelectionMode; }
  124. set {
  125. if (value == SelectionMode.MultiSimple || value == SelectionMode.MultiExtended)
  126. throw new InvalidEnumArgumentException ("Multi selection modes not supported");
  127. base.SelectionMode = value;
  128. }
  129. }
  130. [DefaultValue (false)]
  131. public bool ThreeDCheckBoxes {
  132. get { return three_dcheckboxes; }
  133. set {
  134. if (three_dcheckboxes == value)
  135. return;
  136. three_dcheckboxes = value;
  137. Refresh ();
  138. }
  139. }
  140. [Browsable (false)]
  141. [EditorBrowsable (EditorBrowsableState.Never)]
  142. public new string ValueMember {
  143. get { return base.ValueMember; }
  144. set { base.ValueMember = value; }
  145. }
  146. #endregion Public Properties
  147. #region Public Methods
  148. protected override AccessibleObject CreateAccessibilityInstance ()
  149. {
  150. throw new NotImplementedException ();
  151. }
  152. protected override ListBox.ObjectCollection CreateItemCollection ()
  153. {
  154. return new ObjectCollection (this);
  155. }
  156. public bool GetItemChecked (int index)
  157. {
  158. return (GetItemCheckState (index) == CheckState.Checked);
  159. }
  160. public CheckState GetItemCheckState (int index)
  161. {
  162. if (index < 0 || index >= Items.Count)
  163. throw new ArgumentOutOfRangeException ("Index of out range");
  164. return (Items.GetListBoxItem (index)).State;
  165. }
  166. protected override void OnBackColorChanged (EventArgs e)
  167. {
  168. base.OnBackColorChanged (e);
  169. }
  170. protected override void OnClick (EventArgs e)
  171. {
  172. base.OnClick (e);
  173. if (check_onclick) {
  174. if (focused_item != -1) {
  175. SetItemChecked (focused_item, !GetItemChecked (focused_item));
  176. }
  177. }
  178. if (Click != null)
  179. Click (this, EventArgs.Empty);
  180. }
  181. protected override void OnDrawItem (DrawItemEventArgs e)
  182. {
  183. Color back_color, fore_color;
  184. Rectangle item_rect = e.Bounds;
  185. ButtonState state;
  186. /* Draw checkbox */
  187. if ((Items.GetListBoxItem (e.Index)).State == CheckState.Checked)
  188. state = ButtonState.Checked;
  189. else
  190. state = ButtonState.Normal;
  191. if (ThreeDCheckBoxes == false)
  192. state |= ButtonState.Flat;
  193. ControlPaint.DrawCheckBox (e.Graphics,
  194. item_rect.X + checkbox_rect.X, item_rect.Y + checkbox_rect.Y,
  195. checkbox_rect.Width, checkbox_rect.Height,
  196. state);
  197. item_rect.X += checkbox_rect.Width + checkbox_rect.X * 2;
  198. item_rect.Width -= checkbox_rect.Width + checkbox_rect.X * 2;
  199. /* Draw text*/
  200. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
  201. back_color = ThemeEngine.Current.ColorHilight;
  202. fore_color = ThemeEngine.Current.ColorHilightText;
  203. }
  204. else {
  205. back_color = e.BackColor;
  206. fore_color = e.ForeColor;
  207. }
  208. e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  209. (back_color), item_rect);
  210. e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
  211. ThemeEngine.Current.ResPool.GetSolidBrush (fore_color),
  212. item_rect, string_format);
  213. if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
  214. ThemeEngine.Current.CPDrawFocusRectangle (e.Graphics, item_rect,
  215. fore_color, back_color);
  216. }
  217. }
  218. protected override void OnFontChanged (EventArgs e)
  219. {
  220. base.OnFontChanged (e);
  221. }
  222. protected override void OnHandleCreated (EventArgs e)
  223. {
  224. base.OnHandleCreated (e);
  225. }
  226. protected virtual void OnItemCheck (ItemCheckEventArgs ice)
  227. {
  228. if (ItemCheck != null)
  229. ItemCheck (this, ice);
  230. }
  231. protected override void OnKeyPress (KeyPressEventArgs e)
  232. {
  233. base.OnKeyPress (e);
  234. if (e.KeyChar == ' ') { // Space should check focused item
  235. if (focused_item != -1) {
  236. SetItemChecked (focused_item, !GetItemChecked (focused_item));
  237. }
  238. }
  239. }
  240. protected override void OnMeasureItem (MeasureItemEventArgs e)
  241. {
  242. if (MeasureItem != null)
  243. MeasureItem (this, e);
  244. }
  245. protected override void OnSelectedIndexChanged (EventArgs e)
  246. {
  247. base.OnSelectedIndexChanged (e);
  248. }
  249. public void SetItemChecked (int index, bool value)
  250. {
  251. SetItemCheckState (index, value ? CheckState.Checked : CheckState.Unchecked);
  252. }
  253. public void SetItemCheckState (int index, CheckState value)
  254. {
  255. if (index < 0 || index >= Items.Count)
  256. throw new ArgumentOutOfRangeException ("Index of out range");
  257. if (!Enum.IsDefined (typeof (CheckState), value))
  258. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for CheckState", value));
  259. CheckState old_value = (Items.GetListBoxItem (index)).State;
  260. if (old_value == value)
  261. return;
  262. (Items.GetListBoxItem (index)).State = value;
  263. Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  264. switch (value) {
  265. case CheckState.Checked:
  266. checked_indices.AddIndex (index);
  267. checked_items.AddObject (Items[index]);
  268. break;
  269. case CheckState.Unchecked:
  270. checked_indices.RemoveIndex (index);
  271. checked_items.RemoveObject (Items[index]);
  272. break;
  273. case CheckState.Indeterminate:
  274. default:
  275. break;
  276. }
  277. OnItemCheck (new ItemCheckEventArgs (index, value, old_value));
  278. if (ClientRectangle.Contains (invalidate))
  279. Invalidate (invalidate);
  280. }
  281. protected override void WmReflectCommand (ref Message m)
  282. {
  283. base.WmReflectCommand (ref m);
  284. }
  285. protected override void WndProc (ref Message m)
  286. {
  287. base.WndProc (ref m);
  288. }
  289. #endregion Public Methods
  290. #region Private Methods
  291. internal override void OnMouseDownLB (object sender, MouseEventArgs e)
  292. {
  293. Rectangle hit_rect, item_rect;
  294. CheckState value = CheckState.Checked;
  295. bool set_value = false;
  296. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  297. if (index == -1)
  298. return;
  299. /* CheckBox hit */
  300. hit_rect = item_rect = GetItemDisplayRectangle (index, LBoxInfo.top_item); // Full item rect
  301. hit_rect.X += checkbox_rect.X;
  302. hit_rect.Y += checkbox_rect.Y;
  303. hit_rect.Width = checkbox_rect.Width;
  304. hit_rect.Height = checkbox_rect.Height;
  305. if ((Items.GetListBoxItem (index)).State == CheckState.Checked)
  306. value = CheckState.Unchecked;
  307. if (hit_rect.Contains (e.X, e.Y) == true) {
  308. set_value = true;
  309. } else {
  310. if (item_rect.Contains (e.X, e.Y) == true) {
  311. if (!check_onclick) {
  312. if ((Items.GetListBoxItem (index)).Selected == true)
  313. set_value = true;
  314. }
  315. }
  316. }
  317. if (set_value)
  318. SetItemCheckState (index, value);
  319. base.OnMouseDownLB (sender, e);
  320. }
  321. internal override void UpdateItemInfo (UpdateOperation operation, int first, int last)
  322. {
  323. base.UpdateItemInfo (operation, first, last);
  324. CheckedItems.ReCreate ();
  325. CheckedIndices.ReCreate ();
  326. }
  327. #endregion Private Methods
  328. public class ObjectCollection : ListBox.ObjectCollection
  329. {
  330. public ObjectCollection (CheckedListBox owner) : base (owner)
  331. {
  332. }
  333. public int Add (object item, bool isChecked)
  334. {
  335. if (isChecked)
  336. return Add (item, CheckState.Checked);
  337. return Add (item, CheckState.Unchecked);
  338. }
  339. public int Add (object item, CheckState check)
  340. {
  341. int cnt = object_items.Count;
  342. ListBox.ListBoxItem box_item = new ListBox.ListBoxItem (cnt);
  343. box_item.State = check;
  344. object_items.Add (item);
  345. listbox_items.Add (box_item);
  346. return cnt;
  347. }
  348. }
  349. /*
  350. CheckedListBox.CheckedIndexCollection
  351. */
  352. public class CheckedIndexCollection : IList, ICollection, IEnumerable
  353. {
  354. private CheckedListBox owner;
  355. private ArrayList indices = new ArrayList ();
  356. internal CheckedIndexCollection (CheckedListBox owner)
  357. {
  358. this.owner = owner;
  359. }
  360. #region Public Properties
  361. public virtual int Count {
  362. get { return indices.Count; }
  363. }
  364. public virtual bool IsReadOnly {
  365. get { return true;}
  366. }
  367. bool ICollection.IsSynchronized {
  368. get { return false; }
  369. }
  370. bool IList.IsFixedSize{
  371. get { return true; }
  372. }
  373. object ICollection.SyncRoot {
  374. get { return this; }
  375. }
  376. public int this[int index] {
  377. get {
  378. if (index < 0 || index >= Count)
  379. throw new ArgumentOutOfRangeException ("Index of out range");
  380. return (int) indices[index];
  381. }
  382. }
  383. #endregion Public Properties
  384. public bool Contains (int index)
  385. {
  386. return indices.Contains (index);
  387. }
  388. public virtual void CopyTo (Array dest, int index)
  389. {
  390. indices.CopyTo (dest, index);
  391. }
  392. public virtual IEnumerator GetEnumerator ()
  393. {
  394. return indices.GetEnumerator ();
  395. }
  396. int IList.Add (object value)
  397. {
  398. throw new NotSupportedException ();
  399. }
  400. void IList.Clear ()
  401. {
  402. throw new NotSupportedException ();
  403. }
  404. bool IList.Contains (object index)
  405. {
  406. return Contains ((int)index);
  407. }
  408. int IList.IndexOf (object index)
  409. {
  410. return IndexOf ((int) index);
  411. }
  412. void IList.Insert (int index, object value)
  413. {
  414. throw new NotSupportedException ();
  415. }
  416. void IList.Remove (object value)
  417. {
  418. throw new NotSupportedException ();
  419. }
  420. void IList.RemoveAt (int index)
  421. {
  422. throw new NotSupportedException ();
  423. }
  424. object IList.this[int index]{
  425. get {return indices[index]; }
  426. set {throw new NotImplementedException (); }
  427. }
  428. public int IndexOf (int index)
  429. {
  430. return indices.IndexOf (index);
  431. }
  432. #region Private Methods
  433. internal void AddIndex (int index)
  434. {
  435. indices.Add (index);
  436. }
  437. internal void ClearIndices ()
  438. {
  439. indices.Clear ();
  440. }
  441. internal void RemoveIndex (int index)
  442. {
  443. indices.Remove (index);
  444. }
  445. internal void ReCreate ()
  446. {
  447. indices.Clear ();
  448. for (int i = 0; i < owner.Items.Count; i++) {
  449. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  450. if (item.State == CheckState.Checked)
  451. indices.Add (item.Index);
  452. }
  453. }
  454. #endregion Private Methods
  455. }
  456. /*
  457. CheckedItemCollection
  458. */
  459. public class CheckedItemCollection : IList, ICollection, IEnumerable
  460. {
  461. private CheckedListBox owner;
  462. private ArrayList object_items = new ArrayList ();
  463. internal CheckedItemCollection (CheckedListBox owner)
  464. {
  465. this.owner = owner;
  466. }
  467. #region Public Properties
  468. public virtual int Count {
  469. get { return object_items.Count; }
  470. }
  471. public virtual bool IsReadOnly {
  472. get { return true; }
  473. }
  474. public virtual object this [int index] {
  475. get {
  476. if (index < 0 || index >= Count)
  477. throw new ArgumentOutOfRangeException ("Index of out range");
  478. return object_items[index];
  479. }
  480. set {throw new NotSupportedException ();}
  481. }
  482. bool ICollection.IsSynchronized {
  483. get { return true; }
  484. }
  485. object ICollection.SyncRoot {
  486. get { return this; }
  487. }
  488. bool IList.IsFixedSize {
  489. get { return true; }
  490. }
  491. object IList.this[int index] {
  492. get { return object_items[index]; }
  493. set { throw new NotSupportedException (); }
  494. }
  495. #endregion Public Properties
  496. #region Public Methods
  497. public virtual bool Contains (object selectedObject)
  498. {
  499. return object_items.Contains (selectedObject);
  500. }
  501. public virtual void CopyTo (Array dest, int index)
  502. {
  503. object_items.CopyTo (dest, index);
  504. }
  505. int IList.Add (object value)
  506. {
  507. throw new NotSupportedException ();
  508. }
  509. void IList.Clear ()
  510. {
  511. throw new NotSupportedException ();
  512. }
  513. bool IList.Contains (object selectedIndex)
  514. {
  515. throw new NotImplementedException ();
  516. }
  517. void IList.Insert (int index, object value)
  518. {
  519. throw new NotSupportedException ();
  520. }
  521. void IList.Remove (object value)
  522. {
  523. throw new NotSupportedException ();
  524. }
  525. void IList.RemoveAt (int index)
  526. {
  527. throw new NotSupportedException ();
  528. }
  529. public int IndexOf (object item)
  530. {
  531. return object_items.IndexOf (item);
  532. }
  533. public virtual IEnumerator GetEnumerator ()
  534. {
  535. return object_items.GetEnumerator ();
  536. }
  537. #endregion Public Methods
  538. #region Private Methods
  539. internal void AddObject (object obj)
  540. {
  541. object_items.Add (obj);
  542. }
  543. internal void ClearObjects ()
  544. {
  545. object_items.Clear ();
  546. }
  547. internal void ReCreate ()
  548. {
  549. object_items.Clear ();
  550. for (int i = 0; i < owner.Items.Count; i++) {
  551. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  552. if (item.State == CheckState.Checked)
  553. object_items.Add (owner.Items[item.Index]);
  554. }
  555. }
  556. internal void RemoveObject (object obj)
  557. {
  558. object_items.Remove (obj);
  559. }
  560. #endregion Private Methods
  561. }
  562. }
  563. }