Skip to content

Commit 9ab1bb4

Browse files
committed
Added multi item sortable boxes (for codecs primarily)
1 parent 423bd5c commit 9ab1bb4

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

Field.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static FieldOption GetByValue(FieldOption[] options, String value) {
2323
return (from o in options where o.value == value select o).SingleOrDefault();
2424
}
2525
}
26-
public enum FIELD_TYPE { String, Int, Bool, Combo, MultiItem, Password };
26+
public enum FIELD_TYPE { String, Int, Bool, Combo, MultiItem, Password, MultiItemSort };
2727
public FIELD_TYPE type;
2828
public delegate string validate_field_del(String value);
2929
public validate_field_del Validator;

GenericEditor.xaml.cs

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Windows;
44
using System.Windows.Automation;
55
using System.Windows.Controls;
6+
using System.Windows.Input;
67

78
namespace FSClient {
89
/// <summary>
@@ -54,6 +55,53 @@ public void Init(String title, IEnumerable<FieldValue> values) {
5455

5556
}
5657
}
58+
private void CreateSortableContextMenu(ListBox box){
59+
ContextMenu menu = new ContextMenu();
60+
var item = new MenuItem() { Header = "Move Up" };
61+
item.Click += (s,e) => SortableContextMenuMove(box,-1,s,e);
62+
menu.Items.Add(item);
63+
item = new MenuItem() { Header = "Move Down" };
64+
item.Click += (s, e) => SortableContextMenuMove(box, 1, s, e);
65+
menu.Items.Add(item);
66+
box.ContextMenu = menu;
67+
box.ContextMenuOpening += (s,e) => SortableMenuOpening(box,menu,s,e);
68+
box.PreviewMouseDown += box_MouseDown;
69+
70+
}
71+
72+
void box_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
73+
if (e.RightButton == MouseButtonState.Pressed)
74+
e.Handled = true;
75+
}
76+
77+
78+
79+
private void SortableMenuOpening(ListBox box,ContextMenu menu,object sender, ContextMenuEventArgs e){
80+
menu.DataContext = ((FrameworkElement)e.OriginalSource).DataContext;
81+
object elem = menu.DataContext;
82+
}
83+
84+
85+
private void SortableContextMenuMove(ListBox box, int move_by, object sender, RoutedEventArgs e){
86+
MenuItem item = (sender as MenuItem);
87+
if (item == null || item.DataContext == null)
88+
return;
89+
object to_move = item.DataContext;
90+
for (int pos = 0; pos < box.Items.Count; pos++){
91+
if (box.Items[pos] == to_move){
92+
if (pos + move_by < 0 || pos + move_by >= box.Items.Count)
93+
return;
94+
bool selected = box.SelectedItems.Contains(to_move);
95+
box.Items.Remove(to_move);
96+
box.Items.Insert(pos + move_by, to_move);
97+
if (selected)
98+
box.SelectedItems.Add(to_move);
99+
return;
100+
}
101+
}
102+
103+
}
104+
57105
private UIElement CreateElementValuer(FieldValue value) {
58106
Control ret = null;
59107
switch (value.field.type) {
@@ -64,19 +112,27 @@ private UIElement CreateElementValuer(FieldValue value) {
64112
ret = ibox;
65113
break;
66114
case Field.FIELD_TYPE.MultiItem:
115+
case Field.FIELD_TYPE.MultiItemSort:
67116
ListBox listBox = new ListBox();
68117
listBox.SelectionMode = SelectionMode.Multiple;
118+
if (value.field.type == Field.FIELD_TYPE.MultiItemSort)
119+
CreateSortableContextMenu(listBox);
69120
listBox.Height = 100;
70121
listBox.Width = 190;
71-
foreach (Field.FieldOption option in value.field.options) {
72-
if (value.field.Validator == null || String.IsNullOrEmpty(value.field.Validator(option.value)))
73-
listBox.Items.Add(option);
74-
}
75122
String[] vals = value.value.Split(',');
76123
foreach (String val in vals) {
77124
Field.FieldOption opt = Field.FieldOption.GetByValue(value.field.options, val);
78-
if (opt != null)
125+
if (opt != null){
126+
listBox.Items.Add(opt);
79127
listBox.SelectedItems.Add(opt);
128+
}
129+
}
130+
foreach (Field.FieldOption option in value.field.options) {
131+
if (value.field.Validator != null && !String.IsNullOrEmpty(value.field.Validator(option.value)))
132+
continue;
133+
if (listBox.Items.Contains(option))
134+
continue;
135+
listBox.Items.Add(option);
80136
}
81137
ret = listBox;
82138
break;
@@ -153,9 +209,12 @@ private String GetValueFromUI(FieldValue value, UIElement elem) {
153209
String val = null;
154210
switch (value.field.type) {
155211
case Field.FIELD_TYPE.MultiItem:
212+
case Field.FIELD_TYPE.MultiItemSort:
156213
ListBox listBox = (elem as ListBox);
157214
val = "";
158-
foreach (Object obj in listBox.SelectedItems) {
215+
foreach (Object obj in listBox.Items) {
216+
if (listBox.SelectedItems.Contains(obj) == false)
217+
continue;
159218
Field.FieldOption opt2 = obj as Field.FieldOption;
160219
if (opt2 != null) {
161220
if (!String.IsNullOrEmpty(val))

Sofia.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class Sofia {
2929
public static Field[] fields = {
3030

3131
/*Default*/
32-
new Field(Field.FIELD_TYPE.MultiItem,"Codec Preferences","codec-prefs","codec-prefs","CELT@48000h@10i,PCMU,PCMA,GSM","","CELT@32000h","CELT@48000h@10i","PCMA","PCMU","GSM","G722","G7221@16000h","G7221@32000h","AAL2-G726-16","AAL2-G726-24","AAL2-G726-32","AAL2-G726-40","BV16","BV32","DVI4@16000h@40i","DVI4@8000h@20i","G726-16","G726-24","G726-32","G726-40","L16","LPC","iLBC@30i","speex@16000h@20i","speex@32000h@20i","speex@8000h@20i","G729","isac"),
32+
new Field(Field.FIELD_TYPE.MultiItemSort,"Codec Preferences","codec-prefs","codec-prefs","CELT@48000h@10i,PCMU,PCMA,GSM","","CELT@32000h","CELT@48000h@10i","PCMA","PCMU","GSM","G722","G7221@16000h","G7221@32000h","AAL2-G726-16","AAL2-G726-24","AAL2-G726-32","AAL2-G726-40","BV16","BV32","DVI4@16000h@40i","DVI4@8000h@20i","G726-16","G726-24","G726-32","G726-40","L16","LPC","iLBC@30i","speex@16000h@20i","speex@32000h@20i","speex@8000h@20i","G729","isac"),
3333
new Field(Field.FIELD_TYPE.Combo,"Inbound Codec Negotiation","inbound-codec-negotiation","inbound-codec-negotiation","generous","","generous","greedy","scrooge"),
3434
new Field(Field.FIELD_TYPE.String,"External RTP IP","ext-rtp-ip","ext-rtp-ip","auto-nat",""),
3535
new Field(Field.FIELD_TYPE.String,"External SIP IP","ext-sip-ip","ext-sip-ip","auto-nat",""),

0 commit comments

Comments
 (0)