CustomLabelForm.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Printing;
  4. using FastReport.Utils;
  5. using FastReport.TypeConverters;
  6. using FastReport.Design.PageDesigners.Page;
  7. namespace FastReport.Forms
  8. {
  9. internal partial class CustomLabelForm : BaseDialogForm
  10. {
  11. private XmlItem labelParameters;
  12. private PrinterSettings printerSettings;
  13. private Report sampleReport;
  14. private bool updating;
  15. public XmlItem LabelParameters
  16. {
  17. get { return labelParameters; }
  18. }
  19. private string ConvertMMtoUnits(float value)
  20. {
  21. return Converter.ToString(value, typeof(PaperConverter));
  22. }
  23. private float ConvertUnitsToInches(string value)
  24. {
  25. float floatValue = Converter.StringToFloat(value, true);
  26. switch (ReportWorkspace.Grid.GridUnits)
  27. {
  28. case PageUnits.Centimeters:
  29. floatValue /= 2.54f;
  30. break;
  31. case PageUnits.HundrethsOfInch:
  32. floatValue /= 100;
  33. break;
  34. case PageUnits.Inches:
  35. // do nothing
  36. break;
  37. case PageUnits.Millimeters:
  38. floatValue /= 25.4f;
  39. break;
  40. }
  41. return floatValue;
  42. }
  43. private string ConvertToXml(float value)
  44. {
  45. return Converter.ToString(Math.Round(value, 3));
  46. }
  47. private void UpdateSample()
  48. {
  49. float paperWidth = ConvertUnitsToInches(tbPaperWidth.Text);
  50. float paperHeight = ConvertUnitsToInches(tbPaperHeight.Text);
  51. float leftMargin = ConvertUnitsToInches(tbLeftMargin.Text);
  52. float topMargin = ConvertUnitsToInches(tbTopMargin.Text);
  53. float labelWidth = ConvertUnitsToInches(tbLabelWidth.Text);
  54. float labelHeight = ConvertUnitsToInches(tbLabelHeight.Text);
  55. int rows = (int)udRows.Value;
  56. int columns = (int)udColumns.Value;
  57. float rowGap = ConvertUnitsToInches(tbRowGap.Text);
  58. float columnGap = ConvertUnitsToInches(tbColumnGap.Text);
  59. labelParameters.Name = "";
  60. labelParameters.ClearProps();
  61. labelParameters.SetProp("Name", tbName.Text);
  62. labelParameters.SetProp("Width", ConvertToXml(labelWidth));
  63. labelParameters.SetProp("Height", ConvertToXml(labelHeight));
  64. labelParameters.SetProp("PaperWidth", ConvertToXml(paperWidth));
  65. labelParameters.SetProp("PaperHeight", ConvertToXml(paperHeight));
  66. if (cbLandscape.Checked)
  67. labelParameters.SetProp("Landscape", "true");
  68. labelParameters.SetProp("LeftMargin", ConvertToXml(leftMargin));
  69. labelParameters.SetProp("TopMargin", ConvertToXml(topMargin));
  70. labelParameters.SetProp("Rows", rows.ToString());
  71. labelParameters.SetProp("Columns", columns.ToString());
  72. labelParameters.SetProp("RowGap", ConvertToXml(rowGap));
  73. labelParameters.SetProp("ColumnGap", ConvertToXml(columnGap));
  74. ReportPage page = sampleReport.Pages[0] as ReportPage;
  75. page.Clear();
  76. page.Landscape = cbLandscape.Checked;
  77. page.PaperWidth = paperWidth * 25.4f;
  78. page.PaperHeight = paperHeight * 25.4f;
  79. page.LeftMargin = leftMargin * 25.4f;
  80. page.TopMargin = topMargin * 25.4f;
  81. DataBand band = new DataBand();
  82. band.Parent = page;
  83. bool fit = true;
  84. for (int x = 0; x < columns; x++)
  85. {
  86. for (int y = 0; y < rows; y++)
  87. {
  88. ShapeObject shape = new ShapeObject();
  89. shape.Parent = band;
  90. shape.Shape = ShapeKind.RoundRectangle;
  91. shape.Border.Color = Color.Gray;
  92. shape.Bounds = new RectangleF(x * (labelWidth + columnGap) * 96,
  93. y * (labelHeight + rowGap) * 96,
  94. labelWidth * 96,
  95. labelHeight * 96);
  96. if (shape.Right / Units.Millimeters > page.PaperWidth - page.LeftMargin + 0.1f ||
  97. shape.Bottom / Units.Millimeters > page.PaperHeight - page.TopMargin + 0.1f)
  98. {
  99. (shape.Fill as SolidFill).Color = Color.Red;
  100. fit = false;
  101. }
  102. }
  103. }
  104. rcSample.Report = sampleReport;
  105. lblWarning.Visible = !fit;
  106. btnOk.Enabled = fit;
  107. }
  108. private void cbxPaper_SelectedIndexChanged(object sender, EventArgs e)
  109. {
  110. if (updating)
  111. return;
  112. foreach (PaperSize ps in printerSettings.PaperSizes)
  113. {
  114. if (ps.PaperName == (string)cbxPaper.SelectedItem)
  115. {
  116. float psWidth = ps.Width / 100f * 25.4f;
  117. float psHeight = ps.Height / 100f * 25.4f;
  118. if (ps.Kind == PaperKind.A4)
  119. {
  120. psWidth = 210;
  121. psHeight = 297;
  122. }
  123. if (cbLandscape.Checked)
  124. {
  125. float ex = psWidth;
  126. psWidth = psHeight;
  127. psHeight = ex;
  128. }
  129. updating = true;
  130. tbPaperWidth.Text = ConvertMMtoUnits(psWidth);
  131. tbPaperHeight.Text = ConvertMMtoUnits(psHeight);
  132. updating = false;
  133. break;
  134. }
  135. }
  136. UpdateSample();
  137. }
  138. private void cbLandscape_CheckedChanged(object sender, EventArgs e)
  139. {
  140. if (updating)
  141. return;
  142. string w = tbPaperWidth.Text;
  143. string h = tbPaperHeight.Text;
  144. // avoid reset papersize to custom
  145. updating = true;
  146. tbPaperWidth.Text = h;
  147. tbPaperHeight.Text = w;
  148. updating = false;
  149. UpdateSample();
  150. }
  151. private void tbPaperWidth_TextChanged(object sender, EventArgs e)
  152. {
  153. if (updating)
  154. return;
  155. cbxPaper.SelectedIndex = 0;
  156. UpdateSample();
  157. }
  158. private void tbLeftMargin_TextChanged(object sender, EventArgs e)
  159. {
  160. if (updating)
  161. return;
  162. UpdateSample();
  163. }
  164. public void Init(string suggestedName)
  165. {
  166. labelParameters = new XmlItem();
  167. printerSettings = new PrinterSettings();
  168. sampleReport = new Report();
  169. sampleReport.Pages.Add(new ReportPage());
  170. sampleReport.SmoothGraphics = true;
  171. tbName.Text = suggestedName;
  172. // paper
  173. PaperSize defaultPaper = printerSettings.DefaultPageSettings.PaperSize;
  174. cbxPaper.Items.Add(Res.Get("Forms,PageSetup,Custom"));
  175. foreach (PaperSize ps in printerSettings.PaperSizes)
  176. {
  177. cbxPaper.Items.Add(ps.PaperName);
  178. if (ps.PaperName == defaultPaper.PaperName)
  179. cbxPaper.SelectedIndex = cbxPaper.Items.Count - 1;
  180. }
  181. tbLeftMargin.Text = ConvertMMtoUnits(0);
  182. tbTopMargin.Text = ConvertMMtoUnits(0);
  183. // label
  184. tbLabelWidth.Text = ConvertMMtoUnits(defaultPaper.Width / 100f * 25.4f / 2);
  185. tbLabelHeight.Text = ConvertMMtoUnits(defaultPaper.Height / 100f * 25.4f / 2);
  186. udRows.Value = 2;
  187. udColumns.Value = 2;
  188. tbRowGap.Text = ConvertMMtoUnits(0);
  189. tbColumnGap.Text = ConvertMMtoUnits(0);
  190. }
  191. public override void Localize()
  192. {
  193. base.Localize();
  194. MyRes res = new MyRes("Forms,CustomLabel");
  195. Text = res.Get("");
  196. lblName.Text = res.Get("Name");
  197. gbPaper.Text = res.Get("Paper");
  198. cbLandscape.Text = res.Get("Landscape");
  199. lblPaperWidth.Text = res.Get("PaperWidth");
  200. lblPaperHeight.Text = res.Get("PaperHeight");
  201. lblLeftMargin.Text = res.Get("LeftMargin");
  202. lblTopMargin.Text = res.Get("TopMargin");
  203. gbLabel.Text = res.Get("Label");
  204. lblLabelWidth.Text = res.Get("LabelWidth");
  205. lblLabelHeight.Text = res.Get("LabelHeight");
  206. lblRows.Text = Res.Get("Forms,LabelWizard,Rows");
  207. lblColumns.Text = Res.Get("Forms,LabelWizard,Columns");
  208. lblRowGap.Text = res.Get("RowGap");
  209. lblColumnGap.Text = res.Get("ColumnGap");
  210. gbSample.Text = Res.Get("Misc,Sample");
  211. lblWarning.Text = res.Get("Warning");
  212. }
  213. public CustomLabelForm()
  214. {
  215. InitializeComponent();
  216. Localize();
  217. UIUtils.CheckRTL(this);
  218. }
  219. }
  220. }