DynamicEditorForm.xaml.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Windows;
  6. using InABox.Core;
  7. using InABox.Wpf;
  8. using InABox.WPF;
  9. using Syncfusion.Windows.Shared;
  10. using Syncfusion.Windows.Tools.Controls;
  11. namespace InABox.DynamicGrid
  12. {
  13. public class DynamicEditorFormModel
  14. {
  15. /// <summary>
  16. /// Constructor of the UtilityViewModel class.
  17. /// </summary>
  18. public DynamicEditorFormModel()
  19. {
  20. var utilities = new ObservableCollection<UtilityItem>();
  21. utilities.Add(new UtilityItem
  22. { Name = "Help", Icon = Resources.help.AsBitmapImage(), Text = "", Mode = SizeMode.Normal, Command = HelpCommand });
  23. Utilities = utilities;
  24. }
  25. /// <summary>
  26. /// Collection containing the complete details of the items to be bound in the title bar.
  27. /// </summary>
  28. public ObservableCollection<UtilityItem> Utilities { get; }
  29. /// <summary>
  30. /// Commmand for the Help button.
  31. /// </summary>
  32. public DelegateCommand HelpCommand => new(HelpCommandAction);
  33. public static string Slug { get; set; }
  34. /// <summary>
  35. /// Action that is performed when clicking the help button.
  36. /// </summary>
  37. private void HelpCommandAction(object param)
  38. {
  39. Process.Start("https://prsdigital.com.au/wiki/index.php/" + Slug);
  40. }
  41. }
  42. /// <summary>
  43. /// Interaction logic for DynamicEditor.xaml
  44. /// </summary>
  45. public partial class DynamicEditorForm : ThemableChromelessWindow, IDynamicEditorForm
  46. {
  47. #region IDynamicEditorForm
  48. public bool ReadOnly { get => Form.ReadOnly; set => Form.ReadOnly = value; }
  49. public BaseObject[] Items { get => Form.Items; set => Form.Items = value; }
  50. public DynamicEditorPages? Pages { get => Form.Pages; }
  51. public event OnBeforeLoad? OnBeforeLoad;
  52. public void BeforeLoad() => OnBeforeLoad?.Invoke(this);
  53. public event OnAfterLoad? OnAfterLoad;
  54. public void AfterLoad() => OnAfterLoad?.Invoke(this);
  55. public event OnValidateData? OnValidateData { add => Form.OnValidateData += value; remove => Form.OnValidateData -= value; }
  56. public OnCustomiseColumns? OnCustomiseColumns
  57. {
  58. get => Form.OnCustomiseColumns;
  59. set => Form.OnCustomiseColumns = value;
  60. }
  61. public OnDefineLookupFilter? OnDefineFilter { get => Form.OnDefineFilter; set { Form.OnDefineFilter = value; } }
  62. public OnDefineLookup? OnDefineLookups { get => Form.OnDefineLookups; set { Form.OnDefineLookups = value; } }
  63. public DefineEditorEventHandler? OnDefineEditor { get => Form.OnDefineEditor; set { Form.OnDefineEditor = value; } }
  64. public event OnFormCustomiseEditor? OnFormCustomiseEditor;
  65. public OnReconfigureEditors? OnReconfigureEditors { get => Form.OnReconfigureEditors; set { Form.OnReconfigureEditors = value; } }
  66. public event OnAfterEditorValueChanged? OnAfterEditorValueChanged { add => Form.OnAfterEditorValueChanged += value; remove => Form.OnAfterEditorValueChanged -= value; }
  67. public event EditorValueChangedHandler? OnEditorValueChanged { add => Form.OnEditorValueChanged += value; remove => Form.OnEditorValueChanged -= value; }
  68. public event OnSelectPage? OnSelectPage { add => Form.OnSelectPage += value; remove => Form.OnSelectPage -= value; }
  69. public DynamicGridSaveEvent? OnSaveItem { get => Form.OnSaveItem; set { Form.OnSaveItem = value; } }
  70. public IDynamicEditorControl FindEditor(string columnName) => Form.FindEditor(columnName);
  71. public object? GetEditorValue(string columnName) => Form.GetEditorValue(columnName);
  72. public void SetEditorValue(string columnName, object? value) => Form.SetEditorValue(columnName, value);
  73. public void UnloadEditorPages(bool saved) => Form.UnloadEditorPages(saved);
  74. #endregion
  75. public DynamicEditorForm()
  76. {
  77. InitializeComponent();
  78. //this.Loaded += new RoutedEventHandler(ConfigureSystemMenu);
  79. Form.OnEditorCreated += Editor_OnEditorCreated;
  80. Form.OnOK += Form_OnOK;
  81. Form.OnCancel += Form_OnCancel;
  82. Form.OnFormCustomiseEditor += (sender, items, column, editor) => OnFormCustomiseEditor?.Invoke(sender, items, column, editor);
  83. }
  84. public DynamicEditorForm(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  85. Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false): this()
  86. {
  87. Setup(type, pages, buttons, pageDataHandler, preloadPages);
  88. }
  89. public void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  90. Func<Type, CoreTable?>? pageDataHandler = null, bool preloadPages = false)
  91. {
  92. Form.Setup(type, pages, buttons, pageDataHandler, preloadPages);
  93. }
  94. public void SetLayoutType<T>() where T : DynamicEditorGridLayout => Form.SetLayoutType<T>();
  95. private void Form_OnCancel()
  96. {
  97. if (bChanged)
  98. {
  99. var result = MessageWindow.ShowYesNoCancel("Save Changes?", "Confirm");
  100. switch (result)
  101. {
  102. case MessageWindowResult.Yes:
  103. DialogResult = true;
  104. break;
  105. case MessageWindowResult.No:
  106. DialogResult = false;
  107. break;
  108. }
  109. }
  110. else
  111. DialogResult = false;
  112. }
  113. private void Form_OnOK()
  114. {
  115. DialogResult = true;
  116. }
  117. private void Editor_OnEditorCreated(object sender, double height, double width)
  118. {
  119. var screen = WpfScreen.GetScreenFrom(new Point(Left, Top));
  120. double spareheight = 90;
  121. double sparewidth = 25;
  122. var desiredheight = height;
  123. var desiredwidth = width;
  124. if (Form.Pages != null)
  125. foreach (var page in Form.Pages)
  126. {
  127. if (desiredheight < page.MinimumSize().Height)
  128. desiredheight = page.MinimumSize().Height;
  129. if (desiredwidth < page.MinimumSize().Width)
  130. desiredwidth = page.MinimumSize().Width;
  131. }
  132. desiredheight += spareheight;
  133. desiredwidth += sparewidth;
  134. var maxheight = screen.WorkingArea.Height - 0;
  135. Height = desiredheight > maxheight ? maxheight : desiredheight;
  136. var maxwidth = screen.WorkingArea.Width - 0;
  137. Width = desiredwidth > maxwidth ? maxwidth : desiredwidth;
  138. Left = screen.DeviceBounds.Left + (screen.DeviceBounds.Width - Width) / 2.0F;
  139. Top = screen.DeviceBounds.Top + (screen.DeviceBounds.Height - Height) / 2.0F;
  140. var scaption = Form.Items[0].GetType().GetCaption();
  141. Title = "Edit " + scaption.SplitCamelCase();
  142. }
  143. private void Window_Closing(object sender, CancelEventArgs e)
  144. {
  145. if (DialogResult == true)
  146. Form.SaveItem(e);
  147. }
  148. private bool bChanged = false;
  149. private void Form_OnOnChanged(object? sender, EventArgs e)
  150. {
  151. bChanged = true;
  152. }
  153. }
  154. }