DynamicEditorForm.xaml.cs 9.6 KB

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