Mark tests as not working under TARGET_JVM
[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
183                                 if (i.HasAttributes)
184                                         i.Attributes.AddAttributes (writer);
185 #endif
186                                 writer.RenderBeginTag (HtmlTextWriterTag.Li);
187                                 this.RenderBulletText (i, idx ++, writer);
188                                 writer.RenderEndTag ();
189                         }
190                 }
191
192                 protected internal override void Render (HtmlTextWriter writer)
193                 {
194                         base.Render (writer);
195                 }
196                 
197                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
198                 {
199                         RaisePostBackEvent (eventArgument);
200                 }
201                         
202                 protected virtual void RaisePostBackEvent (string eventArgument)
203                 {
204                         if (CausesValidation)
205                                 Page.Validate (ValidationGroup);
206                         
207                         this.OnClick (new BulletedListEventArgs (int.Parse (eventArgument, CultureInfo.InvariantCulture)));
208                 }
209                         
210             [BrowsableAttribute (false)]
211         [EditorBrowsableAttribute (EditorBrowsableState.Never)]
212                 public override bool AutoPostBack { 
213                         get { return base.AutoPostBack; }
214                         set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
215                 }
216                 
217                 [Bindable (false)]
218                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
219                 public override int SelectedIndex {
220                         get { return -1; }
221                         set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
222                 }
223                 
224             [EditorBrowsableAttribute (EditorBrowsableState.Never)]
225                 public override ListItem SelectedItem {
226                         get { return null; }
227                 }
228
229                 [EditorBrowsable (EditorBrowsableState.Never)]
230                 [Bindable (false)]
231                 public override string SelectedValue
232                 {
233                         get { return string.Empty; }
234                         set { throw new NotSupportedException (); }
235                 }
236                 
237                 [DefaultValueAttribute ("")]
238                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
239                 [UrlPropertyAttribute]
240                 public virtual string BulletImageUrl {
241                         get { return ViewState.GetString ("BulletImageUrl", ""); }
242                         set { ViewState ["BulletImageUrl"] = value; }
243                 }
244                 
245             [DefaultValueAttribute (BulletStyle.NotSet)]
246                 public virtual BulletStyle BulletStyle {
247                         get { return (BulletStyle) ViewState.GetInt ("BulletStyle", (int) BulletStyle.NotSet); }
248                         set {
249                                 if ((int) value < 0 || (int) value > 9)
250                                         throw new ArgumentOutOfRangeException ("value");
251
252                                 ViewState ["BulletStyle"] = value;
253                         }
254                 }
255                 
256                 public override ControlCollection Controls { get { return new EmptyControlCollection (this); } }
257                 
258             [DefaultValueAttribute (BulletedListDisplayMode.Text)]
259                 public virtual BulletedListDisplayMode DisplayMode {
260                         get { return (BulletedListDisplayMode) ViewState.GetInt ("DisplayMode", (int)BulletedListDisplayMode.Text); }
261                         set {
262                                 if ((int) value < 0 || (int) value > 2)
263                                         throw new ArgumentOutOfRangeException ("value");
264
265                                 ViewState ["DisplayMode"] = value;
266                         }
267                 }
268                 
269             [DefaultValueAttribute (1)]
270                 public virtual int FirstBulletNumber {
271                         get { return ViewState.GetInt ("FirstBulletNumber", 1); }
272                         set { ViewState ["FirstBulletNumber"] = value; }
273                 }
274                 
275
276                 protected override HtmlTextWriterTag TagKey {
277                         get {
278                                 switch (BulletStyle) {                  
279                                         case BulletStyle.Numbered:
280                                         case BulletStyle.LowerAlpha:
281                                         case BulletStyle.UpperAlpha:
282                                         case BulletStyle.LowerRoman:
283                                         case BulletStyle.UpperRoman:
284                                                 return HtmlTextWriterTag.Ol;
285                                         
286                                         case BulletStyle.NotSet:
287                                         case BulletStyle.Disc:
288                                         case BulletStyle.Circle:
289                                         case BulletStyle.Square:
290                                         case BulletStyle.CustomImage:
291                                         default:
292                                                 return HtmlTextWriterTag.Ul;
293                                 }
294                         }
295                 }
296                 
297                 [DefaultValueAttribute ("")]
298                 [TypeConverter (typeof (TargetConverter))]
299                 public virtual string Target {
300                         get { return ViewState.GetString ("Target", ""); }
301                         set { ViewState ["Target"] = value; }
302                 }
303
304                 [EditorBrowsable (EditorBrowsableState.Never)]
305                 public override string Text
306                 {
307                         get { return string.Empty; }
308                         set { throw new NotSupportedException (); }
309                 }
310                 
311                 
312                 static readonly object ClickEvent = new object ();
313                 public event BulletedListEventHandler Click
314                 {
315                         add {
316                                 Events.AddHandler (ClickEvent, value);
317                         }
318                         remove {
319                                 Events.RemoveHandler (ClickEvent, value);
320                         }
321                 }
322                 
323                 protected virtual void OnClick (BulletedListEventArgs e)
324                 {
325                         if (Events != null) {
326                                 BulletedListEventHandler eh = (BulletedListEventHandler) (Events [ClickEvent]);
327                                 if (eh != null)
328                                         eh (this, e);
329                         }
330                 }
331         }
332 }
333 #endif