Flush (work in progress)
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / TreeNodeStyle.cs
1 //
2 // System.Web.UI.WebControls.TreeNodeStyle.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2004 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
29 //
30
31 #if NET_2_0
32
33 using System;
34 using System.Web.UI;
35 using System.ComponentModel;
36
37 namespace System.Web.UI.WebControls
38 {
39         public sealed class TreeNodeStyle: Style
40         {
41                 const string CHILD_PADD = "ChildNodesPadding";
42                 const string HORZ_PADD = "HorizontalPadding";
43                 const string IMG_URL = "ImageUrl";
44                 const string SPACING = "NodeSpacing";
45                 const string VERT_PADD = "VerticalPadding";
46
47                 [Flags]
48                 enum TreeNodeStyles
49                 {
50                         ChildNodesPadding = 0x00010000,
51                         HorizontalPadding = 0x00020000,
52                         ImageUrl = 0x00040000,
53                         NodeSpacing = 0x00080000,
54                         VerticalPadding = 0x00100000,
55                 }
56                 
57                 public TreeNodeStyle ()
58                         : base ()
59                 {
60                 }
61
62                 public TreeNodeStyle (StateBag bag)
63                         : base (bag)
64                 {
65                 }
66
67                 [DefaultValue ("")]
68                 [UrlProperty]
69                 [NotifyParentProperty (true)]
70                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
71                 public string ImageUrl {
72                         get {
73                                 if (!CheckBit ((int) TreeNodeStyles.ImageUrl))
74                                         return String.Empty;
75                                 return ViewState.GetString (IMG_URL, String.Empty);
76                         }
77                         set {
78                                 if(value == null)
79                                         throw new ArgumentNullException("value");
80                                 ViewState [IMG_URL] = value;
81                                 SetBit ((int) TreeNodeStyles.ImageUrl);
82                         }
83                 }
84
85                 [DefaultValue (0)]
86                 [NotifyParentProperty (true)]
87                 public Unit ChildNodesPadding {
88                         get {
89                                 if (!CheckBit ((int) TreeNodeStyles.ChildNodesPadding))
90                                         return 0;
91                                 return ViewState [CHILD_PADD] == null ? 0 : (Unit) ViewState [CHILD_PADD];
92                         }
93                         set {
94                                 ViewState [CHILD_PADD] = value;
95                                 SetBit ((int) TreeNodeStyles.ChildNodesPadding);
96                         }
97                 }
98
99                 [DefaultValue (0)]
100                 [NotifyParentProperty (true)]
101                 public Unit HorizontalPadding {
102                         get {
103                                 if (!CheckBit ((int) TreeNodeStyles.HorizontalPadding))
104                                         return 0;
105                                 return ViewState [HORZ_PADD] == null ? 0 : (Unit) ViewState [HORZ_PADD];
106                         }
107                         set {
108                                 ViewState[HORZ_PADD] = value;
109                                 SetBit ((int) TreeNodeStyles.HorizontalPadding);
110                         }
111                 }
112
113                 [DefaultValue (0)]
114                 [NotifyParentProperty (true)]
115                 public Unit VerticalPadding {
116                         get {
117                                 if (!CheckBit ((int) TreeNodeStyles.VerticalPadding))
118                                         return 0;
119                                 return ViewState [VERT_PADD] == null ? 0 : (Unit) ViewState [VERT_PADD];
120                         }
121                         set {
122                                 ViewState [VERT_PADD] = value;
123                                 SetBit ((int) TreeNodeStyles.VerticalPadding);
124                         }
125                 }
126
127                 [DefaultValue (0)]
128                 [NotifyParentProperty (true)]
129                 public Unit NodeSpacing {
130                         get {
131                                 if (!CheckBit ((int) TreeNodeStyles.NodeSpacing))
132                                         return 0;
133                                 return ViewState [SPACING] == null ? 0 : (Unit) ViewState [SPACING];
134                         }
135                         set {
136                                 ViewState [SPACING] = value;
137                                 SetBit ((int) TreeNodeStyles.NodeSpacing);
138                         }
139                 }
140                 
141                 public override void CopyFrom (Style s)
142                 {
143                         if (s == null || s.IsEmpty)
144                                 return;
145
146                         base.CopyFrom (s);
147                         TreeNodeStyle from = s as TreeNodeStyle;
148                         if (from == null)
149                                 return;
150
151                         if (from.CheckBit ((int) TreeNodeStyles.ChildNodesPadding))
152                                 ChildNodesPadding = from.ChildNodesPadding;
153
154                         if (from.CheckBit ((int) TreeNodeStyles.HorizontalPadding))
155                                 HorizontalPadding = from.HorizontalPadding;
156
157                         if (from.CheckBit ((int) TreeNodeStyles.ImageUrl))
158                                 ImageUrl = from.ImageUrl;
159
160                         if (from.CheckBit ((int) TreeNodeStyles.NodeSpacing))
161                                 NodeSpacing = from.NodeSpacing;
162
163                         if (from.CheckBit ((int) TreeNodeStyles.VerticalPadding))
164                                 VerticalPadding = from.VerticalPadding;
165                 }
166                 
167                 public override void MergeWith(Style s)
168                 {
169                         if(s != null && !s.IsEmpty)
170                         {
171                                 if (IsEmpty) {
172                                         CopyFrom (s);
173                                         return;
174                                 }
175                                 base.MergeWith(s);
176
177                                 TreeNodeStyle with = s as TreeNodeStyle;
178                                 if (with == null) return;
179                                 
180                                 if (with.CheckBit ((int) TreeNodeStyles.ChildNodesPadding) && !CheckBit ((int) TreeNodeStyles.ChildNodesPadding)) {
181                                         ChildNodesPadding = with.ChildNodesPadding;
182                                 }
183                                 if (with.CheckBit ((int) TreeNodeStyles.HorizontalPadding) && !CheckBit ((int) TreeNodeStyles.HorizontalPadding)) {
184                                         HorizontalPadding = with.HorizontalPadding;
185                                 }
186                                 if (with.CheckBit ((int) TreeNodeStyles.ImageUrl) && !CheckBit ((int) TreeNodeStyles.ImageUrl)) {
187                                         ImageUrl = with.ImageUrl;
188                                 }
189                                 if (with.CheckBit ((int) TreeNodeStyles.NodeSpacing) && !CheckBit ((int) TreeNodeStyles.NodeSpacing)) {
190                                         NodeSpacing = with.NodeSpacing;
191                                 }
192                                 if (with.CheckBit ((int) TreeNodeStyles.VerticalPadding) && !CheckBit ((int) TreeNodeStyles.VerticalPadding)) {
193                                         VerticalPadding = with.VerticalPadding;
194                                 }
195                         }
196                 }
197
198                 public override void Reset()
199                 {
200                         if (CheckBit ((int) TreeNodeStyles.ChildNodesPadding))
201                                 ViewState.Remove(CHILD_PADD);
202                         if (CheckBit ((int) TreeNodeStyles.HorizontalPadding))
203                                 ViewState.Remove(HORZ_PADD);
204                         if (CheckBit ((int) TreeNodeStyles.ImageUrl))
205                                 ViewState.Remove(IMG_URL);
206                         if (CheckBit ((int) TreeNodeStyles.NodeSpacing))
207                                 ViewState.Remove(SPACING);
208                         if (CheckBit ((int) TreeNodeStyles.VerticalPadding))
209                                 ViewState.Remove(VERT_PADD);
210                         base.Reset();
211                 }
212
213                 protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver) {
214                         base.FillStyleAttributes (attributes, urlResolver);
215                         if (CheckBit ((int) TreeNodeStyles.HorizontalPadding)) {
216                                 attributes.Add (HtmlTextWriterStyle.PaddingLeft, HorizontalPadding.ToString ());
217                                 attributes.Add (HtmlTextWriterStyle.PaddingRight, HorizontalPadding.ToString ());
218                         }
219                         if (CheckBit ((int) TreeNodeStyles.VerticalPadding)) {
220                                 attributes.Add (HtmlTextWriterStyle.PaddingTop, VerticalPadding.ToString ());
221                                 attributes.Add (HtmlTextWriterStyle.PaddingBottom, VerticalPadding.ToString ());
222                         }
223                 }
224         }
225 }
226
227 #endif