RepeatInfo.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. //
  2. // System.Web.UI.WebControls.RepeatInfo.cs
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. //#define DEBUG_REPEAT_INFO
  29. using System.Diagnostics;
  30. using System.ComponentModel;
  31. using System.Security.Permissions;
  32. namespace System.Web.UI.WebControls {
  33. // CAS - no inheritance demand required because the class is sealed
  34. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. public sealed class RepeatInfo {
  36. // What is baseControl for ?
  37. public void RenderRepeater (HtmlTextWriter w, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
  38. {
  39. PrintValues (user);
  40. if (RepeatDirection == RepeatDirection.Vertical)
  41. RenderVert (w, user, controlStyle, baseControl);
  42. else
  43. RenderHoriz (w, user, controlStyle, baseControl);
  44. }
  45. void RenderBr (HtmlTextWriter w)
  46. {
  47. #if NET_2_0
  48. w.Write ("<br />");
  49. #else
  50. // grrr, not xhtml...
  51. w.Write ("<br>");
  52. #endif
  53. }
  54. void RenderVert (HtmlTextWriter w, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
  55. {
  56. int itms = user.RepeatedItemCount;
  57. // total number of rows/columns in our table
  58. int cols = RepeatColumns == 0 ? 1 : RepeatColumns;
  59. // this gets ceil (itms / cols)
  60. int rows = (itms + cols - 1) / cols;
  61. bool sep = user.HasSeparators;
  62. bool oti = OuterTableImplied;
  63. int hdr_span = cols * ((sep && cols != 1) ? 2 : 1);
  64. bool table = RepeatLayout == RepeatLayout.Table && !oti;
  65. #if NET_2_0
  66. bool show_empty_trailing_items = true;
  67. bool show_empty_trailing_sep = true;
  68. #else
  69. bool show_empty_trailing_items = false;
  70. bool show_empty_trailing_sep = false;
  71. #endif
  72. if (! oti)
  73. RenderBeginTag (w, controlStyle, baseControl);
  74. if (UseAccessibleHeader) {
  75. if (CaptionAlign != TableCaptionAlign.NotSet)
  76. w.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString());
  77. w.RenderBeginTag (HtmlTextWriterTag.Caption);
  78. w.Write (Caption);
  79. w.RenderEndTag ();
  80. }
  81. // Render the header
  82. if (user.HasHeader) {
  83. if (oti)
  84. user.RenderItem (ListItemType.Header, -1, this, w);
  85. else if (table) {
  86. w.RenderBeginTag (HtmlTextWriterTag.Tr);
  87. // Make sure the header takes up the full width. We have two
  88. // columns per item if we are using separators, otherwise
  89. // one per item.
  90. if (hdr_span != 1)
  91. w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString ());
  92. Style s = user.GetItemStyle (ListItemType.Header, -1);
  93. if (s != null)
  94. s.AddAttributesToRender (w);
  95. w.RenderBeginTag (HtmlTextWriterTag.Td);
  96. user.RenderItem (ListItemType.Header, -1, this, w);
  97. w.RenderEndTag (); // td
  98. w.RenderEndTag (); // tr
  99. } else {
  100. user.RenderItem (ListItemType.Header, -1, this, w);
  101. RenderBr (w);
  102. }
  103. }
  104. for (int r = 0; r < rows; r ++) {
  105. if (table)
  106. w.RenderBeginTag (HtmlTextWriterTag.Tr);
  107. for (int c = 0; c < cols; c ++) {
  108. // Find the item number we are in according to the repeat
  109. // direction.
  110. int item = index_vert (rows, cols, r, c, itms);
  111. // This item is blank because there there not enough items
  112. // to make a full row.
  113. if (!show_empty_trailing_items && item >= itms)
  114. continue;
  115. if (table) {
  116. Style s = null;
  117. if (item < itms)
  118. s = user.GetItemStyle (ListItemType.Item, item);
  119. if (s != null)
  120. s.AddAttributesToRender (w);
  121. w.RenderBeginTag (HtmlTextWriterTag.Td);
  122. }
  123. if (item < itms)
  124. user.RenderItem (ListItemType.Item, item, this, w);
  125. if (table)
  126. w.RenderEndTag (); // td
  127. if (sep && cols != 1) {
  128. if (table) {
  129. if (item < itms - 1) {
  130. Style s = user.GetItemStyle (ListItemType.Separator, item);
  131. if (s != null)
  132. s.AddAttributesToRender (w);
  133. }
  134. if (item < itms - 1 || show_empty_trailing_sep)
  135. w.RenderBeginTag (HtmlTextWriterTag.Td);
  136. }
  137. if (item < itms - 1)
  138. user.RenderItem (ListItemType.Separator, item, this, w);
  139. if (table && (item < itms - 1 || show_empty_trailing_sep))
  140. w.RenderEndTag (); // td
  141. }
  142. }
  143. if (oti) {
  144. } else if (table) {
  145. w.RenderEndTag (); // tr
  146. } else if (r != rows - 1) {
  147. RenderBr(w);
  148. }
  149. if (sep && r != rows - 1 /* no sep on last item */ && cols == 1) {
  150. if (table) {
  151. w.RenderBeginTag (HtmlTextWriterTag.Tr);
  152. Style s = user.GetItemStyle (ListItemType.Separator, r);
  153. if (s != null)
  154. s.AddAttributesToRender (w);
  155. w.RenderBeginTag (HtmlTextWriterTag.Td);
  156. } else if (oti) {
  157. #if !NET_2_0
  158. /* 2.0 doesn't render this <br /> */
  159. RenderBr (w);
  160. #endif
  161. }
  162. user.RenderItem (ListItemType.Separator, r, this, w);
  163. if (table) {
  164. w.RenderEndTag (); // td
  165. w.RenderEndTag (); // tr
  166. } else if (!oti) {
  167. RenderBr (w);
  168. }
  169. }
  170. }
  171. // Render the footer
  172. if (user.HasFooter) {
  173. if (oti)
  174. user.RenderItem (ListItemType.Footer, -1, this, w);
  175. else if (table) {
  176. w.RenderBeginTag (HtmlTextWriterTag.Tr);
  177. if (hdr_span != 1)
  178. w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString ());
  179. Style s = user.GetItemStyle (ListItemType.Footer, -1);
  180. if (s != null)
  181. s.AddAttributesToRender (w);
  182. w.RenderBeginTag (HtmlTextWriterTag.Td);
  183. user.RenderItem (ListItemType.Footer, -1, this, w);
  184. w.RenderEndTag (); // td
  185. w.RenderEndTag (); // tr
  186. } else {
  187. // avoid dups on 0 items
  188. if (itms != 0)
  189. RenderBr (w);
  190. user.RenderItem (ListItemType.Footer, -1, this, w);
  191. }
  192. }
  193. if (! oti)
  194. w.RenderEndTag (); // table/span
  195. }
  196. void RenderHoriz (HtmlTextWriter w, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
  197. {
  198. int itms = user.RepeatedItemCount;
  199. // total number of rows/columns in our table
  200. int cols = RepeatColumns == 0 ? itms : RepeatColumns;
  201. // this gets ceil (itms / cols)
  202. int rows = cols == 0 ? 0 : (itms + cols - 1) / cols;
  203. bool sep = user.HasSeparators;
  204. //bool oti = OuterTableImplied;
  205. int hdr_span = cols * (sep ? 2 : 1);
  206. bool table = RepeatLayout == RepeatLayout.Table;
  207. #if NET_2_0
  208. bool show_empty_trailing_items = true;
  209. bool show_empty_trailing_sep = true;
  210. #else
  211. bool show_empty_trailing_items = false;
  212. bool show_empty_trailing_sep = false;
  213. #endif
  214. RenderBeginTag (w, controlStyle, baseControl);
  215. if (UseAccessibleHeader) {
  216. if (CaptionAlign != TableCaptionAlign.NotSet)
  217. w.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString());
  218. w.RenderBeginTag (HtmlTextWriterTag.Caption);
  219. w.Write (Caption);
  220. w.RenderEndTag ();
  221. }
  222. // Render the header
  223. if (user.HasHeader) {
  224. if (table) {
  225. w.RenderBeginTag (HtmlTextWriterTag.Tr);
  226. // Make sure the header takes up the full width. We have two
  227. // columns per item if we are using separators, otherwise
  228. // one per item.
  229. if (hdr_span != 1)
  230. w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString ());
  231. Style s = user.GetItemStyle (ListItemType.Header, -1);
  232. if (s != null)
  233. s.AddAttributesToRender (w);
  234. w.RenderBeginTag (HtmlTextWriterTag.Td);
  235. user.RenderItem (ListItemType.Header, -1, this, w);
  236. w.RenderEndTag (); // td
  237. w.RenderEndTag (); // tr
  238. } else {
  239. user.RenderItem (ListItemType.Header, -1, this, w);
  240. if (!table && RepeatColumns != 0 && itms != 0)
  241. RenderBr (w);
  242. }
  243. }
  244. for (int r = 0; r < rows; r ++) {
  245. if (table)
  246. w.RenderBeginTag (HtmlTextWriterTag.Tr);
  247. for (int c = 0; c < cols; c ++) {
  248. // Find the item number we are in according to the repeat
  249. // direction.
  250. int item = r * cols + c;
  251. // This item is blank because there there not enough items
  252. // to make a full row.
  253. if (!show_empty_trailing_items && item >= itms)
  254. continue;
  255. if (table) {
  256. Style s = null;
  257. if (item < itms)
  258. s = user.GetItemStyle (ListItemType.Item, item);
  259. if (s != null)
  260. s.AddAttributesToRender (w);
  261. w.RenderBeginTag (HtmlTextWriterTag.Td);
  262. }
  263. if (item < itms)
  264. user.RenderItem (ListItemType.Item, item, this, w);
  265. if (table)
  266. w.RenderEndTag (); // td
  267. if (sep) {
  268. if (table) {
  269. if (item < itms - 1) {
  270. Style s = user.GetItemStyle (ListItemType.Separator, item);
  271. if (s != null)
  272. s.AddAttributesToRender (w);
  273. }
  274. if (item < itms - 1 || show_empty_trailing_sep)
  275. w.RenderBeginTag (HtmlTextWriterTag.Td);
  276. }
  277. if (item < itms - 1)
  278. user.RenderItem (ListItemType.Separator, item, this, w);
  279. if (table && (item < itms - 1 || show_empty_trailing_sep))
  280. w.RenderEndTag (); // td
  281. }
  282. }
  283. if (table) {
  284. // if (!oti)
  285. w.RenderEndTag (); // tr
  286. } else if (!(r == rows -1 && RepeatColumns == 0))
  287. RenderBr (w);
  288. }
  289. // Render the footer
  290. if (user.HasFooter) {
  291. if (table) {
  292. w.RenderBeginTag (HtmlTextWriterTag.Tr);
  293. if (hdr_span != 1)
  294. w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString ());
  295. Style s = user.GetItemStyle (ListItemType.Footer, -1);
  296. if (s != null)
  297. s.AddAttributesToRender (w);
  298. w.RenderBeginTag (HtmlTextWriterTag.Td);
  299. user.RenderItem (ListItemType.Footer, -1, this, w);
  300. w.RenderEndTag (); // td
  301. w.RenderEndTag (); // tr
  302. } else {
  303. user.RenderItem (ListItemType.Footer, -1, this, w);
  304. }
  305. }
  306. if (true)
  307. w.RenderEndTag (); // table/span
  308. }
  309. int index_vert (int rows, int cols, int r, int c, int items)
  310. {
  311. int last = items % cols;
  312. if (last == 0)
  313. last = cols;
  314. if (r == rows - 1 && c >= last)
  315. return items;
  316. int add;
  317. int v;
  318. if (c > last){
  319. add = last * rows + (c-last) * (rows-1);
  320. v = add + r;
  321. } else
  322. v = rows * c + r;
  323. return v;
  324. }
  325. void RenderBeginTag (HtmlTextWriter w, Style s, WebControl wc)
  326. {
  327. WebControl c;
  328. if (RepeatLayout == RepeatLayout.Table)
  329. c = new Table ();
  330. else
  331. c = new Label ();
  332. c.ID = wc.ClientID;
  333. c.CopyBaseAttributes (wc);
  334. c.ApplyStyle (s);
  335. c.Enabled = wc.Enabled;
  336. c.RenderBeginTag (w);
  337. }
  338. bool outer_table_implied;
  339. public bool OuterTableImplied {
  340. get {
  341. return outer_table_implied;
  342. }
  343. set {
  344. outer_table_implied = value;
  345. }
  346. }
  347. int repeat_cols;
  348. public int RepeatColumns {
  349. get {
  350. return repeat_cols;
  351. }
  352. set {
  353. repeat_cols = value;
  354. }
  355. }
  356. RepeatDirection dir = RepeatDirection.Vertical;
  357. public RepeatDirection RepeatDirection {
  358. get {
  359. return dir;
  360. }
  361. set {
  362. if (value != RepeatDirection.Horizontal &&
  363. value != RepeatDirection.Vertical)
  364. throw new ArgumentOutOfRangeException ();
  365. dir = value;
  366. }
  367. }
  368. RepeatLayout layout;
  369. public RepeatLayout RepeatLayout {
  370. get {
  371. return layout;
  372. }
  373. set {
  374. if (value != RepeatLayout.Flow &&
  375. value != RepeatLayout.Table)
  376. throw new ArgumentOutOfRangeException ();
  377. layout = value;
  378. }
  379. }
  380. [Conditional ("DEBUG_REPEAT_INFO")]
  381. internal void PrintValues (IRepeatInfoUser riu)
  382. {
  383. string s = String.Format ("Layout {0}; Direction {1}; Cols {2}; OuterTableImplied {3}\n" +
  384. "User: itms {4}, hdr {5}; ftr {6}; sep {7}", RepeatLayout, RepeatDirection,
  385. RepeatColumns, OuterTableImplied, riu.RepeatedItemCount, riu.HasSeparators, riu.HasHeader,
  386. riu.HasFooter, riu.HasSeparators
  387. );
  388. Console.WriteLine (s);
  389. if (HttpContext.Current != null)
  390. HttpContext.Current.Trace.Write (s);
  391. }
  392. private string caption = "";
  393. private TableCaptionAlign captionAlign = TableCaptionAlign.NotSet;
  394. private bool useAccessibleHeader = false;
  395. [WebSysDescription ("")]
  396. [WebCategory ("Accessibility")]
  397. public string Caption {
  398. get {return caption;}
  399. set { caption = value; }
  400. }
  401. [WebSysDescription ("")]
  402. [DefaultValue (TableCaptionAlign.NotSet)]
  403. [WebCategory ("Accessibility")]
  404. public TableCaptionAlign CaptionAlign {
  405. get {return captionAlign;}
  406. set { captionAlign = value; }
  407. }
  408. [WebSysDescription ("")]
  409. [DefaultValue (false)]
  410. [WebCategory ("Accessibility")]
  411. public bool UseAccessibleHeader {
  412. get {return useAccessibleHeader;}
  413. set { useAccessibleHeader = value; }
  414. }
  415. }
  416. }