WelcomeForm.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using FastReport.Design;
  2. using FastReport.Utils;
  3. using FastReport.Wizards;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Windows.Forms;
  9. namespace FastReport.Forms
  10. {
  11. /// <summary>
  12. /// Represents the Welcome window displayed on the designer startup
  13. /// </summary>
  14. public partial class WelcomeForm : BaseForm
  15. {
  16. private Designer designer;
  17. private Button btnOpen;
  18. private const int maxItemsInColumn = 9;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="WelcomeForm"/> class.
  21. /// </summary>
  22. /// <param name="designer"></param>
  23. public WelcomeForm(Designer designer)
  24. {
  25. InitializeComponent();
  26. this.banner.Image = GetImage("Images.Welcome.png");
  27. #if COMMUNITY
  28. if (Config.WelcomeScreen != null)
  29. this.banner.Image = Config.WelcomeScreen;
  30. #endif
  31. this.designer = designer;
  32. setupLeftPanel();
  33. setupRightPanel();
  34. Localize();
  35. UIUtils.CheckRTL(this);
  36. cbShowOnStartup.Checked = Config.WelcomeShowOnStartup;
  37. Config.DesignerSettings.ReportLoaded += DesignerSettings_ReportLoaded;
  38. }
  39. #region Setup
  40. private void setupLeftPanel()
  41. {
  42. int i = 0;
  43. // Open item
  44. btnOpen = createButton
  45. (
  46. open_Click,
  47. "Open...",
  48. 66,
  49. getX(),
  50. getY(i),
  51. panelLeft,
  52. null,
  53. true,
  54. null
  55. );
  56. i++;
  57. // Recent items
  58. if (designer.cmdRecentFiles.Enabled && designer.RecentFiles.Count > 0)
  59. {
  60. for (int k = designer.RecentFiles.Count - 1; k >= 0; k--)
  61. {
  62. string file = designer.RecentFiles[k];
  63. createButton
  64. (
  65. recent_Click,
  66. Path.GetFileName(file),
  67. 0,
  68. getX(),
  69. getY(i),
  70. panelLeft,
  71. file,
  72. true,
  73. file
  74. );
  75. i++;
  76. if (i >= maxItemsInColumn)
  77. break;
  78. }
  79. }
  80. }
  81. private void setupRightPanel()
  82. {
  83. List<ObjectInfo> objects = new List<ObjectInfo>();
  84. RegisteredObjects.Objects.EnumItems(objects);
  85. int i = 0;
  86. // Wizards
  87. foreach (ObjectInfo info in objects)
  88. {
  89. if (info.Object != null &&
  90. info.Object.IsSubclassOf(typeof(WizardBase)) &&
  91. info.Flags == 0)
  92. {
  93. createButton
  94. (
  95. new_Click,
  96. Res.TryGet(info.Text),
  97. info.ImageIndex,
  98. getX(),
  99. getY(i),
  100. panelRight,
  101. null,
  102. true,
  103. info
  104. );
  105. i++;
  106. if (i >= maxItemsInColumn)
  107. break;
  108. }
  109. }
  110. }
  111. private void Localize()
  112. {
  113. MyRes res = new MyRes("Designer,Welcome");
  114. Text = res.Get("Title");
  115. #if COMMUNITY
  116. Text += " Community";
  117. #endif
  118. cbShowOnStartup.Text = res.Get("Show");
  119. lblOpen.Text = res.Get("Open");
  120. lblNew.Text = res.Get("New");
  121. btnOpen.Text = " " + Res.Get("Designer,Menu,File,Open");
  122. }
  123. #endregion
  124. #region Events
  125. private void open_Click(object sender, EventArgs e)
  126. {
  127. designer.cmdOpen.Invoke();
  128. }
  129. private void recent_Click(object sender, EventArgs e)
  130. {
  131. designer.UpdatePlugins(null);
  132. designer.cmdOpen.LoadFile((sender as Button).Tag as string);
  133. }
  134. private void new_Click(object sender, EventArgs e)
  135. {
  136. (Activator.CreateInstance(((sender as Button).Tag as ObjectInfo).Object) as WizardBase).Run(designer);
  137. }
  138. private void table_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
  139. {
  140. if (e.Column == 0 && e.Row == 0)
  141. {
  142. int t = 15;
  143. e.Graphics.DrawLine(new Pen(Color.DarkGray),
  144. e.CellBounds.Right,
  145. e.CellBounds.Top + t,
  146. e.CellBounds.Right,
  147. e.CellBounds.Bottom - t);
  148. }
  149. }
  150. private void bottom_Paint(object sender, PaintEventArgs e)
  151. {
  152. e.Graphics.DrawLine(new Pen(Color.LightGray),
  153. e.ClipRectangle.Left,
  154. e.ClipRectangle.Top,
  155. e.ClipRectangle.Right,
  156. e.ClipRectangle.Top);
  157. }
  158. private void DesignerSettings_ReportLoaded(object sender, ReportLoadedEventArgs e)
  159. {
  160. Config.DesignerSettings.ReportLoaded -= DesignerSettings_ReportLoaded;
  161. Close();
  162. }
  163. private void cbShowOnStartup_CheckedChanged(object sender, EventArgs e)
  164. {
  165. Config.WelcomeShowOnStartup = cbShowOnStartup.Checked;
  166. }
  167. #endregion
  168. #region Utils
  169. private Button createButton(EventHandler onClick,
  170. string text,
  171. int icon,
  172. int x,
  173. int y,
  174. Control parent,
  175. string tooltipText,
  176. bool trimText,
  177. object tag)
  178. {
  179. Button b = new Button();
  180. b.Tag = tag;
  181. if (onClick != null)
  182. b.Click += onClick;
  183. b.Location = new Point(x, y);
  184. b.Height = 28;
  185. b.Width = parent.Width - x - x;
  186. b.Text = " " + text;
  187. b.TextAlign = ContentAlignment.MiddleLeft;
  188. b.TextImageRelation = TextImageRelation.ImageBeforeText;
  189. b.FlatStyle = FlatStyle.Flat;
  190. b.FlatAppearance.BorderSize = 0;
  191. b.FlatAppearance.BorderColor = parent != null ? parent.BackColor : Color.White;
  192. b.FlatAppearance.MouseOverBackColor = Color.FromArgb(-2628366);
  193. b.FlatAppearance.MouseDownBackColor = Color.FromArgb(-4599318);
  194. b.Image = this.GetImage(icon);
  195. b.ImageAlign = ContentAlignment.TopLeft;
  196. if (tooltipText != null && tooltipText.Trim() != "")
  197. {
  198. ToolTip tooltip = new ToolTip();
  199. tooltip.SetToolTip(b, tooltipText);
  200. }
  201. if (trimText)
  202. trim(b);
  203. if (parent != null)
  204. parent.Controls.Add(b);
  205. return b;
  206. }
  207. private int getX()
  208. {
  209. return 40;
  210. }
  211. private int getY(int i)
  212. {
  213. return i * 28 + 45;
  214. }
  215. private bool trim(Control control)
  216. {
  217. string txt = control.Text;
  218. if (txt.Length == 0 || control.Width == 0)
  219. return false;
  220. bool trimmed = false;
  221. int i = txt.Length;
  222. int iconWidth = 30;
  223. while (TextRenderer.MeasureText(txt + "...", control.Font).Width > control.Width - iconWidth)
  224. {
  225. txt = control.Text.Substring(0, --i);
  226. trimmed = true;
  227. if (i == 0)
  228. break;
  229. }
  230. control.Text = txt + (trimmed ? "..." : "");
  231. return trimmed;
  232. }
  233. #endregion
  234. }
  235. }