StyleEditorForm.cs 13 KB

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