File: EditorPartChrome.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 (183 lines) | stat: -rw-r--r-- 7,835 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
//------------------------------------------------------------------------------
// <copyright file="EditorPartChrome.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.UI.WebControls.WebParts {

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Globalization;
    using System.Web.Handlers;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class EditorPartChrome {

        private EditorZoneBase _zone;

        // PERF: Cache these, since they are needed for every EditorPart in the zone
        private Style _chromeStyleNoBorder;
        private Style _titleTextStyle;

        public EditorPartChrome(EditorZoneBase zone) {
            if (zone == null) {
                throw new ArgumentNullException("zone");
            }
            _zone = zone;
        }

        protected EditorZoneBase Zone {
            get {
                return _zone;
            }
        }

        protected virtual Style CreateEditorPartChromeStyle(EditorPart editorPart, PartChromeType chromeType) {
            if (editorPart == null) {
                throw new ArgumentNullException("editorPart");
            }
            if ((chromeType < PartChromeType.Default) || (chromeType > PartChromeType.BorderOnly)) {
                throw new ArgumentOutOfRangeException("chromeType");
            }

            // PERF: Cache these, since they are needed for every EditorPart in the zone.
            if (chromeType == PartChromeType.BorderOnly || chromeType == PartChromeType.TitleAndBorder) {
                // We don't want to set any border styles for ChromeType of TitleAndBorder or BorderOnly,
                // since the FrameSet has a default border, and it will use XP themes as long as no border styles
                // are set.
                // PERF: Just return the Zone.PartChromeStyle directly without making a copy
                return Zone.PartChromeStyle;
            }
            else {
                if (_chromeStyleNoBorder == null) {
                    Style style = new Style();

                    // create copy of PartChromeStyle so we can modify it
                    style.CopyFrom(Zone.PartChromeStyle);

                    if (style.BorderStyle != BorderStyle.None) {
                        style.BorderStyle = BorderStyle.None;
                    }
                    if (style.BorderWidth != Unit.Empty) {
                        style.BorderWidth = Unit.Empty;
                    }
                    if (style.BorderColor != Color.Empty) {
                        style.BorderColor = Color.Empty;
                    }

                    _chromeStyleNoBorder = style;
                }
                return _chromeStyleNoBorder;
            }
        }

        public virtual void PerformPreRender() {
        }

        public virtual void RenderEditorPart(HtmlTextWriter writer, EditorPart editorPart) {
            if (editorPart == null) {
                throw new ArgumentNullException("editorPart");
            }

            PartChromeType chromeType = Zone.GetEffectiveChromeType(editorPart);
            Style partChromeStyle = CreateEditorPartChromeStyle(editorPart, chromeType);

            // Apply ChromeStyle to the Fieldset
            if (!partChromeStyle.IsEmpty) {
                partChromeStyle.AddAttributesToRender(writer, Zone);
            }

            writer.RenderBeginTag(HtmlTextWriterTag.Fieldset);

            // Use ChromeType to determine whether to render the legend
            if (chromeType == PartChromeType.TitleAndBorder || chromeType == PartChromeType.TitleOnly) {
                RenderTitle(writer, editorPart);
            }

            if (editorPart.ChromeState != PartChromeState.Minimized) {
                // Apply PartStyle to a <div> around the part rendering
                Style partStyle = Zone.PartStyle;
                if (!partStyle.IsEmpty) {
                    partStyle.AddAttributesToRender(writer, Zone);
                }

                // We want to have 5 pixels of spacing aroung the EditorPart contents.  There are
                // 3 ways to accomplish this:
                // 1. <fieldset style="padding:5px"> - This is bad because it adds 5px of space above
                //    the legend.  It also makes the fieldset too wide.
                // 2. <fieldset><div style="padding:5px"> - This is bad because the PartStyle-BackColor
                //    will now span the whole width of the legend.  For consistency with WebPartChrome,
                //    we want the PartChromeStyle-BackColor to show in the 5px of space around the contents.
                // 3. <fieldset><div style="margin:5px"> - This is the best option.
                //
                // For now, I don't think we should render a margin here.  People writing custom
                // EditorParts can add a margin to their contents if they want it.  This is not the
                // same as the WebPartChrome case, since for WebParts we allow people to use ServerControls,
                // that will likely not have a margin.  (VSWhidbey 324397)
                // writer.AddStyleAttribute(HtmlTextWriterStyle.Margin, "5px");

                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                RenderPartContents(writer, editorPart);
                writer.RenderEndTag();  // Div
            }

            writer.RenderEndTag();  // Fieldset
        }

        protected virtual void RenderPartContents(HtmlTextWriter writer, EditorPart editorPart) {
            // The AccessKey is rendered by the chrome on the <legend> tag, so we don't want
            // the EditorPart to render it on its own tags.
            string accessKey = editorPart.AccessKey;
            if (!String.IsNullOrEmpty(accessKey)) {
                editorPart.AccessKey = String.Empty;
            }
            editorPart.RenderControl(writer);
            if (!String.IsNullOrEmpty(accessKey)) {
                editorPart.AccessKey = accessKey;
            }
        }

        private void RenderTitle(HtmlTextWriter writer, EditorPart editorPart) {
            string displayTitle = editorPart.DisplayTitle;

            if (String.IsNullOrEmpty(displayTitle)) {
                return;
            }

            // Apply TitleStyle to the Legend
            TableItemStyle titleTableItemStyle = Zone.PartTitleStyle;

            // PERF: Cache this, since it is needed for every EditorPart in the zone
            if (_titleTextStyle == null) {
                // Need to copy the TableItemStyle to a plain Style, since we are going to apply it to
                // the <legend> tag, which is not a table item.  We ignore the horizontal align,
                // vertical align, and nowrap properties.
                Style style = new Style();
                style.CopyFrom(titleTableItemStyle);
                _titleTextStyle = style;
            }

            if (!_titleTextStyle.IsEmpty) {
                _titleTextStyle.AddAttributesToRender(writer, Zone);
            }

            string description = editorPart.Description;
            if (!String.IsNullOrEmpty(description)) {
                writer.AddAttribute(HtmlTextWriterAttribute.Title, description);
            }

            string accessKey = editorPart.AccessKey;
            if (!String.IsNullOrEmpty(accessKey)) {
                writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, accessKey);
            }

            writer.RenderBeginTag(HtmlTextWriterTag.Legend);
            writer.Write(displayTitle);
            writer.RenderEndTag();  // Legend
        }
    }
}