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