1 //------------------------------------------------------------------------------
2 // <copyright file="CatalogPartChrome.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 namespace System.Web.UI.WebControls.WebParts {
8 
9     using System;
10     using System.Collections;
11     using System.ComponentModel;
12     using System.Drawing;
13     using System.Globalization;
14     using System.Web;
15     using System.Web.UI;
16     using System.Web.UI.WebControls;
17 
18     public class CatalogPartChrome {
19 
20         private CatalogZoneBase _zone;
21 
22         // PERF: Cache these, since they are needed for every CatalogPart in the zone
23         private Page _page;
24         private Style _chromeStyleWithBorder;
25         private Style _chromeStyleNoBorder;
26 
CatalogPartChrome(CatalogZoneBase zone)27         public CatalogPartChrome(CatalogZoneBase zone) {
28             if (zone == null) {
29                 throw new ArgumentNullException("zone");
30             }
31             _zone = zone;
32             _page = zone.Page;
33         }
34 
35         protected CatalogZoneBase Zone {
36             get {
37                 return _zone;
38             }
39         }
40 
CreateCatalogPartChromeStyle(CatalogPart catalogPart, PartChromeType chromeType)41         protected virtual Style CreateCatalogPartChromeStyle(CatalogPart catalogPart, PartChromeType chromeType) {
42             if (catalogPart == null) {
43                 throw new ArgumentNullException("catalogPart");
44             }
45             if ((chromeType < PartChromeType.Default) || (chromeType > PartChromeType.BorderOnly)) {
46                 throw new ArgumentOutOfRangeException("chromeType");
47             }
48 
49             if (chromeType == PartChromeType.BorderOnly || chromeType == PartChromeType.TitleAndBorder) {
50                 if (_chromeStyleWithBorder == null) {
51                     Style style = new Style();
52                     style.CopyFrom(Zone.PartChromeStyle);
53 
54                     if (style.BorderStyle == BorderStyle.NotSet) {
55                         style.BorderStyle = BorderStyle.Solid;
56                     }
57                     if (style.BorderWidth == Unit.Empty) {
58                         style.BorderWidth = Unit.Pixel(1);
59                     }
60                     if (style.BorderColor == Color.Empty) {
61                         style.BorderColor = Color.Black;
62                     }
63 
64                     _chromeStyleWithBorder = style;
65                 }
66                 return _chromeStyleWithBorder;
67             }
68             else {
69                 if (_chromeStyleNoBorder == null) {
70                     Style style = new Style();
71                     style.CopyFrom(Zone.PartChromeStyle);
72 
73                     if (style.BorderStyle != BorderStyle.NotSet) {
74                         style.BorderStyle = BorderStyle.NotSet;
75                     }
76                     if (style.BorderWidth != Unit.Empty) {
77                         style.BorderWidth = Unit.Empty;
78                     }
79                     if (style.BorderColor != Color.Empty) {
80                         style.BorderColor = Color.Empty;
81                     }
82 
83                     _chromeStyleNoBorder = style;
84                 }
85                 return _chromeStyleNoBorder;
86             }
87         }
88 
PerformPreRender()89         public virtual void PerformPreRender() {
90         }
91 
RenderCatalogPart(HtmlTextWriter writer, CatalogPart catalogPart)92         public virtual void RenderCatalogPart(HtmlTextWriter writer, CatalogPart catalogPart) {
93             if (catalogPart == null) {
94                 throw new ArgumentNullException("catalogPart");
95             }
96 
97             PartChromeType chromeType = Zone.GetEffectiveChromeType(catalogPart);
98             Style partChromeStyle = CreateCatalogPartChromeStyle(catalogPart, chromeType);
99 
100             //
101             if (!partChromeStyle.IsEmpty) {
102                 partChromeStyle.AddAttributesToRender(writer, Zone);
103             }
104             writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
105             // Use CellPadding=2 to match WebPartChrome (VSWhidbey 324397)
106             writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "2");
107             writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
108 
109             writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
110 
111             writer.RenderBeginTag(HtmlTextWriterTag.Table);
112 
113             if (chromeType == PartChromeType.TitleOnly || chromeType == PartChromeType.TitleAndBorder) {
114                 writer.RenderBeginTag(HtmlTextWriterTag.Tr);
115 
116                 // Can apply PartTitleStyle directly, since the title bar doesn't contain a nested table
117                 Style partTitleStyle = Zone.PartTitleStyle;
118                 if (!partTitleStyle.IsEmpty) {
119                     partTitleStyle.AddAttributesToRender(writer, Zone);
120                 }
121                 writer.RenderBeginTag(HtmlTextWriterTag.Td);
122 
123                 RenderTitle(writer, catalogPart);
124 
125                 writer.RenderEndTag();  // Td
126                 writer.RenderEndTag();  // Tr
127             }
128 
129             if (catalogPart.ChromeState != PartChromeState.Minimized) {
130                 writer.RenderBeginTag(HtmlTextWriterTag.Tr);
131                 Style partStyle = Zone.PartStyle;
132                 if (!partStyle.IsEmpty) {
133                     partStyle.AddAttributesToRender(writer, Zone);
134                 }
135 
136                 // For now, I don't think we should render extra padding here.  People writing custom
137                 // CatalogParts can add a margin to their contents if they want it.  This is not the
138                 // same as the WebPartChrome case, since for WebParts we allow people to use ServerControls,
139                 // that will likely not have a margin.  (VSWhidbey 324397)
140                 // writer.AddStyleAttribute(HtmlTextWriterStyle.Padding, "5px");
141 
142                 writer.RenderBeginTag(HtmlTextWriterTag.Td);
143 
144                 //
145                 RenderPartContents(writer, catalogPart);
146 
147                 RenderItems(writer, catalogPart);
148 
149                 writer.RenderEndTag();  // Td
150                 writer.RenderEndTag();  // Tr
151             }
152 
153             writer.RenderEndTag();  // Table
154         }
155 
RenderItem(HtmlTextWriter writer, WebPartDescription webPartDescription)156         private void RenderItem(HtmlTextWriter writer, WebPartDescription webPartDescription) {
157             string description = webPartDescription.Description;
158             if (String.IsNullOrEmpty(description)) {
159                 description = webPartDescription.Title;
160             }
161 
162             RenderItemCheckBox(writer, webPartDescription.ID);
163 
164             writer.Write("&nbsp;");
165 
166             if (Zone.ShowCatalogIcons) {
167                 string icon = webPartDescription.CatalogIconImageUrl;
168                 if (!String.IsNullOrEmpty(icon)) {
169                     RenderItemIcon(writer, icon, description);
170                     writer.Write("&nbsp;");
171                 }
172             }
173 
174             RenderItemText(writer, webPartDescription.ID, webPartDescription.Title, description);
175 
176             writer.WriteBreak();
177         }
178 
RenderItemCheckBox(HtmlTextWriter writer, string value)179         private void RenderItemCheckBox(HtmlTextWriter writer, string value) {
180             Zone.EditUIStyle.AddAttributesToRender(writer, Zone);
181             writer.AddAttribute(HtmlTextWriterAttribute.Type, "checkbox");
182             writer.AddAttribute(HtmlTextWriterAttribute.Id, Zone.GetCheckBoxID(value));
183             writer.AddAttribute(HtmlTextWriterAttribute.Name, Zone.CheckBoxName);
184             writer.AddAttribute(HtmlTextWriterAttribute.Value, value);
185             writer.RenderBeginTag(HtmlTextWriterTag.Input);
186             writer.RenderEndTag();  // Input
187 
188             if (_page != null) {
189                 _page.ClientScript.RegisterForEventValidation(Zone.CheckBoxName);
190             }
191         }
192 
RenderItemIcon(HtmlTextWriter writer, string iconUrl, string description)193         private void RenderItemIcon(HtmlTextWriter writer, string iconUrl, string description) {
194             System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
195             img.AlternateText = description;
196 
197             //
198             img.ImageUrl = iconUrl;
199             img.BorderStyle = BorderStyle.None;
200             img.Page = _page;
201             img.RenderControl(writer);
202         }
203 
RenderItemText(HtmlTextWriter writer, string value, string text, string description)204         private void RenderItemText(HtmlTextWriter writer, string value, string text, string description) {
205             Zone.LabelStyle.AddAttributesToRender(writer, Zone);
206             writer.AddAttribute(HtmlTextWriterAttribute.For, Zone.GetCheckBoxID(value));
207             writer.AddAttribute(HtmlTextWriterAttribute.Title, description, true /* fEncode */);
208             writer.RenderBeginTag(HtmlTextWriterTag.Label);
209             writer.WriteEncodedText(text);
210             writer.RenderEndTag();
211         }
212 
RenderItems(HtmlTextWriter writer, CatalogPart catalogPart)213         private void RenderItems(HtmlTextWriter writer, CatalogPart catalogPart) {
214             WebPartDescriptionCollection availableWebParts = catalogPart.GetAvailableWebPartDescriptions();
215 
216             if (availableWebParts != null) {
217                 foreach (WebPartDescription webPartDescription in availableWebParts) {
218                     RenderItem(writer, webPartDescription);
219                 }
220             }
221         }
222 
RenderPartContents(HtmlTextWriter writer, CatalogPart catalogPart)223         protected virtual void RenderPartContents(HtmlTextWriter writer, CatalogPart catalogPart) {
224             if (catalogPart == null) {
225                 throw new ArgumentNullException("catalogPart");
226             }
227             catalogPart.RenderControl(writer);
228         }
229 
RenderTitle(HtmlTextWriter writer, CatalogPart catalogPart)230         private void RenderTitle(HtmlTextWriter writer, CatalogPart catalogPart) {
231             Label label = new Label();
232             label.Text = catalogPart.DisplayTitle;
233             label.ToolTip = catalogPart.Description;
234             label.Page = _page;
235             label.RenderControl(writer);
236         }
237     }
238 }
239 
240