ListView.cs 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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.1 $
  26. // $Modtime: $
  27. // $Log: ListView.cs,v $
  28. // Revision 1.1 2004/09/30 13:24:25 ravindra
  29. // Initial implementation.
  30. //
  31. //
  32. // NOT COMPLETE
  33. //
  34. using System;
  35. using System.ComponentModel;
  36. using System.Collections;
  37. using System.Drawing;
  38. namespace System.Windows.Forms
  39. {
  40. public class ListView : Control
  41. {
  42. private ItemActivation activation = ItemActivation.Standard;
  43. private ListViewAlignment alignment = ListViewAlignment.Top;
  44. private bool allowColumnReorder = false;
  45. private bool autoArrange = true;
  46. private BorderStyle borderStyle = BorderStyle.Fixed3D;
  47. private bool checkBoxes = false;
  48. private CheckedIndexCollection checkedIndices;
  49. private CheckedListViewItemCollection checkedItems;
  50. private ColumnHeaderCollection columns;
  51. private ListViewItem focusedItem;
  52. private bool fullRowSelect = false;
  53. private bool gridLines = false;
  54. private ColumnHeaderStyle headerStyle = ColumnHeaderStyle.Clickable;
  55. private bool hideSelection = true;
  56. private bool hoverSelection = false;
  57. private ListViewItemCollection items;
  58. private bool labelEdit = false;
  59. private bool labelWrap = true;
  60. internal ImageList largeImageList;
  61. private IComparer itemSorter;
  62. private bool multiselect = true;
  63. private bool redraw = true;
  64. private bool scrollable = true;
  65. private SelectedIndexCollection selectedIndices;
  66. private SelectedListViewItemCollection selectedItems;
  67. internal ImageList smallImageList;
  68. private SortOrder sortOrder = SortOrder.None;
  69. private ImageList stateImageList;
  70. private bool updating = false;
  71. private View view = View.LargeIcon;
  72. #region Events
  73. public event LabelEditEventHandler AfterLabelEdit;
  74. public new event EventHandler BackgroundImageChanged;
  75. public event LabelEditEventHandler BeforeLabelEdit;
  76. public event ColumnClickEventHandler ColumnClick;
  77. public event EventHandler ItemActivate;
  78. public event ItemCheckEventHandler ItemCheck;
  79. public event ItemDragEventHandler ItemDrag;
  80. public new event PaintEventHandler Paint;
  81. public event EventHandler SelectedIndexChanged;
  82. public new event EventHandler TextChanged;
  83. #endregion // Events
  84. #region Public Constructors
  85. public ListView ()
  86. {
  87. checkedIndices = new CheckedIndexCollection (this);
  88. checkedItems = new CheckedListViewItemCollection (this);
  89. columns = new ColumnHeaderCollection (this);
  90. items = new ListViewItemCollection (this);
  91. selectedIndices = new SelectedIndexCollection (this);
  92. selectedItems = new SelectedListViewItemCollection (this);
  93. }
  94. #endregion // Public Constructors
  95. #region Protected Properties
  96. protected override CreateParams CreateParams {
  97. get { return base.CreateParams; }
  98. }
  99. protected override Size DefaultSize {
  100. get { return new Size (121, 97); }
  101. }
  102. #endregion // Protected Properties
  103. #region Public Instance Properties
  104. public ItemActivation Activation {
  105. get { return activation; }
  106. set { activation = value; }
  107. }
  108. public ListViewAlignment Alignment {
  109. get { return alignment; }
  110. set { alignment = value; }
  111. }
  112. public bool AllowColumnReorder {
  113. get { return allowColumnReorder; }
  114. set { allowColumnReorder = value; }
  115. }
  116. public bool AutoArrange {
  117. get { return autoArrange; }
  118. set { autoArrange = value; }
  119. }
  120. public override Color BackColor {
  121. get { return base.BackColor; }
  122. set { base.BackColor = value; }
  123. }
  124. public override Image BackgroundImage {
  125. get { return background_image; }
  126. set {
  127. if (value == background_image)
  128. return;
  129. background_image = value;
  130. if (BackgroundImageChanged != null)
  131. BackgroundImageChanged (this, new EventArgs ());
  132. }
  133. }
  134. public BorderStyle BorderStyle {
  135. get { return borderStyle; }
  136. set { borderStyle = value; }
  137. }
  138. public bool CheckBoxes {
  139. get { return checkBoxes; }
  140. set { checkBoxes = value; }
  141. }
  142. public CheckedIndexCollection CheckedIndices {
  143. get { return checkedIndices; }
  144. }
  145. public CheckedListViewItemCollection CheckedItems {
  146. get { return checkedItems; }
  147. }
  148. public ColumnHeaderCollection Columns {
  149. get { return columns; }
  150. }
  151. public ListViewItem FocusedItem {
  152. get { return focusedItem; }
  153. }
  154. public override Color ForeColor {
  155. get { return base.ForeColor; }
  156. set { base.ForeColor = value; }
  157. }
  158. public bool FullRowSelect {
  159. get { return fullRowSelect; }
  160. set { fullRowSelect = value; }
  161. }
  162. public bool GridLines {
  163. get { return gridLines; }
  164. set { gridLines = value; }
  165. }
  166. public ColumnHeaderStyle HeaderStyle {
  167. get { return headerStyle; }
  168. set { headerStyle = value; }
  169. }
  170. public bool HideSelection {
  171. get { return hideSelection; }
  172. set { hideSelection = value; }
  173. }
  174. public bool HoverSelection {
  175. get { return hoverSelection; }
  176. set { hoverSelection = value; }
  177. }
  178. public ListViewItemCollection Items {
  179. get { return items; }
  180. }
  181. public bool LabelEdit {
  182. get { return labelEdit; }
  183. set { labelEdit = value; }
  184. }
  185. public bool LabelWrap {
  186. get { return labelWrap; }
  187. set { labelWrap = value; }
  188. }
  189. public ImageList LargeImageList {
  190. get { return largeImageList; }
  191. set { largeImageList = value; }
  192. }
  193. public IComparer ListViewItemSorter {
  194. get { return itemSorter; }
  195. set { itemSorter = value; }
  196. }
  197. public bool MultiSelect {
  198. get { return multiselect; }
  199. set { multiselect = value; }
  200. }
  201. public bool Scrollable {
  202. get { return scrollable; }
  203. set { scrollable = value; }
  204. }
  205. public SelectedIndexCollection SelectedIndices {
  206. get { return selectedIndices; }
  207. }
  208. public SelectedListViewItemCollection SelectedItems {
  209. get { return selectedItems; }
  210. }
  211. public ImageList SmallImageList {
  212. get { return smallImageList; }
  213. set { smallImageList = value; }
  214. }
  215. public SortOrder Sorting {
  216. get { return sortOrder; }
  217. set { sortOrder = value; }
  218. }
  219. public ImageList StateImageList {
  220. get { return stateImageList; }
  221. set { stateImageList = value; }
  222. }
  223. public override string Text {
  224. get { return text; }
  225. set {
  226. if (value == text)
  227. return;
  228. text = value;
  229. if (TextChanged != null)
  230. TextChanged (this, new EventArgs ());
  231. }
  232. }
  233. public ListViewItem TopItem {
  234. get { return items [0]; }
  235. }
  236. public View View {
  237. get { return view; }
  238. set { view = value; }
  239. }
  240. #endregion // Public Instance Properties
  241. #region Internal Methods
  242. internal void Redraw (bool recalculate)
  243. {
  244. if (recalculate)
  245. CalculateListView ();
  246. redraw = true;
  247. }
  248. private void CalculateListView ()
  249. {
  250. // FIXME: TODO
  251. }
  252. #endregion // Internal Methods
  253. #region Protected Methods
  254. protected override void CreateHandle ()
  255. {
  256. base.CreateHandle ();
  257. }
  258. protected override void Dispose (bool disposing)
  259. {
  260. Clear ();
  261. }
  262. protected override bool IsInputKey (Keys keyData)
  263. {
  264. return base.IsInputKey (keyData);
  265. }
  266. protected virtual void OnAfterLabelEdit (LabelEditEventArgs e)
  267. {
  268. if (AfterLabelEdit != null)
  269. AfterLabelEdit (this, e);
  270. }
  271. protected virtual void OnBeforeLabelEdit (LabelEditEventArgs e)
  272. {
  273. if (BeforeLabelEdit != null)
  274. BeforeLabelEdit (this, e);
  275. }
  276. protected virtual void OnColumnClick (ColumnClickEventArgs e)
  277. {
  278. if (ColumnClick != null)
  279. ColumnClick (this, e);
  280. }
  281. protected override void OnEnabledChanged (EventArgs e)
  282. {
  283. base.OnEnabledChanged (e);
  284. }
  285. protected override void OnFontChanged (EventArgs e)
  286. {
  287. base.OnFontChanged (e);
  288. }
  289. protected override void OnHandleCreated (EventArgs e)
  290. {
  291. base.OnHandleCreated (e);
  292. }
  293. protected override void OnHandleDestroyed (EventArgs e)
  294. {
  295. base.OnHandleDestroyed (e);
  296. }
  297. protected virtual void OnItemActivate (EventArgs e)
  298. {
  299. if (ItemActivate != null)
  300. ItemActivate (this, e);
  301. }
  302. protected virtual void OnItemCheck (ItemCheckEventArgs ice)
  303. {
  304. if (ItemCheck != null)
  305. ItemCheck (this, ice);
  306. }
  307. protected virtual void OnItemDrag (ItemDragEventArgs e)
  308. {
  309. if (ItemDrag != null)
  310. ItemDrag (this, e);
  311. }
  312. protected virtual void OnSelectedIndexChanged (EventArgs e)
  313. {
  314. if (SelectedIndexChanged != null)
  315. SelectedIndexChanged (this, e);
  316. }
  317. protected override void OnSystemColorsChanged (EventArgs e)
  318. {
  319. base.OnSystemColorsChanged (e);
  320. }
  321. protected void RealizeProperties ()
  322. {
  323. // FIXME: TODO
  324. }
  325. protected void UpdateExtendedStyles ()
  326. {
  327. // FIXME: TODO
  328. }
  329. protected override void WndProc (ref Message m)
  330. {
  331. base.WndProc (ref m);
  332. }
  333. #endregion // Protected Methods
  334. #region Public Instance Methods
  335. public void ArrangeIcons ()
  336. {
  337. ArrangeIcons (ListViewAlignment.Default);
  338. }
  339. public void ArrangeIcons (ListViewAlignment alignment)
  340. {
  341. // Icons are arranged only if view is set to LargeIcon or SmallIcon
  342. if (view == View.LargeIcon || view == View.SmallIcon) {
  343. // FIXME: TODO
  344. }
  345. }
  346. public void BeginUpdate ()
  347. {
  348. // flag to avoid painting
  349. updating = true;
  350. }
  351. public void Clear ()
  352. {
  353. columns.Clear ();
  354. items.Clear ();
  355. }
  356. public void EndUpdate ()
  357. {
  358. // flag to avoid painting
  359. updating = false;
  360. }
  361. public void EnsureVisible (int index)
  362. {
  363. // FIXME: TODO
  364. }
  365. public ListViewItem GetItemAt (int x, int y)
  366. {
  367. foreach (ListViewItem item in items) {
  368. if (item.Bounds.Contains (x, y))
  369. return item;
  370. }
  371. return null;
  372. }
  373. public Rectangle GetItemRect (int index)
  374. {
  375. return GetItemRect (index, ItemBoundsPortion.Entire);
  376. }
  377. public Rectangle GetItemRect (int index, ItemBoundsPortion portion)
  378. {
  379. if (index < 0 || index >= items.Count)
  380. throw new IndexOutOfRangeException ("Invalid Index");
  381. return items [index].GetBounds (portion);
  382. }
  383. public void Sort ()
  384. {
  385. if (sortOrder != SortOrder.None)
  386. items.list.Sort (itemSorter);
  387. if (sortOrder == SortOrder.Descending)
  388. items.list.Reverse ();
  389. }
  390. public override string ToString ()
  391. {
  392. int count = this.Items.Count;
  393. if (count == 0)
  394. return string.Format ("System.Windows.Forms.ListView, Items.Count: 0");
  395. else
  396. return string.Format ("System.Windows.Forms.ListView, Items.Count: {0}, Items[0]: {1}", count, this.Items [0].ToString ());
  397. }
  398. #endregion // Public Instance Methods
  399. #region Subclasses
  400. public class CheckedIndexCollection : IList, ICollection, IEnumerable
  401. {
  402. internal ArrayList list;
  403. private ListView owner;
  404. #region Public Constructor
  405. public CheckedIndexCollection (ListView owner)
  406. {
  407. list = new ArrayList ();
  408. this.owner = owner;
  409. }
  410. #endregion // Public Constructor
  411. #region Public Properties
  412. public virtual int Count {
  413. get { return list.Count; }
  414. }
  415. public virtual bool IsReadOnly {
  416. get { return true; }
  417. }
  418. public int this [int index] {
  419. get {
  420. if (index < 0 || index >= list.Count)
  421. throw new ArgumentOutOfRangeException ("Index out of range.");
  422. return (int) list [index];
  423. }
  424. }
  425. bool ICollection.IsSynchronized {
  426. get { return list.IsSynchronized; }
  427. }
  428. object ICollection.SyncRoot {
  429. get { return list.SyncRoot; }
  430. }
  431. bool IList.IsFixedSize {
  432. get { return list.IsFixedSize; }
  433. }
  434. object IList.this [int index] {
  435. get { return this [index]; }
  436. set { throw new NotSupportedException ("SetItem operation is not supported."); }
  437. }
  438. #endregion // Public Properties
  439. #region Public Methods
  440. public bool Contains (int checkedIndex)
  441. {
  442. return list.Contains (checkedIndex);
  443. }
  444. public virtual IEnumerator GetEnumerator ()
  445. {
  446. return list.GetEnumerator ();
  447. }
  448. void ICollection.CopyTo (Array dest, int index)
  449. {
  450. list.CopyTo (dest, index);
  451. }
  452. int IList.Add (object value)
  453. {
  454. throw new NotSupportedException ("Add operation is not supported.");
  455. }
  456. void IList.Clear ()
  457. {
  458. throw new NotSupportedException ("Clear operation is not supported.");
  459. }
  460. bool IList.Contains (object checkedIndex)
  461. {
  462. return list.Contains (checkedIndex);
  463. }
  464. int IList.IndexOf (object checkedIndex)
  465. {
  466. return list.IndexOf (checkedIndex);
  467. }
  468. void IList.Insert (int index, object value)
  469. {
  470. throw new NotSupportedException ("Insert operation is not supported.");
  471. }
  472. void IList.Remove (object value)
  473. {
  474. throw new NotSupportedException ("Remove operation is not supported.");
  475. }
  476. void IList.RemoveAt (int index)
  477. {
  478. throw new NotSupportedException ("RemoveAt operation is not supported.");
  479. }
  480. public int IndexOf (int checkedIndex)
  481. {
  482. return list.IndexOf (checkedIndex);
  483. }
  484. #endregion // Public Methods
  485. } // CheckedIndexCollection
  486. public class CheckedListViewItemCollection : IList, ICollection, IEnumerable
  487. {
  488. internal ArrayList list;
  489. private ListView owner;
  490. #region Public Constructor
  491. public CheckedListViewItemCollection (ListView owner)
  492. {
  493. list = new ArrayList ();
  494. this.owner = owner;
  495. }
  496. #endregion // Public Constructor
  497. #region Public Properties
  498. public virtual int Count {
  499. get { return list.Count; }
  500. }
  501. public virtual bool IsReadOnly {
  502. get { return true; }
  503. }
  504. public ListViewItem this [int index] {
  505. get {
  506. if (index < 0 || index >= list.Count)
  507. throw new ArgumentOutOfRangeException ("Index out of range.");
  508. return (ListViewItem) list [index];
  509. }
  510. }
  511. bool ICollection.IsSynchronized {
  512. get { return list.IsSynchronized; }
  513. }
  514. object ICollection.SyncRoot {
  515. get { return list.SyncRoot; }
  516. }
  517. bool IList.IsFixedSize {
  518. get { return list.IsFixedSize; }
  519. }
  520. object IList.this [int index] {
  521. get { return this [index]; }
  522. set { throw new NotSupportedException ("SetItem operation is not supported."); }
  523. }
  524. #endregion // Public Properties
  525. #region Public Methods
  526. public bool Contains (ListViewItem item)
  527. {
  528. return list.Contains (item);
  529. }
  530. public virtual void CopyTo (Array dest, int index)
  531. {
  532. list.CopyTo (dest, index);
  533. }
  534. public virtual IEnumerator GetEnumerator ()
  535. {
  536. return list.GetEnumerator ();
  537. }
  538. int IList.Add (object value)
  539. {
  540. throw new NotSupportedException ("Add operation is not supported.");
  541. }
  542. void IList.Clear ()
  543. {
  544. throw new NotSupportedException ("Clear operation is not supported.");
  545. }
  546. bool IList.Contains (object item)
  547. {
  548. return list.Contains (item);
  549. }
  550. int IList.IndexOf (object item)
  551. {
  552. return list.IndexOf (item);
  553. }
  554. void IList.Insert (int index, object value)
  555. {
  556. throw new NotSupportedException ("Insert operation is not supported.");
  557. }
  558. void IList.Remove (object value)
  559. {
  560. throw new NotSupportedException ("Remove operation is not supported.");
  561. }
  562. void IList.RemoveAt (int index)
  563. {
  564. throw new NotSupportedException ("RemoveAt operation is not supported.");
  565. }
  566. public int IndexOf (ListViewItem item)
  567. {
  568. return list.IndexOf (item);
  569. }
  570. #endregion // Public Methods
  571. } // CheckedListViewItemCollection
  572. public class ColumnHeaderCollection : IList, ICollection, IEnumerable
  573. {
  574. internal ArrayList list;
  575. private ListView owner;
  576. #region Public Constructor
  577. public ColumnHeaderCollection (ListView owner)
  578. {
  579. list = new ArrayList ();
  580. this.owner = owner;
  581. }
  582. #endregion // Public Constructor
  583. #region Public Properties
  584. public virtual int Count {
  585. get { return list.Count; }
  586. }
  587. public virtual bool IsReadOnly {
  588. get { return false; }
  589. }
  590. public virtual ColumnHeader this [int index] {
  591. get {
  592. if (index < 0 || index >= list.Count)
  593. throw new ArgumentOutOfRangeException ("Index out of range.");
  594. return (ColumnHeader) list [index];
  595. }
  596. }
  597. bool ICollection.IsSynchronized {
  598. get { return list.IsSynchronized; }
  599. }
  600. object ICollection.SyncRoot {
  601. get { return list.SyncRoot; }
  602. }
  603. bool IList.IsFixedSize {
  604. get { return list.IsFixedSize; }
  605. }
  606. object IList.this [int index] {
  607. get { return this [index]; }
  608. set { throw new NotSupportedException ("SetItem operation is not supported."); }
  609. }
  610. #endregion // Public Properties
  611. #region Public Methods
  612. public virtual int Add (ColumnHeader value)
  613. {
  614. return list.Add (value);
  615. }
  616. public virtual ColumnHeader Add (string str, int width, HorizontalAlignment textAlign)
  617. {
  618. ColumnHeader colHeader = new ColumnHeader (this.owner, str, textAlign, width);
  619. this.Add (colHeader);
  620. return colHeader;
  621. }
  622. public virtual void AddRange (ColumnHeader [] values)
  623. {
  624. foreach (ColumnHeader colHeader in values)
  625. this.Add (colHeader);
  626. }
  627. public virtual void Clear ()
  628. {
  629. list.Clear ();
  630. }
  631. public bool Contains (ColumnHeader value)
  632. {
  633. return list.Contains (value);
  634. }
  635. public virtual IEnumerator GetEnumerator ()
  636. {
  637. return list.GetEnumerator ();
  638. }
  639. void ICollection.CopyTo (Array dest, int index)
  640. {
  641. list.CopyTo (dest, index);
  642. }
  643. int IList.Add (object value)
  644. {
  645. if (! (value is ColumnHeader)) {
  646. throw new ArgumentException("Not of type ColumnHeader", "value");
  647. }
  648. return this.Add ((ColumnHeader) value);
  649. }
  650. bool IList.Contains (object value)
  651. {
  652. if (! (value is ColumnHeader)) {
  653. throw new ArgumentException("Not of type ColumnHeader", "value");
  654. }
  655. return this.Contains ((ColumnHeader) value);
  656. }
  657. int IList.IndexOf (object value)
  658. {
  659. if (! (value is ColumnHeader)) {
  660. throw new ArgumentException("Not of type ColumnHeader", "value");
  661. }
  662. return this.IndexOf ((ColumnHeader) value);
  663. }
  664. void IList.Insert (int index, object value)
  665. {
  666. if (! (value is ColumnHeader)) {
  667. throw new ArgumentException("Not of type ColumnHeader", "value");
  668. }
  669. this.Insert (index, (ColumnHeader) value);
  670. }
  671. void IList.Remove (object value)
  672. {
  673. if (! (value is ColumnHeader)) {
  674. throw new ArgumentException("Not of type ColumnHeader", "value");
  675. }
  676. this.Remove ((ColumnHeader) value);
  677. }
  678. public int IndexOf (ColumnHeader value)
  679. {
  680. return list.IndexOf (value);
  681. }
  682. public void Insert (int index, ColumnHeader value)
  683. {
  684. if (index < 0 || index >= list.Count)
  685. throw new ArgumentOutOfRangeException ("Index out of range.");
  686. list.Insert (index, value);
  687. }
  688. public void Insert (int index, string str, int width, HorizontalAlignment textAlign)
  689. {
  690. ColumnHeader colHeader = new ColumnHeader (this.owner, str, textAlign, width);
  691. this.Insert (index, colHeader);
  692. }
  693. public virtual void Remove (ColumnHeader column)
  694. {
  695. list.Remove (column);
  696. }
  697. public virtual void RemoveAt (int index)
  698. {
  699. if (index < 0 || index >= list.Count)
  700. throw new ArgumentOutOfRangeException ("Index out of range.");
  701. list.RemoveAt (index);
  702. }
  703. #endregion // Public Methods
  704. } // ColumnHeaderCollection
  705. public class ListViewItemCollection : IList, ICollection, IEnumerable
  706. {
  707. internal ArrayList list;
  708. private ListView owner;
  709. #region Public Constructor
  710. public ListViewItemCollection (ListView owner)
  711. {
  712. list = new ArrayList ();
  713. this.owner = owner;
  714. }
  715. #endregion // Public Constructor
  716. #region Public Properties
  717. public virtual int Count {
  718. get { return list.Count; }
  719. }
  720. public virtual bool IsReadOnly {
  721. get { return false; }
  722. }
  723. public virtual ListViewItem this [int displayIndex] {
  724. get {
  725. if (displayIndex < 0 || displayIndex >= list.Count)
  726. throw new ArgumentOutOfRangeException ("Index out of range.");
  727. return (ListViewItem) list [displayIndex];
  728. }
  729. set {
  730. if (displayIndex < 0 || displayIndex >= list.Count)
  731. throw new ArgumentOutOfRangeException ("Index out of range.");
  732. value.owner = this.owner;
  733. list [displayIndex] = value;
  734. owner.Redraw (true);
  735. }
  736. }
  737. bool ICollection.IsSynchronized {
  738. get { return list.IsSynchronized; }
  739. }
  740. object ICollection.SyncRoot {
  741. get { return list.SyncRoot; }
  742. }
  743. bool IList.IsFixedSize {
  744. get { return list.IsFixedSize; }
  745. }
  746. object IList.this [int index] {
  747. get { return this [index]; }
  748. set {
  749. if (value is ListViewItem)
  750. this [index] = (ListViewItem) value;
  751. else
  752. this [index] = new ListViewItem (value.ToString ());
  753. }
  754. }
  755. #endregion // Public Properties
  756. #region Public Methods
  757. public virtual ListViewItem Add (ListViewItem value)
  758. {
  759. value.owner = this.owner;
  760. list.Add (value);
  761. if (owner.Sorting != SortOrder.None)
  762. owner.Sort ();
  763. owner.Redraw (true);
  764. return value;
  765. }
  766. public virtual ListViewItem Add (string text)
  767. {
  768. ListViewItem item = new ListViewItem (text);
  769. return this.Add (item);
  770. }
  771. public virtual ListViewItem Add (string text, int imageIndex)
  772. {
  773. ListViewItem item = new ListViewItem (text, imageIndex);
  774. return this.Add (item);
  775. }
  776. public void AddRange (ListViewItem [] values)
  777. {
  778. list.Clear ();
  779. foreach (ListViewItem item in values) {
  780. item.owner = this.owner;
  781. list.Add (item);
  782. }
  783. if (owner.Sorting != SortOrder.None)
  784. owner.Sort ();
  785. owner.Redraw (true);
  786. }
  787. public virtual void Clear ()
  788. {
  789. list.Clear ();
  790. owner.Redraw (true);
  791. }
  792. public bool Contains (ListViewItem item)
  793. {
  794. return list.Contains (item);
  795. }
  796. public virtual void CopyTo (Array dest, int index)
  797. {
  798. list.CopyTo (dest, index);
  799. }
  800. public virtual IEnumerator GetEnumerator ()
  801. {
  802. return list.GetEnumerator ();
  803. }
  804. int IList.Add (object item)
  805. {
  806. int result;
  807. ListViewItem li;
  808. if (item is ListViewItem)
  809. li = (ListViewItem) item;
  810. else
  811. li = new ListViewItem (item.ToString ());
  812. li.owner = this.owner;
  813. result = list.Add (li);
  814. owner.Redraw (true);
  815. return result;
  816. }
  817. bool IList.Contains (object item)
  818. {
  819. return list.Contains (item);
  820. }
  821. int IList.IndexOf (object item)
  822. {
  823. return list.IndexOf (item);
  824. }
  825. void IList.Insert (int index, object item)
  826. {
  827. if (item is ListViewItem)
  828. this.Insert (index, (ListViewItem) item);
  829. else
  830. this.Insert (index, item.ToString ());
  831. }
  832. void IList.Remove (object item)
  833. {
  834. if (list.Contains (item)) {
  835. list.Remove (item);
  836. owner.Redraw (true);
  837. }
  838. }
  839. public int IndexOf (ListViewItem item)
  840. {
  841. return list.IndexOf (item);
  842. }
  843. public ListViewItem Insert (int index, ListViewItem item)
  844. {
  845. if (index < 0 || index >= list.Count)
  846. throw new ArgumentOutOfRangeException ("Index out of range.");
  847. item.owner = this.owner;
  848. list.Insert (index, item);
  849. owner.Redraw (true);
  850. return item;
  851. }
  852. public ListViewItem Insert (int index, string text)
  853. {
  854. return this.Insert (index, new ListViewItem (text));
  855. }
  856. public ListViewItem Insert (int index, string text, int imageIndex)
  857. {
  858. return this.Insert (index, new ListViewItem (text, imageIndex));
  859. }
  860. public virtual void Remove (ListViewItem item)
  861. {
  862. if (list.Contains (item)) {
  863. list.Remove (item);
  864. owner.Redraw (true);
  865. }
  866. }
  867. public virtual void RemoveAt (int index)
  868. {
  869. if (index < 0 || index >= list.Count)
  870. throw new ArgumentOutOfRangeException ("Index out of range.");
  871. list.RemoveAt (index);
  872. owner.Redraw (false);
  873. }
  874. #endregion // Public Methods
  875. } // ListViewItemCollection
  876. public class SelectedIndexCollection : IList, ICollection, IEnumerable
  877. {
  878. internal ArrayList list;
  879. private ListView owner;
  880. #region Public Constructor
  881. public SelectedIndexCollection (ListView owner)
  882. {
  883. list = new ArrayList ();
  884. this.owner = owner;
  885. }
  886. #endregion // Public Constructor
  887. #region Public Properties
  888. public virtual int Count {
  889. get { return list.Count; }
  890. }
  891. public virtual bool IsReadOnly {
  892. get { return true; }
  893. }
  894. public int this [int index] {
  895. get {
  896. if (index < 0 || index >= list.Count)
  897. throw new ArgumentOutOfRangeException ("Index out of range.");
  898. return (int) list [index];
  899. }
  900. }
  901. bool ICollection.IsSynchronized {
  902. get { return list.IsSynchronized; }
  903. }
  904. object ICollection.SyncRoot {
  905. get { return list.SyncRoot; }
  906. }
  907. bool IList.IsFixedSize {
  908. get { return list.IsFixedSize; }
  909. }
  910. object IList.this [int index] {
  911. get { return this [index]; }
  912. set { throw new NotSupportedException ("SetItem operation is not supported."); }
  913. }
  914. #endregion // Public Properties
  915. #region Public Methods
  916. public bool Contains (int selectedIndex)
  917. {
  918. return list.Contains (selectedIndex);
  919. }
  920. public virtual void CopyTo (Array dest, int index)
  921. {
  922. list.CopyTo (dest, index);
  923. }
  924. public virtual IEnumerator GetEnumerator ()
  925. {
  926. return list.GetEnumerator ();
  927. }
  928. int IList.Add (object value)
  929. {
  930. throw new NotSupportedException ("Add operation is not supported.");
  931. }
  932. void IList.Clear ()
  933. {
  934. throw new NotSupportedException ("Clear operation is not supported.");
  935. }
  936. bool IList.Contains (object selectedIndex)
  937. {
  938. return list.Contains (selectedIndex);
  939. }
  940. int IList.IndexOf (object selectedIndex)
  941. {
  942. return list.IndexOf (selectedIndex);
  943. }
  944. void IList.Insert (int index, object value)
  945. {
  946. throw new NotSupportedException ("Insert operation is not supported.");
  947. }
  948. void IList.Remove (object value)
  949. {
  950. throw new NotSupportedException ("Remove operation is not supported.");
  951. }
  952. void IList.RemoveAt (int index)
  953. {
  954. throw new NotSupportedException ("RemoveAt operation is not supported.");
  955. }
  956. public int IndexOf (int selectedIndex)
  957. {
  958. return list.IndexOf (selectedIndex);
  959. }
  960. #endregion // Public Methods
  961. } // SelectedIndexCollection
  962. public class SelectedListViewItemCollection : IList, ICollection, IEnumerable
  963. {
  964. internal ArrayList list;
  965. private ListView owner;
  966. #region Public Constructor
  967. public SelectedListViewItemCollection (ListView owner)
  968. {
  969. list = new ArrayList ();
  970. this.owner = owner;
  971. }
  972. #endregion // Public Constructor
  973. #region Public Properties
  974. public virtual int Count {
  975. get { return list.Count; }
  976. }
  977. public virtual bool IsReadOnly {
  978. get { return true; }
  979. }
  980. public ListViewItem this [int index] {
  981. get {
  982. if (index < 0 || index >= list.Count)
  983. throw new ArgumentOutOfRangeException ("Index out of range.");
  984. return (ListViewItem) list [index];
  985. }
  986. }
  987. bool ICollection.IsSynchronized {
  988. get { return list.IsSynchronized; }
  989. }
  990. object ICollection.SyncRoot {
  991. get { return list.SyncRoot; }
  992. }
  993. bool IList.IsFixedSize {
  994. get { return list.IsFixedSize; }
  995. }
  996. object IList.this [int index] {
  997. get { return this [index]; }
  998. set { throw new NotSupportedException ("SetItem operation is not supported."); }
  999. }
  1000. #endregion // Public Properties
  1001. #region Public Methods
  1002. public virtual void Clear ()
  1003. {
  1004. list.Clear ();
  1005. }
  1006. public bool Contains (ListViewItem item)
  1007. {
  1008. return list.Contains (item);
  1009. }
  1010. public virtual void CopyTo (Array dest, int index)
  1011. {
  1012. list.CopyTo (dest, index);
  1013. }
  1014. public virtual IEnumerator GetEnumerator ()
  1015. {
  1016. return list.GetEnumerator ();
  1017. }
  1018. int IList.Add (object value)
  1019. {
  1020. throw new NotSupportedException ("Add operation is not supported.");
  1021. }
  1022. bool IList.Contains (object item)
  1023. {
  1024. return list.Contains (item);
  1025. }
  1026. int IList.IndexOf (object item)
  1027. {
  1028. return list.IndexOf (item);
  1029. }
  1030. void IList.Insert (int index, object value)
  1031. {
  1032. throw new NotSupportedException ("Insert operation is not supported.");
  1033. }
  1034. void IList.Remove (object value)
  1035. {
  1036. throw new NotSupportedException ("Remove operation is not supported.");
  1037. }
  1038. void IList.RemoveAt (int index)
  1039. {
  1040. throw new NotSupportedException ("RemoveAt operation is not supported.");
  1041. }
  1042. public int IndexOf (ListViewItem item)
  1043. {
  1044. return list.IndexOf (item);
  1045. }
  1046. #endregion // Public Methods
  1047. } // SelectedListViewItemCollection
  1048. #endregion // Subclasses
  1049. }
  1050. }