DynamicEditorForm.xaml.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 && !ReadOnly)
  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. if (Result == false)
  127. {
  128. Form.CancelItem();
  129. }
  130. }
  131. else
  132. {
  133. Result = false;
  134. Close();
  135. }
  136. }
  137. private void Form_OnOK()
  138. {
  139. Result = true;
  140. Close();
  141. }
  142. private void Editor_OnEditorCreated(object sender, double height, double width)
  143. {
  144. }
  145. private void Window_Closing(object sender, CancelEventArgs e)
  146. {
  147. if (bChanged && !ReadOnly && Result == null)
  148. {
  149. var result = MessageWindow.ShowYesNoCancel("Save Changes?", "Confirm");
  150. switch (result)
  151. {
  152. case MessageWindowResult.Yes:
  153. Result = true;
  154. break;
  155. case MessageWindowResult.No:
  156. Result = false;
  157. break;
  158. case MessageWindowResult.Cancel:
  159. e.Cancel = true;
  160. return;
  161. }
  162. if(Result == false)
  163. {
  164. Form.CancelItem();
  165. }
  166. }
  167. if (Result == true)
  168. {
  169. Form.SaveItem(e);
  170. }
  171. if (e.Cancel)
  172. {
  173. Result = null;
  174. return;
  175. }
  176. SubPanelClosed?.Invoke(this);
  177. }
  178. private bool bChanged = false;
  179. private void Form_OnOnChanged(object? sender, EventArgs e)
  180. {
  181. bChanged = true;
  182. }
  183. private void Form_OnReload(IDynamicEditorForm form)
  184. {
  185. SetSize();
  186. }
  187. private void SetSize()
  188. {
  189. var screen = WpfScreen.GetScreenFrom(new Point(Left, Top));
  190. double spareheight = 90;
  191. double sparewidth = 25;
  192. var desiredheight = Form.ContentHeight + spareheight;
  193. var desiredwidth = Form.ContentWidth + sparewidth;
  194. var oldHeight = Height;
  195. var maxheight = screen.WorkingArea.Height - 300;
  196. Height = desiredheight > maxheight ? maxheight : desiredheight;
  197. Top += (oldHeight - Height) / 2;
  198. var maxwidth = screen.WorkingArea.Width - 0;
  199. Width = desiredwidth > maxwidth ? maxwidth : desiredwidth;
  200. var scaption = Form.Items[0].GetType().GetCaption();
  201. Title = "Edit " + scaption.SplitCamelCase();
  202. }
  203. #region ISubPanel
  204. public event ISubPanel.ClosedEvent? SubPanelClosed;
  205. void ISubPanel.Shutdown(CancelEventArgs? cancel)
  206. {
  207. if (bChanged)
  208. {
  209. this.Focus();
  210. var window = cancel is null
  211. ? MessageWindow.NewYesNo($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?")
  212. : MessageWindow.NewYesNoCancel($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?");
  213. var result = window.Display().Result;
  214. switch (result)
  215. {
  216. case MessageWindowResult.Yes:
  217. Result = true;
  218. Close();
  219. break;
  220. case MessageWindowResult.No:
  221. Result = false;
  222. Close();
  223. break;
  224. case MessageWindowResult.Cancel:
  225. if(cancel is not null)
  226. {
  227. cancel.Cancel = true;
  228. }
  229. return;
  230. }
  231. }
  232. else
  233. {
  234. Close();
  235. }
  236. }
  237. #endregion
  238. private void ThemableChromelessWindow_Loaded(object sender, RoutedEventArgs e)
  239. {
  240. SetSize();
  241. }
  242. }