|
@@ -22,6 +22,7 @@ using System.Windows.Media.Imaging;
|
|
|
using InABox.Clients;
|
|
|
using InABox.Core;
|
|
|
using InABox.WPF;
|
|
|
+using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
|
using Brush = System.Windows.Media.Brush;
|
|
|
using Color = System.Drawing.Color;
|
|
|
using Image = System.Windows.Controls.Image;
|
|
@@ -32,12 +33,15 @@ namespace InABox.DynamicGrid
|
|
|
{
|
|
|
public class DynamicFormCreateElementArgs : EventArgs
|
|
|
{
|
|
|
- public DynamicFormCreateElementArgs(DFLayoutElement element)
|
|
|
+ public DynamicFormCreateElementArgs(DFLayoutElement element, string name)
|
|
|
{
|
|
|
Element = element;
|
|
|
+ Name = name;
|
|
|
}
|
|
|
|
|
|
public DFLayoutElement Element { get; }
|
|
|
+
|
|
|
+ public string Name { get; }
|
|
|
}
|
|
|
|
|
|
public delegate FrameworkElement DynamicFormCreateElementDelegate(object sender, DynamicFormCreateElementArgs e);
|
|
@@ -107,6 +111,7 @@ namespace InABox.DynamicGrid
|
|
|
#region Backing Properties
|
|
|
|
|
|
private readonly List<DynamicFormElement> _elements = new();
|
|
|
+ private readonly List<DynamicFormElementAction> _elementActions = new();
|
|
|
private bool _showBorders = true;
|
|
|
private IDigitalFormDataModel? _datamodel;
|
|
|
private DFLayout form = new();
|
|
@@ -274,33 +279,63 @@ namespace InABox.DynamicGrid
|
|
|
public FrameworkElement? Element { get; set; }
|
|
|
public bool AllowDuplicate { get; set; }
|
|
|
|
|
|
- public DynamicFormElement(string caption, Type elementType, string category, FrameworkElement? element, bool allowDuplicate)
|
|
|
+ public bool Visible { get; set; }
|
|
|
+
|
|
|
+ public DynamicFormElement(string caption, Type elementType, string category, FrameworkElement? element, bool allowDuplicate, bool visible)
|
|
|
{
|
|
|
Caption = caption;
|
|
|
ElementType = elementType;
|
|
|
Category = category;
|
|
|
Element = element;
|
|
|
AllowDuplicate = allowDuplicate;
|
|
|
+ Visible = visible;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ class DynamicFormElementAction
|
|
|
+ {
|
|
|
+ public string Caption { get; set; }
|
|
|
+
|
|
|
+ public Bitmap? Image { get; set; }
|
|
|
+
|
|
|
+ public string Category { get; set; }
|
|
|
+
|
|
|
+ public object? Tag { get; set; }
|
|
|
+
|
|
|
+ public Func<object?, DFLayoutElement?> OnClick { get; set; }
|
|
|
+
|
|
|
+ public DynamicFormElementAction(string caption, Bitmap? image, string category, object? tag, Func<object?, DFLayoutElement> onClick)
|
|
|
+ {
|
|
|
+ Caption = caption;
|
|
|
+ Image = image;
|
|
|
+ Category = category;
|
|
|
+ Tag = tag;
|
|
|
+ OnClick = onClick;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void AddElement<TElement>(string caption, string category, bool allowduplicate = false)
|
|
|
+ public void AddElement<TElement>(string caption, string category, bool allowduplicate = false, bool visible = true)
|
|
|
where TElement : DFLayoutElement
|
|
|
{
|
|
|
- AddElement(typeof(TElement), caption, category, allowduplicate);
|
|
|
+ AddElement(typeof(TElement), caption, category, allowduplicate, visible: visible);
|
|
|
}
|
|
|
|
|
|
- public void AddElement(Type TElement, string caption, string category, bool allowduplicate = false)
|
|
|
+ public void AddElement(Type TElement, string caption, string category, bool allowduplicate = false, bool visible = true)
|
|
|
{
|
|
|
- _elements.Add(new DynamicFormElement(caption, TElement, category, null, allowduplicate));
|
|
|
+ _elements.Add(new DynamicFormElement(caption, TElement, category, null, allowduplicate, visible));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void AddElementAction<TTag>(string caption, Bitmap? image, string category, TTag tag, Func<TTag, DFLayoutElement?> onClick)
|
|
|
+ {
|
|
|
+ _elementActions.Add(new(caption, image, category, tag, x => onClick((TTag)x)));
|
|
|
}
|
|
|
|
|
|
internal FrameworkElement? CreateElement(DFLayoutElement element)
|
|
|
{
|
|
|
var elementType = element.GetType();
|
|
|
- if(_elements.Any(x => x.ElementType == elementType))
|
|
|
+ if(_elements.FirstOrDefault(x => x.ElementType == elementType) is DynamicFormElement el)
|
|
|
{
|
|
|
- return OnCreateElement?.Invoke(this, new DynamicFormCreateElementArgs(element));
|
|
|
+ return OnCreateElement?.Invoke(this, new DynamicFormCreateElementArgs(element, el.Caption));
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
@@ -1467,6 +1502,17 @@ namespace InABox.DynamicGrid
|
|
|
method.Invoke(this, new object[] { tuple.Item2 });
|
|
|
}
|
|
|
|
|
|
+ private void ElementActionClick(Tuple<DynamicFormElementAction, CellRange> tuple)
|
|
|
+ {
|
|
|
+ var element = tuple.Item1.OnClick(tuple.Item1.Tag);
|
|
|
+ if(element is not null)
|
|
|
+ {
|
|
|
+ SetControlRange(element, tuple.Item2);
|
|
|
+ form.Elements.Add(element);
|
|
|
+ Render();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void SetControlRange(DFLayoutControl control, CellRange range)
|
|
|
{
|
|
|
var minRow = Math.Min(range.StartRow, range.EndRow);
|
|
@@ -1575,8 +1621,10 @@ namespace InABox.DynamicGrid
|
|
|
|
|
|
var elements = CreateMenuItem("Add Object", cellRange, 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);
|
|
|
+ var available = _elements.Where(x => x.Visible && (x.AllowDuplicate || !form.Elements.Any(v => (v as DFLayoutElement)?.GetType() == x.ElementType)))
|
|
|
+ .ToArray();
|
|
|
+
|
|
|
+ var cats = available.Select(x => x.Category).Concat(_elementActions.Select(x => x.Category)).Distinct().OrderBy(x => x);
|
|
|
foreach (var cat in cats)
|
|
|
{
|
|
|
var parentMenu = elements;
|
|
@@ -1586,8 +1634,16 @@ namespace InABox.DynamicGrid
|
|
|
elements.Items.Add(parentMenu);
|
|
|
}
|
|
|
|
|
|
+ foreach(var action in _elementActions.Where(x => x.Category == cat))
|
|
|
+ {
|
|
|
+ parentMenu.AddItem(action.Caption, action.Image, new Tuple<DynamicFormElementAction, CellRange>(action, cellRange), ElementActionClick);
|
|
|
+ }
|
|
|
+
|
|
|
+ parentMenu.AddSeparatorIfNeeded();
|
|
|
+
|
|
|
foreach (var element in available.Where(x => string.Equals(x.Category, cat)))
|
|
|
parentMenu.AddItem(element.Caption, null, new Tuple<Type, CellRange>(element.ElementType, cellRange), AddElementClick);
|
|
|
+ parentMenu.RemoveUnnecessarySeparators();
|
|
|
}
|
|
|
|
|
|
if (elements.Items.Count > 0)
|