CheckedListBox.cs 15 KB

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