2 // System.Web.UI.WebControls.RepeatInfo.cs
5 // Ben Maurer (bmaurer@novell.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //#define DEBUG_REPEAT_INFO
31 using System.Diagnostics;
32 using System.ComponentModel;
33 using System.Security.Permissions;
35 namespace System.Web.UI.WebControls {
37 // CAS - no inheritance demand required because the class is sealed
38 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 public sealed class RepeatInfo {
41 // What is baseControl for ?
42 public void RenderRepeater (HtmlTextWriter w, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
46 if (RepeatDirection == RepeatDirection.Vertical)
47 RenderVert (w, user, controlStyle, baseControl);
49 RenderHoriz (w, user, controlStyle, baseControl);
52 void RenderBr (HtmlTextWriter w)
63 void RenderVert (HtmlTextWriter w, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
65 int itms = user.RepeatedItemCount;
66 // total number of rows/columns in our table
67 int cols = RepeatColumns == 0 ? 1 : RepeatColumns;
68 // this gets ceil (itms / cols)
69 int rows = (itms + cols - 1) / cols;
70 bool sep = user.HasSeparators;
71 bool oti = OuterTableImplied;
72 int hdr_span = cols * ((sep && cols != 1) ? 2 : 1);
74 bool table = RepeatLayout == RepeatLayout.Table && !oti;
77 bool show_empty_trailing_items = true;
78 bool show_empty_trailing_sep = true;
80 bool show_empty_trailing_items = false;
81 bool show_empty_trailing_sep = false;
85 RenderBeginTag (w, controlStyle, baseControl);
87 if (UseAccessibleHeader) {
88 if (CaptionAlign != TableCaptionAlign.NotSet)
89 w.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString());
91 w.RenderBeginTag (HtmlTextWriterTag.Caption);
100 user.RenderItem (ListItemType.Header, -1, this, w);
102 w.RenderBeginTag (HtmlTextWriterTag.Tr);
103 // Make sure the header takes up the full width. We have two
104 // columns per item if we are using separators, otherwise
107 w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString ());
109 Style s = user.GetItemStyle (ListItemType.Header, -1);
111 s.AddAttributesToRender (w);
113 w.RenderBeginTag (HtmlTextWriterTag.Td);
114 user.RenderItem (ListItemType.Header, -1, this, w);
115 w.RenderEndTag (); // td
116 w.RenderEndTag (); // tr
118 user.RenderItem (ListItemType.Header, -1, this, w);
123 for (int r = 0; r < rows; r ++) {
125 w.RenderBeginTag (HtmlTextWriterTag.Tr);
127 for (int c = 0; c < cols; c ++) {
128 // Find the item number we are in according to the repeat
130 int item = index_vert (rows, cols, r, c, itms);
132 // This item is blank because there there not enough items
133 // to make a full row.
134 if (!show_empty_trailing_items && item >= itms)
140 s = user.GetItemStyle (ListItemType.Item, item);
142 s.AddAttributesToRender (w);
143 w.RenderBeginTag (HtmlTextWriterTag.Td);
147 user.RenderItem (ListItemType.Item, item, this, w);
150 w.RenderEndTag (); // td
152 if (sep && cols != 1) {
154 if (item < itms - 1) {
155 Style s = user.GetItemStyle (ListItemType.Separator, item);
157 s.AddAttributesToRender (w);
159 if (item < itms - 1 || show_empty_trailing_sep)
160 w.RenderBeginTag (HtmlTextWriterTag.Td);
164 user.RenderItem (ListItemType.Separator, item, this, w);
166 if (table && (item < itms - 1 || show_empty_trailing_sep))
167 w.RenderEndTag (); // td
172 w.RenderEndTag (); // tr
173 } else if (r != rows - 1) {
177 if (sep && r != rows - 1 /* no sep on last item */ && cols == 1) {
179 w.RenderBeginTag (HtmlTextWriterTag.Tr);
180 Style s = user.GetItemStyle (ListItemType.Separator, r);
182 s.AddAttributesToRender (w);
184 w.RenderBeginTag (HtmlTextWriterTag.Td);
187 /* 2.0 doesn't render this <br /> */
192 user.RenderItem (ListItemType.Separator, r, this, w);
195 w.RenderEndTag (); // td
196 w.RenderEndTag (); // tr
204 if (user.HasFooter) {
206 user.RenderItem (ListItemType.Footer, -1, this, w);
208 w.RenderBeginTag (HtmlTextWriterTag.Tr);
210 w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString ());
212 Style s = user.GetItemStyle (ListItemType.Footer, -1);
214 s.AddAttributesToRender (w);
216 w.RenderBeginTag (HtmlTextWriterTag.Td);
217 user.RenderItem (ListItemType.Footer, -1, this, w);
218 w.RenderEndTag (); // td
219 w.RenderEndTag (); // tr
221 // avoid dups on 0 items
224 user.RenderItem (ListItemType.Footer, -1, this, w);
228 w.RenderEndTag (); // table/span
232 void RenderHoriz (HtmlTextWriter w, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
234 int itms = user.RepeatedItemCount;
235 // total number of rows/columns in our table
236 int cols = RepeatColumns == 0 ? itms : RepeatColumns;
237 // this gets ceil (itms / cols)
238 int rows = cols == 0 ? 0 : (itms + cols - 1) / cols;
239 bool sep = user.HasSeparators;
240 //bool oti = OuterTableImplied;
241 int hdr_span = cols * (sep ? 2 : 1);
243 bool table = RepeatLayout == RepeatLayout.Table;
246 bool show_empty_trailing_items = true;
247 bool show_empty_trailing_sep = true;
249 bool show_empty_trailing_items = false;
250 bool show_empty_trailing_sep = false;
253 RenderBeginTag (w, controlStyle, baseControl);
255 if (UseAccessibleHeader) {
256 if (CaptionAlign != TableCaptionAlign.NotSet)
257 w.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString());
259 w.RenderBeginTag (HtmlTextWriterTag.Caption);
266 if (user.HasHeader) {
268 w.RenderBeginTag (HtmlTextWriterTag.Tr);
269 // Make sure the header takes up the full width. We have two
270 // columns per item if we are using separators, otherwise
273 w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString ());
275 Style s = user.GetItemStyle (ListItemType.Header, -1);
277 s.AddAttributesToRender (w);
279 w.RenderBeginTag (HtmlTextWriterTag.Td);
280 user.RenderItem (ListItemType.Header, -1, this, w);
281 w.RenderEndTag (); // td
282 w.RenderEndTag (); // tr
284 user.RenderItem (ListItemType.Header, -1, this, w);
285 if (!table && RepeatColumns != 0 && itms != 0)
290 for (int r = 0; r < rows; r ++) {
292 w.RenderBeginTag (HtmlTextWriterTag.Tr);
294 for (int c = 0; c < cols; c ++) {
295 // Find the item number we are in according to the repeat
297 int item = r * cols + c;
299 // This item is blank because there there not enough items
300 // to make a full row.
301 if (!show_empty_trailing_items && item >= itms)
307 s = user.GetItemStyle (ListItemType.Item, item);
310 s.AddAttributesToRender (w);
311 w.RenderBeginTag (HtmlTextWriterTag.Td);
315 user.RenderItem (ListItemType.Item, item, this, w);
318 w.RenderEndTag (); // td
322 if (item < itms - 1) {
323 Style s = user.GetItemStyle (ListItemType.Separator, item);
325 s.AddAttributesToRender (w);
327 if (item < itms - 1 || show_empty_trailing_sep)
328 w.RenderBeginTag (HtmlTextWriterTag.Td);
332 user.RenderItem (ListItemType.Separator, item, this, w);
334 if (table && (item < itms - 1 || show_empty_trailing_sep))
335 w.RenderEndTag (); // td
341 w.RenderEndTag (); // tr
342 } else if (!(r == rows -1 && RepeatColumns == 0))
348 if (user.HasFooter) {
350 w.RenderBeginTag (HtmlTextWriterTag.Tr);
352 w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString ());
354 Style s = user.GetItemStyle (ListItemType.Footer, -1);
356 s.AddAttributesToRender (w);
358 w.RenderBeginTag (HtmlTextWriterTag.Td);
359 user.RenderItem (ListItemType.Footer, -1, this, w);
360 w.RenderEndTag (); // td
361 w.RenderEndTag (); // tr
363 user.RenderItem (ListItemType.Footer, -1, this, w);
367 w.RenderEndTag (); // table/span
371 int index_vert (int rows, int cols, int r, int c, int items)
373 int last = items % cols;
377 if (r == rows - 1 && c >= last)
384 add = last * rows + (c-last) * (rows-1);
392 void RenderBeginTag (HtmlTextWriter w, Style s, WebControl wc)
395 if (RepeatLayout == RepeatLayout.Table)
401 c.CopyBaseAttributes (wc);
403 c.Enabled = wc.Enabled;
404 c.RenderBeginTag (w);
408 bool outer_table_implied;
409 public bool OuterTableImplied {
411 return outer_table_implied;
414 outer_table_implied = value;
419 public int RepeatColumns {
428 RepeatDirection dir = RepeatDirection.Vertical;
429 public RepeatDirection RepeatDirection {
434 if (value != RepeatDirection.Horizontal &&
435 value != RepeatDirection.Vertical)
436 throw new ArgumentOutOfRangeException ();
443 public RepeatLayout RepeatLayout {
448 if (value != RepeatLayout.Flow &&
449 value != RepeatLayout.Table)
450 throw new ArgumentOutOfRangeException ();
455 [Conditional ("DEBUG_REPEAT_INFO")]
456 internal void PrintValues (IRepeatInfoUser riu)
458 string s = String.Format ("Layout {0}; Direction {1}; Cols {2}; OuterTableImplied {3}\n" +
459 "User: itms {4}, hdr {5}; ftr {6}; sep {7}", RepeatLayout, RepeatDirection,
460 RepeatColumns, OuterTableImplied, riu.RepeatedItemCount, riu.HasSeparators, riu.HasHeader,
461 riu.HasFooter, riu.HasSeparators
463 Console.WriteLine (s);
464 if (HttpContext.Current != null)
465 HttpContext.Current.Trace.Write (s);
468 private string caption = "";
469 private TableCaptionAlign captionAlign = TableCaptionAlign.NotSet;
470 private bool useAccessibleHeader = false;
472 [WebSysDescription ("")]
473 [WebCategory ("Accessibility")]
474 public string Caption {
475 get {return caption;}
476 set { caption = value; }
479 [WebSysDescription ("")]
480 [DefaultValue (TableCaptionAlign.NotSet)]
481 [WebCategory ("Accessibility")]
482 public TableCaptionAlign CaptionAlign {
483 get {return captionAlign;}
484 set { captionAlign = value; }
487 [WebSysDescription ("")]
488 [DefaultValue (false)]
489 [WebCategory ("Accessibility")]
490 public bool UseAccessibleHeader {
491 get {return useAccessibleHeader;}
492 set { useAccessibleHeader = value; }