SPARK-2275 / SPARK-2277 remove DIYGrid hidden edit-control (a vestige from AW2, unused in Spark)
This commit is contained in:
1 parent
405685ece3
commit
c741093ab9
5 files changed
+247
-382
No files matched your search
@@ -332,14 +332,7 @@ namespace mbts.ui.controls {
|
||||
_row_presenter.ItemContainerGenerator;
|
||||
|
||||
private void OnCopy(object sender, ExecutedRoutedEventArgs e) {
|
||||
if (_row_presenter != null) {
|
||||
IEnumerable<RowBase> selected_rows = _row_presenter.SelectedItems.Cast<object>()
|
||||
.Select(_row_presenter.ItemContainerGenerator.ContainerFromItem).OfType<FrameworkElement>()
|
||||
.Select(el => el.descendants_of_type<RowBase>().FirstOrDefault())
|
||||
.Where(r => r != null);
|
||||
string clipboard_text = ClipboardHelper.GetExcelFormattedText(selected_rows.Select(r => r.GetClipboardCellStrings()).Where(cells => cells != null));
|
||||
if (!string.IsNullOrEmpty(clipboard_text)) ClipboardHelper.CopyTextToClipboard(clipboard_text);
|
||||
}
|
||||
// TODO: implement excel-style copy rows/cells
|
||||
}
|
||||
|
||||
private void OnCopyCanExecute(object sender, CanExecuteRoutedEventArgs e) {
|
||||
@@ -357,7 +350,6 @@ namespace mbts.ui.controls {
|
||||
ScrollBar _header_horizontal_scroll_bar;
|
||||
Panel _superheader_container;
|
||||
DIYGridSuperHeader[] _superheader_column_index = new DIYGridSuperHeader[0];
|
||||
FrameworkElement _edit_control;
|
||||
|
||||
GridViewColumnCollection _frozen_columns = new GridViewColumnCollection();
|
||||
GridViewColumnCollection _scroll_columns = new GridViewColumnCollection();
|
||||
@@ -376,19 +368,7 @@ namespace mbts.ui.controls {
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region Private Properties
|
||||
private RowBase GetSelectedRow() {
|
||||
// DIYRowBase is the template of the content of the ListboxItem, whew
|
||||
var item = _row_presenter.ItemContainerGenerator.ContainerFromItem(_row_presenter.SelectedItem) as FrameworkElement;
|
||||
if (item != null) {
|
||||
return item.descendants_of_type<RowBase>().FirstOrDefault();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// Private Methods
|
||||
#region Private Methods
|
||||
|
||||
void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) =>
|
||||
@@ -556,24 +536,6 @@ namespace mbts.ui.controls {
|
||||
grid.invalidate_column_layout();
|
||||
}
|
||||
}
|
||||
|
||||
private void commit_edit() {
|
||||
// This is a bit of a hack...
|
||||
foreach (var tb in _edit_control.descendants_of_type<TextBox>()) {
|
||||
var bind = tb.GetBindingExpression(TextBox.TextProperty);
|
||||
if (bind != null) {
|
||||
bind.UpdateSource();
|
||||
}
|
||||
}
|
||||
_edit_control.DataContext = null;
|
||||
_edit_control.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
private void cancel_edit() {
|
||||
_edit_control.DataContext = null;
|
||||
_edit_control.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Scrolling
|
||||
@@ -585,7 +547,6 @@ namespace mbts.ui.controls {
|
||||
private void on_horizontal_scroll(object sender, RoutedPropertyChangedEventArgs<double> e) {
|
||||
if (_header_scroll_viewer != null) {
|
||||
if (_header_horizontal_scroll_bar != null) {
|
||||
commit_edit();
|
||||
if (!DoubleUtil.AreClose(e.NewValue, _header_horizontal_scroll_bar.Value)) {
|
||||
_header_scroll_viewer.ScrollToHorizontalOffset(e.NewValue);
|
||||
invalidate_column_layout();
|
||||
@@ -831,13 +792,6 @@ namespace mbts.ui.controls {
|
||||
_scroll_horizontal.Value += delta;
|
||||
}
|
||||
|
||||
public T get_edit_control<T>() where T : FrameworkElement {
|
||||
// In the future we can add an EditControls property to the DIYGrid
|
||||
// and let the user supply a set of edit controls. Those controls could
|
||||
// be placed on a Canvas and moved that way.
|
||||
return (T)_edit_control;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// Overrides
|
||||
@@ -853,12 +807,6 @@ namespace mbts.ui.controls {
|
||||
_row_presenter = (ListBox)GetTemplateChild("_row_presenter");
|
||||
Log.assert_d(!_row_presenter.IsLoaded);
|
||||
_try_set_row_style();
|
||||
_row_presenter.Loaded += (o, e) => {
|
||||
var sv = _row_presenter.descendants_of_type<ScrollViewer>().FirstOrDefault();
|
||||
if (sv != null) {
|
||||
sv.ScrollChanged += (_, __) => commit_edit();
|
||||
}
|
||||
};
|
||||
_header_scroll_viewer = (ScrollViewer)GetTemplateChild("_header_scroll_viewer");
|
||||
Log.assert_d(!_header_scroll_viewer.IsLoaded);
|
||||
_header_scroll_viewer.Loaded += (o,e) => {
|
||||
@@ -877,10 +825,6 @@ namespace mbts.ui.controls {
|
||||
_superheader_container = GetTemplateChild("_super_headers") as Panel;
|
||||
Disposable.replace(ref _data_context_hooks, new DataContextHooks(this, DataContext));
|
||||
update_superheaders();
|
||||
|
||||
// TODO: All edit controls go in a canvas - edit api is called with an edit type.
|
||||
// cache requested controls in a dict and collapse them all
|
||||
_edit_control = GetTemplateChild("_edit") as FrameworkElement;
|
||||
}
|
||||
|
||||
protected override Size ArrangeOverride(Size arrangeBounds) {
|
||||
@@ -898,63 +842,6 @@ namespace mbts.ui.controls {
|
||||
base.OnRenderSizeChanged(sizeInfo);
|
||||
}
|
||||
|
||||
/* Keyboard navigation
|
||||
*
|
||||
* alt+arrow left/right => move left/right
|
||||
* up/down => move up/down
|
||||
* tab => move right
|
||||
* shift+tab => move left
|
||||
* enter => move down
|
||||
*/
|
||||
protected override void OnPreviewKeyDown(KeyEventArgs e) {
|
||||
RowBase row = GetSelectedRow();
|
||||
if (row != null) {
|
||||
if (e.SystemKey == Key.Right && Keyboard.Modifiers == ModifierKeys.Alt) {
|
||||
row.FocusNext();
|
||||
}
|
||||
else if (e.SystemKey == Key.Left && Keyboard.Modifiers == ModifierKeys.Alt) {
|
||||
row.FocusPrevious();
|
||||
}
|
||||
else if (e.Key == Key.Up) {
|
||||
if (_row_presenter.SelectedIndex > 0) {
|
||||
_row_presenter.SelectedIndex--;
|
||||
move_row_focus(row, GetSelectedRow());
|
||||
}
|
||||
}
|
||||
else if (e.Key == Key.Down) {
|
||||
_row_presenter.SelectedIndex++;
|
||||
move_row_focus(row, GetSelectedRow());
|
||||
}
|
||||
else if (e.Key == Key.Tab) {
|
||||
e.Handled = true;
|
||||
if (e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)) {
|
||||
row.FocusPrevious();
|
||||
}
|
||||
else {
|
||||
row.FocusNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
base.OnPreviewKeyDown(e);
|
||||
}
|
||||
|
||||
protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e) {
|
||||
base.OnLostKeyboardFocus(e);
|
||||
var origin = e.OriginalSource as FrameworkElement;
|
||||
if (!IsKeyboardFocusWithin &&
|
||||
!(e.NewFocus is ContextMenu && origin != null && origin.IsFocused)) {
|
||||
commit_edit();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnPreviewMouseDown(MouseButtonEventArgs e) {
|
||||
base.OnPreviewMouseDown(e);
|
||||
if (!e.Handled && e.OriginalSource is RowBase && e.LeftButton == MouseButtonState.Pressed) {
|
||||
var row = GetSelectedRow();
|
||||
if (row != null) row.ClearFocus();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMouseDoubleClick(MouseButtonEventArgs e) {
|
||||
base.OnMouseDoubleClick(e);
|
||||
if (e.ChangedButton == MouseButton.Left)
|
||||
@@ -962,37 +849,6 @@ namespace mbts.ui.controls {
|
||||
mb => mb.MouseAction == MouseAction.LeftDoubleClick,
|
||||
mb => mb.Command.ExecuteIfCan(mb.CommandParameter));
|
||||
}
|
||||
|
||||
private void move_row_focus(RowBase src, RowBase dst) {
|
||||
if (src != null) {
|
||||
var selected = src.SelectedColumn;
|
||||
src.ClearFocus();
|
||||
if (dst != null) {
|
||||
dst.SelectedColumn = selected;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void clear_other_row_focus(RowBase focus) {
|
||||
foreach (var row in this.descendants_of_type<RowBase>()) {
|
||||
if (!ReferenceEquals(focus, row)) {
|
||||
row.EraseFocusState();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnKeyDown(KeyEventArgs e) {
|
||||
if (e.Key == Key.Enter) {
|
||||
var previous_row = GetSelectedRow();
|
||||
_row_presenter.SelectedIndex++;
|
||||
move_row_focus(previous_row, GetSelectedRow());
|
||||
}
|
||||
else if (e.Key == Key.Escape) {
|
||||
cancel_edit();
|
||||
}
|
||||
base.OnKeyDown(e);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
class DataContextHooks : IDisposable {
|
||||
|
||||
@@ -356,24 +356,6 @@
|
||||
</bts:DIYGridRowsPresenter.Template>
|
||||
</bts:DIYGridRowsPresenter>
|
||||
<ScrollBar Grid.Row="3" Grid.Column="1" Orientation="Horizontal" LargeChange="10" x:Name="_scroll_horiz" Visibility="{TemplateBinding RowsHorizontalScrollBarVisibility}"/>
|
||||
|
||||
<!-- todo: All edit controls go in a canvas - edit api is called with an edit type. Hide all controls -->
|
||||
<!-- This stole hours of my life. The Text binding down there used to be 'value' and I was encountering -->
|
||||
<!-- another bug, but this thing sent me down a rabbit hole. F you TickUpDown -->
|
||||
<bts:TickUpDown
|
||||
x:Name="_edit"
|
||||
Grid.RowSpan="3"
|
||||
Grid.ColumnSpan="2"
|
||||
Width="0"
|
||||
Height="0"
|
||||
HorizontalAlignment="Left"
|
||||
VerticalAlignment="Top"
|
||||
TickUpCommand="{Binding tick_up}"
|
||||
TickDownCommand="{Binding tick_down}"
|
||||
Visibility="Collapsed"
|
||||
>
|
||||
<TextBox Text="{Binding value}"/>
|
||||
</bts:TickUpDown>
|
||||
</Grid>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
using mbts.spark;
|
||||
using mbts.ui.extensions;
|
||||
using mbts.ui.settings;
|
||||
using bts.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace mbts.ui.controls {
|
||||
partial class DIYGrid {
|
||||
public interface IRowVM {
|
||||
event Action invalidated;
|
||||
void raise_invalidated();
|
||||
}
|
||||
|
||||
public interface IAnimatableRowVM<TRowData> : IRowVM {
|
||||
ObservableCollection<RowAnimation> row_animations { get; }
|
||||
IReadOnlyList<IGridCellStore> cell_stores { get; }
|
||||
TRowData current_data { get; }
|
||||
event Action<(TRowData old_val, TRowData new_val)> updated;
|
||||
}
|
||||
|
||||
public interface IActionableRowVM {
|
||||
ICommand default_command { get; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,231 +1,140 @@
|
||||
using mbts.spark;
|
||||
using mbts.ui.extensions;
|
||||
using mbts.ui.settings;
|
||||
using bts.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
//using mbts.spark;
|
||||
//using mbts.ui.extensions;
|
||||
//using mbts.ui.settings;
|
||||
//using bts.utils;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Collections.ObjectModel;
|
||||
//using System.Linq;
|
||||
//using System.Reactive.Linq;
|
||||
//using System.Windows;
|
||||
//using System.Windows.Controls;
|
||||
//using System.Windows.Data;
|
||||
//using System.Windows.Input;
|
||||
//using System.Windows.Media;
|
||||
|
||||
namespace mbts.ui.controls {
|
||||
partial class DIYGrid {
|
||||
public interface IRowVM {
|
||||
event Action invalidated;
|
||||
void raise_invalidated();
|
||||
}
|
||||
//namespace mbts.ui.controls {
|
||||
// partial class DIYGrid {
|
||||
// // Changes to RowBase are discouraged; however, if necessary, they should be made with extreme caution.
|
||||
// // Please try to accomplish new/different Row behavior through [RowBase]inhertiance before considering changing RowBase.
|
||||
// // If you think changing RowBase is the best option, please consult Eric or John before committing such changes.
|
||||
// public abstract class RowBase : FrameworkElement {
|
||||
// protected DIYGrid _grid;
|
||||
// protected ListBoxItem _container;
|
||||
// protected int _selected_cell = RowUtils.NO_CELL;
|
||||
// protected List<ColumnGeometry> _column_geometry = new List<ColumnGeometry>();
|
||||
// IFormatCellsToClipboard _clipboard_formatter;
|
||||
// IDisposable _container_hooks;
|
||||
|
||||
public interface IAnimatableRowVM<TRowData> : IRowVM {
|
||||
ObservableCollection<RowAnimation> row_animations { get; }
|
||||
IReadOnlyList<IGridCellStore> cell_stores { get; }
|
||||
TRowData current_data { get; }
|
||||
event Action<(TRowData old_val, TRowData new_val)> updated;
|
||||
}
|
||||
// protected RowBase() {
|
||||
// DataContextChanged += (s, e) => _clipboard_formatter = e.NewValue as IFormatCellsToClipboard;
|
||||
// Loaded += (s, e) => {
|
||||
// Disposable.dispose(ref _container_hooks);
|
||||
// if (_grid != null) {
|
||||
// _grid.ColumnLayoutChanged -= on_columns_changed;
|
||||
// BindingOperations.ClearBinding(this, HeightProperty);
|
||||
// }
|
||||
// _container = this.find_ancestor_of_type<ListBoxItem>();
|
||||
// _container_hooks = _container.SubscribeSizeChanged(InvalidateMeasure);
|
||||
// _grid = _container.find_ancestor_of_type<DIYGrid>();
|
||||
// _grid.ColumnLayoutChanged += on_columns_changed;
|
||||
// _column_geometry = _grid.GetColumnGeometry().ToList();
|
||||
// BindingOperations.SetBinding(this, HeightProperty, new Binding("RowHeight") {
|
||||
// Source = _grid
|
||||
// });
|
||||
// on_grid_changed();
|
||||
// InvalidateVisual();
|
||||
// };
|
||||
// void on_columns_changed(List<ColumnGeometry> columnGeometry) {
|
||||
// _column_geometry = columnGeometry;
|
||||
// InvalidateVisual();
|
||||
// }
|
||||
// }
|
||||
|
||||
public interface IActionableRowVM {
|
||||
ICommand default_command { get; }
|
||||
}
|
||||
// internal int SelectedColumn {
|
||||
// get => _selected_cell;
|
||||
// set {
|
||||
// _selected_cell = value;
|
||||
// _grid.commit_edit();
|
||||
// if (_selected_cell == RowUtils.NO_CELL) {
|
||||
// ClearFocus();
|
||||
// } else {
|
||||
// var geom = RowUtils.column_at(_column_geometry, value);
|
||||
// if (geom != null) {
|
||||
// OnFocusMoveCore(geom);
|
||||
// } else {
|
||||
// ClearFocus();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
public static class RowUtils {
|
||||
public const int NO_CELL = -1;
|
||||
// internal void FocusNext() => SelectedColumn++;
|
||||
// internal void FocusPrevious() => SelectedColumn--;
|
||||
// internal void EraseFocusState() => _selected_cell = RowUtils.NO_CELL;
|
||||
|
||||
public static ColumnGeometry? column_at(IReadOnlyList<ColumnGeometry> column_geometry, Point p) {
|
||||
foreach (var col in column_geometry) {
|
||||
if (p.X >= col.left && p.X <= col.right) {
|
||||
return col;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// protected virtual void on_grid_changed() { }
|
||||
|
||||
public static ColumnGeometry? column_at(IReadOnlyList<ColumnGeometry> column_geometry, int index) {
|
||||
if (index >= 0 && index < column_geometry.Count) {
|
||||
return column_geometry[index];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// protected override Size ArrangeOverride(Size finalSize) => finalSize;
|
||||
// protected override Size MeasureOverride(Size availableSize) =>
|
||||
// RowUtils.measure(_container, _column_geometry, availableSize, _grid.RowHeight);
|
||||
|
||||
public static int column_index(IReadOnlyList<ColumnGeometry> column_geometry, ColumnGeometry column) {
|
||||
for (var x = 0; x < column_geometry.Count; ++x) {
|
||||
if (column_geometry[x].column == column.column) {
|
||||
return x;
|
||||
}
|
||||
}
|
||||
return NO_CELL;
|
||||
}
|
||||
|
||||
public static Size measure(ListBoxItem container, IReadOnlyCollection<ColumnGeometry> column_geometry, Size available_size, double row_height) {
|
||||
if (container != null) {
|
||||
double cw = container.ActualWidth;
|
||||
if (!double.IsNaN(cw) && cw <= available_size.Width)
|
||||
return new Size(cw, row_height);
|
||||
}
|
||||
double col_width_sum = column_geometry.AggregateWhile(0.0, (width, col) => width + col.width, (width, col) => width <= available_size.Width, available_size.Width);
|
||||
return new Size(col_width_sum, row_height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scrolls the given column into view.
|
||||
/// Returns the adjusted ColumnGeometry for the new column position
|
||||
/// <para/>Note: the new column geometry has been pre-computed and the grid layout pass has probably not yet happened
|
||||
/// </summary>
|
||||
public static ColumnGeometry scroll_into_view(DIYGrid grid, ColumnGeometry col) {
|
||||
if (col.is_partially_obscured) {
|
||||
var obscured_width = col.left - col.unclipped_left;
|
||||
grid.scroll_horizontal(-obscured_width);
|
||||
return new ColumnGeometry() {
|
||||
column = col.column,
|
||||
left = col.left,
|
||||
unclipped_left = col.left
|
||||
};
|
||||
}
|
||||
return col;
|
||||
}
|
||||
|
||||
public static FrameworkElement position_control_over(FrameworkElement row_view, DIYGrid grid, FrameworkElement control, ColumnGeometry column) {
|
||||
Point actual_left = row_view.TranslatePoint(new Point(column.unclipped_left, 0), grid);
|
||||
// Use the margin to position the control
|
||||
control.Margin = new Thickness(actual_left.X + 1, actual_left.Y + 1, 0, 0);
|
||||
control.Width = Math.Max(column.width - 2, 0);
|
||||
control.Height = row_view.Height;
|
||||
if (column.is_partially_obscured) {
|
||||
var clipped_width = column.left - column.unclipped_left;
|
||||
// Rect is in the coordinate space of the edit control
|
||||
control.Clip = new RectangleGeometry(new Rect(clipped_width, 0, column.width - clipped_width, row_view.Height));
|
||||
} else {
|
||||
control.Clip = null;
|
||||
}
|
||||
control.Visibility = Visibility.Visible;
|
||||
control.Focus();
|
||||
return control;
|
||||
}
|
||||
}
|
||||
|
||||
// Changes to RowBase are discouraged; however, if necessary, they should be made with extreme caution.
|
||||
// Please try to accomplish new/different Row behavior through [RowBase]inhertiance before considering changing RowBase.
|
||||
// If you think changing RowBase is the best option, please consult Eric or John before committing such changes.
|
||||
public abstract class RowBase : FrameworkElement {
|
||||
protected DIYGrid _grid;
|
||||
protected ListBoxItem _container;
|
||||
protected int _selected_cell = RowUtils.NO_CELL;
|
||||
protected List<ColumnGeometry> _column_geometry = new List<ColumnGeometry>();
|
||||
IFormatCellsToClipboard _clipboard_formatter;
|
||||
IDisposable _container_hooks;
|
||||
|
||||
protected RowBase() {
|
||||
DataContextChanged += (s, e) => _clipboard_formatter = e.NewValue as IFormatCellsToClipboard;
|
||||
Loaded += (s, e) => {
|
||||
Disposable.dispose(ref _container_hooks);
|
||||
if (_grid != null) {
|
||||
_grid.ColumnLayoutChanged -= on_columns_changed;
|
||||
BindingOperations.ClearBinding(this, HeightProperty);
|
||||
}
|
||||
_container = this.find_ancestor_of_type<ListBoxItem>();
|
||||
_container_hooks = _container.SubscribeSizeChanged(InvalidateMeasure);
|
||||
_grid = _container.find_ancestor_of_type<DIYGrid>();
|
||||
_grid.ColumnLayoutChanged += on_columns_changed;
|
||||
_column_geometry = _grid.GetColumnGeometry().ToList();
|
||||
BindingOperations.SetBinding(this, HeightProperty, new Binding("RowHeight") {
|
||||
Source = _grid
|
||||
});
|
||||
on_grid_changed();
|
||||
InvalidateVisual();
|
||||
};
|
||||
void on_columns_changed(List<ColumnGeometry> columnGeometry) {
|
||||
_column_geometry = columnGeometry;
|
||||
InvalidateVisual();
|
||||
}
|
||||
}
|
||||
|
||||
internal int SelectedColumn {
|
||||
get => _selected_cell;
|
||||
set {
|
||||
_selected_cell = value;
|
||||
_grid.commit_edit();
|
||||
if (_selected_cell == RowUtils.NO_CELL) {
|
||||
ClearFocus();
|
||||
} else {
|
||||
var geom = RowUtils.column_at(_column_geometry, value);
|
||||
if (geom != null) {
|
||||
OnFocusMoveCore(geom);
|
||||
} else {
|
||||
ClearFocus();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void FocusNext() => SelectedColumn++;
|
||||
internal void FocusPrevious() => SelectedColumn--;
|
||||
internal void EraseFocusState() => _selected_cell = RowUtils.NO_CELL;
|
||||
|
||||
protected virtual void on_grid_changed() { }
|
||||
|
||||
protected override Size ArrangeOverride(Size finalSize) => finalSize;
|
||||
protected override Size MeasureOverride(Size availableSize) =>
|
||||
RowUtils.measure(_container, _column_geometry, availableSize, _grid.RowHeight);
|
||||
|
||||
protected void RenderGridLines(DrawingContext dc, Pen pen) {
|
||||
foreach (var r in _column_geometry) {
|
||||
dc.DrawRectangle(pen.Brush, null, new Rect(r.right, 0, pen.Thickness, Height));
|
||||
}
|
||||
}
|
||||
// protected void RenderGridLines(DrawingContext dc, Pen pen) {
|
||||
// foreach (var r in _column_geometry) {
|
||||
// dc.DrawRectangle(pen.Brush, null, new Rect(r.right, 0, pen.Thickness, Height));
|
||||
// }
|
||||
// }
|
||||
|
||||
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
|
||||
base.OnMouseLeftButtonDown(e);
|
||||
var position = e.GetPosition(this);
|
||||
var col = RowUtils.column_at(_column_geometry, position);
|
||||
if (col != null) {
|
||||
// Best just to provide all the data and let the callee figure out what it needs
|
||||
// Provide general helper methods like PositionControl/ScrollIntoView
|
||||
_grid.commit_edit();
|
||||
_selected_cell = RowUtils.column_index(_column_geometry, col.Value);
|
||||
_grid.commit_edit();
|
||||
OnFocusMoveCore(col);
|
||||
OnClick(col.Value);
|
||||
}
|
||||
else ClearFocus();
|
||||
}
|
||||
// protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
|
||||
// base.OnMouseLeftButtonDown(e);
|
||||
// var position = e.GetPosition(this);
|
||||
// var col = RowUtils.column_at(_column_geometry, position);
|
||||
// if (col != null) {
|
||||
// // Best just to provide all the data and let the callee figure out what it needs
|
||||
// // Provide general helper methods like PositionControl/ScrollIntoView
|
||||
// _grid.commit_edit();
|
||||
// _selected_cell = RowUtils.column_index(_column_geometry, col.Value);
|
||||
// _grid.commit_edit();
|
||||
// OnFocusMoveCore(col);
|
||||
// OnClick(col.Value);
|
||||
// }
|
||||
// else ClearFocus();
|
||||
// }
|
||||
|
||||
protected override void OnLostFocus(RoutedEventArgs e) {
|
||||
base.OnLostFocus(e);
|
||||
ClearFocus();
|
||||
}
|
||||
protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e) {
|
||||
base.OnLostKeyboardFocus(e);
|
||||
ClearFocus();
|
||||
}
|
||||
internal void ClearFocus() {
|
||||
_grid.commit_edit();
|
||||
_selected_cell = RowUtils.NO_CELL;
|
||||
OnFocusMove(null);
|
||||
var ctrl = _grid.get_edit_control<FrameworkElement>();
|
||||
ctrl.Visibility = Visibility.Collapsed;
|
||||
ctrl.DataContext = null;
|
||||
OnClearFocusOverride();
|
||||
}
|
||||
// protected override void OnLostFocus(RoutedEventArgs e) {
|
||||
// base.OnLostFocus(e);
|
||||
// ClearFocus();
|
||||
// }
|
||||
// protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e) {
|
||||
// base.OnLostKeyboardFocus(e);
|
||||
// ClearFocus();
|
||||
// }
|
||||
// internal void ClearFocus() {
|
||||
// _grid.commit_edit();
|
||||
// _selected_cell = RowUtils.NO_CELL;
|
||||
// OnFocusMove(null);
|
||||
// var ctrl = _grid.get_edit_control<FrameworkElement>();
|
||||
// ctrl.Visibility = Visibility.Collapsed;
|
||||
// ctrl.DataContext = null;
|
||||
// OnClearFocusOverride();
|
||||
// }
|
||||
|
||||
private void OnFocusMoveCore(ColumnGeometry? new_column) {
|
||||
_grid.clear_other_row_focus(this);
|
||||
OnFocusMove(new_column);
|
||||
}
|
||||
// private void OnFocusMoveCore(ColumnGeometry? new_column) {
|
||||
// _grid.clear_other_row_focus(this);
|
||||
// OnFocusMove(new_column);
|
||||
// }
|
||||
|
||||
#region Virtual Methods for derived type to implement
|
||||
protected virtual void OnClearFocusOverride() { }
|
||||
protected abstract void OnClick(ColumnGeometry column);
|
||||
protected abstract void OnFocusMove(ColumnGeometry? new_column);
|
||||
public virtual string[] GetClipboardCellStrings() { return _clipboard_formatter == null ? null : _clipboard_formatter.get_cell_clipboard_strings(); }
|
||||
#endregion
|
||||
// #region Virtual Methods for derived type to implement
|
||||
// protected virtual void OnClearFocusOverride() { }
|
||||
// protected abstract void OnClick(ColumnGeometry column);
|
||||
// protected abstract void OnFocusMove(ColumnGeometry? new_column);
|
||||
// public virtual string[] GetClipboardCellStrings() { return _clipboard_formatter == null ? null : _clipboard_formatter.get_cell_clipboard_strings(); }
|
||||
// #endregion
|
||||
|
||||
public IObservable<ColumnGeometry?> ObserveMouseHover() =>
|
||||
Observable.FromEvent<MouseEventHandler, MouseEventArgs>(raw_cb => (s, e) => raw_cb(e), cb => MouseMove += cb, cb => MouseMove -= cb)
|
||||
.Select(e => RowUtils.column_at(_column_geometry, e.GetPosition(this)))
|
||||
.DistinctUntilChanged(EqualityComparer.Create<ColumnGeometry?>((x, y) => x == null ? y == null : x.Value.column == y.Value.column));
|
||||
}
|
||||
}
|
||||
}
|
||||
// public IObservable<ColumnGeometry?> ObserveMouseHover() =>
|
||||
// Observable.FromEvent<MouseEventHandler, MouseEventArgs>(raw_cb => (s, e) => raw_cb(e), cb => MouseMove += cb, cb => MouseMove -= cb)
|
||||
// .Select(e => RowUtils.column_at(_column_geometry, e.GetPosition(this)))
|
||||
// .DistinctUntilChanged(EqualityComparer.Create<ColumnGeometry?>((x, y) => x == null ? y == null : x.Value.column == y.Value.column));
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace mbts.ui.controls {
|
||||
partial class DIYGrid {
|
||||
public static class RowUtils {
|
||||
public const int NO_CELL = -1;
|
||||
|
||||
public static ColumnGeometry? column_at(IReadOnlyList<ColumnGeometry> column_geometry, Point p) {
|
||||
foreach (var col in column_geometry) {
|
||||
if (p.X >= col.left && p.X <= col.right) {
|
||||
return col;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ColumnGeometry? column_at(IReadOnlyList<ColumnGeometry> column_geometry, int index) {
|
||||
if (index >= 0 && index < column_geometry.Count) {
|
||||
return column_geometry[index];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int column_index(IReadOnlyList<ColumnGeometry> column_geometry, ColumnGeometry column) {
|
||||
for (var x = 0; x < column_geometry.Count; ++x) {
|
||||
if (column_geometry[x].column == column.column) {
|
||||
return x;
|
||||
}
|
||||
}
|
||||
return NO_CELL;
|
||||
}
|
||||
|
||||
public static Size measure(ListBoxItem container, IReadOnlyCollection<ColumnGeometry> column_geometry, Size available_size, double row_height) {
|
||||
if (container != null) {
|
||||
double cw = container.ActualWidth;
|
||||
if (!double.IsNaN(cw) && cw <= available_size.Width)
|
||||
return new Size(cw, row_height);
|
||||
}
|
||||
double col_width_sum = column_geometry.AggregateWhile(0.0, (width, col) => width + col.width, (width, col) => width <= available_size.Width, available_size.Width);
|
||||
return new Size(col_width_sum, row_height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scrolls the given column into view.
|
||||
/// Returns the adjusted ColumnGeometry for the new column position
|
||||
/// <para/>Note: the new column geometry has been pre-computed and the grid layout pass has probably not yet happened
|
||||
/// </summary>
|
||||
public static ColumnGeometry scroll_into_view(DIYGrid grid, ColumnGeometry col) {
|
||||
if (col.is_partially_obscured) {
|
||||
var obscured_width = col.left - col.unclipped_left;
|
||||
grid.scroll_horizontal(-obscured_width);
|
||||
return new ColumnGeometry() {
|
||||
column = col.column,
|
||||
left = col.left,
|
||||
unclipped_left = col.left
|
||||
};
|
||||
}
|
||||
return col;
|
||||
}
|
||||
|
||||
public static FrameworkElement position_control_over(FrameworkElement row_view, DIYGrid grid, FrameworkElement control, ColumnGeometry column) {
|
||||
Point actual_left = row_view.TranslatePoint(new Point(column.unclipped_left, 0), grid);
|
||||
// Use the margin to position the control
|
||||
control.Margin = new Thickness(actual_left.X + 1, actual_left.Y + 1, 0, 0);
|
||||
control.Width = Math.Max(column.width - 2, 0);
|
||||
control.Height = row_view.Height;
|
||||
if (column.is_partially_obscured) {
|
||||
var clipped_width = column.left - column.unclipped_left;
|
||||
// Rect is in the coordinate space of the edit control
|
||||
control.Clip = new RectangleGeometry(new Rect(clipped_width, 0, column.width - clipped_width, row_view.Height));
|
||||
} else {
|
||||
control.Clip = null;
|
||||
}
|
||||
control.Visibility = Visibility.Visible;
|
||||
control.Focus();
|
||||
return control;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user