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