CheckedListBox.cs 15 KB

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