/
InputBox.cs
413 lines (378 loc) · 15.2 KB
/
InputBox.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* Copyright (C) 2013 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
using System;
using System.Drawing;
using System.Windows.Forms;
namespace InputText
{
public partial class InputBox : Form
{
private SkinWindow parent = null;
private bool _FocusDismiss = true;
private bool _Numeric = false;
private string _TextValue = string.Empty;
public InputBox(SkinWindow parent)
{
this.parent = parent;
InitializeComponent();
}
// This event will make sure we have focus when the inputbox is shown ot the user.
// it will only cause focus if the window itself has focus (i.e. doesn't steal).
private void InputBox_Shown(object sender, EventArgs e)
{
this.Activate();
this.BringToFront();
this.Activate();
this.BringToFront();
this.txtInput.Focus();
}
private void InputBox_Load(object sender, EventArgs e)
{
this.Activate();
this.BringToFront();
this.Activate();
this.BringToFront();
}
private void txtInput_KeyPressed(object sender, KeyPressEventArgs e)
{
if (_Numeric)
{
if (!char.IsControl(e.KeyChar) && (!char.IsDigit(e.KeyChar))
&& (e.KeyChar != '.') && (e.KeyChar != '-'))
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
// only allow minus sign at the beginning
if (e.KeyChar == '-' && (sender as TextBox).Text.Length > 0)
{
e.Handled = true;
}
}
}
// All exceptions are swallowed for the sake of this example. Since the majority
// (if not all) of these are rather simple in nature, debugging without errors
// should be fairly simple anyway.
//
// However, if you wanted to log errors, a simple way would be to add something like
// this:
//
// try
// {
// ... // code you are testing
// }
// catch (Exception ex)
// {
// API.Log(API.LogType.Warning, "InputText :: exception :: " + ex.GetType().ToString() + ": " + ex.Message);
// }
#region TextValue -- returns the inputted text
public string TextValue
{
get { return this._TextValue; }
}
#endregion
#region ChangeFontFace(string) -- changes the text's font name
public void ChangeFontFace(string sFont)
{
try
{
this.txtInput.Font = new Font(sFont, this.txtInput.Font.Size);
}
catch { }
}
#endregion
#region ChangeFontColor(string) -- changes the font's color, supports 6 and 8-digit hex or a series of 3 or 4 comma-separated numbers
public void ChangeFontColor(string sColor)
{
try
{
sColor = sColor.Trim();
if (sColor.Contains(",")) // #,#,# or #,#,#,# format
{
string[] sParts = sColor.Trim().Replace(" ", string.Empty).Split(new char[] { ',' }, StringSplitOptions.None);
this.txtInput.ForeColor = Color.FromArgb(int.Parse(sParts[0]), int.Parse(sParts[1]), int.Parse(sParts[2]));
this.txtInput.ForeColor = Color.FromArgb(int.Parse(sParts[3]), int.Parse(sParts[0]), int.Parse(sParts[1]), int.Parse(sParts[2]));
}
else
{
if (sColor.Length == 6) // 6-digit hex (no alpha transparency)
{
uint iColor = uint.Parse(sColor, System.Globalization.NumberStyles.HexNumber);
// Since it was omitted, force full opacity (not transparent at all)
this.txtInput.ForeColor = Color.FromArgb((int)(0xFF000000 | iColor));
}
else if (sColor.Length == 8) // 8-digit hex (with alpha transparency)
{
uint iColor = uint.Parse(sColor, System.Globalization.NumberStyles.HexNumber);
// This swaps RRGGBBAA for AARRGGBB, which Color.FromArgb() wants
iColor = ((iColor & 0xFFFFFF00) >> 8) | ((iColor & 0x000000FF) << 24);
this.txtInput.ForeColor = Color.FromArgb((int)iColor);
}
}
}
catch { }
}
#endregion
#region ChangeBackColor(string) -- changes the background color, supports 6 and 8-digit hex or a series of 3 or 4 comma-separated numbers
public void ChangeBackColor(string sColor)
{
try
{
sColor = sColor.Trim();
if (sColor.Contains(",")) // #,#,# or #,#,#,# format
{
string[] sParts = sColor.Trim().Replace(" ", string.Empty).Split(new char[] { ',' }, StringSplitOptions.None);
this.txtInput.BackColor = Color.FromArgb(int.Parse(sParts[0]), int.Parse(sParts[1]), int.Parse(sParts[2]));
// Change 0-255 transparency to a 0.0-1.0 transparency range
double dTrans = (double)int.Parse(sParts[3]);
dTrans /= 255.0;
this.Opacity = dTrans;
}
else
{
if (sColor.Length == 6) // 6-digit hex (no alpha transparency)
{
uint iColor = uint.Parse(sColor, System.Globalization.NumberStyles.HexNumber);
this.txtInput.BackColor = Color.FromArgb((int)(0xFF000000 | iColor));
// Since it was omitted, force full opacity (not transparent at all)
this.Opacity = 1.0;
}
else if (sColor.Length == 8) // 8-digit hex (with alpha transparency)
{
uint iColor = uint.Parse(sColor, System.Globalization.NumberStyles.HexNumber);
// This swaps RRGGBBAA for AARRGGBB, which Color.FromArgb() wants
iColor = ((iColor & 0xFFFFFF00) >> 8) | ((iColor & 0x000000FF) << 24);
this.txtInput.BackColor = Color.FromArgb((int)((0xFF000000) | (0x00FFFFFF & iColor)));
// Change 0-255 transparency to a 0.0-1.0 transparency range
double dTrans = (double)((iColor & 0xFF000000) >> 24);
dTrans /= 255.0;
this.Opacity = dTrans;
}
}
}
catch { }
}
#endregion
#region ChangeStringAlign(string) -- Changes text horizontal alignment
public void ChangeStringAlign(string sAlign)
{
try
{
sAlign = sAlign.ToUpper().Trim();
if (sAlign == "LEFT")
this.txtInput.TextAlign = HorizontalAlignment.Left;
else if (sAlign == "CENTER")
this.txtInput.TextAlign = HorizontalAlignment.Center;
else if (sAlign == "RIGHT")
this.txtInput.TextAlign = HorizontalAlignment.Right;
}
catch { }
}
#endregion
#region ChangeFontSize(string) -- changes the font's point size (supports floating-point values)
public void ChangeFontSize(string sSize)
{
try
{
this.txtInput.Font = new Font(this.txtInput.Font.Name, float.Parse(sSize));
}
catch { }
}
#endregion
#region MakeTopmost() -- forces the form to have the 'Always On Top' property
public void MakeTopmost()
{
this.TopMost = true;
}
#endregion
#region MakePassword(bool) -- causes the textbox to be password style
public void MakePassword(bool bPass)
{
this.txtInput.PasswordChar = bPass ? '*' : '\0';
}
#endregion
#region ChangeFontStringStyle() -- changes the font's bold/italic styles
public void ChangeFontStringStyle(string sStyle)
{
try
{
sStyle = sStyle.ToUpper().Trim();
if (sStyle == "NORMAL")
this.txtInput.Font = new Font(this.txtInput.Font, FontStyle.Regular);
else if (sStyle == "BOLD")
this.txtInput.Font = new Font(this.txtInput.Font, FontStyle.Bold);
else if (sStyle == "ITALIC")
this.txtInput.Font = new Font(this.txtInput.Font, FontStyle.Italic);
else if (sStyle == "BOLDITALIC")
this.txtInput.Font = new Font(this.txtInput.Font, FontStyle.Bold | FontStyle.Italic);
}
catch { }
}
#endregion
#region ChangeW(string) -- changes the width of the input textbox
public void ChangeW(string sWidth)
{
try
{
this.txtInput.Width = int.Parse(sWidth);
}
catch { }
}
#endregion
#region ChangeH(string) -- changes the height of the input textbox
public void ChangeH(string sHeight)
{
try
{
this.txtInput.Height = int.Parse(sHeight);
}
catch { }
}
#endregion
#region ChangeX(string) -- changes the X (horizontal) position of the input textbox, relative to its parent skin
public void ChangeX(string sX)
{
try
{
// If the position is changed, make sure the form's auto-location is disabled.
if (this.StartPosition != FormStartPosition.Manual)
this.StartPosition = FormStartPosition.Manual;
// Notice that we need the position of the parent window for offset location.
//
// The Rainmeter class does this for us
this.Location = new System.Drawing.Point(this.parent.X + int.Parse(sX), this.Location.Y);
}
catch { }
}
#endregion
#region ChangeY(string) -- changes the Y (vertical) position of the input textbox, relative to its parent skin
public void ChangeY(string sY)
{
try
{
// If the position is changed, make sure the form's auto-location is disabled.
if (this.StartPosition != FormStartPosition.Manual)
this.StartPosition = FormStartPosition.Manual;
// Notice that we need the position of the parent window for offset location.
//
// The Rainmeter class does this for us
this.Location = new System.Drawing.Point(this.Location.X, this.parent.Y + int.Parse(sY));
}
catch { }
}
#endregion
#region DefaultValue(string) -- sets the default text to appear in the input textbox
public void DefaultValue(string val)
{
this.txtInput.Text = val;
}
#endregion
#region MakeFocusDismiss(bool) -- dismisses the input textbox if it loses cursor/window focus
public void MakeFocusDismiss(bool bDismissing)
{
this._FocusDismiss = bDismissing;
}
#endregion
#region TextMaxLength(int length)
public void TextMaxLength(string length)
{
try
{
this.txtInput.MaxLength = int.Parse(length);
}
catch { }
}
#endregion
#region MakeNumeric(bool numeric) //ES_NUMBER
public void MakeNumeric(bool numeric)
{
_Numeric = numeric;
}
#endregion
#region ShowInputBox() -- shows text input textbox
private DialogResult drBackup = DialogResult.None;
public bool ShowInputBox()
{
if (this.drBackup != DialogResult.None)
return false;
if (this._FocusDismiss)
{
this.Show(this.parent);
while (this.DialogResult == DialogResult.None &&
this.drBackup == DialogResult.None)
{
Application.DoEvents();
System.Threading.Thread.Sleep(44);
}
}
else
{
this.ShowDialog(this.parent);
}
if (this.drBackup != DialogResult.None)
{
if (this.drBackup != DialogResult.OK)
return false;
}
else if (this.DialogResult != DialogResult.None)
{
if (this.DialogResult != DialogResult.OK)
return false;
}
return true;
}
public void Abort()
{
this.drBackup = DialogResult.Abort;
this.Close();
}
private void txtInput_Leave(object sender, EventArgs e)
{
if (this._FocusDismiss)
{
this.drBackup = DialogResult.Cancel;
this.Close();
}
}
private void InputBox_Leave(object sender, EventArgs e)
{
if (this._FocusDismiss)
{
this.drBackup = DialogResult.Cancel;
this.Close();
}
}
private void InputBox_Deactivate(object sender, EventArgs e)
{
if (this._FocusDismiss)
{
this.drBackup = DialogResult.Cancel;
this.Close();
}
}
private void btnOK_Click(object sender, EventArgs e)
{
this._FocusDismiss = false;
this._TextValue = this.txtInput.Text.Trim();
this.drBackup = DialogResult.OK;
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this._FocusDismiss = false;
this.drBackup = DialogResult.Cancel;
this.DialogResult = DialogResult.Cancel;
this.Close();
}
#endregion
}
}