CheckedListBox.cs 16 KB

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