HighlightEditorForm.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using FastReport.Utils;
  5. using FastReport.Controls;
  6. namespace FastReport.Forms
  7. {
  8. internal partial class HighlightEditorForm : BaseDialogForm
  9. {
  10. private Report report;
  11. private ImageList images;
  12. private ConditionCollection conditions;
  13. private Timer refreshTimer;
  14. private bool updating;
  15. public ConditionCollection Conditions
  16. {
  17. get { return conditions; }
  18. set
  19. {
  20. conditions = new ConditionCollection();
  21. conditions.Assign(value);
  22. PopulateConditions();
  23. }
  24. }
  25. private HighlightCondition CurrentCondition
  26. {
  27. get
  28. {
  29. if (lvConditions.SelectedItems.Count == 0)
  30. return null;
  31. return lvConditions.SelectedItems[0].Tag as HighlightCondition;
  32. }
  33. }
  34. private Bitmap GetImage(HighlightCondition c)
  35. {
  36. Bitmap bmp = new Bitmap(16, 16);
  37. using (Graphics g = Graphics.FromImage(bmp))
  38. {
  39. g.FillRectangle(Brushes.White, new RectangleF(0, 0, 16, 16));
  40. using (TextObject sample = new TextObject())
  41. {
  42. sample.Bounds = new RectangleF(0, 0, 16, 16);
  43. sample.ApplyCondition(c);
  44. sample.Font = new Font("Times New Roman", 12);
  45. sample.Text = "A";
  46. sample.HorzAlign = HorzAlign.Center;
  47. sample.VertAlign = VertAlign.Center;
  48. using (GraphicCache cache = new GraphicCache())
  49. {
  50. sample.Draw(new FRPaintEventArgs(g, 1, 1, cache));
  51. }
  52. }
  53. }
  54. return bmp;
  55. }
  56. private int GetImageIndex(HighlightCondition c)
  57. {
  58. Bitmap bmp = GetImage(c);
  59. images.Images.Add(bmp);
  60. return images.Images.Count - 1;
  61. }
  62. private void RefreshSample()
  63. {
  64. pnSample.Refresh();
  65. }
  66. private void UpdateControls()
  67. {
  68. bool enabled = CurrentCondition != null;
  69. btnDelete.Enabled = enabled;
  70. btnEdit.Enabled = enabled;
  71. btnUp.Enabled = enabled;
  72. btnDown.Enabled = enabled;
  73. gbStyle.Enabled = enabled;
  74. if (enabled)
  75. {
  76. updating = true;
  77. cbApplyBorder.Checked = CurrentCondition.ApplyBorder;
  78. cbApplyFill.Checked = CurrentCondition.ApplyFill;
  79. cbApplyTextFill.Checked = CurrentCondition.ApplyTextFill;
  80. cbApplyFont.Checked = CurrentCondition.ApplyFont;
  81. cbVisible.Checked = CurrentCondition.Visible;
  82. btnBorder.Enabled = CurrentCondition.ApplyBorder;
  83. btnFill.Enabled = CurrentCondition.ApplyFill;
  84. btnTextColor.Enabled = CurrentCondition.ApplyTextFill;
  85. btnFont.Enabled = CurrentCondition.ApplyFont;
  86. updating = false;
  87. }
  88. }
  89. private void SetApply()
  90. {
  91. foreach (ListViewItem li in lvConditions.SelectedItems)
  92. {
  93. HighlightCondition c = li.Tag as HighlightCondition;
  94. c.ApplyBorder = cbApplyBorder.Checked;
  95. c.ApplyFill = cbApplyFill.Checked;
  96. c.ApplyTextFill = cbApplyTextFill.Checked;
  97. c.ApplyFont = cbApplyFont.Checked;
  98. c.Visible = cbVisible.Checked;
  99. li.ImageIndex = GetImageIndex(c);
  100. }
  101. RefreshSample();
  102. }
  103. private void PopulateConditions()
  104. {
  105. foreach (HighlightCondition c in conditions)
  106. {
  107. ListViewItem li = lvConditions.Items.Add(c.Expression, GetImageIndex(c));
  108. li.Tag = c;
  109. }
  110. if (lvConditions.Items.Count > 0)
  111. lvConditions.Items[0].Selected = true;
  112. UpdateControls();
  113. }
  114. private void pnSample_Paint(object sender, PaintEventArgs e)
  115. {
  116. if (CurrentCondition == null)
  117. return;
  118. #if AVALONIA
  119. e.Graphics.FontScale = 1;
  120. #endif
  121. using (TextObject sample = new TextObject())
  122. {
  123. sample.Text = Res.Get("Misc,Sample");
  124. sample.ApplyCondition(CurrentCondition);
  125. float scale = this.DpiMultiplier();
  126. sample.Bounds = new RectangleF(2, 2, pnSample.Width / scale - 4, pnSample.Height / scale - 4);
  127. sample.HorzAlign = HorzAlign.Center;
  128. sample.VertAlign = VertAlign.Center;
  129. using (GraphicCache cache = new GraphicCache())
  130. {
  131. sample.Draw(new FRPaintEventArgs(e.Graphics, scale, scale, cache));
  132. }
  133. }
  134. }
  135. private void FTimer_Tick(object sender, EventArgs e)
  136. {
  137. UpdateControls();
  138. RefreshSample();
  139. refreshTimer.Stop();
  140. }
  141. private void lvConditions_SelectedIndexChanged(object sender, EventArgs e)
  142. {
  143. refreshTimer.Start();
  144. }
  145. private void btnAdd_Click(object sender, EventArgs e)
  146. {
  147. using (ExpressionEditorForm form = new ExpressionEditorForm(report))
  148. {
  149. form.ExpressionText = report.ScriptLanguage == Language.CSharp ? "Value == 0" : "Value = 0";
  150. if (form.ShowDialog() == DialogResult.OK)
  151. {
  152. HighlightCondition c = new HighlightCondition();
  153. conditions.Add(c);
  154. c.Expression = form.ExpressionText;
  155. ListViewItem li = lvConditions.Items.Add(c.Expression, GetImageIndex(c));
  156. li.Tag = c;
  157. lvConditions.SelectedItems.Clear();
  158. li.Selected = true;
  159. }
  160. }
  161. }
  162. private void btnDelete_Click(object sender, EventArgs e)
  163. {
  164. while (lvConditions.SelectedItems.Count > 0)
  165. {
  166. HighlightCondition c = lvConditions.SelectedItems[0].Tag as HighlightCondition;
  167. conditions.Remove(c);
  168. lvConditions.Items.Remove(lvConditions.SelectedItems[0]);
  169. }
  170. }
  171. private void btnEdit_Click(object sender, EventArgs e)
  172. {
  173. if (lvConditions.SelectedItems.Count == 1)
  174. {
  175. using (ExpressionEditorForm form = new ExpressionEditorForm(report))
  176. {
  177. form.ExpressionText = CurrentCondition.Expression;
  178. if (form.ShowDialog() == DialogResult.OK)
  179. {
  180. CurrentCondition.Expression = form.ExpressionText;
  181. lvConditions.SelectedItems[0].Text = CurrentCondition.Expression;
  182. }
  183. }
  184. }
  185. }
  186. private void btnUp_Click(object sender, EventArgs e)
  187. {
  188. if (lvConditions.SelectedItems.Count != 1)
  189. return;
  190. int index = lvConditions.SelectedIndices[0];
  191. if (index > 0)
  192. {
  193. ListViewItem li = lvConditions.SelectedItems[0];
  194. lvConditions.Items.Remove(li);
  195. lvConditions.Items.Insert(index - 1, li);
  196. li.Selected = false;
  197. li.Selected = true;
  198. HighlightCondition c = li.Tag as HighlightCondition;
  199. conditions.Remove(c);
  200. conditions.Insert(index - 1, c);
  201. }
  202. }
  203. private void btnDown_Click(object sender, EventArgs e)
  204. {
  205. if (lvConditions.SelectedItems.Count != 1)
  206. return;
  207. int index = lvConditions.SelectedIndices[0];
  208. if (index < lvConditions.Items.Count - 1)
  209. {
  210. ListViewItem li = lvConditions.SelectedItems[0];
  211. lvConditions.Items.Remove(li);
  212. lvConditions.Items.Insert(index + 1, li);
  213. li.Selected = false;
  214. li.Selected = true;
  215. HighlightCondition c = li.Tag as HighlightCondition;
  216. conditions.Remove(c);
  217. conditions.Insert(index + 1, c);
  218. }
  219. }
  220. private void cbApplyBorder_CheckedChanged(object sender, EventArgs e)
  221. {
  222. if (updating || CurrentCondition == null)
  223. return;
  224. btnBorder.Enabled = cbApplyBorder.Checked;
  225. SetApply();
  226. }
  227. private void cbApplyFill_CheckedChanged(object sender, EventArgs e)
  228. {
  229. if (updating || CurrentCondition == null)
  230. return;
  231. btnFill.Enabled = cbApplyFill.Checked;
  232. SetApply();
  233. }
  234. private void cbApplyTextFill_CheckedChanged(object sender, EventArgs e)
  235. {
  236. if (updating || CurrentCondition == null)
  237. return;
  238. btnTextColor.Enabled = cbApplyTextFill.Checked;
  239. SetApply();
  240. }
  241. private void cbApplyFont_CheckedChanged(object sender, EventArgs e)
  242. {
  243. if (updating || CurrentCondition == null)
  244. return;
  245. btnFont.Enabled = cbApplyFont.Checked;
  246. SetApply();
  247. }
  248. private void btnBorder_Click(object sender, EventArgs e)
  249. {
  250. if (CurrentCondition == null)
  251. return;
  252. using (BorderEditorForm editor = new BorderEditorForm())
  253. {
  254. editor.Border = CurrentCondition.Border.Clone();
  255. if (editor.ShowDialog() == DialogResult.OK)
  256. {
  257. foreach (ListViewItem li in lvConditions.SelectedItems)
  258. {
  259. HighlightCondition c = li.Tag as HighlightCondition;
  260. c.Border = editor.Border.Clone();
  261. li.ImageIndex = GetImageIndex(c);
  262. }
  263. RefreshSample();
  264. }
  265. }
  266. }
  267. private void btnFill_Click(object sender, EventArgs e)
  268. {
  269. if (CurrentCondition == null)
  270. return;
  271. using (FillEditorForm editor = new FillEditorForm())
  272. {
  273. editor.Fill = CurrentCondition.Fill.Clone();
  274. if (editor.ShowDialog() == DialogResult.OK)
  275. {
  276. foreach (ListViewItem li in lvConditions.SelectedItems)
  277. {
  278. HighlightCondition c = li.Tag as HighlightCondition;
  279. c.Fill = editor.Fill.Clone();
  280. li.ImageIndex = GetImageIndex(c);
  281. }
  282. RefreshSample();
  283. }
  284. }
  285. }
  286. private void btnTextColor_Click(object sender, EventArgs e)
  287. {
  288. if (CurrentCondition == null)
  289. return;
  290. using (FillEditorForm editor = new FillEditorForm())
  291. {
  292. editor.Fill = CurrentCondition.TextFill.Clone();
  293. if (editor.ShowDialog() == DialogResult.OK)
  294. {
  295. foreach (ListViewItem li in lvConditions.SelectedItems)
  296. {
  297. HighlightCondition c = li.Tag as HighlightCondition;
  298. c.TextFill = editor.Fill.Clone();
  299. li.ImageIndex = GetImageIndex(c);
  300. }
  301. RefreshSample();
  302. }
  303. }
  304. }
  305. private void btnFont_Click(object sender, EventArgs e)
  306. {
  307. if (CurrentCondition == null)
  308. return;
  309. using (FontDialog dialog = new FontDialog())
  310. {
  311. dialog.Font = CurrentCondition.Font;
  312. if (dialog.ShowDialog() == DialogResult.OK)
  313. {
  314. foreach (ListViewItem li in lvConditions.SelectedItems)
  315. {
  316. HighlightCondition c = li.Tag as HighlightCondition;
  317. c.Font = dialog.Font;
  318. li.ImageIndex = GetImageIndex(c);
  319. }
  320. RefreshSample();
  321. }
  322. }
  323. }
  324. private void cbVisible_CheckedChanged(object sender, EventArgs e)
  325. {
  326. if (updating || CurrentCondition == null)
  327. return;
  328. SetApply();
  329. }
  330. private void HighlightEditorForm_FormClosed(object sender, FormClosedEventArgs e)
  331. {
  332. Done();
  333. }
  334. private void Init()
  335. {
  336. images = new ImageList();
  337. images.ImageSize = new Size(16, 16);
  338. images.ColorDepth = ColorDepth.Depth24Bit;
  339. lvConditions.SmallImageList = images;
  340. refreshTimer = new Timer();
  341. refreshTimer.Interval = 50;
  342. refreshTimer.Tick += FTimer_Tick;
  343. }
  344. private void Done()
  345. {
  346. images.Dispose();
  347. }
  348. public override void Localize()
  349. {
  350. base.Localize();
  351. MyRes res = new MyRes("Forms,HighlightEditor");
  352. Text = res.Get("");
  353. gbConditions.Text = res.Get("Conditions");
  354. btnAdd.Text = res.Get("Add");
  355. btnDelete.Text = res.Get("Delete");
  356. btnEdit.Text = res.Get("Edit");
  357. gbStyle.Text = res.Get("Style");
  358. btnBorder.Text = Res.Get("Forms,Style,Border");
  359. btnFill.Text = res.Get("Fill");
  360. btnTextColor.Text = res.Get("TextColor");
  361. btnFont.Text = res.Get("Font");
  362. cbVisible.Text = res.Get("Visible");
  363. }
  364. public override void UpdateDpiDependencies()
  365. {
  366. base.UpdateDpiDependencies();
  367. btnUp.Image = GetImage(208);
  368. btnDown.Image = GetImage(209);
  369. btnBorder.Image = GetImage(36);
  370. btnFill.Image = GetImage(38);
  371. btnTextColor.Image = GetImage(23);
  372. btnFont.Image = GetImage(59);
  373. MinimumSize = this.LogicalToDevice(new Size(480, 322));
  374. }
  375. public HighlightEditorForm(Report report)
  376. {
  377. this.report = report;
  378. CanSaveRestoreState = true;
  379. InitializeComponent();
  380. Init();
  381. Localize();
  382. UIUtils.CheckRTL(this);
  383. UpdateDpiDependencies();
  384. }
  385. }
  386. }