using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.Windows; using InABox.Core; using InABox.Wpf; using InABox.WPF; using Syncfusion.Windows.Shared; using Syncfusion.Windows.Tools.Controls; namespace InABox.DynamicGrid; 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, ISubPanel { #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 OnBeforeLoad? OnBeforeLoad; public void BeforeLoad() => OnBeforeLoad?.Invoke(this); public event OnAfterLoad? OnAfterLoad; public void AfterLoad() => OnAfterLoad?.Invoke(this); public event OnValidateData? OnValidateData { add => Form.OnValidateData += value; remove => Form.OnValidateData -= value; } public OnCustomiseColumns? OnCustomiseColumns { get => Form.OnCustomiseColumns; set => Form.OnCustomiseColumns = value; } public OnDefineLookupFilter? OnDefineFilter { get => Form.OnDefineFilter; set { Form.OnDefineFilter = value; } } public OnDefineLookup? OnDefineLookups { get => Form.OnDefineLookups; set { Form.OnDefineLookups = value; } } public DefineEditorEventHandler? OnDefineEditor { get => Form.OnDefineEditor; set { Form.OnDefineEditor = value; } } public event OnFormCustomiseEditor? OnFormCustomiseEditor; public OnReconfigureEditors? OnReconfigureEditors { get => Form.OnReconfigureEditors; set { 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 OnSelectPage? OnSelectPage { add => Form.OnSelectPage += value; remove => Form.OnSelectPage -= value; } public DynamicGridSaveEvent? OnSaveItem { get => Form.OnSaveItem; set { 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 bool? Result { get; set; } public DynamicEditorForm() { InitializeComponent(); Form.OnEditorCreated += Editor_OnEditorCreated; Form.OnOK += Form_OnOK; Form.OnCancel += Form_OnCancel; Form.OnFormCustomiseEditor += (sender, items, column, editor) => OnFormCustomiseEditor?.Invoke(sender, items, column, editor); } private bool _first = true; protected override void OnActivated(EventArgs e) { if (_first) { _first = false; this.MoveToCenter(); } base.OnActivated(e); } 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(); // Providing new implementation to avoid using DIalogResult, which breaks if non-modal. public new bool? ShowDialog() { base.ShowDialog(); return Result; } private void Form_OnCancel() { if (bChanged) { var result = MessageWindow.ShowYesNoCancel("Save Changes?", "Confirm"); switch (result) { case MessageWindowResult.Yes: Result = true; Close(); break; case MessageWindowResult.No: Result = false; Close(); break; } } else { Result = false; Close(); } } private void Form_OnOK() { Result = true; Close(); } private void Editor_OnEditorCreated(object sender, double height, double width) { } private void Window_Closing(object sender, CancelEventArgs e) { if (bChanged && Result == null) { var result = MessageWindow.ShowYesNoCancel("Save Changes?", "Confirm"); switch (result) { case MessageWindowResult.Yes: Result = true; break; case MessageWindowResult.No: Result = false; break; case MessageWindowResult.Cancel: e.Cancel = true; return; } } if (Result == true) Form.SaveItem(e); SubPanelClosed?.Invoke(this); } private bool bChanged = false; private void Form_OnOnChanged(object? sender, EventArgs e) { bChanged = true; } private void ThemableChromelessWindow_Loaded(object sender, RoutedEventArgs e) { var screen = WpfScreen.GetScreenFrom(new Point(Left, Top)); double spareheight = 90; double sparewidth = 25; var desiredheight = Form.ContentHeight + spareheight; var desiredwidth = Form.ContentWidth + 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(); } #region ISubPanel public event ISubPanel.ClosedEvent? SubPanelClosed; void ISubPanel.Shutdown(CancelEventArgs? cancel) { if (bChanged) { this.Focus(); var window = cancel is null ? MessageWindow.NewYesNo($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?") : MessageWindow.NewYesNoCancel($"You have unsaved changes: do you wish to save this {CoreUtils.Neatify(Form.Items[0].GetType().GetCaption())}?", "Save changes?"); var result = window.Display().Result; switch (result) { case MessageWindowResult.Yes: Result = true; Close(); break; case MessageWindowResult.No: Result = false; Close(); break; case MessageWindowResult.Cancel: if(cancel is not null) { cancel.Cancel = true; } return; } } else { Close(); } } #endregion }