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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK.Input;
using Cairo;
using System.Diagnostics;
namespace go
{
public class TextBoxWidget : Label
{
public TextBoxWidget(string _initialValue, WidgetEvent _onTextChanged = null)
: base(_initialValue)
{
onTextChanged = _onTextChanged;
focusable = true;
selColor = Color.SkyBlue;
selFontColor = Color.Black;
fontColor = Color.Black;
background = Color.White;
textAlignment = Alignment.LeftCenter;
}
//trigger update when lost focus to errase text beam
public override bool hasFocus
{
get
{
return base.hasFocus;
}
set
{
base.hasFocus = value;
registerForGraphicUpdate();
}
}
public WidgetEvent onTextChanged;
Color selColor;
Color selFontColor;
//mouse coord in widget space, filled only when clicked
Point mouseLocalPos;
//0 based cursor position in string
private int _currentPos;
public int currentPos
{
get { return _currentPos; }
set { _currentPos = value; }
}
public int selStartPos;
public int selEndPos;
//ordered selection start and end positions
public int selectionStart
{
get { return selEndPos < 0 ? selStartPos : Math.Min(selStartPos, selEndPos); }
}
public int selectionEnd
{ get { return selEndPos < 0 ? selEndPos : Math.Max(selStartPos, selEndPos); } }
//cursor position in cairo units in widget client coord.
double textCursorPos;
double SelStartCursorPos = -1;
double SelEndCursorPos = -1;
bool SelectionInProgress = false;
public string selectedText
{ get { return selectionEnd < 0 ? null : text.Substring(selectionStart, selectionEnd - selectionStart); } }
public bool selectionIsEmpty
{ get { return string.IsNullOrEmpty(selectedText); } }
#region Keyboard handling
public override void ProcessKeyboard(Key key)
{
switch (key)
{
case Key.Back:
if (!selectionIsEmpty)
{
text = text.Remove(selectionStart, selectionEnd - selectionStart);
selEndPos = -1;
currentPos = selStartPos;
}
else if (currentPos > 0)
{
currentPos--;
text = text.Remove(currentPos, 1);
}
break;
case Key.Clear:
break;
case Key.Delete:
if (!selectionIsEmpty)
{
text = text.Remove(selectionStart, selectionEnd - selectionStart);
selEndPos = -1;
currentPos = selStartPos;
}
else if (currentPos < text.Length)
text = text.Remove(currentPos, 1);
break;
case Key.Down:
break;
case Key.End:
currentPos = text.Length;
break;
case Key.Enter:
case Key.KeypadEnter:
if (onTextChanged != null)
onTextChanged(this);
break;
case Key.Escape:
text = "";
currentPos = 0;
selEndPos = -1;
break;
case Key.Home:
currentPos = 0;
break;
case Key.Insert:
break;
case Key.Left:
if (currentPos > 0)
currentPos--;
break;
case Key.Menu:
break;
case Key.NumLock:
break;
case Key.PageDown:
break;
case Key.PageUp:
break;
case Key.RWin:
break;
case Key.Right:
if (currentPos < text.Length)
currentPos++;
break;
case Key.Tab:
text = text.Insert(currentPos, "\t");
currentPos++;
break;
case Key.Up:
break;
case Key.KeypadDecimal:
text = text.Insert(currentPos, new string(new char[] { Interface.decimalSeparator }));
currentPos++;
break;
case Key.Space:
text = text.Insert(currentPos, " ");
currentPos++;
break;
case Key.KeypadDivide:
case Key.Slash:
text = text.Insert(currentPos, "/");
currentPos++;
break;
case Key.KeypadMultiply:
text = text.Insert(currentPos, "*");
currentPos++;
break;
case Key.KeypadMinus:
case Key.Minus:
text = text.Insert(currentPos, "-");
currentPos++;
break;
case Key.KeypadPlus:
case Key.Plus:
text = text.Insert(currentPos, "+");
currentPos++;
break;
default:
if (!selectionIsEmpty)
{
text = text.Remove(selectionStart, selectionEnd - selectionStart);
currentPos = selStartPos;
}
string k = "?";
if ((char)key >= 67 && (char)key <= 76)
k = ((int)key - 67).ToString();
else if ((char)key >= 109 && (char)key <= 118)
k = ((int)key - 109).ToString();
else if (Interface.capitalOn)
k = key.ToString();
else
k = key.ToString().ToLower();
text = text.Insert(currentPos, k);
currentPos++;
selEndPos = -1;
selStartPos = currentPos;
break;
}
registerForGraphicUpdate();
}
#endregion
#region Mouse handling
public override bool ProcessMousePosition(Point mousePos)
{
Rectangle scb = ScreenCoordBounds;
if (scb.Contains(mousePos))
{
Interface.hoverWidget = this;
//Interface.SetCursor(System.Windows.Forms.Cursors.IBeam.Handle);
if (Interface.Mouse[MouseButton.Left])
{
SelectionInProgress = true;
Debug.WriteLine("selection in progress");
mouseLocalPos = mousePos - ScreenCoordBounds.TopLeft - rText.TopLeft;
registerForGraphicUpdate();
}
return true;
}
else
{
Interface.hoverWidget = null;
return false;
}
}
public override void ProcessMouseDown(Point mousePos)
{
Interface.activeWidget = this;
if (!this.hasFocus)
{
selStartPos = 0;
selEndPos = text.Length;
}
else
{
mouseLocalPos = mousePos - ScreenCoordBounds.TopLeft - rText.TopLeft;
selStartPos = -1;
selEndPos = -1;
}
base.ProcessMouseDown(mousePos);
registerForGraphicUpdate();
}
public override void ProcessMouseUp(Point mousePos)
{
SelectionInProgress = false;
}
#endregion
void computeTextCursor(Context gr)
{
FontExtents fe = gr.FontExtents;
TextExtents te;
double cPos = 0f;
for (int i = 0; i < _text.Length; i++)
{
#if _WIN32 || _WIN64
byte[] c = System.Text.UTF8Encoding.UTF8.GetBytes(text.Substring(i, 1));
te = gr.TextExtents(c);
#elif __linux__
te = gr.TextExtents(text.Substring(i, 1));
#endif
double halfWidth = te.XAdvance / 2;
if (mouseLocalPos.X <= cPos + halfWidth)
{
currentPos = i;
textCursorPos = cPos;
mouseLocalPos = -1;
return;
}
cPos += te.XAdvance;
}
currentPos = _text.Length;
textCursorPos = cPos;
//reset mouseLocalPos
mouseLocalPos = -1;
}
void computeTextCursorPosition(Context gr)
{
FontExtents fe = gr.FontExtents;
TextExtents te;
double cPos = 0f;
int limit = currentPos;
if (selectionEnd > 0)
limit = Math.Max(currentPos, selectionEnd);
for (int i = 0; i <= limit; i++)
{
if (i == currentPos)
textCursorPos = cPos;
if (i == selectionStart)
SelStartCursorPos = cPos;
if (i == selectionEnd)
SelEndCursorPos = cPos;
if (i < text.Length)
{
#if _WIN32 || _WIN64
byte[] c = System.Text.UTF8Encoding.UTF8.GetBytes(text.Substring(i, 1));
te = gr.TextExtents(c);
#elif __linux__
te = gr.TextExtents(text.Substring(i,1));
#endif
cPos += te.XAdvance;
}
}
}
internal override void updateGraphic()
{
byte[] txt = utf8Text;
int stride = 4 * renderBounds.Width;
//init bmp with widget background and border
base.updateGraphic();
using (ImageSurface bitmap =
new ImageSurface(bmp, Format.Argb32, renderBounds.Width, renderBounds.Height, stride))
{
using (Context gr = new Context(bitmap))
{
gr.FontOptions.Antialias = Antialias.Subpixel;
gr.FontOptions.HintMetrics = HintMetrics.On;
gr.SetFontSize(fontSize);
FontExtents fe = gr.FontExtents;
rText = new Rectangle(new Point(0, 0), measureRawSize());
//gr.Rotate(Math.PI);
// double a = Math.PI;
//gr.Transform(new c.Matrix(Math.Cos(a),-Math.Sin(a),Math.Sin(a),Math.Cos(a),renderBounds.Width,renderBounds.Height));
gr.Antialias = Antialias.Subpixel;
gr.LineWidth = borderWidth;
float widthRatio = 1f;
float heightRatio = 1f;
Rectangle cb = clientBounds;
Interface.StrokeLoweredRectangle(gr, cb, 1);
//ignore text alignment if size to content = true
if (!sizeToContent)
{
switch (textAlignment)
{
case Alignment.None:
break;
case Alignment.TopLeft: //ok
rText.X = cb.X;
rText.Y = cb.Y;
break;
case Alignment.TopCenter: //ok
rText.Y = cb.Y;
rText.X = cb.X + cb.Width / 2 - rText.Width / 2;
break;
case Alignment.TopRight: //ok
rText.X = cb.Right - rText.Width;
rText.Y = cb.Y;
break;
case Alignment.TopStretch://ok
heightRatio = widthRatio = (float)cb.Width / rText.Width;
rText.X = cb.X;
rText.Y = cb.Y;
rText.Width = cb.Width;
rText.Height = (int)(rText.Height * heightRatio);
break;
case Alignment.LeftCenter://ok
rText.X = cb.X;
rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2;
break;
case Alignment.LeftStretch://ok
heightRatio = widthRatio = (float)cb.Height / rText.Height;
rText.X = cb.X;
rText.Y = cb.Y;
rText.Height = cb.Height;
rText.Width = (int)(widthRatio * cb.Width);
break;
case Alignment.RightCenter://ok
rText.X = cb.X + cb.Width - rText.Width;
rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2;
break;
case Alignment.RightStretch://ok
heightRatio = widthRatio = (float)cb.Height / rText.Height;
rText.Height = cb.Height;
rText.Width = (int)(widthRatio * cb.Width);
rText.X = cb.X;
rText.Y = cb.Y;
break;
case Alignment.BottomCenter://ok
rText.X = cb.Width / 2 - rText.Width / 2;
rText.Y = cb.Height - rText.Height;
break;
case Alignment.BottomStretch://ok
heightRatio = widthRatio = (float)cb.Width / rText.Width;
rText.Width = cb.Width;
rText.Height = (int)(rText.Height * heightRatio);
rText.Y = cb.Bottom - rText.Height;
rText.X = cb.X;
break;
case Alignment.BottomLeft://ok
rText.X = cb.X;
rText.Y = cb.Bottom - rText.Height;
break;
case Alignment.BottomRight://ok
rText.Y = cb.Bottom - rText.Height;
rText.X = cb.Right - rText.Width;
break;
case Alignment.Center://ok
rText.X = cb.X + cb.Width / 2 - rText.Width / 2;
rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2;
break;
case Alignment.Fit://ok, peut être mieu aligné
widthRatio = (float)cb.Width / rText.Width;
heightRatio = (float)cb.Height / rText.Height;
rText = cb.Clone;
break;
case Alignment.HorizontalStretch://ok
heightRatio = widthRatio = (float)cb.Width / rText.Width;
rText.Width = cb.Width;
rText.Height = (int)(heightRatio * rText.Height);
rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2;
rText.X = cb.X;
break;
case Alignment.VerticalStretch://ok
heightRatio = widthRatio = (float)cb.Height / rText.Height;
rText.Height = cb.Height;
rText.Width = (int)(widthRatio * rText.Width);
rText.X = cb.X + cb.Width / 2 - rText.Width / 2;
rText.Y = cb.Y;
break;
default:
break;
}
}
gr.FontMatrix = new Matrix(widthRatio * fontSize, 0, 0, heightRatio * fontSize, 0, 0);
fe = gr.FontExtents;
#region draw text cursor
if (mouseLocalPos > 0)
{
computeTextCursor(gr);
if (SelectionInProgress)
{
selEndPos = currentPos;
SelEndCursorPos = textCursorPos;
}
else if (selStartPos < 0)
{
selStartPos = currentPos;
SelStartCursorPos = textCursorPos;
selEndPos = -1;
}
else
computeTextCursorPosition(gr);
}
else
computeTextCursorPosition(gr);
if (hasFocus)
{
gr.LineWidth = 1;
gr.MoveTo(new PointD(textCursorPos + rText.TopLeft.X, rText.Y + rText.TopLeft.Y));
gr.LineTo(new PointD(textCursorPos + rText.TopLeft.X, rText.Y + fe.Height + rText.TopLeft.Y));
gr.Stroke();
}
#endregion
if (selEndPos >= 0)
{
gr.Color = selColor;
Rectangle selRect =
new Rectangle((int)SelStartCursorPos + rText.TopLeft.X, (int)(rText.Y) + rText.TopLeft.Y, (int)(SelEndCursorPos - SelStartCursorPos), (int)fe.Height);
gr.Rectangle(selRect);
gr.Fill();
gr.Color = selFontColor;
}
else
gr.Color = fontColor;
gr.MoveTo(rText.X, rText.Y + fe.Ascent);
#if _WIN32 || _WIN64
gr.ShowText(txt);
#elif __linux__
gr.ShowText(text);
#endif
gr.Fill();
//gr.LineWidth = 1;
//gr.MoveTo(new PointD(rText.TopLeft.X, rText.Y));
//gr.LineTo(new PointD(rText.TopLeft.X, rText.Y + fe.Ascent));
//gr.Stroke();
}
bitmap.Flush();
//bitmap.WriteToPng(directories.rootDir + @"test.png");
}
//registerForRedraw();
}
}
}
|