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 public sealed class ListViewGroup : ISerializable
44 internal string header = string.Empty;
45 private string name = null;
46 private HorizontalAlignment header_alignment = HorizontalAlignment.Left;
47 private ListView list_view_owner = null;
48 private ListView.ListViewItemCollection items = null;
49 private object tag = null;
50 private Rectangle header_bounds = Rectangle.Empty;
51 internal int starting_row; // At which row the group starts
52 internal int current_item; // Current item when doing layout
53 internal Point items_area_location;
54 bool is_default_group;
55 int item_count; // Used by default group to store item count
57 #region ListViewGroup constructors
59 public ListViewGroup()
61 header = "ListViewGroup";
62 header_alignment = HorizontalAlignment.Left;
65 public ListViewGroup(string header)
68 header_alignment = HorizontalAlignment.Left;
71 public ListViewGroup(string key, string headerText)
75 header_alignment = HorizontalAlignment.Left;
78 public ListViewGroup(string header, HorizontalAlignment headerAlignment)
81 header_alignment = headerAlignment;
84 private ListViewGroup(SerializationInfo info, StreamingContext context)
86 header = info.GetString("Header");
87 name = info.GetString("Name");
88 header_alignment = (HorizontalAlignment)info.GetInt32("HeaderAlignment");
89 tag = info.GetValue("Tag", typeof(object));
91 int count = info.GetInt32("ListViewItemCount");
94 items = new ListView.ListViewItemCollection(list_view_owner);
96 for (int i = 0; i < count; i++)
98 items.Add((ListViewItem)info.GetValue(string.Format("ListViewItem_{0}", i), typeof(ListViewItem)));
105 #region ListViewGroup properties
107 public string Header {
108 get { return header; }
110 if (!header.Equals(value)) {
113 if (list_view_owner != null)
114 list_view_owner.Redraw(true);
119 [DefaultValue (HorizontalAlignment.Left)]
120 public HorizontalAlignment HeaderAlignment {
121 get { return header_alignment; }
123 if (!header_alignment.Equals(value)) {
124 if (value != HorizontalAlignment.Left && value != HorizontalAlignment.Right &&
125 value != HorizontalAlignment.Center)
126 throw new InvalidEnumArgumentException("HeaderAlignment", (int)value, typeof(HorizontalAlignment));
128 header_alignment = value;
130 if (list_view_owner != null)
131 list_view_owner.Redraw(true);
137 public ListView.ListViewItemCollection Items {
140 items = new ListView.ListViewItemCollection (list_view_owner, this);
146 [DesignerSerializationVisibility(0)]
148 public ListView ListView {
149 get { return list_view_owner; }
152 internal ListView ListViewOwner {
153 get { return list_view_owner; }
155 list_view_owner = value;
156 if (!is_default_group)
161 internal Rectangle HeaderBounds {
163 Rectangle retval = header_bounds;
164 retval.X -= list_view_owner.h_marker;
165 retval.Y -= list_view_owner.v_marker;
169 if (list_view_owner != null)
170 list_view_owner.item_control.Invalidate (HeaderBounds);
172 header_bounds = value;
174 if (list_view_owner != null)
175 list_view_owner.item_control.Invalidate (HeaderBounds);
180 internal bool IsDefault {
182 return is_default_group;
185 is_default_group = value;
189 internal int ItemCount {
191 return is_default_group ? item_count : items.Count;
194 if (!is_default_group)
195 throw new InvalidOperationException ("ItemCount cannot be set for non-default groups.");
205 set { name = value; }
208 [TypeConverter(typeof(StringConverter))]
219 public override string ToString()
224 #region ISerializable Members
226 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
228 info.AddValue("Header", header);
229 info.AddValue("Name", name);
230 info.AddValue("HeaderAlignment", header_alignment);
231 info.AddValue("Tag", tag);
233 info.AddValue("ListViewItemCount", items.Count);
236 foreach (ListViewItem item in items)
238 info.AddValue(string.Format("ListViewItem_{0}", i), item);