WelcomeForm.cs 8.2 KB

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