| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- //
- // System.Web.UI.WebControls.RepeatInfo.cs
- //
- // Authors:
- // Ben Maurer ([email protected])
- //
- // (C) 2005 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- //#define DEBUG_REPEAT_INFO
- using System.Diagnostics;
- using System.ComponentModel;
- using System.Security.Permissions;
- namespace System.Web.UI.WebControls {
- // CAS - no inheritance demand required because the class is sealed
- [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
- public sealed class RepeatInfo {
- // What is baseControl for ?
- public void RenderRepeater (HtmlTextWriter w, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
- {
- PrintValues (user);
- if (RepeatDirection == RepeatDirection.Vertical)
- RenderVert (w, user, controlStyle, baseControl);
- else
- RenderHoriz (w, user, controlStyle, baseControl);
- }
- void RenderBr (HtmlTextWriter w)
- {
- #if NET_2_0
- w.Write ("<br />");
- #else
- // grrr, not xhtml...
- w.Write ("<br>");
- #endif
- }
- void RenderVert (HtmlTextWriter w, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
- {
- int itms = user.RepeatedItemCount;
- // total number of rows/columns in our table
- int cols = RepeatColumns == 0 ? 1 : RepeatColumns;
- // this gets ceil (itms / cols)
- int rows = (itms + cols - 1) / cols;
- bool sep = user.HasSeparators;
- bool oti = OuterTableImplied;
- int hdr_span = cols * ((sep && cols != 1) ? 2 : 1);
- bool table = RepeatLayout == RepeatLayout.Table && !oti;
- #if NET_2_0
- bool show_empty_trailing_items = true;
- bool show_empty_trailing_sep = true;
- #else
- bool show_empty_trailing_items = false;
- bool show_empty_trailing_sep = false;
- #endif
-
- if (! oti)
- RenderBeginTag (w, controlStyle, baseControl);
- if (Caption.Length > 0) {
- if (CaptionAlign != TableCaptionAlign.NotSet)
- w.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString());
- w.RenderBeginTag (HtmlTextWriterTag.Caption);
- w.Write (Caption);
- w.RenderEndTag ();
- }
- // Render the header
- if (user.HasHeader) {
- if (oti)
- user.RenderItem (ListItemType.Header, -1, this, w);
- else if (table) {
- w.RenderBeginTag (HtmlTextWriterTag.Tr);
- // Make sure the header takes up the full width. We have two
- // columns per item if we are using separators, otherwise
- // one per item.
- if (hdr_span != 1)
- w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString (), false);
- if (UseAccessibleHeader)
- w.AddAttribute ("scope", "col", false);
-
- Style s = user.GetItemStyle (ListItemType.Header, -1);
- if (s != null)
- s.AddAttributesToRender (w);
- if (UseAccessibleHeader)
- w.RenderBeginTag (HtmlTextWriterTag.Th);
- else
- w.RenderBeginTag (HtmlTextWriterTag.Td);
- user.RenderItem (ListItemType.Header, -1, this, w);
- w.RenderEndTag (); // td
- w.RenderEndTag (); // tr
- } else {
- user.RenderItem (ListItemType.Header, -1, this, w);
- RenderBr (w);
- }
- }
- for (int r = 0; r < rows; r ++) {
- if (table)
- w.RenderBeginTag (HtmlTextWriterTag.Tr);
-
- for (int c = 0; c < cols; c ++) {
- // Find the item number we are in according to the repeat
- // direction.
- int item = index_vert (rows, cols, r, c, itms);
- // This item is blank because there there not enough items
- // to make a full row.
- if (!show_empty_trailing_items && item >= itms)
- continue;
- if (table) {
- Style s = null;
- if (item < itms)
- s = user.GetItemStyle (ListItemType.Item, item);
- if (s != null)
- s.AddAttributesToRender (w);
- w.RenderBeginTag (HtmlTextWriterTag.Td);
- }
-
- if (item < itms)
- user.RenderItem (ListItemType.Item, item, this, w);
- if (table)
- w.RenderEndTag (); // td
- if (sep && cols != 1) {
- if (table) {
- if (item < itms - 1) {
- Style s = user.GetItemStyle (ListItemType.Separator, item);
- if (s != null)
- s.AddAttributesToRender (w);
- }
- if (item < itms - 1 || show_empty_trailing_sep)
- w.RenderBeginTag (HtmlTextWriterTag.Td);
- }
- if (item < itms - 1)
- user.RenderItem (ListItemType.Separator, item, this, w);
- if (table && (item < itms - 1 || show_empty_trailing_sep))
- w.RenderEndTag (); // td
- }
- }
- if (oti) {
- } else if (table) {
- w.RenderEndTag (); // tr
- } else if (r != rows - 1) {
- RenderBr(w);
- }
-
- if (sep && r != rows - 1 /* no sep on last item */ && cols == 1) {
- if (table) {
- w.RenderBeginTag (HtmlTextWriterTag.Tr);
- Style s = user.GetItemStyle (ListItemType.Separator, r);
- if (s != null)
- s.AddAttributesToRender (w);
-
- w.RenderBeginTag (HtmlTextWriterTag.Td);
- } else if (oti) {
- #if !NET_2_0
- /* 2.0 doesn't render this <br /> */
- RenderBr (w);
- #endif
- }
-
- user.RenderItem (ListItemType.Separator, r, this, w);
- if (table) {
- w.RenderEndTag (); // td
- w.RenderEndTag (); // tr
- } else if (!oti) {
- RenderBr (w);
- }
- }
- }
- // Render the footer
- if (user.HasFooter) {
- if (oti)
- user.RenderItem (ListItemType.Footer, -1, this, w);
- else if (table) {
- w.RenderBeginTag (HtmlTextWriterTag.Tr);
- if (hdr_span != 1)
- w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString (), false);
- Style s = user.GetItemStyle (ListItemType.Footer, -1);
- if (s != null)
- s.AddAttributesToRender (w);
-
- w.RenderBeginTag (HtmlTextWriterTag.Td);
- user.RenderItem (ListItemType.Footer, -1, this, w);
- w.RenderEndTag (); // td
- w.RenderEndTag (); // tr
- } else {
- // avoid dups on 0 items
- if (itms != 0)
- RenderBr (w);
- user.RenderItem (ListItemType.Footer, -1, this, w);
- }
- }
- if (! oti)
- w.RenderEndTag (); // table/span
-
- }
-
- void RenderHoriz (HtmlTextWriter w, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
- {
- int itms = user.RepeatedItemCount;
- // total number of rows/columns in our table
- int cols = RepeatColumns == 0 ? itms : RepeatColumns;
- // this gets ceil (itms / cols)
- int rows = cols == 0 ? 0 : (itms + cols - 1) / cols;
- bool sep = user.HasSeparators;
- //bool oti = OuterTableImplied;
- int hdr_span = cols * (sep ? 2 : 1);
- bool table = RepeatLayout == RepeatLayout.Table;
- #if NET_2_0
- bool show_empty_trailing_items = true;
- bool show_empty_trailing_sep = true;
- #else
- bool show_empty_trailing_items = false;
- bool show_empty_trailing_sep = false;
- #endif
- RenderBeginTag (w, controlStyle, baseControl);
- if (Caption.Length > 0) {
- if (CaptionAlign != TableCaptionAlign.NotSet)
- w.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString());
- w.RenderBeginTag (HtmlTextWriterTag.Caption);
- w.Write (Caption);
- w.RenderEndTag ();
- }
-
- // Render the header
- if (user.HasHeader) {
- if (table) {
- w.RenderBeginTag (HtmlTextWriterTag.Tr);
- // Make sure the header takes up the full width. We have two
- // columns per item if we are using separators, otherwise
- // one per item.
- if (hdr_span != 1)
- w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString (), false);
- if (UseAccessibleHeader)
- w.AddAttribute ("scope", "col", false);
- Style s = user.GetItemStyle (ListItemType.Header, -1);
- if (s != null)
- s.AddAttributesToRender (w);
- if (UseAccessibleHeader)
- w.RenderBeginTag (HtmlTextWriterTag.Th);
- else
- w.RenderBeginTag (HtmlTextWriterTag.Td);
- user.RenderItem (ListItemType.Header, -1, this, w);
- w.RenderEndTag (); // td
- w.RenderEndTag (); // tr
- } else {
- user.RenderItem (ListItemType.Header, -1, this, w);
- if (!table && RepeatColumns != 0 && itms != 0)
- RenderBr (w);
- }
- }
-
- for (int r = 0; r < rows; r ++) {
- if (table)
- w.RenderBeginTag (HtmlTextWriterTag.Tr);
-
- for (int c = 0; c < cols; c ++) {
- // Find the item number we are in according to the repeat
- // direction.
- int item = r * cols + c;
- // This item is blank because there there not enough items
- // to make a full row.
- if (!show_empty_trailing_items && item >= itms)
- continue;
- if (table) {
- Style s = null;
- if (item < itms)
- s = user.GetItemStyle (ListItemType.Item, item);
- if (s != null)
- s.AddAttributesToRender (w);
- w.RenderBeginTag (HtmlTextWriterTag.Td);
- }
- if (item < itms)
- user.RenderItem (ListItemType.Item, item, this, w);
- if (table)
- w.RenderEndTag (); // td
- if (sep) {
- if (table) {
- if (item < itms - 1) {
- Style s = user.GetItemStyle (ListItemType.Separator, item);
- if (s != null)
- s.AddAttributesToRender (w);
- }
- if (item < itms - 1 || show_empty_trailing_sep)
- w.RenderBeginTag (HtmlTextWriterTag.Td);
- }
- if (item < itms - 1)
- user.RenderItem (ListItemType.Separator, item, this, w);
- if (table && (item < itms - 1 || show_empty_trailing_sep))
- w.RenderEndTag (); // td
- }
- }
- if (table) {
- // if (!oti)
- w.RenderEndTag (); // tr
- } else if (!(r == rows -1 && RepeatColumns == 0))
- RenderBr (w);
-
- }
- // Render the footer
- if (user.HasFooter) {
- if (table) {
- w.RenderBeginTag (HtmlTextWriterTag.Tr);
- if (hdr_span != 1)
- w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString (), false);
- Style s = user.GetItemStyle (ListItemType.Footer, -1);
- if (s != null)
- s.AddAttributesToRender (w);
-
- w.RenderBeginTag (HtmlTextWriterTag.Td);
- user.RenderItem (ListItemType.Footer, -1, this, w);
- w.RenderEndTag (); // td
- w.RenderEndTag (); // tr
- } else {
- user.RenderItem (ListItemType.Footer, -1, this, w);
- }
- }
- if (true)
- w.RenderEndTag (); // table/span
-
- }
- int index_vert (int rows, int cols, int r, int c, int items)
- {
- int last = items % cols;
- if (last == 0)
- last = cols;
- if (r == rows - 1 && c >= last)
- return items;
-
-
- int add;
- int v;
- if (c > last){
- add = last * rows + (c-last) * (rows-1);
- v = add + r;
- } else
- v = rows * c + r;
-
- return v;
- }
- void RenderBeginTag (HtmlTextWriter w, Style s, WebControl wc)
- {
- WebControl c;
- if (RepeatLayout == RepeatLayout.Table)
- c = new Table ();
- else
- c = new Label ();
- c.ID = wc.ClientID;
- c.CopyBaseAttributes (wc);
- c.ApplyStyle (s);
- c.Enabled = wc.IsEnabled;
- c.RenderBeginTag (w);
- }
-
-
- bool outer_table_implied;
- public bool OuterTableImplied {
- get {
- return outer_table_implied;
- }
- set {
- outer_table_implied = value;
- }
- }
- int repeat_cols;
- public int RepeatColumns {
- get {
- return repeat_cols;
- }
- set {
- repeat_cols = value;
- }
- }
- RepeatDirection dir = RepeatDirection.Vertical;
- public RepeatDirection RepeatDirection {
- get {
- return dir;
- }
- set {
- if (value != RepeatDirection.Horizontal &&
- value != RepeatDirection.Vertical)
- throw new ArgumentOutOfRangeException ();
-
- dir = value;
- }
- }
- RepeatLayout layout;
- public RepeatLayout RepeatLayout {
- get {
- return layout;
- }
- set {
- if (value != RepeatLayout.Flow &&
- value != RepeatLayout.Table)
- throw new ArgumentOutOfRangeException ();
- layout = value;
- }
- }
- [Conditional ("DEBUG_REPEAT_INFO")]
- internal void PrintValues (IRepeatInfoUser riu)
- {
- string s = String.Format ("Layout {0}; Direction {1}; Cols {2}; OuterTableImplied {3}\n" +
- "User: itms {4}, hdr {5}; ftr {6}; sep {7}", RepeatLayout, RepeatDirection,
- RepeatColumns, OuterTableImplied, riu.RepeatedItemCount, riu.HasSeparators, riu.HasHeader,
- riu.HasFooter, riu.HasSeparators
- );
- Console.WriteLine (s);
- if (HttpContext.Current != null)
- HttpContext.Current.Trace.Write (s);
- }
- string caption = String.Empty;
- TableCaptionAlign captionAlign = TableCaptionAlign.NotSet;
- bool useAccessibleHeader = false;
- [WebSysDescription ("")]
- [WebCategory ("Accessibility")]
- public string Caption {
- get {return caption;}
- set { caption = value; }
- }
- [WebSysDescription ("")]
- [WebCategory ("Accessibility")]
- public TableCaptionAlign CaptionAlign {
- get {return captionAlign;}
- set { captionAlign = value; }
- }
- [WebSysDescription ("")]
- [WebCategory ("Accessibility")]
- public bool UseAccessibleHeader {
- get {return useAccessibleHeader;}
- set { useAccessibleHeader = value; }
- }
- }
- }
|