FillEditorForm.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using FastReport.Utils;
  5. using System.Drawing.Drawing2D;
  6. using System.IO;
  7. namespace FastReport.Forms
  8. {
  9. internal partial class FillEditorForm : BaseDialogForm
  10. {
  11. private FillBase fill;
  12. private FillBase[] fills;
  13. private bool updating;
  14. private SolidFill SolidFill
  15. {
  16. get { return fills[0] as SolidFill; }
  17. set { fills[0] = value; }
  18. }
  19. private LinearGradientFill LinearGradientFill
  20. {
  21. get { return fills[1] as LinearGradientFill; }
  22. set { fills[1] = value; }
  23. }
  24. private PathGradientFill PathGradientFill
  25. {
  26. get { return fills[2] as PathGradientFill; }
  27. set { fills[2] = value; }
  28. }
  29. private HatchFill HatchFill
  30. {
  31. get { return fills[3] as HatchFill; }
  32. set { fills[3] = value; }
  33. }
  34. private GlassFill GlassFill
  35. {
  36. get { return fills[4] as GlassFill; }
  37. set { fills[4] = value; }
  38. }
  39. private TextureFill TextureFill
  40. {
  41. get { return fills[5] as TextureFill; }
  42. set { fills[5] = value; }
  43. }
  44. public FillBase Fill
  45. {
  46. get { return fill; }
  47. set
  48. {
  49. fill = value;
  50. UpdateControls();
  51. }
  52. }
  53. private void PopulateFills()
  54. {
  55. fills = new FillBase[] {
  56. new SolidFill(Color.Silver), new LinearGradientFill(), new PathGradientFill(),
  57. new HatchFill(), new GlassFill(), new TextureFill() };
  58. (fills[0] as SolidFill).Color = Color.Transparent;
  59. }
  60. private void UpdateControls()
  61. {
  62. if (fill is SolidFill)
  63. {
  64. SolidFill = fill as SolidFill;
  65. cbxSolidColor.Color = SolidFill.Color;
  66. pcPanels.ActivePageIndex = 0;
  67. }
  68. else if (fill is LinearGradientFill)
  69. {
  70. LinearGradientFill = fill as LinearGradientFill;
  71. cbxLinearStartColor.Color = LinearGradientFill.StartColor;
  72. cbxLinearEndColor.Color = LinearGradientFill.EndColor;
  73. trbLinearFocus.Value = (int)Math.Round(LinearGradientFill.Focus * 100);
  74. trbLinearContrast.Value = (int)Math.Round(LinearGradientFill.Contrast * 100);
  75. acLinearAngle.Angle = LinearGradientFill.Angle;
  76. pcPanels.ActivePageIndex = 1;
  77. }
  78. else if (fill is PathGradientFill)
  79. {
  80. PathGradientFill = fill as PathGradientFill;
  81. cbxPathCenterColor.Color = PathGradientFill.CenterColor;
  82. cbxPathEdgeColor.Color = PathGradientFill.EdgeColor;
  83. rbPathEllipse.Checked = PathGradientFill.Style == PathGradientStyle.Elliptic;
  84. rbPathRectangle.Checked = PathGradientFill.Style == PathGradientStyle.Rectangular;
  85. pcPanels.ActivePageIndex = 2;
  86. }
  87. else if (fill is HatchFill)
  88. {
  89. HatchFill = fill as HatchFill;
  90. cbxHatchForeColor.Color = HatchFill.ForeColor;
  91. cbxHatchBackColor.Color = HatchFill.BackColor;
  92. cbxHatch.Style = HatchFill.Style;
  93. pcPanels.ActivePageIndex = 3;
  94. }
  95. else if (fill is GlassFill)
  96. {
  97. GlassFill = fill as GlassFill;
  98. cbxGlassColor.Color = GlassFill.Color;
  99. trbGlassBlend.Value = (int)Math.Round(GlassFill.Blend * 100);
  100. cbGlassHatch.Checked = GlassFill.Hatch;
  101. pcPanels.ActivePageIndex = 4;
  102. }
  103. else if (fill is TextureFill)
  104. {
  105. TextureFill = fill as TextureFill;
  106. cbWrapMode.SelectedItem = TextureFill.WrapMode.ToString();
  107. numImgWidth.Value = TextureFill.ImageWidth;
  108. numImgHeight.Value = TextureFill.ImageHeight;
  109. chBxPreserveRatio.Checked = TextureFill.PreserveAspectRatio;
  110. numOffsetX.Value = TextureFill.ImageOffsetX;
  111. numOffsetY.Value = TextureFill.ImageOffsetY;
  112. pcPanels.ActivePageIndex = 5;
  113. }
  114. }
  115. public override void Localize()
  116. {
  117. base.Localize();
  118. MyRes res = new MyRes("Forms,Fill");
  119. Text = res.Get("");
  120. pnSolid.Text = res.Get("Solid");
  121. pnLinear.Text = res.Get("LinearGradient");
  122. pnPath.Text = res.Get("PathGradient");
  123. pnHatch.Text = res.Get("Hatch");
  124. pnGlass.Text = res.Get("Glass");
  125. pnTexture.Text = res.Get("Texture");
  126. lblWrapMode.Text = res.Get("WrapMode");
  127. btnOpenFileDialog.Text = res.Get("ChooseImage");
  128. gpImageSize.Text = res.Get("ImageSize");
  129. lblImgWidth.Text = res.Get("ImageWidth");
  130. lblImgHeight.Text = res.Get("ImageHeight");
  131. chBxPreserveRatio.Text = res.Get("PreserveAspectRatio");
  132. gbOffset.Text = res.Get("ImageOffset");
  133. gbSolidColors.Text = res.Get("Color");
  134. lblSolidColor.Text = res.Get("Color");
  135. gbLinearColors.Text = res.Get("Colors");
  136. lblLinearStartColor.Text = res.Get("StartColor");
  137. lblLinearEndColor.Text = res.Get("EndColor");
  138. gbLinearOptions.Text = res.Get("Options");
  139. lblLinearFocus.Text = res.Get("Focus");
  140. lblLinearContrast.Text = res.Get("Contrast");
  141. gbLinearAngle.Text = res.Get("Angle");
  142. gbPathColors.Text = res.Get("Colors");
  143. lblPathCenterColor.Text = res.Get("CenterColor");
  144. lblPathEdgeColor.Text = res.Get("EdgeColor");
  145. gbPathShape.Text = res.Get("Shape");
  146. rbPathEllipse.Text = res.Get("Ellipse");
  147. rbPathRectangle.Text = res.Get("Rectangle");
  148. gbHatchColors.Text = res.Get("Colors");
  149. lblHatchForeColor.Text = res.Get("ForeColor");
  150. lblHatchBackColor.Text = res.Get("BackColor");
  151. gbHatchStyle.Text = res.Get("Style");
  152. gbGlassOptions.Text = res.Get("Options");
  153. lblGlassColor.Text = res.Get("Color");
  154. lblGlassBlend.Text = res.Get("Blend");
  155. cbGlassHatch.Text = res.Get("GlassHatch");
  156. }
  157. public override void UpdateDpiDependencies()
  158. {
  159. base.UpdateDpiDependencies();
  160. btnOpenFileDialog.Image = GetImage(1);
  161. cbxHatch.ItemHeight = this.LogicalToDevice(26);
  162. }
  163. private void pcPanels_PageSelected(object sender, EventArgs e)
  164. {
  165. if (updating)
  166. return;
  167. updating = true;
  168. int currentFill = pcPanels.ActivePageIndex;
  169. Fill = fills[currentFill];
  170. pnSample.Refresh();
  171. updating = false;
  172. }
  173. private void pnSample_Paint(object sender, PaintEventArgs e)
  174. {
  175. Graphics g = e.Graphics;
  176. if (Fill is TextureFill)
  177. {
  178. (Fill as TextureFill).Draw(new FRPaintEventArgs(g, 1, 1, null),
  179. new RectangleF(0, 0, pnSample.Width, pnSample.Height));
  180. }
  181. if (Fill is GlassFill)
  182. {
  183. (Fill as GlassFill).Draw(new FRPaintEventArgs(g, 1, 1, null),
  184. new RectangleF(0, 0, pnSample.Width, pnSample.Height));
  185. }
  186. else if (Fill != null)
  187. {
  188. Brush brush = null;
  189. if (Fill is SolidFill && Fill.IsTransparent)
  190. brush = new HatchBrush(HatchStyle.LargeCheckerBoard, Color.White, Color.Gainsboro);
  191. else
  192. brush = Fill.CreateBrush(new RectangleF(0, 0, pnSample.Width, pnSample.Height));
  193. g.FillRectangle(brush, 0, 0, pnSample.Width, pnSample.Height);
  194. brush.Dispose();
  195. }
  196. g.DrawRectangle(SystemPens.ControlDark, 0, 0, pnSample.Width - 1, pnSample.Height - 1);
  197. }
  198. private void Change()
  199. {
  200. pnSample.Refresh();
  201. }
  202. private void cbxSolidColor_ColorSelected(object sender, EventArgs e)
  203. {
  204. SolidFill.Color = cbxSolidColor.Color;
  205. Change();
  206. }
  207. private void cbxLinearStartColor_ColorSelected(object sender, EventArgs e)
  208. {
  209. LinearGradientFill.StartColor = cbxLinearStartColor.Color;
  210. Change();
  211. }
  212. private void cbxLinearEndColor_ColorSelected(object sender, EventArgs e)
  213. {
  214. LinearGradientFill.EndColor = cbxLinearEndColor.Color;
  215. Change();
  216. }
  217. private void trbLinearFocus_ValueChanged(object sender, EventArgs e)
  218. {
  219. LinearGradientFill.Focus = trbLinearFocus.Value / 100f;
  220. Change();
  221. }
  222. private void trbLinearContrast_ValueChanged(object sender, EventArgs e)
  223. {
  224. LinearGradientFill.Contrast = trbLinearContrast.Value / 100f;
  225. Change();
  226. }
  227. private void acLinearAngle_AngleChanged(object sender, EventArgs e)
  228. {
  229. LinearGradientFill.Angle = acLinearAngle.Angle;
  230. Change();
  231. }
  232. private void cbxPathCenterColor_ColorSelected(object sender, EventArgs e)
  233. {
  234. PathGradientFill.CenterColor = cbxPathCenterColor.Color;
  235. Change();
  236. }
  237. private void cbxPathEdgeColor_ColorSelected(object sender, EventArgs e)
  238. {
  239. PathGradientFill.EdgeColor = cbxPathEdgeColor.Color;
  240. Change();
  241. }
  242. private void rbPathEllipse_CheckedChanged(object sender, EventArgs e)
  243. {
  244. PathGradientFill.Style = rbPathEllipse.Checked ? PathGradientStyle.Elliptic : PathGradientStyle.Rectangular;
  245. Change();
  246. }
  247. private void cbxHatchForeColor_ColorSelected(object sender, EventArgs e)
  248. {
  249. HatchFill.ForeColor = cbxHatchForeColor.Color;
  250. Change();
  251. }
  252. private void cbxHatchBackColor_ColorSelected(object sender, EventArgs e)
  253. {
  254. HatchFill.BackColor = cbxHatchBackColor.Color;
  255. Change();
  256. }
  257. private void cbxHatch_HatchSelected(object sender, EventArgs e)
  258. {
  259. HatchFill.Style = cbxHatch.Style;
  260. Change();
  261. }
  262. private void cbxGlassColor_ColorSelected(object sender, EventArgs e)
  263. {
  264. GlassFill.Color = cbxGlassColor.Color;
  265. Change();
  266. }
  267. private void trbGlassBlend_ValueChanged(object sender, EventArgs e)
  268. {
  269. GlassFill.Blend = trbGlassBlend.Value / 100f;
  270. Change();
  271. }
  272. private void cbGlassHatch_CheckedChanged(object sender, EventArgs e)
  273. {
  274. GlassFill.Hatch = cbGlassHatch.Checked;
  275. Change();
  276. }
  277. private void btnOpenFileDialog_Click(object sender, EventArgs e)
  278. {
  279. if (Dialogs.OpenImage(out string fileName))
  280. {
  281. TextureFill = new TextureFill(File.ReadAllBytes(fileName));
  282. Fill = TextureFill;
  283. Change();
  284. }
  285. }
  286. private void numImgWidth_ValueChanged(object sender, EventArgs e)
  287. {
  288. if (TextureFill.ImageWidth == (int)numImgWidth.Value)
  289. return;
  290. TextureFill.ImageWidth = (int)numImgWidth.Value;
  291. numImgHeight.Value = TextureFill.ImageHeight;
  292. Change();
  293. }
  294. private void numImgHeight_ValueChanged(object sender, EventArgs e)
  295. {
  296. if (TextureFill.ImageHeight == (int)numImgHeight.Value)
  297. return;
  298. TextureFill.ImageHeight = (int)numImgHeight.Value;
  299. Change();
  300. numImgWidth.Value = TextureFill.ImageWidth;
  301. }
  302. private void numOffsetX_ValueChanged(object sender, EventArgs e)
  303. {
  304. if (TextureFill.ImageOffsetX == (int)numOffsetX.Value)
  305. return;
  306. TextureFill.ImageOffsetX = (int)numOffsetX.Value;
  307. Change();
  308. numOffsetX.Value = TextureFill.ImageOffsetX;
  309. }
  310. private void numOffsetY_ValueChanged(object sender, EventArgs e)
  311. {
  312. if (TextureFill.ImageOffsetY == (int)numOffsetY.Value)
  313. return;
  314. TextureFill.ImageOffsetY = (int)numOffsetY.Value;
  315. Change();
  316. numOffsetY.Value = TextureFill.ImageOffsetY;
  317. }
  318. private void chBxPreserveRatio_CheckedChanged(object sender, EventArgs e)
  319. {
  320. TextureFill.PreserveAspectRatio = chBxPreserveRatio.Checked;
  321. Fill = TextureFill;
  322. Change();
  323. }
  324. private void cbWrapMode_SelectedIndexChanged(object sender, EventArgs e)
  325. {
  326. switch (Convert.ToString(cbWrapMode.SelectedItem))
  327. {
  328. case "Clamp":
  329. TextureFill.WrapMode = WrapMode.Clamp;
  330. break;
  331. case "Tile":
  332. TextureFill.WrapMode = WrapMode.Tile;
  333. break;
  334. case "TileFlipX":
  335. TextureFill.WrapMode = WrapMode.TileFlipX;
  336. break;
  337. case "TileFlipY":
  338. TextureFill.WrapMode = WrapMode.TileFlipY;
  339. break;
  340. case "TileFlipXY":
  341. TextureFill.WrapMode = WrapMode.TileFlipXY;
  342. break;
  343. }
  344. Fill = TextureFill;
  345. Change();
  346. }
  347. public FillEditorForm()
  348. {
  349. InitializeComponent();
  350. PopulateFills();
  351. Localize();
  352. UIUtils.CheckRTL(this);
  353. UpdateDpiDependencies();
  354. }
  355. internal class FillSample : Panel
  356. {
  357. public FillSample()
  358. {
  359. Size = new Size(156, 156);
  360. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  361. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  362. }
  363. }
  364. }
  365. }