merge -r 98047:98048
[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(" + ResolveClientUrl (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                         string text = HttpUtility.HtmlEncode (item.Text);
117                         
118                         switch (DisplayMode) {
119                                 case BulletedListDisplayMode.Text:
120                                         if (!item.Enabled) {
121                                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
122                                                 writer.RenderBeginTag (HtmlTextWriterTag.Span);
123                                         }
124                                         
125                                         writer.Write (text);
126                                         
127                                         if (!item.Enabled)
128                                                 writer.RenderEndTag ();
129                                         
130                                         break;
131
132                                 case BulletedListDisplayMode.HyperLink:
133                                         if (Enabled && item.Enabled) {
134                                                 writer.AddAttribute (HtmlTextWriterAttribute.Href, item.Value);
135                                                 if (Target != "")
136                                                         writer.AddAttribute(HtmlTextWriterAttribute.Target, this.Target);
137                                                 
138                                         }
139                                         else
140                                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
141                                         
142                                         writer.RenderBeginTag (HtmlTextWriterTag.A);
143                                         writer.Write (text);
144                                         writer.RenderEndTag ();
145                                         break;
146
147                                 case BulletedListDisplayMode.LinkButton:
148                                         if (Enabled && item.Enabled)
149                                                 writer.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (index.ToString (CultureInfo.InvariantCulture)), true));
150                                         else
151                                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
152                                         writer.RenderBeginTag (HtmlTextWriterTag.A);
153                                         writer.Write (text);
154                                         writer.RenderEndTag ();
155                                         break;
156                         }
157                 }
158
159                 PostBackOptions GetPostBackOptions (string argument) {
160                         if (postBackOptions == null) {
161                                 postBackOptions = new PostBackOptions (this);
162                                 postBackOptions.ActionUrl = null;
163                                 postBackOptions.ValidationGroup = null;
164                                 postBackOptions.RequiresJavaScriptProtocol = true;
165                                 postBackOptions.ClientSubmit = true;
166                                 postBackOptions.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
167                                 if (postBackOptions.PerformValidation)
168                                         postBackOptions.ValidationGroup = ValidationGroup;
169                         }
170                         postBackOptions.Argument = argument;
171                         return postBackOptions;
172                 }
173                 
174                 protected internal override void RenderContents (HtmlTextWriter writer)
175                 {
176                         int idx = 0;
177 #if NET_2_0
178                         bool havePage = Page != null;
179 #endif
180                         foreach (ListItem i in Items) {
181 #if NET_2_0
182                                 if (havePage)
183                                         Page.ClientScript.RegisterForEventValidation (this.UniqueID, i.Value.ToString ());
184
185                                 if (i.HasAttributes)
186                                         i.Attributes.AddAttributes (writer);
187 #endif
188                                 writer.RenderBeginTag (HtmlTextWriterTag.Li);
189                                 this.RenderBulletText (i, idx ++, writer);
190                                 writer.RenderEndTag ();
191                         }
192                 }
193
194                 protected internal override void Render (HtmlTextWriter writer)
195                 {
196                         base.Render (writer);
197                 }
198                 
199                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
200                 {
201                         RaisePostBackEvent (eventArgument);
202                 }
203                         
204                 protected virtual void RaisePostBackEvent (string eventArgument)
205                 {
206                         if (CausesValidation)
207                                 Page.Validate (ValidationGroup);
208                         
209                         this.OnClick (new BulletedListEventArgs (int.Parse (eventArgument, CultureInfo.InvariantCulture)));
210                 }
211                         
212             [BrowsableAttribute (false)]
213         [EditorBrowsableAttribute (EditorBrowsableState.Never)]
214                 public override bool AutoPostBack { 
215                         get { return base.AutoPostBack; }
216                         set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
217                 }
218                 
219                 [Bindable (false)]
220                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
221                 public override int SelectedIndex {
222                         get { return -1; }
223                         set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
224                 }
225                 
226             [EditorBrowsableAttribute (EditorBrowsableState.Never)]
227                 public override ListItem SelectedItem {
228                         get { return null; }
229                 }
230
231                 [EditorBrowsable (EditorBrowsableState.Never)]
232                 [Bindable (false)]
233                 public override string SelectedValue
234                 {
235                         get { return string.Empty; }
236                         set { throw new NotSupportedException (); }
237                 }
238                 
239                 [DefaultValueAttribute ("")]
240                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
241                 [UrlPropertyAttribute]
242                 public virtual string BulletImageUrl {
243                         get { return ViewState.GetString ("BulletImageUrl", ""); }
244                         set { ViewState ["BulletImageUrl"] = value; }
245                 }
246                 
247             [DefaultValueAttribute (BulletStyle.NotSet)]
248                 public virtual BulletStyle BulletStyle {
249                         get { return (BulletStyle) ViewState.GetInt ("BulletStyle", (int) BulletStyle.NotSet); }
250                         set {
251                                 if ((int) value < 0 || (int) value > 9)
252                                         throw new ArgumentOutOfRangeException ("value");
253
254                                 ViewState ["BulletStyle"] = value;
255                         }
256                 }
257                 
258                 public override ControlCollection Controls { get { return new EmptyControlCollection (this); } }
259                 
260             [DefaultValueAttribute (BulletedListDisplayMode.Text)]
261                 public virtual BulletedListDisplayMode DisplayMode {
262                         get { return (BulletedListDisplayMode) ViewState.GetInt ("DisplayMode", (int)BulletedListDisplayMode.Text); }
263                         set {
264                                 if ((int) value < 0 || (int) value > 2)
265                                         throw new ArgumentOutOfRangeException ("value");
266
267                                 ViewState ["DisplayMode"] = value;
268                         }
269                 }
270                 
271             [DefaultValueAttribute (1)]
272                 public virtual int FirstBulletNumber {
273                         get { return ViewState.GetInt ("FirstBulletNumber", 1); }
274                         set { ViewState ["FirstBulletNumber"] = value; }
275                 }
276                 
277
278                 protected override HtmlTextWriterTag TagKey {
279                         get {
280                                 switch (BulletStyle) {                  
281                                         case BulletStyle.Numbered:
282                                         case BulletStyle.LowerAlpha:
283                                         case BulletStyle.UpperAlpha:
284                                         case BulletStyle.LowerRoman:
285                                         case BulletStyle.UpperRoman:
286                                                 return HtmlTextWriterTag.Ol;
287                                         
288                                         case BulletStyle.NotSet:
289                                         case BulletStyle.Disc:
290                                         case BulletStyle.Circle:
291                                         case BulletStyle.Square:
292                                         case BulletStyle.CustomImage:
293                                         default:
294                                                 return HtmlTextWriterTag.Ul;
295                                 }
296                         }
297                 }
298                 
299                 [DefaultValueAttribute ("")]
300                 [TypeConverter (typeof (TargetConverter))]
301                 public virtual string Target {
302                         get { return ViewState.GetString ("Target", ""); }
303                         set { ViewState ["Target"] = value; }
304                 }
305
306                 [EditorBrowsable (EditorBrowsableState.Never)]
307                 public override string Text
308                 {
309                         get { return string.Empty; }
310                         set { throw new NotSupportedException (); }
311                 }
312                 
313                 
314                 static readonly object ClickEvent = new object ();
315                 public event BulletedListEventHandler Click
316                 {
317                         add {
318                                 Events.AddHandler (ClickEvent, value);
319                         }
320                         remove {
321                                 Events.RemoveHandler (ClickEvent, value);
322                         }
323                 }
324                 
325                 protected virtual void OnClick (BulletedListEventArgs e)
326                 {
327                         if (Events != null) {
328                                 BulletedListEventHandler eh = (BulletedListEventHandler) (Events [ClickEvent]);
329                                 if (eh != null)
330                                         eh (this, e);
331                         }
332                 }
333         }
334 }
335 #endif