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
54 internal int current_item; // Current item when doing layout
55 internal Point items_area_location;
56 bool is_default_group;
57 int item_count; // Used by default group to store item count
59 #region ListViewGroup constructors
61 public ListViewGroup()
63 header = "ListViewGroup";
64 header_alignment = HorizontalAlignment.Left;
67 public ListViewGroup(string header)
70 header_alignment = HorizontalAlignment.Left;
73 public ListViewGroup(string key, string headerText)
77 header_alignment = HorizontalAlignment.Left;
80 public ListViewGroup(string header, HorizontalAlignment headerAlignment)
83 header_alignment = headerAlignment;
86 private ListViewGroup(SerializationInfo info, StreamingContext context)
88 header = info.GetString("Header");
89 name = info.GetString("Name");
90 header_alignment = (HorizontalAlignment)info.GetInt32("HeaderAlignment");
91 tag = info.GetValue("Tag", typeof(object));
93 int count = info.GetInt32("ListViewItemCount");
96 items = new ListView.ListViewItemCollection(list_view_owner);
98 for (int i = 0; i < count; i++)
100 items.Add((ListViewItem)info.GetValue(string.Format("ListViewItem_{0}", i), typeof(ListViewItem)));
107 #region ListViewGroup properties
109 public string Header {
110 get { return header; }
112 if (!header.Equals(value)) {
115 if (list_view_owner != null)
116 list_view_owner.Redraw(true);
121 [DefaultValue (HorizontalAlignment.Left)]
122 public HorizontalAlignment HeaderAlignment {
123 get { return header_alignment; }
125 if (!header_alignment.Equals(value)) {
126 if (value != HorizontalAlignment.Left && value != HorizontalAlignment.Right &&
127 value != HorizontalAlignment.Center)
128 throw new InvalidEnumArgumentException("HeaderAlignment", (int)value, typeof(HorizontalAlignment));
130 header_alignment = value;
132 if (list_view_owner != null)
133 list_view_owner.Redraw(true);
139 public ListView.ListViewItemCollection Items {
142 items = new ListView.ListViewItemCollection (list_view_owner, this);
148 [DesignerSerializationVisibility(0)]
150 public ListView ListView {
151 get { return list_view_owner; }
154 internal ListView ListViewOwner {
155 get { return list_view_owner; }
157 list_view_owner = value;
158 if (!is_default_group)
163 internal Rectangle HeaderBounds {
165 Rectangle retval = header_bounds;
166 retval.X -= list_view_owner.h_marker;
167 retval.Y -= list_view_owner.v_marker;
171 if (list_view_owner != null)
172 list_view_owner.item_control.Invalidate (HeaderBounds);
174 header_bounds = value;
176 if (list_view_owner != null)
177 list_view_owner.item_control.Invalidate (HeaderBounds);
182 internal bool IsDefault {
184 return is_default_group;
187 is_default_group = value;
191 internal int ItemCount {
193 return is_default_group ? item_count : items.Count;
196 if (!is_default_group)
197 throw new InvalidOperationException ("ItemCount cannot be set for non-default groups.");
207 set { name = value; }
210 [TypeConverter(typeof(StringConverter))]
221 public override string ToString()
226 #region ISerializable Members
228 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
230 info.AddValue("Header", header);
231 info.AddValue("Name", name);
232 info.AddValue("HeaderAlignment", header_alignment);
233 info.AddValue("Tag", tag);
235 info.AddValue("ListViewItemCount", items.Count);
238 foreach (ListViewItem item in items)
240 info.AddValue(string.Format("ListViewItem_{0}", i), item);
248 internal class ListViewGroupConverter : TypeConverter
250 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
256 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
258 return new StandardValuesCollection (new object [] {});