ListViewItem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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: ListViewItem.cs,v $
  28. // Revision 1.1 2004/09/30 13:24:45 ravindra
  29. // Initial implementation.
  30. //
  31. //
  32. // NOT COMPLETE
  33. //
  34. using System;
  35. using System.Collections;
  36. using System.ComponentModel;
  37. using System.Drawing;
  38. using System.Runtime.Serialization;
  39. namespace System.Windows.Forms
  40. {
  41. [Serializable]
  42. public class ListViewItem : ICloneable, ISerializable
  43. {
  44. #region Instance Variables
  45. private Color backColor;
  46. private Font font = ThemeEngine.Current.DefaultFont;
  47. private Color foreColor;
  48. private int imageIndex = -1;
  49. private bool isChecked = false;
  50. private bool isFocused = false;
  51. internal ListView owner;
  52. private bool selected;
  53. private int stateImageIndex = -1;
  54. private ListViewSubItemCollection subItems;
  55. private object tag;
  56. private string text;
  57. private bool useItemStyle = true;
  58. #endregion Instance Variables
  59. #region Public Constructors
  60. public ListViewItem ()
  61. {
  62. this.subItems = new ListViewSubItemCollection (this);
  63. }
  64. public ListViewItem (string text) : this (text, -1)
  65. {
  66. }
  67. public ListViewItem (string [] items) : this (items, -1)
  68. {
  69. }
  70. public ListViewItem (ListViewItem.ListViewSubItem [] subItems, int imageIndex)
  71. {
  72. this.subItems = new ListViewSubItemCollection (this);
  73. this.subItems.AddRange (subItems);
  74. this.imageIndex = imageIndex;
  75. }
  76. public ListViewItem (string text, int imageIndex)
  77. {
  78. this.text = text;
  79. this.imageIndex = imageIndex;
  80. this.subItems = new ListViewSubItemCollection (this);
  81. }
  82. public ListViewItem (string [] items, int imageIndex)
  83. {
  84. this.subItems = new ListViewSubItemCollection (this);
  85. this.subItems.AddRange (items);
  86. this.imageIndex = imageIndex;
  87. }
  88. public ListViewItem (string [] items, int imageIndex, Color foreColor,
  89. Color backColor, Font font)
  90. {
  91. this.subItems = new ListViewSubItemCollection (this);
  92. this.subItems.AddRange (items);
  93. this.imageIndex = imageIndex;
  94. this.foreColor = foreColor;
  95. this.backColor = backColor;
  96. this.font = font;
  97. }
  98. #endregion // Public Constructors
  99. #region Public Instance Properties
  100. public Color BackColor {
  101. get { return backColor; }
  102. set { this.backColor = value; }
  103. }
  104. public Rectangle Bounds {
  105. get {
  106. return GetBounds (ItemBoundsPortion.Entire);
  107. }
  108. }
  109. public bool Checked {
  110. get { return isChecked; }
  111. set { isChecked = value; }
  112. }
  113. public bool Focused {
  114. get { return isFocused; }
  115. set { isFocused = value; }
  116. }
  117. public Font Font {
  118. get { return font; }
  119. set { font = value; }
  120. }
  121. public Color ForeColor {
  122. get { return foreColor; }
  123. set { foreColor = value; }
  124. }
  125. public int ImageIndex {
  126. get { return imageIndex; }
  127. set {
  128. if (value < -1)
  129. throw new ArgumentException ("Invalid ImageIndex. It must be greater than or equal to -1.");
  130. imageIndex = value;
  131. }
  132. }
  133. public ImageList ImageList {
  134. get {
  135. if (owner == null)
  136. return null;
  137. else if (owner.View == View.LargeIcon)
  138. return owner.largeImageList;
  139. else
  140. return owner.smallImageList;
  141. }
  142. }
  143. public int Index {
  144. get {
  145. if (owner == null)
  146. return -1;
  147. else
  148. return owner.Items.IndexOf (this);
  149. }
  150. }
  151. public ListView ListView {
  152. get { return owner; }
  153. }
  154. public bool Selected {
  155. get { return selected; }
  156. set {
  157. if (value != selected) {
  158. selected = value;
  159. if (owner != null && owner.MultiSelect) {
  160. if (selected)
  161. //do we need !owner.SelectedItems.Contains (this))
  162. owner.SelectedItems.list.Add (this);
  163. else
  164. owner.SelectedItems.list.Remove (this);
  165. }
  166. }
  167. }
  168. }
  169. public int StateImageIndex {
  170. get { return stateImageIndex; }
  171. set {
  172. if (value < -1 || value > 14)
  173. throw new ArgumentOutOfRangeException ("Invalid StateImageIndex. It must be in the range of [-1, 14].");
  174. stateImageIndex = value;
  175. }
  176. }
  177. public ListViewSubItemCollection SubItems {
  178. get { return subItems; }
  179. }
  180. public object Tag {
  181. get { return tag; }
  182. set { tag = value; }
  183. }
  184. public string Text {
  185. get { return text; }
  186. set { text = value; }
  187. }
  188. public bool UseItemStyleForSubItems {
  189. get { return useItemStyle; }
  190. set { useItemStyle = value; }
  191. }
  192. #endregion // Public Instance Properties
  193. #region Public Instance Methods
  194. public void BeginEdit ()
  195. {
  196. // FIXME: TODO
  197. // if (owner != null && owner.LabelEdit)
  198. // allow editing
  199. // else
  200. // throw new InvalidOperationException ();
  201. }
  202. public virtual object Clone ()
  203. {
  204. // FIXME: TODO
  205. return new ListViewItem ();
  206. }
  207. public virtual void EnsureVisible ()
  208. {
  209. // FIXME: TODO
  210. }
  211. public Rectangle GetBounds (ItemBoundsPortion portion)
  212. {
  213. // FIXME: TODO
  214. return new Rectangle (0, 0, 0, 0);
  215. }
  216. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  217. {
  218. // FIXME: TODO
  219. }
  220. public virtual void Remove ()
  221. {
  222. if (owner != null)
  223. owner.Items.Remove (this);
  224. owner = null;
  225. }
  226. public override string ToString ()
  227. {
  228. return string.Format ("ListViewItem: {{0}}", text);
  229. }
  230. #endregion // Public Instance Methods
  231. #region Protected Methods
  232. protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
  233. {
  234. // FIXME: TODO
  235. }
  236. protected virtual void Serialize (SerializationInfo info, StreamingContext context)
  237. {
  238. // FIXME: TODO
  239. }
  240. #endregion // Protected Methods
  241. #region Subclasses
  242. [Serializable]
  243. public class ListViewSubItem
  244. {
  245. private Color backColor;
  246. private Font font;
  247. private Color foreColor;
  248. internal ListViewItem owner;
  249. private string text;
  250. #region Public Constructors
  251. public ListViewSubItem ()
  252. {
  253. }
  254. public ListViewSubItem (ListViewItem owner, string text)
  255. {
  256. this.owner = owner;
  257. this.text = text;
  258. }
  259. public ListViewSubItem (ListViewItem owner, string text, Color foreColor, Color backColor, Font font)
  260. {
  261. this.owner = owner;
  262. this.text = text;
  263. this.foreColor = foreColor;
  264. this.backColor = backColor;
  265. this.font = font;
  266. }
  267. #endregion // Public Constructors
  268. #region Public Instance Properties
  269. public Color BackColor {
  270. get { return backColor; }
  271. set { backColor = value; }
  272. }
  273. public Font Font {
  274. get { return font; }
  275. set { font = value; }
  276. }
  277. public Color ForeColor {
  278. get { return foreColor; }
  279. set { foreColor = value; }
  280. }
  281. public string Text {
  282. get { return text; }
  283. set { text = value; }
  284. }
  285. #endregion // Public Instance Properties
  286. #region Public Methods
  287. public void ResetStyle ()
  288. {
  289. font = ThemeEngine.Current.DefaultFont;
  290. backColor = ThemeEngine.Current.DefaultControlBackColor;
  291. foreColor = ThemeEngine.Current.DefaultControlForeColor;
  292. }
  293. public override string ToString ()
  294. {
  295. return string.Format ("ListViewSubItem {{0}}", text);
  296. }
  297. #endregion // Public Methods
  298. }
  299. public class ListViewSubItemCollection : IList, ICollection, IEnumerable
  300. {
  301. private ArrayList list;
  302. internal ListViewItem owner;
  303. #region Public Constructors
  304. public ListViewSubItemCollection (ListViewItem owner)
  305. {
  306. this.owner = owner;
  307. this.list = new ArrayList ();
  308. }
  309. #endregion // Public Constructors
  310. #region Public Properties
  311. public virtual int Count {
  312. get { return list.Count; }
  313. }
  314. public virtual bool IsReadOnly {
  315. get { return false; }
  316. }
  317. public ListViewSubItem this [int index] {
  318. get { return (ListViewSubItem) list [index]; }
  319. set {
  320. value.owner = this.owner;
  321. list [index] = value;
  322. }
  323. }
  324. bool ICollection.IsSynchronized {
  325. get { return list.IsSynchronized; }
  326. }
  327. object ICollection.SyncRoot {
  328. get { return list.SyncRoot; }
  329. }
  330. bool IList.IsFixedSize {
  331. get { return list.IsFixedSize; }
  332. }
  333. object IList.this [int index] {
  334. get { return this [index]; }
  335. set {
  336. if (! (value is ListViewSubItem))
  337. throw new ArgumentException("Not of type ListViewSubItem", "value");
  338. this [index] = (ListViewSubItem) value;
  339. }
  340. }
  341. #endregion // Public Properties
  342. #region Public Methods
  343. public ListViewSubItem Add (ListViewSubItem item)
  344. {
  345. item.owner = this.owner;
  346. list.Add (item);
  347. return item;
  348. }
  349. public ListViewSubItem Add (string text)
  350. {
  351. ListViewSubItem item = new ListViewSubItem (this.owner, text);
  352. list.Add (item);
  353. return item;
  354. }
  355. public ListViewSubItem Add (string text, Color foreColor, Color backColor, Font font)
  356. {
  357. ListViewSubItem item = new ListViewSubItem (this.owner, text, foreColor, backColor, font);
  358. list.Add (item);
  359. return item;
  360. }
  361. public void AddRange (ListViewSubItem [] items)
  362. {
  363. list.Clear ();
  364. foreach (ListViewSubItem item in items)
  365. this.Add (item);
  366. }
  367. public void AddRange (string [] items)
  368. {
  369. list.Clear ();
  370. foreach (string item in items)
  371. this.Add (item);
  372. }
  373. public void AddRange (string [] items, Color foreColor, Color backColor, Font font)
  374. {
  375. list.Clear ();
  376. foreach (string item in items)
  377. this.Add (item, foreColor, backColor, font);
  378. }
  379. public virtual void Clear ()
  380. {
  381. list.Clear ();
  382. }
  383. public bool Contains (ListViewSubItem item)
  384. {
  385. return list.Contains (item);
  386. }
  387. public virtual IEnumerator GetEnumerator ()
  388. {
  389. return list.GetEnumerator ();
  390. }
  391. void ICollection.CopyTo (Array dest, int index)
  392. {
  393. list.CopyTo (dest, index);
  394. }
  395. int IList.Add (object item)
  396. {
  397. if (! (item is ListViewSubItem)) {
  398. throw new ArgumentException("Not of type ListViewSubItem", "item");
  399. }
  400. ListViewSubItem subItem = (ListViewSubItem) item;
  401. subItem.owner = this.owner;
  402. return list.Add (subItem);
  403. }
  404. bool IList.Contains (object subItem)
  405. {
  406. if (! (subItem is ListViewSubItem)) {
  407. throw new ArgumentException("Not of type ListViewSubItem", "subItem");
  408. }
  409. return this.Contains ((ListViewSubItem) subItem);
  410. }
  411. int IList.IndexOf (object subItem)
  412. {
  413. if (! (subItem is ListViewSubItem)) {
  414. throw new ArgumentException("Not of type ListViewSubItem", "subItem");
  415. }
  416. return this.IndexOf ((ListViewSubItem) subItem);
  417. }
  418. void IList.Insert (int index, object item)
  419. {
  420. if (! (item is ListViewSubItem)) {
  421. throw new ArgumentException("Not of type ListViewSubItem", "item");
  422. }
  423. this.Insert (index, (ListViewSubItem) item);
  424. }
  425. void IList.Remove (object item)
  426. {
  427. if (! (item is ListViewSubItem)) {
  428. throw new ArgumentException("Not of type ListViewSubItem", "item");
  429. }
  430. this.Remove ((ListViewSubItem) item);
  431. }
  432. public int IndexOf (ListViewSubItem subItem)
  433. {
  434. return list.IndexOf (subItem);
  435. }
  436. public void Insert (int index, ListViewSubItem item)
  437. {
  438. item.owner = this.owner;
  439. list.Insert (index, item);
  440. }
  441. public void Remove (ListViewSubItem item)
  442. {
  443. list.Remove (item);
  444. }
  445. public virtual void RemoveAt (int index)
  446. {
  447. list.RemoveAt (index);
  448. }
  449. #endregion // Public Methods
  450. }
  451. #endregion // Subclasses
  452. }
  453. }