New test.
[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                 PostBackOptions postBackOptions;
53
54                 [MonoTODO ("we are missing a new style enum, we should be using it")]
55                 protected override void AddAttributesToRender (HtmlTextWriter writer)
56                 {
57                         const string ListStyleType = "list-style-type";
58                         const string ListStyleImage = "list-style-image";
59                         
60                         bool isNumeric = false;
61                         switch (BulletStyle)
62                         {
63                                 case BulletStyle.NotSet:
64                                         break;
65                                 
66                                 case BulletStyle.Numbered:
67                                         writer.AddStyleAttribute (ListStyleType, "decimal");
68                                         isNumeric = true;
69                                         break;
70                                 
71                                 case BulletStyle.LowerAlpha:
72                                         writer.AddStyleAttribute (ListStyleType, "lower-alpha");
73                                         isNumeric = true;
74                                         break;
75                                 
76                                 case BulletStyle.UpperAlpha:
77                                         writer.AddStyleAttribute (ListStyleType, "upper-alpha");
78                                         isNumeric = true;
79                                         break;
80                                 
81                                 case BulletStyle.LowerRoman:
82                                         writer.AddStyleAttribute (ListStyleType, "lower-roman");
83                                         isNumeric = true;
84                                         break;
85                                 
86                                 case BulletStyle.UpperRoman:
87                                         writer.AddStyleAttribute (ListStyleType, "upper-roman");
88                                         isNumeric = true;
89                                         break;
90
91                                 case BulletStyle.Disc:
92                                         writer.AddStyleAttribute (ListStyleType, "disc");
93                                         break;
94                                 
95                                 case BulletStyle.Circle:
96                                         writer.AddStyleAttribute (ListStyleType, "circle");
97                                         break;
98                                 
99                                 case BulletStyle.Square:
100                                         writer.AddStyleAttribute (ListStyleType, "square");
101                                         break;
102                                                                 
103                                 case BulletStyle.CustomImage:
104                                         writer.AddStyleAttribute (ListStyleImage, "url(" + BulletImageUrl+ ")");
105                                         break;
106                         }
107
108                         if (isNumeric && FirstBulletNumber != 1)
109                                 writer.AddAttribute ("start", FirstBulletNumber.ToString ());
110                         
111                         base.AddAttributesToRender (writer);
112                 }
113
114                 protected virtual void RenderBulletText (ListItem item, int index, HtmlTextWriter writer)
115                 {
116                         switch (DisplayMode) {
117                                 case BulletedListDisplayMode.Text:
118                                         if (!item.Enabled) {
119                                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
120                                                 writer.RenderBeginTag (HtmlTextWriterTag.Span);
121                                         }
122                                         
123                                         writer.Write (item.Text);
124                                         
125                                         if (!item.Enabled)
126                                                 writer.RenderEndTag ();
127                                         
128                                         break;
129
130                                 case BulletedListDisplayMode.HyperLink:
131                                         if (Enabled && item.Enabled) {
132                                                 writer.AddAttribute (HtmlTextWriterAttribute.Href, item.Value);
133                                                 if (Target != "")
134                                                         writer.AddAttribute(HtmlTextWriterAttribute.Target, this.Target);
135                                                 
136                                         }
137                                         else
138                                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
139                                         
140                                         writer.RenderBeginTag (HtmlTextWriterTag.A);
141                                         writer.Write (item.Text);
142                                         writer.RenderEndTag ();
143                                         break;
144
145                                 case BulletedListDisplayMode.LinkButton:
146                                         if (Enabled && item.Enabled)
147                                                 writer.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (index.ToString (CultureInfo.InvariantCulture))));
148                                         else
149                                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
150                                         writer.RenderBeginTag (HtmlTextWriterTag.A);
151                                         writer.Write (item.Text);
152                                         writer.RenderEndTag ();
153                                         break;
154                         }
155                 }
156
157                 PostBackOptions GetPostBackOptions (string argument) {
158                         if (postBackOptions == null) {
159                                 postBackOptions = new PostBackOptions (this);
160                                 postBackOptions.ActionUrl = null;
161                                 postBackOptions.ValidationGroup = null;
162                                 postBackOptions.RequiresJavaScriptProtocol = true;
163                                 postBackOptions.ClientSubmit = true;
164                                 postBackOptions.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
165                                 if (postBackOptions.PerformValidation)
166                                         postBackOptions.ValidationGroup = ValidationGroup;
167                         }
168                         postBackOptions.Argument = argument;
169                         return postBackOptions;
170                 }
171                 
172                 protected internal override void RenderContents (HtmlTextWriter writer)
173                 {
174                         int idx = 0;
175 #if NET_2_0
176                         bool havePage = Page != null;
177 #endif
178                         foreach (ListItem i in Items) {
179 #if NET_2_0
180                                 if (havePage)
181                                         Page.ClientScript.RegisterForEventValidation (this.UniqueID, i.Value.ToString ());
182 #endif
183                                 writer.RenderBeginTag (HtmlTextWriterTag.Li);
184                                 this.RenderBulletText (i, idx ++, writer);
185                                 writer.RenderEndTag ();
186                         }
187                 }
188
189                 protected internal override void Render (HtmlTextWriter writer)
190                 {
191                         base.Render (writer);
192                 }
193                 
194                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
195                 {
196                         RaisePostBackEvent (eventArgument);
197                 }
198                         
199                 protected virtual void RaisePostBackEvent (string eventArgument)
200                 {
201                         if (CausesValidation)
202                                 Page.Validate (ValidationGroup);
203                         
204                         this.OnClick (new BulletedListEventArgs (int.Parse (eventArgument, CultureInfo.InvariantCulture)));
205                 }
206                         
207             [BrowsableAttribute (false)]
208         [EditorBrowsableAttribute (EditorBrowsableState.Never)]
209                 public override bool AutoPostBack { 
210                         get { return base.AutoPostBack; }
211                         set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
212                 }
213                 
214                 [Bindable (false)]
215                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
216                 public override int SelectedIndex {
217                         get { return -1; }
218                         set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
219                 }
220                 
221             [EditorBrowsableAttribute (EditorBrowsableState.Never)]
222                 public override ListItem SelectedItem {
223                         get { return null; }
224                 }
225
226                 [EditorBrowsable (EditorBrowsableState.Never)]
227                 [Bindable (false)]
228                 public override string SelectedValue
229                 {
230                         get { return string.Empty; }
231                         set { throw new NotSupportedException (); }
232                 }
233                 
234                 [DefaultValueAttribute ("")]
235                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
236                 [UrlPropertyAttribute]
237                 public virtual string BulletImageUrl {
238                         get { return ViewState.GetString ("BulletImageUrl", ""); }
239                         set { ViewState ["BulletImageUrl"] = value; }
240                 }
241                 
242             [DefaultValueAttribute (BulletStyle.NotSet)]
243                 public virtual BulletStyle BulletStyle {
244                         get { return (BulletStyle) ViewState.GetInt ("BulletStyle", (int) BulletStyle.NotSet); }
245                         set {
246                                 if ((int) value < 0 || (int) value > 9)
247                                         throw new ArgumentOutOfRangeException ("value");
248
249                                 ViewState ["BulletStyle"] = value;
250                         }
251                 }
252                 
253                 public override ControlCollection Controls { get { return new EmptyControlCollection (this); } }
254                 
255             [DefaultValueAttribute (BulletedListDisplayMode.Text)]
256                 public virtual BulletedListDisplayMode DisplayMode {
257                         get { return (BulletedListDisplayMode) ViewState.GetInt ("DisplayMode", (int)BulletedListDisplayMode.Text); }
258                         set {
259                                 if ((int) value < 0 || (int) value > 2)
260                                         throw new ArgumentOutOfRangeException ("value");
261
262                                 ViewState ["DisplayMode"] = value;
263                         }
264                 }
265                 
266             [DefaultValueAttribute (1)]
267                 public virtual int FirstBulletNumber {
268                         get { return ViewState.GetInt ("FirstBulletNumber", 1); }
269                         set { ViewState ["FirstBulletNumber"] = value; }
270                 }
271                 
272
273                 protected override HtmlTextWriterTag TagKey {
274                         get {
275                                 switch (BulletStyle) {                  
276                                         case BulletStyle.Numbered:
277                                         case BulletStyle.LowerAlpha:
278                                         case BulletStyle.UpperAlpha:
279                                         case BulletStyle.LowerRoman:
280                                         case BulletStyle.UpperRoman:
281                                                 return HtmlTextWriterTag.Ol;
282                                         
283                                         case BulletStyle.NotSet:
284                                         case BulletStyle.Disc:
285                                         case BulletStyle.Circle:
286                                         case BulletStyle.Square:
287                                         case BulletStyle.CustomImage:
288                                         default:
289                                                 return HtmlTextWriterTag.Ul;
290                                 }
291                         }
292                 }
293                 
294                 [DefaultValueAttribute ("")]
295                 [TypeConverter (typeof (TargetConverter))]
296                 public virtual string Target {
297                         get { return ViewState.GetString ("Target", ""); }
298                         set { ViewState ["Target"] = value; }
299                 }
300
301                 [EditorBrowsable (EditorBrowsableState.Never)]
302                 public override string Text
303                 {
304                         get { return string.Empty; }
305                         set { throw new NotSupportedException (); }
306                 }
307                 
308                 
309                 static readonly object ClickEvent = new object ();
310                 public event BulletedListEventHandler Click
311                 {
312                         add {
313                                 Events.AddHandler (ClickEvent, value);
314                         }
315                         remove {
316                                 Events.RemoveHandler (ClickEvent, value);
317                         }
318                 }
319                 
320                 protected virtual void OnClick (BulletedListEventArgs e)
321                 {
322                         if (Events != null) {
323                                 BulletedListEventHandler eh = (BulletedListEventHandler) (Events [ClickEvent]);
324                                 if (eh != null)
325                                         eh (this, e);
326                         }
327                 }
328         }
329 }
330 #endif