DynamicEditorForm.xaml.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Windows;
  7. using InABox.Core;
  8. using InABox.Wpf;
  9. using InABox.WPF;
  10. using Syncfusion.Windows.Shared;
  11. using Syncfusion.Windows.Tools.Controls;
  12. namespace InABox.DynamicGrid;
  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, ISubPanel
  46. {
  47. #region IDynamicEditorForm
  48. public bool ReadOnly { get => Form.ReadOnly; set => Form.ReadOnly = value; }
  49. public BaseObject[] Items
  50. {
  51. get => Form.Items;
  52. set
  53. {
  54. Form.Items = value;
  55. }
  56. }
  57. public DynamicEditorPages? Pages { get => Form.Pages; }
  58. public event OnBeforeLoad? OnBeforeLoad;
  59. public void BeforeLoad() => OnBeforeLoad?.Invoke(this);
  60. public event OnAfterLoad? OnAfterLoad;
  61. public void AfterLoad() => OnAfterLoad?.Invoke(this);
  62. public event OnValidateData? OnValidateData { add => Form.OnValidateData += value; remove => Form.OnValidateData -= value; }
  63. public OnCustomiseColumns? OnCustomiseColumns
  64. {
  65. get => Form.OnCustomiseColumns;
  66. set => Form.OnCustomiseColumns = value;
  67. }
  68. public OnDefineLookupFilter? OnDefineFilter { get => Form.OnDefineFilter; set { Form.OnDefineFilter = value; } }
  69. public OnDefineLookup? OnDefineLookups { get => Form.OnDefineLookups; set { Form.OnDefineLookups = value; } }
  70. public DefineEditorEventHandler? OnDefineEditor { get => Form.OnDefineEditor; set { Form.OnDefineEditor = value; } }
  71. public event OnFormCustomiseEditor? OnFormCustomiseEditor { add => Form.OnFormCustomiseEditor += value; remove => Form.OnFormCustomiseEditor -= value; }
  72. public OnReconfigureEditors? OnReconfigureEditors { get => Form.OnReconfigureEditors; set { Form.OnReconfigureEditors = value; } }
  73. public event OnCreateEditorControl? OnCreateEditorControl { add => Form.OnCreateEditorControl += value; remove => Form.OnCreateEditorControl -= value; }
  74. public event OnAfterEditorValueChanged? OnAfterEditorValueChanged { add => Form.OnAfterEditorValueChanged += value; remove => Form.OnAfterEditorValueChanged -= value; }
  75. public event EditorValueChangedHandler? OnEditorValueChanged { add => Form.OnEditorValueChanged += value; remove => Form.OnEditorValueChanged -= value; }
  76. public event OnSelectPage? OnSelectPage { add => Form.OnSelectPage += value; remove => Form.OnSelectPage -= value; }
  77. public DynamicGridSaveEvent? OnSaveItem { get => Form.OnSaveItem; set { Form.OnSaveItem = value; } }
  78. public event IDynamicEditorForm.OnReloadEventHandler? OnReload { add => Form.OnReload += value; remove => Form.OnReload -= value; }
  79. public bool TryFindEditor(string columnname, [NotNullWhen(true)] out IDynamicEditorControl? editor) => Form.TryFindEditor(columnname, out editor);
  80. public object? GetEditorValue(string columnName) => Form.GetEditorValue(columnName);
  81. public void SetEditorValue(string columnName, object? value) => Form.SetEditorValue(columnName, value);
  82. public void UnloadEditorPages(bool saved) => Form.UnloadEditorPages(saved);
  83. #endregion
  84. public bool? Result { get; set; }
  85. public DynamicEditorForm()
  86. {
  87. InitializeComponent();
  88. Form.OnEditorCreated += Editor_OnEditorCreated;
  89. Form.OnOK += Form_OnOK;
  90. Form.OnCancel += Form_OnCancel;
  91. }
  92. public DynamicEditorForm(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  93. Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false): this()
  94. {
  95. Setup(type, pages, buttons, pageDataHandler, preloadPages);
  96. }
  97. public void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  98. Func<Type, CoreTable?>? pageDataHandler = null, bool preloadPages = false)
  99. {
  100. Form.Setup(type, pages, buttons, pageDataHandler, preloadPages);
  101. }
  102. public void SetLayoutType<T>() where T : DynamicEditorGridLayout => Form.SetLayoutType<T>();
  103. public void SetLayout(DynamicEditorGridLayout layout) => Form.SetLayout(layout);
  104. // Providing new implementation to avoid using DIalogResult, which breaks if non-modal.
  105. public new bool? ShowDialog()
  106. {
  107. base.ShowDialog();
  108. return Result;
  109. }
  110. private void Form_OnCancel()
  111. {
  112. if (bChanged)
  113. {
  114. var result = MessageWindow.ShowYesNoCancel("Save Changes?", "Confirm");
  115. switch (result)
  116. {
  117. case MessageWindowResult.Yes:
  118. Result = true;
  119. Close();
  120. break;
  121. case MessageWindowResult.No:
  122. Result = false;
  123. Close();
  124. break;
  125. }
  126. }
  127. else
  128. {
  129. Result = false;
  130. Close();
  131. }
  132. }
  133. private void Form_OnOK()
  134. {
  135. Result = true;
  136. Close();
  137. }
  138. private void Editor_OnEditorCreated(object sender, double height, double width)
  139. {
  140. }
  141. private void Window_Closing(object sender, CancelEventArgs e)
  142. {
  143. if (bChanged && Result == null)
  144. {
  145. var result = MessageWindow.ShowYesNoCancel("Save Changes?", "Confirm");
  146. switch (result)
  147. {
  148. case MessageWindowResult.Yes:
  149. Result = true;
  150. break;
  151. case MessageWindowResult.No:
  152. Result = false;
  153. break;
  154. case MessageWindowResult.Cancel:
  155. e.Cancel = true;
  156. return;
  157. }
  158. }
  159. if (Result == true)
  160. Form.SaveItem(e);
  161. SubPanelClosed?.Invoke(this);
  162. }
  163. private bool bChanged = false;
  164. private void Form_OnOnChanged(object? sender, EventArgs e)
  165. {
  166. bChanged = true;
  167. }
  168. private void Form_OnReload(IDynamicEditorForm form)
  169. {
  170. SetSize();
  171. }
  172. private void SetSize()
  173. {
  174. var screen = WpfScreen.GetScreenFrom(new Point(Left, Top));
  175. double spareheight = 90;
  176. double sparewidth = 25;
  177. var desiredheight = Form.ContentHeight + spareheight;
  178. var desiredwidth = Form.ContentWidth + sparewidth;
  179. var oldHeight = Height;
  180. var maxheight = screen.WorkingArea.Height - 300;
  181. Height = desiredheight > maxheight ? maxheight : desiredheight;
  182. Top += (oldHeight - Height) / 2;
  183. var maxwidth = screen.WorkingArea.Width - 0;
  184. Width = desiredwidth > maxwidth ? maxwidth : desiredwidth;
  185. var scaption = Form.Items[0].GetType().GetCaption();
  186. Title = "Edit " + scaption.SplitCamelCase();
  187. }
  188. #region ISubPanel
  189. public event ISubPanel.ClosedEvent? SubPanelClosed;
  190. void ISubPanel.Shutdown(CancelEventArgs? cancel)
  191. {
  192. if (bChanged)
  193. {
  194. this.Focus();
  195. var window = cancel is null
  196. ? MessageWindow.NewYesNo($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?")
  197. : MessageWindow.NewYesNoCancel($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?");
  198. var result = window.Display().Result;
  199. switch (result)
  200. {
  201. case MessageWindowResult.Yes:
  202. Result = true;
  203. Close();
  204. break;
  205. case MessageWindowResult.No:
  206. Result = false;
  207. Close();
  208. break;
  209. case MessageWindowResult.Cancel:
  210. if(cancel is not null)
  211. {
  212. cancel.Cancel = true;
  213. }
  214. return;
  215. }
  216. }
  217. else
  218. {
  219. Close();
  220. }
  221. }
  222. #endregion
  223. private void ThemableChromelessWindow_Loaded(object sender, RoutedEventArgs e)
  224. {
  225. SetSize();
  226. }
  227. }