ListBox.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  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. // TODO:
  26. // - Keyboard navigation
  27. // - Horizontal item scroll
  28. // - Performance testing
  29. //
  30. //
  31. // NOT COMPLETE
  32. using System;
  33. using System.Drawing;
  34. using System.Collections;
  35. using System.ComponentModel;
  36. using System.Reflection;
  37. namespace System.Windows.Forms
  38. {
  39. public class ListBox : ListControl
  40. {
  41. internal class ListBoxInfo
  42. {
  43. internal int item_height; /* Item's height */
  44. internal int top_item; /* First item that we show the in the current page */
  45. internal int last_item; /* Last visible item */
  46. internal int page_size; /* Number of listbox items per page. In MultiColumn listbox indicates items per column */
  47. internal Rectangle textdrawing_rect; /* Displayable Client Rectangle minus the scrollbars and with IntegralHeight calculated*/
  48. internal bool show_verticalsb; /* Is Vertical scrollbar show it? */
  49. internal bool show_horizontalsb; /* Is Horizontal scrollbar show it? */
  50. internal Rectangle client_rect; /* Client Rectangle. Usually = ClientRectangle except when IntegralHeight has been applied*/
  51. internal int max_itemwidth; /* Maxium item width within the listbox */
  52. public ListBoxInfo ()
  53. {
  54. last_item = 0;
  55. item_height = 0;
  56. top_item = 0;
  57. page_size = 0;
  58. max_itemwidth = 0;
  59. show_verticalsb = false;
  60. show_horizontalsb = false;
  61. }
  62. }
  63. internal class ListBoxItem
  64. {
  65. internal int Index;
  66. internal bool Selected;
  67. internal CheckState State;
  68. public ListBoxItem (int index)
  69. {
  70. Index = index;
  71. Selected = false;
  72. State = CheckState.Unchecked;
  73. }
  74. }
  75. internal enum ItemNavigation
  76. {
  77. First,
  78. Last,
  79. Next,
  80. Previous,
  81. NextPage,
  82. PreviousPage,
  83. PreviousColumn,
  84. NextColumn
  85. }
  86. private BorderStyle border_style;
  87. private int column_width;
  88. private DrawMode draw_mode;
  89. private int horizontal_extent;
  90. private bool horizontal_scrollbar;
  91. private bool integral_height;
  92. private bool multicolumn;
  93. private bool scroll_always_visible;
  94. private int selected_index;
  95. private SelectedIndexCollection selected_indices;
  96. private object selected_item;
  97. private SelectedObjectCollection selected_items;
  98. private SelectionMode selection_mode;
  99. private bool sorted;
  100. private bool use_tabstops;
  101. private int preferred_height;
  102. private int top_index;
  103. private int column_width_internal;
  104. private VScrollBar vscrollbar_ctrl;
  105. private HScrollBar hscrollbar_ctrl;
  106. private bool suspend_ctrlupdate;
  107. internal StringFormat string_format;
  108. internal ListBoxInfo listbox_info;
  109. internal ObjectCollection items;
  110. public ListBox ()
  111. {
  112. border_style = BorderStyle.Fixed3D;
  113. draw_mode = DrawMode.Normal;
  114. horizontal_extent = 0;
  115. horizontal_scrollbar = false;
  116. integral_height = true;
  117. multicolumn = false;
  118. preferred_height = 7;
  119. scroll_always_visible = false;
  120. selected_index = -1;
  121. selected_item = null;
  122. selection_mode = SelectionMode.One;
  123. sorted = false;
  124. top_index = 0;
  125. use_tabstops = true;
  126. BackColor = ThemeEngine.Current.ColorWindow;
  127. ColumnWidth = 0;
  128. suspend_ctrlupdate = false;
  129. items = new ObjectCollection (this);
  130. selected_indices = new SelectedIndexCollection (this);
  131. selected_items = new SelectedObjectCollection (this);
  132. listbox_info = new ListBoxInfo ();
  133. string_format = new StringFormat ();
  134. listbox_info.item_height = FontHeight;
  135. /* Vertical scrollbar */
  136. vscrollbar_ctrl = new VScrollBar ();
  137. vscrollbar_ctrl.Minimum = 0;
  138. vscrollbar_ctrl.SmallChange = 1;
  139. vscrollbar_ctrl.LargeChange = 1;
  140. vscrollbar_ctrl.Maximum = 0;
  141. vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
  142. vscrollbar_ctrl.Visible = false;
  143. /* Horizontal scrollbar */
  144. hscrollbar_ctrl = new HScrollBar ();
  145. hscrollbar_ctrl.Minimum = 0;
  146. hscrollbar_ctrl.SmallChange = 1;
  147. hscrollbar_ctrl.LargeChange = 1;
  148. hscrollbar_ctrl.Maximum = 0;
  149. hscrollbar_ctrl.Visible = false;
  150. hscrollbar_ctrl.ValueChanged += new EventHandler (HorizontalScrollEvent);
  151. /* Events */
  152. MouseDown += new MouseEventHandler (OnMouseDownLB);
  153. KeyDown += new KeyEventHandler (OnKeyDownLB);
  154. UpdateFormatString ();
  155. }
  156. #region Events
  157. public new event EventHandler BackgroundImageChanged;
  158. public new event EventHandler Click;
  159. public event DrawItemEventHandler DrawItem;
  160. public event MeasureItemEventHandler MeasureItem;
  161. public new event PaintEventHandler Paint;
  162. public event EventHandler SelectedIndexChanged;
  163. public new event EventHandler TextChanged;
  164. #endregion // Events
  165. #region Public Properties
  166. public override Color BackColor {
  167. get { return base.BackColor; }
  168. set {
  169. if (base.BackColor == value)
  170. return;
  171. base.BackColor = value;
  172. Refresh ();
  173. }
  174. }
  175. public override Image BackgroundImage {
  176. get { return base.BackgroundImage; }
  177. set {
  178. if (base.BackgroundImage == value)
  179. return;
  180. base.BackgroundImage = value;
  181. if (BackgroundImageChanged != null)
  182. BackgroundImageChanged (this, EventArgs.Empty);
  183. Refresh ();
  184. }
  185. }
  186. public BorderStyle BorderStyle {
  187. get { return border_style; }
  188. set {
  189. if (!Enum.IsDefined (typeof (BorderStyle), value))
  190. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for BorderStyle", value));
  191. if (border_style == value)
  192. return;
  193. border_style = value;
  194. Refresh ();
  195. }
  196. }
  197. public int ColumnWidth {
  198. get { return column_width; }
  199. set {
  200. if (column_width < 0)
  201. throw new ArgumentException ("A value less than zero is assigned to the property.");
  202. column_width = value;
  203. if (value == 0)
  204. ColumnWidthInternal = 120;
  205. else
  206. ColumnWidthInternal = value;
  207. Refresh ();
  208. }
  209. }
  210. protected override CreateParams CreateParams {
  211. get { return base.CreateParams;}
  212. }
  213. protected override Size DefaultSize {
  214. get { return new Size (120, 96); }
  215. }
  216. public virtual DrawMode DrawMode {
  217. get { return draw_mode; }
  218. set {
  219. if (!Enum.IsDefined (typeof (DrawMode), value))
  220. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
  221. if (draw_mode == value)
  222. return;
  223. draw_mode = value;
  224. Refresh ();
  225. }
  226. }
  227. public override Color ForeColor {
  228. get { return base.ForeColor; }
  229. set {
  230. if (base.ForeColor == value)
  231. return;
  232. base.ForeColor = value;
  233. Refresh ();
  234. }
  235. }
  236. public int HorizontalExtent {
  237. get { return horizontal_extent; }
  238. set {
  239. if (horizontal_extent == value)
  240. return;
  241. horizontal_extent = value;
  242. Refresh ();
  243. }
  244. }
  245. public bool HorizontalScrollbar {
  246. get { return horizontal_scrollbar; }
  247. set {
  248. if (horizontal_scrollbar == value)
  249. return;
  250. horizontal_scrollbar = value;
  251. UpdateShowHorizontalScrollBar ();
  252. Refresh ();
  253. }
  254. }
  255. public bool IntegralHeight {
  256. get { return integral_height; }
  257. set {
  258. if (integral_height == value)
  259. return;
  260. integral_height = value;
  261. CalcClientArea ();
  262. }
  263. }
  264. public virtual int ItemHeight {
  265. get { return listbox_info.item_height; }
  266. set {
  267. if (value > 255)
  268. throw new ArgumentOutOfRangeException ("The ItemHeight property was set beyond 255 pixels");
  269. listbox_info.item_height = value;
  270. CalcClientArea ();
  271. }
  272. }
  273. public ObjectCollection Items {
  274. get { return items; }
  275. }
  276. public bool MultiColumn {
  277. get { return multicolumn; }
  278. set {
  279. if (multicolumn == value)
  280. return;
  281. if (value == true && DrawMode == DrawMode.OwnerDrawVariable)
  282. throw new ArgumentException ("A multicolumn ListBox cannot have a variable-sized height.");
  283. multicolumn = value;
  284. UpdateShowVerticalScrollBar (); /* the needs for scrollbars may change */
  285. UpdateShowHorizontalScrollBar ();
  286. Refresh ();
  287. }
  288. }
  289. public int PreferredHeight {
  290. get { return preferred_height;}
  291. }
  292. public override RightToLeft RightToLeft {
  293. get { return base.RightToLeft; }
  294. set {
  295. if (base.RightToLeft == value)
  296. return;
  297. base.RightToLeft = value;
  298. UpdateFormatString ();
  299. Refresh ();
  300. }
  301. }
  302. // Only afects the Vertical ScrollBar
  303. public bool ScrollAlwaysVisible {
  304. get { return scroll_always_visible; }
  305. set {
  306. if (scroll_always_visible == value)
  307. return;
  308. scroll_always_visible = value;
  309. UpdateShowVerticalScrollBar ();
  310. UpdateShowHorizontalScrollBar ();
  311. }
  312. }
  313. public override int SelectedIndex {
  314. get { return selected_index;}
  315. set {
  316. if (value < -1 || value >= Items.Count)
  317. throw new ArgumentOutOfRangeException ("Index of out range");
  318. if (SelectionMode == SelectionMode.None)
  319. throw new ArgumentException ("cannot call this method if SelectionMode is SelectionMode.None");
  320. if (selected_index == value)
  321. return;
  322. if (SelectionMode == SelectionMode.One)
  323. UnSelectItem (selected_index, true);
  324. SelectItem (value);
  325. selected_index = value;
  326. OnSelectedIndexChanged (new EventArgs ());
  327. }
  328. }
  329. public SelectedIndexCollection SelectedIndices {
  330. get { return selected_indices; }
  331. }
  332. public object SelectedItem {
  333. get {
  334. if (SelectedItems.Count > 0)
  335. return SelectedItems[0];
  336. else
  337. return null;
  338. }
  339. set {
  340. if (selected_item == value)
  341. return;
  342. int index = Items.IndexOf (value);
  343. if (index == -1)
  344. return;
  345. SelectedIndex = index;
  346. }
  347. }
  348. public SelectedObjectCollection SelectedItems {
  349. get {return selected_items;}
  350. }
  351. public virtual SelectionMode SelectionMode {
  352. get { return selection_mode; }
  353. set {
  354. if (!Enum.IsDefined (typeof (SelectionMode), value))
  355. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for SelectionMode", value));
  356. if (selection_mode == value)
  357. return;
  358. selection_mode = value;
  359. Refresh ();
  360. }
  361. }
  362. public bool Sorted {
  363. get { return sorted; }
  364. set {
  365. if (sorted == value)
  366. return;
  367. sorted = value;
  368. Sort ();
  369. }
  370. }
  371. public override string Text {
  372. get {
  373. if (SelectionMode != SelectionMode.None && SelectedIndex != -1)
  374. return Items[SelectedIndex].ToString ();
  375. return base.Text;
  376. }
  377. set {
  378. base.Text = value;
  379. if (SelectionMode == SelectionMode.None)
  380. return;
  381. int index;
  382. index = FindStringExact (value);
  383. if (index == -1)
  384. return;
  385. SelectedIndex = index;
  386. }
  387. }
  388. public int TopIndex {
  389. get { return top_index;}
  390. set {
  391. if (value == top_index)
  392. return;
  393. if (value < 0 || value >= Items.Count)
  394. return;
  395. value = top_index;
  396. Refresh ();
  397. }
  398. }
  399. public bool UseTabStops {
  400. get { return use_tabstops; }
  401. set {
  402. if (use_tabstops == value)
  403. return;
  404. use_tabstops = value;
  405. UpdateFormatString ();
  406. Refresh ();
  407. }
  408. }
  409. #endregion Public Properties
  410. #region Private Properties
  411. internal ListBoxInfo LBoxInfo {
  412. get { return listbox_info; }
  413. }
  414. private int ColumnWidthInternal {
  415. get { return column_width_internal; }
  416. set { column_width_internal = value; }
  417. }
  418. #endregion Private Properties
  419. #region Public Methods
  420. protected virtual void AddItemsCore (object[] value)
  421. {
  422. Items.AddRange (value);
  423. }
  424. public void BeginUpdate ()
  425. {
  426. suspend_ctrlupdate = true;
  427. }
  428. public void ClearSelected ()
  429. {
  430. foreach (int i in selected_indices) {
  431. UnSelectItem (i, false);
  432. }
  433. selected_indices.ClearIndices ();
  434. selected_items.ClearObjects ();
  435. }
  436. protected virtual ObjectCollection CreateItemCollection ()
  437. {
  438. return new ObjectCollection (this);
  439. }
  440. public void EndUpdate ()
  441. {
  442. suspend_ctrlupdate = false;
  443. UpdateItemInfo (false, -1, -1);
  444. Refresh ();
  445. }
  446. public int FindString (String s)
  447. {
  448. return FindString (s, 0);
  449. }
  450. public int FindString (string s, int startIndex)
  451. {
  452. for (int i = startIndex; i < Items.Count; i++) {
  453. if ((Items[i].ToString ()).StartsWith (s))
  454. return i;
  455. }
  456. return -1;
  457. }
  458. public int FindStringExact (string s)
  459. {
  460. return FindStringExact (s, 0);
  461. }
  462. public int FindStringExact (string s, int startIndex)
  463. {
  464. for (int i = startIndex; i < Items.Count; i++) {
  465. if ((Items[i].ToString ()).Equals (s))
  466. return i;
  467. }
  468. return -1;
  469. }
  470. public int GetItemHeight (int index)
  471. {
  472. if (index < 0 || index >= Items.Count)
  473. throw new ArgumentOutOfRangeException ("Index of out range");
  474. return ItemHeight;
  475. }
  476. public Rectangle GetItemRectangle (int index)
  477. {
  478. if (index < 0 || index >= Items.Count)
  479. throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
  480. Rectangle rect = new Rectangle ();
  481. if (MultiColumn == false) {
  482. rect.X = 0;
  483. rect.Y = ItemHeight * index;
  484. rect.Height = ItemHeight;
  485. rect.Width = listbox_info.textdrawing_rect.Width;
  486. }
  487. else {
  488. int which_page;
  489. which_page = index / listbox_info.page_size;
  490. rect.Y = (index % listbox_info.page_size) * ItemHeight;
  491. rect.X = which_page * ColumnWidthInternal;
  492. rect.Height = ItemHeight;
  493. rect.Width = ColumnWidthInternal;
  494. }
  495. return rect;
  496. }
  497. public bool GetSelected (int index)
  498. {
  499. if (index < 0 || index >= Items.Count)
  500. throw new ArgumentOutOfRangeException ("Index of out range");
  501. return (Items.GetListBoxItem (index)).Selected;
  502. }
  503. public int IndexFromPoint (Point p)
  504. {
  505. return IndexFromPoint (p.X, p.Y);
  506. }
  507. // Only returns visible points
  508. public int IndexFromPoint (int x, int y)
  509. {
  510. for (int i = LBoxInfo.top_item; i < LBoxInfo.last_item; i++) {
  511. if (GetItemRectangle (i).Contains (x,y) == true)
  512. return i;
  513. }
  514. return -1;
  515. }
  516. protected override void OnChangeUICues (UICuesEventArgs e)
  517. {
  518. base.OnChangeUICues (e);
  519. }
  520. protected override void OnDataSourceChanged (EventArgs e)
  521. {
  522. base.OnDataSourceChanged (e);
  523. }
  524. protected override void OnDisplayMemberChanged (EventArgs e)
  525. {
  526. base.OnDisplayMemberChanged (e);
  527. }
  528. protected virtual void OnDrawItem (DrawItemEventArgs e)
  529. {
  530. if (DrawItem != null && (DrawMode == DrawMode.OwnerDrawFixed || DrawMode == DrawMode.OwnerDrawVariable)) {
  531. DrawItem (this, e);
  532. return;
  533. }
  534. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
  535. e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  536. (ThemeEngine.Current.ColorHilight), e.Bounds);
  537. e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
  538. ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHilightText),
  539. e.Bounds, string_format);
  540. }
  541. else {
  542. e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  543. (e.BackColor), e.Bounds);
  544. e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
  545. ThemeEngine.Current.ResPool.GetSolidBrush (e.ForeColor),
  546. e.Bounds, string_format);
  547. }
  548. }
  549. protected override void OnFontChanged (EventArgs e)
  550. {
  551. base.OnFontChanged (e);
  552. UpdateShowHorizontalScrollBar ();
  553. UpdateShowVerticalScrollBar ();
  554. RellocateScrollBars ();
  555. CalcClientArea ();
  556. UpdateItemInfo (false, -1, -1);
  557. }
  558. protected override void OnHandleCreated (EventArgs e)
  559. {
  560. base.OnHandleCreated (e);
  561. UpdateInternalClientRect (ClientRectangle);
  562. Controls.Add (vscrollbar_ctrl);
  563. Controls.Add (hscrollbar_ctrl);
  564. UpdateItemInfo (false, -1, -1);
  565. }
  566. protected override void OnHandleDestroyed (EventArgs e)
  567. {
  568. base.OnHandleDestroyed (e);
  569. }
  570. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  571. {
  572. }
  573. protected override void OnParentChanged (EventArgs e)
  574. {
  575. base.OnParentChanged (e);
  576. }
  577. protected override void OnResize (EventArgs e)
  578. {
  579. base.OnResize (e);
  580. UpdateInternalClientRect (ClientRectangle);
  581. }
  582. protected override void OnSelectedIndexChanged (EventArgs e)
  583. {
  584. base.OnSelectedIndexChanged (e);
  585. if (SelectedIndexChanged != null)
  586. SelectedIndexChanged (this, e);
  587. }
  588. protected override void OnSelectedValueChanged (EventArgs e)
  589. {
  590. base.OnSelectedValueChanged (e);
  591. }
  592. public override void Refresh ()
  593. {
  594. base.Refresh ();
  595. }
  596. protected override void RefreshItem (int index)
  597. {
  598. }
  599. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  600. {
  601. base.SetBoundsCore (x, y, width, height, specified);
  602. }
  603. protected override void SetItemCore (int index, object value)
  604. {
  605. if (index < 0 || index >= Items.Count)
  606. return;
  607. Items[index] = value;
  608. }
  609. protected override void SetItemsCore (IList value)
  610. {
  611. }
  612. public void SetSelected (int index, bool value)
  613. {
  614. if (index < 0 || index >= Items.Count)
  615. throw new ArgumentOutOfRangeException ("Index of out range");
  616. if (SelectionMode == SelectionMode.None)
  617. throw new InvalidOperationException ();
  618. if (value)
  619. SelectItem (index);
  620. else
  621. UnSelectItem (index, true);
  622. }
  623. protected virtual void Sort ()
  624. {
  625. if (Items.Count == 0)
  626. return;
  627. Items.Sort ();
  628. Refresh ();
  629. }
  630. public override string ToString ()
  631. {
  632. return base.ToString () + ", Items Count: " + Items.Count;
  633. }
  634. protected virtual void WmReflectCommand (ref Message m)
  635. {
  636. }
  637. protected override void WndProc (ref Message m)
  638. {
  639. switch ((Msg) m.Msg) {
  640. case Msg.WM_PAINT: {
  641. PaintEventArgs paint_event;
  642. paint_event = XplatUI.PaintEventStart (Handle);
  643. OnPaintLB (paint_event);
  644. XplatUI.PaintEventEnd (Handle);
  645. return;
  646. }
  647. case Msg.WM_ERASEBKGND:
  648. m.Result = (IntPtr) 1;
  649. return;
  650. default:
  651. break;
  652. }
  653. base.WndProc (ref m);
  654. }
  655. #endregion Public Methods
  656. #region Private Methods
  657. internal void CalcClientArea ()
  658. {
  659. listbox_info.textdrawing_rect = listbox_info.client_rect;
  660. listbox_info.textdrawing_rect.Y += ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle);
  661. listbox_info.textdrawing_rect.X += ThemeEngine.Current.DrawListBoxDecorationLeft (BorderStyle);
  662. //BUG: Top and Left decorations
  663. listbox_info.textdrawing_rect.Height -= ThemeEngine.Current.DrawListBoxDecorationBottom (BorderStyle);
  664. listbox_info.textdrawing_rect.Width -= ThemeEngine.Current.DrawListBoxDecorationRight (BorderStyle);
  665. if (listbox_info.show_verticalsb)
  666. listbox_info.textdrawing_rect.Width -= vscrollbar_ctrl.Width;
  667. if (listbox_info.show_horizontalsb)
  668. listbox_info.textdrawing_rect.Height -= hscrollbar_ctrl.Height;
  669. listbox_info.page_size = listbox_info.textdrawing_rect.Height / listbox_info.item_height;
  670. if (listbox_info.page_size == 0) {
  671. listbox_info.page_size = 1;
  672. }
  673. /* Adjust size to visible the maxim number of displayable items */
  674. if (IntegralHeight == true) {
  675. // From MS Docs: The integral height is based on the height of the ListBox, rather than
  676. // the client area height. As a result, when the IntegralHeight property is set true,
  677. // items can still be partially shown if scroll bars are displayed.
  678. int remaining = (listbox_info.client_rect.Height -
  679. ThemeEngine.Current.DrawListBoxDecorationBottom (BorderStyle) -
  680. ThemeEngine.Current.DrawListBoxDecorationBottom (BorderStyle)) %
  681. listbox_info.item_height;
  682. if (remaining > 0) {
  683. listbox_info.client_rect.Height -= remaining;
  684. CalcClientArea ();
  685. RellocateScrollBars ();
  686. Refresh ();
  687. }
  688. }
  689. }
  690. internal void Draw (Rectangle clip)
  691. {
  692. if (LBoxInfo.textdrawing_rect.Contains (clip) == false) {
  693. // IntegralHeight has effect, we also have to paint the unused area
  694. if (ClientRectangle.Height > listbox_info.client_rect.Height) {
  695. Region area = new Region (ClientRectangle);
  696. area.Exclude (listbox_info.client_rect);
  697. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (Parent.BackColor),
  698. area.GetBounds (DeviceContext));
  699. }
  700. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), LBoxInfo.textdrawing_rect);
  701. }
  702. if (Items.Count > 0) {
  703. Rectangle item_rect;
  704. DrawItemState state = DrawItemState.None;
  705. for (int i = LBoxInfo.top_item; i <= LBoxInfo.last_item; i++) {
  706. item_rect = GetItemDisplayRectangle (i, LBoxInfo.top_item);
  707. if (clip.IntersectsWith (item_rect) == false)
  708. continue;
  709. /* Draw item */
  710. state = DrawItemState.None;
  711. if ((Items.GetListBoxItem (i)).Selected) {
  712. state |= DrawItemState.Selected;
  713. }
  714. OnDrawItem (new DrawItemEventArgs (DeviceContext, Font, item_rect,
  715. i, state, ForeColor, BackColor));
  716. }
  717. }
  718. ThemeEngine.Current.DrawListBoxDecorations (DeviceContext, this);
  719. }
  720. // Converts a GetItemRectangle to a one that we can display
  721. internal Rectangle GetItemDisplayRectangle (int index, int first_displayble)
  722. {
  723. Rectangle item_rect;
  724. Rectangle first_item_rect = GetItemRectangle (first_displayble);
  725. item_rect = GetItemRectangle (index);
  726. item_rect.X -= first_item_rect.X;
  727. item_rect.Y -= first_item_rect.Y;
  728. item_rect.Y += ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle);
  729. item_rect.X += ThemeEngine.Current.DrawListBoxDecorationLeft (BorderStyle);
  730. item_rect.Width -= ThemeEngine.Current.DrawListBoxDecorationRight (BorderStyle);
  731. return item_rect;
  732. }
  733. // Value Changed
  734. private void HorizontalScrollEvent (object sender, EventArgs e)
  735. {
  736. LBoxInfo.top_item = listbox_info.page_size * hscrollbar_ctrl.Value;
  737. LBoxInfo.last_item = LastVisibleItem ();
  738. Refresh ();
  739. }
  740. // Only returns visible points. The diference of with IndexFromPoint is that the rectangle
  741. // has screen coordinates
  742. internal int IndexFromPointDisplayRectangle (int x, int y)
  743. {
  744. for (int i = LBoxInfo.top_item; i < LBoxInfo.last_item; i++) {
  745. if (GetItemDisplayRectangle (i, LBoxInfo.top_item).Contains (x, y) == true)
  746. return i;
  747. }
  748. return -1;
  749. }
  750. private int LastVisibleItem ()
  751. {
  752. Rectangle item_rect;
  753. int top_y = LBoxInfo.textdrawing_rect.Y + LBoxInfo.textdrawing_rect.Height;
  754. int i = 0;
  755. if (LBoxInfo.top_item >= Items.Count)
  756. return LBoxInfo.top_item;
  757. for (i = LBoxInfo.top_item; i < Items.Count; i++) {
  758. item_rect = GetItemDisplayRectangle (i, LBoxInfo.top_item);
  759. if (MultiColumn) {
  760. if (item_rect.X > LBoxInfo.textdrawing_rect.Width)
  761. return i - 1;
  762. }
  763. else {
  764. if (IntegralHeight) {
  765. if (item_rect.Y + item_rect.Height > top_y) {
  766. return i - 1;
  767. }
  768. }
  769. else {
  770. if (item_rect.Y + item_rect.Height > top_y)
  771. return i - 1;
  772. }
  773. }
  774. }
  775. return i - 1;
  776. }
  777. private void UpdatedTopItem ()
  778. {
  779. if (multicolumn) {
  780. int col = (LBoxInfo.top_item / LBoxInfo.page_size);
  781. hscrollbar_ctrl.Value = col;
  782. }
  783. else {
  784. if (LBoxInfo.top_item > vscrollbar_ctrl.Maximum)
  785. vscrollbar_ctrl.Value = vscrollbar_ctrl.Maximum;
  786. else
  787. vscrollbar_ctrl.Value = LBoxInfo.top_item;
  788. }
  789. }
  790. private void NavigateItem (ItemNavigation navigation)
  791. {
  792. int highlighted_item = SelectedIndex;
  793. int page_size, columns;
  794. if (multicolumn) {
  795. columns = LBoxInfo.textdrawing_rect.Width / ColumnWidthInternal;
  796. page_size = columns * LBoxInfo.page_size;
  797. if (page_size == 0) {
  798. page_size = LBoxInfo.page_size;
  799. }
  800. } else {
  801. page_size = LBoxInfo.page_size;
  802. }
  803. switch (navigation) {
  804. case ItemNavigation.PreviousColumn: {
  805. if (highlighted_item - LBoxInfo.page_size < 0) {
  806. return;
  807. }
  808. if (highlighted_item - LBoxInfo.page_size < LBoxInfo.top_item) {
  809. LBoxInfo.top_item = highlighted_item - LBoxInfo.page_size;
  810. UpdatedTopItem ();
  811. }
  812. SelectedIndex = highlighted_item - LBoxInfo.page_size;
  813. break;
  814. }
  815. case ItemNavigation.NextColumn: {
  816. if (highlighted_item + LBoxInfo.page_size >= Items.Count) {
  817. return;
  818. }
  819. if (highlighted_item + LBoxInfo.page_size > LBoxInfo.last_item) {
  820. LBoxInfo.top_item = highlighted_item;
  821. UpdatedTopItem ();
  822. }
  823. SelectedIndex = highlighted_item + LBoxInfo.page_size;
  824. break;
  825. }
  826. case ItemNavigation.First: {
  827. LBoxInfo.top_item = 0;
  828. SelectedIndex = 0;
  829. UpdatedTopItem ();
  830. break;
  831. }
  832. case ItemNavigation.Last: {
  833. if (Items.Count < LBoxInfo.page_size) {
  834. LBoxInfo.top_item = 0;
  835. SelectedIndex = Items.Count - 1;
  836. UpdatedTopItem ();
  837. } else {
  838. LBoxInfo.top_item = Items.Count - LBoxInfo.page_size;
  839. SelectedIndex = Items.Count - 1;
  840. UpdatedTopItem ();
  841. }
  842. break;
  843. }
  844. case ItemNavigation.Next: {
  845. if (highlighted_item + 1 < Items.Count) {
  846. if (highlighted_item + 1 > LBoxInfo.last_item) {
  847. LBoxInfo.top_item++;
  848. UpdatedTopItem ();
  849. }
  850. SelectedIndex = highlighted_item + 1;
  851. }
  852. break;
  853. }
  854. case ItemNavigation.Previous: {
  855. if (highlighted_item > 0) {
  856. if (highlighted_item - 1 < LBoxInfo.top_item) {
  857. LBoxInfo.top_item--;
  858. UpdatedTopItem ();
  859. }
  860. SelectedIndex = highlighted_item - 1;
  861. }
  862. break;
  863. }
  864. case ItemNavigation.NextPage: {
  865. if (Items.Count < page_size) {
  866. NavigateItem (ItemNavigation.Last);
  867. return;
  868. }
  869. if (highlighted_item + page_size - 1 >= Items.Count) {
  870. LBoxInfo.top_item = Items.Count - page_size;
  871. UpdatedTopItem ();
  872. SelectedIndex = Items.Count - 1;
  873. }
  874. else {
  875. if (highlighted_item + page_size - 1 > LBoxInfo.last_item) {
  876. LBoxInfo.top_item = highlighted_item;
  877. UpdatedTopItem ();
  878. }
  879. SelectedIndex = highlighted_item + page_size - 1;
  880. }
  881. break;
  882. }
  883. case ItemNavigation.PreviousPage: {
  884. if (highlighted_item - (LBoxInfo.page_size - 1) <= 0) {
  885. LBoxInfo.top_item = 0;
  886. vscrollbar_ctrl.Value = LBoxInfo.top_item;
  887. SelectedIndex = 0;
  888. }
  889. else {
  890. if (highlighted_item - (LBoxInfo.page_size - 1) < LBoxInfo.top_item) {
  891. LBoxInfo.top_item = highlighted_item - (LBoxInfo.page_size - 1);
  892. vscrollbar_ctrl.Value = LBoxInfo.top_item;
  893. }
  894. SelectedIndex = highlighted_item - (LBoxInfo.page_size - 1);
  895. }
  896. break;
  897. }
  898. default:
  899. break;
  900. }
  901. }
  902. private void OnKeyDownLB (object sender, KeyEventArgs e)
  903. {
  904. if (SelectionMode == SelectionMode.None) // No keyboard navigation
  905. return;
  906. switch (e.KeyCode) {
  907. case Keys.Home:
  908. NavigateItem (ItemNavigation.First);
  909. break;
  910. case Keys.End:
  911. NavigateItem (ItemNavigation.Last);
  912. break;
  913. case Keys.Up:
  914. NavigateItem (ItemNavigation.Previous);
  915. break;
  916. case Keys.Down:
  917. NavigateItem (ItemNavigation.Next);
  918. break;
  919. case Keys.PageUp:
  920. NavigateItem (ItemNavigation.PreviousPage);
  921. break;
  922. case Keys.PageDown:
  923. NavigateItem (ItemNavigation.NextPage);
  924. break;
  925. case Keys.Right:
  926. if (multicolumn == true) {
  927. NavigateItem (ItemNavigation.NextColumn);
  928. }
  929. break;
  930. case Keys.Left:
  931. if (multicolumn == true) {
  932. NavigateItem (ItemNavigation.PreviousColumn);
  933. }
  934. break;
  935. default:
  936. break;
  937. }
  938. }
  939. internal virtual void OnMouseDownLB (object sender, MouseEventArgs e)
  940. {
  941. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  942. if (index == -1) return;
  943. switch (SelectionMode) {
  944. case SelectionMode.None: // Do nothing
  945. break;
  946. case SelectionMode.One: {
  947. SelectedIndex = index;
  948. break;
  949. }
  950. case SelectionMode.MultiSimple: {
  951. if (selected_index == -1) {
  952. SelectedIndex = index;
  953. } else {
  954. if ((Items.GetListBoxItem (index)).Selected)
  955. UnSelectItem (index, true);
  956. else {
  957. SelectItem (index);
  958. OnSelectedIndexChanged (new EventArgs ());
  959. }
  960. }
  961. break;
  962. }
  963. default:
  964. break;
  965. }
  966. }
  967. private void OnPaintLB (PaintEventArgs pevent)
  968. {
  969. if (Width <= 0 || Height <= 0 || Visible == false || suspend_ctrlupdate == true)
  970. return;
  971. /* Copies memory drawing buffer to screen*/
  972. Draw (pevent.ClipRectangle);
  973. pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
  974. if (Paint != null)
  975. Paint (this, pevent);
  976. }
  977. internal void RellocateScrollBars ()
  978. {
  979. if (listbox_info.show_verticalsb) {
  980. vscrollbar_ctrl.Size = new Size (vscrollbar_ctrl.Width,
  981. listbox_info.client_rect.Height - ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle) -
  982. ThemeEngine.Current.DrawListBoxDecorationBottom (BorderStyle));
  983. vscrollbar_ctrl.Location = new Point (listbox_info.client_rect.Width - vscrollbar_ctrl.Width
  984. - ThemeEngine.Current.DrawListBoxDecorationRight (BorderStyle),
  985. ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle));
  986. }
  987. if (listbox_info.show_horizontalsb) {
  988. int width;
  989. width = listbox_info.client_rect.Width - (ThemeEngine.Current.DrawListBoxDecorationLeft (BorderStyle) + ThemeEngine.Current.DrawListBoxDecorationRight (BorderStyle));
  990. if (listbox_info.show_verticalsb)
  991. width -= vscrollbar_ctrl.Width;
  992. hscrollbar_ctrl.Size = new Size (width, hscrollbar_ctrl.Height);
  993. hscrollbar_ctrl.Location = new Point (ThemeEngine.Current.DrawListBoxDecorationLeft (BorderStyle),
  994. listbox_info.client_rect.Height - hscrollbar_ctrl.Height
  995. - ThemeEngine.Current.DrawListBoxDecorationTop (BorderStyle));
  996. }
  997. CalcClientArea ();
  998. }
  999. // Add an item in the Selection array and marks it visually as selected
  1000. private void SelectItem (int index)
  1001. {
  1002. if (index == -1)
  1003. return;
  1004. Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  1005. (Items.GetListBoxItem (index)).Selected = true;
  1006. selected_indices.AddIndex (index);
  1007. selected_items.AddObject (Items[index]);
  1008. if (ClientRectangle.Contains (invalidate))
  1009. Invalidate (invalidate);
  1010. }
  1011. // Removes an item in the Selection array and marks it visually as unselected
  1012. private void UnSelectItem (int index, bool remove)
  1013. {
  1014. if (index == -1)
  1015. return;
  1016. Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
  1017. (Items.GetListBoxItem (index)).Selected = false;
  1018. if (remove) {
  1019. selected_indices.RemoveIndex (index);
  1020. selected_items.RemoveObject (Items[index]);
  1021. }
  1022. if (ClientRectangle.Contains (invalidate))
  1023. Invalidate (invalidate);
  1024. }
  1025. private void UpdateFormatString ()
  1026. {
  1027. if (RightToLeft == RightToLeft.No)
  1028. string_format.Alignment = StringAlignment.Near;
  1029. else
  1030. string_format.Alignment = StringAlignment.Far;
  1031. if (UseTabStops)
  1032. string_format.SetTabStops (0, new float [] {(float)(Font.Height * 3.7)});
  1033. }
  1034. // Updates the scrollbar's position with the new items and inside area
  1035. internal virtual void UpdateItemInfo (bool adding, int first, int last)
  1036. {
  1037. if (!IsHandleCreated || suspend_ctrlupdate == true)
  1038. return;
  1039. UpdateShowVerticalScrollBar ();
  1040. if (listbox_info.show_verticalsb && Items.Count > listbox_info.page_size)
  1041. if (vscrollbar_ctrl.Enabled)
  1042. vscrollbar_ctrl.Maximum = Items.Count - listbox_info.page_size;
  1043. if (listbox_info.show_horizontalsb) {
  1044. if (MultiColumn) {
  1045. int fullpage = (listbox_info.page_size * (listbox_info.client_rect.Width / ColumnWidthInternal));
  1046. if (hscrollbar_ctrl.Enabled && listbox_info.page_size > 0)
  1047. hscrollbar_ctrl.Maximum = Math.Max (0, 1 + ((Items.Count - fullpage) / listbox_info.page_size));
  1048. }
  1049. }
  1050. if (MultiColumn == false) {
  1051. /* Calc the longest items for non multicolumn listboxes */
  1052. if ((first == -1 && last == -1) || (adding == false)) {
  1053. SizeF size;
  1054. for (int i = 0; i < Items.Count; i++) {
  1055. size = DeviceContext.MeasureString (Items[i].ToString(), Font);
  1056. if ((int) size.Width > listbox_info.max_itemwidth)
  1057. listbox_info.max_itemwidth = (int) size.Width;
  1058. }
  1059. }
  1060. else {
  1061. if (adding) {
  1062. SizeF size;
  1063. for (int i = first; i < last + 1; i++) {
  1064. size = DeviceContext.MeasureString (Items[i].ToString(), Font);
  1065. if ((int) size.Width > listbox_info.max_itemwidth)
  1066. listbox_info.max_itemwidth = (int) size.Width;
  1067. }
  1068. }
  1069. }
  1070. }
  1071. if (sorted)
  1072. Sort ();
  1073. SelectedItems.ReCreate ();
  1074. SelectedIndices.ReCreate ();
  1075. LBoxInfo.last_item = LastVisibleItem ();
  1076. UpdateShowHorizontalScrollBar ();
  1077. Refresh ();
  1078. }
  1079. private void UpdateInternalClientRect (Rectangle client_rectangle)
  1080. {
  1081. listbox_info.client_rect = client_rectangle;
  1082. UpdateShowHorizontalScrollBar ();
  1083. UpdateShowVerticalScrollBar ();
  1084. RellocateScrollBars ();
  1085. UpdateItemInfo (false, -1, -1);
  1086. }
  1087. /* Determines if the horizontal scrollbar has to be displyed */
  1088. private void UpdateShowHorizontalScrollBar ()
  1089. {
  1090. bool show = false;
  1091. bool enabled = true;
  1092. if (MultiColumn) { /* Horizontal scrollbar is always shown in Multicolum mode */
  1093. /* Is it really need it */
  1094. int page_size = listbox_info.client_rect.Height / listbox_info.item_height;
  1095. int fullpage = (page_size * (listbox_info.textdrawing_rect.Height / ColumnWidthInternal));
  1096. if (Items.Count > fullpage) {
  1097. if (IntegralHeight == false)
  1098. show = true;
  1099. }
  1100. else { /* Acording to MS Documentation ScrollAlwaysVisible only affects Horizontal scrollbars but
  1101. this is not true for MultiColumn listboxes */
  1102. if (ScrollAlwaysVisible == true) {
  1103. enabled = false;
  1104. show = true;
  1105. }
  1106. }
  1107. } else { /* If large item*/
  1108. if (listbox_info.max_itemwidth > listbox_info.client_rect.Width && HorizontalScrollbar) {
  1109. show = true;
  1110. hscrollbar_ctrl.Maximum = listbox_info.max_itemwidth;
  1111. }
  1112. }
  1113. if (hscrollbar_ctrl.Enabled != enabled)
  1114. hscrollbar_ctrl.Enabled = enabled;
  1115. if (listbox_info.show_horizontalsb == show)
  1116. return;
  1117. listbox_info.show_horizontalsb = show;
  1118. hscrollbar_ctrl.Visible = show;
  1119. if (show == true) {
  1120. RellocateScrollBars ();
  1121. }
  1122. CalcClientArea ();
  1123. }
  1124. /* Determines if the vertical scrollbar has to be displyed */
  1125. private void UpdateShowVerticalScrollBar ()
  1126. {
  1127. bool show = false;
  1128. bool enabled = true;
  1129. if (!MultiColumn) { /* Vertical scrollbar is never shown in Multicolum mode */
  1130. if (Items.Count > listbox_info.page_size) {
  1131. show = true;
  1132. }
  1133. else
  1134. if (ScrollAlwaysVisible) {
  1135. show = true;
  1136. enabled = false;
  1137. }
  1138. }
  1139. if (vscrollbar_ctrl.Enabled != enabled)
  1140. vscrollbar_ctrl.Enabled = enabled;
  1141. if (listbox_info.show_verticalsb == show)
  1142. return;
  1143. listbox_info.show_verticalsb = show;
  1144. vscrollbar_ctrl.Visible = show;
  1145. if (show == true) {
  1146. if (vscrollbar_ctrl.Enabled)
  1147. vscrollbar_ctrl.Maximum = Items.Count - listbox_info.page_size;
  1148. RellocateScrollBars ();
  1149. }
  1150. CalcClientArea ();
  1151. }
  1152. // Value Changed
  1153. private void VerticalScrollEvent (object sender, EventArgs e)
  1154. {
  1155. LBoxInfo.top_item = /*listbox_info.page_size + */ vscrollbar_ctrl.Value;
  1156. LBoxInfo.last_item = LastVisibleItem ();
  1157. Refresh ();
  1158. }
  1159. #endregion Private Methods
  1160. /*
  1161. ListBox.ObjectCollection
  1162. */
  1163. public class ObjectCollection : IList, ICollection, IEnumerable
  1164. {
  1165. // Compare objects
  1166. internal class ListObjectComparer : IComparer
  1167. {
  1168. private ListBox owner;
  1169. public ListObjectComparer (ListBox owner)
  1170. {
  1171. this.owner = owner;
  1172. }
  1173. public int Compare (object a, object b)
  1174. {
  1175. string str1 = a.ToString ();
  1176. string str2 = b.ToString ();
  1177. return str1.CompareTo (str2);
  1178. }
  1179. }
  1180. // Compare ListItem
  1181. internal class ListItemComparer : IComparer
  1182. {
  1183. private ListBox owner;
  1184. public ListItemComparer (ListBox owner)
  1185. {
  1186. this.owner = owner;
  1187. }
  1188. public int Compare (object a, object b)
  1189. {
  1190. int index1 = ((ListBox.ListBoxItem) (a)).Index;
  1191. int index2 = ((ListBox.ListBoxItem) (b)).Index;
  1192. string str1 = owner.Items[index1].ToString ();
  1193. string str2 = owner.Items[index2].ToString ();
  1194. return str1.CompareTo (str2);
  1195. }
  1196. }
  1197. private ListBox owner;
  1198. internal ArrayList object_items = new ArrayList ();
  1199. internal ArrayList listbox_items = new ArrayList ();
  1200. public ObjectCollection (ListBox owner)
  1201. {
  1202. this.owner = owner;
  1203. }
  1204. public ObjectCollection (ListBox owner, object[] obj)
  1205. {
  1206. this.owner = owner;
  1207. AddRange (obj);
  1208. }
  1209. public ObjectCollection (ListBox owner, ObjectCollection obj)
  1210. {
  1211. this.owner = owner;
  1212. AddRange (obj);
  1213. }
  1214. #region Public Properties
  1215. public virtual int Count {
  1216. get { return object_items.Count; }
  1217. }
  1218. public virtual bool IsReadOnly {
  1219. get { return false; }
  1220. }
  1221. public virtual object this [int index] {
  1222. get {
  1223. if (index < 0 || index >= Count)
  1224. throw new ArgumentOutOfRangeException ("Index of out range");
  1225. return object_items[index];
  1226. }
  1227. set {
  1228. if (index < 0 || index >= Count)
  1229. throw new ArgumentOutOfRangeException ("Index of out range");
  1230. object_items[index] = value;
  1231. }
  1232. }
  1233. bool ICollection.IsSynchronized {
  1234. get { return false; }
  1235. }
  1236. object ICollection.SyncRoot {
  1237. get { return this; }
  1238. }
  1239. bool IList.IsFixedSize {
  1240. get { return false; }
  1241. }
  1242. #endregion Public Properties
  1243. #region Public Methods
  1244. public int Add (object item)
  1245. {
  1246. int idx;
  1247. idx = AddItem (item);
  1248. owner.UpdateItemInfo (true, idx, idx);
  1249. return idx;
  1250. }
  1251. public void AddRange (object[] items)
  1252. {
  1253. int cnt = Count;
  1254. foreach (object mi in items)
  1255. AddItem (mi);
  1256. owner.UpdateItemInfo (true, cnt, Count);
  1257. }
  1258. public void AddRange (ObjectCollection col)
  1259. {
  1260. int cnt = Count;
  1261. foreach (object mi in col)
  1262. AddItem (mi);
  1263. owner.UpdateItemInfo (true, cnt, Count);
  1264. }
  1265. public virtual void Clear ()
  1266. {
  1267. object_items.Clear ();
  1268. listbox_items.Clear ();
  1269. owner.UpdateItemInfo (false, -1, -1);
  1270. }
  1271. public virtual bool Contains (object obj)
  1272. {
  1273. return object_items.Contains (obj);
  1274. }
  1275. public void CopyTo (object[] dest, int arrayIndex)
  1276. {
  1277. object_items.CopyTo (dest, arrayIndex);
  1278. }
  1279. void ICollection.CopyTo (Array dest, int index)
  1280. {
  1281. object_items.CopyTo (dest, index);
  1282. }
  1283. public virtual IEnumerator GetEnumerator ()
  1284. {
  1285. return object_items.GetEnumerator ();
  1286. }
  1287. int IList.Add (object item)
  1288. {
  1289. return Add (item);
  1290. }
  1291. public virtual int IndexOf (object value)
  1292. {
  1293. return object_items.IndexOf (value);
  1294. }
  1295. public virtual void Insert (int index, object item)
  1296. {
  1297. throw new NotImplementedException ();
  1298. }
  1299. public virtual void Remove (object value)
  1300. {
  1301. RemoveAt (IndexOf (value));
  1302. owner.UpdateItemInfo (false, -1, -1);
  1303. }
  1304. public virtual void RemoveAt (int index)
  1305. {
  1306. if (index < 0 || index >= Count)
  1307. throw new ArgumentOutOfRangeException ("Index of out range");
  1308. object_items.RemoveAt (index);
  1309. listbox_items.RemoveAt (index);
  1310. owner.UpdateItemInfo (false, -1, -1);
  1311. }
  1312. #endregion Public Methods
  1313. #region Private Methods
  1314. private int AddItem (object item)
  1315. {
  1316. int cnt = object_items.Count;
  1317. object_items.Add (item);
  1318. listbox_items.Add (new ListBox.ListBoxItem (cnt));
  1319. return cnt;
  1320. }
  1321. internal ListBox.ListBoxItem GetListBoxItem (int index)
  1322. {
  1323. if (index < 0 || index >= Count)
  1324. throw new ArgumentOutOfRangeException ("Index of out range");
  1325. return (ListBox.ListBoxItem) listbox_items[index];
  1326. }
  1327. internal void SetListBoxItem (ListBox.ListBoxItem item, int index)
  1328. {
  1329. if (index < 0 || index >= Count)
  1330. throw new ArgumentOutOfRangeException ("Index of out range");
  1331. listbox_items[index] = item;
  1332. }
  1333. internal void Sort ()
  1334. {
  1335. /* Keep this order */
  1336. listbox_items.Sort (new ListItemComparer (owner));
  1337. object_items.Sort (new ListObjectComparer (owner));
  1338. for (int i = 0; i < listbox_items.Count; i++) {
  1339. ListBox.ListBoxItem item = GetListBoxItem (i);
  1340. item.Index = i;
  1341. }
  1342. }
  1343. #endregion Private Methods
  1344. }
  1345. /*
  1346. ListBox.SelectedIndexCollection
  1347. */
  1348. public class SelectedIndexCollection : IList, ICollection, IEnumerable
  1349. {
  1350. private ListBox owner;
  1351. private ArrayList indices = new ArrayList ();
  1352. public SelectedIndexCollection (ListBox owner)
  1353. {
  1354. this.owner = owner;
  1355. }
  1356. #region Public Properties
  1357. public virtual int Count {
  1358. get { return indices.Count; }
  1359. }
  1360. public virtual bool IsReadOnly {
  1361. get { return true; }
  1362. }
  1363. public int this [int index] {
  1364. get {
  1365. if (index < 0 || index >= Count)
  1366. throw new ArgumentOutOfRangeException ("Index of out range");
  1367. return (int) indices[index];
  1368. }
  1369. }
  1370. bool ICollection.IsSynchronized {
  1371. get { return true; }
  1372. }
  1373. bool IList.IsFixedSize{
  1374. get { return true; }
  1375. }
  1376. object ICollection.SyncRoot {
  1377. get { return this; }
  1378. }
  1379. #endregion Public Properties
  1380. #region Public Methods
  1381. public bool Contains (int selectedIndex)
  1382. {
  1383. return indices.Contains (selectedIndex);
  1384. }
  1385. public virtual void CopyTo (Array dest, int index)
  1386. {
  1387. indices.CopyTo (dest, index);
  1388. }
  1389. public virtual IEnumerator GetEnumerator ()
  1390. {
  1391. return indices.GetEnumerator ();
  1392. }
  1393. int IList.Add (object obj)
  1394. {
  1395. throw new NotSupportedException ();
  1396. }
  1397. void IList.Clear ()
  1398. {
  1399. throw new NotSupportedException ();
  1400. }
  1401. bool IList.Contains (object selectedIndex)
  1402. {
  1403. return Contains ((int)selectedIndex);
  1404. }
  1405. int IList.IndexOf (object selectedIndex)
  1406. {
  1407. return IndexOf ((int) selectedIndex);
  1408. }
  1409. void IList.Insert (int index, object value)
  1410. {
  1411. throw new NotSupportedException ();
  1412. }
  1413. void IList.Remove (object value)
  1414. {
  1415. throw new NotSupportedException ();
  1416. }
  1417. void IList.RemoveAt (int index)
  1418. {
  1419. throw new NotSupportedException ();
  1420. }
  1421. object IList.this[int index]{
  1422. get {return indices[index]; }
  1423. set {throw new NotImplementedException (); }
  1424. }
  1425. public int IndexOf (int selectedIndex)
  1426. {
  1427. return indices.IndexOf (selectedIndex);
  1428. }
  1429. #endregion Public Methods
  1430. #region Private Methods
  1431. internal void AddIndex (int index)
  1432. {
  1433. indices.Add (index);
  1434. }
  1435. internal void ClearIndices ()
  1436. {
  1437. indices.Clear ();
  1438. }
  1439. internal void RemoveIndex (int index)
  1440. {
  1441. indices.Remove (index);
  1442. }
  1443. internal void ReCreate ()
  1444. {
  1445. indices.Clear ();
  1446. for (int i = 0; i < owner.Items.Count; i++) {
  1447. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  1448. if (item.Selected)
  1449. indices.Add (item.Index);
  1450. }
  1451. }
  1452. #endregion Private Methods
  1453. }
  1454. /*
  1455. SelectedObjectCollection
  1456. */
  1457. public class SelectedObjectCollection : IList, ICollection, IEnumerable
  1458. {
  1459. private ListBox owner;
  1460. private ArrayList object_items = new ArrayList ();
  1461. public SelectedObjectCollection (ListBox owner)
  1462. {
  1463. this.owner = owner;
  1464. }
  1465. #region Public Properties
  1466. public virtual int Count {
  1467. get { return object_items.Count; }
  1468. }
  1469. public virtual bool IsReadOnly {
  1470. get { return true; }
  1471. }
  1472. public virtual object this [int index] {
  1473. get {
  1474. if (index < 0 || index >= Count)
  1475. throw new ArgumentOutOfRangeException ("Index of out range");
  1476. return object_items[index];
  1477. }
  1478. set {throw new NotSupportedException ();}
  1479. }
  1480. bool ICollection.IsSynchronized {
  1481. get { return true; }
  1482. }
  1483. object ICollection.SyncRoot {
  1484. get { return this; }
  1485. }
  1486. bool IList.IsFixedSize {
  1487. get { return true; }
  1488. }
  1489. object IList.this[int index] {
  1490. get { return object_items[index]; }
  1491. set { throw new NotSupportedException (); }
  1492. }
  1493. #endregion Public Properties
  1494. #region Public Methods
  1495. public virtual bool Contains (object selectedObject)
  1496. {
  1497. return object_items.Contains (selectedObject);
  1498. }
  1499. public virtual void CopyTo (Array dest, int index)
  1500. {
  1501. object_items.CopyTo (dest, index);
  1502. }
  1503. int IList.Add (object value)
  1504. {
  1505. throw new NotSupportedException ();
  1506. }
  1507. void IList.Clear ()
  1508. {
  1509. throw new NotSupportedException ();
  1510. }
  1511. bool IList.Contains (object selectedIndex)
  1512. {
  1513. throw new NotImplementedException ();
  1514. }
  1515. int IList.IndexOf (object selectedIndex)
  1516. {
  1517. return IndexOf ((int) selectedIndex);
  1518. }
  1519. void IList.Insert (int index, object value)
  1520. {
  1521. throw new NotSupportedException ();
  1522. }
  1523. void IList.Remove (object value)
  1524. {
  1525. throw new NotSupportedException ();
  1526. }
  1527. void IList.RemoveAt (int index)
  1528. {
  1529. throw new NotSupportedException ();
  1530. }
  1531. public int IndexOf (int selectedIndex)
  1532. {
  1533. return object_items.IndexOf (selectedIndex);
  1534. }
  1535. public virtual IEnumerator GetEnumerator ()
  1536. {
  1537. return object_items.GetEnumerator ();
  1538. }
  1539. #endregion Public Methods
  1540. #region Private Methods
  1541. internal void AddObject (object obj)
  1542. {
  1543. object_items.Add (obj);
  1544. }
  1545. internal void ClearObjects ()
  1546. {
  1547. object_items.Clear ();
  1548. }
  1549. internal void ReCreate ()
  1550. {
  1551. object_items.Clear ();
  1552. for (int i = 0; i < owner.Items.Count; i++) {
  1553. ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
  1554. if (item.Selected)
  1555. object_items.Add (owner.Items[item.Index]);
  1556. }
  1557. }
  1558. internal void RemoveObject (object obj)
  1559. {
  1560. object_items.Remove (obj);
  1561. }
  1562. #endregion Private Methods
  1563. }
  1564. }
  1565. }