RepeatInfo.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Namespace: System.Web.UI.WebControls
  23. * Class: RepeatInfo
  24. *
  25. * Author: Gaurav Vaish
  26. * Maintainer: [email protected]
  27. * Contact: <[email protected]>, <[email protected]>
  28. * Implementation: yes
  29. * Status: 100%
  30. *
  31. * (C) Gaurav Vaish (2002)
  32. */
  33. using System;
  34. using System.Globalization;
  35. using System.Web;
  36. using System.Web.UI;
  37. namespace System.Web.UI.WebControls
  38. {
  39. public sealed class RepeatInfo
  40. {
  41. private bool outerTableImp;
  42. private int repeatColumns;
  43. private RepeatDirection repeatDirection;
  44. private RepeatLayout repeatLayout;
  45. public RepeatInfo()
  46. {
  47. outerTableImp = false;
  48. repeatColumns = 0;
  49. repeatDirection = RepeatDirection.Vertical;
  50. repeatLayout = RepeatLayout.Table;
  51. }
  52. public bool OuterTableImplied
  53. {
  54. get
  55. {
  56. return outerTableImp;
  57. }
  58. set
  59. {
  60. outerTableImp = value;
  61. }
  62. }
  63. public int RepeatColumns
  64. {
  65. get
  66. {
  67. return repeatColumns;
  68. }
  69. set
  70. {
  71. repeatColumns = value;
  72. }
  73. }
  74. public RepeatDirection RepeatDirection
  75. {
  76. get
  77. {
  78. return repeatDirection;
  79. }
  80. set
  81. {
  82. if(!Enum.IsDefined(typeof(RepeatDirection), value))
  83. throw new ArgumentException();
  84. repeatDirection = value;
  85. }
  86. }
  87. public RepeatLayout RepeatLayout
  88. {
  89. get
  90. {
  91. return repeatLayout;
  92. }
  93. set
  94. {
  95. if(!Enum.IsDefined(typeof(RepeatLayout), value))
  96. throw new ArgumentException();
  97. repeatLayout = value;
  98. }
  99. }
  100. public void RenderRepeater(HtmlTextWriter writer, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
  101. {
  102. if(RepeatDirection == RepeatDirection.Vertical)
  103. {
  104. DoVerticalRendering(writer, user, controlStyle, baseControl);
  105. } else
  106. {
  107. DoHorizontalRendering(writer, user, controlStyle, baseControl);
  108. }
  109. }
  110. private void DoVerticalRendering(HtmlTextWriter writer, IRepeatInfoUser user, Style controlStyle, WebControl baseControl)
  111. {
  112. int total = user.RepeatedItemCount;
  113. int colsCount;
  114. int rowsCount;
  115. if(repeatColumns == 0 || repeatColumns==1)
  116. {
  117. colsCount = 1;
  118. rowsCount = total;
  119. } else
  120. {
  121. colsCount = repeatColumns;
  122. rowsCount = (total + repeatColumns - 1) / repeatColumns;
  123. if(rowsCount ==0 && total != 0)
  124. {
  125. rowsCount = 1;
  126. colsCount = total;
  127. }
  128. }
  129. WebControl ctrl = null;
  130. bool isTable = false;
  131. bool hasSeps = user.HasSeparators;
  132. if(!outerTableImp)
  133. {
  134. if(RepeatLayout == RepeatLayout.Table)
  135. {
  136. ctrl = new Table();
  137. isTable = true;
  138. } else
  139. {
  140. ctrl = new WebControl(HtmlTextWriterTag.Span);
  141. }
  142. }
  143. if(ctrl != null)
  144. {
  145. ctrl.ID = baseControl.ClientID;
  146. ctrl.CopyBaseAttributes(baseControl);
  147. ctrl.ApplyStyle(controlStyle);
  148. ctrl.RenderBeginTag(writer);
  149. }
  150. Style itemStyle;
  151. int colSpan = 0;
  152. if(user.HasHeader)
  153. {
  154. if(isTable)
  155. {
  156. writer.RenderBeginTag(HtmlTextWriterTag.Tr);
  157. if(colsCount != 1)
  158. {
  159. colSpan = colsCount;
  160. if(hasSeps)
  161. colSpan += colsCount;
  162. writer.AddAttribute(HtmlTextWriterAttribute.Colspan, colSpan.ToString(NumberFormatInfo.InvariantInfo));
  163. }
  164. itemStyle = user.GetItemStyle(ListItemType.Header, -1);
  165. if(itemStyle != null)
  166. {
  167. itemStyle.AddAttributesToRender(writer);
  168. }
  169. writer.RenderBeginTag(HtmlTextWriterTag.Td);
  170. }
  171. user.RenderItem(ListItemType.Header, -1, this, writer);
  172. if(isTable)
  173. {
  174. writer.RenderEndTag();
  175. writer.RenderEndTag();
  176. } else
  177. {
  178. if(!outerTableImp)
  179. {
  180. writer.WriteFullBeginTag("br");
  181. }
  182. }
  183. }
  184. int rowIndex = 0;
  185. int colIndex = 0;
  186. int index = 0;
  187. int diff = colsCount - (rowsCount*colsCount - total);
  188. while(rowIndex < rowsCount)
  189. {
  190. if(isTable)
  191. writer.RenderBeginTag(HtmlTextWriterTag.Tr);
  192. colIndex = 0;
  193. while(colIndex < colsCount)
  194. {
  195. if (rowIndex == rowsCount-1 && colIndex >= diff)
  196. break;
  197. if (colIndex < diff)
  198. index = rowIndex + colIndex * rowsCount;
  199. else
  200. index = rowIndex + colIndex * (rowsCount-1) + diff;
  201. if(index < total)
  202. {
  203. if(isTable)
  204. {
  205. itemStyle = user.GetItemStyle(ListItemType.Item, index);
  206. if(itemStyle != null)
  207. {
  208. itemStyle.AddAttributesToRender(writer);
  209. }
  210. writer.RenderBeginTag(HtmlTextWriterTag.Td);
  211. }
  212. user.RenderItem(ListItemType.Item, index, this, writer);
  213. if(isTable)
  214. writer.RenderEndTag();
  215. if(hasSeps && index != (total - 1))
  216. {
  217. if(colsCount == 1)
  218. {
  219. writer.RenderEndTag();
  220. writer.RenderBeginTag(HtmlTextWriterTag.Tr);
  221. } else
  222. {
  223. writer.WriteFullBeginTag("br");
  224. }
  225. if(isTable)
  226. {
  227. itemStyle = user.GetItemStyle(ListItemType.Separator, index);
  228. if(itemStyle != null)
  229. itemStyle.AddAttributesToRender(writer);
  230. writer.RenderBeginTag(HtmlTextWriterTag.Td);
  231. }
  232. user.RenderItem(ListItemType.Separator, index, this, writer);
  233. if(isTable)
  234. writer.RenderEndTag();
  235. }
  236. }
  237. colIndex++;
  238. }
  239. if(isTable)
  240. writer.RenderEndTag();
  241. else
  242. if((rowIndex != (rowsCount - 1) || user.HasFooter) && !outerTableImp)
  243. writer.WriteFullBeginTag("br");
  244. rowIndex++;
  245. }
  246. if(user.HasFooter)
  247. {
  248. if(isTable)
  249. {
  250. writer.RenderBeginTag(HtmlTextWriterTag.Tr);
  251. if(colsCount != 1)
  252. {
  253. writer.AddAttribute(HtmlTextWriterAttribute.Colspan, colSpan.ToString(NumberFormatInfo.InvariantInfo));
  254. }
  255. itemStyle = user.GetItemStyle(ListItemType.Footer, -1);
  256. if(itemStyle != null)
  257. {
  258. itemStyle.AddAttributesToRender(writer);
  259. }
  260. writer.RenderBeginTag(HtmlTextWriterTag.Td);
  261. }
  262. user.RenderItem(ListItemType.Footer, -1, this, writer);
  263. if(isTable)
  264. {
  265. writer.RenderEndTag();
  266. writer.RenderEndTag();
  267. }
  268. }
  269. if(ctrl != null)
  270. {
  271. ctrl.RenderEndTag(writer);
  272. }
  273. }
  274. private void DoHorizontalRendering (HtmlTextWriter writer,
  275. IRepeatInfoUser user,
  276. Style controlStyle,
  277. WebControl baseControl)
  278. {
  279. /* Based on DoVerticalRendering */
  280. int total = user.RepeatedItemCount;
  281. int colsCount = 0;
  282. int rowsCount = 0;
  283. WebControl ctrl = null;
  284. bool isTable = true;
  285. bool hasSeps = user.HasSeparators;
  286. if (!outerTableImp){
  287. isTable = (RepeatLayout == RepeatLayout.Table);
  288. ctrl = (isTable) ? new Table () : new WebControl (HtmlTextWriterTag.Span);
  289. ctrl.ID = baseControl.ClientID;
  290. ctrl.CopyBaseAttributes (baseControl);
  291. ctrl.ApplyStyle (controlStyle);
  292. ctrl.RenderBeginTag (writer);
  293. }
  294. Style itemStyle;
  295. int colSpan = 0;
  296. if (user.HasHeader){
  297. if (isTable){
  298. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  299. if (colsCount != 1){
  300. colSpan = colsCount;
  301. if (hasSeps)
  302. colSpan += colsCount;
  303. writer.AddAttribute (HtmlTextWriterAttribute.Colspan,
  304. colSpan.ToString (NumberFormatInfo.InvariantInfo));
  305. }
  306. itemStyle = user.GetItemStyle (ListItemType.Header, -1);
  307. if (itemStyle != null)
  308. itemStyle.AddAttributesToRender (writer);
  309. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  310. }
  311. user.RenderItem (ListItemType.Header, -1, this, writer);
  312. if (isTable){
  313. writer.RenderEndTag();
  314. writer.RenderEndTag();
  315. } else if (repeatColumns < user.RepeatedItemCount)
  316. writer.WriteFullBeginTag ("br");
  317. }
  318. for (int index = 0; index < total; index++){
  319. if (isTable && index == 0)
  320. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  321. if (isTable){
  322. itemStyle = user.GetItemStyle (ListItemType.Item, index);
  323. if (itemStyle != null)
  324. itemStyle.AddAttributesToRender(writer);
  325. writer.RenderBeginTag(HtmlTextWriterTag.Td);
  326. }
  327. user.RenderItem(ListItemType.Item, index, this, writer);
  328. if (isTable)
  329. writer.RenderEndTag ();
  330. if (hasSeps && index != (total - 1)){
  331. if (isTable){
  332. itemStyle = user.GetItemStyle (ListItemType.Separator, index);
  333. if (itemStyle != null)
  334. itemStyle.AddAttributesToRender (writer);
  335. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  336. }
  337. user.RenderItem (ListItemType.Separator, index, this, writer);
  338. if (isTable)
  339. writer.RenderEndTag ();
  340. }
  341. colsCount++;
  342. if (colsCount == repeatColumns) {
  343. if (isTable) {
  344. writer.RenderEndTag ();
  345. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  346. }
  347. else if (rowsCount < total)
  348. writer.WriteFullBeginTag ("br");
  349. colsCount = 0;
  350. }
  351. if (index == (total - 1)) {
  352. if (isTable)
  353. writer.RenderEndTag ();
  354. else if (rowsCount < total)
  355. writer.WriteFullBeginTag ("br");
  356. }
  357. }
  358. if (user.HasFooter){
  359. if (isTable){
  360. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  361. if (colsCount != 1)
  362. writer.AddAttribute (HtmlTextWriterAttribute.Colspan,
  363. colSpan.ToString(NumberFormatInfo.InvariantInfo));
  364. itemStyle = user.GetItemStyle (ListItemType.Footer, -1);
  365. if(itemStyle != null)
  366. itemStyle.AddAttributesToRender (writer);
  367. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  368. }
  369. user.RenderItem (ListItemType.Footer, -1, this, writer);
  370. if (isTable){
  371. writer.RenderEndTag ();
  372. writer.RenderEndTag ();
  373. }
  374. }
  375. if (ctrl != null)
  376. ctrl.RenderEndTag(writer);
  377. }
  378. }
  379. }