-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMessageBox.cs
219 lines (196 loc) · 6.85 KB
/
MessageBox.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
using System;
using System.Collections.Generic;
namespace OpenTKGUI
{
/// <summary>
/// Contains function related to the creation and display of modal message boxes.
/// </summary>
public static class MessageBox
{
/// <summary>
/// Shows a messagebox with the specified options.
/// </summary>
public static void Show(LayerContainer LayerContainer, MessageBoxOptions Options)
{
MessageBoxStyle style = Options.Style;
// Message
Label message = new Label(Options.Message, style.MessageColor, style.MessageLabelStyle);
ClickHandler anyclick = null;
// Buttons
FlowContainer buttonflow = new FlowContainer(style.ButtonSeperation, Axis.Horizontal);
ButtonStyle bstyle = style.ButtonStyle;
foreach (MessageBoxOptions._Button b in Options._Buttons)
{
string name = b.Name;
Label label = new Label(name, style.ButtonTextColor, style.ButtonLabelStyle);
Button button = new Button(bstyle, label);
button.Click += b.Click;
button.Click += delegate { anyclick(); };
buttonflow.AddChild(button, button.GetFullSize(label.SuggestSize).X);
}
// Main flow container
FlowContainer mainflow = new FlowContainer(style.MessageButtonSeperation, Axis.Vertical);
mainflow.AddChild(message, message.GetHeight(style.ContentWidth));
mainflow.AddChild(buttonflow.WithCenterAlign(new Point(buttonflow.SuggestLength, style.ButtonHeight)), style.ButtonHeight);
// Margin and border
MarginContainer margin = mainflow.WithMargin(style.Margin);
Point finalsize = margin.GetSize(new Point(style.ContentWidth, mainflow.SuggestLength));
Control final = margin;
if (style.BorderSize > 0.0)
{
double bs = style.BorderSize;
final = final.WithBorder(style.BorderColor, bs, bs, bs, bs);
finalsize += new Point(bs, bs) * 2.0;
}
// Form (finally)
Form form = new Form(final, Options.Title);
form.ClientSize = finalsize;
LayerContainer.AddControl(form, LayerContainer.Size * 0.5 - form.Size * 0.5);
// Make it modal
ModalOptions mo = new ModalOptions()
{
Lightbox = true,
LowestModal = form,
MouseFallthrough = false
};
LayerContainer.Modal = mo;
// Create destruction procedure.
anyclick = delegate
{
LayerContainer.Modal = null;
form.Dismiss();
};
}
/// <summary>
/// Shows a messagebox with the OK and cancel options. Action is only performed on OK.
/// </summary>
public static void ShowOKCancel(LayerContainer Container, string Title, string Message, ClickHandler OnOKClick, MessageBoxStyle Style)
{
MessageBoxOptions mbo = new MessageBoxOptions();
mbo.AddButton("OK", OnOKClick);
mbo.AddButton("Cancel", null);
mbo.Title = Title;
mbo.Message = Message;
mbo.Style = Style;
Show(Container, mbo);
}
/// <summary>
/// Shows a messagebox with the default style with the OK and cancel options. Action is only performed on OK.
/// </summary>
public static void ShowOKCancel(LayerContainer Container, string Title, string Message, ClickHandler OnOKClick)
{
ShowOKCancel(Container, Title, Message, OnOKClick, MessageBoxStyle.Default);
}
}
/// <summary>
/// Options for showing a message box.
/// </summary>
public class MessageBoxOptions
{
public MessageBoxOptions()
{
this._Buttons = new List<_Button>();
this._Style = MessageBoxStyle.Default;
}
/// <summary>
/// Gets or sets the style of the message box.
/// </summary>
public MessageBoxStyle Style
{
get
{
return this._Style;
}
set
{
this._Style = value;
}
}
/// <summary>
/// Gets or sets the title of the message box.
/// </summary>
public string Title
{
get
{
return this._Title;
}
set
{
this._Title = value;
}
}
/// <summary>
/// Gets or sets the message of the message box.
/// </summary>
public string Message
{
get
{
return this._Message;
}
set
{
this._Message = value;
}
}
/// <summary>
/// Adds a button to the message box.
/// </summary>
public void AddButton(string Name, ClickHandler OnClick)
{
this._Buttons.Add(new _Button()
{
Name = Name,
Click = OnClick
});
}
internal class _Button
{
public string Name;
public ClickHandler Click;
}
private string _Title;
private string _Message;
private MessageBoxStyle _Style;
internal List<_Button> _Buttons;
}
/// <summary>
/// Gives styling options for a message box.
/// </summary>
public class MessageBoxStyle
{
public MessageBoxStyle()
{
}
public MessageBoxStyle(Skin Skin)
{
this.ButtonStyle = new ButtonStyle(Skin);
this.FormStyle = new FormStyle(Skin);
}
public static readonly MessageBoxStyle Default = new MessageBoxStyle(Skin.Default);
public ButtonStyle ButtonStyle;
public FormStyle FormStyle;
public LabelStyle MessageLabelStyle = new LabelStyle()
{
HorizontalAlign = TextAlign.Center,
VerticalAlign = TextAlign.Center,
Wrap = TextWrap.Wrap,
};
public LabelStyle ButtonLabelStyle = new LabelStyle()
{
HorizontalAlign = TextAlign.Center,
VerticalAlign = TextAlign.Center,
Wrap = TextWrap.Ellipsis,
};
public Color ButtonTextColor = Color.RGB(0.0, 0.0, 0.0);
public Color BorderColor = Color.RGB(0.0, 0.0, 0.0);
public double BorderSize = 1.0;
public Color MessageColor = Color.RGB(0.0, 0.0, 0.0);
public double Margin = 20.0;
public double MessageButtonSeperation = 10.0;
public double ButtonSeperation = 5.0;
public double ButtonHeight = 30.0;
public double ContentWidth = 400.0;
}
}