* ListViewGroup.cs: Adda TypeConverter attribute for this class,
[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         [TypeConverter (typeof (ListViewGroupConverter))]
43         public sealed class ListViewGroup : ISerializable
44         {
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 rows;
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
58
59                 #region ListViewGroup constructors
60
61                 public ListViewGroup()
62                 {
63                         header = "ListViewGroup";
64                         header_alignment = HorizontalAlignment.Left;
65                 }
66
67                 public ListViewGroup(string header)
68                 {
69                         this.header = header;
70                         header_alignment = HorizontalAlignment.Left;
71                 }
72
73                 public ListViewGroup(string key, string headerText)
74                 {
75                         header = headerText;
76                         name = key;
77                         header_alignment = HorizontalAlignment.Left;
78                 }
79
80                 public ListViewGroup(string header, HorizontalAlignment headerAlignment)
81                 {
82                         this.header = header;
83                         header_alignment = headerAlignment;
84                 }
85
86                 private ListViewGroup(SerializationInfo info, StreamingContext context)
87                 {
88                         header = info.GetString("Header");
89                         name = info.GetString("Name");
90                         header_alignment = (HorizontalAlignment)info.GetInt32("HeaderAlignment");
91                         tag = info.GetValue("Tag", typeof(object));
92
93                         int count = info.GetInt32("ListViewItemCount");
94                         if (count > 0) {
95                                 if(items == null)
96                                 items = new ListView.ListViewItemCollection(list_view_owner);
97
98                                 for (int i = 0; i < count; i++)
99                                 {
100                                         items.Add((ListViewItem)info.GetValue(string.Format("ListViewItem_{0}", i), typeof(ListViewItem)));
101                                 }
102                         }
103                 }
104
105                 #endregion
106
107                 #region ListViewGroup properties
108
109                 public string Header {
110                         get { return header; }
111                         set {
112                                 if (!header.Equals(value)) {
113                                         header = value;
114
115                                         if (list_view_owner != null)
116                                                 list_view_owner.Redraw(true);
117                                 }
118                         }
119                 }
120
121                 [DefaultValue (HorizontalAlignment.Left)]
122                 public HorizontalAlignment HeaderAlignment {
123                         get { return header_alignment; }
124                         set {
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));
129
130                                         header_alignment = value;
131
132                                         if (list_view_owner != null)
133                                                 list_view_owner.Redraw(true);
134                                 }
135                         }
136                 }
137
138                 [Browsable(false)]
139                 public ListView.ListViewItemCollection Items {
140                         get {
141                                 if (items == null)
142                                         items = new ListView.ListViewItemCollection (list_view_owner, this);
143
144                                 return items;
145                         }
146                 }
147
148                 [DesignerSerializationVisibility(0)]
149                 [Browsable(false)]
150                 public ListView ListView {
151                         get { return list_view_owner; }
152                 }
153
154                 internal ListView ListViewOwner {
155                         get { return list_view_owner; }
156                         set { 
157                                 list_view_owner = value; 
158                                 if (!is_default_group)
159                                         items.Owner = value;
160                         }
161                 }
162
163                 internal Rectangle HeaderBounds {
164                         get {
165                                 Rectangle retval = header_bounds;
166                                 retval.X -= list_view_owner.h_marker;
167                                 retval.Y -= list_view_owner.v_marker;
168                                 return retval;
169                         }
170                         set { 
171                                 if (list_view_owner != null)
172                                         list_view_owner.item_control.Invalidate (HeaderBounds);
173
174                                 header_bounds = value; 
175
176                                 if (list_view_owner != null)
177                                         list_view_owner.item_control.Invalidate (HeaderBounds);
178
179                         }
180                 }
181
182                 internal bool IsDefault {
183                         get {
184                                 return is_default_group;
185                         }
186                         set {
187                                 is_default_group = value;
188                         }
189                 }
190
191                 internal int ItemCount {
192                         get {
193                                 return is_default_group ? item_count : items.Count;
194                         }
195                         set {
196                                 if (!is_default_group)
197                                         throw new InvalidOperationException ("ItemCount cannot be set for non-default groups.");
198
199                                 item_count = value;
200                         }
201                 }
202
203                 [Browsable(true)]
204                 [DefaultValue("")]
205                 public string Name {
206                         get { return name; }
207                         set { name = value; }
208                 }
209
210                 [TypeConverter(typeof(StringConverter))]
211                 [DefaultValue(null)]
212                 [Localizable(false)]
213                 [Bindable(true)]
214                 public Object Tag {
215                         get { return tag; }
216                         set { tag = value; }
217                 }
218
219                 #endregion
220
221                 public override string ToString()
222                 {
223                         return header;
224                 }
225
226                 #region ISerializable Members
227
228                 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
229                 {
230                         info.AddValue("Header", header);
231                         info.AddValue("Name", name);
232                         info.AddValue("HeaderAlignment", header_alignment);
233                         info.AddValue("Tag", tag);
234
235                         info.AddValue("ListViewItemCount", items.Count);
236
237                         int i = 0;
238                         foreach (ListViewItem item in items)
239                         {
240                                 info.AddValue(string.Format("ListViewItem_{0}", i), item);
241                                 i++;
242                         }
243                 }
244
245                 #endregion
246         }
247
248         internal class ListViewGroupConverter : TypeConverter
249         {
250                 public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
251                 {
252                         return true;
253                 }
254
255                 // Weird
256                 public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
257                 {
258                         return new StandardValuesCollection (new object [] {});
259                 }
260         }
261 }
262 #endif