using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using InABox.Core; using InABox.Wpf; using InABox.WPF; using Syncfusion.Windows.Shared; using Syncfusion.Windows.Tools.Controls; namespace InABox.DynamicGrid { public delegate BaseEditor? DefineEditorEventHandler(object item, DynamicGridColumn column); public delegate void DynamicGridSaveEvent(object sender, CancelEventArgs args); public interface IDynamicEditorForm { public delegate Document? FindDocumentEvent(string FileName); public delegate Document? GetDocumentEvent(Guid id); public delegate void SaveDocumentEvent(Document document); public event OnValidateData? OnValidateData; public event OnCustomiseColumns? OnCustomiseColumns; public event OnDefineFilter? OnDefineFilter; public event OnDefineLookup? OnDefineLookups; public event OnLookupsDefined? OnLookupsDefined; public event DefineEditorEventHandler? OnDefineEditor; public event OnFormCustomiseEditor? OnFormCustomiseEditor; public event OnReconfigureEditors? OnReconfigureEditors; public event OnAfterEditorValueChanged? OnAfterEditorValueChanged; public event EditorValueChangedHandler? OnEditorValueChanged; public event GetDocumentEvent? OnGetDocument; public event FindDocumentEvent? OnFindDocument; public event SaveDocumentEvent? OnSaveDocument; public event OnSelectPage? OnSelectPage; public event DynamicGridSaveEvent? OnSaveItem; DynamicEditorPages? Pages { get; } bool ReadOnly { get; set; } BaseObject[] Items { get; set; } void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null, Func? pageDataHandler = null, bool preloadPages = false); IDynamicEditorControl FindEditor(string columnName); object? GetEditorValue(string columnName); void SetEditorValue(string columnName, object? value); void SetLayoutType() where T : DynamicEditorGridLayout; void UnloadEditorPages(bool saved); } public class UtilityItem { public string Name { get; set; } public ImageSource Icon { get; set; } public string Text { get; set; } public SizeMode Mode { get; set; } public ICommand Command { get; set; } } public class DynamicEditorFormModel { /// /// Constructor of the UtilityViewModel class. /// public DynamicEditorFormModel() { var utilities = new ObservableCollection(); utilities.Add(new UtilityItem { Name = "Help", Icon = Resources.help.AsBitmapImage(), Text = "", Mode = SizeMode.Normal, Command = HelpCommand }); Utilities = utilities; } /// /// Collection containing the complete details of the items to be bound in the title bar. /// public ObservableCollection Utilities { get; } /// /// Commmand for the Help button. /// public DelegateCommand HelpCommand => new(HelpCommandAction); public static string Slug { get; set; } /// /// Action that is performed when clicking the help button. /// private void HelpCommandAction(object param) { Process.Start("https://prsdigital.com.au/wiki/index.php/" + Slug); } } /// /// Interaction logic for DynamicEditor.xaml /// public partial class DynamicEditorForm : ThemableChromelessWindow, IDynamicEditorForm { #region IDynamicEditorForm public bool ReadOnly { get => Form.ReadOnly; set => Form.ReadOnly = value; } public BaseObject[] Items { get => Form.Items; set => Form.Items = value; } public DynamicEditorPages? Pages { get => Form.Pages; } public event OnValidateData? OnValidateData { add => Form.OnValidateData += value; remove => Form.OnValidateData -= value; } public event OnCustomiseColumns? OnCustomiseColumns { add => Form.OnCustomiseColumns += value; remove => Form.OnCustomiseColumns -= value; } public event OnDefineFilter? OnDefineFilter { add => Form.OnDefineFilter += value; remove => Form.OnDefineFilter -= value; } public event OnDefineLookup? OnDefineLookups { add => Form.OnDefineLookups += value; remove => Form.OnDefineLookups -= value; } public event OnLookupsDefined? OnLookupsDefined { add => Form.OnLookupsDefined += value; remove => Form.OnLookupsDefined -= value; } public event DefineEditorEventHandler? OnDefineEditor { add => Form.OnDefineEditor += value; remove => Form.OnDefineEditor -= value; } public event OnFormCustomiseEditor? OnFormCustomiseEditor { add => Form.OnFormCustomiseEditor += value; remove => Form.OnFormCustomiseEditor -= value; } public event OnReconfigureEditors? OnReconfigureEditors { add => Form.OnReconfigureEditors += value; remove => Form.OnReconfigureEditors -= value; } public event OnAfterEditorValueChanged? OnAfterEditorValueChanged { add => Form.OnAfterEditorValueChanged += value; remove => Form.OnAfterEditorValueChanged -= value; } public event EditorValueChangedHandler? OnEditorValueChanged { add => Form.OnEditorValueChanged += value; remove => Form.OnEditorValueChanged -= value; } public event IDynamicEditorForm.GetDocumentEvent? OnGetDocument { add => Form.OnGetDocument += value; remove => Form.OnGetDocument -= value; } public event IDynamicEditorForm.FindDocumentEvent? OnFindDocument { add => Form.OnFindDocument += value; remove => Form.OnFindDocument -= value; } public event IDynamicEditorForm.SaveDocumentEvent? OnSaveDocument { add => Form.OnSaveDocument += value; remove => Form.OnSaveDocument -= value; } public event OnSelectPage? OnSelectPage { add => Form.OnSelectPage += value; remove => Form.OnSelectPage -= value; } public event DynamicGridSaveEvent? OnSaveItem { add => Form.OnSaveItem += value; remove => Form.OnSaveItem -= value; } public IDynamicEditorControl FindEditor(string columnName) => Form.FindEditor(columnName); public object? GetEditorValue(string columnName) => Form.GetEditorValue(columnName); public void SetEditorValue(string columnName, object? value) => Form.SetEditorValue(columnName, value); public void UnloadEditorPages(bool saved) => Form.UnloadEditorPages(saved); #endregion public DynamicEditorForm() { InitializeComponent(); //this.Loaded += new RoutedEventHandler(ConfigureSystemMenu); Form.OnEditorCreated += Editor_OnEditorCreated; Form.OnOK += Form_OnOK; Form.OnCancel += Form_OnCancel; } public DynamicEditorForm(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null, Func? pageDataHandler = null, bool preloadPages = false): this() { Setup(type, pages, buttons, pageDataHandler, preloadPages); } public void Setup(Type type, DynamicEditorPages? pages = null, DynamicEditorButtons? buttons = null, Func? pageDataHandler = null, bool preloadPages = false) { Form.Setup(type, pages, buttons, pageDataHandler, preloadPages); } public void SetLayoutType() where T : DynamicEditorGridLayout => Form.SetLayoutType(); private void Form_OnCancel() { DialogResult = false; } private void Form_OnOK() { DialogResult = true; } private void Editor_OnEditorCreated(object sender, double height, double width) { var screen = WpfScreen.GetScreenFrom(new Point(Left, Top)); double spareheight = 90; double sparewidth = 25; var desiredheight = height; var desiredwidth = width; if (Form.Pages != null) foreach (var page in Form.Pages) { if (desiredheight < page.MinimumSize().Height) desiredheight = page.MinimumSize().Height; if (desiredwidth < page.MinimumSize().Width) desiredwidth = page.MinimumSize().Width; } desiredheight += spareheight; desiredwidth += sparewidth; var maxheight = screen.WorkingArea.Height - 0; Height = desiredheight > maxheight ? maxheight : desiredheight; var maxwidth = screen.WorkingArea.Width - 0; Width = desiredwidth > maxwidth ? maxwidth : desiredwidth; Left = screen.DeviceBounds.Left + (screen.DeviceBounds.Width - Width) / 2.0F; Top = screen.DeviceBounds.Top + (screen.DeviceBounds.Height - Height) / 2.0F; var scaption = Form.Items[0].GetType().GetCaption(); Title = "Edit " + scaption.SplitCamelCase(); } private void Window_Closing(object sender, CancelEventArgs e) { if (DialogResult == true) Form.SaveItem(e); } } }