RepeatInfo.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 (Caption.Length > 0) {
  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 (), false);
  92. if (UseAccessibleHeader)
  93. w.AddAttribute ("scope", "col", false);
  94. Style s = user.GetItemStyle (ListItemType.Header, -1);
  95. if (s != null)
  96. s.AddAttributesToRender (w);
  97. if (UseAccessibleHeader)
  98. w.RenderBeginTag (HtmlTextWriterTag.Th);
  99. else
  100. w.RenderBeginTag (HtmlTextWriterTag.Td);
  101. user.RenderItem (ListItemType.Header, -1, this, w);
  102. w.RenderEndTag (); // td
  103. w.RenderEndTag (); // tr
  104. } else {
  105. user.RenderItem (ListItemType.Header, -1, this, w);
  106. RenderBr (w);
  107. }
  108. }
  109. for (int r = 0; r < rows; r ++) {
  110. if (table)
  111. w.RenderBeginTag (HtmlTextWriterTag.Tr);
  112. for (int c = 0; c < cols; c ++) {
  113. // Find the item number we are in according to the repeat
  114. // direction.
  115. int item = index_vert (rows, cols, r, c, itms);
  116. // This item is blank because there there not enough items
  117. // to make a full row.
  118. if (!show_empty_trailing_items && item >= itms)
  119. continue;
  120. if (table) {
  121. Style s = null;
  122. if (item < itms)
  123. s = user.GetItemStyle (ListItemType.Item, item);
  124. if (s != null)
  125. s.AddAttributesToRender (w);
  126. w.RenderBeginTag (HtmlTextWriterTag.Td);
  127. }
  128. if (item < itms)
  129. user.RenderItem (ListItemType.Item, item, this, w);
  130. if (table)
  131. w.RenderEndTag (); // td
  132. if (sep && cols != 1) {
  133. if (table) {
  134. if (item < itms - 1) {
  135. Style s = user.GetItemStyle (ListItemType.Separator, item);
  136. if (s != null)
  137. s.AddAttributesToRender (w);
  138. }
  139. if (item < itms - 1 || show_empty_trailing_sep)
  140. w.RenderBeginTag (HtmlTextWriterTag.Td);
  141. }
  142. if (item < itms - 1)
  143. user.RenderItem (ListItemType.Separator, item, this, w);
  144. if (table && (item < itms - 1 || show_empty_trailing_sep))
  145. w.RenderEndTag (); // td
  146. }
  147. }
  148. if (oti) {
  149. } else if (table) {
  150. w.RenderEndTag (); // tr
  151. } else if (r != rows - 1) {
  152. RenderBr(w);
  153. }
  154. if (sep && r != rows - 1 /* no sep on last item */ && cols == 1) {
  155. if (table) {
  156. w.RenderBeginTag (HtmlTextWriterTag.Tr);
  157. Style s = user.GetItemStyle (ListItemType.Separator, r);
  158. if (s != null)
  159. s.AddAttributesToRender (w);
  160. w.RenderBeginTag (HtmlTextWriterTag.Td);
  161. } else if (oti) {
  162. #if !NET_2_0
  163. /* 2.0 doesn't render this <br /> */
  164. RenderBr (w);
  165. #endif
  166. }
  167. user.RenderItem (ListItemType.Separator, r, this, w);
  168. if (table) {
  169. w.RenderEndTag (); // td
  170. w.RenderEndTag (); // tr
  171. } else if (!oti) {
  172. RenderBr (w);
  173. }
  174. }
  175. }
  176. // Render the footer
  177. if (user.HasFooter) {
  178. if (oti)
  179. user.RenderItem (ListItemType.Footer, -1, this, w);
  180. else if (table) {
  181. w.RenderBeginTag (HtmlTextWriterTag.Tr);
  182. if (hdr_span != 1)
  183. w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString (), false);
  184. Style s = user.GetItemStyle (ListItemType.Footer, -1);
  185. if (s != null)
  186. s.AddAttributesToRender (w);
  187. w.RenderBeginTag (HtmlTextWriterTag.Td);
  188. user.RenderItem (ListItemType.Footer, -1, this, w);
  189. w.RenderEndTag (); // td
  190. w.RenderEndTag (); // tr
  191. } else {
  192. // avoid dups on 0 items
  193. if (itms != 0)
  194. RenderBr (w);
  195. user.RenderItem (ListItemType.Footer, -1, this, w);
  196. }
  197. }
  198. if (! oti)
  199. w.RenderEndTag (); // table/span
  200. }
  201. void RenderHoriz (HtmlTextWriter w, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
  202. {
  203. int itms = user.RepeatedItemCount;
  204. // total number of rows/columns in our table
  205. int cols = RepeatColumns == 0 ? itms : RepeatColumns;
  206. // this gets ceil (itms / cols)
  207. int rows = cols == 0 ? 0 : (itms + cols - 1) / cols;
  208. bool sep = user.HasSeparators;
  209. //bool oti = OuterTableImplied;
  210. int hdr_span = cols * (sep ? 2 : 1);
  211. bool table = RepeatLayout == RepeatLayout.Table;
  212. #if NET_2_0
  213. bool show_empty_trailing_items = true;
  214. bool show_empty_trailing_sep = true;
  215. #else
  216. bool show_empty_trailing_items = false;
  217. bool show_empty_trailing_sep = false;
  218. #endif
  219. RenderBeginTag (w, controlStyle, baseControl);
  220. if (Caption.Length > 0) {
  221. if (CaptionAlign != TableCaptionAlign.NotSet)
  222. w.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString());
  223. w.RenderBeginTag (HtmlTextWriterTag.Caption);
  224. w.Write (Caption);
  225. w.RenderEndTag ();
  226. }
  227. // Render the header
  228. if (user.HasHeader) {
  229. if (table) {
  230. w.RenderBeginTag (HtmlTextWriterTag.Tr);
  231. // Make sure the header takes up the full width. We have two
  232. // columns per item if we are using separators, otherwise
  233. // one per item.
  234. if (hdr_span != 1)
  235. w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString (), false);
  236. if (UseAccessibleHeader)
  237. w.AddAttribute ("scope", "col", false);
  238. Style s = user.GetItemStyle (ListItemType.Header, -1);
  239. if (s != null)
  240. s.AddAttributesToRender (w);
  241. if (UseAccessibleHeader)
  242. w.RenderBeginTag (HtmlTextWriterTag.Th);
  243. else
  244. w.RenderBeginTag (HtmlTextWriterTag.Td);
  245. user.RenderItem (ListItemType.Header, -1, this, w);
  246. w.RenderEndTag (); // td
  247. w.RenderEndTag (); // tr
  248. } else {
  249. user.RenderItem (ListItemType.Header, -1, this, w);
  250. if (!table && RepeatColumns != 0 && itms != 0)
  251. RenderBr (w);
  252. }
  253. }
  254. for (int r = 0; r < rows; r ++) {
  255. if (table)
  256. w.RenderBeginTag (HtmlTextWriterTag.Tr);
  257. for (int c = 0; c < cols; c ++) {
  258. // Find the item number we are in according to the repeat
  259. // direction.
  260. int item = r * cols + c;
  261. // This item is blank because there there not enough items
  262. // to make a full row.
  263. if (!show_empty_trailing_items && item >= itms)
  264. continue;
  265. if (table) {
  266. Style s = null;
  267. if (item < itms)
  268. s = user.GetItemStyle (ListItemType.Item, item);
  269. if (s != null)
  270. s.AddAttributesToRender (w);
  271. w.RenderBeginTag (HtmlTextWriterTag.Td);
  272. }
  273. if (item < itms)
  274. user.RenderItem (ListItemType.Item, item, this, w);
  275. if (table)
  276. w.RenderEndTag (); // td
  277. if (sep) {
  278. if (table) {
  279. if (item < itms - 1) {
  280. Style s = user.GetItemStyle (ListItemType.Separator, item);
  281. if (s != null)
  282. s.AddAttributesToRender (w);
  283. }
  284. if (item < itms - 1 || show_empty_trailing_sep)
  285. w.RenderBeginTag (HtmlTextWriterTag.Td);
  286. }
  287. if (item < itms - 1)
  288. user.RenderItem (ListItemType.Separator, item, this, w);
  289. if (table && (item < itms - 1 || show_empty_trailing_sep))
  290. w.RenderEndTag (); // td
  291. }
  292. }
  293. if (table) {
  294. // if (!oti)
  295. w.RenderEndTag (); // tr
  296. } else if (!(r == rows -1 && RepeatColumns == 0))
  297. RenderBr (w);
  298. }
  299. // Render the footer
  300. if (user.HasFooter) {
  301. if (table) {
  302. w.RenderBeginTag (HtmlTextWriterTag.Tr);
  303. if (hdr_span != 1)
  304. w.AddAttribute (HtmlTextWriterAttribute.Colspan, hdr_span.ToString (), false);
  305. Style s = user.GetItemStyle (ListItemType.Footer, -1);
  306. if (s != null)
  307. s.AddAttributesToRender (w);
  308. w.RenderBeginTag (HtmlTextWriterTag.Td);
  309. user.RenderItem (ListItemType.Footer, -1, this, w);
  310. w.RenderEndTag (); // td
  311. w.RenderEndTag (); // tr
  312. } else {
  313. user.RenderItem (ListItemType.Footer, -1, this, w);
  314. }
  315. }
  316. if (true)
  317. w.RenderEndTag (); // table/span
  318. }
  319. int index_vert (int rows, int cols, int r, int c, int items)
  320. {
  321. int last = items % cols;
  322. if (last == 0)
  323. last = cols;
  324. if (r == rows - 1 && c >= last)
  325. return items;
  326. int add;
  327. int v;
  328. if (c > last){
  329. add = last * rows + (c-last) * (rows-1);
  330. v = add + r;
  331. } else
  332. v = rows * c + r;
  333. return v;
  334. }
  335. void RenderBeginTag (HtmlTextWriter w, Style s, WebControl wc)
  336. {
  337. WebControl c;
  338. if (RepeatLayout == RepeatLayout.Table)
  339. c = new Table ();
  340. else
  341. c = new Label ();
  342. c.ID = wc.ClientID;
  343. c.CopyBaseAttributes (wc);
  344. c.ApplyStyle (s);
  345. c.Enabled = wc.IsEnabled;
  346. c.RenderBeginTag (w);
  347. }
  348. bool outer_table_implied;
  349. public bool OuterTableImplied {
  350. get {
  351. return outer_table_implied;
  352. }
  353. set {
  354. outer_table_implied = value;
  355. }
  356. }
  357. int repeat_cols;
  358. public int RepeatColumns {
  359. get {
  360. return repeat_cols;
  361. }
  362. set {
  363. repeat_cols = value;
  364. }
  365. }
  366. RepeatDirection dir = RepeatDirection.Vertical;
  367. public RepeatDirection RepeatDirection {
  368. get {
  369. return dir;
  370. }
  371. set {
  372. if (value != RepeatDirection.Horizontal &&
  373. value != RepeatDirection.Vertical)
  374. throw new ArgumentOutOfRangeException ();
  375. dir = value;
  376. }
  377. }
  378. RepeatLayout layout;
  379. public RepeatLayout RepeatLayout {
  380. get {
  381. return layout;
  382. }
  383. set {
  384. if (value != RepeatLayout.Flow &&
  385. value != RepeatLayout.Table)
  386. throw new ArgumentOutOfRangeException ();
  387. layout = value;
  388. }
  389. }
  390. [Conditional ("DEBUG_REPEAT_INFO")]
  391. internal void PrintValues (IRepeatInfoUser riu)
  392. {
  393. string s = String.Format ("Layout {0}; Direction {1}; Cols {2}; OuterTableImplied {3}\n" +
  394. "User: itms {4}, hdr {5}; ftr {6}; sep {7}", RepeatLayout, RepeatDirection,
  395. RepeatColumns, OuterTableImplied, riu.RepeatedItemCount, riu.HasSeparators, riu.HasHeader,
  396. riu.HasFooter, riu.HasSeparators
  397. );
  398. Console.WriteLine (s);
  399. if (HttpContext.Current != null)
  400. HttpContext.Current.Trace.Write (s);
  401. }
  402. string caption = String.Empty;
  403. TableCaptionAlign captionAlign = TableCaptionAlign.NotSet;
  404. bool useAccessibleHeader = false;
  405. [WebSysDescription ("")]
  406. [WebCategory ("Accessibility")]
  407. public string Caption {
  408. get {return caption;}
  409. set { caption = value; }
  410. }
  411. [WebSysDescription ("")]
  412. [WebCategory ("Accessibility")]
  413. public TableCaptionAlign CaptionAlign {
  414. get {return captionAlign;}
  415. set { captionAlign = value; }
  416. }
  417. [WebSysDescription ("")]
  418. [WebCategory ("Accessibility")]
  419. public bool UseAccessibleHeader {
  420. get {return useAccessibleHeader;}
  421. set { useAccessibleHeader = value; }
  422. }
  423. }
  424. }