DynamicEditorForm.xaml.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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? OnReconfigureEditors;
  39. public event OnAfterEditorValueChanged? OnAfterEditorValueChanged;
  40. public event EditorValueChangedHandler? OnEditorValueChanged;
  41. public event GetDocumentEvent? OnGetDocument;
  42. public event FindDocumentEvent? OnFindDocument;
  43. public event SaveDocumentEvent? OnSaveDocument;
  44. public event OnSelectPage? OnSelectPage;
  45. public event DynamicGridSaveEvent? OnSaveItem;
  46. DynamicEditorPages? Pages { get; }
  47. bool ReadOnly { get; set; }
  48. BaseObject[] Items { get; set; }
  49. void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  50. Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false);
  51. IDynamicEditorControl FindEditor(string columnName);
  52. void SetLayoutType<T>() where T : DynamicEditorGridLayout;
  53. void UnloadEditorPages(bool saved);
  54. }
  55. public class UtilityItem
  56. {
  57. public string Name { get; set; }
  58. public ImageSource Icon { get; set; }
  59. public string Text { get; set; }
  60. public SizeMode Mode { get; set; }
  61. public ICommand Command { get; set; }
  62. }
  63. public class DynamicEditorFormModel
  64. {
  65. /// <summary>
  66. /// Constructor of the UtilityViewModel class.
  67. /// </summary>
  68. public DynamicEditorFormModel()
  69. {
  70. var utilities = new ObservableCollection<UtilityItem>();
  71. utilities.Add(new UtilityItem
  72. { Name = "Help", Icon = Resources.help.AsBitmapImage(), Text = "", Mode = SizeMode.Normal, Command = HelpCommand });
  73. Utilities = utilities;
  74. }
  75. /// <summary>
  76. /// Collection containing the complete details of the items to be bound in the title bar.
  77. /// </summary>
  78. public ObservableCollection<UtilityItem> Utilities { get; }
  79. /// <summary>
  80. /// Commmand for the Help button.
  81. /// </summary>
  82. public DelegateCommand HelpCommand => new(HelpCommandAction);
  83. public static string Slug { get; set; }
  84. /// <summary>
  85. /// Action that is performed when clicking the help button.
  86. /// </summary>
  87. private void HelpCommandAction(object param)
  88. {
  89. Process.Start("https://prs-software.com.au/wiki/index.php/" + Slug);
  90. }
  91. }
  92. /// <summary>
  93. /// Interaction logic for DynamicEditor.xaml
  94. /// </summary>
  95. public partial class DynamicEditorForm : ThemableChromelessWindow, IDynamicEditorForm
  96. {
  97. #region IDynamicEditorForm
  98. public bool ReadOnly { get => Form.ReadOnly; set => Form.ReadOnly = value; }
  99. public BaseObject[] Items { get => Form.Items; set => Form.Items = value; }
  100. public DynamicEditorPages? Pages { get => Form.Pages; }
  101. public event OnValidateData? OnValidateData { add => Form.OnValidateData += value; remove => Form.OnValidateData -= value; }
  102. public event OnCustomiseColumns? OnCustomiseColumns { add => Form.OnCustomiseColumns += value; remove => Form.OnCustomiseColumns -= value; }
  103. public event OnDefineFilter? OnDefineFilter { add => Form.OnDefineFilter += value; remove => Form.OnDefineFilter -= value; }
  104. public event OnDefineLookup? OnDefineLookups { add => Form.OnDefineLookups += value; remove => Form.OnDefineLookups -= value; }
  105. public event OnLookupsDefined? OnLookupsDefined { add => Form.OnLookupsDefined += value; remove => Form.OnLookupsDefined -= value; }
  106. public event DefineEditorEventHandler? OnDefineEditor { add => Form.OnDefineEditor += value; remove => Form.OnDefineEditor -= value; }
  107. public event OnFormCustomiseEditor? OnFormCustomiseEditor { add => Form.OnFormCustomiseEditor += value; remove => Form.OnFormCustomiseEditor -= value; }
  108. public event OnReconfigureEditors? OnReconfigureEditors { add => Form.OnReconfigureEditors += value; remove => Form.OnReconfigureEditors -= value; }
  109. public event OnAfterEditorValueChanged? OnAfterEditorValueChanged { add => Form.OnAfterEditorValueChanged += value; remove => Form.OnAfterEditorValueChanged -= value; }
  110. public event EditorValueChangedHandler? OnEditorValueChanged { add => Form.OnEditorValueChanged += value; remove => Form.OnEditorValueChanged -= value; }
  111. public event IDynamicEditorForm.GetDocumentEvent? OnGetDocument { add => Form.OnGetDocument += value; remove => Form.OnGetDocument -= value; }
  112. public event IDynamicEditorForm.FindDocumentEvent? OnFindDocument { add => Form.OnFindDocument += value; remove => Form.OnFindDocument -= value; }
  113. public event IDynamicEditorForm.SaveDocumentEvent? OnSaveDocument { add => Form.OnSaveDocument += value; remove => Form.OnSaveDocument -= value; }
  114. public event OnSelectPage? OnSelectPage { add => Form.OnSelectPage += value; remove => Form.OnSelectPage -= value; }
  115. public event DynamicGridSaveEvent? OnSaveItem { add => Form.OnSaveItem += value; remove => Form.OnSaveItem -= value; }
  116. public IDynamicEditorControl FindEditor(string columnName) => Form.FindEditor(columnName);
  117. public void UnloadEditorPages(bool saved) => Form.UnloadEditorPages(saved);
  118. #endregion
  119. public DynamicEditorForm()
  120. {
  121. InitializeComponent();
  122. //this.Loaded += new RoutedEventHandler(ConfigureSystemMenu);
  123. Form.OnEditorCreated += Editor_OnEditorCreated;
  124. Form.OnOK += Form_OnOK;
  125. Form.OnCancel += Form_OnCancel;
  126. }
  127. public DynamicEditorForm(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  128. Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false): this()
  129. {
  130. Setup(type, pages, buttons, pageDataHandler, preloadPages);
  131. }
  132. public void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  133. Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false)
  134. {
  135. Form.Setup(type, pages, buttons, pageDataHandler, preloadPages);
  136. }
  137. public void SetLayoutType<T>() where T : DynamicEditorGridLayout => Form.SetLayoutType<T>();
  138. private void Form_OnCancel()
  139. {
  140. DialogResult = false;
  141. }
  142. private void Form_OnOK()
  143. {
  144. DialogResult = true;
  145. }
  146. private void Editor_OnEditorCreated(object sender, double height, double width)
  147. {
  148. var screen = WpfScreen.GetScreenFrom(new Point(Left, Top));
  149. double spareheight = 90;
  150. double sparewidth = 25;
  151. var desiredheight = height;
  152. var desiredwidth = width;
  153. if (Form.Pages != null)
  154. foreach (var page in Form.Pages)
  155. {
  156. if (desiredheight < page.MinimumSize().Height)
  157. desiredheight = page.MinimumSize().Height;
  158. if (desiredwidth < page.MinimumSize().Width)
  159. desiredwidth = page.MinimumSize().Width;
  160. }
  161. desiredheight += spareheight;
  162. desiredwidth += sparewidth;
  163. var maxheight = screen.WorkingArea.Height - 0;
  164. Height = desiredheight > maxheight ? maxheight : desiredheight;
  165. var maxwidth = screen.WorkingArea.Width - 0;
  166. Width = desiredwidth > maxwidth ? maxwidth : desiredwidth;
  167. Left = screen.DeviceBounds.Left + (screen.DeviceBounds.Width - Width) / 2.0F;
  168. Top = screen.DeviceBounds.Top + (screen.DeviceBounds.Height - Height) / 2.0F;
  169. var scaption = Form.Items[0].GetType().GetCaption();
  170. Title = "Edit " + scaption.SplitCamelCase();
  171. }
  172. private void Window_Closing(object sender, CancelEventArgs e)
  173. {
  174. if (DialogResult == true)
  175. Form.SaveItem(e);
  176. }
  177. }
  178. }