Grid.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using FastReport.Utils;
  2. using System;
  3. using System.Drawing;
  4. namespace FastReport.Design.PageDesigners.Page
  5. {
  6. internal class Grid : GridBase
  7. {
  8. private PageUnits gridUnits;
  9. private bool dotted;
  10. private float snapSizeMillimeters;
  11. private float snapSizeCentimeters;
  12. private float snapSizeInches;
  13. private float snapSizeHundrethsOfInch;
  14. private float snapSize;
  15. private Bitmap gridBmp;
  16. public PageUnits GridUnits
  17. {
  18. get { return gridUnits; }
  19. set
  20. {
  21. gridUnits = value;
  22. UpdateGridSize();
  23. }
  24. }
  25. public bool Dotted
  26. {
  27. get { return dotted; }
  28. set { dotted = value; }
  29. }
  30. public override float SnapSize
  31. {
  32. get { return snapSize; }
  33. set
  34. {
  35. switch (GridUnits)
  36. {
  37. case PageUnits.Millimeters:
  38. SnapSizeMillimeters = value;
  39. break;
  40. case PageUnits.Centimeters:
  41. SnapSizeCentimeters = value;
  42. break;
  43. case PageUnits.Inches:
  44. SnapSizeInches = value;
  45. break;
  46. case PageUnits.HundrethsOfInch:
  47. SnapSizeHundrethsOfInch = value;
  48. break;
  49. }
  50. }
  51. }
  52. public float SnapSizeMillimeters
  53. {
  54. get { return snapSizeMillimeters; }
  55. set
  56. {
  57. snapSizeMillimeters = value;
  58. UpdateGridSize();
  59. }
  60. }
  61. public float SnapSizeCentimeters
  62. {
  63. get { return snapSizeCentimeters; }
  64. set
  65. {
  66. snapSizeCentimeters = value;
  67. UpdateGridSize();
  68. }
  69. }
  70. public float SnapSizeInches
  71. {
  72. get { return snapSizeInches; }
  73. set
  74. {
  75. snapSizeInches = value;
  76. UpdateGridSize();
  77. }
  78. }
  79. public float SnapSizeHundrethsOfInch
  80. {
  81. get { return snapSizeHundrethsOfInch; }
  82. set
  83. {
  84. snapSizeHundrethsOfInch = value;
  85. UpdateGridSize();
  86. }
  87. }
  88. private void UpdateGridSize()
  89. {
  90. switch (gridUnits)
  91. {
  92. case PageUnits.Millimeters:
  93. snapSize = snapSizeMillimeters * Units.Millimeters;
  94. break;
  95. case PageUnits.Centimeters:
  96. snapSize = snapSizeCentimeters * Units.Centimeters;
  97. break;
  98. case PageUnits.Inches:
  99. snapSize = snapSizeInches * Units.Inches;
  100. break;
  101. case PageUnits.HundrethsOfInch:
  102. snapSize = snapSizeHundrethsOfInch * Units.HundrethsOfInch;
  103. break;
  104. }
  105. ResetGridBmp();
  106. }
  107. private void ResetGridBmp()
  108. {
  109. gridBmp = new Bitmap(1, 1);
  110. }
  111. private void DrawLinesGrid(Graphics g, RectangleF visibleArea, float scale)
  112. {
  113. Pen linePen;
  114. Pen pen5 = new Pen(Color.FromArgb(255, 0xF8, 0xF8, 0xF8));
  115. Pen pen10 = new Pen(Color.FromArgb(255, 0xE8, 0xE8, 0xE8));
  116. float dx = GridUnits == PageUnits.Millimeters || GridUnits == PageUnits.Centimeters ?
  117. Units.Millimeters * scale : Units.TenthsOfInch * scale;
  118. float i = visibleArea.Left;
  119. int i1 = 0;
  120. while (i < visibleArea.Right)
  121. {
  122. if (i1 % 10 == 0)
  123. linePen = pen10;
  124. else if (i1 % 5 == 0)
  125. linePen = pen5;
  126. else
  127. linePen = null;
  128. if (linePen != null)
  129. g.DrawLine(linePen, i, visibleArea.Top, i, visibleArea.Bottom);
  130. i += dx;
  131. i1++;
  132. }
  133. i = visibleArea.Top;
  134. i1 = 0;
  135. while (i < visibleArea.Bottom)
  136. {
  137. if (i1 % 10 == 0)
  138. linePen = pen10;
  139. else if (i1 % 5 == 0)
  140. linePen = pen5;
  141. else
  142. linePen = null;
  143. if (linePen != null)
  144. g.DrawLine(linePen, visibleArea.Left, i, visibleArea.Right, i);
  145. i += dx;
  146. i1++;
  147. }
  148. pen5.Dispose();
  149. pen10.Dispose();
  150. }
  151. private void DrawDotGrid(Graphics g, RectangleF visibleArea, float scale)
  152. {
  153. float dx = snapSize * scale;
  154. float dy = dx;
  155. if (visibleArea.Width > 0 && visibleArea.Height > 0 && dx > 2 && dy > 2)
  156. {
  157. float i = 0;
  158. if (gridBmp.Width != (int)visibleArea.Width)
  159. {
  160. gridBmp = new Bitmap((int)visibleArea.Width, 1);
  161. // draw points on one line
  162. i = 0;
  163. while (i < (int)visibleArea.Width - 1)
  164. {
  165. gridBmp.SetPixel((int)Math.Round(i), 0, Color.Silver);
  166. i += dx;
  167. }
  168. }
  169. // draw lines
  170. i = visibleArea.Top;
  171. while (i < visibleArea.Bottom - 1)
  172. {
  173. g.DrawImage(gridBmp, (int)Math.Round(visibleArea.Left), (int)Math.Round(i));
  174. i += dy;
  175. }
  176. }
  177. }
  178. public void Draw(Graphics g, RectangleF visibleArea, float scale)
  179. {
  180. if (dotted)
  181. DrawDotGrid(g, visibleArea, scale);
  182. else
  183. DrawLinesGrid(g, visibleArea, scale);
  184. }
  185. public void SaveState(StorageService storage)
  186. {
  187. storage.SetStr("Units", GridUnits.ToString());
  188. storage.SetFloat("SnapSizeMillimeters", SnapSizeMillimeters);
  189. storage.SetFloat("SnapSizeCentimeters", SnapSizeCentimeters);
  190. storage.SetFloat("SnapSizeInches", SnapSizeInches);
  191. storage.SetFloat("SnapSizeHundrethsOfInch", SnapSizeHundrethsOfInch);
  192. storage.SetBool("DottedGrid", Dotted);
  193. }
  194. public void RestoreState(StorageService storage)
  195. {
  196. GridUnits = storage.GetEnum("Units", GridUnits);
  197. SnapSizeMillimeters = storage.GetFloat("SnapSizeMillimeters", SnapSizeMillimeters);
  198. SnapSizeCentimeters = storage.GetFloat("SnapSizeCentimeters", SnapSizeCentimeters);
  199. SnapSizeInches = storage.GetFloat("SnapSizeInches", SnapSizeInches);
  200. SnapSizeHundrethsOfInch = storage.GetFloat("SnapSizeHundrethsOfInch", SnapSizeHundrethsOfInch);
  201. Dotted = storage.GetBool("DottedGrid");
  202. }
  203. public Grid()
  204. {
  205. gridUnits = PageUnits.Centimeters;
  206. dotted = true;
  207. snapSizeMillimeters = 2.5f;
  208. snapSizeCentimeters = 0.25f;
  209. snapSizeInches = 0.1f;
  210. snapSizeHundrethsOfInch = 10f;
  211. UpdateGridSize();
  212. }
  213. }
  214. }