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;
33 namespace System.Web.UI.WebControls {
35 public sealed class RepeatInfo {
37 // What is baseControl for ?
38 public void RenderRepeater (HtmlTextWriter w, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
42 if (RepeatDirection == RepeatDirection.Vertical)
43 RenderVert (w, user, controlStyle, baseControl);
45 RenderHoriz (w, user, controlStyle, baseControl);
48 void RenderBr (HtmlTextWriter w)
59 void RenderVert (HtmlTextWriter w, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
61 int itms = user.RepeatedItemCount;
62 // total number of rows/columns in our table
63 int cols = RepeatColumns == 0 ? 1 : RepeatColumns;
64 // this gets ceil (itms / cols)
65 int rows = (itms + cols - 1) / cols;
66 bool sep = user.HasSeparators;
67 bool oti = OuterTableImplied;
68 int hdr_span = cols * ((sep && cols != 1) ? 2 : 1);
70 bool table = RepeatLayout == RepeatLayout.Table && !oti;
73 bool show_empty_trailing_items = true;
74 bool show_empty_trailing_sep = true;
76 bool show_empty_trailing_items = false;
77 bool show_empty_trailing_sep = false;
81 RenderBeginTag (w, controlStyle, baseControl);
86 user.RenderItem (ListItemType.Header, -1, this, w);
88 w.RenderBeginTag (HtmlTextWriterTag.Tr);
89 // Make sure the header takes up the full width. We have two
90 // columns per item if we are using separators, otherwise
93 w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString ());
95 Style s = user.GetItemStyle (ListItemType.Header, -1);
97 s.AddAttributesToRender (w);
99 w.RenderBeginTag (HtmlTextWriterTag.Td);
100 user.RenderItem (ListItemType.Header, -1, this, w);
101 w.RenderEndTag (); // td
102 w.RenderEndTag (); // tr
104 user.RenderItem (ListItemType.Header, -1, this, w);
109 for (int r = 0; r < rows; r ++) {
111 w.RenderBeginTag (HtmlTextWriterTag.Tr);
113 for (int c = 0; c < cols; c ++) {
114 // Find the item number we are in according to the repeat
116 int item = index_vert (rows, cols, r, c, itms);
118 // This item is blank because there there not enough items
119 // to make a full row.
120 if (!show_empty_trailing_items && item >= itms)
126 s = user.GetItemStyle (ListItemType.Item, item);
128 s.AddAttributesToRender (w);
129 w.RenderBeginTag (HtmlTextWriterTag.Td);
133 user.RenderItem (ListItemType.Item, item, this, w);
136 w.RenderEndTag (); // td
138 if (sep && cols != 1) {
140 if (item < itms - 1) {
141 Style s = user.GetItemStyle (ListItemType.Separator, item);
143 s.AddAttributesToRender (w);
145 if (item < itms - 1 || show_empty_trailing_sep)
146 w.RenderBeginTag (HtmlTextWriterTag.Td);
150 user.RenderItem (ListItemType.Separator, item, this, w);
152 if (table && (item < itms - 1 || show_empty_trailing_sep))
153 w.RenderEndTag (); // td
158 w.RenderEndTag (); // tr
159 } else if (r != rows - 1) {
163 if (sep && r != rows - 1 /* no sep on last item */ && cols == 1) {
165 w.RenderBeginTag (HtmlTextWriterTag.Tr);
166 Style s = user.GetItemStyle (ListItemType.Separator, r);
168 s.AddAttributesToRender (w);
170 w.RenderBeginTag (HtmlTextWriterTag.Td);
173 /* 2.0 doesn't render this <br /> */
178 user.RenderItem (ListItemType.Separator, r, this, w);
181 w.RenderEndTag (); // td
182 w.RenderEndTag (); // tr
190 if (user.HasFooter) {
192 user.RenderItem (ListItemType.Footer, -1, this, w);
194 w.RenderBeginTag (HtmlTextWriterTag.Tr);
196 w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString ());
198 Style s = user.GetItemStyle (ListItemType.Footer, -1);
200 s.AddAttributesToRender (w);
202 w.RenderBeginTag (HtmlTextWriterTag.Td);
203 user.RenderItem (ListItemType.Footer, -1, this, w);
204 w.RenderEndTag (); // td
205 w.RenderEndTag (); // tr
207 // avoid dups on 0 items
210 user.RenderItem (ListItemType.Footer, -1, this, w);
214 w.RenderEndTag (); // table/span
218 void RenderHoriz (HtmlTextWriter w, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
220 int itms = user.RepeatedItemCount;
221 // total number of rows/columns in our table
222 int cols = RepeatColumns == 0 ? itms : RepeatColumns;
223 // this gets ceil (itms / cols)
224 int rows = cols == 0 ? 0 : (itms + cols - 1) / cols;
225 bool sep = user.HasSeparators;
226 //bool oti = OuterTableImplied;
227 int hdr_span = cols * (sep ? 2 : 1);
229 bool table = RepeatLayout == RepeatLayout.Table;
232 bool show_empty_trailing_items = true;
233 bool show_empty_trailing_sep = true;
235 bool show_empty_trailing_items = false;
236 bool show_empty_trailing_sep = false;
239 RenderBeginTag (w, controlStyle, baseControl);
242 if (user.HasHeader) {
244 w.RenderBeginTag (HtmlTextWriterTag.Tr);
245 // Make sure the header takes up the full width. We have two
246 // columns per item if we are using separators, otherwise
249 w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString ());
251 Style s = user.GetItemStyle (ListItemType.Header, -1);
253 s.AddAttributesToRender (w);
255 w.RenderBeginTag (HtmlTextWriterTag.Td);
256 user.RenderItem (ListItemType.Header, -1, this, w);
257 w.RenderEndTag (); // td
258 w.RenderEndTag (); // tr
260 user.RenderItem (ListItemType.Header, -1, this, w);
261 if (!table && RepeatColumns != 0 && itms != 0)
266 for (int r = 0; r < rows; r ++) {
268 w.RenderBeginTag (HtmlTextWriterTag.Tr);
270 for (int c = 0; c < cols; c ++) {
271 // Find the item number we are in according to the repeat
273 int item = r * cols + c;
275 // This item is blank because there there not enough items
276 // to make a full row.
277 if (!show_empty_trailing_items && item >= itms)
283 s = user.GetItemStyle (ListItemType.Item, item);
286 s.AddAttributesToRender (w);
287 w.RenderBeginTag (HtmlTextWriterTag.Td);
291 user.RenderItem (ListItemType.Item, item, this, w);
294 w.RenderEndTag (); // td
298 if (item < itms - 1) {
299 Style s = user.GetItemStyle (ListItemType.Separator, item);
301 s.AddAttributesToRender (w);
303 if (item < itms - 1 || show_empty_trailing_sep)
304 w.RenderBeginTag (HtmlTextWriterTag.Td);
308 user.RenderItem (ListItemType.Separator, item, this, w);
310 if (table && (item < itms - 1 || show_empty_trailing_sep))
311 w.RenderEndTag (); // td
317 w.RenderEndTag (); // tr
318 } else if (!(r == rows -1 && RepeatColumns == 0))
324 if (user.HasFooter) {
326 w.RenderBeginTag (HtmlTextWriterTag.Tr);
328 w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString ());
330 Style s = user.GetItemStyle (ListItemType.Footer, -1);
332 s.AddAttributesToRender (w);
334 w.RenderBeginTag (HtmlTextWriterTag.Td);
335 user.RenderItem (ListItemType.Footer, -1, this, w);
336 w.RenderEndTag (); // td
337 w.RenderEndTag (); // tr
339 user.RenderItem (ListItemType.Footer, -1, this, w);
343 w.RenderEndTag (); // table/span
347 int index_vert (int rows, int cols, int r, int c, int items)
349 int last = items % cols;
353 if (r == rows - 1 && c >= last)
360 add = last * rows + (c-last) * (rows-1);
368 void RenderBeginTag (HtmlTextWriter w, Style s, WebControl wc)
371 if (RepeatLayout == RepeatLayout.Table)
377 c.CopyBaseAttributes (wc);
379 c.RenderBeginTag (w);
383 bool outer_table_implied;
384 public bool OuterTableImplied {
386 return outer_table_implied;
389 outer_table_implied = value;
394 public int RepeatColumns {
403 RepeatDirection dir = RepeatDirection.Vertical;
404 public RepeatDirection RepeatDirection {
409 if (value != RepeatDirection.Horizontal &&
410 value != RepeatDirection.Vertical)
411 throw new ArgumentOutOfRangeException ();
418 public RepeatLayout RepeatLayout {
423 if (value != RepeatLayout.Flow &&
424 value != RepeatLayout.Table)
425 throw new ArgumentOutOfRangeException ();
430 [Conditional ("DEBUG_REPEAT_INFO")]
431 internal void PrintValues (IRepeatInfoUser riu)
433 string s = String.Format ("Layout {0}; Direction {1}; Cols {2}; OuterTableImplied {3}\n" +
434 "User: itms {4}, hdr {5}; ftr {6}; sep {7}", RepeatLayout, RepeatDirection,
435 RepeatColumns, OuterTableImplied, riu.RepeatedItemCount, riu.HasSeparators, riu.HasHeader,
436 riu.HasFooter, riu.HasSeparators
438 Console.WriteLine (s);
439 if (HttpContext.Current != null)
440 HttpContext.Current.Trace.Write (s);