Repository navigation
Expand file tree
/
Copy pathMessageBox.cs
More file actions
201 lines (160 loc) · 5.3 KB
/
Copy pathMessageBox.cs
File metadata and controls
201 lines (160 loc) · 5.3 KB
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
using Source.Common.Formats.Keyvalues;
using Source.Common.GUI;
namespace Source.GUI.Controls;
public class MessageBox : Frame
{
protected Button OkButton;
protected Button CancelButton;
protected Label MessageLabel;
protected KeyValues? OkCommand;
protected KeyValues? CancelCommand;
Frame? FrameOver;
bool NoAutoClose;
bool ShowMessageBoxOverCursor;
public MessageBox(string title, string text, Panel? parent = null) : base(parent, null, false) {
SetTitle(title, true);
MessageLabel = new Label(this, null, text);
Init();
}
public MessageBox(ReadOnlySpan<char> title, ReadOnlySpan<char> text, Panel? parent = null) : base(parent, null, false) {
SetTitle(title, true);
MessageLabel = new Label(this, null, new string(text));
Init();
}
void Init() {
SetDeleteSelfOnClose(true);
FrameOver = null;
ShowMessageBoxOverCursor = false;
SetMenuButtonResponsive(false);
SetMinimizeButtonVisible(false);
SetCloseButtonVisible(false);
SetSizeable(false);
OkButton = new Button(this, null, "#MessageBox_OK");
OkButton.SetCommand("OnOk");
OkButton.AddActionSignalTarget(this);
CancelButton = new Button(this, null, "#MessageBox_Cancel");
CancelButton.SetCommand("OnCancel");
CancelButton.AddActionSignalTarget(this);
CancelButton.SetVisible(false);
OkCommand = CancelCommand = null;
NoAutoClose = false;
}
public void SetOKButtonVisible(bool state) => OkButton.SetVisible(state);
public void SetCancelButtonVisible(bool state) => CancelButton.SetVisible(state);
public void SetOKButtonText(ReadOnlySpan<char> buttonText) {
OkButton.SetText(buttonText);
InvalidateLayout();
}
public void SetCancelButtonText(ReadOnlySpan<char> buttonText) {
CancelButton.SetText(buttonText);
InvalidateLayout();
}
public void SetOKCommand(KeyValues? command) => OkCommand = command;
public void SetCancelCommand(KeyValues? command) => CancelCommand = command;
public virtual void DoModal(Frame? frameOver = null) {
ShowWindow(frameOver);
Input.SetAppModalSurface(this);
}
[UiCommand("OnOk")]
private void OnOk() {
ReleaseModalSurface();
if (OkCommand != null)
PostActionSignal(OkCommand.MakeCopy());
AutoClose();
}
[UiCommand("OnCancel")]
private void OnCancel() {
ReleaseModalSurface();
if (CancelCommand != null)
PostActionSignal(CancelCommand.MakeCopy());
AutoClose();
}
[UiCommand("Close")]
[UiCommand("CloseModal")]
private void OnCloseCommand() {
ReleaseModalSurface();
AutoClose();
}
public override void OnCommand(ReadOnlySpan<char> command) {
ReleaseModalSurface();
AutoClose();
}
private void ReleaseModalSurface() {
if (Input.GetAppModalSurface() == this)
Input.ReleaseAppModalSurface();
}
private void AutoClose() {
if (!NoAutoClose)
OnShutdownRequest();
}
private void OnShutdownRequest() {
PostMessage(this, new("Close"));
}
public override void ApplySchemeSettings(IScheme scheme) {
base.ApplySchemeSettings(scheme);
MessageLabel.GetContentSize(out int wide, out int tall);
MessageLabel.SetSize(wide, tall);
wide += 100;
tall += 100;
SetSize(wide, tall);
if (ShowMessageBoxOverCursor) {
PlaceUnderCursor();
}
if (FrameOver != null) {
FrameOver.GetPos(out int frameX, out int frameY);
FrameOver.GetSize(out int frameWide, out int frameTall);
SetPos((frameWide - wide) / 2 + frameX, (frameTall - tall) / 2 + frameY);
}
else {
Surface.GetScreenSize(out int screenWide, out int screenTall);
SetPos((screenWide - wide) / 2, (screenTall - tall) / 2);
}
}
public override void PerformLayout() {
GetClientArea(out int x, out int y, out int wide, out int tall);
wide += x;
tall += y;
GetSize(out int boxWidth, out int boxTall);
OkButton.GetSize(out int oldWide, out int oldTall);
OkButton.GetContentSize(out int btnWide, out int btnTall);
btnWide = Math.Max(oldWide, btnWide + 10);
btnTall = Math.Max(oldTall, btnTall + 10);
OkButton.SetSize(btnWide, btnTall);
int btnWide2 = 0, btnTall2 = 0;
if (CancelButton.IsVisible()) {
CancelButton.GetSize(out oldWide, out oldTall);
CancelButton.GetContentSize(out btnWide2, out btnTall2);
btnWide2 = Math.Max(oldWide, btnWide2 + 10);
btnTall2 = Math.Max(oldTall, btnTall2 + 10);
CancelButton.SetSize(btnWide2, btnTall2);
}
boxWidth = Math.Max(boxWidth, MessageLabel.GetWide() + 100);
boxWidth = Math.Max(boxWidth, (btnWide + btnWide2) * 2 + 30);
SetSize(boxWidth, boxTall);
GetSize(out boxWidth, out boxTall);
MessageLabel.SetPos((wide / 2) - (MessageLabel.GetWide() / 2) + x, y + 5);
if (!CancelButton.IsVisible())
OkButton.SetPos((wide / 2) - (OkButton.GetWide() / 2) + x, tall - OkButton.GetTall() - 15);
else {
OkButton.SetPos((wide / 4) - (OkButton.GetWide() / 2) + x, tall - OkButton.GetTall() - 15);
CancelButton.SetPos((3 * wide / 4) - (OkButton.GetWide() / 2) + x, tall - OkButton.GetTall() - 15);
}
base.PerformLayout();
}
private void PlaceUnderCursor() {
throw new NotImplementedException();
}
public void ShowWindow(Frame? frameOver) {
FrameOver = frameOver;
SetVisible(true);
SetEnabled(true);
MoveToFront();
if (OkButton.IsVisible())
OkButton.RequestFocus();
else
RequestFocus();
InvalidateLayout();
}
public void SetCommand(ReadOnlySpan<char> command) => OkCommand = new("Command", "command", command);
public void SetCommand(KeyValues command) => OkCommand = command;
}