DynamicEditorForm.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using System.Diagnostics.CodeAnalysis;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Runtime.InteropServices;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Input;
  13. using System.Windows.Interop;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using InABox.Core;
  17. using InABox.DynamicGrid.Properties;
  18. using InABox.Wpf;
  19. using InABox.WPF;
  20. using Syncfusion.Windows.Shared;
  21. using Syncfusion.Windows.Tools.Controls;
  22. namespace InABox.DynamicGrid
  23. {
  24. public delegate BaseEditor? DefineEditorEventHandler(object item, DynamicGridColumn column);
  25. public delegate void DynamicGridSaveEvent(object sender, CancelEventArgs args);
  26. public interface IDynamicEditorForm
  27. {
  28. public delegate Document? FindDocumentEvent(string FileName);
  29. public delegate Document? GetDocumentEvent(Guid id);
  30. public delegate void SaveDocumentEvent(Document document);
  31. public event OnValidateData? OnValidateData;
  32. public event OnCustomiseColumns? OnCustomiseColumns;
  33. public event OnDefineFilter? OnDefineFilter;
  34. public event OnDefineLookup? OnDefineLookups;
  35. public event OnLookupsDefined? OnLookupsDefined;
  36. public event DefineEditorEventHandler? OnDefineEditor;
  37. public event OnFormCustomiseEditor? OnFormCustomiseEditor;
  38. public event OnReconfigureEditors? ReconfigureEditors;
  39. public event EditorValueChangedHandler? OnEditorValueChanged;
  40. public event GetDocumentEvent? OnGetDocument;
  41. public event FindDocumentEvent? OnFindDocument;
  42. public event SaveDocumentEvent? OnSaveDocument;
  43. public event OnSelectPage? OnSelectPage;
  44. public event DynamicGridSaveEvent? OnSaveItem;
  45. DynamicEditorPages? Pages { get; }
  46. bool ReadOnly { get; set; }
  47. BaseObject[] Items { get; set; }
  48. void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  49. Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false);
  50. IDynamicEditorControl FindEditor(string columnName);
  51. void UnloadEditorPages(bool saved);
  52. }
  53. public class UtilityItem
  54. {
  55. public string Name { get; set; }
  56. public ImageSource Icon { get; set; }
  57. public string Text { get; set; }
  58. public SizeMode Mode { get; set; }
  59. public ICommand Command { get; set; }
  60. }
  61. public class UtilityViewModel
  62. {
  63. /// <summary>
  64. /// Constructor of the UtilityViewModel class.
  65. /// </summary>
  66. public UtilityViewModel()
  67. {
  68. var utilities = new ObservableCollection<UtilityItem>();
  69. utilities.Add(new UtilityItem
  70. { Name = "Help", Icon = Resources.help.AsBitmapImage(), Text = "", Mode = SizeMode.Normal, Command = HelpCommand });
  71. Utilities = utilities;
  72. }
  73. /// <summary>
  74. /// Collection containing the complete details of the items to be bound in the title bar.
  75. /// </summary>
  76. public ObservableCollection<UtilityItem> Utilities { get; }
  77. /// <summary>
  78. /// Commmand for the Help button.
  79. /// </summary>
  80. public DelegateCommand HelpCommand => new(HelpCommandAction);
  81. public static string Slug { get; set; }
  82. /// <summary>
  83. /// Action that is performed when clicking the help button.
  84. /// </summary>
  85. private void HelpCommandAction(object param)
  86. {
  87. Process.Start("https://prs-software.com.au/wiki/index.php/" + Slug);
  88. }
  89. }
  90. /// <summary>
  91. /// Interaction logic for DynamicEditor.xaml
  92. /// </summary>
  93. public partial class DynamicEditorForm : ThemableChromelessWindow, IDynamicEditorForm
  94. {
  95. #region IDynamicEditorForm
  96. public bool ReadOnly { get => Form.ReadOnly; set => Form.ReadOnly = value; }
  97. public BaseObject[] Items { get => Form.Items; set => Form.Items = value; }
  98. public DynamicEditorPages? Pages { get => Form.Pages; }
  99. public event OnValidateData? OnValidateData { add => Form.OnValidateData += value; remove => Form.OnValidateData -= value; }
  100. public event OnCustomiseColumns? OnCustomiseColumns { add => Form.OnCustomiseColumns += value; remove => Form.OnCustomiseColumns -= value; }
  101. public event OnDefineFilter? OnDefineFilter { add => Form.OnDefineFilter += value; remove => Form.OnDefineFilter -= value; }
  102. public event OnDefineLookup? OnDefineLookups { add => Form.OnDefineLookups += value; remove => Form.OnDefineLookups -= value; }
  103. public event OnLookupsDefined? OnLookupsDefined { add => Form.OnLookupsDefined += value; remove => Form.OnLookupsDefined -= value; }
  104. public event DefineEditorEventHandler? OnDefineEditor { add => Form.OnDefineEditor += value; remove => Form.OnDefineEditor -= value; }
  105. public event OnFormCustomiseEditor? OnFormCustomiseEditor { add => Form.OnFormCustomiseEditor += value; remove => Form.OnFormCustomiseEditor -= value; }
  106. public event OnReconfigureEditors? ReconfigureEditors { add => Form.ReconfigureEditors += value; remove => Form.ReconfigureEditors -= value; }
  107. public event EditorValueChangedHandler? OnEditorValueChanged { add => Form.OnEditorValueChanged += value; remove => Form.OnEditorValueChanged -= value; }
  108. public event IDynamicEditorForm.GetDocumentEvent? OnGetDocument { add => Form.OnGetDocument += value; remove => Form.OnGetDocument -= value; }
  109. public event IDynamicEditorForm.FindDocumentEvent? OnFindDocument { add => Form.OnFindDocument += value; remove => Form.OnFindDocument -= value; }
  110. public event IDynamicEditorForm.SaveDocumentEvent? OnSaveDocument { add => Form.OnSaveDocument += value; remove => Form.OnSaveDocument -= value; }
  111. public event OnSelectPage? OnSelectPage { add => Form.OnSelectPage += value; remove => Form.OnSelectPage -= value; }
  112. public event DynamicGridSaveEvent? OnSaveItem { add => Form.OnSaveItem += value; remove => Form.OnSaveItem -= value; }
  113. public IDynamicEditorControl FindEditor(string columnName) => Form.FindEditor(columnName);
  114. public void UnloadEditorPages(bool saved) => Form.UnloadEditorPages(saved);
  115. #endregion
  116. public DynamicEditorForm()
  117. {
  118. InitializeComponent();
  119. //this.Loaded += new RoutedEventHandler(ConfigureSystemMenu);
  120. Form.OnEditorCreated += Editor_OnEditorCreated;
  121. Form.OnOK += Form_OnOK;
  122. Form.OnCancel += Form_OnCancel;
  123. }
  124. public DynamicEditorForm(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  125. Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false): this()
  126. {
  127. Setup(type, pages, buttons, pageDataHandler, preloadPages);
  128. }
  129. public void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  130. Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false)
  131. {
  132. Form.Setup(type, pages, buttons, pageDataHandler, preloadPages);
  133. }
  134. private void Form_OnCancel()
  135. {
  136. DialogResult = false;
  137. }
  138. private void Form_OnOK()
  139. {
  140. DialogResult = true;
  141. }
  142. private void Editor_OnEditorCreated(object sender, double height, double width)
  143. {
  144. var screen = WpfScreen.GetScreenFrom(new Point(Left, Top));
  145. double spareheight = 90;
  146. double sparewidth = 25;
  147. var desiredheight = height + spareheight;
  148. var desiredwidth = width + sparewidth;
  149. if (Form.Pages != null)
  150. foreach (var page in Form.Pages)
  151. {
  152. if (desiredheight < page.MinimumSize().Height)
  153. desiredheight = page.MinimumSize().Height;
  154. if (desiredwidth < page.MinimumSize().Width)
  155. desiredwidth = page.MinimumSize().Width;
  156. }
  157. var maxheight = screen.WorkingArea.Height - 0;
  158. Height = desiredheight > maxheight ? maxheight : desiredheight;
  159. var maxwidth = screen.WorkingArea.Width - 0;
  160. Width = desiredwidth > maxwidth ? maxwidth : desiredwidth;
  161. Left = screen.DeviceBounds.Left + (screen.DeviceBounds.Width - Width) / 2.0F;
  162. Top = screen.DeviceBounds.Top + (screen.DeviceBounds.Height - Height) / 2.0F;
  163. var scaption = Form.Items[0].GetType().GetCaption();
  164. Title = "Edit " + scaption.SplitCamelCase();
  165. if (Form.Editor.IsCustomLayout)
  166. Title += "*";
  167. }
  168. private void Window_Closing(object sender, CancelEventArgs e)
  169. {
  170. if (DialogResult == true)
  171. Form.SaveItem(e);
  172. }
  173. #region Win32 API Stuff
  174. // Define the Win32 API methods we are going to use
  175. [DllImport("user32.dll")]
  176. private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
  177. [DllImport("user32.dll")]
  178. private static extern bool InsertMenu(IntPtr hMenu, int wPosition, int wFlags, int wIDNewItem, string lpNewItem);
  179. /// Define our Constants we will use
  180. public const int WM_SYSCOMMAND = 0x112;
  181. public const int MF_SEPARATOR = 0x800;
  182. public const int MF_BYPOSITION = 0x400;
  183. public const int MF_STRING = 0x0;
  184. public const int _EditLayoutID = 1000;
  185. public const int _ResetLayoutID = 1001;
  186. public IntPtr Handle => new WindowInteropHelper(this).Handle;
  187. private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  188. {
  189. // Check if a System Command has been executed
  190. if (msg == WM_SYSCOMMAND)
  191. // Execute the appropriate code for the System Menu item that was clicked
  192. switch (wParam.ToInt32())
  193. {
  194. case _EditLayoutID:
  195. Form.EditLayout();
  196. handled = true;
  197. break;
  198. case _ResetLayoutID:
  199. if (MessageBox.Show(
  200. "WARNING: This will delete any customisations you have made!\n\nAre you sure you wish to reset this layout?",
  201. "Confirm Layout Reset", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  202. Form.ResetLayout();
  203. handled = true;
  204. break;
  205. }
  206. return IntPtr.Zero;
  207. }
  208. private void ConfigureSystemMenu(object sender, RoutedEventArgs e)
  209. {
  210. /// Get the Handle for the Forms System Menu
  211. var systemMenuHandle = GetSystemMenu(Handle, false);
  212. /// Create our new System Menu items just before the Close menu item
  213. InsertMenu(systemMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty); // <-- Add a menu seperator
  214. InsertMenu(systemMenuHandle, 6, MF_BYPOSITION, _EditLayoutID, "Edit Layout");
  215. InsertMenu(systemMenuHandle, 7, MF_BYPOSITION, _ResetLayoutID, "Reset Layout");
  216. // Attach our WndProc handler to this Window
  217. var source = HwndSource.FromHwnd(Handle);
  218. source.AddHook(WndProc);
  219. }
  220. #endregion
  221. //private void Wiki_Click(object sender, RoutedEventArgs e)
  222. //{
  223. // System.Diagnostics.Process.Start("https://prs-software.com.au/wiki/index.php/" + CurrentPanelSlug());
  224. //}
  225. //private string CurrentPanelSlug()
  226. //{
  227. // if ((Items != null) && Items.Any())
  228. // return Items.First().GetType().EntityName().Split('.').Last();
  229. // return "";
  230. //}
  231. }
  232. }