File: BulletedList.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (502 lines) | stat: -rw-r--r-- 18,058 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//------------------------------------------------------------------------------
// <copyright file="BulletedList.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.UI.WebControls {

    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Globalization;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing.Design;
    using System.Web.Util;



    /// <devdoc>
    ///     <para>Generates a bulleted list.</para>
    /// </devdoc>
    [DefaultProperty("BulletStyle")]
    [DefaultEvent("Click")]
    [Designer("System.Web.UI.Design.WebControls.BulletedListDesigner, " + AssemblyRef.SystemDesign)]
    [SupportsEventValidation]
    public class BulletedList : ListControl, IPostBackEventHandler {

        private static readonly object EventClick = new object();

        private bool _cachedIsEnabled;
        private int _firstItem;
        private int _itemCount;


        /// <devdoc></devdoc>
        public BulletedList() {
            _firstItem = 0;
            _itemCount = -1;
        }


        /// <devdoc>
        ///    <para>Gets the value of the base classes AutoPostBack propert.
        ///       AutoPostBack is not applicable to the bulleted list control</para>
        /// </devdoc>
        [
        Browsable(false),
        EditorBrowsable(EditorBrowsableState.Never)
        ]
        public override bool AutoPostBack {
            get {
                return base.AutoPostBack;
            }
            set {
                throw new NotSupportedException(SR.GetString(SR.Property_Set_Not_Supported, "AutoPostBack", this.GetType().ToString()));
            }
        }


        /// <devdoc>
        ///     <para>Gets or sets a value indicating the style of bullet to be
        ///        applied to the list.</para>
        /// </devdoc>
        [
        WebCategory("Appearance"),
        DefaultValue(BulletStyle.NotSet),
        WebSysDescription(SR.BulletedList_BulletStyle)
        ]
        public virtual BulletStyle BulletStyle {
            get {
                object o = ViewState["BulletStyle"];
                return((o == null) ? BulletStyle.NotSet : (BulletStyle)o);
            }
            set {
                if (value < BulletStyle.NotSet || value > BulletStyle.CustomImage) {
                    throw new ArgumentOutOfRangeException("value");
                }
                ViewState["BulletStyle"] = value;
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets the source of the image used for an
        ///       Image styled bulleted list.</para>
        /// </devdoc>
        [
        WebCategory("Appearance"),
        DefaultValue(""),
        Editor("System.Web.UI.Design.ImageUrlEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        UrlProperty(),
        WebSysDescription(SR.BulletedList_BulletImageUrl)
        ]
        public virtual string BulletImageUrl {
            get {
                object o = ViewState["BulletImageUrl"];
                return((o == null) ? string.Empty : (string)o);
            }
            set {
                ViewState["BulletImageUrl"] = value;
            }
        }


        /// <devdoc>
        ///    <para>Gets the EmptyControlCollection.</para>
        /// </devdoc>
        public override ControlCollection Controls {
            get {
                return new EmptyControlCollection(this);
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets the display mode of the bulleted list.</para>
        /// </devdoc>
        [
        WebCategory("Behavior"),
        DefaultValue(BulletedListDisplayMode.Text),
        WebSysDescription(SR.BulletedList_BulletedListDisplayMode) //
        ]
        public virtual BulletedListDisplayMode DisplayMode {
            get {
                object o = ViewState["DisplayMode"];
                return ((o == null) ? BulletedListDisplayMode.Text : (BulletedListDisplayMode)o);
            }
            set {
                if (value < BulletedListDisplayMode.Text || value > BulletedListDisplayMode.LinkButton) {
                    throw new ArgumentOutOfRangeException("value");
                }
                ViewState["DisplayMode"] = value;
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets the value at which an ordered list should
        ///       begin its numbering.</para>
        /// </devdoc>
        [
        WebCategory("Appearance"),
        DefaultValue(1),
        WebSysDescription(SR.BulletedList_FirstBulletNumber)
        ]
        public virtual int FirstBulletNumber {
            get {
                object o = ViewState["FirstBulletNumber"];
                return((o == null) ? 1 : (int)o);
            }
            set {
                ViewState["FirstBulletNumber"] = value;
            }
        }


        /// <summary>
        /// <para>Indicates whether the control will be rendered when the data source has no items.</para>
        /// </summary>
        [DefaultValue(false)]
        [Themeable(true)]
        [WebCategory("Behavior")]
        [WebSysDescription(SR.ListControl_RenderWhenDataEmpty)]
        public virtual bool RenderWhenDataEmpty {
            get {
                object o = ViewState["RenderWhenDataEmpty"];
                return ((o == null) ? false : (bool)o);
            }
            set {
                ViewState["RenderWhenDataEmpty"] = value;
            }
        }


        /// <devdoc>
        ///    <para>Gets the value of selected index.  Not applicable to the
        ///       bulleted list control.</para>
        /// </devdoc>
        [
        Bindable(false),
        EditorBrowsable(EditorBrowsableState.Never)
        ]
        public override int SelectedIndex {
            get {
                return base.SelectedIndex;
            }
            set {
                throw new NotSupportedException(SR.GetString(SR.BulletedList_SelectionNotSupported));
            }
        }


        /// <devdoc>
        ///    <para>Gets the selected item.  Not applicable to the
        ///       bulleted list control.</para>
        /// </devdoc>
        [
        EditorBrowsable(EditorBrowsableState.Never)
        ]
        public override ListItem SelectedItem {
            get {
                return base.SelectedItem;
            }
        }

        [
        Bindable(false),
        EditorBrowsable(EditorBrowsableState.Never)
        ]
        public override string SelectedValue {
            get {
                return base.SelectedValue;
            }
            set {
                throw new NotSupportedException(SR.GetString(SR.BulletedList_SelectionNotSupported));
            }
        }


        /// <devdoc>
        ///    <para>Gets the HtmlTextWriterTag value that corresponds
        ///       to the particular bulleted list.</para>
        /// </devdoc>
        protected override HtmlTextWriterTag TagKey {
            get {
                return TagKeyInternal;
            }
        }

        internal HtmlTextWriterTag TagKeyInternal {
            get {
                switch (BulletStyle) {
                    // Ordered Lists
                    case BulletStyle.LowerAlpha:
                    case BulletStyle.UpperAlpha:
                    case BulletStyle.LowerRoman:
                    case BulletStyle.UpperRoman:
                    case BulletStyle.Numbered:
                        return HtmlTextWriterTag.Ol;
                    // Unordered Lists
                    case BulletStyle.Square:
                    case BulletStyle.Circle:
                    case BulletStyle.Disc:
                        return HtmlTextWriterTag.Ul;
                    // Image Lists
                    case BulletStyle.CustomImage:
                        return HtmlTextWriterTag.Ul;
                    // Not Set
                    case BulletStyle.NotSet:
                        // NotSet is specified as an unordered list.
                        return HtmlTextWriterTag.Ul;
                    default:
                        Debug.Assert(false, "Invalid BulletStyle");
                        return HtmlTextWriterTag.Ol;
                }
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets the Target window when the
        ///       list is displayed as Hyperlinks.</para>
        /// </devdoc>
        [
        WebCategory("Behavior"),
        DefaultValue(""),
        WebSysDescription(SR.BulletedList_Target),
        TypeConverter(typeof(TargetConverter))
        ]
        public virtual string Target {
            get {
                object o = ViewState["Target"];
                return ((o == null) ? string.Empty : (string)o);
            }
            set {
                ViewState["Target"] = value;
            }
        }

        [
        EditorBrowsable(EditorBrowsableState.Never)
        ]
        public override string Text {
            get {
                return base.Text;
            }
            set {
                throw new NotSupportedException(SR.GetString(SR.BulletedList_TextNotSupported));
            }
        }


        /// <devdoc>
        ///    <para>Occurs when the a link button is clicked.</para>
        /// </devdoc>
        [
        WebCategory("Action"),
        WebSysDescription(SR.BulletedList_OnClick)
        ]
        public event BulletedListEventHandler Click {
            add {
                Events.AddHandler(EventClick, value);
            }
            remove {
                Events.RemoveHandler(EventClick, value);
            }
        }


        /// <devdoc>
        ///    <para>Adds HTML attributes that need to be rendered.</para>
        /// </devdoc>
        protected override void AddAttributesToRender(HtmlTextWriter writer) {
            bool addBulletNumber = false;

            switch (BulletStyle) {
                case BulletStyle.NotSet:
                    break;
                case BulletStyle.Numbered:
                    writer.AddStyleAttribute(HtmlTextWriterStyle.ListStyleType, "decimal");
                    addBulletNumber = true;
                    break;
                case BulletStyle.LowerAlpha:
                    writer.AddStyleAttribute(HtmlTextWriterStyle.ListStyleType, "lower-alpha");
                    addBulletNumber = true;
                    break;
                case BulletStyle.UpperAlpha:
                    writer.AddStyleAttribute(HtmlTextWriterStyle.ListStyleType, "upper-alpha");
                    addBulletNumber = true;
                    break;
                case BulletStyle.LowerRoman:
                    writer.AddStyleAttribute(HtmlTextWriterStyle.ListStyleType, "lower-roman");
                    addBulletNumber = true;
                    break;
                case BulletStyle.UpperRoman:
                    writer.AddStyleAttribute(HtmlTextWriterStyle.ListStyleType, "upper-roman");
                    addBulletNumber = true;
                    break;
                case BulletStyle.Disc:
                    writer.AddStyleAttribute(HtmlTextWriterStyle.ListStyleType, "disc");
                    break;
                case BulletStyle.Circle:
                    writer.AddStyleAttribute(HtmlTextWriterStyle.ListStyleType, "circle");
                    break;
                case BulletStyle.Square:
                    writer.AddStyleAttribute(HtmlTextWriterStyle.ListStyleType, "square");
                    break;
                case BulletStyle.CustomImage:
                    String url = ResolveClientUrl(BulletImageUrl);
                    writer.AddStyleAttribute(HtmlTextWriterStyle.ListStyleImage, "url(" + HttpUtility.UrlPathEncode(url) + ")");
                    break;
                default:
                    Debug.Assert(false, "Invalid BulletStyle");
                    break;
            }
            int firstBulletNumber = FirstBulletNumber;
            if ((addBulletNumber == true) && (firstBulletNumber != 1)) {
                writer.AddAttribute("start", firstBulletNumber.ToString(CultureInfo.InvariantCulture));
            }
            base.AddAttributesToRender(writer);
        }

        private string GetPostBackEventReference(string eventArgument) {
            if (CausesValidation && Page.GetValidators(ValidationGroup).Count > 0) {
                return ClientScriptManager.JscriptPrefix + Util.GetClientValidatedPostback(this, ValidationGroup, eventArgument);
            } else {
                return Page.ClientScript.GetPostBackClientHyperlink(this, eventArgument, true);
            }
        }

        /// <devdoc>
        /// <para>Raises the Click event.</para>
        /// </devdoc>
        protected virtual void OnClick(BulletedListEventArgs e) {
            BulletedListEventHandler onClickHandler = (BulletedListEventHandler)Events[EventClick];
            if (onClickHandler != null)
                onClickHandler(this, e);
        }


        /// <devdoc>
        ///     <para>Writes the text of each bullet according to the list's display mode.</para>
        /// </devdoc>
        protected virtual void RenderBulletText(ListItem item, int index, HtmlTextWriter writer) {
            switch (DisplayMode) {
                case BulletedListDisplayMode.Text:
                    if (!item.Enabled) {
                        RenderDisabledAttributeHelper(writer, false);
                        writer.RenderBeginTag(HtmlTextWriterTag.Span);
                    }
                    HttpUtility.HtmlEncode(item.Text, writer);
                    if (!item.Enabled) {
                        writer.RenderEndTag();
                    }
                    break;

                case BulletedListDisplayMode.HyperLink:
                    if (_cachedIsEnabled && item.Enabled) {
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, ResolveClientUrl(item.Value));
                        string target = Target;
                        if (!String.IsNullOrEmpty(target)) {
                            writer.AddAttribute(HtmlTextWriterAttribute.Target, Target);
                        }
                    }
                    else {
                        RenderDisabledAttributeHelper(writer, item.Enabled);
                    }

                    RenderAccessKey(writer, AccessKey);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    HttpUtility.HtmlEncode(item.Text, writer);
                    writer.RenderEndTag();
                    break;

                case BulletedListDisplayMode.LinkButton:
                    if (_cachedIsEnabled && item.Enabled) {
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, GetPostBackEventReference(index.ToString(CultureInfo.InvariantCulture)));
                    }
                    else {
                        RenderDisabledAttributeHelper(writer, item.Enabled);
                    }

                    RenderAccessKey(writer, AccessKey);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    HttpUtility.HtmlEncode(item.Text, writer);
                    writer.RenderEndTag();
                    break;

                default:
                    Debug.Assert(false, "Invalid BulletedListDisplayMode");
                    break;
            }

        }

        private void RenderDisabledAttributeHelper(HtmlTextWriter writer, bool isItemEnabled) {
            if (SupportsDisabledAttribute) {
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            }
            else if (!isItemEnabled && !String.IsNullOrEmpty(DisabledCssClass)) {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, DisabledCssClass);
            }
        }

        internal void RenderAccessKey(HtmlTextWriter writer, string AccessKey) {
            string s = AccessKey;
            if (s.Length > 0) {
                writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, s);
            }
        }

        protected internal override void Render(HtmlTextWriter writer) {
            // Don't render anything if the control is empty (unless the developer opts in)
            if (Items.Count == 0 && !RenderWhenDataEmpty) {
                return;
            }

            base.Render(writer);
        }


        /// <devdoc>
        ///    <para>Renders the ListItems as bullets in the bulleted list.</para>
        /// </devdoc>
        protected internal override void RenderContents(HtmlTextWriter writer) {
            _cachedIsEnabled = IsEnabled;

            if (_itemCount == -1) {
                for (int i = 0; i < Items.Count; i++) {
                    Items[i].RenderAttributes(writer);
                    writer.RenderBeginTag(HtmlTextWriterTag.Li);
                    RenderBulletText(Items[i], i, writer);
                    writer.RenderEndTag();
                }
            }
            else {
                for (int i = _firstItem; i < _firstItem + _itemCount; i++) {
                    Items[i].RenderAttributes(writer);
                    writer.RenderBeginTag(HtmlTextWriterTag.Li);
                    RenderBulletText(Items[i], i, writer);
                    writer.RenderEndTag();
                }
            }
        }

        protected virtual void RaisePostBackEvent(string eventArgument) {
            ValidateEvent(this.UniqueID, eventArgument);

            if (CausesValidation) {
                Page.Validate(ValidationGroup);
            }
            OnClick(new BulletedListEventArgs(Int32.Parse(eventArgument, CultureInfo.InvariantCulture)));
        }

        void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) {
            RaisePostBackEvent(eventArgument);
        }
    }
}