RichEditorForm.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using FastReport.Utils;
  6. using FastReport.Data;
  7. using System.Reflection;
  8. namespace FastReport.Forms
  9. {
  10. internal partial class RichEditorForm : BaseForm
  11. {
  12. private RichObject rich;
  13. private bool updating;
  14. private static List<string> expandedNodes;
  15. private bool textChanged;
  16. private string GetTextWithBrackets(string text)
  17. {
  18. string[] brackets = rich.Brackets.Split(',');
  19. return brackets[0] + text + brackets[1];
  20. }
  21. private void UpdateControls()
  22. {
  23. if (updating)
  24. return;
  25. updating = true;
  26. Font font = rtbText.SelectionFont;
  27. if (font != null)
  28. {
  29. cbxFontName.FontName = font.Name;
  30. cbxFontSize.FontSize = font.Size;
  31. btnBold.Checked = font.Bold;
  32. btnItalic.Checked = font.Italic;
  33. btnUnderline.Checked = font.Underline;
  34. }
  35. else
  36. {
  37. cbxFontName.FontName = "Microsoft Sans Serif";
  38. cbxFontSize.FontSize = 8.5f;
  39. }
  40. TextAlign align = rtbText.SelectionAlignment;
  41. btnAlignLeft.Checked = align == TextAlign.Left;
  42. btnAlignCenter.Checked = align == TextAlign.Center;
  43. btnAlignRight.Checked = align == TextAlign.Right;
  44. btnAlignJustify.Checked = align == TextAlign.Justify;
  45. btnColor.Color = rtbText.SelectionColor;
  46. btnSubscript.Checked = rtbText.SelectionCharOffset == -4;
  47. btnSuperscript.Checked = rtbText.SelectionCharOffset == 4;
  48. btnBullets.Checked = rtbText.SelectionBullet;
  49. updating = false;
  50. }
  51. private void btnOk_Click(object sender, EventArgs e)
  52. {
  53. DialogResult = DialogResult.OK;
  54. }
  55. private void btnCancel_Click(object sender, EventArgs e)
  56. {
  57. DialogResult = DialogResult.Abort;
  58. }
  59. private void btnOpen_Click(object sender, EventArgs e)
  60. {
  61. using (OpenFileDialog dialog = new OpenFileDialog())
  62. {
  63. dialog.Filter = Res.Get("FileFilters,RtfFile");
  64. if (dialog.ShowDialog() == DialogResult.OK)
  65. rtbText.LoadFile(dialog.FileName);
  66. }
  67. }
  68. private void btnSave_Click(object sender, EventArgs e)
  69. {
  70. using (SaveFileDialog dialog = new SaveFileDialog())
  71. {
  72. dialog.Filter = Res.Get("FileFilters,RtfFile");
  73. dialog.DefaultExt = "rtf";
  74. if (dialog.ShowDialog() == DialogResult.OK)
  75. rtbText.SaveFile(dialog.FileName);
  76. }
  77. }
  78. private void btnUndo_Click(object sender, EventArgs e)
  79. {
  80. rtbText.Undo();
  81. }
  82. private void btnRedo_Click(object sender, EventArgs e)
  83. {
  84. rtbText.Redo();
  85. }
  86. private void cbxFontName_FontSelected(object sender, EventArgs e)
  87. {
  88. if (!updating)
  89. {
  90. rtbText.SetSelectionFont(cbxFontName.FontName);
  91. UpdateControls();
  92. rtbText.Focus();
  93. }
  94. }
  95. private void cbxFontSize_SizeSelected(object sender, EventArgs e)
  96. {
  97. if (!updating)
  98. {
  99. rtbText.SetSelectionSize((int)cbxFontSize.FontSize);
  100. rtbText.Focus();
  101. }
  102. }
  103. private void btnBold_Click(object sender, EventArgs e)
  104. {
  105. if (!updating)
  106. rtbText.SetSelectionBold(btnBold.Checked);
  107. }
  108. private void btnItalic_Click(object sender, EventArgs e)
  109. {
  110. if (!updating)
  111. rtbText.SetSelectionItalic(btnItalic.Checked);
  112. }
  113. private void btnUnderline_Click(object sender, EventArgs e)
  114. {
  115. if (!updating)
  116. rtbText.SetSelectionUnderline(btnUnderline.Checked);
  117. }
  118. private void btnAlignLeft_Click(object sender, EventArgs e)
  119. {
  120. if (!updating)
  121. {
  122. rtbText.SelectionAlignment = TextAlign.Left;
  123. UpdateControls();
  124. }
  125. }
  126. private void btnAlignCenter_Click(object sender, EventArgs e)
  127. {
  128. if (!updating)
  129. {
  130. rtbText.SelectionAlignment = TextAlign.Center;
  131. UpdateControls();
  132. }
  133. }
  134. private void btnAlignRight_Click(object sender, EventArgs e)
  135. {
  136. if (!updating)
  137. {
  138. rtbText.SelectionAlignment = TextAlign.Right;
  139. UpdateControls();
  140. }
  141. }
  142. private void btnAlignJustify_Click(object sender, EventArgs e)
  143. {
  144. if (!updating)
  145. {
  146. rtbText.SelectionAlignment = TextAlign.Justify;
  147. UpdateControls();
  148. }
  149. }
  150. private void btnColor_ButtonClick(object sender, EventArgs e)
  151. {
  152. if (!updating)
  153. rtbText.SelectionColor = btnColor.DefaultColor;
  154. }
  155. private void btnSubscript_Click(object sender, EventArgs e)
  156. {
  157. if (!updating)
  158. {
  159. rtbText.SelectionCharOffset = btnSubscript.Checked ? -4 : 0;
  160. UpdateControls();
  161. }
  162. }
  163. private void btnSuperscript_Click(object sender, EventArgs e)
  164. {
  165. if (!updating)
  166. {
  167. rtbText.SelectionCharOffset = btnSuperscript.Checked ? 4 : 0;
  168. UpdateControls();
  169. }
  170. }
  171. private void btnBullets_Click(object sender, EventArgs e)
  172. {
  173. if (!updating)
  174. rtbText.SelectionBullet = btnBullets.Checked;
  175. }
  176. private void rtbText_SelectionChanged(object sender, EventArgs e)
  177. {
  178. UpdateControls();
  179. }
  180. private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
  181. {
  182. rtbText.Focus();
  183. }
  184. private void tvData_AfterSelect(object sender, TreeViewEventArgs e)
  185. {
  186. bool descrVisible = tvData.SelectedNode != null &&
  187. (tvData.SelectedNode.Tag is MethodInfo || tvData.SelectedNode.Tag is SystemVariable);
  188. expandableSplitter1.Visible = descrVisible;
  189. lblDescription.Visible = descrVisible;
  190. if (descrVisible)
  191. lblDescription.ShowDescription(rich.Report, tvData.SelectedNode.Tag);
  192. }
  193. private void tvData_ItemDrag(object sender, ItemDragEventArgs e)
  194. {
  195. tvData.SelectedNode = e.Item as TreeNode;
  196. if (tvData.SelectedItem != "")
  197. tvData.DoDragDrop(e.Item, DragDropEffects.Move);
  198. else
  199. tvData.DoDragDrop(e.Item, DragDropEffects.None);
  200. }
  201. private void rtbText_DragOver(object sender, DragEventArgs e)
  202. {
  203. e.Effect = e.AllowedEffect;
  204. }
  205. private void rtbText_DragDrop(object sender, DragEventArgs e)
  206. {
  207. rtbText.SelectedText = GetTextWithBrackets(tvData.SelectedItem);
  208. rtbText.Focus();
  209. }
  210. private void tvData_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
  211. {
  212. if (tvData.SelectedItem != "")
  213. {
  214. rtbText.SelectedText = GetTextWithBrackets(tvData.SelectedItem);
  215. rtbText.Focus();
  216. }
  217. }
  218. private void rtbText_TextChanged(object sender, EventArgs e)
  219. {
  220. textChanged = true;
  221. }
  222. private void RichEditorForm_Shown(object sender, EventArgs e)
  223. {
  224. UpdateControls();
  225. rtbText.Focus();
  226. }
  227. private void RichEditorForm_FormClosing(object sender, FormClosingEventArgs e)
  228. {
  229. if (DialogResult != DialogResult.OK && DialogResult != DialogResult.Abort && textChanged)
  230. {
  231. string askText = Res.Get("Forms,RichTextEditor,ConfirmChanges");
  232. DialogResult askResult = FRMessageBox.Confirm(askText, MessageBoxButtons.YesNoCancel);
  233. switch (askResult)
  234. {
  235. case DialogResult.Yes:
  236. DialogResult = DialogResult.OK;
  237. break;
  238. case DialogResult.No:
  239. break;
  240. case DialogResult.Cancel:
  241. e.Cancel = true;
  242. break;
  243. }
  244. }
  245. }
  246. private void RichEditorForm_FormClosed(object sender, FormClosedEventArgs e)
  247. {
  248. Done();
  249. }
  250. protected override void SaveState()
  251. {
  252. base.SaveState();
  253. Storage.SetDip("Splitter", splitContainer1.SplitterDistance);
  254. Storage.SetDip("DescriptionHeight", lblDescription.Height);
  255. expandedNodes = tvData.ExpandedNodes;
  256. }
  257. protected override void RestoreState()
  258. {
  259. base.RestoreState();
  260. splitContainer1.SplitterDistance = Storage.GetDip("Splitter", 550, 50, Width - 50);
  261. lblDescription.Height = Storage.GetDip("DescriptionHeight", 50, 50, 200);
  262. if (expandedNodes != null)
  263. tvData.ExpandedNodes = expandedNodes;
  264. }
  265. public override void Localize()
  266. {
  267. Text = Res.Get("Forms,RichTextEditor");
  268. MyRes res = new MyRes("Designer,Toolbar,Standard");
  269. btnUndo.ToolTipText = res.Get("Undo");
  270. btnRedo.ToolTipText = res.Get("Redo");
  271. res = new MyRes("Designer,Toolbar,Text");
  272. cbxFontName.ToolTipText = res.Get("Name");
  273. cbxFontSize.ToolTipText = res.Get("Size");
  274. btnBold.ToolTipText = res.Get("Bold");
  275. btnItalic.ToolTipText = res.Get("Italic");
  276. btnUnderline.ToolTipText = res.Get("Underline");
  277. btnAlignLeft.ToolTipText = res.Get("Left");
  278. btnAlignCenter.ToolTipText = res.Get("Center");
  279. btnAlignRight.ToolTipText = res.Get("Right");
  280. btnAlignJustify.ToolTipText = res.Get("Justify");
  281. btnColor.ToolTipText = res.Get("Color");
  282. res = new MyRes("Forms,RichTextEditor");
  283. btnOk.ToolTipText = Res.Get("Buttons,OK");
  284. btnCancel.ToolTipText = Res.Get("Buttons,Cancel");
  285. btnOpen.ToolTipText = res.Get("Open");
  286. btnSave.ToolTipText = res.Get("Save");
  287. btnSubscript.ToolTipText = res.Get("Subscript");
  288. btnSuperscript.ToolTipText = res.Get("Superscript");
  289. btnBullets.ToolTipText = res.Get("Bullets");
  290. }
  291. public override void UpdateDpiDependencies()
  292. {
  293. base.UpdateDpiDependencies();
  294. ts1.Height = this.LogicalToDevice(28);
  295. ts1.Font = Font;
  296. cbxFontName.Owner = this;
  297. cbxFontName.Font = Font;
  298. cbxFontName.Width = this.LogicalToDevice(140);
  299. cbxFontName.UpdateDpiDependencies();
  300. cbxFontSize.Owner = this;
  301. cbxFontSize.Font = Font;
  302. cbxFontSize.Width = this.LogicalToDevice(50);
  303. cbxFontSize.UpdateDpiDependencies();
  304. tvData.ImageList = GetImages();
  305. btnOk.Image = GetImage(210);
  306. btnCancel.Image = GetImage(212);
  307. btnOpen.Image = GetImage(1);
  308. btnSave.Image = GetImage(2);
  309. btnUndo.Image = GetImage(8);
  310. btnRedo.Image = GetImage(9);
  311. btnBold.Image = GetImage(20);
  312. btnItalic.Image = GetImage(21);
  313. btnUnderline.Image = GetImage(22);
  314. btnAlignLeft.Image = GetImage(25);
  315. btnAlignCenter.Image = GetImage(26);
  316. btnAlignRight.Image = GetImage(27);
  317. btnAlignJustify.Image = GetImage(28);
  318. btnSubscript.Image = GetImage(171);
  319. btnSuperscript.Image = GetImage(172);
  320. btnBullets.Image = GetImage(170);
  321. btnColor.Image = GetImage(23);
  322. }
  323. private void Init()
  324. {
  325. btnColor.ImageIndex = 23;
  326. btnColor.DefaultColor = Color.Black;
  327. ts1.GripStyle = ToolStripGripStyle.Hidden;
  328. ts1.Renderer = UIStyleUtils.GetToolStripRenderer(UIStyle.Light);
  329. tvData.CreateNodes(rich.Report.Dictionary);
  330. if (rich.Text != null && rich.Text.StartsWith(@"{\rtf"))
  331. rtbText.Rtf = rich.Text;
  332. else
  333. rtbText.Text = rich.Text;
  334. rtbText.Modified = false;
  335. rtbText.AllowDrop = true;
  336. rtbText.DragOver += rtbText_DragOver;
  337. rtbText.DragDrop += rtbText_DragDrop;
  338. textChanged = false;
  339. }
  340. private void Done()
  341. {
  342. if (DialogResult == DialogResult.OK)
  343. rich.Text = rtbText.Rtf;
  344. }
  345. public RichEditorForm(RichObject rich)
  346. {
  347. this.rich = rich;
  348. CanSaveRestoreState = true;
  349. InitializeComponent();
  350. Localize();
  351. Init();
  352. UIUtils.CheckRTL(this);
  353. UpdateDpiDependencies();
  354. }
  355. }
  356. }