2006-12-16 Daniel Nauck <dna@mono-project.de>
[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
35 namespace System.Windows.Forms
36 {
37         [SerializableAttribute]
38         [ToolboxItem(false)]
39         [DesignTimeVisible(false)]
40         [DefaultProperty("Header")]
41         public sealed class ListViewGroup : ISerializable
42         {
43                 private string header = string.Empty;
44                 private string name = null;
45                 private HorizontalAlignment header_alignment = HorizontalAlignment.Left;
46                 private ListView list_view_owner = null;
47                 private ListView.ListViewItemCollection items = null;
48                 private object tag = null;
49
50                 #region ListViewGroup constructors
51
52                 public ListViewGroup()
53                 {
54                         header = "ListViewGroup";
55                         header_alignment = HorizontalAlignment.Left;
56                 }
57
58                 public ListViewGroup(string header)
59                 {
60                         this.header = header;
61                         header_alignment = HorizontalAlignment.Left;
62                 }
63
64                 public ListViewGroup(string key, string headerText)
65                 {
66                         header = headerText;
67                         name = key;
68                         header_alignment = HorizontalAlignment.Left;
69                 }
70
71                 public ListViewGroup(string header, HorizontalAlignment headerAlignment)
72                 {
73                         this.header = header;
74                         header_alignment = headerAlignment;
75                 }
76
77                 private ListViewGroup(SerializationInfo info, StreamingContext context)
78                 {
79                         header = info.GetString("Header");
80                         name = info.GetString("Name");
81                         header_alignment = (HorizontalAlignment)info.GetInt32("HeaderAlignment");
82                         tag = info.GetValue("Tag", typeof(object));
83
84                         int count = info.GetInt32("ListViewItemCount");
85                         if (count > 0) {
86                                 if(items == null)
87                                 items = new ListView.ListViewItemCollection(list_view_owner);
88
89                                 for (int i = 0; i < count; i++)
90                                 {
91                                         items.Add((ListViewItem)info.GetValue(string.Format("ListViewItem_{0}", i), typeof(ListViewItem)));
92                                 }
93                         }
94                 }
95
96                 #endregion
97
98                 #region ListViewGroup properties
99
100                 public string Header {
101                         get { return header; }
102                         set {
103                                 if (!header.Equals(value)) {
104                                         header = value;
105
106                                         if (list_view_owner != null)
107                                                 list_view_owner.Redraw(true);
108                                 }
109                         }
110                 }
111
112                 [DefaultValue (HorizontalAlignment.Left)]
113                 public HorizontalAlignment HeaderAlignment {
114                         get { return header_alignment; }
115                         set {
116                                 if (!header_alignment.Equals(value)) {
117                                         if (value != HorizontalAlignment.Left && value != HorizontalAlignment.Right &&
118                                                 value != HorizontalAlignment.Center)
119                                                 throw new InvalidEnumArgumentException("HeaderAlignment", (int)value, typeof(HorizontalAlignment));
120
121                                         header_alignment = value;
122
123                                         if (list_view_owner != null)
124                                                 list_view_owner.Redraw(true);
125                                 }
126                         }
127                 }
128
129                 [Browsable(false)]
130                 public ListView.ListViewItemCollection Items {
131                         get {
132                                 if (items == null)
133                                         items = new ListView.ListViewItemCollection(list_view_owner);
134
135                                 return items;
136                         }
137                 }
138
139                 [DesignerSerializationVisibility(0)]
140                 [Browsable(false)]
141                 public ListView ListView {
142                         get { return list_view_owner; }
143                 }
144
145                 internal ListView ListViewOwner {
146                         get { return list_view_owner; }
147                         set { list_view_owner = value; }
148                 }
149
150                 [Browsable(true)]
151                 [DefaultValue("")]
152                 public string Name {
153                         get { return name; }
154                         set { name = value; }
155                 }
156
157                 [TypeConverter(typeof(StringConverter))]
158                 [DefaultValue(null)]
159                 [Localizable(false)]
160                 [Bindable(true)]
161                 public Object Tag {
162                         get { return tag; }
163                         set { tag = value; }
164                 }
165
166                 #endregion
167
168                 public override string ToString()
169                 {
170                         return header;
171                 }
172
173                 #region ISerializable Members
174
175                 public void GetObjectData(SerializationInfo info, StreamingContext context)
176                 {
177                         info.AddValue("Header", header);
178                         info.AddValue("Name", name);
179                         info.AddValue("HeaderAlignment", header_alignment);
180                         info.AddValue("Tag", tag);
181
182                         info.AddValue("ListViewItemCount", items.Count);
183
184                         int i = 0;
185                         foreach (ListViewItem item in items)
186                         {
187                                 info.AddValue(string.Format("ListViewItem_{0}", i), item);
188                                 i++;
189                         }
190                 }
191
192                 #endregion
193         }
194 }
195 #endif