ItemPager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * Project : Mono
  23. * Namespace : System.Web.UI.MobileControls
  24. * Class : ItemPager
  25. * Author : Gaurav Vaish
  26. *
  27. * Copyright : 2003 with Gaurav Vaish, and with
  28. * Ximian Inc
  29. */
  30. using System.Web.Mobile;
  31. namespace System.Web.UI.MobileControls
  32. {
  33. public class ItemPager
  34. {
  35. private MobileControl control;
  36. private int firstPage;
  37. private int lastPage;
  38. private int firstPageItemCount;
  39. private int fullPageItemCount;
  40. private int lastPageItemCount;
  41. public ItemPager()
  42. {
  43. }
  44. public ItemPager(ControlPager pager, MobileControl control,
  45. int itemCount, int itemsPerPage, int itemWeight)
  46. {
  47. this.control = control;
  48. if(itemsPerPage > 0)
  49. {
  50. if(itemCount < itemsPerPage)
  51. {
  52. firstPageItemCount = itemCount;
  53. firstPage = pager.GetPage(itemWeight * itemCount);
  54. lastPage = firstPage;
  55. } else
  56. {
  57. int ppic = (itemCount - 1)/itemsPerPage + 1;
  58. firstPageItemCount = itemsPerPage;
  59. fullPageItemCount = itemsPerPage;
  60. lastPageItemCount = ppic - (ppic - 1)*itemsPerPage;
  61. firstPage = pager.GetPage(itemsPerPage * itemWeight);
  62. pager.PageCount += (ppic - 1);
  63. if(ppic > 1)
  64. {
  65. pager.RemainingWeight = pager.PageWeight
  66. - (itemsPerPage * itemWeight);
  67. lastPage = firstPage + ppic - 1;
  68. }
  69. }
  70. } else
  71. {
  72. int totalWt = itemWeight * itemCount;
  73. if(totalWt <= pager.RemainingWeight)
  74. {
  75. firstPageItemCount = itemCount;
  76. firstPage = pager.GetPage(totalWt);
  77. lastPage = firstPage;
  78. } else
  79. {
  80. firstPageItemCount = pager.RemainingWeight / itemWeight;
  81. int rem = itemCount - firstPageItemCount;
  82. fullPageItemCount = Math.Max(itemWeight, pager.PageWeight);
  83. int pages = rem / fullPageItemCount;
  84. lastPageItemCount = rem % fullPageItemCount;
  85. firstPage = pager.PageCount;
  86. pager.PageCount += 1;
  87. pager.RemainingWeight = pager.PageWeight;
  88. pager.PageCount += pages;
  89. pager.RemainingWeight -= lastPageItemCount * itemWeight;
  90. if(firstPageItemCount == 0)
  91. {
  92. firstPage += 1;
  93. firstPageItemCount = Math.Min(fullPageItemCount,
  94. itemCount);
  95. }
  96. if(lastPageItemCount == 0)
  97. {
  98. pager.PageCount -= 1;
  99. lastPageItemCount = Math.Min(fullPageItemCount,
  100. itemCount);
  101. pager.RemainingWeight = 0;
  102. }
  103. lastPage = pager.PageCount;
  104. }
  105. control.FirstPage = firstPage;
  106. control.LastPage = lastPage;
  107. }
  108. }
  109. public int ItemCount
  110. {
  111. get
  112. {
  113. return GetItemCount();
  114. }
  115. }
  116. public int ItemIndex
  117. {
  118. get
  119. {
  120. return GetItemIndex();
  121. }
  122. }
  123. private int GetItemCount()
  124. {
  125. int cp = control.Form.CurrentPage;
  126. int retVal;
  127. if(cp >= firstPage && cp <= lastPage)
  128. {
  129. if(cp == firstPage)
  130. retVal = firstPageItemCount;
  131. else if(cp == lastPage)
  132. retVal = lastPageItemCount;
  133. else
  134. retVal = fullPageItemCount;
  135. } else
  136. {
  137. retVal = -1;
  138. }
  139. return retVal;
  140. }
  141. private int GetItemIndex()
  142. {
  143. int cp = control.Form.CurrentPage;
  144. int retVal;
  145. if(cp >= firstPage && cp <= lastPage)
  146. {
  147. if(cp == firstPage)
  148. retVal = 0;
  149. else
  150. {
  151. retVal = (cp - firstPage - 1)* fullPageItemCount
  152. + firstPageItemCount;
  153. }
  154. } else
  155. {
  156. retVal = -1;
  157. }
  158. return retVal;
  159. }
  160. }
  161. }