CheckedListBox.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. SetStyle (ControlStyles.ResizeRedraw, true);
  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 { base.DataSource = value; }
  95. }
  96. [EditorBrowsable (EditorBrowsableState.Never)]
  97. [Browsable (false)]
  98. public new string DisplayMember {
  99. get { return base.DisplayMember; }
  100. set { base.DisplayMember = value; }
  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.ListControlStringCollectionEditor, " + 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. return base.CreateAccessibilityInstance ();
  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.Unchecked);
  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 (Click != null)
  174. Click (this, EventArgs.Empty);
  175. }
  176. protected override void OnDrawItem (DrawItemEventArgs e)
  177. {
  178. ThemeEngine.Current.DrawCheckedListBoxItem (this, e);
  179. }
  180. protected override void OnFontChanged (EventArgs e)
  181. {
  182. base.OnFontChanged (e);
  183. }
  184. protected override void OnHandleCreated (EventArgs e)
  185. {
  186. base.OnHandleCreated (e);
  187. }
  188. protected virtual void OnItemCheck (ItemCheckEventArgs ice)
  189. {
  190. if (ItemCheck != null)
  191. ItemCheck (this, ice);
  192. }
  193. protected override void OnKeyPress (KeyPressEventArgs e)
  194. {
  195. base.OnKeyPress (e);
  196. if (e.KeyChar == ' ') { // Space should check focused item
  197. if (focused_item != -1) {
  198. SetItemChecked (focused_item, !GetItemChecked (focused_item));
  199. }
  200. }
  201. }
  202. protected override void OnMeasureItem (MeasureItemEventArgs e)
  203. {
  204. if (MeasureItem != null)
  205. MeasureItem (this, e);
  206. }
  207. protected override void OnSelectedIndexChanged (EventArgs e)
  208. {
  209. base.OnSelectedIndexChanged (e);
  210. }
  211. public void SetItemChecked (int index, bool value)
  212. {
  213. SetItemCheckState (index, value ? CheckState.Checked : CheckState.Unchecked);
  214. }
  215. public void SetItemCheckState (int index, CheckState value)
  216. {
  217. if (index < 0 || index >= Items.Count)
  218. throw new ArgumentOutOfRangeException ("Index of out range");
  219. if (!Enum.IsDefined (typeof (CheckState), value))
  220. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for CheckState", value));
  221. CheckState old_value = (Items.GetListBoxItem (index)).State;
  222. if (old_value == value)
  223. return;
  224. (Items.GetListBoxItem (index)).State = value;
  225. Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  226. switch (value) {
  227. case CheckState.Checked:
  228. checked_indices.AddIndex (index);
  229. checked_items.AddObject (Items[index]);
  230. break;
  231. case CheckState.Unchecked:
  232. checked_indices.RemoveIndex (index);
  233. checked_items.RemoveObject (Items[index]);
  234. break;
  235. case CheckState.Indeterminate:
  236. default:
  237. break;
  238. }
  239. OnItemCheck (new ItemCheckEventArgs (index, value, old_value));
  240. if (ClientRectangle.Contains (invalidate))
  241. Invalidate (invalidate);
  242. }
  243. protected override void WmReflectCommand (ref Message m)
  244. {
  245. base.WmReflectCommand (ref m);
  246. }
  247. protected override void WndProc (ref Message m)
  248. {
  249. base.WndProc (ref m);
  250. }
  251. #endregion Public Methods
  252. #region Private Methods
  253. internal override void OnMouseDownLB (object sender, MouseEventArgs e)
  254. {
  255. Rectangle hit_rect;
  256. CheckState value = CheckState.Checked;
  257. bool set_value = false;
  258. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  259. if (Click != null) {
  260. if (e.Button == MouseButtons.Left) {
  261. Click (this, e);
  262. }
  263. }
  264. if (index == -1)
  265. return;
  266. /* CheckBox hit */
  267. hit_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) { //From checked to unchecked
  273. value = CheckState.Unchecked;
  274. set_value = true;
  275. } else {
  276. if (check_onclick) {
  277. set_value = true;
  278. } else {
  279. if ((Items.GetListBoxItem (index)).Selected == true)
  280. set_value = true;
  281. }
  282. }
  283. if (set_value)
  284. SetItemCheckState (index, value);
  285. SelectedItemFromNavigation (index);
  286. SetFocusedItem (index);
  287. }
  288. internal override void UpdateItemInfo (UpdateOperation operation, int first, int last)
  289. {
  290. base.UpdateItemInfo (operation, first, last);
  291. CheckedItems.ReCreate ();
  292. CheckedIndices.ReCreate ();
  293. }
  294. #endregion Private Methods
  295. public class ObjectCollection : ListBox.ObjectCollection
  296. {
  297. private CheckedListBox owner;
  298. public ObjectCollection (CheckedListBox owner) : base (owner)
  299. {
  300. this.owner = owner;
  301. }
  302. public int Add (object item, bool isChecked)
  303. {
  304. if (isChecked)
  305. return Add (item, CheckState.Checked);
  306. return Add (item, CheckState.Unchecked);
  307. }
  308. public int Add (object item, CheckState check)
  309. {
  310. int cnt = object_items.Count;
  311. ListBox.ListBoxItem box_item = new ListBox.ListBoxItem (cnt);
  312. box_item.State = check;
  313. object_items.Add (item);
  314. listbox_items.Add (box_item);
  315. if (check == CheckState.Checked)
  316. owner.OnItemCheck (new ItemCheckEventArgs (cnt, check, CheckState.Unchecked));
  317. owner.UpdateItemInfo (UpdateOperation.AddItems, cnt, cnt);
  318. return cnt;
  319. }
  320. }
  321. /*
  322. CheckedListBox.CheckedIndexCollection
  323. */
  324. public class CheckedIndexCollection : IList, ICollection, IEnumerable
  325. {
  326. private CheckedListBox owner;
  327. private ArrayList indices = new ArrayList ();
  328. internal CheckedIndexCollection (CheckedListBox owner)
  329. {
  330. this.owner = owner;
  331. }
  332. #region Public Properties
  333. public int Count {
  334. get { return indices.Count; }
  335. }
  336. public bool IsReadOnly {
  337. get { return true;}
  338. }
  339. bool ICollection.IsSynchronized {
  340. get { return false; }
  341. }
  342. bool IList.IsFixedSize{
  343. get { return true; }
  344. }
  345. object ICollection.SyncRoot {
  346. get { return this; }
  347. }
  348. [Browsable (false)]
  349. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  350. public int this[int index] {
  351. get {
  352. if (index < 0 || index >= Count)
  353. throw new ArgumentOutOfRangeException ("Index of out range");
  354. return (int) indices[index];
  355. }
  356. }
  357. #endregion Public Properties
  358. public bool Contains (int index)
  359. {
  360. return indices.Contains (index);
  361. }
  362. public void CopyTo (Array dest, int index)
  363. {
  364. indices.CopyTo (dest, index);
  365. }
  366. public IEnumerator GetEnumerator ()
  367. {
  368. return indices.GetEnumerator ();
  369. }
  370. int IList.Add (object value)
  371. {
  372. throw new NotSupportedException ();
  373. }
  374. void IList.Clear ()
  375. {
  376. throw new NotSupportedException ();
  377. }
  378. bool IList.Contains (object index)
  379. {
  380. return Contains ((int)index);
  381. }
  382. int IList.IndexOf (object index)
  383. {
  384. return IndexOf ((int) index);
  385. }
  386. void IList.Insert (int index, object value)
  387. {
  388. throw new NotSupportedException ();
  389. }
  390. void IList.Remove (object value)
  391. {
  392. throw new NotSupportedException ();
  393. }
  394. void IList.RemoveAt (int index)
  395. {
  396. throw new NotSupportedException ();
  397. }
  398. object IList.this[int index]{
  399. get {return indices[index]; }
  400. set {throw new NotImplementedException (); }
  401. }
  402. public int IndexOf (int index)
  403. {
  404. return indices.IndexOf (index);
  405. }
  406. #region Private Methods
  407. internal void AddIndex (int index)
  408. {
  409. indices.Add (index);
  410. }
  411. internal void ClearIndices ()
  412. {
  413. indices.Clear ();
  414. }
  415. internal void RemoveIndex (int index)
  416. {
  417. indices.Remove (index);
  418. }
  419. internal void ReCreate ()
  420. {
  421. indices.Clear ();
  422. for (int i = 0; i < owner.Items.Count; i++) {
  423. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  424. if (item.State == CheckState.Checked)
  425. indices.Add (item.Index);
  426. }
  427. }
  428. #endregion Private Methods
  429. }
  430. /*
  431. CheckedItemCollection
  432. */
  433. public class CheckedItemCollection : IList, ICollection, IEnumerable
  434. {
  435. private CheckedListBox owner;
  436. private ArrayList object_items = new ArrayList ();
  437. internal CheckedItemCollection (CheckedListBox owner)
  438. {
  439. this.owner = owner;
  440. }
  441. #region Public Properties
  442. public int Count {
  443. get { return object_items.Count; }
  444. }
  445. public bool IsReadOnly {
  446. get { return true; }
  447. }
  448. [Browsable (false)]
  449. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  450. public object this [int index] {
  451. get {
  452. if (index < 0 || index >= Count)
  453. throw new ArgumentOutOfRangeException ("Index of out range");
  454. return object_items[index];
  455. }
  456. set {throw new NotSupportedException ();}
  457. }
  458. bool ICollection.IsSynchronized {
  459. get { return true; }
  460. }
  461. object ICollection.SyncRoot {
  462. get { return this; }
  463. }
  464. bool IList.IsFixedSize {
  465. get { return true; }
  466. }
  467. object IList.this[int index] {
  468. get { return object_items[index]; }
  469. set { throw new NotSupportedException (); }
  470. }
  471. #endregion Public Properties
  472. #region Public Methods
  473. public bool Contains (object selectedObject)
  474. {
  475. return object_items.Contains (selectedObject);
  476. }
  477. public void CopyTo (Array dest, int index)
  478. {
  479. object_items.CopyTo (dest, index);
  480. }
  481. int IList.Add (object value)
  482. {
  483. throw new NotSupportedException ();
  484. }
  485. void IList.Clear ()
  486. {
  487. throw new NotSupportedException ();
  488. }
  489. bool IList.Contains (object selectedIndex)
  490. {
  491. throw new NotImplementedException ();
  492. }
  493. void IList.Insert (int index, object value)
  494. {
  495. throw new NotSupportedException ();
  496. }
  497. void IList.Remove (object value)
  498. {
  499. throw new NotSupportedException ();
  500. }
  501. void IList.RemoveAt (int index)
  502. {
  503. throw new NotSupportedException ();
  504. }
  505. public int IndexOf (object item)
  506. {
  507. return object_items.IndexOf (item);
  508. }
  509. public IEnumerator GetEnumerator ()
  510. {
  511. return object_items.GetEnumerator ();
  512. }
  513. #endregion Public Methods
  514. #region Private Methods
  515. internal void AddObject (object obj)
  516. {
  517. object_items.Add (obj);
  518. }
  519. internal void ClearObjects ()
  520. {
  521. object_items.Clear ();
  522. }
  523. internal void ReCreate ()
  524. {
  525. object_items.Clear ();
  526. for (int i = 0; i < owner.Items.Count; i++) {
  527. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  528. if (item.State == CheckState.Checked)
  529. object_items.Add (owner.Items[item.Index]);
  530. }
  531. }
  532. internal void RemoveObject (object obj)
  533. {
  534. object_items.Remove (obj);
  535. }
  536. #endregion Private Methods
  537. }
  538. #if NET_2_0
  539. public bool UseCompatibleTextRendering {
  540. get {
  541. return use_compatible_text_rendering;
  542. }
  543. set {
  544. use_compatible_text_rendering = value;
  545. }
  546. }
  547. #endif
  548. }
  549. }