535 lines
20 KiB
C#
535 lines
20 KiB
C#
namespace mbts.ui.controls {
|
|
using bts;
|
|
using bts.utils;
|
|
|
|
using mbts.spark;
|
|
using mbts.spark.filters;
|
|
using mbts.ui.dpi;
|
|
using mbts.ui.settings;
|
|
|
|
using Spark.Core.Disposables;
|
|
using Spark.Core.Reflection;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.ComponentModel;
|
|
using System.Reflection;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
|
|
// Column types are nested inside the DIYGrid
|
|
partial class DIYGrid {
|
|
public static class DefaultColumnWidths {
|
|
public const double
|
|
Account = 62,
|
|
CheckMark = 42,
|
|
Currency = 68,
|
|
Exchange = 66,
|
|
FillKey = 68,
|
|
OrderKey = 78,
|
|
OrderType = 88,
|
|
MViewPrice = 56,
|
|
Price = 68,
|
|
AveragePrice = 76,
|
|
LastPrice = 78,
|
|
Settlement = 82,
|
|
Greeks = 56,
|
|
Security = 134,
|
|
SecurityKey = 90,
|
|
SecurityType = 96,
|
|
Side = 47,
|
|
Qty = 48,
|
|
MergedQty = 68,
|
|
NotionalValue = 68,
|
|
Filled = 56,
|
|
FilledNotionalValue = 81,
|
|
Symbol = 60,
|
|
Time = 112,
|
|
TimeOfDay = 84,
|
|
TimeInForce = 47,
|
|
User = 53,
|
|
Aggressor = 80,
|
|
Volume = 74,
|
|
FillKind = 70,
|
|
FillStatus = 76,
|
|
Description = 300,
|
|
N_Legs = 68,
|
|
N_Working_Orders = 123,
|
|
Start_Net_Pos = 100,
|
|
Total_Net_Pos = 100,
|
|
PnL_Generic = 80,
|
|
Working = 72,
|
|
LastMessage = 100,
|
|
ParentKey = 100,
|
|
AttachedTargetOrderKey = 100;
|
|
|
|
public static class MiniOrderBook {
|
|
public const double
|
|
Cancel = 41,
|
|
AttachDetach = 45,
|
|
Update = 45,
|
|
EditableSize = 40,
|
|
EditablePrice = 64,
|
|
Commit = 20;
|
|
}
|
|
|
|
public static class PNL {
|
|
public const double
|
|
// 'PNL Account' is a bit wider than normal Account cols, bc it is a 'grouping' column, so it has additional content/icons
|
|
Account = 100,
|
|
Exchange = 85,
|
|
Expiration = 75,
|
|
Symbol = 72,
|
|
Security = DefaultColumnWidths.Security,
|
|
Qty = 42,
|
|
// deafult RawDayQty headers are too big for [normal]Qty width
|
|
RawDayQty = 51,
|
|
Price = 64,
|
|
PriceBig = 92,
|
|
SecurityType = 64,
|
|
WorkingQty = 54,
|
|
HasDayTrades = 74;
|
|
}
|
|
}
|
|
|
|
public static class ShortColumnWidths {
|
|
public const double
|
|
Time = 89,
|
|
OrderType = 77,
|
|
Side = 27,
|
|
Qty = 35,
|
|
Price = 54;
|
|
}
|
|
|
|
public static class DefaultColumnHeaders {
|
|
public const string
|
|
Account = "Account",
|
|
AvgCost = "Avg Cost",
|
|
AvgFilled = "Avg Filled",
|
|
AttachedTargetOrderKey = "Attached Target Key",
|
|
Currency = "Currency",
|
|
Exchange = "Exchange",
|
|
Expiration = "Exp",
|
|
FillKey = "Fill Key",
|
|
FillTime = "Fill Time",
|
|
LastChange = "Last Change",
|
|
OrderKey = "Order Key",
|
|
OrderType = "Order Type",
|
|
Price = "Price",
|
|
Security = "Security",
|
|
SecurityKey = "Security Key",
|
|
SecurityType = "Security Type",
|
|
Side = "B/S",
|
|
Qty = "Qty",
|
|
NotionalValue = "Notional",
|
|
MergedQty = "Qty",
|
|
DirectQty = "Qty (D)",
|
|
ImpliedQty = "Qty (I)",
|
|
Symbol = "Symbol",
|
|
TimeInForce = "TIF",
|
|
User = "User",
|
|
Time = "Time",
|
|
Aggressor = "Aggressor",
|
|
Volume = "Volume",
|
|
Last = "Last",
|
|
Pos = "Pos",
|
|
Working = "Working",
|
|
Bid = "Bid",
|
|
Ask = "Ask",
|
|
Mid = "Mid",
|
|
Settlement = "Settlement",
|
|
DayChange = "Change",
|
|
SessionLow = "Low",
|
|
SessionHigh = "High",
|
|
FillKind = "Fill Kind",
|
|
FilledPrice = "Fill Price",
|
|
FilledQuantity = "Fill Qty",
|
|
FilledNotionalValue = "Fill Notional",
|
|
FillStatus = "Fill Status",
|
|
Name = "Name",
|
|
Description = "Description",
|
|
N_Legs = "# Legs",
|
|
N_Working_Orders = "# Working Orders",
|
|
StartNetPos = "Open Pos",
|
|
StartPnL = "Open PnL",
|
|
DayLong = "Day Long",
|
|
DayShort = "Day Short",
|
|
RawDayPos = "Raw Day Pos",
|
|
DayPnL = "Day PnL",
|
|
TotalNetPos = "Total Net Pos",
|
|
TotalPnL = "Total PnL",
|
|
PnLPrice = "PnL Price",
|
|
Cancel = "Cancel",
|
|
AttachDetach = "A/D",
|
|
Update = "Update",
|
|
MarketDescription = "Market Description",
|
|
ClearedAlias = "Cleared Alias",
|
|
ProductDescription = "Product Description",
|
|
HubAlias = "Hub Alias",
|
|
StripName = "Strip Name",
|
|
MicCode = "Mic Code",
|
|
PositionInQueue = "PIQ",
|
|
RootAlgoType = "Root Algo",
|
|
Volatility = "Vol",
|
|
Driver = "Driver",
|
|
Theo = "Theo",
|
|
BuyEdge = "B Edge",
|
|
SellEdge = "S Edge",
|
|
Delta = "Delta"
|
|
;
|
|
|
|
public static class PNLHeaders {
|
|
public const string
|
|
PNL = "PnL",
|
|
Long = "Long",
|
|
Short = "Short",
|
|
Net = "Pos",
|
|
AvgPrice = "Avg",
|
|
HasDayTrades = "Traded Today",
|
|
SecurityType = "Type",
|
|
Delta = "Delta",
|
|
Gamma = "Gamma",
|
|
Vega = "Vega",
|
|
Theta = "Theta";
|
|
|
|
public static readonly Part
|
|
SOD = new Part("Open"),
|
|
Day = new Part("Day"),
|
|
RawDay = new Part("Raw Day"),
|
|
Total = new Part("Total"),
|
|
Working = new Part("Working");
|
|
|
|
public readonly struct Part {
|
|
public readonly string prefix;
|
|
|
|
public Part(string prefix) =>
|
|
this.prefix = prefix;
|
|
|
|
public string PNL => $"{prefix} PNL";
|
|
public string Net => $"{prefix} {PNLHeaders.Net}";
|
|
public string Long => $"{prefix} {PNLHeaders.Long}";
|
|
public string LongAvg => $"{Long} {AvgPrice}";
|
|
public string Short => $"{prefix} {PNLHeaders.Short}";
|
|
public string ShortAvg => $"{Short} {AvgPrice}";
|
|
public string Delta => $"{prefix} {PNLHeaders.Delta}";
|
|
public string Gamma => $"{prefix} {PNLHeaders.Gamma}";
|
|
public string Vega => $"{prefix} {PNLHeaders.Vega}";
|
|
public string Theta => $"{prefix} {PNLHeaders.Theta}";
|
|
}
|
|
}
|
|
}
|
|
|
|
public interface IKeyedColumn<TColKey> {
|
|
TColKey key { get; }
|
|
}
|
|
|
|
public interface IFilterableColumn {
|
|
IDataGridColumnFilter filter_model { get; }
|
|
string filter_template { get; }
|
|
}
|
|
|
|
public interface IGridColumn {
|
|
string id { get; }
|
|
}
|
|
|
|
public interface IGridColumn<TRow> : IGridColumn where TRow : DIYGrid.IRowVM {
|
|
IGridCellStore get_cell_store(TRow row);
|
|
}
|
|
|
|
public interface IGridCellStore {
|
|
IGridColumn column { get; }
|
|
IGridCell current { get; }
|
|
ObservableCollection<CellAnimation> animations { get; }
|
|
bool refresh();
|
|
void regenerate_cell();
|
|
}
|
|
|
|
public interface IGridCell {
|
|
string text { get; }
|
|
}
|
|
|
|
public interface IGridCell<T> : IGridCell {
|
|
T value { get; }
|
|
}
|
|
|
|
public interface ISelectionHighlightListener {
|
|
void on_selection_fill(SolidColorBrush selection_fill);
|
|
}
|
|
|
|
// To include extra data with your column type you are encouraged to derive from DIYGridColumn
|
|
public abstract class ColumnBase : GridViewColumn, IValueFactory<GridColumnSettings>, IDIYGridColumn, ISortableDIYGridColumn, INotifyPropertyChanged {
|
|
//readonly ExternalProperty<FontWeight?> _font_weigh
|
|
GridColumnSettings _settings;
|
|
FontWeight _fallback_font_weight;
|
|
Color _fallback_font_color;
|
|
Color _fallback_background_color;
|
|
double _fallback_font_size;
|
|
FontFamily _fallback_font_family;
|
|
FontWeight? _font_weight_override;
|
|
Color? _font_color_override;
|
|
Color? _background_color_override;
|
|
object _header_copy;
|
|
bool _is_visible;
|
|
bool _is_expanded = true;
|
|
IDisposable _model_hooks;
|
|
|
|
static ColumnBase() {
|
|
WidthProperty.OverrideMetadata(typeof(ColumnBase),
|
|
new PropertyMetadata((d, e) => {
|
|
if (d is not ColumnBase cb || e.NewValue is not double newval) {
|
|
return;
|
|
}
|
|
if (!cb._is_expanded && newval > _collapsed_width + 2) {
|
|
cb.expanded_width = newval;
|
|
cb.is_expanded = true;
|
|
}
|
|
cb.width_changed?.Invoke((double)e.NewValue);
|
|
}));
|
|
}
|
|
|
|
protected ColumnBase() {
|
|
var gs = GridSettings.Default;
|
|
_fallback_font_weight = gs.font_weight;
|
|
_fallback_font_color = gs.font_color.rgb_color();
|
|
_fallback_background_color = gs.background_color.argb_color();
|
|
_fallback_font_size = gs.font_size;
|
|
_fallback_font_family = new FontFamily(gs.font_family);
|
|
expanded_width = double.NaN;
|
|
}
|
|
|
|
public static readonly DependencyProperty SortDirectionProperty =
|
|
WPFHelper.create_dp<ListSortDirection?, ColumnBase>(
|
|
nameof(SortDirection), (o, v) => o.OnSortDirectionChanged(v));
|
|
|
|
public ListSortDirection? SortDirection {
|
|
get => GetValue(SortDirectionProperty) as ListSortDirection?;
|
|
set => SetValue(SortDirectionProperty, value);
|
|
}
|
|
ListSortDirection? ISortableDIYGridColumn.SortDirection {
|
|
get => SortDirection;
|
|
set => SetCurrentValue(SortDirectionProperty, value);
|
|
}
|
|
|
|
#region IsVisible
|
|
public static readonly DependencyProperty IsVisibleProperty =
|
|
WPFHelper.create_dp<bool, ColumnBase>(nameof(IsVisible), (o, v) => o.is_visible = v);
|
|
|
|
public bool IsVisible {
|
|
get => (bool)GetValue(IsVisibleProperty);
|
|
set => SetValue(IsVisibleProperty, value);
|
|
}
|
|
#endregion
|
|
|
|
public event Action<ListSortDirection?> sort_direction_changed;
|
|
public event Action font_params_changed;
|
|
public event Action<bool> is_visible_changed;
|
|
public event Action<double> width_changed;
|
|
public event Action<bool> is_expanded_changed;
|
|
|
|
public string id { get; set; }
|
|
public string sort_member_path { get; set; }
|
|
public PropertyPath data_member_path { get; set; }
|
|
public double expanded_width { get; set; }
|
|
|
|
public bool is_expanded {
|
|
get => _is_expanded;
|
|
set {
|
|
if (_is_expanded == value) {
|
|
return;
|
|
}
|
|
if (value) {
|
|
expand();
|
|
} else {
|
|
collapse();
|
|
}
|
|
is_expanded_changed?.Invoke(value);
|
|
raise_prop_changed();
|
|
}
|
|
}
|
|
|
|
public bool is_visible {
|
|
get => _is_visible;
|
|
set {
|
|
if (_is_visible != value) {
|
|
_is_visible = value;
|
|
is_visible_changed?.Invoke(value);
|
|
raise_prop_changed();
|
|
SetCurrentValue(IsVisibleProperty, value);
|
|
}
|
|
}
|
|
}
|
|
public FontWeight? font_weight_override { get => _font_weight_override; set => _set_font_param(ref _font_weight_override, value); }
|
|
public Color? font_color_override { get => _font_color_override; set => _set_font_param(ref _font_color_override, value); }
|
|
public HorizontalAlignment horizontal_alignment { get; set; }
|
|
public double horizontal_offset { get; set; }
|
|
public FontFamily font_family => _fallback_font_family;
|
|
public double font_size => _fallback_font_size; //TODO: font_size override
|
|
public FontWeight font_weight => font_weight_override ?? _fallback_font_weight;
|
|
public Color font_color => font_color_override ?? _fallback_font_color;
|
|
public Brush font_brush => new SolidColorBrush(font_color);
|
|
public Color? background_color_override { get => _background_color_override; set => _set_font_param(ref _background_color_override, value); }
|
|
public Color? background_color => background_color_override ?? _fallback_background_color;
|
|
public Brush background_brush => background_color?.to_brush();
|
|
public GridColumnSettings settings => _settings ?? (_settings = create());
|
|
// TODO: find a better way to store the header-property in a thread-safe manner - a value for GridSettings.display_name, more specifically.
|
|
// Since save_state is called from a different thread in IShared/SlowWindows, it is not safe to access the DependencyProperty store from that method.
|
|
// By updating a local _header_copy field in the Header setter, the DependencyProperty lookup may be bypassed when appropriate (see create method below).
|
|
public new object Header {
|
|
get => base.Header;
|
|
set => base.Header = (_header_copy = value);
|
|
}
|
|
|
|
static double _collapsed_width => 3.0.RoundLayoutValue();
|
|
|
|
public bool expand() {
|
|
if (!expanded_width.is_real()) {
|
|
return false;
|
|
}
|
|
_is_expanded = true;
|
|
SetCurrentValue(WidthProperty, expanded_width);
|
|
return true;
|
|
}
|
|
|
|
public bool collapse() {
|
|
if (!ActualWidth.is_real()) {
|
|
return false;
|
|
}
|
|
expanded_width = ActualWidth;
|
|
_is_expanded = false;
|
|
SetCurrentValue(WidthProperty, _collapsed_width);
|
|
return true;
|
|
}
|
|
|
|
public void set_model(IDIYGridColumnModel model) {
|
|
Disposable.dispose(ref _model_hooks);
|
|
model.width = Width;
|
|
width_changed += on_width_changed;
|
|
model.width_changed += on_model_width_changed;
|
|
model.settings_changed += on_result_changed;
|
|
_model_hooks = Disposable.Create(() => {
|
|
width_changed -= on_width_changed;
|
|
model.width_changed -= on_model_width_changed;
|
|
});
|
|
void on_model_width_changed(double value) {
|
|
if (!value.epsilon_equals(Width, HighDpiExtensions.EPSILON)) {
|
|
SetCurrentValue(WidthProperty, value);
|
|
}
|
|
}
|
|
void on_width_changed(double value) {
|
|
model.width_changed -= on_model_width_changed;
|
|
model.width = value;
|
|
model.width_changed += on_model_width_changed;
|
|
}
|
|
void on_result_changed() =>
|
|
load_from(model.create());
|
|
}
|
|
|
|
/// important: don't access any dependency props in here.
|
|
/// this may be called from a thread other than that of this dispatcher (i.e., when <see cref="IShellWindow.GetState(System.IO.Stream)"/> is called).
|
|
public GridColumnSettings create() =>
|
|
new GridColumnSettings(id, _header_copy?.ToString(), is_visible, is_expanded, font_weight_override, font_color_override, ActualWidth, horizontal_alignment, horizontal_offset, background_color_override);
|
|
|
|
void _set_font_param<T>(ref T field, T value) {
|
|
field = value;
|
|
font_params_changed?.Invoke();
|
|
}
|
|
|
|
public virtual void load_from(GridColumnSettings settings) {
|
|
if (settings == null)
|
|
return;
|
|
_settings = settings;
|
|
using (ReflectionHelper.detect_property_changes(this, raise_prop_changed, nameof(is_visible), nameof(is_expanded), nameof(font_weight_override), nameof(font_weight),
|
|
nameof(font_color_override), nameof(font_color), nameof(font_brush), nameof(font_family), nameof(font_size), nameof(horizontal_alignment), nameof(horizontal_offset), nameof(background_color_override), nameof(background_color), nameof(background_brush))) {
|
|
//
|
|
id = settings.id;
|
|
if (!string.IsNullOrEmpty(settings.display_name))
|
|
Header = settings.display_name;
|
|
is_visible = settings.is_visible;
|
|
is_expanded = settings.is_expanded;
|
|
font_weight_override = settings.font_weight_override;
|
|
font_color_override = settings.font_color_override;
|
|
horizontal_alignment = settings.horizontal_alignment;
|
|
horizontal_offset = settings.horizontal_offset;
|
|
background_color_override = settings.background_color_override;
|
|
}
|
|
if (expanded_width != settings.width) {
|
|
expanded_width = settings.width;
|
|
if (is_expanded) {
|
|
SetCurrentValue(WidthProperty, expanded_width);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void load_from(GridSettings settings) {
|
|
if (settings is null) {
|
|
return;
|
|
}
|
|
set_fallback_font_params(
|
|
settings.font_family is null ? null : new FontFamily(settings.font_family),
|
|
settings.font_size,
|
|
settings.font_color.argb_color(),
|
|
settings.font_weight,
|
|
settings.background);
|
|
}
|
|
|
|
// 'fallback' font-params (aka 'grid-level' font-params)
|
|
public void set_fallback_font_params(FontFamily font_family, double font_size, Color font_color, FontWeight font_weight, Color background_color) {
|
|
using (ReflectionHelper.detect_property_changes(this, raise_prop_changed, nameof(font_family), nameof(font_size), nameof(font_color), nameof(font_weight), nameof(background_color), nameof(background_brush))) {
|
|
_fallback_font_family = font_family;
|
|
_fallback_font_size = font_size;
|
|
_fallback_font_color = font_color;
|
|
_fallback_background_color = background_color;
|
|
_fallback_font_weight = font_weight;
|
|
}
|
|
font_params_changed?.Invoke();
|
|
}
|
|
|
|
protected virtual void OnSortDirectionChanged(ListSortDirection? new_value) =>
|
|
sort_direction_changed?.Invoke(new_value);
|
|
|
|
protected void raise_prop_changed([CallerMemberName] string prop = "") =>
|
|
OnPropertyChanged(new PropertyChangedEventArgs(prop));
|
|
|
|
protected bool set<T>(ref T backing, T new_value, [CallerMemberName] string prop = "") =>
|
|
set(ref backing, new_value, EqualityComparer<T>.Default, prop);
|
|
|
|
protected bool set<T>(ref T backing, T new_value, IEqualityComparer<T> comparer, [CallerMemberName] string prop = "") {
|
|
if (!comparer.Equals(backing, new_value)) {
|
|
backing = new_value;
|
|
OnPropertyChanged(new PropertyChangedEventArgs(prop));
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
public override string ToString() =>
|
|
id.ToString();
|
|
}
|
|
|
|
internal abstract class ColumnBase<TColKey> : ColumnBase, IKeyedColumn<TColKey>, IDIYGridColumn<TColKey>, ISortableDIYGridColumn<TColKey>, IGridColumn {
|
|
TColKey _key;
|
|
bool _key_initialized;
|
|
// need a default constructor to support declaration in xaml
|
|
protected ColumnBase() { }
|
|
protected ColumnBase(TColKey key) => this.key = key;
|
|
|
|
public TColKey key {
|
|
get => _key;
|
|
set {
|
|
if (Equals(_key, value))
|
|
return;
|
|
Log.assert(!_key_initialized, "Column key already initialized.");
|
|
_key_initialized = true;
|
|
_key = value;
|
|
on_key_initialized(value);
|
|
}
|
|
}
|
|
TColKey IDIYGridColumn<TColKey>.id => key;
|
|
|
|
protected virtual void on_key_initialized(TColKey key) { }
|
|
}
|
|
}
|
|
}
|