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 internal Rectangle checkbox_rect; // calculated by CalcListViewItem method
58 internal Rectangle entire_rect;
59 internal Rectangle icon_rect;
60 internal Rectangle item_rect;
61 internal Rectangle label_rect;
62 internal Point location = Point.Empty; // set by the ListView control
63 internal ListView owner;
64 internal bool selected;
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)
149 owner.Invalidate (Bounds);
154 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
155 public bool Focused {
156 get { return is_focused; }
158 if (is_focused == value)
164 owner.Invalidate (Bounds);
169 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
172 if (sub_items.Count > 0)
173 return sub_items[0].Font;
178 return ThemeEngine.Current.DefaultFont;
181 if (sub_items[0].Font == value)
184 sub_items[0].Font = value;
187 owner.Invalidate (Bounds);
191 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
192 public Color ForeColor {
194 if (sub_items.Count > 0)
195 return sub_items[0].ForeColor;
198 return owner.ForeColor;
200 return ThemeEngine.Current.ColorWindowText;
202 set { sub_items[0].ForeColor = value; }
206 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
207 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design,
208 typeof (System.Drawing.Design.UITypeEditor))]
210 [TypeConverter (typeof (ImageIndexConverter))]
211 public int ImageIndex {
212 get { return image_index; }
215 throw new ArgumentException ("Invalid ImageIndex. It must be greater than or equal to -1.");
220 owner.Invalidate (Bounds);
225 public ImageList ImageList {
229 else if (owner.View == View.LargeIcon)
230 return owner.large_image_list;
232 return owner.small_image_list;
242 return owner.Items.IndexOf (this);
247 public ListView ListView {
248 get { return owner; }
252 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
253 public bool Selected {
254 get { return selected; }
257 if (owner.CanMultiselect == false &&
258 owner.SelectedItems.Count > 0) {
259 owner.SelectedItems.Clear ();
260 owner.SelectedIndices.list.Clear ();
265 //do we need !owner.SelectedItems.Contains (this))
266 owner.SelectedItems.list.Add (this);
267 owner.SelectedIndices.list.Add (this.Index);
270 owner.SelectedItems.list.Remove (this);
271 owner.SelectedIndices.list.Remove (this.Index);
278 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design,
279 typeof (System.Drawing.Design.UITypeEditor))]
281 [TypeConverter (typeof (ImageIndexConverter))]
282 public int StateImageIndex {
283 get { return state_image_index; }
285 if (value < -1 || value > 14)
286 throw new ArgumentOutOfRangeException ("Invalid StateImageIndex. It must be in the range of [-1, 14].");
288 state_image_index = value;
292 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
293 public ListViewSubItemCollection SubItems {
294 get { return sub_items; }
298 [DefaultValue (null)]
299 [Localizable (false)]
300 [TypeConverter (typeof (StringConverter))]
307 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
310 if (this.sub_items.Count > 0)
311 return this.sub_items [0].Text;
315 set { this.sub_items [0].Text = value; }
318 [DefaultValue (true)]
319 public bool UseItemStyleForSubItems {
320 get { return use_item_style; }
321 set { use_item_style = value; }
323 #endregion // Public Instance Properties
325 #region Public Instance Methods
326 public void BeginEdit ()
329 // if (owner != null && owner.LabelEdit
330 // && owner.Activation == ItemActivation.Standard)
333 // throw new InvalidOperationException ();
336 public virtual object Clone ()
338 ListViewItem clone = new ListViewItem ();
339 clone.image_index = this.image_index;
340 clone.is_checked = this.is_checked;
341 clone.is_focused = this.is_focused;
342 clone.selected = this.selected;
343 clone.state_image_index = this.state_image_index;
344 clone.sub_items = new ListViewSubItemCollection (this);
346 foreach (ListViewSubItem subItem in this.sub_items)
347 clone.sub_items.Add (subItem.Text, subItem.ForeColor,
348 subItem.BackColor, subItem.Font);
349 clone.tag = this.tag;
350 clone.use_item_style = this.use_item_style;
356 public virtual void EnsureVisible ()
358 if (this.owner != null) {
359 owner.EnsureVisible (owner.Items.IndexOf (this));
363 public Rectangle GetBounds (ItemBoundsPortion portion)
366 return Rectangle.Empty;
368 // should we check for dirty flag to optimize this ?
373 case ItemBoundsPortion.Icon:
376 case ItemBoundsPortion.Label:
379 case ItemBoundsPortion.ItemOnly:
382 case ItemBoundsPortion.Entire:
386 throw new ArgumentException ("Invalid value for portion.");
390 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
395 public virtual void Remove ()
398 owner.Items.Remove (this);
402 public override string ToString ()
404 return string.Format ("ListViewItem: {0}", this.Text);
406 #endregion // Public Instance Methods
408 #region Protected Methods
409 protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
414 protected virtual void Serialize (SerializationInfo info, StreamingContext context)
418 #endregion // Protected Methods
420 #region Private Internal Methods
421 internal Rectangle CheckRect {
422 get { return this.checkbox_rect; }
425 internal Rectangle EntireRect {
426 get { return this.entire_rect; }
429 internal Rectangle IconRect {
430 get { return this.icon_rect; }
433 internal Rectangle LabelRect {
434 get { return this.label_rect; }
437 internal void CalcListViewItem ()
440 Size text_size = owner.text_size;
442 if (owner.CheckBoxes)
443 checkbox_rect.Size = owner.CheckBoxSize;
445 checkbox_rect = Rectangle.Empty;
447 checkbox_rect.Location = this.location;
449 switch (owner.View) {
452 // LAMESPEC: MSDN says, "In all views except the details
453 // view of the ListView, this value specifies the same
454 // bounding rectangle as the Entire value." Actually, it
455 // returns same bounding rectangles for Item and Entire
456 // values in the case of Details view.
458 icon_rect.X = checkbox_rect.X + checkbox_rect.Width + 2;
459 icon_rect.Y = checkbox_rect.Y;
461 item_ht = Math.Max (owner.CheckBoxSize.Height + 1,
464 if (owner.SmallImageList != null) {
465 item_ht = Math.Max (item_ht,
466 owner.SmallImageList.ImageSize.Height + 1);
467 icon_rect.Width = owner.SmallImageList.ImageSize.Width;
472 label_rect.Height = checkbox_rect.Height = icon_rect.Height = item_ht;
474 label_rect.X = icon_rect.X + icon_rect.Width;
475 label_rect.Y = icon_rect.Y;
477 if (owner.Columns.Count > 0)
478 label_rect.Width = Math.Max (text_size.Width, owner.Columns[0].Wd);
480 label_rect.Width = text_size.Width;
482 item_rect = entire_rect = Rectangle.Union
483 (Rectangle.Union (checkbox_rect, icon_rect), label_rect);
485 // Take into account the rest of columns. First column
486 // is already taken into account above.
487 for (int i = 1; i < owner.Columns.Count; i++) {
488 item_rect.Width += owner.Columns [i].Wd;
489 entire_rect.Width += owner.Columns [i].Wd;
494 if (owner.LargeImageList != null) {
495 icon_rect.Width = owner.LargeImageList.ImageSize.Width + 16;
496 icon_rect.Height = owner.LargeImageList.ImageSize.Height + 4;
499 icon_rect.Width = 16;
500 icon_rect.Height = 4;
503 if (text_size.Width <= (checkbox_rect.Width + icon_rect.Width)) {
504 icon_rect.X = checkbox_rect.X + checkbox_rect.Width;
505 icon_rect.Y = checkbox_rect.Y;
507 label_rect.X = icon_rect.X + (icon_rect.Width
508 - text_size.Width) / 2;
509 label_rect.Y = Math.Max (checkbox_rect.Bottom,
510 icon_rect.Bottom) + 2;
511 label_rect.Size = text_size;
514 label_rect.X = this.location.X;
516 int centerX = label_rect.X + text_size.Width / 2;
517 icon_rect.X = centerX - icon_rect.Width / 2;
518 checkbox_rect.X = (icon_rect.X - checkbox_rect.Width);
520 icon_rect.Y = checkbox_rect.Y;
522 label_rect.Y = Math.Max (checkbox_rect.Bottom,
523 icon_rect.Bottom) + 2;
524 label_rect.Size = text_size;
527 item_rect = Rectangle.Union (icon_rect, label_rect);
528 entire_rect = Rectangle.Union (item_rect, checkbox_rect);
532 goto case View.SmallIcon;
535 icon_rect.X = checkbox_rect.X + checkbox_rect.Width;
536 icon_rect.Y = checkbox_rect.Y;
538 item_ht = Math.Max (owner.CheckBoxSize.Height, text_size.Height);
540 if (owner.SmallImageList != null) {
541 item_ht = Math.Max (item_ht,
542 owner.SmallImageList.ImageSize.Height + 1);
543 icon_rect.Width = owner.SmallImageList.ImageSize.Width;
548 label_rect.Height = checkbox_rect.Height = icon_rect.Height = item_ht;
550 label_rect.X = icon_rect.X + icon_rect.Width;
551 label_rect.Y = icon_rect.Y;
552 label_rect.Width = text_size.Width;
554 item_rect = Rectangle.Union (icon_rect, label_rect);
555 entire_rect = Rectangle.Union (item_rect, checkbox_rect);
559 #endregion // Private Internal Methods
563 [DefaultProperty ("Text")]
564 [DesignTimeVisible (false)]
566 [ToolboxItem (false)]
567 [TypeConverter (typeof(ListViewSubItemConverter))]
568 public class ListViewSubItem
570 private Color back_color;
572 private Color fore_color;
573 internal ListViewItem owner;
576 #region Public Constructors
577 public ListViewSubItem ()
581 public ListViewSubItem (ListViewItem owner, string text)
582 : this (owner, text, ThemeEngine.Current.ColorWindowText,
583 ThemeEngine.Current.ColorWindow,
584 ThemeEngine.Current.DefaultFont)
588 public ListViewSubItem (ListViewItem owner, string text, Color foreColor,
589 Color backColor, Font font)
593 this.fore_color = foreColor;
594 this.back_color = backColor;
597 #endregion // Public Constructors
599 #region Public Instance Properties
600 public Color BackColor {
601 get { return back_color; }
613 else if (owner != null)
623 public Color ForeColor {
624 get { return fore_color; }
639 #endregion // Public Instance Properties
641 #region Public Methods
642 public void ResetStyle ()
644 font = ThemeEngine.Current.DefaultFont;
645 back_color = ThemeEngine.Current.DefaultControlBackColor;
646 fore_color = ThemeEngine.Current.DefaultControlForeColor;
650 public override string ToString ()
652 return string.Format ("ListViewSubItem {{0}}", text);
654 #endregion // Public Methods
657 #region Private Methods
658 private void Invalidate ()
660 if (owner == null || owner.owner == null)
663 owner.owner.Invalidate ();
665 #endregion // Private Methods
668 public class ListViewSubItemCollection : IList, ICollection, IEnumerable
670 private ArrayList list;
671 internal ListViewItem owner;
673 #region Public Constructors
674 public ListViewSubItemCollection (ListViewItem owner)
677 this.list = new ArrayList ();
679 #endregion // Public Constructors
681 #region Public Properties
683 public virtual int Count {
684 get { return list.Count; }
687 public virtual bool IsReadOnly {
688 get { return false; }
691 public ListViewSubItem this [int index] {
692 get { return (ListViewSubItem) list [index]; }
694 value.owner = this.owner;
695 list [index] = value;
699 bool ICollection.IsSynchronized {
700 get { return list.IsSynchronized; }
703 object ICollection.SyncRoot {
704 get { return list.SyncRoot; }
707 bool IList.IsFixedSize {
708 get { return list.IsFixedSize; }
711 object IList.this [int index] {
712 get { return this [index]; }
714 if (! (value is ListViewSubItem))
715 throw new ArgumentException ("Not of type ListViewSubItem", "value");
716 this [index] = (ListViewSubItem) value;
719 #endregion // Public Properties
721 #region Public Methods
722 public ListViewSubItem Add (ListViewSubItem item)
724 item.owner = this.owner;
729 public ListViewSubItem Add (string text)
731 ListViewSubItem item = new ListViewSubItem (this.owner, text);
736 public ListViewSubItem Add (string text, Color foreColor,
737 Color backColor, Font font)
739 ListViewSubItem item = new ListViewSubItem (this.owner, text,
740 foreColor, backColor, font);
745 public void AddRange (ListViewSubItem [] items)
748 foreach (ListViewSubItem item in items)
752 public void AddRange (string [] items)
755 foreach (string item in items)
759 public void AddRange (string [] items, Color foreColor,
760 Color backColor, Font font)
763 foreach (string item in items)
764 this.Add (item, foreColor, backColor, font);
767 public virtual void Clear ()
772 public bool Contains (ListViewSubItem item)
774 return list.Contains (item);
777 public virtual IEnumerator GetEnumerator ()
779 return list.GetEnumerator ();
782 void ICollection.CopyTo (Array dest, int index)
784 list.CopyTo (dest, index);
787 int IList.Add (object item)
789 if (! (item is ListViewSubItem)) {
790 throw new ArgumentException ("Not of type ListViewSubItem", "item");
793 ListViewSubItem sub_item = (ListViewSubItem) item;
794 sub_item.owner = this.owner;
795 return list.Add (sub_item);
798 bool IList.Contains (object subItem)
800 if (! (subItem is ListViewSubItem)) {
801 throw new ArgumentException ("Not of type ListViewSubItem", "subItem");
804 return this.Contains ((ListViewSubItem) subItem);
807 int IList.IndexOf (object subItem)
809 if (! (subItem is ListViewSubItem)) {
810 throw new ArgumentException ("Not of type ListViewSubItem", "subItem");
813 return this.IndexOf ((ListViewSubItem) subItem);
816 void IList.Insert (int index, object item)
818 if (! (item is ListViewSubItem)) {
819 throw new ArgumentException ("Not of type ListViewSubItem", "item");
822 this.Insert (index, (ListViewSubItem) item);
825 void IList.Remove (object item)
827 if (! (item is ListViewSubItem)) {
828 throw new ArgumentException ("Not of type ListViewSubItem", "item");
831 this.Remove ((ListViewSubItem) item);
834 public int IndexOf (ListViewSubItem subItem)
836 return list.IndexOf (subItem);
839 public void Insert (int index, ListViewSubItem item)
841 item.owner = this.owner;
842 list.Insert (index, item);
845 public void Remove (ListViewSubItem item)
850 public virtual void RemoveAt (int index)
852 list.RemoveAt (index);
854 #endregion // Public Methods
856 #endregion // Subclasses