This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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         public class BulletedList : ListControl, IPostBackEventHandler {
41                 
42                 [MonoTODO ("we are missing a new style enum, we should be using it")]
43                 protected override void AddAttributesToRender (HtmlTextWriter writer)
44                 {
45                         const string ListStyleType = "list-style-type";
46                         const string ListStyleImage = "list-style-image";
47                         
48                         bool isNumeric = false;
49                         switch (BulletStyle)
50                         {
51                                 case BulletStyle.NotSet:
52                                         break;
53                                 
54                                 case BulletStyle.Numbered:
55                                         writer.AddStyleAttribute (ListStyleType, "decimal");
56                                         isNumeric = true;
57                                         break;
58                                 
59                                 case BulletStyle.LowerAlpha:
60                                         writer.AddStyleAttribute (ListStyleType, "lower-alpha");
61                                         isNumeric = true;
62                                         break;
63                                 
64                                 case BulletStyle.UpperAlpha:
65                                         writer.AddStyleAttribute (ListStyleType, "upper-alpha");
66                                         isNumeric = true;
67                                         break;
68                                 
69                                 case BulletStyle.LowerRoman:
70                                         writer.AddStyleAttribute (ListStyleType, "lower-roman");
71                                         isNumeric = true;
72                                         break;
73                                 
74                                 case BulletStyle.UpperRoman:
75                                         writer.AddStyleAttribute (ListStyleType, "lower-roman");
76                                         isNumeric = true;
77                                         break;
78
79                                 case BulletStyle.Disc:
80                                         writer.AddStyleAttribute (ListStyleType, "disc");
81                                         break;
82                                 
83                                 case BulletStyle.Circle:
84                                         writer.AddStyleAttribute (ListStyleType, "circle");
85                                         break;
86                                 
87                                 case BulletStyle.Square:
88                                         writer.AddStyleAttribute (ListStyleType, "square");
89                                         break;
90                                                                 
91                                 case BulletStyle.CustomImage:
92                                         writer.AddStyleAttribute (ListStyleImage, "url(" + BulletImageUrl+ ")");
93                                         break;
94                         }
95
96                         if (isNumeric && FirstBulletNumber != 1)
97                                 writer.AddAttribute ("start", FirstBulletNumber.ToString ());
98                         
99                         base.AddAttributesToRender (writer);
100                 }
101                 
102                 bool cacheIsEnabled;
103                 [MonoTODO ("new bool prop on ListItem: Enabled")]
104                 protected virtual void RenderBulletText (ListItem item, int index, HtmlTextWriter writer)
105                 {
106                         switch (DisplayMode) {
107                                 case BulletedListDisplayMode.Text:
108                                         //if (!item.Enabled) {
109                                         //      writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
110                                         //      writer.RenderBeginTag (HtmlTextWriterTag.Span);
111                                         //}
112                                         writer.Write (item.Text);
113                                         //if (!item.Enabled)
114                                         //      writer.RenderEndTag ();
115                                         break;
116
117                                 case BulletedListDisplayMode.HyperLink:
118                                         //if (cacheIsEnabled && item.Enabled) {
119                                         //      writer.AddAttribute (HtmlTextWriterAttribute.Href, item.Value);
120                                         //      if (Target != "")
121                                         //              writer.AddAttribute(HtmlTextWriterAttribute.Target, this.Target);
122                                         //      
123                                         //}
124                                         //else
125                                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
126                                         
127                                         writer.RenderBeginTag (HtmlTextWriterTag.A);
128                                         writer.Write (item.Text);
129                                         writer.RenderEndTag ();
130                                         break;
131
132                                 case BulletedListDisplayMode.LinkButton:
133                                         //if (cacheIsEnabled && item.Enabled)
134                                                 writer.AddAttribute (HtmlTextWriterAttribute.Href, Page.GetPostBackClientHyperlink (this, (index.ToString (CultureInfo.InvariantCulture))));
135                                         //else
136                                         //      writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
137                                         writer.RenderBeginTag (HtmlTextWriterTag.A);
138                                         writer.Write (item.Text);
139                                         writer.RenderEndTag ();
140                                         break;
141                         }
142                 }
143                 
144                 protected override void RenderContents (HtmlTextWriter writer)
145                 {
146                         cacheIsEnabled = this.Enabled;
147                         int idx = 0;
148                         foreach (ListItem i in Items) {
149                                 writer.RenderBeginTag (HtmlTextWriterTag.Li);
150                                 this.RenderBulletText (i, idx ++, writer);
151                                 writer.RenderEndTag ();
152                         }
153                 }
154                 
155                 [MonoTODO ("ListControl has a CausesValidation prop in v1.2, we need to use it")]
156                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
157                 {
158                         //if (CausesValidation)
159                         //      Page.Validate ();
160                         
161                         this.OnClick (new BulletedListEventArgs (int.Parse (eventArgument, CultureInfo.InvariantCulture)));
162                 }
163                         
164                 public override bool AutoPostBack { 
165                         get { return base.AutoPostBack; }
166                         set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
167                 }
168                 
169                 public override int SelectedIndex {
170                         get { return base.SelectedIndex; }
171                         set { throw new NotSupportedException (String.Format ("This property is not supported in {0}", GetType ())); }
172                 }
173                 
174                 public override ListItem SelectedItem {
175                         get { return base.SelectedItem; }
176                 }
177                 
178                 public virtual string BulletImageUrl {
179                         get {
180                                 object ret = ViewState ["BulletImageUrl"];
181                                 if (ret != null)
182                                         return (string) ret; 
183                         
184                                 return "";
185                         }
186                         set {
187                                 ViewState ["BulletImageUrl"] = value;
188                         }
189                 }
190                 
191                 public virtual BulletStyle BulletStyle {
192                         get {
193                                 object ret = ViewState ["BulletStyle"];
194                                 if (ret != null)
195                                         return (BulletStyle) ret; 
196                         
197                                 return BulletStyle.NotSet;
198                         }
199                         set {
200                                 if ((int) value < 0 || (int) value > 9)
201                                         throw new ArgumentOutOfRangeException ("value");
202
203                                 ViewState ["BulletStyle"] = value;
204                         }
205                 }
206                 
207                 public override ControlCollection Controls { get { return new EmptyControlCollection (this); } }
208                 
209                 public virtual BulletedListDisplayMode DisplayMode {
210                         get {
211                                 object ret = ViewState ["DisplayMode"];
212                                 if (ret != null)
213                                         return (BulletedListDisplayMode) ret; 
214                         
215                                 return BulletedListDisplayMode.Text;
216                         }
217                         set {
218                                 if ((int) value < 0 || (int) value > 2)
219                                         throw new ArgumentOutOfRangeException ("value");
220
221                                 ViewState ["DisplayMode"] = value;
222                         }
223                 }
224                 
225                 public virtual int FirstBulletNumber {
226                         get {
227                                 object ret = ViewState ["FirstBulletNumber"];
228                                 if (ret != null)
229                                         return (int) ret; 
230                         
231                                 return 1;
232                         }
233                         set {
234                                 ViewState ["FirstBulletNumber"] = value;
235                         }
236                 }
237                 
238
239                 protected override HtmlTextWriterTag TagKey {
240                         get {
241                                 switch (BulletStyle) {                  
242                                         case BulletStyle.Numbered:
243                                         case BulletStyle.LowerAlpha:
244                                         case BulletStyle.UpperAlpha:
245                                         case BulletStyle.LowerRoman:
246                                         case BulletStyle.UpperRoman:
247                                                 return HtmlTextWriterTag.Ol;
248                                         
249                                         case BulletStyle.NotSet:
250                                         case BulletStyle.Disc:
251                                         case BulletStyle.Circle:
252                                         case BulletStyle.Square:
253                                         case BulletStyle.CustomImage:
254                                         default:
255                                                 return HtmlTextWriterTag.Ul;
256                                 }
257                         }
258                 }
259                 
260                 public virtual string Target {
261                         get {
262                                 object ret = ViewState ["Target"];
263                                 if (ret != null)
264                                         return (string) ret; 
265                         
266                                 return "";
267                         }
268                         set {
269                                 ViewState ["Target"] = value;
270                         }
271                 }
272                 
273                 static readonly object ClickEvent = new object ();
274                 public event EventHandler Click
275                 {
276                         add {
277                                 Events.AddHandler (ClickEvent, value);
278                         }
279                         remove {
280                                 Events.RemoveHandler (ClickEvent, value);
281                         }
282                 }
283                 
284                 protected virtual void OnClick (BulletedListEventArgs e)
285                 {
286                         if (Events != null) {
287                                 EventHandler eh = (EventHandler) (Events [ClickEvent]);
288                                 if (eh != null)
289                                         eh (this, e);
290                         }
291                 }
292         }
293 }
294 #endif