SecondaryWindow.xaml.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Configuration;
  12. using InABox.Core;
  13. using InABox.DynamicGrid;
  14. using InABox.Reports;
  15. using InABox.Core.Reports;
  16. using InABox.Scripting;
  17. using InABox.Wpf.Reports;
  18. using InABox.WPF;
  19. using InABox.Wpf;
  20. using PRSDesktop.Configuration;
  21. using Fluent;
  22. using Button = Fluent.Button;
  23. using ContextMenu = System.Windows.Controls.ContextMenu;
  24. using System.Collections.Generic;
  25. using System.Collections.ObjectModel;
  26. using MenuItem = Fluent.MenuItem;
  27. namespace PRSDesktop;
  28. /// <summary>
  29. /// Interaction logic for SecondaryWindow.xaml
  30. /// </summary>
  31. public partial class SecondaryWindow : RibbonWindow, IPanelHostControl
  32. {
  33. private readonly Guid _id = Guid.Empty;
  34. private readonly string _modulesection = "";
  35. private readonly string _type = "";
  36. private bool bLoaded;
  37. private List<Control> CurrentModules = new List<Control>();
  38. public IBasePanel Panel { get; private set; }
  39. private PanelHost PanelHost;
  40. public SecondaryWindow(Guid id, string type, string modulesection, double left, double top, double width, double height)
  41. {
  42. PanelHost = new PanelHost(this);
  43. _id = id;
  44. _modulesection = modulesection;
  45. InitializeComponent();
  46. Left = left;
  47. Top = top;
  48. Width = width;
  49. Height = height;
  50. Title = string.Format("{0} - PRS Desktop (Release {1})", modulesection, CoreUtils.GetVersion());
  51. SecondaryTab.Header = modulesection;
  52. Panel = PanelHost.LoadPanel(Type.GetType(type), modulesection);
  53. ContentBorder.Child = Panel as UIElement;
  54. }
  55. private void Window_Loaded(object sender, RoutedEventArgs e)
  56. {
  57. PanelHost.Refresh();
  58. bLoaded = true;
  59. }
  60. private void RefreshMenu_Click(object sender, RoutedEventArgs e)
  61. {
  62. PanelHost.Refresh();
  63. }
  64. private void Wiki_Click(object sender, RoutedEventArgs e)
  65. {
  66. Process.Start("https://prsdigital.com.au/wiki/index.php/" + _type.Replace(" ", "_").Replace("/", ""));
  67. }
  68. #region Save / Load Window Location
  69. private void SaveSettings()
  70. {
  71. if (App.IsClosing)
  72. return;
  73. var tuple = new Tuple<string, string, double, double, double, double>(
  74. Panel.GetType().EntityName(),
  75. _modulesection,
  76. Left,
  77. Top,
  78. Width,
  79. Height
  80. );
  81. App.DatabaseSettings.SecondaryWindows[_id] = tuple;
  82. new LocalConfiguration<DatabaseSettings>(App.Profile).Save(App.DatabaseSettings);
  83. }
  84. private void Window_Closing(object sender, CancelEventArgs e)
  85. {
  86. PanelHost.UnloadPanel(e);
  87. if (e.Cancel) return;
  88. if (App.IsClosing)
  89. return;
  90. App.DatabaseSettings.SecondaryWindows.Remove(_id);
  91. new LocalConfiguration<DatabaseSettings>(App.Profile).Save(App.DatabaseSettings);
  92. }
  93. private void Window_LocationChanged(object sender, EventArgs e)
  94. {
  95. if (bLoaded)
  96. SaveSettings();
  97. }
  98. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  99. {
  100. if (bLoaded)
  101. SaveSettings();
  102. }
  103. #endregion
  104. public void CreatePanelAction(PanelAction action)
  105. {
  106. if (action.OnPopulate != null)
  107. {
  108. Fluent.DropDownButton button = null;
  109. if (action.OnExecute != null)
  110. {
  111. button = new Fluent.SplitButton();
  112. ((Fluent.SplitButton)button).Click += (o, e) => action.Execute();
  113. }
  114. else
  115. button = new Fluent.DropDownButton();
  116. button.MinWidth = 60;
  117. button.DataContext = action;
  118. button.DropDownOpened += (sender, args) => { button.ItemsSource = CreateMenuItems(action); };
  119. button.Bind<PanelAction, String>(Fluent.DropDownButton.HeaderProperty, x => x.Caption, null);
  120. button.Bind<PanelAction, Bitmap?>(Fluent.DropDownButton.LargeIconProperty, x => x.Image,
  121. new BitmapToBitmapImageConverter());
  122. button.Bind<PanelAction, ContextMenu?>(ContextMenuProperty, x => x.Menu);
  123. button.Bind<PanelAction, bool>(IsEnabledProperty, x => x.IsEnabled);
  124. button.Bind<PanelAction, Visibility>(VisibilityProperty, x => x.Visibility);
  125. Actions.Items.Add(button);
  126. CurrentModules.Add(button);
  127. }
  128. else
  129. {
  130. var button = new Fluent.Button()
  131. {
  132. MinWidth = 60,
  133. DataContext = action
  134. };
  135. button.Bind<PanelAction, String>(Fluent.Button.HeaderProperty, x => x.Caption, null);
  136. button.Bind<PanelAction, Bitmap?>(Fluent.Button.LargeIconProperty, x => x.Image,
  137. new BitmapToBitmapImageConverter());
  138. button.Bind<PanelAction, ContextMenu?>(Fluent.Button.ContextMenuProperty, x => x.Menu);
  139. button.Bind<PanelAction, bool>(Fluent.Button.IsEnabledProperty, x => x.IsEnabled);
  140. button.Bind<PanelAction, Visibility>(Fluent.Button.VisibilityProperty, x => x.Visibility);
  141. button.Click += (o, e) => action.Execute();
  142. Actions.Items.Add(button);
  143. CurrentModules.Add(button);
  144. }
  145. ActionsSeparator.Visibility = Visibility.Visible;
  146. }
  147. private static ObservableCollection<MenuItem> CreateMenuItems(PanelAction action)
  148. {
  149. var items = new ObservableCollection<MenuItem>();
  150. var entries = action.Populate();
  151. if (entries != null)
  152. {
  153. foreach (var entry in entries)
  154. {
  155. var item = new Fluent.MenuItem()
  156. {
  157. Header = entry.Caption,
  158. DataContext = entry.Data,
  159. Icon = entry.Image?.AsBitmapImage()
  160. };
  161. item.Click += (o, eventArgs) => entry.Execute();
  162. items.Add(item);
  163. }
  164. }
  165. return items;
  166. }
  167. public void CreateReport(PanelAction action)
  168. {
  169. Print.Visibility = Visibility.Visible;
  170. var button = new Button
  171. {
  172. Header = action.Caption,
  173. LargeIcon = action.Image.AsBitmapImage()
  174. };
  175. if (action.Menu is not null)
  176. {
  177. button.ContextMenu = action.Menu;
  178. }
  179. button.Click += (o, e) => action.Execute();
  180. Print.Items.Add(button);
  181. }
  182. private void Setup_Click(object sender, RoutedEventArgs e)
  183. {
  184. var menu = new ContextMenu();
  185. PanelHost.InitialiseSetupMenu(menu);
  186. menu.IsOpen = true;
  187. }
  188. public void ClearActions()
  189. {
  190. foreach (var module in CurrentModules)
  191. {
  192. if (module.Parent is Fluent.RibbonGroupBox bar)
  193. bar.Items.Remove(module);
  194. }
  195. CurrentModules.Clear();
  196. ActionsSeparator.Visibility = Visibility.Collapsed;
  197. }
  198. public void ClearReports()
  199. {
  200. Print.Items.Clear();
  201. Print.Visibility = Visibility.Visible;
  202. }
  203. public void Heartbeat()
  204. {
  205. PanelHost.Heartbeat();
  206. }
  207. }