-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIComboBox.cs
145 lines (123 loc) · 5.26 KB
/
UIComboBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WinForMono {
public class UIComboBox<T> : UIElement {
public UIComboBox() {
derived_underlying = new ComboBox();
derived_underlying.SelectedIndexChanged += _underlying_selection_handler;
derived_underlying.DropDownStyle = ComboBoxStyle.DropDownList;
selection_changed += fake_event_user;
CALL_THIS_AFTER_CONSTRUCTION_PLEASE();
}
~UIComboBox() {
derived_underlying.SelectedIndexChanged -= _underlying_selection_handler;
selection_changed -= fake_event_user;
}
private void _underlying_selection_handler(object _s, EventArgs _e) {
selection_changed.Invoke();
}
public override Control underlying {
get => (Control)derived_underlying;
set => derived_underlying = (ComboBox)value;
}
private ComboBox derived_underlying;
private class UIComboBoxItem<J> {
private string user_name;
private J value;
public UIComboBoxItem(string name, J _value) { user_name = name; value = _value; }
public static implicit operator J(UIComboBoxItem<J> self) => self.value;
public override string ToString() {
return user_name;
}
}
public ComboBox.ObjectCollection underlying_items {
get => derived_underlying.Items;
//private set => derived_underlying.Items = value;
}
public T selected_val {
get {
if (derived_underlying.SelectedItem == null) return (dynamic)null;
else {
try {
return (UIComboBoxItem<T>)derived_underlying.SelectedItem;
} catch {
return new UIComboBoxItem<T>(derived_underlying.SelectedText, (T)derived_underlying.SelectedValue);
}
}
}
}
public int selected_index {
get => derived_underlying.SelectedIndex;
set => derived_underlying.SelectedIndex = value;
}
public void add_item(T item, int? pos=null) => underlying_items.Insert(pos ?? underlying_items.Count, item);
public void add_item(string name, T item, int? pos=null) => underlying_items.Insert(pos ?? underlying_items.Count, new UIComboBoxItem<T>(name, item));
public void add_items(T[] items, int? pos=null) {
pos = pos ?? underlying_items.Count;
for (int i=0; i<items.Length; i++) {
add_item(items[i], pos+i);
}
}
public void add_items(string[] names, T[] items, int? pos=null) {
if (names.Length != items.Length) throw new Exception("Cannot have a non-equal amount of items and names!");
pos = pos ?? underlying_items.Count;
for (int i=0; i<names.Length; i++) {
add_item(names[i], items[i], pos+i);
}
}
public void add_items(Dictionary<string, T> items, int? pos=null) {
pos = pos ?? underlying_items.Count;
int i=0;
foreach (string index in items.Keys) {
add_item(index, items[index], pos+i);
i++;
}
}
public void set_items(T[] items) {
underlying_items.Clear();
add_items(items);
}
public void set_items(string[] names, T[] items) {
if (names.Length != items.Length) throw new Exception("Cannot have a non-equal amount of items and names!");
underlying_items.Clear();
add_items(names, items);
}
public void set_items(Dictionary<string, T> items) {
underlying_items.Clear();
add_items(items);
}
public void remove_item(int index) => underlying_items.RemoveAt(index);
public void remove_item(T item, bool search_named=false) {
switch (search_named) {
case true:
foreach (object o in underlying_items) {
if (!(o is T || o is UIComboBoxItem<T>)) continue; // I have no idea when this could occur
if (((T)o).Equals(item)) { underlying_items.Remove(o); break; }
}
break;
case false:
underlying_items.Remove(item);
break;
}
}
public void bind_source(object source) {
derived_underlying.DataSource = source;
derived_underlying.BindingContext = new BindingContext();
}
public void refresh_contents() {
int previously_selected = derived_underlying.SelectedIndex;
object _placeholder = derived_underlying.DataSource;
bind_source(null);
bind_source(_placeholder);
derived_underlying.SelectedIndex = previously_selected;
}
protected override void on_transform_invalidated() {
base.on_transform_invalidated();
refresh_contents();
}
public delegate void OnUIComboBoxSelectionChanged();
public event OnUIComboBoxSelectionChanged selection_changed;
}
}