use ResolveClientUrl instead of ResolveUrl to be complient with MS.NET
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BulletedList.cs
1 //
2 // System.Web.UI.WebControls.BulletedList.cs
3 //
4 // Authors:
5 //   Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 #if NET_2_0
31 using System.IO;
32 using System.Collections;
33 using System.Globalization;
34 using System.Text;
35 using System.Drawing;
36 using System.ComponentModel;
37 using System.ComponentModel.Design;
38 using System.Security.Permissions;
39
40 namespace System.Web.UI.WebControls {
41
42         // CAS
43         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45         // attributes
46         [DesignerAttribute ("System.Web.UI.Design.WebControls.BulletedListDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
47         [DefaultEventAttribute ("Click")]
48         [DefaultPropertyAttribute ("BulletStyle")]
49         [SupportsEventValidation]
50         public class BulletedList : ListControl, IPostBackEventHandler {
51                 
52                 [MonoTODO ("we are missing a new style enum, we should be using it")]
53                 protected override void AddAttributesToRender (HtmlTextWriter writer)
54                 {
55                         const string ListStyleType = "list-style-type";
56                         const string ListStyleImage = "list-style-image";
57                         
58                         bool isNumeric = false;
59                         switch (BulletStyle)
60                         {
61                                 case BulletStyle.NotSet:
62                                         break;
63                                 
64                                 case BulletStyle.Numbered:
65                                         writer.AddStyleAttribute (ListStyleType, "decimal");
66                                         isNumeric = true;
67                                         break;
68                                 
69                                 case BulletStyle.LowerAlpha:
70                                         writer.AddStyleAttribute (ListStyleType, "lower-alpha");
71                                         isNumeric = true;
72                                         break;
73                                 
74                                 case BulletStyle.UpperAlpha:
75                                         writer.AddStyleAttribute (ListStyleType, "upper-alpha");
76                                         isNumeric = true;
77                                         break;
78                                 
79                                 case BulletStyle.LowerRoman:
80                                         writer.AddStyleAttribute (ListStyleType, "lower-roman");
81                                         isNumeric = true;
82                                         break;
83                                 
84                                 case BulletStyle.UpperRoman:
85                                         writer.AddStyleAttribute (ListStyleType, "upper-roman");
86                                         isNumeric = true;
87                                         break;
88
89                                 case BulletStyle.Disc:
90                                         writer.AddStyleAttribute (ListStyleType, "disc");
91                                         break;
92                                 
93                                 case BulletStyle.Circle:
94                                         writer.AddStyleAttribute (ListStyleType, "circle");
95                                         break;
96                                 
97                                 case BulletStyle.Square:
98                                         writer.AddStyleAttribute (ListStyleType, "square");
99                                         break;
100                                                                 
101                                 case BulletStyle.CustomImage:
102                                         writer.AddStyleAttribute (ListStyleImage, "url(" + BulletImageUrl+ ")");
103                                         break;
104                         }
105
106                         if (isNumeric && FirstBulletNumber != 1)
107                                 writer.AddAttribute ("start", FirstBulletNumber.ToString ());
108                         
109                         base.AddAttributesToRender (writer);
110                 }
111
112                 protected virtual void RenderBulletText (ListItem item, int index, HtmlTextWriter writer)
113                 {
114                         switch (DisplayMode) {
115                                 case BulletedListDisplayMode.Text:
116                                         if (!item.Enabled) {
117                                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
118                                                 writer.RenderBeginTag (HtmlTextWriterTag.Span);
119                                         }
120                                         
121                                         writer.Write (item.Text);
122                                         
123                                         if (!item.Enabled)
124                                                 writer.RenderEndTag ();
125                                         
126                                         break;
127
128                                 case BulletedListDisplayMode.HyperLink:
129                                         if (Enabled && item.Enabled) {
130                                                 writer.AddAttribute (HtmlTextWriterAttribute.Href, item.Value);
131                                                 if (Target != "")
132                                                         writer.AddAttribute(HtmlTextWriterAttribute.Target, this.Target);
133                                                 
134                                         }
135                                         else
136                                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
137                                         
138                                         writer.RenderBeginTag (HtmlTextWriterTag.A);
139                                         writer.Write (item.Text);
140                                         writer.RenderEndTag ();
141                                         break;
142
143                                 case BulletedListDisplayMode.LinkButton:
144                                         if (Enabled && item.Enabled)
145                                                 writer.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, (index.ToString (CultureInfo.InvariantCulture))));
146                                         else
147                                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
148                                         writer.RenderBeginTag (HtmlTextWriterTag.A);
149                                         writer.Write (item.Text);
150                                         writer.RenderEndTag ();
151                                         break;
152                         }
153                 }
154                 
155                 protected internal override void RenderContents (HtmlTextWriter writer)
156                 {
157                         int idx = 0;
158                         foreach (ListItem i in Items) {
159                                 writer.RenderBeginTag (HtmlTextWriterTag.Li);
160                                 this.RenderBulletText (i, idx ++, writer);
161                                 writer.RenderEndTag ();
162                         }
163                 }
164
165                 protected internal override void Render (HtmlTextWriter writer)
166                 {
167                         base.Render (writer);
168                 }
169                 
170                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
171                 {
172                         RaisePostBackEvent (eventArgument);
173                 }
174                         
175                 [MonoTODO ("ListControl has a CausesValidation prop in v1.2, we need to use it")]
176                 protected virtual void RaisePostBackEvent (string eventArgument)
177                 {
178                         //if (CausesValidation)
179                         //      Page.Validate ();
180                         
181                         this.OnClick (new BulletedListEventArgs (int.Parse (eventArgument, CultureInfo.InvariantCulture)));
182                 }
183                         
184             [BrowsableAttribute (false)]
185         [EditorBrowsableAttribute (EditorBrowsableState.Never)]
186                 public override bool AutoPostBack { 
187                         get { return base.AutoPostBack; }
188                         set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
189                 }
190                 
191                 [Bindable (false)]
192                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
193                 public override int SelectedIndex {
194                         get { return -1; }
195                         set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
196                 }
197                 
198             [EditorBrowsableAttribute (EditorBrowsableState.Never)]
199                 public override ListItem SelectedItem {
200                         get { return null; }
201                 }
202
203                 [EditorBrowsable (EditorBrowsableState.Never)]
204                 [Bindable (false)]
205                 public override string SelectedValue
206                 {
207                         get { return string.Empty; }
208                         set { throw new NotSupportedException (); }
209                 }
210                 
211                 [DefaultValueAttribute ("")]
212                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
213                 [UrlPropertyAttribute]
214                 public virtual string BulletImageUrl {
215                         get { return ViewState.GetString ("BulletImageUrl", ""); }
216                         set { ViewState ["BulletImageUrl"] = value; }
217                 }
218                 
219             [DefaultValueAttribute (BulletStyle.NotSet)]
220                 public virtual BulletStyle BulletStyle {
221                         get { return (BulletStyle) ViewState.GetInt ("BulletStyle", (int) BulletStyle.NotSet); }
222                         set {
223                                 if ((int) value < 0 || (int) value > 9)
224                                         throw new ArgumentOutOfRangeException ("value");
225
226                                 ViewState ["BulletStyle"] = value;
227                         }
228                 }
229                 
230                 public override ControlCollection Controls { get { return new EmptyControlCollection (this); } }
231                 
232             [DefaultValueAttribute (BulletedListDisplayMode.Text)]
233                 public virtual BulletedListDisplayMode DisplayMode {
234                         get { return (BulletedListDisplayMode) ViewState.GetInt ("DisplayMode", (int)BulletedListDisplayMode.Text); }
235                         set {
236                                 if ((int) value < 0 || (int) value > 2)
237                                         throw new ArgumentOutOfRangeException ("value");
238
239                                 ViewState ["DisplayMode"] = value;
240                         }
241                 }
242                 
243             [DefaultValueAttribute (1)]
244                 public virtual int FirstBulletNumber {
245                         get { return ViewState.GetInt ("FirstBulletNumber", 1); }
246                         set { ViewState ["FirstBulletNumber"] = value; }
247                 }
248                 
249
250                 protected override HtmlTextWriterTag TagKey {
251                         get {
252                                 switch (BulletStyle) {                  
253                                         case BulletStyle.Numbered:
254                                         case BulletStyle.LowerAlpha:
255                                         case BulletStyle.UpperAlpha:
256                                         case BulletStyle.LowerRoman:
257                                         case BulletStyle.UpperRoman:
258                                                 return HtmlTextWriterTag.Ol;
259                                         
260                                         case BulletStyle.NotSet:
261                                         case BulletStyle.Disc:
262                                         case BulletStyle.Circle:
263                                         case BulletStyle.Square:
264                                         case BulletStyle.CustomImage:
265                                         default:
266                                                 return HtmlTextWriterTag.Ul;
267                                 }
268                         }
269                 }
270                 
271                 [DefaultValueAttribute ("")]
272                 [TypeConverter (typeof (TargetConverter))]
273                 public virtual string Target {
274                         get { return ViewState.GetString ("Target", ""); }
275                         set { ViewState ["Target"] = value; }
276                 }
277
278                 [EditorBrowsable (EditorBrowsableState.Never)]
279                 public override string Text
280                 {
281                         get { return string.Empty; }
282                         set { throw new NotSupportedException (); }
283                 }
284                 
285                 
286                 static readonly object ClickEvent = new object ();
287                 public event BulletedListEventHandler Click
288                 {
289                         add {
290                                 Events.AddHandler (ClickEvent, value);
291                         }
292                         remove {
293                                 Events.RemoveHandler (ClickEvent, value);
294                         }
295                 }
296                 
297                 protected virtual void OnClick (BulletedListEventArgs e)
298                 {
299                         if (Events != null) {
300                                 BulletedListEventHandler eh = (BulletedListEventHandler) (Events [ClickEvent]);
301                                 if (eh != null)
302                                         eh (this, e);
303                         }
304                 }
305         }
306 }
307 #endif