DynamicEditorForm.xaml.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 DefineEditorEventHandler? OnDefineEditor;
  35. public event OnFormCustomiseEditor? OnFormCustomiseEditor;
  36. public event OnReconfigureEditors? OnReconfigureEditors;
  37. public event OnAfterEditorValueChanged? OnAfterEditorValueChanged;
  38. public event EditorValueChangedHandler? OnEditorValueChanged;
  39. public event GetDocumentEvent? OnGetDocument;
  40. public event FindDocumentEvent? OnFindDocument;
  41. public event SaveDocumentEvent? OnSaveDocument;
  42. public event OnSelectPage? OnSelectPage;
  43. public event DynamicGridSaveEvent? OnSaveItem;
  44. DynamicEditorPages? Pages { get; }
  45. bool ReadOnly { get; set; }
  46. BaseObject[] Items { get; set; }
  47. void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  48. Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false);
  49. IDynamicEditorControl FindEditor(string columnName);
  50. object? GetEditorValue(string columnName);
  51. void SetEditorValue(string columnName, object? value);
  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://prsdigital.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 DefineEditorEventHandler? OnDefineEditor { add => Form.OnDefineEditor += value; remove => Form.OnDefineEditor -= value; }
  106. public event OnFormCustomiseEditor? OnFormCustomiseEditor { add => Form.OnFormCustomiseEditor += value; remove => Form.OnFormCustomiseEditor -= value; }
  107. public event OnReconfigureEditors? OnReconfigureEditors { add => Form.OnReconfigureEditors += value; remove => Form.OnReconfigureEditors -= value; }
  108. public event OnAfterEditorValueChanged? OnAfterEditorValueChanged { add => Form.OnAfterEditorValueChanged += value; remove => Form.OnAfterEditorValueChanged -= value; }
  109. public event EditorValueChangedHandler? OnEditorValueChanged { add => Form.OnEditorValueChanged += value; remove => Form.OnEditorValueChanged -= value; }
  110. public event IDynamicEditorForm.GetDocumentEvent? OnGetDocument { add => Form.OnGetDocument += value; remove => Form.OnGetDocument -= value; }
  111. public event IDynamicEditorForm.FindDocumentEvent? OnFindDocument { add => Form.OnFindDocument += value; remove => Form.OnFindDocument -= value; }
  112. public event IDynamicEditorForm.SaveDocumentEvent? OnSaveDocument { add => Form.OnSaveDocument += value; remove => Form.OnSaveDocument -= value; }
  113. public event OnSelectPage? OnSelectPage { add => Form.OnSelectPage += value; remove => Form.OnSelectPage -= value; }
  114. public event DynamicGridSaveEvent? OnSaveItem { add => Form.OnSaveItem += value; remove => Form.OnSaveItem -= value; }
  115. public IDynamicEditorControl FindEditor(string columnName) => Form.FindEditor(columnName);
  116. public object? GetEditorValue(string columnName) => Form.GetEditorValue(columnName);
  117. public void SetEditorValue(string columnName, object? value) => Form.SetEditorValue(columnName, value);
  118. public void UnloadEditorPages(bool saved) => Form.UnloadEditorPages(saved);
  119. #endregion
  120. public DynamicEditorForm()
  121. {
  122. InitializeComponent();
  123. //this.Loaded += new RoutedEventHandler(ConfigureSystemMenu);
  124. Form.OnEditorCreated += Editor_OnEditorCreated;
  125. Form.OnOK += Form_OnOK;
  126. Form.OnCancel += Form_OnCancel;
  127. }
  128. public DynamicEditorForm(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  129. Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false): this()
  130. {
  131. Setup(type, pages, buttons, pageDataHandler, preloadPages);
  132. }
  133. public void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  134. Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false)
  135. {
  136. Form.Setup(type, pages, buttons, pageDataHandler, preloadPages);
  137. }
  138. public void SetLayoutType<T>() where T : DynamicEditorGridLayout => Form.SetLayoutType<T>();
  139. private void Form_OnCancel()
  140. {
  141. DialogResult = false;
  142. }
  143. private void Form_OnOK()
  144. {
  145. DialogResult = true;
  146. }
  147. private void Editor_OnEditorCreated(object sender, double height, double width)
  148. {
  149. var screen = WpfScreen.GetScreenFrom(new Point(Left, Top));
  150. double spareheight = 90;
  151. double sparewidth = 25;
  152. var desiredheight = height;
  153. var desiredwidth = width;
  154. if (Form.Pages != null)
  155. foreach (var page in Form.Pages)
  156. {
  157. if (desiredheight < page.MinimumSize().Height)
  158. desiredheight = page.MinimumSize().Height;
  159. if (desiredwidth < page.MinimumSize().Width)
  160. desiredwidth = page.MinimumSize().Width;
  161. }
  162. desiredheight += spareheight;
  163. desiredwidth += sparewidth;
  164. var maxheight = screen.WorkingArea.Height - 0;
  165. Height = desiredheight > maxheight ? maxheight : desiredheight;
  166. var maxwidth = screen.WorkingArea.Width - 0;
  167. Width = desiredwidth > maxwidth ? maxwidth : desiredwidth;
  168. Left = screen.DeviceBounds.Left + (screen.DeviceBounds.Width - Width) / 2.0F;
  169. Top = screen.DeviceBounds.Top + (screen.DeviceBounds.Height - Height) / 2.0F;
  170. var scaption = Form.Items[0].GetType().GetCaption();
  171. Title = "Edit " + scaption.SplitCamelCase();
  172. }
  173. private void Window_Closing(object sender, CancelEventArgs e)
  174. {
  175. if (DialogResult == true)
  176. Form.SaveItem(e);
  177. }
  178. }
  179. }