1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Diagnostics.CodeAnalysis;
- using System.Drawing;
- using System.Drawing.Design;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Media;
- using System.Windows.Media.Effects;
- using System.Windows.Media.Imaging;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid.Properties;
- using InABox.WPF;
- using Microsoft.Win32;
- using Syncfusion.Windows.Shared;
- using Color = System.Drawing.Color;
- using Image = System.Windows.Controls.Image;
- using Point = System.Windows.Point;
- namespace InABox.DynamicGrid
- {
- public class DynamicFormCreateElementArgs : EventArgs
- {
- public DynamicFormCreateElementArgs(DFLayoutElement element)
- {
- Element = element;
- }
- public DFLayoutElement Element { get; }
- }
- public delegate FrameworkElement DynamicFormCreateElementDelegate(object sender, DynamicFormCreateElementArgs e);
- public delegate void DynamicFormAfterDesignDelegate(object sender);
- public delegate void DynamicFormAfterRenderDelegate(DynamicFormDesignGrid sender);
- public delegate void DynamicFormOnChangedDelegate(DynamicFormDesignGrid sender, string fieldName);
- public enum FormMode
- {
- /// <summary>
- /// The form is read-only.
- /// </summary>
- ReadOnly,
- /// <summary>
- /// Standard option for when the form is being filled in by the associated employee.
- /// </summary>
- Filling,
- /// <summary>
- /// Standard option for when the form is being previewed - that is, it is acting with placeholder values that will not get saved.
- /// It is still editable.
- /// </summary>
- Preview,
- /// <summary>
- /// Once the form has been completed, or the person editing is not the employee the form applies to, then they are put in Editing Mode.<br/>
- /// This is only possible if <see cref="CanEditCompletedForms"/> is enabled.
- /// </summary>
- Editing,
- /// <summary>
- /// The form layout is being designed
- /// </summary>
- Designing
- }
- public class DynamicFormDesignGrid : Grid, IDFRenderer
- {
- //private bool _designing;
- public bool GridInitialized { get; private set; } = false;
- //private bool _readonly;
- private readonly SolidColorBrush BackgroundBrush = new(Colors.WhiteSmoke);
- private readonly SolidColorBrush BorderBrush = new(Colors.Silver);
- private readonly Dictionary<Type, IDynamicGrid> elementgrids = new();
- private readonly Dictionary<DFLayoutControl, FrameworkElement> elementmap = new();
- private readonly SolidColorBrush FieldBrush = new(Colors.LightYellow);
- private readonly SolidColorBrush RequiredFieldBrush = new(Colors.Orange);
- #region Backing Properties
- private readonly List<DynamicFormElement> _elements = new();
- private bool _showBorders = true;
- private IDigitalFormDataModel? _datamodel;
- private DFLayout form = new();
- private FormMode _mode;
- private DigitalFormVariable[] _variables = Array.Empty<DigitalFormVariable>();
- private bool _isChanged;
- private bool _changing = false;
- #endregion
- #region Public Properties
- public bool ShowBorders
- {
- get => _showBorders || _mode == FormMode.Designing;
- set
- {
- _showBorders = value;
- CheckRefresh();
- }
- }
- public IDigitalFormDataModel? DataModel
- {
- get => _datamodel;
- set
- {
- _datamodel = value;
- if(_datamodel != null)
- {
- var task = Task.Run(() =>
- {
- var waiting = true;
- _datamodel.Load(m => { waiting = false; });
- while (waiting) ;
- });
- task.Wait();
- }
- CheckRefresh();
- }
- }
- public DFLayout Form
- {
- get => form;
- set
- {
- form = value;
- form.Renderer = this;
- CheckRefresh();
- }
- }
- public FormMode Mode
- {
- get => _mode;
- set
- {
- if (_mode != value)
- {
- var oldMode = _mode;
- if (_mode == FormMode.Designing)
- {
- OnAfterDesign?.Invoke(this);
- }
- _mode = value;
- CheckRefresh(oldMode != FormMode.Designing);
- }
- }
- }
- public bool IsDesigning { get => _mode == FormMode.Designing; }
- public bool IsReadOnly { get => _mode == FormMode.ReadOnly; }
- public bool IsEditing { get => _mode == FormMode.Editing || _mode == FormMode.Filling; }
- public bool IsChanged => _isChanged;
- public DigitalFormVariable[] Variables
- {
- get => _variables;
- set
- {
- _variables = value;
- CheckRefresh();
- }
- }
- #endregion
- #region Events
- public event DynamicFormAfterDesignDelegate? OnAfterDesign;
- public event DynamicFormCreateElementDelegate? OnCreateElement;
- public event DynamicFormAfterRenderDelegate? OnAfterRender;
- public event DynamicFormOnChangedDelegate? OnChanged;
- #endregion
- #region Rows
- class RowData
- {
- public string RowHeight { get; set; }
- public RowData(string rowHeight)
- {
- RowHeight = rowHeight;
- }
- }
- internal void CollapseRows(FormHeader header, bool collapsed)
- {
- var startRow = this.GetRow(header) + this.GetRowSpan(header);
- var nextRow = elementmap
- .Where(x => x.Value is DFHeaderControl headerControl && headerControl.Header != header)
- .Select(x => this.GetRow(x.Value))
- .Where(x => x >= startRow).DefaultIfEmpty(-1).Min();
- if (nextRow == -1)
- {
- nextRow = RowDefinitions.Count;
- }
- for (int row = startRow; row < nextRow; ++row)
- {
- var rowDefinition = RowDefinitions[row];
- if(rowDefinition.Tag is not RowData rowData)
- {
- throw new Exception("Row definition tag is not RowData");
- }
- if (collapsed)
- {
- rowDefinition.Height = new GridLength(0);
- }
- else
- {
- rowDefinition.Height = StringToGridLength(rowData.RowHeight);
- }
- }
- }
- #endregion
- #region Elements
- class DynamicFormElement
- {
- public string Caption { get; set; }
- public Type ElementType { get; set; }
- public string Category { get; set; }
- public FrameworkElement? Element { get; set; }
- public bool AllowDuplicate { get; set; }
- public DynamicFormElement(string caption, Type elementType, string category, FrameworkElement? element, bool allowDuplicate)
- {
- Caption = caption;
- ElementType = elementType;
- Category = category;
- Element = element;
- AllowDuplicate = allowDuplicate;
- }
- }
- public void AddElement<TElement>(string caption, string category, bool allowduplicate = false)
- where TElement : DFLayoutElement
- {
- AddElement(typeof(TElement), caption, category, allowduplicate);
- }
- public void AddElement(Type TElement, string caption, string category, bool allowduplicate = false)
- {
- _elements.Add(new DynamicFormElement(caption, TElement, category, null, allowduplicate));
- }
- internal FrameworkElement? CreateElement(DFLayoutElement element)
- {
- var elementType = element.GetType();
- if(_elements.Any(x => x.ElementType == elementType))
- {
- return OnCreateElement?.Invoke(this, new DynamicFormCreateElementArgs(element));
- }
- return null;
- }
- #endregion
- #region Utilities
- private static VerticalAlignment GetVerticalAlignment(DFLayoutAlignment alignment)
- {
- return alignment == DFLayoutAlignment.Start
- ? VerticalAlignment.Top
- : alignment == DFLayoutAlignment.End
- ? VerticalAlignment.Bottom
- : alignment == DFLayoutAlignment.Middle
- ? VerticalAlignment.Center
- : VerticalAlignment.Stretch;
- }
- private static HorizontalAlignment GetHorizontalAlignment(DFLayoutAlignment alignment)
- {
- return alignment == DFLayoutAlignment.Start
- ? HorizontalAlignment.Left
- : alignment == DFLayoutAlignment.End
- ? HorizontalAlignment.Right
- : alignment == DFLayoutAlignment.Middle
- ? HorizontalAlignment.Center
- : HorizontalAlignment.Stretch;
- }
- private static GridLength StringToGridLength(string length)
- {
- if (string.IsNullOrWhiteSpace(length) || string.Equals(length.ToUpper(), "AUTO"))
- return new GridLength(1, GridUnitType.Auto);
- if (!double.TryParse(length.Replace("*", ""), out var value))
- value = 1.0F;
- var type = length.Contains('*') ? GridUnitType.Star : GridUnitType.Pixel;
- return new GridLength(value, type);
- }
- private static string GridLengthToString(GridLength length)
- {
- if (length.IsStar)
- return length.Value == 1.0F ? "*" : string.Format("{0}*", length.Value);
- if (length.IsAuto)
- return "Auto";
- return length.Value.ToString();
- }
- private static string FormatGridLength(GridLength length, IEnumerable<GridLength> others)
- {
- if (length.IsStar)
- {
- double total = 0.0F;
- foreach (var other in others.Where(x => x.IsStar))
- total += other.Value;
- return string.Format("{0:F0}%", total != 0 ? length.Value * 100.0F / total : 0);
- }
- if (length.IsAuto)
- return "Auto";
- return string.Format("{0}px", length.Value);
- }
- private static MenuItem CreateMenuItem<TTag>(string caption, TTag tag, Action<TTag>? click)
- {
- var result = new MenuItem { Header = caption, Tag = tag };
- if (click != null)
- result.Click += (o, e) => click((TTag)(o as MenuItem)!.Tag);
- return result;
- }
- private static void AddClick<TTag>(ButtonBase button, TTag tag, Action<TTag> click)
- {
- button.Tag = tag;
- button.Click += (o, e) => click((TTag)(o as MenuItem)!.Tag);
- }
- #endregion
- #region Design Mode
- #region Rows
- private void ShiftDown(int row)
- {
- foreach (var element in form.Elements)
- if (element.Row > row)
- element.Row++;
- else if (element.Row + element.RowSpan - 1 > row)
- element.RowSpan++;
- }
- private void AddRowBeforeClick(int row)
- {
- var length = new GridLength(1.0F, GridUnitType.Auto);
- var result = GridLengthToString(length);
- if (form.RowHeights.Count == 0)
- form.RowHeights.Add(result);
- else
- form.RowHeights.Insert(row, result);
- ShiftDown(row);
- Render();
- }
- private void AddRowAfterClick(int row)
- {
- var length = new GridLength(1.0F, GridUnitType.Auto);
- var result = GridLengthToString(length);
- if (row == form.RowHeights.Count - 1)
- form.RowHeights.Add(result);
- else
- form.RowHeights.Insert(row + 1, result);
- ShiftDown(row + 1);
- Render();
- }
- private void SplitRow(int row)
- {
- foreach (var element in form.Elements)
- if (element.Row > row)
- element.Row++;
- else if (element.Row == row)
- element.RowSpan++;
- else if (element.Row + element.RowSpan - 1 >= row)
- element.RowSpan++;
- }
- private void SplitRowClick(int row)
- {
- var result = form.RowHeights[row];
- if (row == form.RowHeights.Count - 1)
- form.RowHeights.Add(result);
- else
- form.RowHeights.Insert(row + 1, result);
- SplitRow(row + 1);
- Render();
- }
- private void RowPropertiesClick(int row)
- {
- var length = StringToGridLength(form.RowHeights[row]);
- var editor = new DynamicFormDesignLength(length);
- if (editor.ShowDialog() == true)
- {
- form.RowHeights[row] = GridLengthToString(editor.GridLength);
- Render();
- }
- }
- private void ShiftUp(int row)
- {
- var deletes = new List<DFLayoutControl>();
- foreach (var element in form.Elements)
- if (element.Row > row)
- element.Row--;
- else if (element.Row == row)
- deletes.Add(element);
- else if (element.Row + element.RowSpan - 1 >= row)
- element.RowSpan--;
- foreach (var delete in deletes)
- form.Elements.Remove(delete);
- }
- private void DeleteRowClick(int row)
- {
- form.RowHeights.RemoveAt(row);
- ShiftUp(row + 1);
- Render();
- }
- private ContextMenu CreateRowMenu(Button button, int row)
- {
- var result = new ContextMenu();
- result.Items.Add(CreateMenuItem("Add Row Before", row, AddRowBeforeClick));
- result.Items.Add(CreateMenuItem("Add Row After", row, AddRowAfterClick));
- result.Items.Add(new Separator());
- result.Items.Add(CreateMenuItem("Split Row", row, SplitRowClick));
- var propertiesSeparator = new Separator();
- var propertiesMenu = CreateMenuItem("Row Properties", row, RowPropertiesClick);
- result.Items.Add(propertiesSeparator);
- result.Items.Add(propertiesMenu);
- result.Items.Add(new Separator());
- result.Items.Add(CreateMenuItem("Delete Row", row, DeleteRowClick));
- result.Opened += (o, e) =>
- {
- propertiesSeparator.Visibility = form.RowHeights.Count > 1 ? Visibility.Visible : Visibility.Collapsed;
- propertiesMenu.Visibility = form.RowHeights.Count > 1 ? Visibility.Visible : Visibility.Collapsed;
- };
- button.SetValue(ContextMenuProperty, result);
- return result;
- }
- #endregion
- #region Columns
- private void ShiftRight(int column)
- {
- foreach (var element in form.Elements)
- if (element.Column > column)
- element.Column++;
- else if (element.Column + element.ColumnSpan - 1 > column)
- element.ColumnSpan++;
- }
- private void AddColumnBeforeClick(int column)
- {
- var length = new GridLength(1.0F, form.ColumnWidths.Count == 0 ? GridUnitType.Star : GridUnitType.Auto);
- var result = GridLengthToString(length);
- if (form.ColumnWidths.Count == 0)
- form.ColumnWidths.Add(result);
- else
- form.ColumnWidths.Insert(column, result);
- ShiftRight(column);
- Render();
- }
- private void AddColumnAfterClick(int column)
- {
- var length = new GridLength(1.0F, form.ColumnWidths.Count == 0 ? GridUnitType.Star : GridUnitType.Auto);
- var result = GridLengthToString(length);
- if (column == form.ColumnWidths.Count - 1)
- form.ColumnWidths.Add(result);
- else
- form.ColumnWidths.Insert(column + 1, result);
- ShiftRight(column + 1);
- Render();
- }
- private void SplitColumn(int column)
- {
- foreach (var element in form.Elements)
- if (element.Column > column)
- element.Column++;
- else if (element.Column == column)
- element.ColumnSpan++;
- else if (element.Column + element.ColumnSpan - 1 >= column)
- element.ColumnSpan++;
- }
- private void SplitColumnClick(int column)
- {
- var result = form.ColumnWidths[column];
- if (column == form.ColumnWidths.Count - 1)
- form.ColumnWidths.Add(result);
- else
- form.ColumnWidths.Insert(column + 1, result);
- SplitColumn(column + 1);
- Render();
- }
- private void ColumnPropertiesClick(int column)
- {
- var length = StringToGridLength(form.ColumnWidths[column]);
- var editor = new DynamicFormDesignLength(length);
- if (editor.ShowDialog() == true)
- {
- form.ColumnWidths[column] = GridLengthToString(editor.GridLength);
- Render();
- }
- }
- private void ShiftLeft(int column)
- {
- var deletes = new List<DFLayoutControl>();
- foreach (var element in form.Elements)
- if (element.Column > column)
- element.Column--;
- else if (element.Column == column)
- deletes.Add(element);
- else if (element.Column + element.ColumnSpan - 1 >= column)
- element.ColumnSpan--;
- foreach (var delete in deletes)
- form.Elements.Remove(delete);
- }
- private void DeleteColumnClick(int column)
- {
- form.ColumnWidths.RemoveAt(column);
- ShiftLeft(column + 1);
- Render();
- }
- private ContextMenu CreateColumnMenu(Button button, int column)
- {
- var result = new ContextMenu();
- result.Items.Add(CreateMenuItem("Add Column Before", column, AddColumnBeforeClick));
- result.Items.Add(CreateMenuItem("Add Column After", column, AddColumnAfterClick));
- result.Items.Add(new Separator());
- result.Items.Add(CreateMenuItem("Split Column", column, SplitColumnClick));
- var propertiesSeparator = new Separator();
- var propertiesMenu = CreateMenuItem("Column Properties", column, ColumnPropertiesClick);
- result.Items.Add(propertiesSeparator);
- result.Items.Add(propertiesMenu);
- result.Items.Add(new Separator());
- result.Items.Add(CreateMenuItem("Delete Column", column, DeleteColumnClick));
- result.Opened += (o, e) =>
- {
- propertiesSeparator.Visibility = form.ColumnWidths.Count > 1 ? Visibility.Visible : Visibility.Collapsed;
- propertiesMenu.Visibility = form.ColumnWidths.Count > 1 ? Visibility.Visible : Visibility.Collapsed;
- };
- button.SetValue(ContextMenuProperty, result);
- return result;
- }
- #endregion
- #region Add Element
- private void AddNewElement<TElement>(int row, int column)
- where TElement : DFLayoutElement, new()
- {
- var element = new TElement();
- element.Row = row;
- element.Column = column;
- var result = new FormControlGrid<TElement>().EditItems(new[] { element });
- if (result)
- {
- form.Elements.Add(element);
- Render();
- }
- }
- private void AddElementClick(Tuple<Type, int, int> tuple)
- {
- var method = typeof(DynamicFormDesignGrid)
- .GetMethod(nameof(AddNewElement), BindingFlags.NonPublic | BindingFlags.Instance)!
- .MakeGenericMethod(tuple.Item1);
- method.Invoke(this, new object[] { tuple.Item3, tuple.Item2 });
- }
- private void CreateLabelClick(Point point)
- {
- var label = new DFLayoutLabel
- {
- Row = (int)point.Y,
- Column = (int)point.X
- };
- var result = new FormControlGrid<DFLayoutLabel>().EditItems(new[] { label });
- if (result)
- {
- form.Elements.Add(label);
- Render();
- }
- }
- private void CreateHeaderClick(Point point)
- {
- var header = new DFLayoutHeader
- {
- Row = (int)point.Y,
- Column = (int)point.X
- };
- var result = new FormControlGrid<DFLayoutHeader>().EditItems(new[] { header });
- if (result)
- {
- form.Elements.Add(header);
- Render();
- }
- }
- private void CreateImageClick(Point point)
- {
- var image = new DFLayoutImage
- {
- Row = (int)point.Y,
- Column = (int)point.X
- };
- var result = new FormControlGrid<DFLayoutImage>().EditItems(new[] { image });
- if (result)
- {
- form.Elements.Add(image);
- Render();
- }
- }
- private void AddVariableClick(Tuple<DigitalFormVariable, int, int> tuple)
- {
- if(Activator.CreateInstance(tuple.Item1.FieldType()) is not DFLayoutField field)
- {
- MessageBox.Show(string.Format("{0}: Unknown Type {1}", tuple.Item1.Code, tuple.Item1.VariableType.ToString()));
- return;
- }
- field.Name = tuple.Item1.Code;
- field.Column = tuple.Item2;
- field.Row = tuple.Item3;
- form.Elements.Add(field);
- form.LoadVariable(tuple.Item1, field);
- Render();
- }
- private ContextMenu CreateEmptyCellMenu(Border border, int row, int column)
- {
- var result = new ContextMenu();
- result.Opened += (o, e) =>
- {
- result.Items.Clear();
- result.Items.Add(CreateMenuItem("Add Label", new Point(column, row), CreateLabelClick));
- result.Items.Add(CreateMenuItem("Add Header", new Point(column, row), CreateHeaderClick));
- result.Items.Add(CreateMenuItem("Add Image", new Point(column, row), CreateImageClick));
- var fields = CreateMenuItem("Add Field", new Point(column, row), null);
- var filtered = _variables.Where(x => !form.Elements.Any(v => string.Equals((v as DFLayoutField)?.Name, x.Code)));
- foreach (var variable in filtered)
- fields.Items.Add(CreateMenuItem(variable.Code, new Tuple<DigitalFormVariable, int, int>(variable, column, row),
- AddVariableClick));
- if (fields.Items.Count > 0)
- result.Items.Add(fields);
- var elements = CreateMenuItem("Add Object", new Point(column, row), null);
- var available = _elements.Where(x => x.AllowDuplicate || !form.Elements.Any(v => (v as DFLayoutElement)?.GetType() == x.ElementType)).ToArray();
- var cats = available.Select(x => x.Category).Distinct().OrderBy(x => x);
- foreach (var cat in cats)
- {
- var menu = elements;
- if (!string.IsNullOrWhiteSpace(cat))
- {
- menu = new MenuItem { Header = cat };
- elements.Items.Add(menu);
- }
- foreach (var element in available.Where(x => string.Equals(x.Category, cat)))
- menu.Items.Add(CreateMenuItem(element.Caption, new Tuple<Type, int, int>(element.ElementType, column, row), AddElementClick));
- }
- if (elements.Items.Count > 0)
- result.Items.Add(elements);
- };
- border.SetValue(ContextMenuProperty, result);
- return result;
- }
- #endregion
- #region Element Context Menu
- private void DeleteElementClick(DFLayoutControl control)
- {
- if (form.Elements.Contains(control))
- {
- form.Elements.Remove(control);
- Render();
- }
- }
- private void ElementPropertiesClick(DFLayoutControl control)
- {
- if(!elementgrids.TryGetValue(control.GetType(), out var grid))
- {
- var type = typeof(FormControlGrid<>).MakeGenericType(control.GetType());
- grid = (Activator.CreateInstance(type) as IDynamicGrid)!;
- elementgrids[control.GetType()] = grid;
- }
- if (grid.EditItems(new[] { control }))
- Render();
- }
- private ContextMenu CreateElementContextMenu(FrameworkElement element, DFLayoutControl control)
- {
- var result = new ContextMenu();
- result.Items.Add(CreateMenuItem("Edit Properties", control, ElementPropertiesClick));
- result.Items.Add(new Separator());
- result.Items.Add(CreateMenuItem("Delete Item", control, DeleteElementClick));
- element.SetValue(ContextMenuProperty, result);
- return result;
- }
- #endregion
- #endregion
- #region Render
- private static void SetDimensions(FrameworkElement element, int row, int column, int rowspan, int colspan, int offset)
- {
- if (column <= 0) return;
- if (row <= 0) return;
- element.MinHeight = 50.0F;
- element.MinWidth = 50.0F;
- element.SetGridPosition(row - offset, column - offset, rowspan, colspan);
- }
- private Border CreateBorder(int row, int column, int rowspan, int colspan, int offset, bool horzstar, bool vertstar)
- {
- var border = new Border();
- if (ShowBorders)
- {
- border.BorderThickness = new Thickness(
- 0.0F,//!IsDesigning && column == 1 - offset ? 0.00F /*0.75F*/ : 0.0F,
- 0.0F,//!IsDesigning && row == 1 - offset ? 0.00F /*0.75F*/ : 0.0F,
- column - offset == ColumnDefinitions.Count - 1 ? horzstar ? 0.00F : 0.75F : 0.75F,
- row - offset == RowDefinitions.Count - 1 ? vertstar ? 0.00F : 0.75F : 0.75F
- );
- border.BorderBrush = BorderBrush;
- }
- border.Background = BackgroundBrush;
- SetDimensions(border, row, column, rowspan, colspan, offset);
- return border;
- }
- private static BitmapImage HeaderClosed = Properties.Resources.rightarrow.AsBitmapImage(32, 32);
- private static BitmapImage HeaderOpen = Properties.Resources.downarrow.AsBitmapImage(32, 32);
- private FrameworkElement CreateHeader(DFLayoutHeader header)
- {
- var formHeader = new FormHeader
- {
- Collapsed = header.Collapsed,
- HeaderText = header.Header
- };
- formHeader.CollapsedChanged += (o, c) =>
- {
- CollapseRows(formHeader, c);
- };
- if (IsDesigning)
- {
- formHeader.IsEnabled = false;
- }
- return formHeader;
- }
- // DFLayoutField -> IDynamicFormFieldControl
- private static Dictionary<Type, Type>? _fieldControls;
- private DynamicFormControl? CreateFieldControl(DFLayoutField field)
- {
- if (_fieldControls == null)
- {
- _fieldControls = new();
- foreach (var controlType in CoreUtils.TypeList(
- AppDomain.CurrentDomain.GetAssemblies(),
- x => x.IsClass
- && !x.IsAbstract
- && !x.IsGenericType
- && x.IsAssignableTo(typeof(IDynamicFormFieldControl))))
- {
- var superDefinition = controlType.GetSuperclassDefinition(typeof(DynamicFormFieldControl<,,>));
- if (superDefinition != null)
- {
- _fieldControls[superDefinition.GenericTypeArguments[0]] = controlType;
- }
- }
- }
- var fieldControlType = _fieldControls.GetValueOrDefault(field.GetType());
- if (fieldControlType is not null)
- {
- var element = (Activator.CreateInstance(fieldControlType) as DynamicFormControl)!;
- if(element is IDynamicFormFieldControl fieldControl)
- {
- fieldControl.FieldChangedEvent += () =>
- {
- ChangeField(field.Name);
- };
- }
- return element;
- }
- return null;
- }
- private DynamicFormControl? CreateFieldPlaceholder(DFLayoutField field)
- {
- var controlType = typeof(DFFieldPlaceholderControl<>).MakeGenericType(field.GetType());
- return Activator.CreateInstance(controlType) as DynamicFormControl;
- }
- private DynamicFormControl? CreateControl(DFLayoutControl item)
- {
- if (item is DFLayoutField field)
- {
- if (IsDesigning)
- {
- return CreateFieldPlaceholder(field);
- }
- return CreateFieldControl(field);
- }
- else if (item is DFLayoutElement)
- {
- return IsDesigning
- ? new DFElementPlaceholderControl()
- : new DFElementControl();
- }
- else if (item is DFLayoutLabel)
- {
- return new DFLabelControl();
- }
- else if (item is DFLayoutHeader)
- {
- return new DFHeaderControl();
- }
- else if (item is DFLayoutImage)
- {
- return new DFImageControl();
- }
- return null;
- }
- private FrameworkElement? CreateElement(DFLayoutControl item)
- {
- var control = CreateControl(item);
- if(control != null)
- {
- control.FormDesignGrid = this;
- control.SetControl(item);
- }
- return control;
- }
- private void RenderElement(DFLayoutControl item, int offset, bool horzstar, bool vertstar)
- {
- var border = CreateBorder(item.Row, item.Column, item.RowSpan, item.ColumnSpan, offset, horzstar, vertstar);
- if (item is DFLayoutField field)
- border.Background = field.GetPropertyValue<bool>("Required") ? RequiredFieldBrush : FieldBrush;
- else
- border.Background = IsDesigning ? FieldBrush : BackgroundBrush;
- if (!ShowBorders)
- border.Padding = new Thickness(
- item.Column == 1 ? 4 : 2,
- item.Row == 1 ? 4 : 2,
- item.Column + item.ColumnSpan - 1 == ColumnDefinitions.Count ? 4 : 2,
- item.Row + item.RowSpan - 1 == RowDefinitions.Count ? 4 : 2
- );
- else
- border.Padding = new Thickness(5);
- var element = CreateElement(item);
- border.Child = element;
- Children.Add(border);
- if (IsDesigning)
- {
- CreateElementContextMenu(border, item);
- }
- if (element != null)
- {
- if(item is DFLayoutField)
- {
- element.IsEnabled = !IsReadOnly;
- }
- elementmap[item] = element;
- element.HorizontalAlignment = GetHorizontalAlignment(item.HorizontalAlignment);
- element.VerticalAlignment = GetVerticalAlignment(item.VerticalAlignment);
- if (IsDesigning)
- {
- CreateElementContextMenu(element, item);
- }
- }
- }
- private void AfterRender()
- {
- if (!IsDesigning)
- {
- foreach (var header in elementmap.Where(x => x.Value is FormHeader head).Select(x => x.Value).Cast<FormHeader>())
- {
- if (header.Collapsed)
- {
- CollapseRows(header, true);
- }
- }
- }
- OnAfterRender?.Invoke(this);
- }
- private void Render()
- {
- foreach (var item in elementmap.Keys)
- {
- var e = elementmap[item];
- if (VisualTreeHelper.GetParent(e) is Border parent)
- parent.Child = null;
- }
- elementmap.Clear();
- Children.Clear();
- ColumnDefinitions.Clear();
- RowDefinitions.Clear();
- if (form == null)
- return;
- foreach (var column in form.ColumnWidths)
- ColumnDefinitions.Add(new ColumnDefinition { Width = StringToGridLength(column) });
- foreach (var row in form.RowHeights)
- RowDefinitions.Add(new RowDefinition { Height = StringToGridLength(row), Tag = new RowData(row) });
- var horzstar = ColumnDefinitions.Any(x => x.Width.IsStar);
- var vertstar = RowDefinitions.Any(x => x.Height.IsStar);
- var offset = 1;
- if (IsDesigning)
- {
- ColumnDefinitions.Insert(0, new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
- RowDefinitions.Insert(0, new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
- var topleft = new Button();
- topleft.SetValue(RowProperty, 0);
- topleft.SetValue(ColumnProperty, 0);
- topleft.Content =
- ""; // new Image() { Source = _designing ? Properties.Resources.view.AsBitmapImage() : Properties.Resources.design.AsBitmapImage() };
- topleft.BorderBrush = BorderBrush;
- topleft.BorderThickness = new Thickness(
- 0.00F,
- 0.00F,
- 0.75F,
- 0.75F
- );
- topleft.Background = BackgroundBrush;
- //topleft.Click += (o, e) =>
- //{
- // Designing = !Designing;
- //};
- Children.Add(topleft);
- for (var i = 1; i < ColumnDefinitions.Count; i++)
- {
- var Button = new Button();
- Button.SetValue(RowProperty, 0);
- Button.SetValue(ColumnProperty, i);
- Button.Content = FormatGridLength(ColumnDefinitions[i].Width, ColumnDefinitions.Select(x => x.Width));
- Button.BorderBrush = BorderBrush;
- Button.BorderThickness = new Thickness(
- 0.00F, //(i == 1) ? 0.75F : 0.0F,
- 0.00F, //0.75F,
- i == ColumnDefinitions.Count - 1 ? horzstar ? 0.00F : 0.75F : 0.75F,
- 0.75F
- );
- Button.Background = BackgroundBrush;
- Button.Padding = new Thickness(10);
- Button.MinHeight = 35;
- CreateColumnMenu(Button, i - 1);
- AddClick(Button, i - 1, ColumnPropertiesClick);
- Children.Add(Button);
- }
- for (var i = 1; i < RowDefinitions.Count; i++)
- {
- var Button = new Button();
- Button.SetValue(RowProperty, i);
- Button.SetValue(ColumnProperty, 0);
- Button.Content = FormatGridLength(RowDefinitions[i].Height, RowDefinitions.Select(x => x.Height));
- Button.BorderBrush = BorderBrush;
- Button.BorderThickness = new Thickness(
- 0.00F, //0.75F,
- 0.00F, //(i == 1) ? 0.75F : 0.0F,
- 0.75F,
- i == RowDefinitions.Count - 1 ? vertstar ? 0.00F : 0.75F : 0.75F
- );
- Button.Background = BackgroundBrush;
- Button.Padding = new Thickness(10);
- CreateRowMenu(Button, i - 1);
- AddClick(Button, i - 1, RowPropertiesClick);
- Children.Add(Button);
- }
- offset = 0;
- }
- var filledcells = new List<Point>();
- foreach (var item in form.Elements)
- {
- try
- {
- if(item.Row > 0 && item.Column > 0)
- {
- RenderElement(item, offset, horzstar, vertstar);
- for (var c = item.Column; c < item.Column + item.ColumnSpan; c++)
- for (var r = item.Row; r < item.Row + item.RowSpan; r++)
- filledcells.Add(new Point(c, r));
- }
- }
- catch (Exception e)
- {
- Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
- }
- }
- for (var c = 1; c <= form.ColumnWidths.Count; c++)
- for (var r = 1; r <= form.RowHeights.Count; r++)
- if (!filledcells.Any(x => x.X.Equals(c) && x.Y.Equals(r)))
- {
- var border = CreateBorder(r, c, 1, 1, offset, horzstar, vertstar);
- if (IsDesigning)
- {
- CreateEmptyCellMenu(border, r, c);
- }
- Children.Add(border);
- }
- AfterRender();
- }
- #endregion
- /// <summary>
- /// Renders the form; after this is called, any changes to properties triggers a refresh.
- /// </summary>
- /// <remarks>
- /// This should be called before <see cref="LoadValues(Dictionary{string, object})"/> or <see cref="SaveValues"/>
- /// </remarks>
- public void Initialize()
- {
- GridInitialized = true;
- CheckRefresh(false);
- }
- private void CheckRefresh(bool keepValues = true)
- {
- if (!GridInitialized) return;
- if (_mode == FormMode.Designing)
- {
- Render();
- }
- else if(keepValues)
- {
- var values = SaveValues();
- Render();
- LoadValues(values);
- }
- else
- {
- Render();
- Form.EvaluateExpressions();
- }
- }
- #region Fields
- private void ChangeField(string fieldName)
- {
- if (!_changing)
- {
- form.ChangeField(fieldName);
- _isChanged = true;
- OnChanged?.Invoke(this, fieldName);
- }
- }
- private IDynamicFormFieldControl? GetFieldControl(DFLayoutField field)
- {
- return elementmap.GetValueOrDefault(field) as IDynamicFormFieldControl;
- }
- public void SetFieldValue(string fieldName, object? value)
- {
- var field = form.Elements.FirstOrDefault(x => string.Equals(fieldName, (x as DFLayoutField)?.Name)) as DFLayoutField;
- if (field != null)
- {
- value = field.ParseValue(value);
- var fieldControl = GetFieldControl(field);
- if (fieldControl != null)
- {
- string? property = null;
- if(DataModel != null)
- {
- property = field.GetPropertyValue<string>("Property");
- if (!string.IsNullOrWhiteSpace(property))
- {
- value = DataModel.GetEntityValue(property);
- }
- }
- if (string.IsNullOrWhiteSpace(property))
- {
- fieldControl.SetValue(value);
- }
- else
- {
- fieldControl.SetEntityValue(value);
- }
- }
- }
- }
- private object? GetFieldValue(DFLayoutField field, out object? entityValue)
- {
- var fieldControl = GetFieldControl(field);
- if(fieldControl != null)
- {
- entityValue = fieldControl.GetEntityValue();
- return fieldControl.GetValue();
- }
- entityValue = null;
- return null;
- }
- public object? GetFieldValue(string fieldName)
- {
- var field = form.Elements.Where(x => (x as DFLayoutField)?.Name == fieldName).FirstOrDefault() as DFLayoutField;
- if (field == null)
- {
- return null;
- }
- return GetFieldValue(field, out var entityValue);
- }
- /// <summary>
- /// Load values into editor; Must be called after <see cref="Initialize"/>.
- /// </summary>
- /// <remarks>
- /// Resets <see cref="IsChanged"/>.
- /// </remarks>
- /// <param name="values">A dictionary of <see cref="DigitalFormVariable.Code"/> -> value.</param>
- public void LoadValues(Dictionary<string, object> values)
- {
- _changing = true;
- foreach (var (name, value) in values)
- {
- SetFieldValue(name, value);
- }
- Form.EvaluateExpressions();
- _changing = false;
- _isChanged = false;
- }
- /// <summary>
- /// Takes values from editors and saves them to a dictionary; must be called after <see cref="Initialize"/>.
- /// </summary>
- /// <returns>A dictionary of <see cref="DigitalFormVariable.Code"/> -> value.</returns>
- public Dictionary<string, object> SaveValues()
- {
- var result = new Dictionary<string, object>();
- foreach (var formElement in form.Elements)
- if (formElement is DFLayoutField field)
- {
- var value = GetFieldValue(field, out var entityValue);
- if (value != null)
- result[field.Name] = value;
- if(DataModel != null)
- {
- var property = field.GetPropertyValue<string>("Property");
- if (!string.IsNullOrWhiteSpace(property))
- DataModel.SetEntityValue(property, entityValue);
- }
- }
- return result;
- }
- public bool Validate(out List<string> messages)
- {
- messages = new List<string>();
- var valid = true;
- foreach(var formElement in form.Elements)
- {
- if(formElement is DFLayoutField field)
- {
- var fieldControl = GetFieldControl(field);
- if(fieldControl != null && !fieldControl.Validate(out var message))
- {
- messages.Add(message);
- valid = false;
- }
- }
- }
- return valid;
- }
- #endregion
- }
- }
|