DynamicEditorForm.xaml.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. Form.OnEditorCreated += Editor_OnEditorCreated;
  79. Form.OnOK += Form_OnOK;
  80. Form.OnCancel += Form_OnCancel;
  81. Form.OnFormCustomiseEditor += (sender, items, column, editor) => OnFormCustomiseEditor?.Invoke(sender, items, column, editor);
  82. }
  83. private bool _first = true;
  84. protected override void OnActivated(EventArgs e)
  85. {
  86. if (_first)
  87. {
  88. _first = false;
  89. this.MoveToCenter();
  90. }
  91. base.OnActivated(e);
  92. }
  93. public DynamicEditorForm(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  94. Func<Type, CoreTable>? pageDataHandler = null, bool preloadPages = false): this()
  95. {
  96. Setup(type, pages, buttons, pageDataHandler, preloadPages);
  97. }
  98. public void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null,
  99. Func<Type, CoreTable?>? pageDataHandler = null, bool preloadPages = false)
  100. {
  101. Form.Setup(type, pages, buttons, pageDataHandler, preloadPages);
  102. }
  103. public void SetLayoutType<T>() where T : DynamicEditorGridLayout => Form.SetLayoutType<T>();
  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 ThemableChromelessWindow_Loaded(object sender, RoutedEventArgs e)
  169. {
  170. var screen = WpfScreen.GetScreenFrom(new Point(Left, Top));
  171. double spareheight = 90;
  172. double sparewidth = 25;
  173. var desiredheight = Form.ContentHeight + spareheight;
  174. var desiredwidth = Form.ContentWidth + sparewidth;
  175. var maxheight = screen.WorkingArea.Height - 0;
  176. Height = desiredheight > maxheight ? maxheight : desiredheight;
  177. var maxwidth = screen.WorkingArea.Width - 0;
  178. Width = desiredwidth > maxwidth ? maxwidth : desiredwidth;
  179. Left = screen.DeviceBounds.Left + (screen.DeviceBounds.Width - Width) / 2.0F;
  180. Top = screen.DeviceBounds.Top + (screen.DeviceBounds.Height - Height) / 2.0F;
  181. var scaption = Form.Items[0].GetType().GetCaption();
  182. Title = "Edit " + scaption.SplitCamelCase();
  183. }
  184. #region ISubPanel
  185. public event ISubPanel.ClosedEvent? SubPanelClosed;
  186. void ISubPanel.Shutdown(CancelEventArgs? cancel)
  187. {
  188. if (bChanged)
  189. {
  190. this.Focus();
  191. var window = cancel is null
  192. ? MessageWindow.NewYesNo($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?")
  193. : MessageWindow.NewYesNoCancel($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?");
  194. var result = window.Display().Result;
  195. switch (result)
  196. {
  197. case MessageWindowResult.Yes:
  198. Result = true;
  199. Close();
  200. break;
  201. case MessageWindowResult.No:
  202. Result = false;
  203. Close();
  204. break;
  205. case MessageWindowResult.Cancel:
  206. if(cancel is not null)
  207. {
  208. cancel.Cancel = true;
  209. }
  210. return;
  211. }
  212. }
  213. else
  214. {
  215. Close();
  216. }
  217. }
  218. #endregion
  219. }