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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004 Novell, Inc. (http://www.novell.com)
23 // Ravindra (rkumar@novell.com)
24 // Mike Kestner <mkestner@novell.com>
31 using System.Collections;
32 using System.ComponentModel;
34 using System.Runtime.Serialization;
36 namespace System.Windows.Forms
38 [DefaultProperty ("Text")]
39 [DesignTimeVisible (false)]
42 [TypeConverter (typeof (ListViewItemConverter))]
43 public class ListViewItem : ICloneable, ISerializable
45 #region Instance Variables
46 private int image_index = -1;
47 private bool is_checked = false;
48 private bool is_focused = false;
49 private int state_image_index = -1;
50 private ListViewSubItemCollection sub_items;
52 private bool use_item_style = true;
55 Rectangle checkbox_rect; // calculated by CalcListViewItem method
66 #endregion Instance Variables
68 #region Public Constructors
69 public ListViewItem ()
71 this.sub_items = new ListViewSubItemCollection (this);
72 this.sub_items.Add ("");
75 public ListViewItem (string text) : this (text, -1)
79 public ListViewItem (string [] items) : this (items, -1)
83 public ListViewItem (ListViewItem.ListViewSubItem [] subItems, int imageIndex)
85 this.sub_items = new ListViewSubItemCollection (this);
86 this.sub_items.AddRange (subItems);
87 this.image_index = imageIndex;
90 public ListViewItem (string text, int imageIndex)
92 this.image_index = imageIndex;
93 this.sub_items = new ListViewSubItemCollection (this);
94 this.sub_items.Add (text);
97 public ListViewItem (string [] items, int imageIndex)
99 this.sub_items = new ListViewSubItemCollection (this);
100 this.sub_items.AddRange (items);
101 this.image_index = imageIndex;
104 public ListViewItem (string [] items, int imageIndex, Color foreColor,
105 Color backColor, Font font)
107 this.sub_items = new ListViewSubItemCollection (this);
108 this.sub_items.AddRange (items);
109 this.image_index = imageIndex;
110 ForeColor = foreColor;
111 BackColor = backColor;
114 #endregion // Public Constructors
116 #region Public Instance Properties
117 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
118 public Color BackColor {
120 if (sub_items.Count > 0)
121 return sub_items[0].BackColor;
124 return owner.BackColor;
126 return ThemeEngine.Current.ColorWindow;
129 set { sub_items[0].BackColor = value; }
133 public Rectangle Bounds {
135 return GetBounds (ItemBoundsPortion.Entire);
139 [DefaultValue (false)]
140 [RefreshProperties (RefreshProperties.Repaint)]
141 public bool Checked {
142 get { return is_checked; }
144 if (is_checked == value)
151 if (owner.CheckedItems.Contains (this) == false) {
152 owner.CheckedItems.list.Add (this);
156 owner.CheckedItems.list.Remove (this);
166 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
167 public bool Focused {
168 get { return is_focused; }
170 if (is_focused == value)
182 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
187 else if (owner != null)
190 return ThemeEngine.Current.DefaultFont;
204 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
205 public Color ForeColor {
207 if (sub_items.Count > 0)
208 return sub_items[0].ForeColor;
211 return owner.ForeColor;
213 return ThemeEngine.Current.ColorWindowText;
215 set { sub_items[0].ForeColor = value; }
219 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
220 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design,
221 typeof (System.Drawing.Design.UITypeEditor))]
223 [TypeConverter (typeof (ImageIndexConverter))]
224 public int ImageIndex {
225 get { return image_index; }
228 throw new ArgumentException ("Invalid ImageIndex. It must be greater than or equal to -1.");
239 public ImageList ImageList {
243 else if (owner.View == View.LargeIcon)
244 return owner.large_image_list;
246 return owner.small_image_list;
256 return owner.Items.IndexOf (this);
261 public ListView ListView {
262 get { return owner; }
266 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
267 public bool Selected {
268 get { return selected; }
270 if (selected == value)
276 owner.UpdateSelection (this);
284 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design,
285 typeof (System.Drawing.Design.UITypeEditor))]
287 [TypeConverter (typeof (ImageIndexConverter))]
288 public int StateImageIndex {
289 get { return state_image_index; }
291 if (value < -1 || value > 14)
292 throw new ArgumentOutOfRangeException ("Invalid StateImageIndex. It must be in the range of [-1, 14].");
294 state_image_index = value;
298 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
299 public ListViewSubItemCollection SubItems {
300 get { return sub_items; }
304 [DefaultValue (null)]
305 [Localizable (false)]
306 [TypeConverter (typeof (StringConverter))]
313 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
316 if (this.sub_items.Count > 0)
317 return this.sub_items [0].Text;
322 if (sub_items [0].Text == value)
325 sub_items [0].Text = value;
333 [DefaultValue (true)]
334 public bool UseItemStyleForSubItems {
335 get { return use_item_style; }
336 set { use_item_style = value; }
338 #endregion // Public Instance Properties
340 #region Public Instance Methods
341 public void BeginEdit ()
344 // if (owner != null && owner.LabelEdit
345 // && owner.Activation == ItemActivation.Standard)
348 // throw new InvalidOperationException ();
351 public virtual object Clone ()
353 ListViewItem clone = new ListViewItem ();
354 clone.image_index = this.image_index;
355 clone.is_checked = this.is_checked;
356 clone.is_focused = this.is_focused;
357 clone.selected = this.selected;
358 clone.font = this.font;
359 clone.state_image_index = this.state_image_index;
360 clone.sub_items = new ListViewSubItemCollection (this);
362 foreach (ListViewSubItem subItem in this.sub_items)
363 clone.sub_items.Add (subItem.Text, subItem.ForeColor,
364 subItem.BackColor, subItem.Font);
365 clone.tag = this.tag;
366 clone.use_item_style = this.use_item_style;
372 public virtual void EnsureVisible ()
374 if (this.owner != null) {
375 owner.EnsureVisible (owner.Items.IndexOf (this));
379 public Rectangle GetBounds (ItemBoundsPortion portion)
382 return Rectangle.Empty;
387 case ItemBoundsPortion.Icon:
391 case ItemBoundsPortion.Label:
395 case ItemBoundsPortion.ItemOnly:
399 case ItemBoundsPortion.Entire:
401 rect.X -= owner.h_marker;
402 rect.Y -= owner.v_marker;
406 throw new ArgumentException ("Invalid value for portion.");
409 rect.X += bounds.X - owner.h_marker;
410 rect.Y += bounds.Y - owner.v_marker;
414 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
419 public virtual void Remove ()
422 owner.Items.Remove (this);
426 public override string ToString ()
428 return string.Format ("ListViewItem: {0}", this.Text);
430 #endregion // Public Instance Methods
432 #region Protected Methods
433 protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
438 protected virtual void Serialize (SerializationInfo info, StreamingContext context)
442 #endregion // Protected Methods
444 #region Private Internal Methods
445 internal Rectangle CheckRectReal {
447 Rectangle rect = checkbox_rect;
448 rect.X += bounds.X - owner.h_marker;
449 rect.Y += bounds.Y - owner.v_marker;
454 Rectangle CheckRect {
455 get { return this.checkbox_rect; }
459 get { return this.icon_rect; }
462 Rectangle LabelRect {
463 get { return this.label_rect; }
466 internal Point Location {
468 if (bounds.X == value.X && bounds.Y == value.Y)
471 Rectangle prev = Bounds;
475 if (prev != Rectangle.Empty)
476 owner.item_control.Invalidate (prev);
477 owner.item_control.Invalidate (Bounds);
482 internal ListView Owner {
494 private void Invalidate ()
496 if (owner == null || owner.item_control == null)
499 owner.item_control.Invalidate (Bounds);
502 internal void Layout ()
506 Size text_size = owner.text_size;
508 checkbox_rect = Rectangle.Empty;
509 if (owner.CheckBoxes)
510 checkbox_rect.Size = owner.CheckBoxSize;
512 switch (owner.View) {
514 // LAMESPEC: MSDN says, "In all views except the details
515 // view of the ListView, this value specifies the same
516 // bounding rectangle as the Entire value." Actually, it
517 // returns same bounding rectangles for Item and Entire
518 // values in the case of Details view.
520 icon_rect = label_rect = Rectangle.Empty;
521 icon_rect.X = checkbox_rect.Width + 2;
522 item_ht = Math.Max (owner.CheckBoxSize.Height, text_size.Height);
524 if (owner.SmallImageList != null) {
525 item_ht = Math.Max (item_ht, owner.SmallImageList.ImageSize.Height);
526 icon_rect.Width = owner.SmallImageList.ImageSize.Width;
529 label_rect.Height = icon_rect.Height = item_ht;
530 checkbox_rect.Y = item_ht - checkbox_rect.Height;
532 label_rect.X = icon_rect.Right + 1;
534 if (owner.Columns.Count > 0)
535 label_rect.Width = Math.Max (text_size.Width, owner.Columns[0].Wd);
537 label_rect.Width = text_size.Width;
539 item_rect = total = Rectangle.Union
540 (Rectangle.Union (checkbox_rect, icon_rect), label_rect);
541 bounds.Size = total.Size;
543 // Take into account the rest of columns. First column
544 // is already taken into account above.
545 for (int i = 1; i < owner.Columns.Count; i++) {
546 item_rect.Width += owner.Columns [i].Wd;
547 bounds.Width += owner.Columns [i].Wd;
552 label_rect = icon_rect = Rectangle.Empty;
554 if (owner.LargeImageList != null) {
555 icon_rect.Width = owner.LargeImageList.ImageSize.Width;
556 icon_rect.Height = owner.LargeImageList.ImageSize.Height;
559 if (checkbox_rect.Height > icon_rect.Height)
560 icon_rect.Y = checkbox_rect.Height - icon_rect.Height;
562 checkbox_rect.Y = icon_rect.Height - checkbox_rect.Height;
565 if (text_size.Width <= icon_rect.Width) {
566 icon_rect.X = checkbox_rect.Width + 1;
567 label_rect.X = icon_rect.X + (icon_rect.Width - text_size.Width) / 2;
568 label_rect.Y = icon_rect.Bottom + 2;
569 label_rect.Size = text_size;
571 int centerX = text_size.Width / 2;
572 icon_rect.X = checkbox_rect.Width + 1 + centerX - icon_rect.Width / 2;
573 label_rect.X = checkbox_rect.Width + 1;
574 label_rect.Y = icon_rect.Bottom + 2;
575 label_rect.Size = text_size;
578 item_rect = Rectangle.Union (icon_rect, label_rect);
579 total = Rectangle.Union (item_rect, checkbox_rect);
580 bounds.Size = total.Size;
585 label_rect = icon_rect = Rectangle.Empty;
586 icon_rect.X = checkbox_rect.Width + 1;
587 item_ht = Math.Max (owner.CheckBoxSize.Height, text_size.Height);
589 if (owner.SmallImageList != null) {
590 item_ht = Math.Max (item_ht, owner.SmallImageList.ImageSize.Height);
591 icon_rect.Width = owner.SmallImageList.ImageSize.Width;
592 icon_rect.Height = owner.SmallImageList.ImageSize.Height;
595 checkbox_rect.Y = item_ht - checkbox_rect.Height;
596 label_rect.X = icon_rect.Right + 1;
597 label_rect.Width = text_size.Width;
598 label_rect.Height = icon_rect.Height = item_ht;
600 item_rect = Rectangle.Union (icon_rect, label_rect);
601 total = Rectangle.Union (item_rect, checkbox_rect);
602 bounds.Size = total.Size;
607 #endregion // Private Internal Methods
611 [DefaultProperty ("Text")]
612 [DesignTimeVisible (false)]
614 [ToolboxItem (false)]
615 [TypeConverter (typeof(ListViewSubItemConverter))]
616 public class ListViewSubItem
618 private Color back_color;
620 private Color fore_color;
621 internal ListViewItem owner;
624 #region Public Constructors
625 public ListViewSubItem ()
629 public ListViewSubItem (ListViewItem owner, string text)
630 : this (owner, text, ThemeEngine.Current.ColorWindowText,
631 ThemeEngine.Current.ColorWindow, null)
635 public ListViewSubItem (ListViewItem owner, string text, Color foreColor,
636 Color backColor, Font font)
640 this.fore_color = foreColor;
641 this.back_color = backColor;
644 #endregion // Public Constructors
646 #region Public Instance Properties
647 public Color BackColor {
648 get { return back_color; }
660 else if (owner != null)
662 return ThemeEngine.Current.DefaultFont;
672 public Color ForeColor {
673 get { return fore_color; }
688 #endregion // Public Instance Properties
690 #region Public Methods
691 public void ResetStyle ()
693 font = ThemeEngine.Current.DefaultFont;
694 back_color = ThemeEngine.Current.DefaultControlBackColor;
695 fore_color = ThemeEngine.Current.DefaultControlForeColor;
699 public override string ToString ()
701 return string.Format ("ListViewSubItem {{0}}", text);
703 #endregion // Public Methods
706 #region Private Methods
707 private void Invalidate ()
709 if (owner == null || owner.owner == null)
712 owner.owner.Invalidate ();
714 #endregion // Private Methods
717 public class ListViewSubItemCollection : IList, ICollection, IEnumerable
719 private ArrayList list;
720 internal ListViewItem owner;
722 #region Public Constructors
723 public ListViewSubItemCollection (ListViewItem owner)
726 this.list = new ArrayList ();
728 #endregion // Public Constructors
730 #region Public Properties
733 get { return list.Count; }
736 public bool IsReadOnly {
737 get { return false; }
740 public ListViewSubItem this [int index] {
741 get { return (ListViewSubItem) list [index]; }
743 value.owner = this.owner;
744 list [index] = value;
748 bool ICollection.IsSynchronized {
749 get { return list.IsSynchronized; }
752 object ICollection.SyncRoot {
753 get { return list.SyncRoot; }
756 bool IList.IsFixedSize {
757 get { return list.IsFixedSize; }
760 object IList.this [int index] {
761 get { return this [index]; }
763 if (! (value is ListViewSubItem))
764 throw new ArgumentException ("Not of type ListViewSubItem", "value");
765 this [index] = (ListViewSubItem) value;
768 #endregion // Public Properties
770 #region Public Methods
771 public ListViewSubItem Add (ListViewSubItem item)
773 item.owner = this.owner;
778 public ListViewSubItem Add (string text)
780 ListViewSubItem item = new ListViewSubItem (this.owner, text);
785 public ListViewSubItem Add (string text, Color foreColor,
786 Color backColor, Font font)
788 ListViewSubItem item = new ListViewSubItem (this.owner, text,
789 foreColor, backColor, font);
794 public void AddRange (ListViewSubItem [] items)
797 foreach (ListViewSubItem item in items)
801 public void AddRange (string [] items)
804 foreach (string item in items)
808 public void AddRange (string [] items, Color foreColor,
809 Color backColor, Font font)
812 foreach (string item in items)
813 this.Add (item, foreColor, backColor, font);
821 public bool Contains (ListViewSubItem item)
823 return list.Contains (item);
826 public IEnumerator GetEnumerator ()
828 return list.GetEnumerator ();
831 void ICollection.CopyTo (Array dest, int index)
833 list.CopyTo (dest, index);
836 int IList.Add (object item)
838 if (! (item is ListViewSubItem)) {
839 throw new ArgumentException ("Not of type ListViewSubItem", "item");
842 ListViewSubItem sub_item = (ListViewSubItem) item;
843 sub_item.owner = this.owner;
844 return list.Add (sub_item);
847 bool IList.Contains (object subItem)
849 if (! (subItem is ListViewSubItem)) {
850 throw new ArgumentException ("Not of type ListViewSubItem", "subItem");
853 return this.Contains ((ListViewSubItem) subItem);
856 int IList.IndexOf (object subItem)
858 if (! (subItem is ListViewSubItem)) {
859 throw new ArgumentException ("Not of type ListViewSubItem", "subItem");
862 return this.IndexOf ((ListViewSubItem) subItem);
865 void IList.Insert (int index, object item)
867 if (! (item is ListViewSubItem)) {
868 throw new ArgumentException ("Not of type ListViewSubItem", "item");
871 this.Insert (index, (ListViewSubItem) item);
874 void IList.Remove (object item)
876 if (! (item is ListViewSubItem)) {
877 throw new ArgumentException ("Not of type ListViewSubItem", "item");
880 this.Remove ((ListViewSubItem) item);
883 public int IndexOf (ListViewSubItem subItem)
885 return list.IndexOf (subItem);
888 public void Insert (int index, ListViewSubItem item)
890 item.owner = this.owner;
891 list.Insert (index, item);
894 public void Remove (ListViewSubItem item)
899 public void RemoveAt (int index)
901 list.RemoveAt (index);
903 #endregion // Public Methods
905 #endregion // Subclasses