Config.DesignExt.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. using FastReport.Design;
  2. using System;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Web;
  6. using System.Windows.Forms;
  7. namespace FastReport.Utils
  8. {
  9. partial class Config
  10. {
  11. #region Private Fields
  12. #if MONO
  13. const UIStyle DEFAULT_UISTYLE = UIStyle.Office2003;
  14. #else
  15. private const UIStyle DEFAULT_UISTYLE = UIStyle.VisualStudio2012Light;
  16. #endif
  17. private static DesignerSettings FDesignerSettings = new DesignerSettings();
  18. private static bool FSplashScreenEnabled = false;
  19. private static UIStyle FUIStyle = DEFAULT_UISTYLE;
  20. private static bool FUseRibbon = false;
  21. private static bool processEvents = false;
  22. private static int iconPack = 0;
  23. private static string saveFolder = "";
  24. #if COMMUNITY
  25. private static Image splashScreen;
  26. private static Image welcomeScreen;
  27. #endif
  28. #endregion Private Fields
  29. #region Public Properties
  30. /// <summary>
  31. /// Gets or sets a value indicating that UI library must use high dpi compatible rendering.
  32. /// </summary>
  33. /// <remarks>This flag is false by default. Turn it on at the application start if you need
  34. /// better appearance of custom drawn UI items in high dpi mode. This however may result in
  35. /// wrong appearance on multi-monitor setup.
  36. /// /// </remarks>
  37. public static bool EnableBarHighDpi
  38. {
  39. #if MONO
  40. get; set;
  41. #else
  42. get
  43. {
  44. return DevComponents.DpiHelper.HighDpiEnabled;
  45. }
  46. set
  47. {
  48. if (!DevComponents.DpiHelper.HighDpiEnabled)
  49. DevComponents.DpiHelper.EnableHighDpi();
  50. }
  51. #endif
  52. }
  53. /// <summary>
  54. /// Gets or sets the settings for the report designer window.
  55. /// </summary>
  56. public static DesignerSettings DesignerSettings
  57. {
  58. get { return FDesignerSettings; }
  59. set { FDesignerSettings = value; }
  60. }
  61. /// <summary>
  62. /// Gets or sets the UI style.
  63. /// </summary>
  64. /// <remarks>
  65. /// This property affects both designer and preview windows.
  66. /// </remarks>
  67. public static UIStyle UIStyle
  68. {
  69. get { return FUIStyle; }
  70. set { FUIStyle = value; }
  71. }
  72. /// <summary>
  73. /// Gets or sets color of backlight intersecting objects.
  74. /// </summary>
  75. public static Color BacklightColor
  76. {
  77. get
  78. {
  79. int result;
  80. if (int.TryParse(Root.FindItem("Designer").FindItem("Report").GetProp("BackLightColor"), out result))
  81. return Color.FromArgb(result);
  82. else
  83. return Color.IndianRed;
  84. }
  85. set { Root.FindItem("Designer").FindItem("Report").SetProp("BackLightColor", value.ToArgb().ToString()); }
  86. }
  87. /// <summary>
  88. /// Gets or sets a value indicating whether the Ribbon UI should be used
  89. /// </summary>
  90. public static bool UseRibbon
  91. {
  92. get
  93. {
  94. #if COMMUNITY
  95. return false;
  96. #else
  97. return FUseRibbon;
  98. #endif
  99. }
  100. set { FUseRibbon = value; }
  101. }
  102. /// <summary>
  103. /// Gets or set the current icon pack index. Default is 0 (classic).
  104. /// </summary>
  105. /// <remarks>Set this property at the application start.</remarks>
  106. public static int IconPack
  107. {
  108. get { return iconPack; }
  109. set
  110. {
  111. if (value < 0)
  112. value = 0;
  113. // increase the number when you add new packs.
  114. int number_of_packs = 2;
  115. if (value >= number_of_packs)
  116. value = number_of_packs - 1;
  117. iconPack = value;
  118. }
  119. }
  120. /// <summary>
  121. /// Gets or sets a value indicating whether SplashScreen should be displayed while loading designer
  122. /// </summary>
  123. public static bool SplashScreenEnabled
  124. {
  125. get { return FSplashScreenEnabled; }
  126. set { FSplashScreenEnabled = value; }
  127. }
  128. /// <summary>
  129. /// Gets or sets a value indicating whether Welcome window feature enabled.
  130. /// If false, interface elements associated with the Welcome window will not be visible.
  131. /// </summary>
  132. public static bool WelcomeEnabled
  133. {
  134. get { return Root.FindItem("Designer").FindItem("Welcome").GetProp("Enabled") != "False"; }
  135. set { Root.FindItem("Designer").FindItem("Welcome").SetProp("Enabled", Converter.ToString(value)); }
  136. }
  137. /// <summary>
  138. /// Gets or sets a value indicating whether Welcome window shoud be displayed on startup
  139. /// </summary>
  140. public static bool WelcomeShowOnStartup
  141. {
  142. get { return Root.FindItem("Designer").FindItem("Welcome").GetProp("ShowOnStartup") != "False"; }
  143. set { Root.FindItem("Designer").FindItem("Welcome").SetProp("ShowOnStartup", Converter.ToString(value)); }
  144. }
  145. /// <summary>
  146. /// Gets the folder to store auto save files
  147. /// </summary>
  148. public static string AutoSaveFolder
  149. {
  150. get { return Path.Combine(GetTempFolder(), "FastReport"); }
  151. }
  152. /// <summary>
  153. /// Gets or sets the default folder for SaveFileDialog.
  154. /// </summary>
  155. public static string SaveFolder
  156. {
  157. get { return saveFolder; }
  158. set { saveFolder = value; }
  159. }
  160. /// <summary>
  161. /// Gets the autosaved report
  162. /// </summary>
  163. public static string AutoSaveFile
  164. {
  165. get { return Path.Combine(AutoSaveFolder, "autosave.frx"); }
  166. }
  167. /// <summary>
  168. /// Gets the autosaved report path
  169. /// </summary>
  170. public static string AutoSaveFileName
  171. {
  172. get { return Path.Combine(AutoSaveFolder, "autosave.txt"); }
  173. }
  174. /// <summary>
  175. /// Is necessary to process abort and some other events in parallel
  176. /// </summary>
  177. public static bool ProcessEvents
  178. {
  179. get { return processEvents; }
  180. set { processEvents = value; }
  181. }
  182. /// <summary>
  183. /// Gets a value indicating that the ASP.NET hosting permission level is set to full trust.
  184. /// </summary>
  185. public static bool FullTrust
  186. {
  187. get
  188. {
  189. return GetCurrentTrustLevel() == AspNetHostingPermissionLevel.Unrestricted;
  190. }
  191. }
  192. /// <summary>
  193. /// Gets or sets a value that determines whether to disable some functionality to run in web mode.
  194. /// </summary>
  195. /// <remarks>
  196. /// Use this property if you use FastReport in ASP.Net. Set this property to <b>true</b> <b>before</b>
  197. /// you access any FastReport.Net objects.
  198. /// </remarks>
  199. public static bool WebMode
  200. {
  201. get
  202. {
  203. return FWebMode;
  204. }
  205. set
  206. {
  207. FWebMode = value;
  208. if (value)
  209. ReportSettings.ShowProgress = false;
  210. }
  211. }
  212. #endregion Public Properties
  213. #region Public Methods
  214. /// <summary>
  215. /// Restores the form state from the configuration file.
  216. /// </summary>
  217. /// <param name="form">The form to restore.</param>
  218. public static void RestoreFormState(Form form)
  219. {
  220. RestoreFormState(form, false);
  221. }
  222. /// <summary>
  223. /// Saves the form state to the configuration file.
  224. /// </summary>
  225. /// <param name="form">The form to save.</param>
  226. public static void SaveFormState(Form form)
  227. {
  228. SaveFormState(form.Name, form.WindowState == FormWindowState.Maximized, form.WindowState == FormWindowState.Minimized,
  229. form.Location, form.Size);
  230. }
  231. /// <summary>
  232. /// Saves the form state to the configuration file.
  233. /// </summary>
  234. /// <param name="formName">The name of the form.</param>
  235. /// <param name="isMaximized">True if the form is in maximized state.</param>
  236. /// <param name="isMinimized">True if the form is in minimized state.</param>
  237. /// <param name="location">The location of the form.</param>
  238. /// <param name="size">The size of the form.</param>
  239. public static void SaveFormState(String formName, bool isMaximized, bool isMinimized, Point location, Size size)
  240. {
  241. XmlItem xi = FDoc.Root.FindItem("Forms").FindItem(formName);
  242. xi.SetProp("Maximized", isMaximized ? "1" : "0");
  243. xi.SetProp("Left", isMinimized ? "0" : location.X.ToString());
  244. xi.SetProp("Top", isMinimized ? "0" : location.Y.ToString());
  245. xi.SetProp("Width", size.Width.ToString());
  246. xi.SetProp("Height", size.Height.ToString());
  247. }
  248. #endregion Public Methods
  249. #region Internal Methods
  250. // we need this to prevent form.Load event to be fired *after* the form is displayed.
  251. // Used in the StandardDesignerForm.Load event
  252. internal static bool RestoreFormState(Form form, bool ignoreWindowState)
  253. {
  254. XmlItem xi = FDoc.Root.FindItem("Forms").FindItem(form.Name);
  255. string left = xi.GetProp("Left");
  256. string top = xi.GetProp("Top");
  257. string width = xi.GetProp("Width");
  258. string height = xi.GetProp("Height");
  259. if (left == "")
  260. {
  261. // there is no state information yet, display the form as it was designed
  262. // (usually centered to screen).
  263. return true;
  264. }
  265. // Get current screen working area
  266. Rectangle screenWorkingArea = Screen.GetWorkingArea(form);
  267. int windowLeftPosition = screenWorkingArea.Left;
  268. int windowTopPosition = screenWorkingArea.Top;
  269. int windowWidth = screenWorkingArea.Width;
  270. int windowHeight = screenWorkingArea.Height;
  271. // Get saved left and top positions
  272. if (left != "" && top != "")
  273. {
  274. windowLeftPosition = int.Parse(left);
  275. windowTopPosition = int.Parse(top);
  276. form.Location = new Point(windowLeftPosition, windowTopPosition);
  277. }
  278. // Get saved width and height
  279. if (width != "" && height != "")
  280. {
  281. windowWidth = int.Parse(width);
  282. windowHeight = int.Parse(height);
  283. form.Size = new Size(windowWidth, windowHeight);
  284. }
  285. Rectangle formRect = new Rectangle(windowLeftPosition, windowTopPosition,
  286. windowWidth, windowHeight);
  287. // Check a visibility of form rectangle on any screen
  288. if (!IsVisibleOnAnyScreen(formRect))
  289. {
  290. form.StartPosition = FormStartPosition.WindowsDefaultLocation;
  291. form.Location = new Point(screenWorkingArea.Left, screenWorkingArea.Top);
  292. }
  293. form.StartPosition = FormStartPosition.Manual;
  294. // Set the window state
  295. if (!ignoreWindowState)
  296. form.WindowState = xi.GetProp("Maximized") == "1" ?
  297. FormWindowState.Maximized : FormWindowState.Normal;
  298. return xi.GetProp("Maximized") == "1";
  299. }
  300. internal static void DoEvent()
  301. {
  302. if (ProcessEvents && !WebMode)
  303. {
  304. System.Windows.Forms.Application.DoEvents();
  305. }
  306. }
  307. #endregion Internal Methods
  308. #region Private Methods
  309. /// <summary>
  310. /// Save default <see cref="Auth.AuthService"/> settings and user tokens.
  311. /// Is user have to consent to save data, clear the data in the config file.
  312. /// </summary>
  313. private static void SaveAuthServiceUser()
  314. {
  315. XmlItem xi = FDoc.Root.FindItem("Auth").FindItem("User");
  316. Auth.AuthService auth = Auth.AuthService.Instance;
  317. if (auth.IsEnable && auth.CanRefresh)
  318. {
  319. xi.ClearProps();
  320. xi.Clear();
  321. if (auth.User.RefreshToken != null)
  322. xi.SetProp("refresh_token", auth.User.RefreshToken);
  323. if (auth.User.IdToken != null)
  324. xi.SetProp("id_token", auth.User.IdToken);
  325. if (auth.User.Token != null)
  326. xi.SetProp("access_token", auth.User.Token);
  327. }
  328. else
  329. {
  330. xi.ClearProps();
  331. xi.Clear();
  332. }
  333. }
  334. /// <summary>
  335. /// Save default <see cref="Auth.AuthService"/> settings and user tokens.
  336. /// By default internal use only, it is able to be a public
  337. /// </summary>
  338. private static void RestoreAuthServiceUser()
  339. {
  340. XmlItem xi = FDoc.Root.FindItem("Auth").FindItem("User");
  341. Auth.AuthService auth = Auth.AuthService.Instance;
  342. if (auth.IsEnable)
  343. {
  344. auth.User.Reset();
  345. auth.User.IdToken = xi.GetProp("id_token");
  346. auth.User.Token = xi.GetProp("access_token");
  347. auth.User.RefreshToken = xi.GetProp("refresh_token");
  348. bool isProgramStart = true;
  349. try
  350. {
  351. auth.ParseTokens(isProgramStart);
  352. }
  353. catch
  354. #if DEBUG
  355. (Exception ex)
  356. #endif
  357. {
  358. auth.User.Reset();
  359. }
  360. }
  361. }
  362. /// <summary>
  363. /// Checks the visibility of rectangle area on currently connected screens with small gap.
  364. /// </summary>
  365. /// <param name="rect">Rectanle area for checking.</param>
  366. /// <returns>True for visible rect.</returns>
  367. private static bool IsVisibleOnAnyScreen(Rectangle rect)
  368. {
  369. Rectangle formRect = new Rectangle(rect.Left + 20, rect.Top + 20, rect.Width - 40, rect.Height - 40);
  370. foreach (Screen screen in Screen.AllScreens)
  371. {
  372. if (screen.WorkingArea.IntersectsWith(formRect))
  373. {
  374. return true;
  375. }
  376. }
  377. return false;
  378. }
  379. private static AspNetHostingPermissionLevel GetCurrentTrustLevel()
  380. {
  381. foreach (AspNetHostingPermissionLevel trustLevel in
  382. new AspNetHostingPermissionLevel[] {
  383. AspNetHostingPermissionLevel.Unrestricted,
  384. AspNetHostingPermissionLevel.High,
  385. AspNetHostingPermissionLevel.Medium,
  386. AspNetHostingPermissionLevel.Low,
  387. AspNetHostingPermissionLevel.Minimal
  388. })
  389. {
  390. try
  391. {
  392. new AspNetHostingPermission(trustLevel).Demand();
  393. }
  394. catch (System.Security.SecurityException)
  395. {
  396. continue;
  397. }
  398. return trustLevel;
  399. }
  400. return AspNetHostingPermissionLevel.None;
  401. }
  402. private static void SaveUIStyle()
  403. {
  404. XmlItem xi = Root.FindItem("UIStyleNew");
  405. xi.SetProp("Style", Converter.ToString(UIStyle));
  406. xi.SetProp("Ribbon", Converter.ToString(UseRibbon));
  407. xi.SetProp("IconPack", IconPack.ToString());
  408. }
  409. private static void RestoreUIStyle()
  410. {
  411. XmlItem xi = Root.FindItem("UIStyleNew");
  412. bool firstStart = true;
  413. string style = xi.GetProp("Style");
  414. if (!String.IsNullOrEmpty(style))
  415. {
  416. firstStart = false;
  417. try
  418. {
  419. UIStyle = (UIStyle)Converter.FromString(typeof(UIStyle), style);
  420. }
  421. catch
  422. {
  423. UIStyle = DEFAULT_UISTYLE;
  424. }
  425. }
  426. string ribbon = xi.GetProp("Ribbon");
  427. if (!String.IsNullOrEmpty(ribbon))
  428. {
  429. FUseRibbon = ribbon != "False";
  430. }
  431. if (firstStart)
  432. iconPack = 1;
  433. string iconpack = xi.GetProp("IconPack");
  434. if (!String.IsNullOrEmpty(iconpack))
  435. {
  436. int.TryParse(iconpack, out iconPack);
  437. }
  438. }
  439. #if !COMMUNITY
  440. private static void SaveExportOptions()
  441. {
  442. ExportsOptions.GetInstance().SaveExportOptions();
  443. }
  444. private static void RestoreExportOptions()
  445. {
  446. ExportsOptions.GetInstance().RestoreExportOptions();
  447. }
  448. #endif
  449. #if COMMUNITY
  450. public static Image SplashScreen
  451. {
  452. get
  453. {
  454. return splashScreen;
  455. }
  456. set
  457. {
  458. splashScreen = value;
  459. }
  460. }
  461. public static Image WelcomeScreen
  462. {
  463. get
  464. {
  465. return welcomeScreen;
  466. }
  467. set
  468. {
  469. welcomeScreen = value;
  470. }
  471. }
  472. #endif
  473. #endregion Private Methods
  474. }
  475. }