svn path=/branches/mono-1-1-9/mcs/; revision=51206
[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
39 namespace System.Web.UI.WebControls {
40
41         [DesignerAttribute ("System.Web.UI.Design.WebControls.BulletedListDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
42         [DefaultEventAttribute ("Click")]
43         [DefaultPropertyAttribute ("BulletStyle")]
44         public class BulletedList : ListControl, IPostBackEventHandler {
45                 
46                 [MonoTODO ("we are missing a new style enum, we should be using it")]
47                 protected override void AddAttributesToRender (HtmlTextWriter writer)
48                 {
49                         const string ListStyleType = "list-style-type";
50                         const string ListStyleImage = "list-style-image";
51                         
52                         bool isNumeric = false;
53                         switch (BulletStyle)
54                         {
55                                 case BulletStyle.NotSet:
56                                         break;
57                                 
58                                 case BulletStyle.Numbered:
59                                         writer.AddStyleAttribute (ListStyleType, "decimal");
60                                         isNumeric = true;
61                                         break;
62                                 
63                                 case BulletStyle.LowerAlpha:
64                                         writer.AddStyleAttribute (ListStyleType, "lower-alpha");
65                                         isNumeric = true;
66                                         break;
67                                 
68                                 case BulletStyle.UpperAlpha:
69                                         writer.AddStyleAttribute (ListStyleType, "upper-alpha");
70                                         isNumeric = true;
71                                         break;
72                                 
73                                 case BulletStyle.LowerRoman:
74                                         writer.AddStyleAttribute (ListStyleType, "lower-roman");
75                                         isNumeric = true;
76                                         break;
77                                 
78                                 case BulletStyle.UpperRoman:
79                                         writer.AddStyleAttribute (ListStyleType, "lower-roman");
80                                         isNumeric = true;
81                                         break;
82
83                                 case BulletStyle.Disc:
84                                         writer.AddStyleAttribute (ListStyleType, "disc");
85                                         break;
86                                 
87                                 case BulletStyle.Circle:
88                                         writer.AddStyleAttribute (ListStyleType, "circle");
89                                         break;
90                                 
91                                 case BulletStyle.Square:
92                                         writer.AddStyleAttribute (ListStyleType, "square");
93                                         break;
94                                                                 
95                                 case BulletStyle.CustomImage:
96                                         writer.AddStyleAttribute (ListStyleImage, "url(" + BulletImageUrl+ ")");
97                                         break;
98                         }
99
100                         if (isNumeric && FirstBulletNumber != 1)
101                                 writer.AddAttribute ("start", FirstBulletNumber.ToString ());
102                         
103                         base.AddAttributesToRender (writer);
104                 }
105                 
106                 bool cacheIsEnabled;
107                 [MonoTODO ("new bool prop on ListItem: Enabled")]
108                 protected virtual void RenderBulletText (ListItem item, int index, HtmlTextWriter writer)
109                 {
110                         switch (DisplayMode) {
111                                 case BulletedListDisplayMode.Text:
112                                         //if (!item.Enabled) {
113                                         //      writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
114                                         //      writer.RenderBeginTag (HtmlTextWriterTag.Span);
115                                         //}
116                                         writer.Write (item.Text);
117                                         //if (!item.Enabled)
118                                         //      writer.RenderEndTag ();
119                                         break;
120
121                                 case BulletedListDisplayMode.HyperLink:
122                                         //if (cacheIsEnabled && item.Enabled) {
123                                         //      writer.AddAttribute (HtmlTextWriterAttribute.Href, item.Value);
124                                         //      if (Target != "")
125                                         //              writer.AddAttribute(HtmlTextWriterAttribute.Target, this.Target);
126                                         //      
127                                         //}
128                                         //else
129                                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
130                                         
131                                         writer.RenderBeginTag (HtmlTextWriterTag.A);
132                                         writer.Write (item.Text);
133                                         writer.RenderEndTag ();
134                                         break;
135
136                                 case BulletedListDisplayMode.LinkButton:
137                                         //if (cacheIsEnabled && item.Enabled)
138                                                 writer.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, (index.ToString (CultureInfo.InvariantCulture))));
139                                         //else
140                                         //      writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
141                                         writer.RenderBeginTag (HtmlTextWriterTag.A);
142                                         writer.Write (item.Text);
143                                         writer.RenderEndTag ();
144                                         break;
145                         }
146                 }
147                 
148                 protected internal override void RenderContents (HtmlTextWriter writer)
149                 {
150                         cacheIsEnabled = this.Enabled;
151                         int idx = 0;
152                         foreach (ListItem i in Items) {
153                                 writer.RenderBeginTag (HtmlTextWriterTag.Li);
154                                 this.RenderBulletText (i, idx ++, writer);
155                                 writer.RenderEndTag ();
156                         }
157                 }
158
159                 [MonoTODO]
160                 protected internal override void Render (HtmlTextWriter w)
161                 {
162                         throw new NotImplementedException ();
163                 }
164                 
165                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
166                 {
167                         RaisePostBackEvent (eventArgument);
168                 }
169                         
170                 [MonoTODO ("ListControl has a CausesValidation prop in v1.2, we need to use it")]
171                 protected virtual void RaisePostBackEvent (string eventArgument)
172                 {
173                         //if (CausesValidation)
174                         //      Page.Validate ();
175                         
176                         this.OnClick (new BulletedListEventArgs (int.Parse (eventArgument, CultureInfo.InvariantCulture)));
177                 }
178                         
179             [BrowsableAttribute (false)]
180         [EditorBrowsableAttribute (EditorBrowsableState.Never)]
181                 public override bool AutoPostBack { 
182                         get { return base.AutoPostBack; }
183                         set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
184                 }
185                 
186                 [Bindable (false)]
187                 [EditorBrowsableAttribute (EditorBrowsableState.Never)]
188                 public override int SelectedIndex {
189                         get { return base.SelectedIndex; }
190                         set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
191                 }
192                 
193             [EditorBrowsableAttribute (EditorBrowsableState.Never)]
194                 public override ListItem SelectedItem {
195                         get { return base.SelectedItem; }
196                 }
197
198                 [EditorBrowsable (EditorBrowsableState.Never)]
199                 [Bindable (false)]
200                 [MonoTODO]
201                 public override string SelectedValue 
202                 {
203                         get {
204                                 throw new NotImplementedException ();
205                         }
206                         set {
207                                 throw new NotImplementedException ();
208                         }
209                 }
210                 
211                 [DefaultValueAttribute ("")]
212                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
213                 [UrlPropertyAttribute]
214                 public virtual string BulletImageUrl {
215                         get {
216                                 object ret = ViewState ["BulletImageUrl"];
217                                 if (ret != null)
218                                         return (string) ret; 
219                         
220                                 return "";
221                         }
222                         set {
223                                 ViewState ["BulletImageUrl"] = value;
224                         }
225                 }
226                 
227             [DefaultValueAttribute (BulletStyle.NotSet)]
228                 public virtual BulletStyle BulletStyle {
229                         get {
230                                 object ret = ViewState ["BulletStyle"];
231                                 if (ret != null)
232                                         return (BulletStyle) ret; 
233                         
234                                 return BulletStyle.NotSet;
235                         }
236                         set {
237                                 if ((int) value < 0 || (int) value > 9)
238                                         throw new ArgumentOutOfRangeException ("value");
239
240                                 ViewState ["BulletStyle"] = value;
241                         }
242                 }
243                 
244                 public override ControlCollection Controls { get { return new EmptyControlCollection (this); } }
245                 
246             [DefaultValueAttribute (BulletedListDisplayMode.Text)]
247                 public virtual BulletedListDisplayMode DisplayMode {
248                         get {
249                                 object ret = ViewState ["DisplayMode"];
250                                 if (ret != null)
251                                         return (BulletedListDisplayMode) ret; 
252                         
253                                 return BulletedListDisplayMode.Text;
254                         }
255                         set {
256                                 if ((int) value < 0 || (int) value > 2)
257                                         throw new ArgumentOutOfRangeException ("value");
258
259                                 ViewState ["DisplayMode"] = value;
260                         }
261                 }
262                 
263             [DefaultValueAttribute (1)]
264                 public virtual int FirstBulletNumber {
265                         get {
266                                 object ret = ViewState ["FirstBulletNumber"];
267                                 if (ret != null)
268                                         return (int) ret; 
269                         
270                                 return 1;
271                         }
272                         set {
273                                 ViewState ["FirstBulletNumber"] = value;
274                         }
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 {
303                                 object ret = ViewState ["Target"];
304                                 if (ret != null)
305                                         return (string) ret; 
306                         
307                                 return "";
308                         }
309                         set {
310                                 ViewState ["Target"] = value;
311                         }
312                 }
313
314                 [EditorBrowsable (EditorBrowsableState.Never)]
315                 [MonoTODO]
316                 public override string Text
317                 {
318                         get {
319                                 throw new NotImplementedException ();
320                         }
321                         set {
322                                 throw new NotImplementedException ();
323                         }
324                 }
325                 
326                 
327                 static readonly object ClickEvent = new object ();
328                 public event BulletedListEventHandler Click
329                 {
330                         add {
331                                 Events.AddHandler (ClickEvent, value);
332                         }
333                         remove {
334                                 Events.RemoveHandler (ClickEvent, value);
335                         }
336                 }
337                 
338                 protected virtual void OnClick (BulletedListEventArgs e)
339                 {
340                         if (Events != null) {
341                                 BulletedListEventHandler eh = (BulletedListEventHandler) (Events [ClickEvent]);
342                                 if (eh != null)
343                                         eh (this, e);
344                         }
345                 }
346         }
347 }
348 #endif