using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tango.Core.Commands;
using Tango.FSE.Diagnostics.Project;
namespace Tango.FSE.Diagnostics.Controls
{
///
/// Interaction logic for DiagnosticsGridLinesEditor.xaml
///
public partial class DiagnosticsGridLinesEditor : UserControl
{
private DiagnosticsGrid _columnsDiagnosticsGrid;
private DiagnosticsGrid _rowsDiagnosticsGrid;
private const int MAX_LENGTH = 20;
public class LineEditor
{
public int Position { get; set; }
}
public ObservableCollection Columns
{
get { return (ObservableCollection)GetValue(ColumnsProperty); }
set { SetValue(ColumnsProperty, value); }
}
public static readonly DependencyProperty ColumnsProperty =
DependencyProperty.Register("Columns", typeof(ObservableCollection), typeof(DiagnosticsGridLinesEditor), new PropertyMetadata(null, (d, e) => (d as DiagnosticsGridLinesEditor).LoadDefinitions()));
public ObservableCollection Rows
{
get { return (ObservableCollection)GetValue(RowsProperty); }
set { SetValue(RowsProperty, value); }
}
public static readonly DependencyProperty RowsProperty =
DependencyProperty.Register("Rows", typeof(ObservableCollection), typeof(DiagnosticsGridLinesEditor), new PropertyMetadata(null, (d, e) => (d as DiagnosticsGridLinesEditor).LoadDefinitions()));
public ObservableCollection ColumnsZero
{
get { return (ObservableCollection)GetValue(ColumnsZeroProperty); }
set { SetValue(ColumnsZeroProperty, value); }
}
public static readonly DependencyProperty ColumnsZeroProperty =
DependencyProperty.Register("ColumnsZero", typeof(ObservableCollection), typeof(DiagnosticsGridLinesEditor), new PropertyMetadata(null));
public ObservableCollection RowsZero
{
get { return (ObservableCollection)GetValue(RowsZeroProperty); }
set { SetValue(RowsZeroProperty, value); }
}
public static readonly DependencyProperty RowsZeroProperty =
DependencyProperty.Register("RowsZero", typeof(ObservableCollection), typeof(DiagnosticsGridLinesEditor), new PropertyMetadata(null));
public List HorizontalLineEditors
{
get { return (List)GetValue(HorizontalLineEditorsProperty); }
set { SetValue(HorizontalLineEditorsProperty, value); }
}
public static readonly DependencyProperty HorizontalLineEditorsProperty =
DependencyProperty.Register("HorizontalLineEditors", typeof(List), typeof(DiagnosticsGridLinesEditor), new PropertyMetadata(null));
public List VerticalLineEditors
{
get { return (List)GetValue(VerticalLineEditorsProperty); }
set { SetValue(VerticalLineEditorsProperty, value); }
}
public static readonly DependencyProperty VerticalLineEditorsProperty =
DependencyProperty.Register("VerticalLineEditors", typeof(List), typeof(DiagnosticsGridLinesEditor), new PropertyMetadata(null));
public DiagnosticsGridLinesEditor()
{
RowsZero = new ObservableCollection();
ColumnsZero = new ObservableCollection();
InitializeComponent();
}
private void LoadDefinitions()
{
if (Columns != null && Rows != null)
{
List verticalLines = new List();
for (int columnIndex = 0; columnIndex < Columns.Count; columnIndex++)
{
verticalLines.Add(new LineEditor() { Position = columnIndex });
}
List horizontalLines = new List();
for (int rowIndex = 0; rowIndex < Rows.Count; rowIndex++)
{
horizontalLines.Add(new LineEditor() { Position = rowIndex });
}
VerticalLineEditors = verticalLines;
HorizontalLineEditors = horizontalLines;
Columns.CollectionChanged -= Columns_CollectionChanged;
Rows.CollectionChanged -= Rows_CollectionChanged;
Columns.CollectionChanged += Columns_CollectionChanged;
Rows.CollectionChanged += Rows_CollectionChanged;
}
}
private void Rows_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
LoadDefinitions();
}
private void Columns_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
LoadDefinitions();
}
private void Vertical_Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
LineEditor line = (sender as FrameworkElement).DataContext as LineEditor;
if (line.Position == Columns.Count - 1) return;
var column = Columns[line.Position];
foreach (var definition in _columnsDiagnosticsGrid.ColumnDefinitions)
{
if (definition.Width.IsStar)
{
definition.Width = new GridLength(definition.ActualWidth);
}
}
DiagnosticsProjectTabColumnDefinition nextColumn = null;
if (line.Position < Columns.Count - 1)
{
nextColumn = Columns[line.Position + 1];
}
double oldWidth = column.Width.Value;
double newWidth = Math.Max(column.Width.Value + e.HorizontalChange, MAX_LENGTH);
double delta = newWidth - column.Width.Value;
if (nextColumn != null)
{
double nextWidth = nextColumn.Width.Value - delta;
if (nextWidth < MAX_LENGTH)
{
return;
}
}
column.Width = new GridLength(newWidth);
if (nextColumn != null)
{
nextColumn.Width = new GridLength(Math.Max(nextColumn.Width.Value - delta, MAX_LENGTH));
}
}
private void Horizontal_Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
LineEditor line = (sender as FrameworkElement).DataContext as LineEditor;
if (line.Position == Rows.Count - 1) return;
var row = Rows[line.Position];
foreach (var definition in _rowsDiagnosticsGrid.RowDefinitions)
{
if (definition.Height.IsStar)
{
definition.Height = new GridLength(definition.ActualHeight);
}
}
DiagnosticsProjectTabRowDefinition nextRow = null;
if (line.Position < Rows.Count - 1)
{
nextRow = Rows[line.Position + 1];
}
double oldHeight = row.Height.Value;
double newHeight = Math.Max(row.Height.Value + e.VerticalChange, MAX_LENGTH);
double delta = newHeight - row.Height.Value;
if (nextRow != null)
{
double nextHeight = nextRow.Height.Value - delta;
if (nextHeight < MAX_LENGTH)
{
return;
}
}
row.Height = new GridLength(newHeight);
if (nextRow != null)
{
nextRow.Height = new GridLength(Math.Max(nextRow.Height.Value - delta, MAX_LENGTH));
}
}
private void Columns_DiagnosticsGrid_Loaded(object sender, RoutedEventArgs e)
{
_columnsDiagnosticsGrid = sender as DiagnosticsGrid;
}
private void Rows_DiagnosticsGrid_Loaded(object sender, RoutedEventArgs e)
{
_rowsDiagnosticsGrid = sender as DiagnosticsGrid;
}
private void BtnRemoveRow_Click(object sender, RoutedEventArgs e)
{
if (Rows.Count > 0)
{
Rows.Remove(Rows.Last());
}
}
private void BtnAddRow_Click(object sender, RoutedEventArgs e)
{
Rows.Add(new DiagnosticsProjectTabRowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
}
private void BtnRemoveColumn_Click(object sender, RoutedEventArgs e)
{
if (Columns.Count > 0)
{
Columns.Remove(Columns.Last());
}
}
private void BtnAddColumn_Click(object sender, RoutedEventArgs e)
{
Columns.Add(new DiagnosticsProjectTabColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
}
}
}