2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Web / System.Web.UI / CssStyleCollection.cs
1 //
2 // System.Web.UI.CssStyleCollection.cs
3 //
4 // Authors:
5 //      Duncan Mak  (duncan@ximian.com)
6 //      Gonzalo Paniagua (gonzalo@ximian.com)
7 //
8 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Collections;
34 using System.Text;
35
36 namespace System.Web.UI {
37
38         public sealed class CssStyleCollection
39         {
40                 private StateBag bag;
41                 private StateBag style;
42
43                 internal CssStyleCollection ()
44                 {
45                         style = new StateBag ();
46                 }
47                 
48                 internal CssStyleCollection (StateBag bag)
49                 {
50                         this.bag = bag;
51                         style = new StateBag ();
52                         string st_string = bag ["style"] as string;
53                         if (st_string != null)
54                                 FillStyle (st_string);
55                 }
56
57                 internal void FillStyle (string s)
58                 {
59                         int mark = s.IndexOf (':');
60                         if (mark == -1)
61                                 return;
62                         string key = s.Substring (0, mark). Trim ();
63                         if (mark + 1 > s.Length)
64                                 return;
65
66                         string fullValue = s.Substring (mark + 1);
67                         if (fullValue == "")
68                                 return;
69
70                         mark = fullValue.IndexOf (';');
71                         string value;
72                         if (mark == -1)
73                                 value = fullValue.Trim ();
74                         else
75                                 value = fullValue.Substring (0, mark).Trim ();
76
77                         style.Add (key, value);
78                         if (mark + 1 > fullValue.Length)
79                                 return;
80                         FillStyle (fullValue.Substring (mark + 1));
81                 }
82
83                 internal string BagToString ()
84                 {
85                         StringBuilder sb = new StringBuilder ();
86                         foreach (string k in style.Keys)
87                                 sb.AppendFormat ("{0}: {1}; ", k, style [k]);
88                         return sb.ToString ();
89                 }
90                 
91                 public int Count
92                 {
93                         get { return style.Count; }
94                 }
95
96                 public string this [string key]
97                 {
98                         get {
99                                 return style [key] as string;
100                         }
101
102                         set {
103                                 Add (key, value);
104                         }
105                 }
106
107                 public ICollection Keys {
108                         get { return style.Keys; }
109                 }
110
111                 public void Add (string key, string value)
112                 {
113                         style [key] = value;
114                         if (bag != null)
115                                 bag ["style"] = BagToString ();
116                 }
117                 
118 #if NET_2_0
119                 public
120 #else
121                 internal
122 #endif
123                 void Add (HtmlTextWriterStyle key, string value)
124                 {
125                         Add (HtmlTextWriter.StaticGetStyleName (key), value);
126                 }
127
128                 public void Clear ()
129                 {
130                         if (bag != null)
131                                 bag.Remove ("style");
132                         style.Clear ();
133                 }
134
135                 public void Remove (string key)
136                 {
137                         if (style [key] != null) {
138                                 style.Remove (key);
139                                 if (bag != null)
140                                         bag ["style"] = BagToString ();
141                         }
142                 }
143         }
144 }
145