ListView.cs 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  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. (http://www.novell.com)
  21. //
  22. // Author:
  23. // Ravindra ([email protected])
  24. //
  25. // $Revision: 1.7 $
  26. // $Modtime: $
  27. // $Log: ListView.cs,v $
  28. // Revision 1.7 2004/10/30 10:21:14 ravindra
  29. // Added support for scrolling and fixed calculations.
  30. //
  31. // Revision 1.6 2004/10/26 19:51:20 jordi
  32. // removes warning
  33. //
  34. // Revision 1.5 2004/10/26 09:55:48 ravindra
  35. // Some formatting for my last checkins.
  36. //
  37. // Revision 1.4 2004/10/26 09:31:35 ravindra
  38. // Added some internal members and calculations for ListView.
  39. //
  40. // Revision 1.3 2004/10/15 15:03:39 ravindra
  41. // Implemented Paint method and fixed coding style.
  42. //
  43. // Revision 1.2 2004/10/02 11:32:01 ravindra
  44. // Added attributes.
  45. //
  46. // Revision 1.1 2004/09/30 13:24:25 ravindra
  47. // Initial implementation.
  48. //
  49. //
  50. // NOT COMPLETE
  51. //
  52. using System.Collections;
  53. using System.ComponentModel;
  54. using System.ComponentModel.Design;
  55. using System.Drawing;
  56. using System.Runtime.InteropServices;
  57. namespace System.Windows.Forms
  58. {
  59. [DefaultEvent ("SelectedIndexChanged")]
  60. [DefaultProperty ("Items")]
  61. [Designer ("System.Windows.Forms.Design.ListViewDesigner, " + Consts.AssemblySystem_Design,
  62. typeof (IDesigner))]
  63. public class ListView : Control
  64. {
  65. private ItemActivation activation = ItemActivation.Standard;
  66. private ListViewAlignment alignment = ListViewAlignment.Top;
  67. private bool allow_column_reorder = false;
  68. private bool auto_arrange = true;
  69. private BorderStyle border_style = BorderStyle.Fixed3D;
  70. private bool check_boxes = false;
  71. private CheckedIndexCollection checked_indices;
  72. private CheckedListViewItemCollection checked_items;
  73. private ColumnHeaderCollection columns;
  74. private ListViewItem focused_item;
  75. private bool full_row_select = false;
  76. private bool grid_lines = false;
  77. private ColumnHeaderStyle header_style = ColumnHeaderStyle.Clickable;
  78. private bool hide_selection = true;
  79. private bool hover_selection = false;
  80. private IComparer item_sorter;
  81. private ListViewItemCollection items;
  82. private bool label_edit = false;
  83. private bool label_wrap = true;
  84. private bool multiselect = true;
  85. private bool redraw = true;
  86. private bool scrollable = true;
  87. private SelectedIndexCollection selected_indices;
  88. private SelectedListViewItemCollection selected_items;
  89. private SortOrder sort_order = SortOrder.None;
  90. private ImageList state_image_list;
  91. private bool updating = false;
  92. private View view = View.LargeIcon;
  93. private int layout_wd; // We might draw more than our client area
  94. private int layout_ht; // therefore we need to have these two.
  95. //private TextBox editor; // Used for editing an item text
  96. private ScrollBar h_scroll; // used for scrolling horizontally
  97. private ScrollBar v_scroll; // used for scrolling vertically
  98. private int h_marker; // Position markers for scrolling
  99. private int v_marker;
  100. // internal variables
  101. internal ImageList large_image_list;
  102. internal ImageList small_image_list;
  103. // FIXME: find a way to calculate width properly in all the
  104. // cases. MS has a value, even if there is no text, no icon
  105. // no checkbox. default ht is Font.ht.
  106. internal Size text_size = Size.Empty;
  107. #region Events
  108. public event LabelEditEventHandler AfterLabelEdit;
  109. [Browsable (false)]
  110. [EditorBrowsable (EditorBrowsableState.Never)]
  111. public new event EventHandler BackgroundImageChanged;
  112. public event LabelEditEventHandler BeforeLabelEdit;
  113. public event ColumnClickEventHandler ColumnClick;
  114. public event EventHandler ItemActivate;
  115. public event ItemCheckEventHandler ItemCheck;
  116. public event ItemDragEventHandler ItemDrag;
  117. [Browsable (false)]
  118. [EditorBrowsable (EditorBrowsableState.Never)]
  119. public new event PaintEventHandler Paint;
  120. public event EventHandler SelectedIndexChanged;
  121. [Browsable (false)]
  122. [EditorBrowsable (EditorBrowsableState.Never)]
  123. public new event EventHandler TextChanged;
  124. #endregion // Events
  125. #region Public Constructors
  126. public ListView ()
  127. {
  128. background_color = SystemColors.Window;
  129. checked_indices = new CheckedIndexCollection (this);
  130. checked_items = new CheckedListViewItemCollection (this);
  131. columns = new ColumnHeaderCollection (this);
  132. foreground_color = SystemColors.WindowText;
  133. items = new ListViewItemCollection (this);
  134. selected_indices = new SelectedIndexCollection (this);
  135. selected_items = new SelectedListViewItemCollection (this);
  136. // we are mostly scrollable
  137. h_scroll = new HScrollBar ();
  138. v_scroll = new VScrollBar ();
  139. h_marker = v_marker = 0;
  140. // scroll bars are disabled initially
  141. h_scroll.Visible = false;
  142. //h_scroll.Dock = DockStyle.Bottom;
  143. h_scroll.Scroll += new ScrollEventHandler(HorizontalScroller);
  144. v_scroll.Visible = false;
  145. //v_scroll.Dock = DockStyle.Right;
  146. v_scroll.Scroll += new ScrollEventHandler(VerticalScroller);
  147. child_controls.Add (this.v_scroll);
  148. child_controls.Add (this.h_scroll);
  149. // event handlers
  150. base.Paint += new PaintEventHandler (ListView_Paint);
  151. }
  152. #endregion // Public Constructors
  153. #region Private Internal Properties
  154. internal Size CheckBoxSize {
  155. get {
  156. if (this.check_boxes) {
  157. if (this.state_image_list != null)
  158. return this.state_image_list.ImageSize;
  159. else
  160. return ThemeEngine.Current.CheckBoxSize;
  161. }
  162. return Size.Empty;
  163. }
  164. }
  165. #endregion // Private Internal Properties
  166. #region Protected Properties
  167. protected override CreateParams CreateParams {
  168. get { return base.CreateParams; }
  169. }
  170. protected override Size DefaultSize {
  171. get { return ThemeEngine.Current.ListViewDefaultSize; }
  172. }
  173. #endregion // Protected Properties
  174. #region Public Instance Properties
  175. [DefaultValue (ItemActivation.Standard)]
  176. public ItemActivation Activation {
  177. get { return activation; }
  178. set { activation = value; }
  179. }
  180. [DefaultValue (ListViewAlignment.Top)]
  181. [Localizable (true)]
  182. public ListViewAlignment Alignment {
  183. get { return alignment; }
  184. set { alignment = value; }
  185. }
  186. [DefaultValue (false)]
  187. public bool AllowColumnReorder {
  188. get { return allow_column_reorder; }
  189. set { allow_column_reorder = value; }
  190. }
  191. [DefaultValue (true)]
  192. public bool AutoArrange {
  193. get { return auto_arrange; }
  194. set { auto_arrange = value; }
  195. }
  196. public override Color BackColor {
  197. get { return base.BackColor; }
  198. set { base.BackColor = value; }
  199. }
  200. [Browsable (false)]
  201. [EditorBrowsable (EditorBrowsableState.Never)]
  202. public override Image BackgroundImage {
  203. get { return background_image; }
  204. set {
  205. if (value == background_image)
  206. return;
  207. background_image = value;
  208. if (BackgroundImageChanged != null)
  209. BackgroundImageChanged (this, new EventArgs ());
  210. }
  211. }
  212. [DefaultValue (BorderStyle.Fixed3D)]
  213. [DispId (-504)]
  214. public BorderStyle BorderStyle {
  215. get { return border_style; }
  216. set { border_style = value; }
  217. }
  218. [DefaultValue (false)]
  219. public bool CheckBoxes {
  220. get { return check_boxes; }
  221. set { check_boxes = value; }
  222. }
  223. [Browsable (false)]
  224. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  225. public CheckedIndexCollection CheckedIndices {
  226. get { return checked_indices; }
  227. }
  228. [Browsable (false)]
  229. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  230. public CheckedListViewItemCollection CheckedItems {
  231. get { return checked_items; }
  232. }
  233. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  234. [Localizable (true)]
  235. [MergableProperty (false)]
  236. public ColumnHeaderCollection Columns {
  237. get { return columns; }
  238. }
  239. [Browsable (false)]
  240. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  241. public ListViewItem FocusedItem {
  242. get { return focused_item; }
  243. }
  244. public override Color ForeColor {
  245. get { return base.ForeColor; }
  246. set { base.ForeColor = value; }
  247. }
  248. [DefaultValue (false)]
  249. public bool FullRowSelect {
  250. get { return full_row_select; }
  251. set { full_row_select = value; }
  252. }
  253. [DefaultValue (false)]
  254. public bool GridLines {
  255. get { return grid_lines; }
  256. set { grid_lines = value; }
  257. }
  258. [DefaultValue (ColumnHeaderStyle.Clickable)]
  259. public ColumnHeaderStyle HeaderStyle {
  260. get { return header_style; }
  261. set { header_style = value; }
  262. }
  263. [DefaultValue (true)]
  264. public bool HideSelection {
  265. get { return hide_selection; }
  266. set { hide_selection = value; }
  267. }
  268. [DefaultValue (false)]
  269. public bool HoverSelection {
  270. get { return hover_selection; }
  271. set { hover_selection = value; }
  272. }
  273. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  274. [Localizable (true)]
  275. [MergableProperty (false)]
  276. public ListViewItemCollection Items {
  277. get { return items; }
  278. }
  279. [DefaultValue (false)]
  280. public bool LabelEdit {
  281. get { return label_edit; }
  282. set { label_edit = value; }
  283. }
  284. [DefaultValue (false)]
  285. [Localizable (true)]
  286. public bool LabelWrap {
  287. get { return label_wrap; }
  288. set { label_wrap = value; }
  289. }
  290. [DefaultValue (null)]
  291. public ImageList LargeImageList {
  292. get { return large_image_list; }
  293. set { large_image_list = value; }
  294. }
  295. [Browsable (false)]
  296. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  297. public IComparer ListViewItemSorter {
  298. get { return item_sorter; }
  299. set { item_sorter = value; }
  300. }
  301. [DefaultValue (true)]
  302. public bool MultiSelect {
  303. get { return multiselect; }
  304. set { multiselect = value; }
  305. }
  306. [DefaultValue (true)]
  307. public bool Scrollable {
  308. get { return scrollable; }
  309. set { scrollable = value; }
  310. }
  311. [Browsable (false)]
  312. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  313. public SelectedIndexCollection SelectedIndices {
  314. get { return selected_indices; }
  315. }
  316. [Browsable (false)]
  317. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  318. public SelectedListViewItemCollection SelectedItems {
  319. get { return selected_items; }
  320. }
  321. [DefaultValue (null)]
  322. public ImageList SmallImageList {
  323. get { return small_image_list; }
  324. set { small_image_list = value; }
  325. }
  326. [DefaultValue (SortOrder.None)]
  327. public SortOrder Sorting {
  328. get { return sort_order; }
  329. set { sort_order = value; }
  330. }
  331. [DefaultValue (null)]
  332. public ImageList StateImageList {
  333. get { return state_image_list; }
  334. set { state_image_list = value; }
  335. }
  336. [Bindable (false)]
  337. [Browsable (false)]
  338. [EditorBrowsable (EditorBrowsableState.Never)]
  339. public override string Text {
  340. get { return text; }
  341. set {
  342. if (value == text)
  343. return;
  344. text = value;
  345. if (TextChanged != null)
  346. TextChanged (this, new EventArgs ());
  347. }
  348. }
  349. [Browsable (false)]
  350. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  351. public ListViewItem TopItem {
  352. get { return items [0]; }
  353. }
  354. [DefaultValue (View.LargeIcon)]
  355. public View View {
  356. get { return view; }
  357. set { view = value; }
  358. }
  359. #endregion // Public Instance Properties
  360. #region Internal Methods Properties
  361. internal int TotalWidth {
  362. get { return this.layout_wd; }
  363. }
  364. internal int TotalHeight {
  365. get { return this.layout_ht; }
  366. }
  367. internal void Redraw (bool recalculate)
  368. {
  369. if (recalculate)
  370. CalculateListView ();
  371. redraw = true;
  372. Refresh ();
  373. }
  374. internal Size GetChildColumnSize (int index)
  375. {
  376. Size ret_size = Size.Empty;
  377. ColumnHeader col = this.columns [index];
  378. if (col.Width == -2) { // autosize = max(items, columnheader)
  379. Size size = Size.Ceiling (this.DeviceContext.MeasureString
  380. (col.Text, this.Font));
  381. ret_size = BiggestItem (index);
  382. if (size.Width > ret_size.Width)
  383. ret_size = size;
  384. }
  385. else { // -1 and all the values < -2 are put under one category
  386. ret_size = BiggestItem (index);
  387. // fall back to empty columns' width if no subitem is available for a column
  388. if (ret_size.IsEmpty) {
  389. ret_size.Width = ThemeEngine.Current.EmptyColumnWidth;
  390. if (col.Text.Length > 0)
  391. ret_size.Height = Size.Ceiling (this.DeviceContext.MeasureString
  392. (col.Text, this.Font)).Height;
  393. else
  394. ret_size.Height = this.Font.Height;
  395. }
  396. }
  397. // adjust the size for icon and checkbox for 0th column
  398. if (index == 0) {
  399. ret_size.Width += (this.CheckBoxSize.Width + 2);
  400. if (this.small_image_list != null)
  401. ret_size.Width += this.small_image_list.ImageSize.Width;
  402. }
  403. return ret_size;
  404. }
  405. // Returns the size of biggest item text in a column.
  406. private Size BiggestItem (int col)
  407. {
  408. Size temp = Size.Empty;
  409. Size ret_size = Size.Empty;
  410. // 0th column holds the item text, we check the size of
  411. // the various subitems falling in that column and get
  412. // the biggest one's size.
  413. foreach (ListViewItem item in items) {
  414. if (col >= item.SubItems.Count)
  415. continue;
  416. temp = Size.Ceiling (this.DeviceContext.MeasureString
  417. (item.SubItems [col].Text, this.Font));
  418. if (temp.Width > ret_size.Width)
  419. ret_size = temp;
  420. }
  421. return ret_size;
  422. }
  423. // Sets the size of the biggest item text as per the view
  424. private void CalcTextSize ()
  425. {
  426. // clear the old value
  427. text_size = Size.Empty;
  428. if (items.Count == 0)
  429. text_size = Size.Empty;
  430. text_size = BiggestItem (0);
  431. if (view == View.LargeIcon && this.label_wrap) {
  432. Size temp = Size.Empty;
  433. if (this.check_boxes)
  434. temp.Width += 2 * this.CheckBoxSize.Width;
  435. if (large_image_list != null)
  436. temp.Width += large_image_list.ImageSize.Width;
  437. if (temp.Width == 0)
  438. temp.Width = 43;
  439. // wrapping is done for two lines only
  440. if (text_size.Width > temp.Width) {
  441. text_size.Width = temp.Width;
  442. text_size.Height *= 2;
  443. }
  444. }
  445. else if (view == View.List) {
  446. // in list view max text shown in determined by the
  447. // control width, even if scolling is enabled.
  448. int max_wd = this.Width - (this.CheckBoxSize.Width - 2);
  449. if (this.small_image_list != null)
  450. max_wd -= this.small_image_list.ImageSize.Width;
  451. if (text_size.Width > max_wd)
  452. text_size.Width = max_wd;
  453. }
  454. // we do the default settings, if we have got 0's
  455. if (text_size.Height <= 0)
  456. text_size.Height = this.Font.Height;
  457. if (text_size.Width <= 0)
  458. text_size.Width = this.Width;
  459. }
  460. // Sets the location of every item on
  461. // the ListView as per the view
  462. private void CalculateListView ()
  463. {
  464. int current_pos = 0; // our position marker
  465. CalcTextSize ();
  466. switch (view) {
  467. case View.Details:
  468. int ht = this.Font.Height;
  469. if (columns.Count > 0) {
  470. foreach (ColumnHeader col in columns) {
  471. col.X = current_pos;
  472. col.Y = 0;
  473. col.CalcColumnHeader ();
  474. current_pos += col.Wd;
  475. Console.WriteLine ("col: " + col.column_rect);
  476. }
  477. this.layout_wd = current_pos;
  478. // set the position marker for placing items
  479. // vertically down
  480. current_pos = ht;
  481. }
  482. if (items.Count > 0) {
  483. foreach (ListViewItem item in items) {
  484. item.location.X = 0;
  485. item.location.Y = current_pos;
  486. item.CalcListViewItem ();
  487. current_pos += item.entire_rect.Height;
  488. }
  489. this.layout_ht = current_pos;
  490. }
  491. break;
  492. case View.LargeIcon:
  493. // take care of alignment, autoarrange too
  494. break;
  495. case View.List:
  496. break;
  497. case View.SmallIcon:
  498. // take care of alignment, autoarrange too
  499. break;
  500. }
  501. if (this.scrollable) {
  502. this.CreateBuffers (this.layout_wd, this.layout_ht);
  503. if (this.layout_wd > this.Width)
  504. this.h_scroll.Visible = true;
  505. if (this.layout_ht > this.Height)
  506. this.v_scroll.Visible = true;
  507. if (this.h_scroll.Visible) {
  508. this.h_scroll.Height = 15;
  509. this.h_scroll.Location = new Point (0, this.Height
  510. - this.h_scroll.Height);
  511. this.h_scroll.Minimum = 0;
  512. this.h_scroll.Maximum = this.layout_wd - this.Width;
  513. // if v_scroll is visible, adjust the maximum of the
  514. // h_scroll to account for the width of v_scroll
  515. if (this.v_scroll.Visible) {
  516. this.h_scroll.Maximum += this.v_scroll.Width;
  517. this.h_scroll.Width = this.Width - this.v_scroll.Width;
  518. }
  519. else
  520. this.h_scroll.Width = this.Width;
  521. this.h_scroll.LargeChange = this.Width;
  522. this.h_scroll.SmallChange = this.Font.Height;
  523. // adjust the maximum value to make the raw Max value attainable
  524. this.h_scroll.Maximum += this.h_scroll.LargeChange;
  525. }
  526. // vertical scrollbar
  527. if (this.v_scroll.Visible) {
  528. this.v_scroll.Width = 15;
  529. this.v_scroll.Location = new Point (this.Width
  530. - this.v_scroll.Width, 0);
  531. this.v_scroll.Minimum = 0;
  532. this.v_scroll.Maximum = this.layout_ht - this.Height;
  533. // if h_scroll is visible, adjust the maximum of the
  534. // v_scroll to account for the height of h_scroll
  535. if (this.h_scroll.Visible) {
  536. this.v_scroll.Maximum += this.h_scroll.Height;
  537. this.v_scroll.Height = this.Height - this.h_scroll.Height;
  538. }
  539. else
  540. this.v_scroll.Height = this.Height;
  541. this.v_scroll.LargeChange = this.Height;
  542. this.v_scroll.SmallChange = this.Font.Height;
  543. // adjust the maximum value to make the raw Max value attainable
  544. this.v_scroll.Maximum += this.v_scroll.LargeChange;
  545. }
  546. }
  547. else {
  548. this.h_scroll.Visible = false;
  549. this.v_scroll.Visible = false;
  550. }
  551. }
  552. private void ListView_Paint (object sender, PaintEventArgs pe)
  553. {
  554. if (this.Width <= 0 || this.Height <= 0 || this.Visible == false)
  555. return;
  556. if (redraw) {
  557. ThemeEngine.Current.DrawListView (this.DeviceContext,
  558. pe.ClipRectangle, this);
  559. redraw = false;
  560. }
  561. // We paint on the screen as per the location set
  562. // by the two scrollbars. In case of details view
  563. // since column headers can scroll only horizontally
  564. // and items can scroll in both directions, paiting is
  565. // done separtely for the column header and the items.
  566. Rectangle srcRect = pe.ClipRectangle;
  567. Rectangle dstRect = pe.ClipRectangle;
  568. if (scrollable) {
  569. srcRect.X = h_marker;
  570. srcRect.Y = v_marker;
  571. }
  572. // paint the column headers
  573. if (this.view == View.Details && this.Columns.Count > 0 && v_marker > 0) {
  574. int col_ht = this.Columns [0].Ht;
  575. // move the source rect by the amount of horizontal scrolling
  576. // done so far.
  577. Rectangle headerSrc = new Rectangle (h_marker, 0, this.Width, col_ht);
  578. // dest rect is always stable at 0,0
  579. Rectangle headerDst = new Rectangle (0, 0, this.Width, col_ht);
  580. pe.Graphics.DrawImage (this.ImageBuffer, headerDst,
  581. headerSrc, GraphicsUnit.Pixel);
  582. // item painting starts below header. both the rects should
  583. // exclude the column header region.
  584. srcRect.Y += col_ht;
  585. dstRect.Y += col_ht;
  586. }
  587. // paint the items
  588. pe.Graphics.DrawImage (this.ImageBuffer, dstRect,
  589. srcRect, GraphicsUnit.Pixel);
  590. if (Paint != null)
  591. Paint (this, pe);
  592. }
  593. private void HorizontalScroller (object sender, ScrollEventArgs se)
  594. {
  595. Point loc;
  596. h_marker = se.NewValue;
  597. foreach (ListViewItem item in this.Items) {
  598. loc = item.checkbox.Location;
  599. loc.X -= h_marker;
  600. item.checkbox.Location = loc;
  601. }
  602. // no need to paint when we reach the end
  603. if (se.Type != ScrollEventType.EndScroll)
  604. this.Refresh ();
  605. }
  606. private void VerticalScroller (object sender, ScrollEventArgs se)
  607. {
  608. v_marker = se.NewValue;
  609. // no need to paint when we reach the end
  610. if (se.Type != ScrollEventType.EndScroll)
  611. this.Refresh ();
  612. }
  613. #endregion // Internal Methods Properties
  614. #region Protected Methods
  615. protected override void CreateHandle ()
  616. {
  617. base.CreateHandle ();
  618. }
  619. protected override void Dispose (bool disposing)
  620. {
  621. Clear ();
  622. }
  623. protected override bool IsInputKey (Keys keyData)
  624. {
  625. return base.IsInputKey (keyData);
  626. }
  627. protected virtual void OnAfterLabelEdit (LabelEditEventArgs e)
  628. {
  629. if (AfterLabelEdit != null)
  630. AfterLabelEdit (this, e);
  631. }
  632. protected virtual void OnBeforeLabelEdit (LabelEditEventArgs e)
  633. {
  634. if (BeforeLabelEdit != null)
  635. BeforeLabelEdit (this, e);
  636. }
  637. protected virtual void OnColumnClick (ColumnClickEventArgs e)
  638. {
  639. if (ColumnClick != null)
  640. ColumnClick (this, e);
  641. }
  642. protected override void OnEnabledChanged (EventArgs e)
  643. {
  644. base.OnEnabledChanged (e);
  645. }
  646. protected override void OnFontChanged (EventArgs e)
  647. {
  648. base.OnFontChanged (e);
  649. }
  650. protected override void OnHandleCreated (EventArgs e)
  651. {
  652. base.OnHandleCreated (e);
  653. }
  654. protected override void OnHandleDestroyed (EventArgs e)
  655. {
  656. base.OnHandleDestroyed (e);
  657. }
  658. protected virtual void OnItemActivate (EventArgs e)
  659. {
  660. if (ItemActivate != null)
  661. ItemActivate (this, e);
  662. }
  663. protected virtual void OnItemCheck (ItemCheckEventArgs ice)
  664. {
  665. if (ItemCheck != null)
  666. ItemCheck (this, ice);
  667. }
  668. protected virtual void OnItemDrag (ItemDragEventArgs e)
  669. {
  670. if (ItemDrag != null)
  671. ItemDrag (this, e);
  672. }
  673. protected virtual void OnSelectedIndexChanged (EventArgs e)
  674. {
  675. if (SelectedIndexChanged != null)
  676. SelectedIndexChanged (this, e);
  677. }
  678. protected override void OnSystemColorsChanged (EventArgs e)
  679. {
  680. base.OnSystemColorsChanged (e);
  681. }
  682. protected void RealizeProperties ()
  683. {
  684. // FIXME: TODO
  685. }
  686. protected void UpdateExtendedStyles ()
  687. {
  688. // FIXME: TODO
  689. }
  690. protected override void WndProc (ref Message m)
  691. {
  692. base.WndProc (ref m);
  693. }
  694. #endregion // Protected Methods
  695. #region Public Instance Methods
  696. public void ArrangeIcons ()
  697. {
  698. ArrangeIcons (ListViewAlignment.Default);
  699. }
  700. public void ArrangeIcons (ListViewAlignment alignment)
  701. {
  702. // Icons are arranged only if view is set to LargeIcon or SmallIcon
  703. if (view == View.LargeIcon || view == View.SmallIcon) {
  704. // FIXME: TODO
  705. }
  706. }
  707. public void BeginUpdate ()
  708. {
  709. // flag to avoid painting
  710. updating = true;
  711. }
  712. public void Clear ()
  713. {
  714. columns.Clear ();
  715. items.Clear ();
  716. }
  717. public void EndUpdate ()
  718. {
  719. // flag to avoid painting
  720. updating = false;
  721. }
  722. public void EnsureVisible (int index)
  723. {
  724. // FIXME: TODO
  725. }
  726. public ListViewItem GetItemAt (int x, int y)
  727. {
  728. foreach (ListViewItem item in items) {
  729. if (item.Bounds.Contains (x, y))
  730. return item;
  731. }
  732. return null;
  733. }
  734. public Rectangle GetItemRect (int index)
  735. {
  736. return GetItemRect (index, ItemBoundsPortion.Entire);
  737. }
  738. public Rectangle GetItemRect (int index, ItemBoundsPortion portion)
  739. {
  740. if (index < 0 || index >= items.Count)
  741. throw new IndexOutOfRangeException ("Invalid Index");
  742. return items [index].GetBounds (portion);
  743. }
  744. public void Sort ()
  745. {
  746. if (sort_order != SortOrder.None)
  747. items.list.Sort (item_sorter);
  748. if (sort_order == SortOrder.Descending)
  749. items.list.Reverse ();
  750. this.Redraw (true);
  751. }
  752. public override string ToString ()
  753. {
  754. int count = this.Items.Count;
  755. if (count == 0)
  756. return string.Format ("System.Windows.Forms.ListView, Items.Count: 0");
  757. else
  758. return string.Format ("System.Windows.Forms.ListView, Items.Count: {0}, Items[0]: {1}", count, this.Items [0].ToString ());
  759. }
  760. #endregion // Public Instance Methods
  761. #region Subclasses
  762. public class CheckedIndexCollection : IList, ICollection, IEnumerable
  763. {
  764. internal ArrayList list;
  765. private ListView owner;
  766. #region Public Constructor
  767. public CheckedIndexCollection (ListView owner)
  768. {
  769. list = new ArrayList ();
  770. this.owner = owner;
  771. }
  772. #endregion // Public Constructor
  773. #region Public Properties
  774. [Browsable (false)]
  775. public virtual int Count {
  776. get { return list.Count; }
  777. }
  778. public virtual bool IsReadOnly {
  779. get { return true; }
  780. }
  781. public int this [int index] {
  782. get {
  783. if (index < 0 || index >= list.Count)
  784. throw new ArgumentOutOfRangeException ("Index out of range.");
  785. return (int) list [index];
  786. }
  787. }
  788. bool ICollection.IsSynchronized {
  789. get { return list.IsSynchronized; }
  790. }
  791. object ICollection.SyncRoot {
  792. get { return list.SyncRoot; }
  793. }
  794. bool IList.IsFixedSize {
  795. get { return list.IsFixedSize; }
  796. }
  797. object IList.this [int index] {
  798. get { return this [index]; }
  799. set { throw new NotSupportedException ("SetItem operation is not supported."); }
  800. }
  801. #endregion // Public Properties
  802. #region Public Methods
  803. public bool Contains (int checkedIndex)
  804. {
  805. return list.Contains (checkedIndex);
  806. }
  807. public virtual IEnumerator GetEnumerator ()
  808. {
  809. return list.GetEnumerator ();
  810. }
  811. void ICollection.CopyTo (Array dest, int index)
  812. {
  813. list.CopyTo (dest, index);
  814. }
  815. int IList.Add (object value)
  816. {
  817. throw new NotSupportedException ("Add operation is not supported.");
  818. }
  819. void IList.Clear ()
  820. {
  821. throw new NotSupportedException ("Clear operation is not supported.");
  822. }
  823. bool IList.Contains (object checkedIndex)
  824. {
  825. return list.Contains (checkedIndex);
  826. }
  827. int IList.IndexOf (object checkedIndex)
  828. {
  829. return list.IndexOf (checkedIndex);
  830. }
  831. void IList.Insert (int index, object value)
  832. {
  833. throw new NotSupportedException ("Insert operation is not supported.");
  834. }
  835. void IList.Remove (object value)
  836. {
  837. throw new NotSupportedException ("Remove operation is not supported.");
  838. }
  839. void IList.RemoveAt (int index)
  840. {
  841. throw new NotSupportedException ("RemoveAt operation is not supported.");
  842. }
  843. public int IndexOf (int checkedIndex)
  844. {
  845. return list.IndexOf (checkedIndex);
  846. }
  847. #endregion // Public Methods
  848. } // CheckedIndexCollection
  849. public class CheckedListViewItemCollection : IList, ICollection, IEnumerable
  850. {
  851. internal ArrayList list;
  852. private ListView owner;
  853. #region Public Constructor
  854. public CheckedListViewItemCollection (ListView owner)
  855. {
  856. list = new ArrayList ();
  857. this.owner = owner;
  858. }
  859. #endregion // Public Constructor
  860. #region Public Properties
  861. [Browsable (false)]
  862. public virtual int Count {
  863. get { return list.Count; }
  864. }
  865. public virtual bool IsReadOnly {
  866. get { return true; }
  867. }
  868. public ListViewItem this [int index] {
  869. get {
  870. if (index < 0 || index >= list.Count)
  871. throw new ArgumentOutOfRangeException ("Index out of range.");
  872. return (ListViewItem) list [index];
  873. }
  874. }
  875. bool ICollection.IsSynchronized {
  876. get { return list.IsSynchronized; }
  877. }
  878. object ICollection.SyncRoot {
  879. get { return list.SyncRoot; }
  880. }
  881. bool IList.IsFixedSize {
  882. get { return list.IsFixedSize; }
  883. }
  884. object IList.this [int index] {
  885. get { return this [index]; }
  886. set { throw new NotSupportedException ("SetItem operation is not supported."); }
  887. }
  888. #endregion // Public Properties
  889. #region Public Methods
  890. public bool Contains (ListViewItem item)
  891. {
  892. return list.Contains (item);
  893. }
  894. public virtual void CopyTo (Array dest, int index)
  895. {
  896. list.CopyTo (dest, index);
  897. }
  898. public virtual IEnumerator GetEnumerator ()
  899. {
  900. return list.GetEnumerator ();
  901. }
  902. int IList.Add (object value)
  903. {
  904. throw new NotSupportedException ("Add operation is not supported.");
  905. }
  906. void IList.Clear ()
  907. {
  908. throw new NotSupportedException ("Clear operation is not supported.");
  909. }
  910. bool IList.Contains (object item)
  911. {
  912. return list.Contains (item);
  913. }
  914. int IList.IndexOf (object item)
  915. {
  916. return list.IndexOf (item);
  917. }
  918. void IList.Insert (int index, object value)
  919. {
  920. throw new NotSupportedException ("Insert operation is not supported.");
  921. }
  922. void IList.Remove (object value)
  923. {
  924. throw new NotSupportedException ("Remove operation is not supported.");
  925. }
  926. void IList.RemoveAt (int index)
  927. {
  928. throw new NotSupportedException ("RemoveAt operation is not supported.");
  929. }
  930. public int IndexOf (ListViewItem item)
  931. {
  932. return list.IndexOf (item);
  933. }
  934. #endregion // Public Methods
  935. } // CheckedListViewItemCollection
  936. public class ColumnHeaderCollection : IList, ICollection, IEnumerable
  937. {
  938. internal ArrayList list;
  939. private ListView owner;
  940. #region Public Constructor
  941. public ColumnHeaderCollection (ListView owner)
  942. {
  943. list = new ArrayList ();
  944. this.owner = owner;
  945. }
  946. #endregion // Public Constructor
  947. #region Public Properties
  948. [Browsable (false)]
  949. public virtual int Count {
  950. get { return list.Count; }
  951. }
  952. public virtual bool IsReadOnly {
  953. get { return false; }
  954. }
  955. public virtual ColumnHeader this [int index] {
  956. get {
  957. if (index < 0 || index >= list.Count)
  958. throw new ArgumentOutOfRangeException ("Index out of range.");
  959. return (ColumnHeader) list [index];
  960. }
  961. }
  962. bool ICollection.IsSynchronized {
  963. get { return list.IsSynchronized; }
  964. }
  965. object ICollection.SyncRoot {
  966. get { return list.SyncRoot; }
  967. }
  968. bool IList.IsFixedSize {
  969. get { return list.IsFixedSize; }
  970. }
  971. object IList.this [int index] {
  972. get { return this [index]; }
  973. set { throw new NotSupportedException ("SetItem operation is not supported."); }
  974. }
  975. #endregion // Public Properties
  976. #region Public Methods
  977. public virtual int Add (ColumnHeader value)
  978. {
  979. return list.Add (value);
  980. }
  981. public virtual ColumnHeader Add (string str, int width, HorizontalAlignment textAlign)
  982. {
  983. ColumnHeader colHeader = new ColumnHeader (this.owner, str, textAlign, width);
  984. this.Add (colHeader);
  985. return colHeader;
  986. }
  987. public virtual void AddRange (ColumnHeader [] values)
  988. {
  989. foreach (ColumnHeader colHeader in values)
  990. this.Add (colHeader);
  991. }
  992. public virtual void Clear ()
  993. {
  994. list.Clear ();
  995. }
  996. public bool Contains (ColumnHeader value)
  997. {
  998. return list.Contains (value);
  999. }
  1000. public virtual IEnumerator GetEnumerator ()
  1001. {
  1002. return list.GetEnumerator ();
  1003. }
  1004. void ICollection.CopyTo (Array dest, int index)
  1005. {
  1006. list.CopyTo (dest, index);
  1007. }
  1008. int IList.Add (object value)
  1009. {
  1010. if (! (value is ColumnHeader)) {
  1011. throw new ArgumentException ("Not of type ColumnHeader", "value");
  1012. }
  1013. return this.Add ((ColumnHeader) value);
  1014. }
  1015. bool IList.Contains (object value)
  1016. {
  1017. if (! (value is ColumnHeader)) {
  1018. throw new ArgumentException ("Not of type ColumnHeader", "value");
  1019. }
  1020. return this.Contains ((ColumnHeader) value);
  1021. }
  1022. int IList.IndexOf (object value)
  1023. {
  1024. if (! (value is ColumnHeader)) {
  1025. throw new ArgumentException ("Not of type ColumnHeader", "value");
  1026. }
  1027. return this.IndexOf ((ColumnHeader) value);
  1028. }
  1029. void IList.Insert (int index, object value)
  1030. {
  1031. if (! (value is ColumnHeader)) {
  1032. throw new ArgumentException ("Not of type ColumnHeader", "value");
  1033. }
  1034. this.Insert (index, (ColumnHeader) value);
  1035. }
  1036. void IList.Remove (object value)
  1037. {
  1038. if (! (value is ColumnHeader)) {
  1039. throw new ArgumentException ("Not of type ColumnHeader", "value");
  1040. }
  1041. this.Remove ((ColumnHeader) value);
  1042. }
  1043. public int IndexOf (ColumnHeader value)
  1044. {
  1045. return list.IndexOf (value);
  1046. }
  1047. public void Insert (int index, ColumnHeader value)
  1048. {
  1049. if (index < 0 || index >= list.Count)
  1050. throw new ArgumentOutOfRangeException ("Index out of range.");
  1051. list.Insert (index, value);
  1052. }
  1053. public void Insert (int index, string str, int width, HorizontalAlignment textAlign)
  1054. {
  1055. ColumnHeader colHeader = new ColumnHeader (this.owner, str, textAlign, width);
  1056. this.Insert (index, colHeader);
  1057. }
  1058. public virtual void Remove (ColumnHeader column)
  1059. {
  1060. list.Remove (column);
  1061. }
  1062. public virtual void RemoveAt (int index)
  1063. {
  1064. if (index < 0 || index >= list.Count)
  1065. throw new ArgumentOutOfRangeException ("Index out of range.");
  1066. list.RemoveAt (index);
  1067. }
  1068. #endregion // Public Methods
  1069. } // ColumnHeaderCollection
  1070. public class ListViewItemCollection : IList, ICollection, IEnumerable
  1071. {
  1072. internal ArrayList list;
  1073. private ListView owner;
  1074. #region Public Constructor
  1075. public ListViewItemCollection (ListView owner)
  1076. {
  1077. list = new ArrayList ();
  1078. this.owner = owner;
  1079. }
  1080. #endregion // Public Constructor
  1081. #region Public Properties
  1082. [Browsable (false)]
  1083. public virtual int Count {
  1084. get { return list.Count; }
  1085. }
  1086. public virtual bool IsReadOnly {
  1087. get { return false; }
  1088. }
  1089. public virtual ListViewItem this [int displayIndex] {
  1090. get {
  1091. if (displayIndex < 0 || displayIndex >= list.Count)
  1092. throw new ArgumentOutOfRangeException ("Index out of range.");
  1093. return (ListViewItem) list [displayIndex];
  1094. }
  1095. set {
  1096. if (displayIndex < 0 || displayIndex >= list.Count)
  1097. throw new ArgumentOutOfRangeException ("Index out of range.");
  1098. value.owner = this.owner;
  1099. list [displayIndex] = value;
  1100. owner.Redraw (true);
  1101. }
  1102. }
  1103. bool ICollection.IsSynchronized {
  1104. get { return list.IsSynchronized; }
  1105. }
  1106. object ICollection.SyncRoot {
  1107. get { return list.SyncRoot; }
  1108. }
  1109. bool IList.IsFixedSize {
  1110. get { return list.IsFixedSize; }
  1111. }
  1112. object IList.this [int index] {
  1113. get { return this [index]; }
  1114. set {
  1115. if (value is ListViewItem)
  1116. this [index] = (ListViewItem) value;
  1117. else
  1118. this [index] = new ListViewItem (value.ToString ());
  1119. }
  1120. }
  1121. #endregion // Public Properties
  1122. #region Public Methods
  1123. public virtual ListViewItem Add (ListViewItem value)
  1124. {
  1125. value.owner = this.owner;
  1126. list.Add (value);
  1127. if (owner.Sorting != SortOrder.None)
  1128. owner.Sort ();
  1129. owner.Redraw (true);
  1130. return value;
  1131. }
  1132. public virtual ListViewItem Add (string text)
  1133. {
  1134. ListViewItem item = new ListViewItem (text);
  1135. return this.Add (item);
  1136. }
  1137. public virtual ListViewItem Add (string text, int imageIndex)
  1138. {
  1139. ListViewItem item = new ListViewItem (text, imageIndex);
  1140. return this.Add (item);
  1141. }
  1142. public void AddRange (ListViewItem [] values)
  1143. {
  1144. list.Clear ();
  1145. foreach (ListViewItem item in values) {
  1146. item.owner = this.owner;
  1147. list.Add (item);
  1148. }
  1149. if (owner.Sorting != SortOrder.None)
  1150. owner.Sort ();
  1151. owner.Redraw (true);
  1152. }
  1153. public virtual void Clear ()
  1154. {
  1155. list.Clear ();
  1156. }
  1157. public bool Contains (ListViewItem item)
  1158. {
  1159. return list.Contains (item);
  1160. }
  1161. public virtual void CopyTo (Array dest, int index)
  1162. {
  1163. list.CopyTo (dest, index);
  1164. }
  1165. public virtual IEnumerator GetEnumerator ()
  1166. {
  1167. return list.GetEnumerator ();
  1168. }
  1169. int IList.Add (object item)
  1170. {
  1171. int result;
  1172. ListViewItem li;
  1173. if (item is ListViewItem)
  1174. li = (ListViewItem) item;
  1175. else
  1176. li = new ListViewItem (item.ToString ());
  1177. li.owner = this.owner;
  1178. result = list.Add (li);
  1179. owner.Redraw (true);
  1180. return result;
  1181. }
  1182. bool IList.Contains (object item)
  1183. {
  1184. return list.Contains (item);
  1185. }
  1186. int IList.IndexOf (object item)
  1187. {
  1188. return list.IndexOf (item);
  1189. }
  1190. void IList.Insert (int index, object item)
  1191. {
  1192. if (item is ListViewItem)
  1193. this.Insert (index, (ListViewItem) item);
  1194. else
  1195. this.Insert (index, item.ToString ());
  1196. }
  1197. void IList.Remove (object item)
  1198. {
  1199. if (list.Contains (item)) {
  1200. list.Remove (item);
  1201. owner.Redraw (true);
  1202. }
  1203. }
  1204. public int IndexOf (ListViewItem item)
  1205. {
  1206. return list.IndexOf (item);
  1207. }
  1208. public ListViewItem Insert (int index, ListViewItem item)
  1209. {
  1210. if (index < 0 || index >= list.Count)
  1211. throw new ArgumentOutOfRangeException ("Index out of range.");
  1212. item.owner = this.owner;
  1213. list.Insert (index, item);
  1214. owner.Redraw (true);
  1215. return item;
  1216. }
  1217. public ListViewItem Insert (int index, string text)
  1218. {
  1219. return this.Insert (index, new ListViewItem (text));
  1220. }
  1221. public ListViewItem Insert (int index, string text, int imageIndex)
  1222. {
  1223. return this.Insert (index, new ListViewItem (text, imageIndex));
  1224. }
  1225. public virtual void Remove (ListViewItem item)
  1226. {
  1227. if (list.Contains (item)) {
  1228. list.Remove (item);
  1229. owner.Redraw (true);
  1230. }
  1231. }
  1232. public virtual void RemoveAt (int index)
  1233. {
  1234. if (index < 0 || index >= list.Count)
  1235. throw new ArgumentOutOfRangeException ("Index out of range.");
  1236. list.RemoveAt (index);
  1237. owner.Redraw (false);
  1238. }
  1239. #endregion // Public Methods
  1240. } // ListViewItemCollection
  1241. public class SelectedIndexCollection : IList, ICollection, IEnumerable
  1242. {
  1243. internal ArrayList list;
  1244. private ListView owner;
  1245. #region Public Constructor
  1246. public SelectedIndexCollection (ListView owner)
  1247. {
  1248. list = new ArrayList ();
  1249. this.owner = owner;
  1250. }
  1251. #endregion // Public Constructor
  1252. #region Public Properties
  1253. [Browsable (false)]
  1254. public virtual int Count {
  1255. get { return list.Count; }
  1256. }
  1257. public virtual bool IsReadOnly {
  1258. get { return true; }
  1259. }
  1260. public int this [int index] {
  1261. get {
  1262. if (index < 0 || index >= list.Count)
  1263. throw new ArgumentOutOfRangeException ("Index out of range.");
  1264. return (int) list [index];
  1265. }
  1266. }
  1267. bool ICollection.IsSynchronized {
  1268. get { return list.IsSynchronized; }
  1269. }
  1270. object ICollection.SyncRoot {
  1271. get { return list.SyncRoot; }
  1272. }
  1273. bool IList.IsFixedSize {
  1274. get { return list.IsFixedSize; }
  1275. }
  1276. object IList.this [int index] {
  1277. get { return this [index]; }
  1278. set { throw new NotSupportedException ("SetItem operation is not supported."); }
  1279. }
  1280. #endregion // Public Properties
  1281. #region Public Methods
  1282. public bool Contains (int selectedIndex)
  1283. {
  1284. return list.Contains (selectedIndex);
  1285. }
  1286. public virtual void CopyTo (Array dest, int index)
  1287. {
  1288. list.CopyTo (dest, index);
  1289. }
  1290. public virtual IEnumerator GetEnumerator ()
  1291. {
  1292. return list.GetEnumerator ();
  1293. }
  1294. int IList.Add (object value)
  1295. {
  1296. throw new NotSupportedException ("Add operation is not supported.");
  1297. }
  1298. void IList.Clear ()
  1299. {
  1300. throw new NotSupportedException ("Clear operation is not supported.");
  1301. }
  1302. bool IList.Contains (object selectedIndex)
  1303. {
  1304. return list.Contains (selectedIndex);
  1305. }
  1306. int IList.IndexOf (object selectedIndex)
  1307. {
  1308. return list.IndexOf (selectedIndex);
  1309. }
  1310. void IList.Insert (int index, object value)
  1311. {
  1312. throw new NotSupportedException ("Insert operation is not supported.");
  1313. }
  1314. void IList.Remove (object value)
  1315. {
  1316. throw new NotSupportedException ("Remove operation is not supported.");
  1317. }
  1318. void IList.RemoveAt (int index)
  1319. {
  1320. throw new NotSupportedException ("RemoveAt operation is not supported.");
  1321. }
  1322. public int IndexOf (int selectedIndex)
  1323. {
  1324. return list.IndexOf (selectedIndex);
  1325. }
  1326. #endregion // Public Methods
  1327. } // SelectedIndexCollection
  1328. public class SelectedListViewItemCollection : IList, ICollection, IEnumerable
  1329. {
  1330. internal ArrayList list;
  1331. private ListView owner;
  1332. #region Public Constructor
  1333. public SelectedListViewItemCollection (ListView owner)
  1334. {
  1335. list = new ArrayList ();
  1336. this.owner = owner;
  1337. }
  1338. #endregion // Public Constructor
  1339. #region Public Properties
  1340. [Browsable (false)]
  1341. public virtual int Count {
  1342. get { return list.Count; }
  1343. }
  1344. public virtual bool IsReadOnly {
  1345. get { return true; }
  1346. }
  1347. public ListViewItem this [int index] {
  1348. get {
  1349. if (index < 0 || index >= list.Count)
  1350. throw new ArgumentOutOfRangeException ("Index out of range.");
  1351. return (ListViewItem) list [index];
  1352. }
  1353. }
  1354. bool ICollection.IsSynchronized {
  1355. get { return list.IsSynchronized; }
  1356. }
  1357. object ICollection.SyncRoot {
  1358. get { return list.SyncRoot; }
  1359. }
  1360. bool IList.IsFixedSize {
  1361. get { return list.IsFixedSize; }
  1362. }
  1363. object IList.this [int index] {
  1364. get { return this [index]; }
  1365. set { throw new NotSupportedException ("SetItem operation is not supported."); }
  1366. }
  1367. #endregion // Public Properties
  1368. #region Public Methods
  1369. public virtual void Clear ()
  1370. {
  1371. list.Clear ();
  1372. }
  1373. public bool Contains (ListViewItem item)
  1374. {
  1375. return list.Contains (item);
  1376. }
  1377. public virtual void CopyTo (Array dest, int index)
  1378. {
  1379. list.CopyTo (dest, index);
  1380. }
  1381. public virtual IEnumerator GetEnumerator ()
  1382. {
  1383. return list.GetEnumerator ();
  1384. }
  1385. int IList.Add (object value)
  1386. {
  1387. throw new NotSupportedException ("Add operation is not supported.");
  1388. }
  1389. bool IList.Contains (object item)
  1390. {
  1391. return list.Contains (item);
  1392. }
  1393. int IList.IndexOf (object item)
  1394. {
  1395. return list.IndexOf (item);
  1396. }
  1397. void IList.Insert (int index, object value)
  1398. {
  1399. throw new NotSupportedException ("Insert operation is not supported.");
  1400. }
  1401. void IList.Remove (object value)
  1402. {
  1403. throw new NotSupportedException ("Remove operation is not supported.");
  1404. }
  1405. void IList.RemoveAt (int index)
  1406. {
  1407. throw new NotSupportedException ("RemoveAt operation is not supported.");
  1408. }
  1409. public int IndexOf (ListViewItem item)
  1410. {
  1411. return list.IndexOf (item);
  1412. }
  1413. #endregion // Public Methods
  1414. } // SelectedListViewItemCollection
  1415. #endregion // Subclasses
  1416. }
  1417. }