-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfobox.cs
More file actions
385 lines (313 loc) · 9.92 KB
/
Infobox.cs
File metadata and controls
385 lines (313 loc) · 9.92 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using System;
using System.Collections.Generic;
using System.Drawing;
//using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace nwn2_Chatter
{
/// <summary>
/// A generic outputbox for Info/Warnings/Errors.
/// </summary>
/// <remarks>The point is to stop wrapping long path-strings like the stock
/// .NET <c>MessageBox</c> does. And to stop beeps. And to make it look a
/// bit nicer.</remarks>
sealed partial class Infobox
: Form
{
#region Fields (static)
/// <summary>
/// info
/// </summary>
internal const string Title_infor = "info";
/// <summary>
/// burp
/// </summary>
internal const string Title_warni = "burp";
/// <summary>
/// aargh!
/// </summary>
internal const string Title_error = "aargh!";
/// <summary>
/// Exception
/// </summary>
internal const string Title_excep = "Exception";
/// <summary>
/// Alert
/// </summary>
/// <remarks>For save file routines and file delete requests only.</remarks>
internal const string Title_alert = "Alert";
/// <summary>
/// a miracle occurred
/// </summary>
internal const string Title_succf = "a miracle occurred";
const int w_Min = 350;
const int w_Max = 800;
const int h_Max = 475;
static string[] CRandorLF = { "\r\n", "\r", "\n" };
#endregion Fields (static)
#region cTor
/// <summary>
/// cTor.
/// </summary>
/// <param name="title">a caption on the titlebar</param>
/// <param name="head">info to be displayed with a proportional font -
/// keep this fairly brief</param>
/// <param name="copyable">info to be displayed in a fixed-width font as
/// readily copyable text - can be <c>null</c></param>
/// <param name="ibt">an <c><see cref="InfoboxType"/></c> to deter the
/// backcolor of <c><see cref="la_head"/></c></param>
/// <param name="buttons"><c><see cref="InfoboxButtons"/></c> to show to
/// the user</param>
internal Infobox(
string title,
string head,
string copyable = null,
InfoboxType ibt = InfoboxType.Info,
InfoboxButtons buttons = InfoboxButtons.Okay)
{
InitializeComponent();
if (copyable != null)
{
InitializePanel();
pa_Copyable.BringToFront();
}
else
la_head.Dock = DockStyle.Fill;
Text = title;
switch (ibt)
{
case InfoboxType.Info: la_head.BackColor = Color.Lavender; break;
case InfoboxType.Warn: la_head.BackColor = Color.Moccasin; break;
case InfoboxType.Error: la_head.BackColor = Color.SandyBrown; break;
case InfoboxType.Success: la_head.BackColor = Color.PaleGreen; break;
}
switch (buttons)
{
case InfoboxButtons.Okay:
bu_Cancel.Text = "ok";
break;
case InfoboxButtons.CancelYes:
bu_Okay.Text = "yes";
bu_Okay.Visible = true;
break;
case InfoboxButtons.AbortLoadNext:
bu_Cancel.Text = "abort";
bu_Okay .Text = "load";
bu_Retry .Text = "next";
bu_Okay .Visible =
bu_Retry.Visible = true;
break;
case InfoboxButtons.Abort:
bu_Cancel.Text = "abort";
break;
case InfoboxButtons.CancelYesNo:
bu_Cancel.Text = "cancel";
bu_Okay .Text = "yes";
bu_Retry .Text = "no";
bu_Okay .Visible =
bu_Retry.Visible = true;
break;
}
SuspendLayout();
int client_w = 0;
int client_h = 0;
int widthScroller = SystemInformation.VerticalScrollBarWidth;
string[] lines;
if (copyable != null)
{
lines = copyable.Split(CRandorLF, StringSplitOptions.None);
int test;
foreach (var line in lines)
{
test = TextRenderer.MeasureText(line, rt_Copyable.Font).Width;
if (test > client_w) client_w = test;
}
client_w += pa_Copyable.Padding.Horizontal + widthScroller;
client_h = rt_Copyable.Font.Height + 2;
pa_Copyable.Height = client_h * (lines.Length + 1)
+ pa_Copyable.Padding.Vertical;
rt_Copyable.Text = copyable + Environment.NewLine;
}
if (client_w < w_Min) client_w = w_Min;
else if (client_w > w_Max) client_w = w_Max;
int lineshead;
if (TextRenderer.MeasureText(head, la_head.Font).Width + la_head.Padding.Horizontal > client_w)
{
lines = SplitString(head, client_w - la_head.Padding.Horizontal, la_head.Font);
lineshead = lines.Length;
head = String.Empty;
for (int i = 0; i != lineshead; ++i)
{
if (i != 0) head += Environment.NewLine;
head += lines[i];
}
}
else
lineshead = 1;
la_head.Text = head;
client_h = la_head.Font.Height + 1;
la_head.Height = client_h * lineshead + la_head.Padding.Vertical + 1;
client_h = la_head.Height
+ (pa_Copyable != null ? pa_Copyable.Height : 0)
+ pa_buttons.Height;
if (client_h > h_Max) client_h = h_Max;
ClientSize = new Size(client_w + 2, client_h + 1); // pad.
MinimumSize = new Size(Width, Height);
ResumeLayout();
bu_Cancel.Select();
}
#endregion cTor
#region Handlers (override)
/// <summary>
/// Overrides the <c>Load</c> handler.
/// </summary>
/// <param name="e"></param>
/// <remarks><c>AutoWordSelection</c> needs to be done here not in the
/// designer or cTor to work right.</remarks>
protected override void OnLoad(EventArgs e)
{
if (pa_Copyable != null)
{
rt_Copyable.AutoWordSelection = false; // <- needs to be here not in the designer to work right.
// if (HoriScrollBarVisible(rt_Copyable))
// {
// int h = SystemInformation.HorizontalScrollBarHeight;
// ClientSize = new Size(ClientSize.Width, ClientSize.Height + h);
// MinimumSize = new Size(Width, Height);
// }
}
// CenterToParent();
base.OnLoad(e); // req'd for (StartPosition = FormStartPosition.CenterParent)
}
/// <summary>
/// Overrides the <c>Resize</c> handler.
/// </summary>
/// <param name="e"></param>
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
SuspendLayout();
int width = ClientSize.Width / 3 - 4; // ~2px padding on both sides of buttons
bu_Retry.Width = bu_Okay.Width = bu_Cancel.Width = width;
bu_Retry .Left = 4;
bu_Okay .Left = 7 + width;
bu_Cancel.Left = 10 + width * 2;
ResumeLayout();
}
/* /// <summary>
/// Copies the text of <c><see cref="rt_Copyable"/></c> to the Windows
/// Clipboard iff <c>rt_Copyable</c> is not focused.
/// </summary>
/// <param name="e"></param>
/// <remarks>Requires <c>KeyPreview</c> <c>true</c>.</remarks>
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyData == (Keys.Control | Keys.C)
&& pa_Copyable != null
&& !rt_Copyable.Focused)
{
e.SuppressKeyPress = true;
ClipboardService.SetText(rt_Copyable.Text);
}
else
base.OnKeyDown(e);
} */
/* /// <summary>
/// Updates the <c><see cref="ClipboardEditor"/></c> when
/// <c>[Ctrl+c]</c> is released.
/// </summary>
/// <param name="e"></param>
protected override void OnKeyUp(KeyEventArgs e)
{
if (e.KeyData == (Keys.Control | Keys.C)
&& Yata.that._fclip != null)
{
Yata.that._fclip.click_Get(null, EventArgs.Empty);
}
} */
/// <summary>
/// Overrides the <c>Paint</c> handler.
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Black, 0,0, 0, Height - 1);
}
#endregion Handlers (override)
#region Handlers
/// <summary>
/// Paints border lines left/top on the head.
/// </summary>
/// <param name="sender"><c><see cref="la_head"/></c></param>
/// <param name="e"></param>
void OnPaintHead(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Black, 0,0, 0, la_head.Height - 1);
e.Graphics.DrawLine(Pens.Black, 1,0, la_head.Width - 1, 0);
}
/// <summary>
/// Paints border lines left/top on the copyable panel.
/// </summary>
/// <param name="sender"><c><see cref="pa_Copyable"/></c></param>
/// <param name="e"></param>
void OnPaintPanel(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Black, 0,0, 0, pa_Copyable.Height - 1);
e.Graphics.DrawLine(Pens.Black, 1,0, pa_Copyable.Width - 1, 0);
// e.Graphics.DrawLine(Pens.Blue, rt_Copyable.Left, rt_Copyable.Top - 1,
// rt_Copyable.Right, rt_Copyable.Top - 1);
// e.Graphics.DrawLine(Pens.Blue, rt_Copyable.Left, rt_Copyable.Bottom,
// rt_Copyable.Right, rt_Copyable.Bottom);
//
// e.Graphics.DrawLine(Pens.Red, rt_Copyable.Left - 1, rt_Copyable.Top,
// rt_Copyable.Left - 1, rt_Copyable.Top + rt_Copyable.Font.Height);
}
/// <summary>
/// Paints border lines left/top on the buttons-panel.
/// </summary>
/// <param name="sender"><c><see cref="pa_buttons"/></c></param>
/// <param name="e"></param>
void OnPaintBot(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Black, 0,0, 0, pa_buttons.Height - 1);
if (pa_Copyable == null)
e.Graphics.DrawLine(Pens.Black, 1,0, pa_buttons.Width - 1, 0);
}
#endregion Handlers
#region Methods (static)
/// <summary>
/// Takes an input-string and splices it with newlines every
/// <paramref name="width"/> in pixels.
/// </summary>
/// <param name="text">input only a single trimmed sentence with no
/// newlines and keep words shorter than width</param>
/// <param name="width">desired width in pixels - each line of output
/// shall not exceed this length</param>
/// <param name="font"></param>
/// <returns>text split into lines no longer than width</returns>
static string[] SplitString(string text, int width, Font font)
{
string[] words = text.Split(new []{ " " }, StringSplitOptions.RemoveEmptyEntries);
var lines = new List<string>();
var sb = new StringBuilder();
foreach (var word in words)
{
if (TextRenderer.MeasureText(sb + word, font).Width > width)
{
sb.Length = sb.Length - 1; // delete " "
lines.Add(sb.ToString());
sb.Length = 0;
}
sb.Append(word + " ");
}
if (sb.Length != 0)
{
sb.Length = sb.Length - 1; // delete " "
lines.Add(sb.ToString());
}
return lines.ToArray();
}
#endregion Methods (static)
}
}