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