Files
spark/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs
T

290 lines
12 KiB
C#

using bts.proto;
using mbts.spark;
using mbts.spark.filters;
using mbts.ui.settings;
using mbts.ui.shell;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace mbts.ui.controls {
// Column types are nested inside the DIYGrid
partial class DIYGrid {
public static class DefaultColumnWidths {
public const double
Account = 80,
Exchange = 86,
FillKey = 68,
OrderKey = 78,
OrderType = 88,
MViewPrice = 56,
Price = 68,
AveragePrice = 76,
LastPrice = 78,
Settlement = 82,
Security = 80,
SecurityKey = 90,
SecurityType = 96,
Side = 47,
Size = 48,
ImpliedDirectMergedSize = 68,
Filled = 56,
Symbol = 78,
Time = 112,
TimeInForce = 47,
User = 53,
Aggressor = 80,
Volume = 74,
FillKind = 70,
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 = 37,
AttachDetach = 45,
Update = 45,
EditableSize = 40,
EditablePrice = 64,
Commit = 20;
}
}
public static class ShortColumnWidths {
public const double
Time = 89,
OrderType = 77,
Side = 27,
Size = 35,
Price = 54;
}
public static class DefaultColumnHeaders {
public const string
Account = "Account",
AttachedTargetOrderKey = "Attached Target Key",
Exchange = "Exchange",
FillKey = "Fill Key",
FillTime = "Fill Time",
OrderKey = "Order Key",
OrderType = "Order Type",
Price = "Price",
Security = "Security",
SecurityKey = "Security Key",
SecurityType = "Security Type",
Side = "Side",
Size = "Qty",
MergedSize = "Qty (M)",
DirectSize = "Qty (D)",
ImpliedSize = "Qty (I)",
Symbol = "Symbol",
TimeInForce = "TIF",
User = "User",
Time = "Time",
Aggressor = "Aggressor",
Volume = "Volume",
Last = "Last",
Pos = "Pos",
Working = "Working",
Bid = "Bid",
Ask = "Ask",
Settlement = "Settlement",
DayChange = "Change",
SessionLow = "Low",
SessionHigh = "High",
FillKind = "Fill Kind",
Name = "Name",
Description = "Description",
N_Legs = "# Legs",
N_Working_Orders = "# Working Orders",
StartNetPos = "Start Net Pos",
StartPnL = "Start PnL",
DayLong = "Day Long",
DayShort = "Day Short",
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"
;
}
public interface IKeyedColumn<TColKey> {
TColKey key { get; }
}
public interface IFilterableColumn {
IDataGridColumnFilter filter { 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>, INotifyPropertyChanged {
//readonly ExternalProperty<FontWeight?> _font_weigh
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;
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);
original_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);
}
public event Action<ColumnBase, ListSortDirection?> sort_direction_changed;
public event Action font_params_changed;
public string id { get; set; }
public string sort_member_path { get; set; }
public double original_width { get; set; }
public bool is_collapsed { get; set; }
// TODO: hook up visibility
public bool is_visible { get; set; } = true;
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();
// 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);
}
public GridColumnSettings create() =>
new GridColumnSettings(id, _header_copy?.ToString(), is_visible, is_collapsed, 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;
using (ReflectionHelper.detect_property_changes(this, _raise_prop_changed, nameof(is_visible), nameof(is_collapsed), 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_collapsed = settings.is_collapsed;
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 (original_width != settings.width) {
original_width = settings.width;
Width = original_width;
}
//
}
// '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(this, new_value);
void _raise_prop_changed(string prop) =>
OnPropertyChanged(new PropertyChangedEventArgs(prop));
}
internal abstract class ColumnBase<TColKey> : ColumnBase, IKeyedColumn<TColKey>, IGridColumn {
protected ColumnBase(TColKey key) => this.key = key;
public TColKey key { get; }
}
}
}