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()
64 header = "ListViewGroup";
65 header_alignment = HorizontalAlignment.Left;
68 public ListViewGroup(string header)
71 header_alignment = HorizontalAlignment.Left;
74 public ListViewGroup(string key, string headerText)
78 header_alignment = HorizontalAlignment.Left;
81 public ListViewGroup(string header, HorizontalAlignment headerAlignment)
84 header_alignment = headerAlignment;
87 private ListViewGroup(SerializationInfo info, StreamingContext context)
89 header = info.GetString("Header");
90 name = info.GetString("Name");
91 header_alignment = (HorizontalAlignment)info.GetInt32("HeaderAlignment");
92 tag = info.GetValue("Tag", typeof(object));
94 int count = info.GetInt32("ListViewItemCount");
97 items = new ListView.ListViewItemCollection(list_view_owner);
99 for (int i = 0; i < count; i++)
101 items.Add((ListViewItem)info.GetValue(string.Format("ListViewItem_{0}", i), typeof(ListViewItem)));
108 #region ListViewGroup properties
110 public string Header {
111 get { return header; }
113 if (!header.Equals(value)) {
116 if (list_view_owner != null)
117 list_view_owner.Redraw(true);
122 [DefaultValue (HorizontalAlignment.Left)]
123 public HorizontalAlignment HeaderAlignment {
124 get { return header_alignment; }
126 if (!header_alignment.Equals(value)) {
127 if (value != HorizontalAlignment.Left && value != HorizontalAlignment.Right &&
128 value != HorizontalAlignment.Center)
129 throw new InvalidEnumArgumentException("HeaderAlignment", (int)value, typeof(HorizontalAlignment));
131 header_alignment = value;
133 if (list_view_owner != null)
134 list_view_owner.Redraw(true);
140 public ListView.ListViewItemCollection Items {
143 items = new ListView.ListViewItemCollection (list_view_owner, this);
149 [DesignerSerializationVisibility(0)]
151 public ListView ListView {
152 get { return list_view_owner; }
155 internal ListView ListViewOwner {
156 get { return list_view_owner; }
158 list_view_owner = value;
159 if (!is_default_group)
164 internal Rectangle HeaderBounds {
166 Rectangle retval = header_bounds;
167 retval.X -= list_view_owner.h_marker;
168 retval.Y -= list_view_owner.v_marker;
172 if (list_view_owner != null)
173 list_view_owner.item_control.Invalidate (HeaderBounds);
175 header_bounds = value;
177 if (list_view_owner != null)
178 list_view_owner.item_control.Invalidate (HeaderBounds);
183 internal bool IsDefault {
185 return is_default_group;
188 is_default_group = value;
192 internal int ItemCount {
194 return is_default_group ? item_count : items.Count;
197 if (!is_default_group)
198 throw new InvalidOperationException ("ItemCount cannot be set for non-default groups.");
208 set { name = value; }
211 [TypeConverter(typeof(StringConverter))]
222 public override string ToString()
227 #region ISerializable Members
229 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
231 info.AddValue("Header", header);
232 info.AddValue("Name", name);
233 info.AddValue("HeaderAlignment", header_alignment);
234 info.AddValue("Tag", tag);
236 info.AddValue("ListViewItemCount", items.Count);
239 foreach (ListViewItem item in items)
241 info.AddValue(string.Format("ListViewItem_{0}", i), item);
249 internal class ListViewGroupConverter : TypeConverter
251 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
257 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
259 return new StandardValuesCollection (new object [] {});