ListBox.cs 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061
  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-2006 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. // Mike Kestner <[email protected]>
  25. //
  26. // COMPLETE
  27. using System;
  28. using System.Drawing;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.ComponentModel.Design;
  32. using System.ComponentModel.Design.Serialization;
  33. using System.Globalization;
  34. using System.Reflection;
  35. using System.Runtime.InteropServices;
  36. #if NET_2_0
  37. using System.Collections.Generic;
  38. #endif
  39. namespace System.Windows.Forms
  40. {
  41. [DefaultProperty("Items")]
  42. [DefaultEvent("SelectedIndexChanged")]
  43. [Designer ("System.Windows.Forms.Design.ListBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  44. #if NET_2_0
  45. [DefaultBindingProperty ("SelectedValue")]
  46. [ClassInterface (ClassInterfaceType.AutoDispatch)]
  47. [ComVisible (true)]
  48. #endif
  49. public class ListBox : ListControl
  50. {
  51. public const int DefaultItemHeight = 13;
  52. public const int NoMatches = -1;
  53. internal enum ItemNavigation
  54. {
  55. First,
  56. Last,
  57. Next,
  58. Previous,
  59. NextPage,
  60. PreviousPage,
  61. PreviousColumn,
  62. NextColumn
  63. }
  64. Hashtable item_heights;
  65. private int item_height = -1;
  66. private int column_width = 0;
  67. private int requested_height;
  68. private DrawMode draw_mode = DrawMode.Normal;
  69. private int horizontal_extent = 0;
  70. private bool horizontal_scrollbar = false;
  71. private bool integral_height = true;
  72. private bool multicolumn = false;
  73. private bool scroll_always_visible = false;
  74. private SelectedIndexCollection selected_indices;
  75. private SelectedObjectCollection selected_items;
  76. private SelectionMode selection_mode = SelectionMode.One;
  77. private bool sorted = false;
  78. private bool use_tabstops = true;
  79. private int column_width_internal = 120;
  80. private ImplicitVScrollBar vscrollbar;
  81. private ImplicitHScrollBar hscrollbar;
  82. private int hbar_offset;
  83. private bool suspend_layout;
  84. private bool ctrl_pressed = false;
  85. private bool shift_pressed = false;
  86. private bool explicit_item_height = false;
  87. private int top_index = 0;
  88. private int last_visible_index = 0;
  89. private Rectangle items_area;
  90. private int focused_item = -1;
  91. private ObjectCollection items;
  92. #if NET_2_0
  93. private IntegerCollection custom_tab_offsets;
  94. private Padding padding;
  95. private bool use_custom_tab_offsets;
  96. #endif
  97. public ListBox ()
  98. {
  99. items = CreateItemCollection ();
  100. selected_indices = new SelectedIndexCollection (this);
  101. selected_items = new SelectedObjectCollection (this);
  102. requested_height = bounds.Height;
  103. InternalBorderStyle = BorderStyle.Fixed3D;
  104. BackColor = ThemeEngine.Current.ColorWindow;
  105. /* Vertical scrollbar */
  106. vscrollbar = new ImplicitVScrollBar ();
  107. vscrollbar.Minimum = 0;
  108. vscrollbar.SmallChange = 1;
  109. vscrollbar.LargeChange = 1;
  110. vscrollbar.Maximum = 0;
  111. vscrollbar.ValueChanged += new EventHandler (VerticalScrollEvent);
  112. vscrollbar.Visible = false;
  113. /* Horizontal scrollbar */
  114. hscrollbar = new ImplicitHScrollBar ();
  115. hscrollbar.Minimum = 0;
  116. hscrollbar.SmallChange = 1;
  117. hscrollbar.LargeChange = 1;
  118. hscrollbar.Maximum = 0;
  119. hscrollbar.Visible = false;
  120. hscrollbar.ValueChanged += new EventHandler (HorizontalScrollEvent);
  121. Controls.AddImplicit (vscrollbar);
  122. Controls.AddImplicit (hscrollbar);
  123. /* Events */
  124. MouseDown += new MouseEventHandler (OnMouseDownLB);
  125. MouseMove += new MouseEventHandler (OnMouseMoveLB);
  126. MouseUp += new MouseEventHandler (OnMouseUpLB);
  127. MouseWheel += new MouseEventHandler (OnMouseWheelLB);
  128. KeyDown += new KeyEventHandler (OnKeyDownLB);
  129. KeyUp += new KeyEventHandler (OnKeyUpLB);
  130. GotFocus += new EventHandler (OnGotFocus);
  131. LostFocus += new EventHandler (OnLostFocus);
  132. SetStyle (ControlStyles.UserPaint, false);
  133. #if NET_2_0
  134. custom_tab_offsets = new IntegerCollection (this);
  135. #endif
  136. }
  137. #region Events
  138. static object DrawItemEvent = new object ();
  139. static object MeasureItemEvent = new object ();
  140. static object SelectedIndexChangedEvent = new object ();
  141. [Browsable (false)]
  142. [EditorBrowsable (EditorBrowsableState.Never)]
  143. public new event EventHandler BackgroundImageChanged {
  144. add { base.BackgroundImageChanged += value; }
  145. remove { base.BackgroundImageChanged -= value; }
  146. }
  147. #if NET_2_0
  148. [Browsable (false)]
  149. [EditorBrowsable (EditorBrowsableState.Never)]
  150. public new event EventHandler BackgroundImageLayoutChanged {
  151. add { base.BackgroundImageLayoutChanged += value; }
  152. remove { base.BackgroundImageLayoutChanged -= value; }
  153. }
  154. [Browsable (true)]
  155. [EditorBrowsable (EditorBrowsableState.Always)]
  156. #else
  157. [Browsable (false)]
  158. [EditorBrowsable (EditorBrowsableState.Advanced)]
  159. #endif
  160. public new event EventHandler Click {
  161. add { base.Click += value; }
  162. remove { base.Click -= value; }
  163. }
  164. public event DrawItemEventHandler DrawItem {
  165. add { Events.AddHandler (DrawItemEvent, value); }
  166. remove { Events.RemoveHandler (DrawItemEvent, value); }
  167. }
  168. public event MeasureItemEventHandler MeasureItem {
  169. add { Events.AddHandler (MeasureItemEvent, value); }
  170. remove { Events.RemoveHandler (MeasureItemEvent, value); }
  171. }
  172. #if NET_2_0
  173. [Browsable (true)]
  174. [EditorBrowsable (EditorBrowsableState.Always)]
  175. public new event MouseEventHandler MouseClick {
  176. add { base.MouseClick += value; }
  177. remove { base.MouseClick -= value; }
  178. }
  179. [Browsable (false)]
  180. [EditorBrowsable (EditorBrowsableState.Never)]
  181. public new event EventHandler PaddingChanged {
  182. add { base.PaddingChanged += value; }
  183. remove { base.PaddingChanged -= value; }
  184. }
  185. #endif
  186. [Browsable (false)]
  187. [EditorBrowsable (EditorBrowsableState.Never)]
  188. public new event PaintEventHandler Paint {
  189. add { base.Paint += value; }
  190. remove { base.Paint -= value; }
  191. }
  192. public event EventHandler SelectedIndexChanged {
  193. add { Events.AddHandler (SelectedIndexChangedEvent, value); }
  194. remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
  195. }
  196. [Browsable (false)]
  197. [EditorBrowsable (EditorBrowsableState.Advanced)]
  198. public new event EventHandler TextChanged {
  199. add { base.TextChanged += value; }
  200. remove { base.TextChanged -= value; }
  201. }
  202. #endregion // Events
  203. #region Public Properties
  204. public override Color BackColor {
  205. get { return base.BackColor; }
  206. set {
  207. if (base.BackColor == value)
  208. return;
  209. base.BackColor = value;
  210. base.Refresh (); // Careful. Calling the base method is not the same that calling
  211. } // the overriden one that refresh also all the items
  212. }
  213. [Browsable (false)]
  214. [EditorBrowsable (EditorBrowsableState.Never)]
  215. public override Image BackgroundImage {
  216. get { return base.BackgroundImage; }
  217. set {
  218. base.BackgroundImage = value;
  219. base.Refresh ();
  220. }
  221. }
  222. #if NET_2_0
  223. [Browsable (false)]
  224. [EditorBrowsable (EditorBrowsableState.Never)]
  225. public override ImageLayout BackgroundImageLayout {
  226. get { return base.BackgroundImageLayout; }
  227. set { base.BackgroundImageLayout = value; }
  228. }
  229. #endif
  230. [DefaultValue (BorderStyle.Fixed3D)]
  231. [DispId(-504)]
  232. public BorderStyle BorderStyle {
  233. get { return InternalBorderStyle; }
  234. set {
  235. InternalBorderStyle = value;
  236. UpdateListBoxBounds ();
  237. }
  238. }
  239. [DefaultValue (0)]
  240. [Localizable (true)]
  241. public int ColumnWidth {
  242. get { return column_width; }
  243. set {
  244. if (value < 0)
  245. throw new ArgumentException ("A value less than zero is assigned to the property.");
  246. column_width = value;
  247. if (value == 0)
  248. ColumnWidthInternal = 120;
  249. else
  250. ColumnWidthInternal = value;
  251. base.Refresh ();
  252. }
  253. }
  254. protected override CreateParams CreateParams {
  255. get { return base.CreateParams;}
  256. }
  257. #if NET_2_0
  258. [Browsable (false)]
  259. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  260. public IntegerCollection CustomTabOffsets {
  261. get { return custom_tab_offsets; }
  262. }
  263. #endif
  264. protected override Size DefaultSize {
  265. get { return new Size (120, 96); }
  266. }
  267. [RefreshProperties(RefreshProperties.Repaint)]
  268. [DefaultValue (DrawMode.Normal)]
  269. public virtual DrawMode DrawMode {
  270. get { return draw_mode; }
  271. set {
  272. if (!Enum.IsDefined (typeof (DrawMode), value))
  273. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
  274. if (value == DrawMode.OwnerDrawVariable && multicolumn == true)
  275. throw new ArgumentException ("Cannot have variable height and multicolumn");
  276. if (draw_mode == value)
  277. return;
  278. draw_mode = value;
  279. if (draw_mode == DrawMode.OwnerDrawVariable)
  280. item_heights = new Hashtable ();
  281. else
  282. item_heights = null;
  283. if (Parent != null)
  284. Parent.PerformLayout (this, "DrawMode");
  285. base.Refresh ();
  286. }
  287. }
  288. #if NET_2_0
  289. public override Font Font {
  290. get { return base.Font; }
  291. set { base.Font = value; }
  292. }
  293. #endif
  294. public override Color ForeColor {
  295. get { return base.ForeColor; }
  296. set {
  297. if (base.ForeColor == value)
  298. return;
  299. base.ForeColor = value;
  300. base.Refresh ();
  301. }
  302. }
  303. [DefaultValue (0)]
  304. [Localizable (true)]
  305. public int HorizontalExtent {
  306. get { return horizontal_extent; }
  307. set {
  308. if (horizontal_extent == value)
  309. return;
  310. horizontal_extent = value;
  311. base.Refresh ();
  312. }
  313. }
  314. [DefaultValue (false)]
  315. [Localizable (true)]
  316. public bool HorizontalScrollbar {
  317. get { return horizontal_scrollbar; }
  318. set {
  319. if (horizontal_scrollbar == value)
  320. return;
  321. horizontal_scrollbar = value;
  322. UpdateScrollBars ();
  323. base.Refresh ();
  324. }
  325. }
  326. [DefaultValue (true)]
  327. [Localizable (true)]
  328. [RefreshProperties(RefreshProperties.Repaint)]
  329. public bool IntegralHeight {
  330. get { return integral_height; }
  331. set {
  332. if (integral_height == value)
  333. return;
  334. integral_height = value;
  335. UpdateListBoxBounds ();
  336. }
  337. }
  338. [DefaultValue (13)]
  339. [Localizable (true)]
  340. [RefreshProperties(RefreshProperties.Repaint)]
  341. public virtual int ItemHeight {
  342. get {
  343. if (item_height == -1) {
  344. SizeF sz = TextRenderer.MeasureString ("The quick brown Fox", Font);
  345. item_height = (int) sz.Height;
  346. }
  347. return item_height;
  348. }
  349. set {
  350. if (value > 255)
  351. throw new ArgumentOutOfRangeException ("The ItemHeight property was set beyond 255 pixels");
  352. explicit_item_height = true;
  353. if (item_height == value)
  354. return;
  355. item_height = value;
  356. if (IntegralHeight)
  357. UpdateListBoxBounds ();
  358. LayoutListBox ();
  359. }
  360. }
  361. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  362. [Localizable (true)]
  363. [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  364. #if NET_2_0
  365. [MergableProperty (false)]
  366. #endif
  367. public ObjectCollection Items {
  368. get { return items; }
  369. }
  370. [DefaultValue (false)]
  371. public bool MultiColumn {
  372. get { return multicolumn; }
  373. set {
  374. if (multicolumn == value)
  375. return;
  376. if (value == true && DrawMode == DrawMode.OwnerDrawVariable)
  377. throw new ArgumentException ("A multicolumn ListBox cannot have a variable-sized height.");
  378. multicolumn = value;
  379. LayoutListBox ();
  380. Invalidate ();
  381. }
  382. }
  383. #if NET_2_0
  384. [Browsable (false)]
  385. [EditorBrowsable (EditorBrowsableState.Never)]
  386. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  387. public new Padding Padding {
  388. get { return padding; }
  389. set { padding = value; }
  390. }
  391. #endif
  392. [Browsable (false)]
  393. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  394. [EditorBrowsable (EditorBrowsableState.Advanced)]
  395. public int PreferredHeight {
  396. get {
  397. int itemsHeight = 0;
  398. if (draw_mode == DrawMode.Normal)
  399. itemsHeight = FontHeight * items.Count;
  400. else if (draw_mode == DrawMode.OwnerDrawFixed)
  401. itemsHeight = ItemHeight * items.Count;
  402. else if (draw_mode == DrawMode.OwnerDrawVariable) {
  403. for (int i = 0; i < items.Count; i++)
  404. itemsHeight += (int) item_heights [Items [i]];
  405. }
  406. return itemsHeight;
  407. }
  408. }
  409. public override RightToLeft RightToLeft {
  410. get { return base.RightToLeft; }
  411. set {
  412. base.RightToLeft = value;
  413. if (base.RightToLeft == RightToLeft.Yes)
  414. StringFormat.Alignment = StringAlignment.Far;
  415. else
  416. StringFormat.Alignment = StringAlignment.Near;
  417. base.Refresh ();
  418. }
  419. }
  420. // Only affects the Vertical ScrollBar
  421. [DefaultValue (false)]
  422. [Localizable (true)]
  423. public bool ScrollAlwaysVisible {
  424. get { return scroll_always_visible; }
  425. set {
  426. if (scroll_always_visible == value)
  427. return;
  428. scroll_always_visible = value;
  429. UpdateScrollBars ();
  430. }
  431. }
  432. [Bindable(true)]
  433. [Browsable (false)]
  434. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  435. public override int SelectedIndex {
  436. get {
  437. if (selected_indices == null)
  438. return -1;
  439. return selected_indices.Count > 0 ? selected_indices [0] : -1;
  440. }
  441. set {
  442. if (value < -1 || value >= Items.Count)
  443. throw new ArgumentOutOfRangeException ("Index of out range");
  444. if (SelectionMode == SelectionMode.None)
  445. throw new ArgumentException ("cannot call this method if SelectionMode is SelectionMode.None");
  446. if (value == -1)
  447. selected_indices.Clear ();
  448. else
  449. selected_indices.Add (value);
  450. }
  451. }
  452. [Browsable (false)]
  453. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  454. public SelectedIndexCollection SelectedIndices {
  455. get { return selected_indices; }
  456. }
  457. [Bindable(true)]
  458. [Browsable (false)]
  459. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  460. public object SelectedItem {
  461. get {
  462. if (SelectedItems.Count > 0)
  463. return SelectedItems[0];
  464. else
  465. return null;
  466. }
  467. set {
  468. if (value != null && !Items.Contains (value))
  469. return; // FIXME: this is probably an exception
  470. SelectedIndex = value == null ? - 1 : Items.IndexOf (value);
  471. }
  472. }
  473. [Browsable (false)]
  474. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  475. public SelectedObjectCollection SelectedItems {
  476. get {return selected_items;}
  477. }
  478. [DefaultValue (SelectionMode.One)]
  479. public virtual SelectionMode SelectionMode {
  480. get { return selection_mode; }
  481. set {
  482. if (!Enum.IsDefined (typeof (SelectionMode), value))
  483. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for SelectionMode", value));
  484. if (selection_mode == value)
  485. return;
  486. selection_mode = value;
  487. switch (selection_mode) {
  488. case SelectionMode.None:
  489. SelectedIndices.Clear ();
  490. break;
  491. case SelectionMode.One:
  492. // FIXME: Probably this can be improved
  493. ArrayList old_selection = (ArrayList) SelectedIndices.List.Clone ();
  494. for (int i = 1; i < old_selection.Count; i++)
  495. SelectedIndices.Remove ((int)old_selection [i]);
  496. break;
  497. default:
  498. break;
  499. }
  500. }
  501. }
  502. [DefaultValue (false)]
  503. public bool Sorted {
  504. get { return sorted; }
  505. set {
  506. if (sorted == value)
  507. return;
  508. sorted = value;
  509. if (sorted)
  510. Sort ();
  511. }
  512. }
  513. [Bindable (false)]
  514. [Browsable (false)]
  515. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  516. [EditorBrowsable (EditorBrowsableState.Advanced)]
  517. public override string Text {
  518. get {
  519. if (SelectionMode != SelectionMode.None && SelectedIndex != -1)
  520. return GetItemText (SelectedItem);
  521. return base.Text;
  522. }
  523. set {
  524. base.Text = value;
  525. if (SelectionMode == SelectionMode.None)
  526. return;
  527. int index;
  528. index = FindStringExact (value);
  529. if (index == -1)
  530. return;
  531. SelectedIndex = index;
  532. }
  533. }
  534. [Browsable (false)]
  535. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  536. public int TopIndex {
  537. get { return top_index; }
  538. set {
  539. if (value == top_index)
  540. return;
  541. if (value < 0 || value >= Items.Count)
  542. return;
  543. int page_size = (items_area.Height / ItemHeight);
  544. if (Items.Count < page_size)
  545. value = 0;
  546. else if (!multicolumn)
  547. top_index = Math.Min (value, Items.Count - page_size);
  548. else
  549. top_index = value;
  550. UpdateTopItem ();
  551. base.Refresh ();
  552. }
  553. }
  554. #if NET_2_0
  555. [Browsable (false)]
  556. [DefaultValue (false)]
  557. public bool UseCustomTabOffsets {
  558. get { return use_custom_tab_offsets; }
  559. set {
  560. if (use_custom_tab_offsets != value) {
  561. use_custom_tab_offsets = value;
  562. CalculateTabStops ();
  563. }
  564. }
  565. }
  566. #endif
  567. [DefaultValue (true)]
  568. public bool UseTabStops {
  569. get { return use_tabstops; }
  570. set {
  571. if (use_tabstops == value)
  572. return;
  573. use_tabstops = value;
  574. CalculateTabStops ();
  575. }
  576. }
  577. #if NET_2_0
  578. protected override bool AllowSelection {
  579. get {
  580. return SelectionMode != SelectionMode.None;
  581. }
  582. }
  583. #endif
  584. #endregion Public Properties
  585. #region Private Properties
  586. private int ColumnWidthInternal {
  587. get { return column_width_internal; }
  588. set { column_width_internal = value; }
  589. }
  590. private int row_count = 1;
  591. private int RowCount {
  592. get {
  593. return MultiColumn ? row_count : Items.Count;
  594. }
  595. }
  596. #endregion Private Properties
  597. #region UIA Framework Properties
  598. internal ScrollBar UIAHScrollBar {
  599. get { return hscrollbar; }
  600. }
  601. internal ScrollBar UIAVScrollBar {
  602. get { return vscrollbar; }
  603. }
  604. #endregion UIA Framework Properties
  605. #region Public Methods
  606. #if NET_2_0
  607. [Obsolete ("this method has been deprecated")]
  608. #endif
  609. protected virtual void AddItemsCore (object[] value)
  610. {
  611. Items.AddRange (value);
  612. }
  613. public void BeginUpdate ()
  614. {
  615. suspend_layout = true;
  616. }
  617. public void ClearSelected ()
  618. {
  619. selected_indices.Clear ();
  620. }
  621. protected virtual ObjectCollection CreateItemCollection ()
  622. {
  623. return new ObjectCollection (this);
  624. }
  625. public void EndUpdate ()
  626. {
  627. suspend_layout = false;
  628. LayoutListBox ();
  629. base.Refresh ();
  630. }
  631. public int FindString (String s)
  632. {
  633. return FindString (s, -1);
  634. }
  635. public int FindString (string s, int startIndex)
  636. {
  637. if (Items.Count == 0)
  638. return -1; // No exception throwing if empty
  639. if (startIndex < -1 || startIndex >= Items.Count)
  640. throw new ArgumentOutOfRangeException ("Index of out range");
  641. startIndex = (startIndex == Items.Count - 1) ? 0 : startIndex + 1;
  642. int i = startIndex;
  643. while (true) {
  644. string text = GetItemText (Items [i]);
  645. if (CultureInfo.CurrentCulture.CompareInfo.IsPrefix (text, s,
  646. CompareOptions.IgnoreCase))
  647. return i;
  648. i = (i == Items.Count - 1) ? 0 : i + 1;
  649. if (i == startIndex)
  650. break;
  651. }
  652. return NoMatches;
  653. }
  654. public int FindStringExact (string s)
  655. {
  656. return FindStringExact (s, -1);
  657. }
  658. public int FindStringExact (string s, int startIndex)
  659. {
  660. if (Items.Count == 0)
  661. return -1; // No exception throwing if empty
  662. if (startIndex < -1 || startIndex >= Items.Count)
  663. throw new ArgumentOutOfRangeException ("Index of out range");
  664. startIndex = (startIndex + 1 == Items.Count) ? 0 : startIndex + 1;
  665. int i = startIndex;
  666. while (true) {
  667. if (String.Compare (GetItemText (Items[i]), s, true) == 0)
  668. return i;
  669. i = (i + 1 == Items.Count) ? 0 : i + 1;
  670. if (i == startIndex)
  671. break;
  672. }
  673. return NoMatches;
  674. }
  675. public int GetItemHeight (int index)
  676. {
  677. if (index < 0 || index >= Items.Count)
  678. throw new ArgumentOutOfRangeException ("Index of out range");
  679. if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated == true) {
  680. object o = Items [index];
  681. if (item_heights.Contains (o))
  682. return (int) item_heights [o];
  683. MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
  684. OnMeasureItem (args);
  685. item_heights [o] = args.ItemHeight;
  686. return args.ItemHeight;
  687. }
  688. return ItemHeight;
  689. }
  690. public Rectangle GetItemRectangle (int index)
  691. {
  692. if (index < 0 || index >= Items.Count)
  693. throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
  694. Rectangle rect = new Rectangle ();
  695. if (MultiColumn) {
  696. int col = index / RowCount;
  697. rect.Y = ((index - top_index) % RowCount) * ItemHeight;
  698. rect.X = col * ColumnWidthInternal;
  699. rect.Height = ItemHeight;
  700. rect.Width = ColumnWidthInternal;
  701. } else {
  702. rect.X = 0;
  703. rect.Height = GetItemHeight (index);
  704. rect.Width = items_area.Width;
  705. if (DrawMode == DrawMode.OwnerDrawVariable) {
  706. rect.Y = 0;
  707. if (index >= top_index) {
  708. for (int i = top_index; i < index; i++) {
  709. rect.Y += GetItemHeight (i);
  710. }
  711. } else {
  712. for (int i = index; i < top_index; i++) {
  713. rect.Y -= GetItemHeight (i);
  714. }
  715. }
  716. } else {
  717. rect.Y = ItemHeight * (index - top_index);
  718. }
  719. }
  720. if (this is CheckedListBox)
  721. rect.Width += 15;
  722. return rect;
  723. }
  724. #if NET_2_0
  725. [EditorBrowsable (EditorBrowsableState.Advanced)]
  726. protected override Rectangle GetScaledBounds (Rectangle bounds, SizeF factor, BoundsSpecified specified)
  727. {
  728. bounds.Height = requested_height;
  729. return base.GetScaledBounds (bounds, factor, specified);
  730. }
  731. #endif
  732. public bool GetSelected (int index)
  733. {
  734. if (index < 0 || index >= Items.Count)
  735. throw new ArgumentOutOfRangeException ("Index of out range");
  736. return SelectedIndices.Contains (index);
  737. }
  738. public int IndexFromPoint (Point p)
  739. {
  740. return IndexFromPoint (p.X, p.Y);
  741. }
  742. // Only returns visible points
  743. public int IndexFromPoint (int x, int y)
  744. {
  745. if (Items.Count == 0) {
  746. return -1;
  747. }
  748. for (int i = top_index; i <= last_visible_index; i++) {
  749. if (GetItemRectangle (i).Contains (x,y) == true)
  750. return i;
  751. }
  752. return -1;
  753. }
  754. protected override void OnChangeUICues (UICuesEventArgs e)
  755. {
  756. base.OnChangeUICues (e);
  757. }
  758. protected override void OnDataSourceChanged (EventArgs e)
  759. {
  760. base.OnDataSourceChanged (e);
  761. BindDataItems ();
  762. if (DataSource == null || DataManager == null) {
  763. SelectedIndex = -1;
  764. } else {
  765. SelectedIndex = DataManager.Position;
  766. }
  767. }
  768. protected override void OnDisplayMemberChanged (EventArgs e)
  769. {
  770. base.OnDisplayMemberChanged (e);
  771. if (DataManager == null || !IsHandleCreated)
  772. return;
  773. BindDataItems ();
  774. base.Refresh ();
  775. }
  776. protected virtual void OnDrawItem (DrawItemEventArgs e)
  777. {
  778. switch (DrawMode) {
  779. case DrawMode.OwnerDrawFixed:
  780. case DrawMode.OwnerDrawVariable:
  781. DrawItemEventHandler eh = (DrawItemEventHandler)(Events [DrawItemEvent]);
  782. if (eh != null)
  783. eh (this, e);
  784. break;
  785. default:
  786. ThemeEngine.Current.DrawListBoxItem (this, e);
  787. break;
  788. }
  789. }
  790. protected override void OnFontChanged (EventArgs e)
  791. {
  792. base.OnFontChanged (e);
  793. if (use_tabstops)
  794. StringFormat.SetTabStops (0, new float [] {(float)(Font.Height * 3.7)});
  795. if (explicit_item_height) {
  796. base.Refresh ();
  797. } else {
  798. SizeF sz = TextRenderer.MeasureString ("The quick brown Fox", Font);
  799. item_height = (int) sz.Height;
  800. if (IntegralHeight)
  801. UpdateListBoxBounds ();
  802. LayoutListBox ();
  803. }
  804. }
  805. protected override void OnHandleCreated (EventArgs e)
  806. {
  807. base.OnHandleCreated (e);
  808. if (IntegralHeight)
  809. UpdateListBoxBounds ();
  810. LayoutListBox ();
  811. EnsureVisible (focused_item);
  812. }
  813. protected override void OnHandleDestroyed (EventArgs e)
  814. {
  815. base.OnHandleDestroyed (e);
  816. }
  817. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  818. {
  819. if (draw_mode != DrawMode.OwnerDrawVariable)
  820. return;
  821. MeasureItemEventHandler eh = (MeasureItemEventHandler)(Events [MeasureItemEvent]);
  822. if (eh != null)
  823. eh (this, e);
  824. }
  825. protected override void OnParentChanged (EventArgs e)
  826. {
  827. base.OnParentChanged (e);
  828. }
  829. protected override void OnResize (EventArgs e)
  830. {
  831. base.OnResize (e);
  832. if (canvas_size.IsEmpty || MultiColumn)
  833. LayoutListBox ();
  834. Invalidate ();
  835. }
  836. protected override void OnSelectedIndexChanged (EventArgs e)
  837. {
  838. base.OnSelectedIndexChanged (e);
  839. EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
  840. if (eh != null)
  841. eh (this, e);
  842. }
  843. protected override void OnSelectedValueChanged (EventArgs e)
  844. {
  845. base.OnSelectedValueChanged (e);
  846. }
  847. public override void Refresh ()
  848. {
  849. if (draw_mode == DrawMode.OwnerDrawVariable)
  850. item_heights.Clear ();
  851. base.Refresh ();
  852. }
  853. protected override void RefreshItem (int index)
  854. {
  855. if (index < 0 || index >= Items.Count)
  856. throw new ArgumentOutOfRangeException ("Index of out range");
  857. if (draw_mode == DrawMode.OwnerDrawVariable)
  858. item_heights.Remove (Items [index]);
  859. }
  860. #if NET_2_0
  861. protected override void RefreshItems ()
  862. {
  863. for (int i = 0; i < Items.Count; i++) {
  864. RefreshItem (i);
  865. }
  866. }
  867. public override void ResetBackColor ()
  868. {
  869. base.ResetBackColor ();
  870. }
  871. public override void ResetForeColor ()
  872. {
  873. base.ResetForeColor ();
  874. }
  875. protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
  876. {
  877. base.ScaleControl (factor, specified);
  878. }
  879. #endif
  880. private int SnapHeightToIntegral (int height)
  881. {
  882. int border;
  883. switch (border_style) {
  884. case BorderStyle.Fixed3D:
  885. border = ThemeEngine.Current.Border3DSize.Height;
  886. break;
  887. case BorderStyle.FixedSingle:
  888. border = ThemeEngine.Current.BorderSize.Height;
  889. break;
  890. case BorderStyle.None:
  891. default:
  892. border = 0;
  893. break;
  894. }
  895. height -= (2 * border);
  896. height -= height % ItemHeight;
  897. height += (2 * border);
  898. return height;
  899. }
  900. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  901. {
  902. if ((specified & BoundsSpecified.Height) == BoundsSpecified.Height)
  903. requested_height = height;
  904. if (IntegralHeight && IsHandleCreated)
  905. height = SnapHeightToIntegral (height);
  906. base.SetBoundsCore (x, y, width, height, specified);
  907. UpdateScrollBars ();
  908. last_visible_index = LastVisibleItem ();
  909. }
  910. protected override void SetItemCore (int index, object value)
  911. {
  912. if (index < 0 || index >= Items.Count)
  913. return;
  914. Items[index] = value;
  915. }
  916. protected override void SetItemsCore (IList value)
  917. {
  918. BeginUpdate ();
  919. try {
  920. Items.Clear ();
  921. Items.AddItems (value);
  922. } finally {
  923. EndUpdate ();
  924. }
  925. }
  926. public void SetSelected (int index, bool value)
  927. {
  928. if (index < 0 || index >= Items.Count)
  929. throw new ArgumentOutOfRangeException ("Index of out range");
  930. if (SelectionMode == SelectionMode.None)
  931. throw new InvalidOperationException ();
  932. if (value)
  933. SelectedIndices.Add (index);
  934. else
  935. SelectedIndices.Remove (index);
  936. }
  937. protected virtual void Sort ()
  938. {
  939. Sort (true);
  940. }
  941. //
  942. // Sometimes we could need to Sort, and request a Refresh
  943. // in a different place, to not have the painting done twice
  944. //
  945. void Sort (bool paint)
  946. {
  947. if (Items.Count == 0)
  948. return;
  949. Items.Sort ();
  950. if (paint)
  951. base.Refresh ();
  952. }
  953. public override string ToString ()
  954. {
  955. return base.ToString ();
  956. }
  957. protected virtual void WmReflectCommand (ref Message m)
  958. {
  959. }
  960. protected override void WndProc (ref Message m)
  961. {
  962. base.WndProc (ref m);
  963. }
  964. #endregion Public Methods
  965. #region Private Methods
  966. private void CalculateTabStops ()
  967. {
  968. if (use_tabstops) {
  969. #if NET_2_0
  970. if (use_custom_tab_offsets) {
  971. float[] f = new float[custom_tab_offsets.Count];
  972. custom_tab_offsets.CopyTo (f, 0);
  973. StringFormat.SetTabStops (0, f);
  974. }
  975. else
  976. #endif
  977. StringFormat.SetTabStops (0, new float[] { (float)(Font.Height * 3.7) });
  978. } else
  979. StringFormat.SetTabStops (0, new float[0]);
  980. this.Invalidate ();
  981. }
  982. private Size canvas_size;
  983. private void LayoutListBox ()
  984. {
  985. if (!IsHandleCreated || suspend_layout)
  986. return;
  987. if (MultiColumn)
  988. LayoutMultiColumn ();
  989. else
  990. LayoutSingleColumn ();
  991. last_visible_index = LastVisibleItem ();
  992. UpdateScrollBars ();
  993. }
  994. private void LayoutSingleColumn ()
  995. {
  996. int height, width;
  997. switch (DrawMode) {
  998. case DrawMode.OwnerDrawVariable:
  999. height = 0;
  1000. width = HorizontalExtent;
  1001. for (int i = 0; i < Items.Count; i++) {
  1002. height += GetItemHeight (i);
  1003. }
  1004. break;
  1005. case DrawMode.OwnerDrawFixed:
  1006. height = Items.Count * ItemHeight;
  1007. width = HorizontalExtent;
  1008. break;
  1009. case DrawMode.Normal:
  1010. default:
  1011. height = Items.Count * ItemHeight;
  1012. width = 0;
  1013. for (int i = 0; i < Items.Count; i++) {
  1014. SizeF sz = TextRenderer.MeasureString (GetItemText (Items[i]), Font);
  1015. int t = (int)sz.Width;
  1016. if (this is CheckedListBox)
  1017. t += 15;
  1018. if (t > width)
  1019. width = t;
  1020. }
  1021. break;
  1022. }
  1023. canvas_size = new Size (width, height);
  1024. }
  1025. private void LayoutMultiColumn ()
  1026. {
  1027. int usable_height = ClientRectangle.Height - (ScrollAlwaysVisible ? hscrollbar.Height : 0);
  1028. row_count = Math.Max (1, usable_height / ItemHeight);
  1029. int cols = (int) Math.Ceiling ((float)Items.Count / (float) row_count);
  1030. Size sz = new Size (cols * ColumnWidthInternal, row_count * ItemHeight);
  1031. if (!ScrollAlwaysVisible && sz.Width > ClientRectangle.Width && row_count > 1) {
  1032. usable_height = ClientRectangle.Height - hscrollbar.Height;
  1033. row_count = Math.Max (1, usable_height / ItemHeight);
  1034. cols = (int) Math.Ceiling ((float)Items.Count / (float) row_count);
  1035. sz = new Size (cols * ColumnWidthInternal, row_count * ItemHeight);
  1036. }
  1037. canvas_size = sz;
  1038. }
  1039. internal void Draw (Rectangle clip, Graphics dc)
  1040. {
  1041. Theme theme = ThemeEngine.Current;
  1042. if (hscrollbar.Visible && vscrollbar.Visible) {
  1043. // Paint the dead space in the bottom right corner
  1044. Rectangle rect = new Rectangle (hscrollbar.Right, vscrollbar.Bottom, vscrollbar.Width, hscrollbar.Height);
  1045. if (rect.IntersectsWith (clip))
  1046. dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), rect);
  1047. }
  1048. dc.FillRectangle (theme.ResPool.GetSolidBrush (BackColor), items_area);
  1049. if (Items.Count == 0)
  1050. return;
  1051. for (int i = top_index; i <= last_visible_index; i++) {
  1052. Rectangle rect = GetItemDisplayRectangle (i, top_index);
  1053. if (!clip.IntersectsWith (rect))
  1054. continue;
  1055. DrawItemState state = DrawItemState.None;
  1056. if (SelectedIndices.Contains (i))
  1057. state |= DrawItemState.Selected;
  1058. if (has_focus && FocusedItem == i)
  1059. state |= DrawItemState.Focus;
  1060. if (MultiColumn == false && hscrollbar != null && hscrollbar.Visible) {
  1061. rect.X -= hscrollbar.Value;
  1062. rect.Width += hscrollbar.Value;
  1063. }
  1064. OnDrawItem (new DrawItemEventArgs (dc, Font, rect, i, state, ForeColor, BackColor));
  1065. }
  1066. }
  1067. // Converts a GetItemRectangle to a one that we can display
  1068. internal Rectangle GetItemDisplayRectangle (int index, int first_displayble)
  1069. {
  1070. Rectangle item_rect;
  1071. Rectangle first_item_rect = GetItemRectangle (first_displayble);
  1072. item_rect = GetItemRectangle (index);
  1073. item_rect.X -= first_item_rect.X;
  1074. item_rect.Y -= first_item_rect.Y;
  1075. // Subtract the checkboxes from the width
  1076. if (this is CheckedListBox)
  1077. item_rect.Width -= 14;
  1078. return item_rect;
  1079. }
  1080. // Value Changed
  1081. private void HorizontalScrollEvent (object sender, EventArgs e)
  1082. {
  1083. if (multicolumn) {
  1084. int top_item = top_index;
  1085. int last_item = last_visible_index;
  1086. top_index = RowCount * hscrollbar.Value;
  1087. last_visible_index = LastVisibleItem ();
  1088. if (top_item != top_index || last_item != last_visible_index)
  1089. Invalidate (items_area);
  1090. }
  1091. else {
  1092. int old_offset = hbar_offset;
  1093. hbar_offset = hscrollbar.Value;
  1094. if (hbar_offset < 0)
  1095. hbar_offset = 0;
  1096. if (IsHandleCreated)
  1097. XplatUI.ScrollWindow (Handle, items_area, old_offset - hbar_offset, 0, false);
  1098. }
  1099. }
  1100. // Only returns visible points. The diference of with IndexFromPoint is that the rectangle
  1101. // has screen coordinates
  1102. private int IndexAtClientPoint (int x, int y)
  1103. {
  1104. if (Items.Count == 0)
  1105. return -1;
  1106. if (x < 0)
  1107. x = 0;
  1108. else if (x > ClientRectangle.Right)
  1109. x = ClientRectangle.Right;
  1110. if (y < 0)
  1111. y = 0;
  1112. else if (y > ClientRectangle.Bottom)
  1113. y = ClientRectangle.Bottom;
  1114. for (int i = top_index; i <= last_visible_index; i++)
  1115. if (GetItemDisplayRectangle (i, top_index).Contains (x, y))
  1116. return i;
  1117. return -1;
  1118. }
  1119. internal override bool IsInputCharInternal (char charCode)
  1120. {
  1121. return true;
  1122. }
  1123. private int LastVisibleItem ()
  1124. {
  1125. Rectangle item_rect;
  1126. int top_y = items_area.Y + items_area.Height;
  1127. int i = 0;
  1128. if (top_index >= Items.Count)
  1129. return top_index;
  1130. for (i = top_index; i < Items.Count; i++) {
  1131. item_rect = GetItemDisplayRectangle (i, top_index);
  1132. if (MultiColumn) {
  1133. if (item_rect.X > items_area.Width)
  1134. return i - 1;
  1135. } else {
  1136. if (item_rect.Y + item_rect.Height > top_y)
  1137. return i;
  1138. }
  1139. }
  1140. return i - 1;
  1141. }
  1142. private void UpdateTopItem ()
  1143. {
  1144. if (MultiColumn) {
  1145. int col = top_index / RowCount;
  1146. if (col > hscrollbar.Maximum)
  1147. hscrollbar.Value = hscrollbar.Maximum;
  1148. else
  1149. hscrollbar.Value = col;
  1150. } else {
  1151. if (top_index > vscrollbar.Maximum)
  1152. vscrollbar.Value = vscrollbar.Maximum;
  1153. else
  1154. vscrollbar.Value = top_index;
  1155. Scroll (vscrollbar, vscrollbar.Value - top_index);
  1156. }
  1157. }
  1158. // Navigates to the indicated item and returns the new item
  1159. private int NavigateItemVisually (ItemNavigation navigation)
  1160. {
  1161. int page_size, columns, selected_index = -1;
  1162. if (multicolumn) {
  1163. columns = items_area.Width / ColumnWidthInternal;
  1164. page_size = columns * RowCount;
  1165. if (page_size == 0) {
  1166. page_size = RowCount;
  1167. }
  1168. } else {
  1169. page_size = items_area.Height / ItemHeight;
  1170. }
  1171. switch (navigation) {
  1172. case ItemNavigation.PreviousColumn: {
  1173. if (SelectedIndex - RowCount < 0) {
  1174. return -1;
  1175. }
  1176. if (SelectedIndex - RowCount < top_index) {
  1177. top_index = SelectedIndex - RowCount;
  1178. UpdateTopItem ();
  1179. }
  1180. selected_index = SelectedIndex - RowCount;
  1181. break;
  1182. }
  1183. case ItemNavigation.NextColumn: {
  1184. if (SelectedIndex + RowCount >= Items.Count) {
  1185. break;
  1186. }
  1187. if (SelectedIndex + RowCount > last_visible_index) {
  1188. top_index = SelectedIndex;
  1189. UpdateTopItem ();
  1190. }
  1191. selected_index = SelectedIndex + RowCount;
  1192. break;
  1193. }
  1194. case ItemNavigation.First: {
  1195. top_index = 0;
  1196. selected_index = 0;
  1197. UpdateTopItem ();
  1198. break;
  1199. }
  1200. case ItemNavigation.Last: {
  1201. int rows = items_area.Height / ItemHeight;
  1202. if (multicolumn) {
  1203. selected_index = Items.Count - 1;
  1204. break;
  1205. }
  1206. if (Items.Count < rows) {
  1207. top_index = 0;
  1208. selected_index = Items.Count - 1;
  1209. UpdateTopItem ();
  1210. } else {
  1211. top_index = Items.Count - rows;
  1212. selected_index = Items.Count - 1;
  1213. UpdateTopItem ();
  1214. }
  1215. break;
  1216. }
  1217. case ItemNavigation.Next: {
  1218. if (FocusedItem == Items.Count - 1)
  1219. return -1;
  1220. if (multicolumn) {
  1221. selected_index = FocusedItem + 1;
  1222. break;
  1223. }
  1224. int bottom = 0;
  1225. ArrayList heights = new ArrayList ();
  1226. if (draw_mode == DrawMode.OwnerDrawVariable) {
  1227. for (int i = top_index; i <= FocusedItem + 1; i++) {
  1228. int h = GetItemHeight (i);
  1229. bottom += h;
  1230. heights.Add (h);
  1231. }
  1232. } else {
  1233. bottom = ((FocusedItem + 1) - top_index + 1) * ItemHeight;
  1234. }
  1235. if (bottom >= items_area.Height) {
  1236. int overhang = bottom - items_area.Height;
  1237. int offset = 0;
  1238. if (draw_mode == DrawMode.OwnerDrawVariable)
  1239. while (overhang > 0)
  1240. overhang -= (int) heights [offset];
  1241. else
  1242. offset = (int) Math.Ceiling ((float)overhang / (float) ItemHeight);
  1243. top_index += offset;
  1244. UpdateTopItem ();
  1245. }
  1246. selected_index = FocusedItem + 1;
  1247. break;
  1248. }
  1249. case ItemNavigation.Previous: {
  1250. if (FocusedItem > 0) {
  1251. if (FocusedItem - 1 < top_index) {
  1252. top_index--;
  1253. UpdateTopItem ();
  1254. }
  1255. selected_index = FocusedItem - 1;
  1256. }
  1257. break;
  1258. }
  1259. case ItemNavigation.NextPage: {
  1260. if (Items.Count < page_size) {
  1261. NavigateItemVisually (ItemNavigation.Last);
  1262. break;
  1263. }
  1264. if (FocusedItem + page_size - 1 >= Items.Count) {
  1265. top_index = Items.Count - page_size;
  1266. UpdateTopItem ();
  1267. selected_index = Items.Count - 1;
  1268. }
  1269. else {
  1270. if (FocusedItem + page_size - 1 > last_visible_index) {
  1271. top_index = FocusedItem;
  1272. UpdateTopItem ();
  1273. }
  1274. selected_index = FocusedItem + page_size - 1;
  1275. }
  1276. break;
  1277. }
  1278. case ItemNavigation.PreviousPage: {
  1279. int rows = items_area.Height / ItemHeight;
  1280. if (FocusedItem - (rows - 1) <= 0) {
  1281. top_index = 0;
  1282. UpdateTopItem ();
  1283. selected_index = 0;
  1284. }
  1285. else {
  1286. if (SelectedIndex - (rows - 1) < top_index) {
  1287. top_index = FocusedItem - (rows - 1);
  1288. UpdateTopItem ();
  1289. }
  1290. selected_index = FocusedItem - (rows - 1);
  1291. }
  1292. break;
  1293. }
  1294. default:
  1295. break;
  1296. }
  1297. return selected_index;
  1298. }
  1299. private void OnGotFocus (object sender, EventArgs e)
  1300. {
  1301. if (Items.Count == 0)
  1302. return;
  1303. if (FocusedItem == -1)
  1304. FocusedItem = 0;
  1305. InvalidateItem (FocusedItem);
  1306. }
  1307. private void OnLostFocus (object sender, EventArgs e)
  1308. {
  1309. if (FocusedItem != -1)
  1310. InvalidateItem (FocusedItem);
  1311. }
  1312. private bool KeySearch (Keys key)
  1313. {
  1314. char c = (char) key;
  1315. if (!Char.IsLetterOrDigit (c))
  1316. return false;
  1317. int idx = FindString (c.ToString (), SelectedIndex);
  1318. if (idx != ListBox.NoMatches)
  1319. SelectedIndex = idx;
  1320. return true;
  1321. }
  1322. internal void OnKeyDownLB (object sender, KeyEventArgs e)
  1323. {
  1324. int new_item = -1;
  1325. if (Items.Count == 0)
  1326. return;
  1327. if (KeySearch (e.KeyCode))
  1328. return;
  1329. switch (e.KeyCode) {
  1330. case Keys.ControlKey:
  1331. ctrl_pressed = true;
  1332. break;
  1333. case Keys.ShiftKey:
  1334. shift_pressed = true;
  1335. break;
  1336. case Keys.Home:
  1337. new_item = NavigateItemVisually (ItemNavigation.First);
  1338. break;
  1339. case Keys.End:
  1340. new_item = NavigateItemVisually (ItemNavigation.Last);
  1341. break;
  1342. case Keys.Up:
  1343. new_item = NavigateItemVisually (ItemNavigation.Previous);
  1344. break;
  1345. case Keys.Down:
  1346. new_item = NavigateItemVisually (ItemNavigation.Next);
  1347. break;
  1348. case Keys.PageUp:
  1349. new_item = NavigateItemVisually (ItemNavigation.PreviousPage);
  1350. break;
  1351. case Keys.PageDown:
  1352. new_item = NavigateItemVisually (ItemNavigation.NextPage);
  1353. break;
  1354. case Keys.Right:
  1355. if (multicolumn == true) {
  1356. new_item = NavigateItemVisually (ItemNavigation.NextColumn);
  1357. }
  1358. break;
  1359. case Keys.Left:
  1360. if (multicolumn == true) {
  1361. new_item = NavigateItemVisually (ItemNavigation.PreviousColumn);
  1362. }
  1363. break;
  1364. case Keys.Space:
  1365. if (selection_mode == SelectionMode.MultiSimple) {
  1366. SelectedItemFromNavigation (FocusedItem);
  1367. }
  1368. break;
  1369. default:
  1370. break;
  1371. }
  1372. if (new_item != -1) {
  1373. FocusedItem = new_item;
  1374. if (selection_mode != SelectionMode.MultiSimple)
  1375. SelectedItemFromNavigation (new_item);
  1376. }
  1377. }
  1378. private void OnKeyUpLB (object sender, KeyEventArgs e)
  1379. {
  1380. switch (e.KeyCode) {
  1381. case Keys.ControlKey:
  1382. ctrl_pressed = false;
  1383. break;
  1384. case Keys.ShiftKey:
  1385. shift_pressed = false;
  1386. break;
  1387. default:
  1388. break;
  1389. }
  1390. }
  1391. internal void InvalidateItem (int index)
  1392. {
  1393. if (!IsHandleCreated)
  1394. return;
  1395. Rectangle bounds = GetItemDisplayRectangle (index, top_index);
  1396. if (ClientRectangle.IntersectsWith (bounds))
  1397. Invalidate (bounds);
  1398. }
  1399. internal virtual void OnItemClick (int index)
  1400. {
  1401. OnSelectedIndexChanged (EventArgs.Empty);
  1402. OnSelectedValueChanged (EventArgs.Empty);
  1403. }
  1404. int anchor = -1;
  1405. int[] prev_selection;
  1406. bool button_pressed = false;
  1407. Point button_pressed_loc = new Point (-1, -1);
  1408. private void SelectExtended (int index)
  1409. {
  1410. SuspendLayout ();
  1411. ArrayList new_selection = new ArrayList ();
  1412. int start = anchor < index ? anchor : index;
  1413. int end = anchor > index ? anchor : index;
  1414. for (int i = start; i <= end; i++)
  1415. new_selection.Add (i);
  1416. if (ctrl_pressed)
  1417. foreach (int i in prev_selection)
  1418. if (!new_selection.Contains (i))
  1419. new_selection.Add (i);
  1420. // Need to make a copy since we can't enumerate and modify the collection
  1421. // at the same time
  1422. ArrayList sel_indices = (ArrayList) selected_indices.List.Clone ();
  1423. foreach (int i in sel_indices)
  1424. if (!new_selection.Contains (i))
  1425. selected_indices.Remove (i);
  1426. foreach (int i in new_selection)
  1427. if (!sel_indices.Contains (i))
  1428. selected_indices.AddCore (i);
  1429. ResumeLayout ();
  1430. }
  1431. private void OnMouseDownLB (object sender, MouseEventArgs e)
  1432. {
  1433. // Only do stuff with the left mouse button
  1434. if ((e.Button & MouseButtons.Left) == 0)
  1435. return;
  1436. int index = IndexAtClientPoint (e.X, e.Y);
  1437. if (index == -1)
  1438. return;
  1439. switch (SelectionMode) {
  1440. case SelectionMode.One:
  1441. SelectedIndices.AddCore (index); // Unselects previous one
  1442. break;
  1443. case SelectionMode.MultiSimple:
  1444. if (SelectedIndices.Contains (index))
  1445. SelectedIndices.RemoveCore (index);
  1446. else
  1447. SelectedIndices.AddCore (index);
  1448. break;
  1449. case SelectionMode.MultiExtended:
  1450. shift_pressed = (XplatUI.State.ModifierKeys & Keys.Shift) != 0;
  1451. ctrl_pressed = (XplatUI.State.ModifierKeys & Keys.Control) != 0;
  1452. if (shift_pressed) {
  1453. SelectedIndices.ClearCore ();
  1454. SelectExtended (index);
  1455. break;
  1456. }
  1457. anchor = index;
  1458. if (ctrl_pressed) {
  1459. prev_selection = new int [SelectedIndices.Count];
  1460. SelectedIndices.CopyTo (prev_selection, 0);
  1461. if (SelectedIndices.Contains (index))
  1462. SelectedIndices.RemoveCore (index);
  1463. else
  1464. SelectedIndices.AddCore (index);
  1465. break;
  1466. }
  1467. SelectedIndices.ClearCore ();
  1468. SelectedIndices.AddCore (index);
  1469. break;
  1470. case SelectionMode.None:
  1471. break;
  1472. default:
  1473. return;
  1474. }
  1475. button_pressed = true;
  1476. button_pressed_loc = new Point (e.X, e.Y);
  1477. FocusedItem = index;
  1478. }
  1479. private void OnMouseMoveLB (object sender, MouseEventArgs e)
  1480. {
  1481. // Don't take into account MouseMove events generated with MouseDown
  1482. if (!button_pressed || button_pressed_loc == new Point (e.X, e.Y))
  1483. return;
  1484. int index = IndexAtClientPoint (e.X, e.Y);
  1485. if (index == -1)
  1486. return;
  1487. switch (SelectionMode) {
  1488. case SelectionMode.One:
  1489. SelectedIndices.AddCore (index); // Unselects previous one
  1490. break;
  1491. case SelectionMode.MultiSimple:
  1492. break;
  1493. case SelectionMode.MultiExtended:
  1494. SelectExtended (index);
  1495. break;
  1496. case SelectionMode.None:
  1497. break;
  1498. default:
  1499. return;
  1500. }
  1501. FocusedItem = index;
  1502. }
  1503. internal override void OnDragDropEnd (DragDropEffects effects)
  1504. {
  1505. // In the case of a DnD operation (started on MouseDown)
  1506. // there will be no MouseUp event, so we need to reset
  1507. // the state here
  1508. button_pressed = false;
  1509. }
  1510. private void OnMouseUpLB (object sender, MouseEventArgs e)
  1511. {
  1512. // Only do stuff with the left mouse button
  1513. if ((e.Button & MouseButtons.Left) == 0)
  1514. return;
  1515. if (e.Clicks > 1) {
  1516. OnDoubleClick (EventArgs.Empty);
  1517. #if NET_2_0
  1518. OnMouseDoubleClick (e);
  1519. #endif
  1520. }
  1521. else if (e.Clicks == 1) {
  1522. OnClick (EventArgs.Empty);
  1523. #if NET_2_0
  1524. OnMouseClick (e);
  1525. #endif
  1526. }
  1527. if (!button_pressed)
  1528. return;
  1529. int index = IndexAtClientPoint (e.X, e.Y);
  1530. OnItemClick (index);
  1531. button_pressed = ctrl_pressed = shift_pressed = false;
  1532. }
  1533. private void Scroll (ScrollBar scrollbar, int delta)
  1534. {
  1535. if (delta == 0 || !scrollbar.Visible || !scrollbar.Enabled)
  1536. return;
  1537. int max;
  1538. if (scrollbar == hscrollbar)
  1539. max = hscrollbar.Maximum - (items_area.Width / ColumnWidthInternal) + 1;
  1540. else
  1541. max = vscrollbar.Maximum - (items_area.Height / ItemHeight) + 1;
  1542. int val = scrollbar.Value + delta;
  1543. if (val > max)
  1544. val = max;
  1545. else if (val < scrollbar.Minimum)
  1546. val = scrollbar.Minimum;
  1547. scrollbar.Value = val;
  1548. }
  1549. private void OnMouseWheelLB (object sender, MouseEventArgs me)
  1550. {
  1551. if (Items.Count == 0)
  1552. return;
  1553. int lines = me.Delta / 120;
  1554. if (MultiColumn)
  1555. Scroll (hscrollbar, -SystemInformation.MouseWheelScrollLines * lines);
  1556. else
  1557. Scroll (vscrollbar, -lines);
  1558. }
  1559. internal override void OnPaintInternal (PaintEventArgs pevent)
  1560. {
  1561. if (suspend_layout)
  1562. return;
  1563. Draw (pevent.ClipRectangle, pevent.Graphics);
  1564. }
  1565. internal void RepositionScrollBars ()
  1566. {
  1567. if (vscrollbar.is_visible) {
  1568. vscrollbar.Size = new Size (vscrollbar.Width, items_area.Height);
  1569. vscrollbar.Location = new Point (items_area.Width, 0);
  1570. }
  1571. if (hscrollbar.is_visible) {
  1572. hscrollbar.Size = new Size (items_area.Width, hscrollbar.Height);
  1573. hscrollbar.Location = new Point (0, items_area.Height);
  1574. }
  1575. }
  1576. // An item navigation operation (mouse or keyboard) has caused to select a new item
  1577. internal void SelectedItemFromNavigation (int index)
  1578. {
  1579. switch (SelectionMode) {
  1580. case SelectionMode.None:
  1581. // .Net doesn't select the item, only ensures that it's visible
  1582. // and fires the selection related events
  1583. EnsureVisible (index);
  1584. OnSelectedIndexChanged (EventArgs.Empty);
  1585. OnSelectedValueChanged (EventArgs.Empty);
  1586. break;
  1587. case SelectionMode.One: {
  1588. SelectedIndex = index;
  1589. break;
  1590. }
  1591. case SelectionMode.MultiSimple: {
  1592. if (SelectedIndex == -1) {
  1593. SelectedIndex = index;
  1594. } else {
  1595. if (SelectedIndices.Contains (index))
  1596. SelectedIndices.Remove (index);
  1597. else {
  1598. SelectedIndices.AddCore (index);
  1599. OnSelectedIndexChanged (EventArgs.Empty);
  1600. OnSelectedValueChanged (EventArgs.Empty);
  1601. }
  1602. }
  1603. break;
  1604. }
  1605. case SelectionMode.MultiExtended: {
  1606. if (SelectedIndex == -1) {
  1607. SelectedIndex = index;
  1608. } else {
  1609. if (ctrl_pressed == false && shift_pressed == false) {
  1610. SelectedIndices.Clear ();
  1611. }
  1612. if (shift_pressed == true) {
  1613. ShiftSelection (index);
  1614. } else { // ctrl_pressed or single item
  1615. SelectedIndices.AddCore (index);
  1616. }
  1617. OnSelectedIndexChanged (EventArgs.Empty);
  1618. OnSelectedValueChanged (EventArgs.Empty);
  1619. }
  1620. break;
  1621. }
  1622. default:
  1623. break;
  1624. }
  1625. }
  1626. private void ShiftSelection (int index)
  1627. {
  1628. int shorter_item = -1, dist = Items.Count + 1, cur_dist;
  1629. foreach (int idx in selected_indices) {
  1630. if (idx > index) {
  1631. cur_dist = idx - index;
  1632. } else {
  1633. cur_dist = index - idx;
  1634. }
  1635. if (cur_dist < dist) {
  1636. dist = cur_dist;
  1637. shorter_item = idx;
  1638. }
  1639. }
  1640. if (shorter_item != -1) {
  1641. int start, end;
  1642. if (shorter_item > index) {
  1643. start = index;
  1644. end = shorter_item;
  1645. } else {
  1646. start = shorter_item;
  1647. end = index;
  1648. }
  1649. selected_indices.Clear ();
  1650. for (int idx = start; idx <= end; idx++) {
  1651. selected_indices.AddCore (idx);
  1652. }
  1653. }
  1654. }
  1655. internal int FocusedItem {
  1656. get { return focused_item; }
  1657. set {
  1658. if (focused_item == value)
  1659. return;
  1660. int prev = focused_item;
  1661. focused_item = value;
  1662. if (has_focus == false)
  1663. return;
  1664. if (prev != -1)
  1665. InvalidateItem (prev);
  1666. if (value != -1)
  1667. InvalidateItem (value);
  1668. }
  1669. }
  1670. StringFormat string_format;
  1671. internal StringFormat StringFormat {
  1672. get {
  1673. if (string_format == null) {
  1674. string_format = new StringFormat ();
  1675. if (RightToLeft == RightToLeft.Yes)
  1676. string_format.Alignment = StringAlignment.Far;
  1677. else
  1678. string_format.Alignment = StringAlignment.Near;
  1679. CalculateTabStops ();
  1680. }
  1681. return string_format;
  1682. }
  1683. }
  1684. internal virtual void CollectionChanged ()
  1685. {
  1686. if (sorted)
  1687. Sort (false);
  1688. if (Items.Count == 0) {
  1689. selected_indices.List.Clear ();
  1690. focused_item = -1;
  1691. top_index = 0;
  1692. }
  1693. if (Items.Count <= focused_item)
  1694. focused_item = Items.Count - 1;
  1695. if (!IsHandleCreated || suspend_layout)
  1696. return;
  1697. LayoutListBox ();
  1698. base.Refresh ();
  1699. }
  1700. void EnsureVisible (int index)
  1701. {
  1702. if (!IsHandleCreated || index == -1)
  1703. return;
  1704. if (index < top_index) {
  1705. top_index = index;
  1706. UpdateTopItem ();
  1707. Invalidate ();
  1708. } else if (!multicolumn) {
  1709. int rows = items_area.Height / ItemHeight;
  1710. if (index >= (top_index + rows))
  1711. top_index = index - rows + 1;
  1712. UpdateTopItem ();
  1713. } else {
  1714. int rows = Math.Max (1, items_area.Height / ItemHeight);
  1715. int cols = Math.Max (1, items_area.Width / ColumnWidthInternal);
  1716. if (index >= (top_index + (rows * cols))) {
  1717. int incolumn = index / rows;
  1718. top_index = (incolumn - (cols - 1)) * rows;
  1719. UpdateTopItem ();
  1720. Invalidate ();
  1721. }
  1722. }
  1723. }
  1724. private void UpdateListBoxBounds ()
  1725. {
  1726. if (IsHandleCreated)
  1727. SetBoundsInternal (bounds.X, bounds.Y, bounds.Width, IntegralHeight ? SnapHeightToIntegral (requested_height) : requested_height, BoundsSpecified.None);
  1728. }
  1729. private void UpdateScrollBars ()
  1730. {
  1731. items_area = ClientRectangle;
  1732. if (UpdateHorizontalScrollBar ()) {
  1733. items_area.Height -= hscrollbar.Height;
  1734. if (UpdateVerticalScrollBar ()) {
  1735. items_area.Width -= vscrollbar.Width;
  1736. UpdateHorizontalScrollBar ();
  1737. }
  1738. } else if (UpdateVerticalScrollBar ()) {
  1739. items_area.Width -= vscrollbar.Width;
  1740. if (UpdateHorizontalScrollBar ()) {
  1741. items_area.Height -= hscrollbar.Height;
  1742. UpdateVerticalScrollBar ();
  1743. }
  1744. }
  1745. RepositionScrollBars ();
  1746. }
  1747. /* Determines if the horizontal scrollbar has to be displyed */
  1748. private bool UpdateHorizontalScrollBar ()
  1749. {
  1750. bool show = false;
  1751. bool enabled = true;
  1752. if (MultiColumn) {
  1753. if (canvas_size.Width > items_area.Width) {
  1754. show = true;
  1755. hscrollbar.Maximum = canvas_size.Width / ColumnWidthInternal - 1;
  1756. } else if (ScrollAlwaysVisible == true) {
  1757. enabled = false;
  1758. show = true;
  1759. hscrollbar.Maximum = 0;
  1760. }
  1761. } else if (canvas_size.Width > ClientRectangle.Width && HorizontalScrollbar) {
  1762. show = true;
  1763. hscrollbar.Maximum = canvas_size.Width;
  1764. hscrollbar.LargeChange = Math.Max (0, items_area.Width);
  1765. } else if (horizontal_scrollbar) {
  1766. show = true;
  1767. enabled = false;
  1768. hscrollbar.Maximum = 0;
  1769. }
  1770. hbar_offset = hscrollbar.Value;
  1771. hscrollbar.Enabled = enabled;
  1772. hscrollbar.Visible = show;
  1773. return show;
  1774. }
  1775. /* Determines if the vertical scrollbar has to be displyed */
  1776. private bool UpdateVerticalScrollBar ()
  1777. {
  1778. if (MultiColumn || (Items.Count == 0 && !scroll_always_visible)) {
  1779. vscrollbar.Visible = false;
  1780. return false;
  1781. } else if (Items.Count == 0) {
  1782. vscrollbar.Visible = true;
  1783. vscrollbar.Enabled = false;
  1784. vscrollbar.Maximum = 0;
  1785. return true;
  1786. }
  1787. bool show = false;
  1788. bool enabled = true;
  1789. if (canvas_size.Height > items_area.Height) {
  1790. show = true;
  1791. vscrollbar.Maximum = Items.Count - 1;
  1792. vscrollbar.LargeChange = Math.Max (items_area.Height / ItemHeight, 0);
  1793. } else if (ScrollAlwaysVisible) {
  1794. show = true;
  1795. enabled = false;
  1796. vscrollbar.Maximum = 0;
  1797. }
  1798. vscrollbar.Enabled = enabled;
  1799. vscrollbar.Visible = show;
  1800. return show;
  1801. }
  1802. // Value Changed
  1803. private void VerticalScrollEvent (object sender, EventArgs e)
  1804. {
  1805. int top_item = top_index;
  1806. top_index = /*row_count + */ vscrollbar.Value;
  1807. last_visible_index = LastVisibleItem ();
  1808. int delta = (top_item - top_index) * ItemHeight;
  1809. if (DrawMode == DrawMode.OwnerDrawVariable) {
  1810. delta = 0;
  1811. if (top_index < top_item)
  1812. for (int i = top_index; i < top_item; i++)
  1813. delta += GetItemHeight (i);
  1814. else
  1815. for (int i = top_item; i < top_index; i++)
  1816. delta -= GetItemHeight (i);
  1817. }
  1818. if (IsHandleCreated)
  1819. XplatUI.ScrollWindow (Handle, items_area, 0, delta, false);
  1820. }
  1821. #endregion Private Methods
  1822. #if NET_2_0
  1823. public class IntegerCollection : IList, ICollection, IEnumerable
  1824. {
  1825. private ListBox owner;
  1826. private List<int> list;
  1827. #region Public Constructor
  1828. public IntegerCollection (ListBox owner)
  1829. {
  1830. this.owner = owner;
  1831. list = new List<int> ();
  1832. }
  1833. #endregion
  1834. #region Public Properties
  1835. [Browsable (false)]
  1836. public int Count {
  1837. get { return list.Count; }
  1838. }
  1839. public int this [int index] {
  1840. get { return list[index]; }
  1841. set { list[index] = value; owner.CalculateTabStops (); }
  1842. }
  1843. #endregion
  1844. #region Public Methods
  1845. public int Add (int item)
  1846. {
  1847. // This collection does not allow duplicates
  1848. if (!list.Contains (item)) {
  1849. list.Add (item);
  1850. list.Sort ();
  1851. owner.CalculateTabStops ();
  1852. }
  1853. return list.IndexOf (item);
  1854. }
  1855. public void AddRange (int[] items)
  1856. {
  1857. AddItems (items);
  1858. }
  1859. public void AddRange (IntegerCollection value)
  1860. {
  1861. AddItems (value);
  1862. }
  1863. void AddItems (IList items)
  1864. {
  1865. if (items == null)
  1866. throw new ArgumentNullException ("items");
  1867. foreach (int i in items)
  1868. if (!list.Contains (i))
  1869. list.Add (i);
  1870. list.Sort ();
  1871. }
  1872. public void Clear ()
  1873. {
  1874. list.Clear ();
  1875. owner.CalculateTabStops ();
  1876. }
  1877. public bool Contains (int item)
  1878. {
  1879. return list.Contains (item);
  1880. }
  1881. public void CopyTo (Array destination, int index)
  1882. {
  1883. for (int i = 0; i < list.Count; i++)
  1884. destination.SetValue (list[i], index++);
  1885. }
  1886. public int IndexOf (int item)
  1887. {
  1888. return list.IndexOf (item);
  1889. }
  1890. public void Remove (int item)
  1891. {
  1892. list.Remove (item);
  1893. list.Sort ();
  1894. owner.CalculateTabStops ();
  1895. }
  1896. public void RemoveAt (int index)
  1897. {
  1898. if (index < 0)
  1899. throw new IndexOutOfRangeException ();
  1900. list.RemoveAt (index);
  1901. list.Sort ();
  1902. owner.CalculateTabStops ();
  1903. }
  1904. #endregion
  1905. #region IEnumerable Members
  1906. IEnumerator IEnumerable.GetEnumerator ()
  1907. {
  1908. return list.GetEnumerator ();
  1909. }
  1910. #endregion
  1911. #region IList Members
  1912. int IList.Add (object item)
  1913. {
  1914. int? intValue = item as int?;
  1915. if (!intValue.HasValue)
  1916. throw new ArgumentException ("item");
  1917. return Add (intValue.Value);
  1918. }
  1919. void IList.Clear ()
  1920. {
  1921. Clear ();
  1922. }
  1923. bool IList.Contains (object item)
  1924. {
  1925. int? intValue = item as int?;
  1926. if (!intValue.HasValue)
  1927. return false;
  1928. return Contains (intValue.Value);
  1929. }
  1930. int IList.IndexOf (object item)
  1931. {
  1932. int? intValue = item as int?;
  1933. if (!intValue.HasValue)
  1934. return -1;
  1935. return IndexOf (intValue.Value);
  1936. }
  1937. void IList.Insert (int index, object value)
  1938. {
  1939. throw new NotSupportedException (string.Format (
  1940. CultureInfo.InvariantCulture, "No items "
  1941. + "can be inserted into {0}, since it is"
  1942. + " a sorted collection.", this.GetType ()));
  1943. }
  1944. bool IList.IsFixedSize
  1945. {
  1946. get { return false; }
  1947. }
  1948. bool IList.IsReadOnly
  1949. {
  1950. get { return false; }
  1951. }
  1952. void IList.Remove (object value)
  1953. {
  1954. int? intValue = value as int?;
  1955. if (!intValue.HasValue)
  1956. throw new ArgumentException ("value");
  1957. Remove (intValue.Value);
  1958. }
  1959. void IList.RemoveAt (int index)
  1960. {
  1961. RemoveAt (index);
  1962. }
  1963. object IList.this[int index] {
  1964. get { return this[index]; }
  1965. set { this[index] = (int)value; }
  1966. }
  1967. #endregion
  1968. #region ICollection Members
  1969. bool ICollection.IsSynchronized {
  1970. get { return true; }
  1971. }
  1972. object ICollection.SyncRoot {
  1973. get { return this; }
  1974. }
  1975. #endregion
  1976. }
  1977. #endif
  1978. [ListBindable (false)]
  1979. public class ObjectCollection : IList, ICollection, IEnumerable
  1980. {
  1981. internal class ListObjectComparer : IComparer
  1982. {
  1983. public int Compare (object a, object b)
  1984. {
  1985. string str1 = a.ToString ();
  1986. string str2 = b.ToString ();
  1987. return str1.CompareTo (str2);
  1988. }
  1989. }
  1990. private ListBox owner;
  1991. internal ArrayList object_items = new ArrayList ();
  1992. #region UIA Framework Events
  1993. #if NET_2_0
  1994. //NOTE:
  1995. // We are using Reflection to add/remove internal events.
  1996. // Class ListProvider uses the events.
  1997. //
  1998. //Event used to generate UIA StructureChangedEvent
  1999. static object UIACollectionChangedEvent = new object ();
  2000. internal event CollectionChangeEventHandler UIACollectionChanged {
  2001. add { owner.Events.AddHandler (UIACollectionChangedEvent, value); }
  2002. remove { owner.Events.RemoveHandler (UIACollectionChangedEvent, value); }
  2003. }
  2004. internal void OnUIACollectionChangedEvent (CollectionChangeEventArgs args)
  2005. {
  2006. CollectionChangeEventHandler eh
  2007. = (CollectionChangeEventHandler) owner.Events [UIACollectionChangedEvent];
  2008. if (eh != null)
  2009. eh (owner, args);
  2010. }
  2011. #endif
  2012. #endregion UIA Framework Events
  2013. public ObjectCollection (ListBox owner)
  2014. {
  2015. this.owner = owner;
  2016. }
  2017. public ObjectCollection (ListBox owner, object[] value)
  2018. {
  2019. this.owner = owner;
  2020. AddRange (value);
  2021. }
  2022. public ObjectCollection (ListBox owner, ObjectCollection value)
  2023. {
  2024. this.owner = owner;
  2025. AddRange (value);
  2026. }
  2027. #region Public Properties
  2028. public int Count {
  2029. get { return object_items.Count; }
  2030. }
  2031. public bool IsReadOnly {
  2032. get { return false; }
  2033. }
  2034. [Browsable(false)]
  2035. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2036. public virtual object this [int index] {
  2037. get {
  2038. if (index < 0 || index >= Count)
  2039. throw new ArgumentOutOfRangeException ("Index of out range");
  2040. return object_items[index];
  2041. }
  2042. set {
  2043. if (index < 0 || index >= Count)
  2044. throw new ArgumentOutOfRangeException ("Index of out range");
  2045. if (value == null)
  2046. throw new ArgumentNullException ("value");
  2047. #if NET_2_0
  2048. //UIA Framework event: Item Removed
  2049. OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, index));
  2050. #endif
  2051. object_items[index] = value;
  2052. #if NET_2_0
  2053. //UIA Framework event: Item Added
  2054. OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, index));
  2055. #endif
  2056. owner.CollectionChanged ();
  2057. }
  2058. }
  2059. bool ICollection.IsSynchronized {
  2060. get { return false; }
  2061. }
  2062. object ICollection.SyncRoot {
  2063. get { return this; }
  2064. }
  2065. bool IList.IsFixedSize {
  2066. get { return false; }
  2067. }
  2068. #endregion Public Properties
  2069. #region Public Methods
  2070. public int Add (object item)
  2071. {
  2072. int idx;
  2073. idx = AddItem (item);
  2074. owner.CollectionChanged ();
  2075. // If we are sorted, the item probably moved indexes, get the real one
  2076. if (owner.sorted)
  2077. return this.IndexOf (item);
  2078. return idx;
  2079. }
  2080. public void AddRange (object[] items)
  2081. {
  2082. AddItems (items);
  2083. }
  2084. public void AddRange (ObjectCollection value)
  2085. {
  2086. AddItems (value);
  2087. }
  2088. internal void AddItems (IList items)
  2089. {
  2090. if (items == null)
  2091. throw new ArgumentNullException ("items");
  2092. #if ONLY_1_1
  2093. foreach (object mi in items)
  2094. if (mi == null)
  2095. throw new ArgumentNullException ("item");
  2096. #endif
  2097. foreach (object mi in items)
  2098. AddItem (mi);
  2099. owner.CollectionChanged ();
  2100. }
  2101. public virtual void Clear ()
  2102. {
  2103. owner.selected_indices.ClearCore ();
  2104. object_items.Clear ();
  2105. owner.CollectionChanged ();
  2106. #if NET_2_0
  2107. //UIA Framework event: Items list cleared
  2108. OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, -1));
  2109. #endif
  2110. }
  2111. public bool Contains (object value)
  2112. {
  2113. if (value == null)
  2114. throw new ArgumentNullException ("value");
  2115. return object_items.Contains (value);
  2116. }
  2117. #if NET_2_0
  2118. public void CopyTo (object[] destination, int arrayIndex)
  2119. {
  2120. object [] dest = destination;
  2121. #else
  2122. public void CopyTo (object [] dest, int arrayIndex)
  2123. {
  2124. #endif
  2125. object_items.CopyTo (dest, arrayIndex);
  2126. }
  2127. #if NET_2_0
  2128. void ICollection.CopyTo (Array destination, int index)
  2129. {
  2130. Array dest = destination;
  2131. #else
  2132. void ICollection.CopyTo (Array dest, int index)
  2133. {
  2134. #endif
  2135. object_items.CopyTo (dest, index);
  2136. }
  2137. public IEnumerator GetEnumerator ()
  2138. {
  2139. return object_items.GetEnumerator ();
  2140. }
  2141. int IList.Add (object item)
  2142. {
  2143. return Add (item);
  2144. }
  2145. public int IndexOf (object value)
  2146. {
  2147. if (value == null)
  2148. throw new ArgumentNullException ("value");
  2149. return object_items.IndexOf (value);
  2150. }
  2151. public void Insert (int index, object item)
  2152. {
  2153. if (index < 0 || index > Count)
  2154. throw new ArgumentOutOfRangeException ("Index of out range");
  2155. if (item == null)
  2156. throw new ArgumentNullException ("item");
  2157. owner.BeginUpdate ();
  2158. object_items.Insert (index, item);
  2159. owner.CollectionChanged ();
  2160. owner.EndUpdate ();
  2161. #if NET_2_0
  2162. //UIA Framework event: Item Added
  2163. OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, index));
  2164. #endif
  2165. }
  2166. public void Remove (object value)
  2167. {
  2168. if (value == null)
  2169. return;
  2170. int index = IndexOf (value);
  2171. if (index != -1)
  2172. RemoveAt (index);
  2173. }
  2174. public void RemoveAt (int index)
  2175. {
  2176. if (index < 0 || index >= Count)
  2177. throw new ArgumentOutOfRangeException ("Index of out range");
  2178. owner.selected_indices.Remove (index);
  2179. object_items.RemoveAt (index);
  2180. owner.CollectionChanged ();
  2181. #if NET_2_0
  2182. //UIA Framework event: Item Removed
  2183. OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, index));
  2184. #endif
  2185. }
  2186. #endregion Public Methods
  2187. #region Private Methods
  2188. internal int AddItem (object item)
  2189. {
  2190. if (item == null)
  2191. throw new ArgumentNullException ("item");
  2192. int cnt = object_items.Count;
  2193. object_items.Add (item);
  2194. #if NET_2_0
  2195. //UIA Framework event: Item Added
  2196. OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, cnt));
  2197. #endif
  2198. return cnt;
  2199. }
  2200. internal void Sort ()
  2201. {
  2202. object_items.Sort (new ListObjectComparer ());
  2203. }
  2204. #endregion Private Methods
  2205. }
  2206. public class SelectedIndexCollection : IList, ICollection, IEnumerable
  2207. {
  2208. private ListBox owner;
  2209. ArrayList selection;
  2210. bool sorting_needed; // Selection state retrieval is done sorted - we do it lazyly
  2211. public SelectedIndexCollection (ListBox owner)
  2212. {
  2213. this.owner = owner;
  2214. selection = new ArrayList ();
  2215. }
  2216. #region Public Properties
  2217. [Browsable (false)]
  2218. public int Count {
  2219. get { return selection.Count; }
  2220. }
  2221. public bool IsReadOnly {
  2222. get { return true; }
  2223. }
  2224. public int this [int index] {
  2225. get {
  2226. if (index < 0 || index >= Count)
  2227. throw new ArgumentOutOfRangeException ("Index of out range");
  2228. CheckSorted ();
  2229. return (int)selection [index];
  2230. }
  2231. }
  2232. bool ICollection.IsSynchronized {
  2233. get { return true; }
  2234. }
  2235. bool IList.IsFixedSize{
  2236. get { return true; }
  2237. }
  2238. object ICollection.SyncRoot {
  2239. get { return selection; }
  2240. }
  2241. #endregion Public Properties
  2242. #region Public Methods
  2243. #if NET_2_0
  2244. public
  2245. #else
  2246. internal
  2247. #endif
  2248. void Add (int index)
  2249. {
  2250. if (AddCore (index)) {
  2251. owner.OnSelectedIndexChanged (EventArgs.Empty);
  2252. owner.OnSelectedValueChanged (EventArgs.Empty);
  2253. }
  2254. }
  2255. // Need to separate selection logic from events,
  2256. // since selection changes using keys/mouse handle them their own way
  2257. internal bool AddCore (int index)
  2258. {
  2259. if (selection.Contains (index))
  2260. return false;
  2261. if (index == -1) // Weird MS behaviour
  2262. return false;
  2263. if (index < -1 || index >= owner.Items.Count)
  2264. throw new ArgumentOutOfRangeException ("index");
  2265. if (owner.selection_mode == SelectionMode.None)
  2266. throw new InvalidOperationException ("Cannot call this method when selection mode is SelectionMode.None");
  2267. if (owner.selection_mode == SelectionMode.One && Count > 0) // Unselect previously selected item
  2268. RemoveCore ((int)selection [0]);
  2269. selection.Add (index);
  2270. sorting_needed = true;
  2271. owner.EnsureVisible (index);
  2272. owner.FocusedItem = index;
  2273. owner.InvalidateItem (index);
  2274. return true;
  2275. }
  2276. #if NET_2_0
  2277. public
  2278. #else
  2279. internal
  2280. #endif
  2281. void Clear ()
  2282. {
  2283. if (ClearCore ()) {
  2284. owner.OnSelectedIndexChanged (EventArgs.Empty);
  2285. owner.OnSelectedValueChanged (EventArgs.Empty);
  2286. }
  2287. }
  2288. internal bool ClearCore ()
  2289. {
  2290. if (selection.Count == 0)
  2291. return false;
  2292. foreach (int index in selection)
  2293. owner.InvalidateItem (index);
  2294. selection.Clear ();
  2295. return true;
  2296. }
  2297. public bool Contains (int selectedIndex)
  2298. {
  2299. foreach (int index in selection)
  2300. if (index == selectedIndex)
  2301. return true;
  2302. return false;
  2303. }
  2304. #if NET_2_0
  2305. public void CopyTo (Array destination, int index)
  2306. {
  2307. Array dest = destination;
  2308. #else
  2309. public void CopyTo (Array dest, int index)
  2310. {
  2311. #endif
  2312. CheckSorted ();
  2313. selection.CopyTo (dest, index);
  2314. }
  2315. public IEnumerator GetEnumerator ()
  2316. {
  2317. CheckSorted ();
  2318. return selection.GetEnumerator ();
  2319. }
  2320. // FIXME: Probably we can avoid sorting when calling
  2321. // IndexOf (imagine a scenario where multiple removal of items
  2322. // happens)
  2323. #if NET_2_0
  2324. public
  2325. #else
  2326. internal
  2327. #endif
  2328. void Remove (int index)
  2329. {
  2330. // Separate logic from events here too
  2331. if (RemoveCore (index)) {
  2332. owner.OnSelectedIndexChanged (EventArgs.Empty);
  2333. owner.OnSelectedValueChanged (EventArgs.Empty);
  2334. }
  2335. }
  2336. internal bool RemoveCore (int index)
  2337. {
  2338. int idx = IndexOf (index);
  2339. if (idx == -1)
  2340. return false;
  2341. selection.RemoveAt (idx);
  2342. owner.InvalidateItem (index);
  2343. return true;
  2344. }
  2345. int IList.Add (object value)
  2346. {
  2347. throw new NotSupportedException ();
  2348. }
  2349. void IList.Clear ()
  2350. {
  2351. throw new NotSupportedException ();
  2352. }
  2353. bool IList.Contains (object selectedIndex)
  2354. {
  2355. return Contains ((int)selectedIndex);
  2356. }
  2357. int IList.IndexOf (object selectedIndex)
  2358. {
  2359. return IndexOf ((int) selectedIndex);
  2360. }
  2361. void IList.Insert (int index, object value)
  2362. {
  2363. throw new NotSupportedException ();
  2364. }
  2365. void IList.Remove (object value)
  2366. {
  2367. throw new NotSupportedException ();
  2368. }
  2369. void IList.RemoveAt (int index)
  2370. {
  2371. throw new NotSupportedException ();
  2372. }
  2373. object IList.this[int index]{
  2374. get { return this [index]; }
  2375. set {throw new NotImplementedException (); }
  2376. }
  2377. public int IndexOf (int selectedIndex)
  2378. {
  2379. CheckSorted ();
  2380. for (int i = 0; i < selection.Count; i++)
  2381. if ((int)selection [i] == selectedIndex)
  2382. return i;
  2383. return -1;
  2384. }
  2385. #endregion Public Methods
  2386. internal ArrayList List {
  2387. get {
  2388. CheckSorted ();
  2389. return selection;
  2390. }
  2391. }
  2392. void CheckSorted ()
  2393. {
  2394. if (sorting_needed) {
  2395. sorting_needed = false;
  2396. selection.Sort ();
  2397. }
  2398. }
  2399. }
  2400. public class SelectedObjectCollection : IList, ICollection, IEnumerable
  2401. {
  2402. private ListBox owner;
  2403. public SelectedObjectCollection (ListBox owner)
  2404. {
  2405. this.owner = owner;
  2406. }
  2407. #region Public Properties
  2408. public int Count {
  2409. get { return owner.selected_indices.Count; }
  2410. }
  2411. public bool IsReadOnly {
  2412. get { return true; }
  2413. }
  2414. [Browsable(false)]
  2415. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2416. public object this [int index] {
  2417. get {
  2418. if (index < 0 || index >= Count)
  2419. throw new ArgumentOutOfRangeException ("Index of out range");
  2420. return owner.items [owner.selected_indices [index]];
  2421. }
  2422. set {throw new NotSupportedException ();}
  2423. }
  2424. bool ICollection.IsSynchronized {
  2425. get { return true; }
  2426. }
  2427. object ICollection.SyncRoot {
  2428. get { return this; }
  2429. }
  2430. bool IList.IsFixedSize {
  2431. get { return true; }
  2432. }
  2433. #endregion Public Properties
  2434. #region Public Methods
  2435. #if NET_2_0
  2436. public void Add (object value)
  2437. {
  2438. if (owner.selection_mode == SelectionMode.None)
  2439. throw new ArgumentException ("Cannot call this method if SelectionMode is SelectionMode.None");
  2440. int idx = owner.items.IndexOf (value);
  2441. if (idx == -1)
  2442. return;
  2443. owner.selected_indices.Add (idx);
  2444. }
  2445. public void Clear ()
  2446. {
  2447. owner.selected_indices.Clear ();
  2448. }
  2449. #endif
  2450. public bool Contains (object selectedObject)
  2451. {
  2452. int idx = owner.items.IndexOf (selectedObject);
  2453. return idx == -1 ? false : owner.selected_indices.Contains (idx);
  2454. }
  2455. #if NET_2_0
  2456. public void CopyTo (Array destination, int index)
  2457. {
  2458. Array dest = destination;
  2459. #else
  2460. public void CopyTo (Array dest, int index)
  2461. {
  2462. #endif
  2463. for (int i = 0; i < Count; i++)
  2464. dest.SetValue (this [i], index++);
  2465. }
  2466. #if NET_2_0
  2467. public void Remove (object value)
  2468. {
  2469. if (value == null)
  2470. return;
  2471. int idx = owner.items.IndexOf (value);
  2472. if (idx == -1)
  2473. return;
  2474. owner.selected_indices.Remove (idx);
  2475. }
  2476. #endif
  2477. int IList.Add (object value)
  2478. {
  2479. throw new NotSupportedException ();
  2480. }
  2481. void IList.Clear ()
  2482. {
  2483. throw new NotSupportedException ();
  2484. }
  2485. void IList.Insert (int index, object value)
  2486. {
  2487. throw new NotSupportedException ();
  2488. }
  2489. void IList.Remove (object value)
  2490. {
  2491. throw new NotSupportedException ();
  2492. }
  2493. void IList.RemoveAt (int index)
  2494. {
  2495. throw new NotSupportedException ();
  2496. }
  2497. public int IndexOf (object selectedObject)
  2498. {
  2499. int idx = owner.items.IndexOf (selectedObject);
  2500. return idx == -1 ? -1 : owner.selected_indices.IndexOf (idx);
  2501. }
  2502. public IEnumerator GetEnumerator ()
  2503. {
  2504. //FIXME: write an enumerator that uses selection.GetEnumerator
  2505. // so that invalidation is write on selection changes
  2506. object [] items = new object [Count];
  2507. for (int i = 0; i < Count; i++) {
  2508. items [i] = owner.items [owner.selected_indices [i]];
  2509. }
  2510. return items.GetEnumerator ();
  2511. }
  2512. #endregion Public Methods
  2513. }
  2514. }
  2515. }