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