256 lines
11 KiB
C#
256 lines
11 KiB
C#
using bts;
|
|
using bts.collections;
|
|
using bts.utils;
|
|
using mbts.ui;
|
|
using mbts.ui.converters;
|
|
using mbts.ui.extensions;
|
|
using mbts.utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Controls.Primitives;
|
|
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, bool, string>((account, show_day_pos, show_raw_day_pos, show_total_pos) => {
|
|
const string day_pos_text = "day", raw_day_pos_text = "raw day", total_pos_text = "total";
|
|
if (!account.HasValue)
|
|
return string.Empty;
|
|
/// Note: 'day,' and 'raw day,' are interchangeable wrt the string format, 'total' however is treated differently.
|
|
/// Different Formats:
|
|
// No parts are visible:
|
|
// null
|
|
//
|
|
// 1 part 'foo' is visible:
|
|
// "<account> foo position
|
|
// click to copy to size"
|
|
//
|
|
// 2 parts 'day' and 'raw day' are visible:
|
|
// "<account> 'day / raw day' position
|
|
// click to copy day to size"
|
|
//
|
|
// 2 parts 'day' and 'total' are visible:
|
|
// "<account> 'day (total)' position
|
|
// click to copy day to size"
|
|
//
|
|
|
|
string primary_pos = null;
|
|
// part count is incremented in the 'add_part' method
|
|
int part_count = 0;
|
|
var sb = new StringBuilderEx();
|
|
|
|
if (show_day_pos) add_part(day_pos_text, " / ");
|
|
if (show_raw_day_pos) add_part(raw_day_pos_text, " / ");
|
|
if (show_total_pos) add_part(total_pos_text, " (", ")");
|
|
//
|
|
if (part_count > 1) {
|
|
// This is a complex expression (showing more than 1 value) - wrap it in single quotes.
|
|
// Explicitly state which value will be copied to the size input.
|
|
sb.Insert(0, "'");
|
|
sb.Add("'");
|
|
sb.Add(" position\n(click to copy ", primary_pos, " to size)");
|
|
} else {
|
|
// Only showing 1 value, no need for single quotes,
|
|
// nor an explicit statement that indicates which value will be copied to the size input.
|
|
sb.Add(" position\n(click to copy to size)");
|
|
}
|
|
return sb.ToString();
|
|
|
|
void add_part(string part_name, string separator = null, string separator_suffix = null) {
|
|
if (part_count++ > 0) {
|
|
sb.Add(separator);
|
|
sb.Add(part_name);
|
|
sb.Add(separator_suffix);
|
|
} else {
|
|
primary_pos = part_name;
|
|
sb.Add($"{account.Value.name} {part_name}");
|
|
}
|
|
}
|
|
});
|
|
|
|
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;
|
|
this.ConnectDataContext<LadderVM>(lvm => lvm.bus.subscribe(SparkTags.SecuritiesTag).OnDispatcher().Subscribe(sec_map => Resources[ResourceKeys.Securities] = sec_map));
|
|
vm.SubscribeDisposed(_connect_left_panel_scroll());
|
|
DataContext = vm;
|
|
DataContextChanged += datacontext_changed;
|
|
_ui_scaler.MouseDoubleClick += _ui_scaler_MouseDoubleClick;
|
|
this.PreviewMouseWheel += LadderView_PreviewMouseWheel;
|
|
this.PreviewMouseDown += LadderView_PreviewMouseDown;
|
|
PreviewKeyDown += (s, e) => vm.key_down(e);
|
|
}
|
|
|
|
IDisposable _connect_left_panel_scroll() {
|
|
// extent height
|
|
bool busy = false;
|
|
var extent_height_hook = _left_panel_scroll.ObserveProperty(ScrollViewer.ExtentHeightProperty, (s, e) => {
|
|
if (!busy) {
|
|
double scroll_pos = vm.left_panel_scroll_position;
|
|
busy = true;
|
|
Dispatcher.InvokeAsync(() => {
|
|
vm.left_panel_extent_height = _left_panel_scroll.ExtentHeight;
|
|
if (_left_panel_scroll.VerticalOffset != scroll_pos)
|
|
_left_panel_scroll.TryScrollToVerticalOffset(scroll_pos);
|
|
busy = false;
|
|
}, System.Windows.Threading.DispatcherPriority.ApplicationIdle);
|
|
}
|
|
});
|
|
// view port height
|
|
_left_panel_scroll.Loaded += on_loaded;
|
|
var dispose = Disposable.CreateDisposeOnce(dispose_impl);
|
|
return Disposable.Create(dispose, vm.SubscribeDisposed(dispose));
|
|
// local methods
|
|
void on_loaded(object sender, RoutedEventArgs e) {
|
|
if (_left_panel_scroll.descendants_of_type<ScrollBar>().TryGetFirst(sb => sb.Orientation == Orientation.Vertical, out var vscroll)) {
|
|
vscroll.SetBinding(ScrollBar.ViewportSizeProperty, new Binding(nameof(LadderVM.left_panel_view_port_height)) {
|
|
Converter = ValueConverter.create<double, Size>(d => new Size(0, d), sz => sz.Height),
|
|
Mode = BindingMode.OneWayToSource
|
|
});
|
|
}
|
|
}
|
|
void dispose_impl() {
|
|
Disposable.dispose(ref extent_height_hook);
|
|
_left_panel_scroll.Loaded -= on_loaded;
|
|
}
|
|
}
|
|
|
|
// (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;
|
|
}
|
|
}
|
|
|
|
}
|
|
} |