-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dialogs.cs
263 lines (213 loc) · 7.08 KB
/
Dialogs.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.Collections.Generic;
using System.Text;
using Terminal.Gui;
namespace Romulus
{
public class TextDialogResponse
{
public enum Buttons
{
Ok,
Cancel
}
public Buttons ButtonPressed;
public string Text;
}
public static class Dialogs
{
public static void MsgBoxOK(string title, string message)
{
var label = new Label()
{
X = 1,
Y = 1,
Width = Dim.Fill(),
Height = 4
};
label.Text = message;
var ok = new Button(24, 7, "Ok");
ok.HotKey = Key.Enter;
ok.Clicked += () => { Application.RequestStop(); };
var dialog = new Dialog(title, 60, 11, ok);
dialog.Add(label);
Application.Run(dialog);
}
//single line text input, pressing returns is same as OK
public static TextDialogResponse SingleLineInputBox(string title, string prompt, string initialValue)
{
bool okpressed = false;
var label = new Label()
{
X = 1,
Y = 1,
Width = Dim.Fill(),
Height = 2
};
label.Text = prompt;
var entry = new TextField()
{
X = 1,
Y = 4,
Width = Dim.Fill() - 1,
ColorScheme = Colors.TopLevel,
Height = 1
};
entry.Text = initialValue;
entry.KeyDown += (View.KeyEventEventArgs keyEvent) =>
{
if (keyEvent.KeyEvent.Key == Key.Enter)
{
Application.RequestStop();
okpressed = true;
}
};
var ok = new Button(18, 6, "Ok");
ok.HotKey = Key.Enter;
var cancel = new Button(25, 6, "Cancel");
cancel.HotKey = Key.Esc;
ok.Clicked += () => { Application.RequestStop(); okpressed = true; };
cancel.Clicked += () => { Application.RequestStop(); };
var dialog = new Dialog(title, 60, 10, ok, cancel);
dialog.Add(label);
dialog.Add(entry);
entry.SetFocus();
Application.Run(dialog);
if (okpressed)
{
return new TextDialogResponse()
{
ButtonPressed = TextDialogResponse.Buttons.Ok,
Text = entry.Text.ToString()
};
}
else
{
return new TextDialogResponse()
{
ButtonPressed = TextDialogResponse.Buttons.Cancel,
Text = ""
};
}
}
private static void ConditionalFocusTo(View.KeyEventEventArgs e, View target, Key handleKey)
{
if (e.KeyEvent.Key == handleKey)
{
e.Handled = true; //to ensure no further processing of the event
target.SetFocus();
}
}
//multiline text input with word wrap
public static TextDialogResponse MultilineInputBox(string title, string prompt, string initialValue)
{
bool okpressed = false;
CheckBox checkboxWrap;
Button ok;
Button cancel;
TextView entry = new TextView(); //workaround so it can be mentioned in event handler for checkbox
Label label;
label = new Label()
{
X = 1,
Y = 1,
Width = Dim.Fill(),
Height = 1,
Text = prompt
};
checkboxWrap = new CheckBox("Wrap text")
{
X = 1,
Y = 3,
TabIndex = 0
};
checkboxWrap.KeyPress += (View.KeyEventEventArgs e) =>
{
ConditionalFocusTo(e, entry, Key.Tab);
};
var wrapHint = new Label()
{
X = 14,
Y = 3,
Width = Dim.Fill(),
Text = ""
};
ok = new Button("Ok")
{
HotKey = Key.Enter,
TabIndex = 2
};
ok.KeyPress += (View.KeyEventEventArgs e) =>
{
ConditionalFocusTo(e, entry, Key.BackTab);
};
cancel = new Button("Cancel")
{
HotKey = Key.Esc,
TabIndex = 3
};
entry = new TextView()
{
X = 1,
Y = 4,
Width = Dim.Fill() - 2,
Height = Dim.Fill() - 2,
ColorScheme = Colors.TopLevel, //green text on black with a visible cursor in the editor, unlinke some other styles
Text = initialValue,
WordWrap = false, //seems to be a bug in the textformatter, we must set this only after setting the text
TabIndex = 1
};
//in theory these should allow tabbing from multiline text edit to next control on the dialog, but not working
//entry.AllowsTab = false;
//entry.AllowsReturn = true;
//instead we handle tab presses explicitly ourselves
entry.KeyPress += (View.KeyEventEventArgs e) =>
{
ConditionalFocusTo(e, ok, Key.Tab);
ConditionalFocusTo(e, checkboxWrap, Key.BackTab);
};
ok.Clicked += () => { Application.RequestStop(); okpressed = true; };
cancel.Clicked += () => { Application.RequestStop(); };
checkboxWrap.Toggled += (bool previous) =>
{
entry.WordWrap = !previous;
if (entry.WordWrap)
{
wrapHint.Text = "n.b using backspace to delete lines is quirky, use DEL instead or resize window";
}
else
{
wrapHint.Text = " ";
}
};
var dialog = new Dialog(title, ok, cancel)
{
Height = Dim.Fill() - 10,
Width = Dim.Fill() - 10,
LayoutStyle = LayoutStyle.Computed
};
dialog.Add(label);
dialog.Add(checkboxWrap);
dialog.Add(wrapHint);
dialog.Add(entry);
entry.SetFocus();
Application.Run(dialog);
if (okpressed)
{
return new TextDialogResponse()
{
ButtonPressed = TextDialogResponse.Buttons.Ok,
Text = entry.Text.ToString()
};
}
else
{
return new TextDialogResponse()
{
ButtonPressed = TextDialogResponse.Buttons.Cancel,
Text = ""
};
}
}
}
}