Files
spark/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml.cs
T

187 lines
7.5 KiB
C#

using bts;
using bts.collections;
using bts.utils;
using mbts.ui.converters;
using mbts.utils;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace mbts.spark {
partial class LadderView : UserControl {
public const double
MinLeftPanelWidth = 60,
MaxLeftPanelWidth = 150,
MinZoomFactor = 0.5,
MaxZoomFactor = 1.5;
public static IMultiValueConverter PositionToolTipConverter =
ValueConverter.create<AccountTuple?, bool, bool, string>((account, show_day_pos, show_total_pos) => {
const string day_pos_text = "day", total_pos_text = "total";
if (!account.HasValue)
return string.Empty;
var sb = new StringBuilderEx(account.Value.name);
string primary_pos;
if (show_day_pos) {
primary_pos = day_pos_text;
if (show_total_pos) {
sb.Add(" '", primary_pos, " ");
sb.Add("(", total_pos_text, ")' ");
} else {
sb.Add(" ", primary_pos, " ");
}
} else if (show_total_pos) {
primary_pos = total_pos_text;
sb.Add(" ", primary_pos, " ");
} else {
// unexpected code path (the button for which this tooltip is destined should be invisible in this case), but no need to throw an exception.
return string.Empty;
}
sb.Add("position\n(click to copy ");
if (show_day_pos && show_total_pos)
sb.Add(primary_pos, " position ");
sb.Add("to size)");
return sb.ToString();
});
internal LadderView(LadderVM vm) {
InitializeComponent();
_column_headers_presenter.Columns = new GridViewColumnCollection();
_ladder.visible_columns_changed += () => _column_headers_presenter.Columns.SetItems(
_ladder.visible_column_types.Select(t => ColumnFactory.create(_ladder, t)));
this.vm = vm;
DataContext = vm;
DataContextChanged += datacontext_changed;
_ui_scaler.MouseDoubleClick += _ui_scaler_MouseDoubleClick;
this.PreviewMouseWheel += LadderView_PreviewMouseWheel;
this.PreviewMouseDown += LadderView_PreviewMouseDown;
}
// (Left|Right) CTRL + Wheel to zoom
// This is neat b/c the user doesn't even need to have the slider visible to zoom
private void LadderView_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) {
base.OnPreviewMouseWheel(e);
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) {
var delta = (e.Delta > 0) ? 0.1 : -0.1;
var proposed_scaler_value = _ui_scaler.Value + delta;
// Here we enforce the same contraints that are on the gui slider regarding min and max
if (MinZoomFactor < proposed_scaler_value && proposed_scaler_value < MaxZoomFactor) {
_ui_scaler.Value = proposed_scaler_value;
}
}
}
// (Left|Right) CTRL + Wheel Click to reset
private void LadderView_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
base.OnPreviewMouseDown(e);
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) {
if (e.MiddleButton == MouseButtonState.Pressed) {
_ui_scaler_MouseDoubleClick(_ui_scaler, e);
}
}
}
// Double click on scaler itself to reset
private void _ui_scaler_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e) {
if (sender is Slider _the_slider) {
_the_slider.Value = 1.0;
}
}
private void datacontext_changed(object sender, DependencyPropertyChangedEventArgs e) {
this.vm = DataContext as LadderVM;
}
// I found this on StackOverflow (and the XAML) and thought it was a neat
// way of enforcing a MinWidth on the columns. This is so that the columns
// cannot be resized to nothing with the mouse.
private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) {
if (sender != null) {
DependencyObject parent = sender as DependencyObject;
if (parent != null) {
try {
do {
parent = System.Windows.Media.VisualTreeHelper.GetParent(parent as DependencyObject);
} while (parent.GetType() != typeof(Canvas));
Canvas canvas = parent as Canvas;
if (canvas != null) {
if (canvas.ActualWidth + e.HorizontalChange < canvas.MinWidth) {
e.Handled = true;
}
}
} catch {
}
}
}
}
internal LadderVM vm { get; private set; }
protected override void OnDragOver(DragEventArgs e) {
e.Handled = true;
e.Effects = DragDropEffects.None;
_ladder.drag_out_of_bounds();
}
protected override void OnGiveFeedback(GiveFeedbackEventArgs e) {
e.Handled = true;
e.UseDefaultCursors = true;
}
static class ColumnFactory {
public static GridViewColumn create(LadderGridHost host, LadderGridHost.ColumnType type) {
string header = null, width_prop = null;
switch (type) {
case LadderGridHost.ColumnType.Order:
header = "Orders";
width_prop = nameof(LadderVM.orders_column_width);
break;
case LadderGridHost.ColumnType.Bid:
header = "Bid";
width_prop = nameof(LadderVM.bid_column_width);
break;
case LadderGridHost.ColumnType.Price:
header = "Price";
width_prop = nameof(LadderVM.price_column_width);
break;
case LadderGridHost.ColumnType.Ask:
header = "Ask";
width_prop = nameof(LadderVM.ask_column_width);
break;
case LadderGridHost.ColumnType.Volume:
header = "Volume";
width_prop = nameof(LadderVM.volume_column_width);
break;
case LadderGridHost.ColumnType.Last:
header = "Last";
width_prop = nameof(LadderVM.last_column_width);
break;
case LadderGridHost.ColumnType.PIQ:
header = "PIQ";
width_prop = nameof(LadderVM.piq_column_width);
break;
default: throw ExceptionBuilder.Argument.unexpected_case(type, nameof(type));
}
return _create(host, header, $"DataContext.{width_prop}");
}
static GridViewColumn _create(LadderGridHost host, string header, string width_prop) {
var gvc = new GridViewColumn { Header = header };
BindingOperations.SetBinding(gvc, GridViewColumn.WidthProperty, new Binding(width_prop) {
Source = host,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Mode = BindingMode.TwoWay
});
return gvc;
}
}
}
}