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)
26 // - Drawing of focus rectangle
33 using System.Collections;
34 using System.ComponentModel;
36 using System.Runtime.Serialization;
38 namespace System.Windows.Forms
40 [DefaultProperty ("Text")]
41 [DesignTimeVisible (false)]
44 [TypeConverter (typeof (ListViewItemConverter))]
45 public class ListViewItem : ICloneable, ISerializable
47 #region Instance Variables
48 private int image_index = -1;
49 private bool is_checked = false;
50 private bool is_focused = false;
51 private int state_image_index = -1;
52 private ListViewSubItemCollection sub_items;
54 private bool use_item_style = true;
57 Rectangle checkbox_rect; // calculated by CalcListViewItem method
64 #endregion Instance Variables
66 #region Public Constructors
67 public ListViewItem ()
69 this.sub_items = new ListViewSubItemCollection (this);
70 this.sub_items.Add ("");
73 public ListViewItem (string text) : this (text, -1)
77 public ListViewItem (string [] items) : this (items, -1)
81 public ListViewItem (ListViewItem.ListViewSubItem [] subItems, int imageIndex)
83 this.sub_items = new ListViewSubItemCollection (this);
84 this.sub_items.AddRange (subItems);
85 this.image_index = imageIndex;
88 public ListViewItem (string text, int imageIndex)
90 this.image_index = imageIndex;
91 this.sub_items = new ListViewSubItemCollection (this);
92 this.sub_items.Add (text);
95 public ListViewItem (string [] items, int imageIndex)
97 this.sub_items = new ListViewSubItemCollection (this);
98 this.sub_items.AddRange (items);
99 this.image_index = imageIndex;
102 public ListViewItem (string [] items, int imageIndex, Color foreColor,
103 Color backColor, Font font)
105 this.sub_items = new ListViewSubItemCollection (this);
106 this.sub_items.AddRange (items);
107 this.image_index = imageIndex;
108 ForeColor = foreColor;
109 BackColor = backColor;
112 #endregion // Public Constructors
114 #region Public Instance Properties
115 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
116 public Color BackColor {
118 if (sub_items.Count > 0)
119 return sub_items[0].BackColor;
122 return owner.BackColor;
124 return ThemeEngine.Current.ColorWindow;
127 set { sub_items[0].BackColor = value; }
131 public Rectangle Bounds {
133 return GetBounds (ItemBoundsPortion.Entire);
137 [DefaultValue (false)]
138 [RefreshProperties (RefreshProperties.Repaint)]
139 public bool Checked {
140 get { return is_checked; }
142 if (is_checked == value)
149 if (owner.CheckedItems.Contains (this) == false) {
150 owner.CheckedItems.list.Add (this);
151 owner.CheckedIndices.list.Add (this.Index);
155 owner.CheckedItems.list.Remove (this);
156 owner.CheckedIndices.list.Remove (this.Index);
166 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
167 public bool Focused {
168 get { return is_focused; }
170 if (is_focused == value)
182 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
185 if (sub_items.Count > 0)
186 return sub_items[0].Font;
191 return ThemeEngine.Current.DefaultFont;
194 if (sub_items[0].Font == value)
197 sub_items[0].Font = value;
205 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
206 public Color ForeColor {
208 if (sub_items.Count > 0)
209 return sub_items[0].ForeColor;
212 return owner.ForeColor;
214 return ThemeEngine.Current.ColorWindowText;
216 set { sub_items[0].ForeColor = value; }
220 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
221 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design,
222 typeof (System.Drawing.Design.UITypeEditor))]
224 [TypeConverter (typeof (ImageIndexConverter))]
225 public int ImageIndex {
226 get { return image_index; }
229 throw new ArgumentException ("Invalid ImageIndex. It must be greater than or equal to -1.");
240 public ImageList ImageList {
244 else if (owner.View == View.LargeIcon)
245 return owner.large_image_list;
247 return owner.small_image_list;
257 return owner.Items.IndexOf (this);
262 public ListView ListView {
263 get { return owner; }
267 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
268 public bool Selected {
269 get { return selected; }
271 if (selected == value)
277 owner.UpdateSelection (this);
285 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design,
286 typeof (System.Drawing.Design.UITypeEditor))]
288 [TypeConverter (typeof (ImageIndexConverter))]
289 public int StateImageIndex {
290 get { return state_image_index; }
292 if (value < -1 || value > 14)
293 throw new ArgumentOutOfRangeException ("Invalid StateImageIndex. It must be in the range of [-1, 14].");
295 state_image_index = value;
299 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
300 public ListViewSubItemCollection SubItems {
301 get { return sub_items; }
305 [DefaultValue (null)]
306 [Localizable (false)]
307 [TypeConverter (typeof (StringConverter))]
314 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
317 if (this.sub_items.Count > 0)
318 return this.sub_items [0].Text;
323 if (sub_items [0].Text == value)
326 sub_items [0].Text = value;
334 [DefaultValue (true)]
335 public bool UseItemStyleForSubItems {
336 get { return use_item_style; }
337 set { use_item_style = value; }
339 #endregion // Public Instance Properties
341 #region Public Instance Methods
342 public void BeginEdit ()
345 // if (owner != null && owner.LabelEdit
346 // && owner.Activation == ItemActivation.Standard)
349 // throw new InvalidOperationException ();
352 public virtual object Clone ()
354 ListViewItem clone = new ListViewItem ();
355 clone.image_index = this.image_index;
356 clone.is_checked = this.is_checked;
357 clone.is_focused = this.is_focused;
358 clone.selected = this.selected;
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.Invalidate (prev);
477 owner.Invalidate (Bounds);
482 internal ListView Owner {
494 private void Invalidate ()
499 owner.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 = icon_rect.Height - checkbox_rect.Height - 1;
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 checkbox_rect.Y = icon_rect.Height - checkbox_rect.Height - 1;
561 if (text_size.Width <= (checkbox_rect.Width + icon_rect.Width)) {
562 icon_rect.X = checkbox_rect.Width + 1;
563 label_rect.X = icon_rect.X + (icon_rect.Width - text_size.Width) / 2;
564 label_rect.Y = Math.Max (checkbox_rect.Bottom, icon_rect.Bottom) + 2;
565 label_rect.Size = text_size;
567 int centerX = text_size.Width / 2;
568 icon_rect.X = centerX - icon_rect.Width / 2;
569 checkbox_rect.X = (icon_rect.X - checkbox_rect.Width);
570 label_rect.Y = Math.Max (checkbox_rect.Bottom, icon_rect.Bottom) + 2;
571 label_rect.Size = text_size;
574 item_rect = Rectangle.Union (icon_rect, label_rect);
575 total = Rectangle.Union (item_rect, checkbox_rect);
576 bounds.Size = total.Size;
581 label_rect = icon_rect = Rectangle.Empty;
582 icon_rect.X = checkbox_rect.Width + 1;
583 item_ht = Math.Max (owner.CheckBoxSize.Height, text_size.Height);
585 if (owner.SmallImageList != null) {
586 item_ht = Math.Max (item_ht, owner.SmallImageList.ImageSize.Height);
587 icon_rect.Width = owner.SmallImageList.ImageSize.Width;
588 icon_rect.Height = owner.SmallImageList.ImageSize.Height;
591 checkbox_rect.Y = icon_rect.Height - checkbox_rect.Height + 1;
592 label_rect.X = icon_rect.Right + 1;
593 label_rect.Width = text_size.Width;
594 label_rect.Height = icon_rect.Height = item_ht;
596 item_rect = Rectangle.Union (icon_rect, label_rect);
597 total = Rectangle.Union (item_rect, checkbox_rect);
598 bounds.Size = total.Size;
603 #endregion // Private Internal Methods
607 [DefaultProperty ("Text")]
608 [DesignTimeVisible (false)]
610 [ToolboxItem (false)]
611 [TypeConverter (typeof(ListViewSubItemConverter))]
612 public class ListViewSubItem
614 private Color back_color;
616 private Color fore_color;
617 internal ListViewItem owner;
620 #region Public Constructors
621 public ListViewSubItem ()
625 public ListViewSubItem (ListViewItem owner, string text)
626 : this (owner, text, ThemeEngine.Current.ColorWindowText,
627 ThemeEngine.Current.ColorWindow,
628 ThemeEngine.Current.DefaultFont)
632 public ListViewSubItem (ListViewItem owner, string text, Color foreColor,
633 Color backColor, Font font)
637 this.fore_color = foreColor;
638 this.back_color = backColor;
641 #endregion // Public Constructors
643 #region Public Instance Properties
644 public Color BackColor {
645 get { return back_color; }
657 else if (owner != null)
667 public Color ForeColor {
668 get { return fore_color; }
683 #endregion // Public Instance Properties
685 #region Public Methods
686 public void ResetStyle ()
688 font = ThemeEngine.Current.DefaultFont;
689 back_color = ThemeEngine.Current.DefaultControlBackColor;
690 fore_color = ThemeEngine.Current.DefaultControlForeColor;
694 public override string ToString ()
696 return string.Format ("ListViewSubItem {{0}}", text);
698 #endregion // Public Methods
701 #region Private Methods
702 private void Invalidate ()
704 if (owner == null || owner.owner == null)
707 owner.owner.Invalidate ();
709 #endregion // Private Methods
712 public class ListViewSubItemCollection : IList, ICollection, IEnumerable
714 private ArrayList list;
715 internal ListViewItem owner;
717 #region Public Constructors
718 public ListViewSubItemCollection (ListViewItem owner)
721 this.list = new ArrayList ();
723 #endregion // Public Constructors
725 #region Public Properties
727 public virtual int Count {
728 get { return list.Count; }
731 public virtual bool IsReadOnly {
732 get { return false; }
735 public ListViewSubItem this [int index] {
736 get { return (ListViewSubItem) list [index]; }
738 value.owner = this.owner;
739 list [index] = value;
743 bool ICollection.IsSynchronized {
744 get { return list.IsSynchronized; }
747 object ICollection.SyncRoot {
748 get { return list.SyncRoot; }
751 bool IList.IsFixedSize {
752 get { return list.IsFixedSize; }
755 object IList.this [int index] {
756 get { return this [index]; }
758 if (! (value is ListViewSubItem))
759 throw new ArgumentException ("Not of type ListViewSubItem", "value");
760 this [index] = (ListViewSubItem) value;
763 #endregion // Public Properties
765 #region Public Methods
766 public ListViewSubItem Add (ListViewSubItem item)
768 item.owner = this.owner;
773 public ListViewSubItem Add (string text)
775 ListViewSubItem item = new ListViewSubItem (this.owner, text);
780 public ListViewSubItem Add (string text, Color foreColor,
781 Color backColor, Font font)
783 ListViewSubItem item = new ListViewSubItem (this.owner, text,
784 foreColor, backColor, font);
789 public void AddRange (ListViewSubItem [] items)
792 foreach (ListViewSubItem item in items)
796 public void AddRange (string [] items)
799 foreach (string item in items)
803 public void AddRange (string [] items, Color foreColor,
804 Color backColor, Font font)
807 foreach (string item in items)
808 this.Add (item, foreColor, backColor, font);
811 public virtual void Clear ()
816 public bool Contains (ListViewSubItem item)
818 return list.Contains (item);
821 public virtual IEnumerator GetEnumerator ()
823 return list.GetEnumerator ();
826 void ICollection.CopyTo (Array dest, int index)
828 list.CopyTo (dest, index);
831 int IList.Add (object item)
833 if (! (item is ListViewSubItem)) {
834 throw new ArgumentException ("Not of type ListViewSubItem", "item");
837 ListViewSubItem sub_item = (ListViewSubItem) item;
838 sub_item.owner = this.owner;
839 return list.Add (sub_item);
842 bool IList.Contains (object subItem)
844 if (! (subItem is ListViewSubItem)) {
845 throw new ArgumentException ("Not of type ListViewSubItem", "subItem");
848 return this.Contains ((ListViewSubItem) subItem);
851 int IList.IndexOf (object subItem)
853 if (! (subItem is ListViewSubItem)) {
854 throw new ArgumentException ("Not of type ListViewSubItem", "subItem");
857 return this.IndexOf ((ListViewSubItem) subItem);
860 void IList.Insert (int index, object item)
862 if (! (item is ListViewSubItem)) {
863 throw new ArgumentException ("Not of type ListViewSubItem", "item");
866 this.Insert (index, (ListViewSubItem) item);
869 void IList.Remove (object item)
871 if (! (item is ListViewSubItem)) {
872 throw new ArgumentException ("Not of type ListViewSubItem", "item");
875 this.Remove ((ListViewSubItem) item);
878 public int IndexOf (ListViewSubItem subItem)
880 return list.IndexOf (subItem);
883 public void Insert (int index, ListViewSubItem item)
885 item.owner = this.owner;
886 list.Insert (index, item);
889 public void Remove (ListViewSubItem item)
894 public virtual void RemoveAt (int index)
896 list.RemoveAt (index);
898 #endregion // Public Methods
900 #endregion // Subclasses