4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 // Copyright (c) 2006 Daniel Nauck
26 // Daniel Nauck (dna(at)mono-project(dot)de)
32 using System.Runtime.Serialization;
33 using System.ComponentModel;
36 namespace System.Windows.Forms
38 [SerializableAttribute]
40 [DesignTimeVisible(false)]
41 [DefaultProperty("Header")]
42 [TypeConverter (typeof (ListViewGroupConverter))]
43 public sealed class ListViewGroup : ISerializable
45 internal string header = string.Empty;
46 private string name = null;
47 private HorizontalAlignment header_alignment = HorizontalAlignment.Left;
48 private ListView list_view_owner = null;
49 private ListView.ListViewItemCollection items = null;
50 private object tag = null;
51 private Rectangle header_bounds = Rectangle.Empty;
52 internal int starting_row; // At which row the group starts
53 internal int starting_item; // The first display item in group
55 internal int current_item; // Current item when doing layout
56 internal Point items_area_location;
57 bool is_default_group;
58 int item_count; // Used by default group to store item count
60 #region ListViewGroup constructors
62 public ListViewGroup () : this ("ListViewGroup", HorizontalAlignment.Left)
66 public ListViewGroup (string header) : this (header, HorizontalAlignment.Left)
70 public ListViewGroup (string key, string headerText) : this (headerText, HorizontalAlignment.Left)
75 public ListViewGroup (string header, HorizontalAlignment headerAlignment)
78 header_alignment = headerAlignment;
79 items = new ListView.ListViewItemCollection (list_view_owner, this);
82 private ListViewGroup(SerializationInfo info, StreamingContext context)
84 header = info.GetString("Header");
85 name = info.GetString("Name");
86 header_alignment = (HorizontalAlignment)info.GetInt32("HeaderAlignment");
87 tag = info.GetValue("Tag", typeof(object));
89 int count = info.GetInt32("ListViewItemCount");
92 items = new ListView.ListViewItemCollection(list_view_owner);
94 for (int i = 0; i < count; i++)
96 items.Add((ListViewItem)info.GetValue(string.Format("ListViewItem_{0}", i), typeof(ListViewItem)));
103 #region ListViewGroup properties
105 public string Header {
106 get { return header; }
108 if (!header.Equals(value)) {
111 if (list_view_owner != null)
112 list_view_owner.Redraw(true);
117 [DefaultValue (HorizontalAlignment.Left)]
118 public HorizontalAlignment HeaderAlignment {
119 get { return header_alignment; }
121 if (!header_alignment.Equals(value)) {
122 if (value != HorizontalAlignment.Left && value != HorizontalAlignment.Right &&
123 value != HorizontalAlignment.Center)
124 throw new InvalidEnumArgumentException("HeaderAlignment", (int)value, typeof(HorizontalAlignment));
126 header_alignment = value;
128 if (list_view_owner != null)
129 list_view_owner.Redraw(true);
135 public ListView.ListViewItemCollection Items {
141 [DesignerSerializationVisibility(0)]
143 public ListView ListView {
144 get { return list_view_owner; }
147 internal ListView ListViewOwner {
148 get { return list_view_owner; }
150 list_view_owner = value;
151 if (!is_default_group)
156 internal Rectangle HeaderBounds {
158 Rectangle retval = header_bounds;
159 retval.X -= list_view_owner.h_marker;
160 retval.Y -= list_view_owner.v_marker;
164 if (list_view_owner != null)
165 list_view_owner.item_control.Invalidate (HeaderBounds);
167 header_bounds = value;
169 if (list_view_owner != null)
170 list_view_owner.item_control.Invalidate (HeaderBounds);
175 internal bool IsDefault {
177 return is_default_group;
180 is_default_group = value;
184 internal int ItemCount {
186 return is_default_group ? item_count : items.Count;
189 if (!is_default_group)
190 throw new InvalidOperationException ("ItemCount cannot be set for non-default groups.");
196 internal int GetActualItemCount ()
198 if (is_default_group)
202 for (int i = 0; i < items.Count; i++)
203 if (items [i].ListView != null) // Ignore.
213 set { name = value; }
216 [TypeConverter(typeof(StringConverter))]
227 public override string ToString()
232 #region ISerializable Members
234 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
236 info.AddValue("Header", header);
237 info.AddValue("Name", name);
238 info.AddValue("HeaderAlignment", header_alignment);
239 info.AddValue("Tag", tag);
241 info.AddValue("ListViewItemCount", items.Count);
244 foreach (ListViewItem item in items)
246 info.AddValue(string.Format("ListViewItem_{0}", i), item);
254 internal class ListViewGroupConverter : TypeConverter
256 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
262 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
264 return new StandardValuesCollection (new object [] {});