DynamicEditorForm.xaml.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. public class DynamicEditorFormModel
  13. {
  14. /// <summary>
  15. /// Constructor of the UtilityViewModel class.
  16. /// </summary>
  17. public DynamicEditorFormModel()
  18. {
  19. var utilities = new ObservableCollection<UtilityItem>();
  20. utilities.Add(new UtilityItem
  21. { Name = "Help", Icon = Resources.help.AsBitmapImage(), Text = "", Mode = SizeMode.Normal, Command = HelpCommand });
  22. Utilities = utilities;
  23. }
  24. /// <summary>
  25. /// Collection containing the complete details of the items to be bound in the title bar.
  26. /// </summary>
  27. public ObservableCollection<UtilityItem> Utilities { get; }
  28. /// <summary>
  29. /// Commmand for the Help button.
  30. /// </summary>
  31. public DelegateCommand HelpCommand => new(HelpCommandAction);
  32. public static string Slug { get; set; }
  33. /// <summary>
  34. /// Action that is performed when clicking the help button.
  35. /// </summary>
  36. private void HelpCommandAction(object param)
  37. {
  38. Process.Start("https://prsdigital.com.au/wiki/index.php/" + Slug);
  39. }
  40. }
  41. /// <summary>
  42. /// Interaction logic for DynamicEditor.xaml
  43. /// </summary>
  44. public partial class DynamicEditorForm : ThemableChromelessWindow, IDynamicEditorForm, ISubPanel
  45. {
  46. #region IDynamicEditorForm
  47. public bool ReadOnly { get => Form.ReadOnly; set => Form.ReadOnly = value; }
  48. public BaseObject[] Items { get => Form.Items; set => Form.Items = value; }
  49. public DynamicEditorPages? Pages { get => Form.Pages; }
  50. public event OnBeforeLoad? OnBeforeLoad;
  51. public void BeforeLoad() => OnBeforeLoad?.Invoke(this);
  52. public event OnAfterLoad? OnAfterLoad;
  53. public void AfterLoad() => OnAfterLoad?.Invoke(this);
  54. public event OnValidateData? OnValidateData { add => Form.OnValidateData += value; remove => Form.OnValidateData -= value; }
  55. public OnCustomiseColumns? OnCustomiseColumns
  56. {
  57. get => Form.OnCustomiseColumns;
  58. set => Form.OnCustomiseColumns = value;
  59. }
  60. public OnDefineLookupFilter? OnDefineFilter { get => Form.OnDefineFilter; set { Form.OnDefineFilter = value; } }
  61. public OnDefineLookup? OnDefineLookups { get => Form.OnDefineLookups; set { Form.OnDefineLookups = value; } }
  62. public DefineEditorEventHandler? OnDefineEditor { get => Form.OnDefineEditor; set { Form.OnDefineEditor = value; } }
  63. public event OnFormCustomiseEditor? OnFormCustomiseEditor;
  64. public OnReconfigureEditors? OnReconfigureEditors { get => Form.OnReconfigureEditors; set { Form.OnReconfigureEditors = value; } }
  65. public event OnAfterEditorValueChanged? OnAfterEditorValueChanged { add => Form.OnAfterEditorValueChanged += value; remove => Form.OnAfterEditorValueChanged -= value; }
  66. public event EditorValueChangedHandler? OnEditorValueChanged { add => Form.OnEditorValueChanged += value; remove => Form.OnEditorValueChanged -= value; }
  67. public event OnSelectPage? OnSelectPage { add => Form.OnSelectPage += value; remove => Form.OnSelectPage -= value; }
  68. public DynamicGridSaveEvent? OnSaveItem { get => Form.OnSaveItem; set { Form.OnSaveItem = value; } }
  69. public IDynamicEditorControl FindEditor(string columnName) => Form.FindEditor(columnName);
  70. public object? GetEditorValue(string columnName) => Form.GetEditorValue(columnName);
  71. public void SetEditorValue(string columnName, object? value) => Form.SetEditorValue(columnName, value);
  72. public void UnloadEditorPages(bool saved) => Form.UnloadEditorPages(saved);
  73. #endregion
  74. public bool? Result { get; set; }
  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. // Providing new implementation to avoid using DIalogResult, which breaks if non-modal.
  96. public new bool? ShowDialog()
  97. {
  98. base.ShowDialog();
  99. return Result;
  100. }
  101. private void Form_OnCancel()
  102. {
  103. if (bChanged)
  104. {
  105. var result = MessageWindow.ShowYesNoCancel("Save Changes?", "Confirm");
  106. switch (result)
  107. {
  108. case MessageWindowResult.Yes:
  109. Result = true;
  110. Close();
  111. break;
  112. case MessageWindowResult.No:
  113. Result = false;
  114. Close();
  115. break;
  116. }
  117. }
  118. else
  119. {
  120. Result = false;
  121. Close();
  122. }
  123. }
  124. private void Form_OnOK()
  125. {
  126. Result = true;
  127. Close();
  128. }
  129. private void Editor_OnEditorCreated(object sender, double height, double width)
  130. {
  131. }
  132. private void Window_Closing(object sender, CancelEventArgs e)
  133. {
  134. if (bChanged && Result == null)
  135. {
  136. var result = MessageWindow.ShowYesNoCancel("Save Changes?", "Confirm");
  137. switch (result)
  138. {
  139. case MessageWindowResult.Yes:
  140. Result = true;
  141. break;
  142. case MessageWindowResult.No:
  143. Result = false;
  144. break;
  145. case MessageWindowResult.Cancel:
  146. e.Cancel = true;
  147. return;
  148. }
  149. }
  150. if (Result == true)
  151. Form.SaveItem(e);
  152. SubPanelClosed?.Invoke(this);
  153. }
  154. private bool bChanged = false;
  155. private void Form_OnOnChanged(object? sender, EventArgs e)
  156. {
  157. bChanged = true;
  158. }
  159. private void ThemableChromelessWindow_Loaded(object sender, RoutedEventArgs e)
  160. {
  161. var screen = WpfScreen.GetScreenFrom(new Point(Left, Top));
  162. double spareheight = 90;
  163. double sparewidth = 25;
  164. var desiredheight = Form.ContentHeight + spareheight;
  165. var desiredwidth = Form.ContentWidth + 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. #region ISubPanel
  176. public event ISubPanel.ClosedEvent? SubPanelClosed;
  177. void ISubPanel.Shutdown(CancelEventArgs? cancel)
  178. {
  179. if (bChanged)
  180. {
  181. this.Focus();
  182. var window = cancel is null
  183. ? MessageWindow.NewYesNo($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?")
  184. : MessageWindow.NewYesNoCancel($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?");
  185. var result = window.Display().Result;
  186. switch (result)
  187. {
  188. case MessageWindowResult.Yes:
  189. Result = true;
  190. Close();
  191. break;
  192. case MessageWindowResult.No:
  193. Result = false;
  194. Close();
  195. break;
  196. case MessageWindowResult.Cancel:
  197. if(cancel is not null)
  198. {
  199. cancel.Cancel = true;
  200. }
  201. return;
  202. }
  203. }
  204. else
  205. {
  206. Close();
  207. }
  208. }
  209. #endregion
  210. }