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