5b42ba371c1e9ffbd26bdde2c950bc6158dfcc09
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ListViewGroup.cs
1 //
2 //  ListViewGroup.cs
3 //
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:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
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.
22 //
23 // Copyright (c) 2006 Daniel Nauck
24 //
25 // Author:
26 //      Daniel Nauck            (dna(at)mono-project(dot)de)
27
28 #if NET_2_0
29
30 using System;
31 using System.Text;
32 using System.Runtime.Serialization;
33 using System.ComponentModel;
34 using System.Drawing;
35
36 namespace System.Windows.Forms
37 {
38         [SerializableAttribute]
39         [ToolboxItem(false)]
40         [DesignTimeVisible(false)]
41         [DefaultProperty("Header")]
42         public sealed class ListViewGroup : ISerializable
43         {
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
56
57                 #region ListViewGroup constructors
58
59                 public ListViewGroup()
60                 {
61                         header = "ListViewGroup";
62                         header_alignment = HorizontalAlignment.Left;
63                 }
64
65                 public ListViewGroup(string header)
66                 {
67                         this.header = header;
68                         header_alignment = HorizontalAlignment.Left;
69                 }
70
71                 public ListViewGroup(string key, string headerText)
72                 {
73                         header = headerText;
74                         name = key;
75                         header_alignment = HorizontalAlignment.Left;
76                 }
77
78                 public ListViewGroup(string header, HorizontalAlignment headerAlignment)
79                 {
80                         this.header = header;
81                         header_alignment = headerAlignment;
82                 }
83
84                 private ListViewGroup(SerializationInfo info, StreamingContext context)
85                 {
86                         header = info.GetString("Header");
87                         name = info.GetString("Name");
88                         header_alignment = (HorizontalAlignment)info.GetInt32("HeaderAlignment");
89                         tag = info.GetValue("Tag", typeof(object));
90
91                         int count = info.GetInt32("ListViewItemCount");
92                         if (count > 0) {
93                                 if(items == null)
94                                 items = new ListView.ListViewItemCollection(list_view_owner);
95
96                                 for (int i = 0; i < count; i++)
97                                 {
98                                         items.Add((ListViewItem)info.GetValue(string.Format("ListViewItem_{0}", i), typeof(ListViewItem)));
99                                 }
100                         }
101                 }
102
103                 #endregion
104
105                 #region ListViewGroup properties
106
107                 public string Header {
108                         get { return header; }
109                         set {
110                                 if (!header.Equals(value)) {
111                                         header = value;
112
113                                         if (list_view_owner != null)
114                                                 list_view_owner.Redraw(true);
115                                 }
116                         }
117                 }
118
119                 [DefaultValue (HorizontalAlignment.Left)]
120                 public HorizontalAlignment HeaderAlignment {
121                         get { return header_alignment; }
122                         set {
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));
127
128                                         header_alignment = value;
129
130                                         if (list_view_owner != null)
131                                                 list_view_owner.Redraw(true);
132                                 }
133                         }
134                 }
135
136                 [Browsable(false)]
137                 public ListView.ListViewItemCollection Items {
138                         get {
139                                 if (items == null)
140                                         items = new ListView.ListViewItemCollection (list_view_owner, this);
141
142                                 return items;
143                         }
144                 }
145
146                 [DesignerSerializationVisibility(0)]
147                 [Browsable(false)]
148                 public ListView ListView {
149                         get { return list_view_owner; }
150                 }
151
152                 internal ListView ListViewOwner {
153                         get { return list_view_owner; }
154                         set { 
155                                 list_view_owner = value; 
156                                 if (!is_default_group)
157                                         items.Owner = value;
158                         }
159                 }
160
161                 internal Rectangle HeaderBounds {
162                         get {
163                                 Rectangle retval = header_bounds;
164                                 retval.X -= list_view_owner.h_marker;
165                                 retval.Y -= list_view_owner.v_marker;
166                                 return retval;
167                         }
168                         set { 
169                                 if (list_view_owner != null)
170                                         list_view_owner.item_control.Invalidate (HeaderBounds);
171
172                                 header_bounds = value; 
173
174                                 if (list_view_owner != null)
175                                         list_view_owner.item_control.Invalidate (HeaderBounds);
176
177                         }
178                 }
179
180                 internal bool IsDefault {
181                         get {
182                                 return is_default_group;
183                         }
184                         set {
185                                 is_default_group = value;
186                         }
187                 }
188
189                 internal int ItemCount {
190                         get {
191                                 return is_default_group ? item_count : items.Count;
192                         }
193                         set {
194                                 if (!is_default_group)
195                                         throw new InvalidOperationException ("ItemCount cannot be set for non-default groups.");
196
197                                 item_count = value;
198                         }
199                 }
200
201                 [Browsable(true)]
202                 [DefaultValue("")]
203                 public string Name {
204                         get { return name; }
205                         set { name = value; }
206                 }
207
208                 [TypeConverter(typeof(StringConverter))]
209                 [DefaultValue(null)]
210                 [Localizable(false)]
211                 [Bindable(true)]
212                 public Object Tag {
213                         get { return tag; }
214                         set { tag = value; }
215                 }
216
217                 #endregion
218
219                 public override string ToString()
220                 {
221                         return header;
222                 }
223
224                 #region ISerializable Members
225
226                 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
227                 {
228                         info.AddValue("Header", header);
229                         info.AddValue("Name", name);
230                         info.AddValue("HeaderAlignment", header_alignment);
231                         info.AddValue("Tag", tag);
232
233                         info.AddValue("ListViewItemCount", items.Count);
234
235                         int i = 0;
236                         foreach (ListViewItem item in items)
237                         {
238                                 info.AddValue(string.Format("ListViewItem_{0}", i), item);
239                                 i++;
240                         }
241                 }
242
243                 #endregion
244         }
245 }
246 #endif