DynamicEditorForm.xaml.cs 7.8 KB

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