forked from mono/mono-curses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.cs
2911 lines (2553 loc) · 66.8 KB
/
gui.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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// gui.cs: Simple curses-based GUI toolkit, core
//
// Authors:
// Miguel de Icaza (miguel.de.icaza@gmail.com)
//
// Copyright (C) 2007-2011 Novell (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Generic;
using Mono;
using System.IO;
namespace Mono.Terminal {
/// <summary>
/// The fill values apply from the given x, y values, they will not do
/// a full fill, you must compute x, y yourself.
/// </summary>
[Flags]
public enum Fill {
None = 0,
Horizontal = 1,
Vertical = 2
}
public delegate void Action ();
/// <summary>
/// Base class for creating curses widgets
/// </summary>
public abstract class Widget {
/// <summary>
/// Points to the container of this widget
/// </summary>
public Container Container;
/// <summary>
/// The x position of this widget
/// </summary>
public int x;
/// <summary>
/// The y position of this widget
/// </summary>
public int y;
/// <summary>
/// The width of this widget, it is the area that receives mouse events and that must be repainted.
/// </summary>
public int w;
/// <summary>
/// The height of this widget, it is the area that receives mouse events and that must be repainted.
/// </summary>
public int h;
bool can_focus;
bool has_focus;
public Fill Fill;
static StreamWriter l;
/// <summary>
/// Utility function to log messages
/// </summary>
/// <remarks>
/// <para>This is a utility function that you can use to log messages
/// that will be stored in a file (as curses has taken over the
/// screen and you can not really log information there).</para>
/// <para>
/// The data is written to the file "log2" for now</para>
/// </remarks>
public static void Log (string s)
{
if (l == null)
l = new StreamWriter (File.Create ("log2"));
l.WriteLine (s);
l.Flush ();
}
/// <summary>
/// Utility function to log messages
/// </summary>
/// <remarks>
/// <para>This is a utility function that you can use to log messages
/// that will be stored in a file (as curses has taken over the
/// screen and you can not really log information there). </para>
/// <para>
/// The data is written to the file "log2" for now</para>
/// </remarks>
public static void Log (string s, params object [] args)
{
Log (String.Format (s, args));
}
/// <summary>
/// Public constructor for widgets
/// </summary>
/// <remarks>
/// <para>
/// Constructs a widget that starts at positio (x,y) and has width w and height h.
/// These parameters are used by the methods <see cref="Clear"/> and <see cref="Redraw"/>
/// </para>
/// </remarks>
public Widget (int x, int y, int w, int h)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
Container = Application.EmptyContainer;
}
/// <summary>
/// Focus status of this widget
/// </summary>
/// <remarks>
/// <para>
/// This is used typically by derived classes to flag whether
/// this widget can receive focus or not. Focus is activated
/// by either clicking with the mouse on that widget or by using
//// the tab key.
/// </para>
/// </remarks>
public bool CanFocus {
get {
return can_focus;
}
set {
can_focus = value;
}
}
/// <summary>
/// Gets or sets the current focus status.
/// </summary>
/// <remarks>
/// <para>
/// A widget can grab the focus by setting this value to true and
/// the current focus status can be inquired by using this property.
/// </para>
/// </remarks>
public virtual bool HasFocus {
get {
return has_focus;
}
set {
has_focus = value;
Redraw ();
}
}
/// <summary>
/// Moves inside the first location inside the container
/// </summary>
/// <remarks>
/// <para>This moves the current cursor position to the specified
/// line and column relative to the container
/// client area where this widget is located.</para>
/// <para>The difference between this
/// method and <see cref="BaseMove"/> is that this
/// method goes to the beginning of the client area
/// inside the container while <see cref="BaseMove"/> goes to the first
/// position that container uses.</para>
/// <para>
/// For example, a Frame usually takes up a couple
/// of characters for padding. This method would
/// position the cursor inside the client area,
/// while <see cref="BaseMove"/> would position
/// the cursor at the top of the frame.
/// </para>
/// </remarks>
public void Move (int line, int col)
{
Container.ContainerMove (line, col);
}
/// <summary>
/// Move relative to the top of the container
/// </summary>
/// <remarks>
/// <para>This moves the current cursor position to the specified
/// line and column relative to the start of the container
/// where this widget is located.</para>
/// <para>The difference between this
/// method and <see cref="Move"/> is that this
/// method goes to the beginning of the container,
/// while <see cref="Move"/> goes to the first
/// position that widgets should use.</para>
/// <para>
/// For example, a Frame usually takes up a couple
/// of characters for padding. This method would
/// position the cursor at the beginning of the
/// frame, while <see cref="Move"/> would position
/// the cursor within the frame.
/// </para>
/// </remarks>
public void BaseMove (int line, int col)
{
Container.ContainerBaseMove (line, col);
}
/// <summary>
/// Clears the widget region withthe current color.
/// </summary>
/// <remarks>
/// <para>
/// This clears the entire region used by this widget.
/// </para>
/// </remarks>
public void Clear ()
{
for (int line = 0; line < h; line++){
BaseMove (y + line, x);
for (int col = 0; col < w; col++){
Curses.addch (' ');
}
}
}
/// <summary>
/// Redraws the current widget, must be overwritten.
/// </summary>
/// <remarks>
/// <para>
/// This method should be overwritten by classes
/// that derive from Widget. The default
/// implementation of this method just fills out
/// the region with the character 'x'.
/// </para>
/// <para>
/// Widgets are responsible for painting the
/// entire region that they have been allocated.
/// </para>
/// </remarks>
public virtual void Redraw ()
{
for (int line = 0; line < h; line++){
Move (y + line, x);
for (int col = 0; col < w; col++){
Curses.addch ('x');
}
}
}
/// <summary>
/// If the widget is focused, gives the widget a
/// chance to process the keystroke.
/// </summary>
/// <remarks>
/// <para>
/// Widgets can override this method if they are
/// interested in processing the given keystroke.
/// If they consume the keystroke, they must
/// return true to stop the keystroke from being
/// processed by other widgets or consumed by the
/// widget engine. If they return false, the
/// keystroke will be passed out to other widgets
/// for processing.
/// </para>
/// </remarks>
public virtual bool ProcessKey (int key)
{
return false;
}
/// <summary>
/// Gives widgets a chance to process the given
/// mouse event.
/// </summary>
/// <remarks>
/// Widgets can inspect the value of
/// ev.ButtonState to determine if this is a
/// message they are interested in (typically
/// ev.ButtonState & Curses.Event.Button1Clicked).
/// </remarks>
public virtual void ProcessMouse (Curses.MouseEvent ev)
{
}
/// <summary>
/// This method can be overwritten by widgets that
/// want to provide accelerator functionality
/// (Alt-key for example).
/// </summary>
/// <remarks>
/// <para>
/// Before keys are sent to the widgets on the
/// current Container, all the widgets are
/// processed and the key is passed to the widgets
/// to allow some of them to process the keystroke
/// as a hot-key. </para>
/// <para>
/// For example, if you implement a button that
/// has a hotkey ok "o", you would catch the
/// combination Alt-o here. If the event is
/// caught, you must return true to stop the
/// keystroke from being dispatched to other
/// widgets.
/// </para>
/// <para>
/// Typically to check if the keystroke is an
/// Alt-key combination, you would use
/// Curses.IsAlt(key) and then Char.ToUpper(key)
/// to compare with your hotkey.
/// </para>
/// </remarks>
public virtual bool ProcessHotKey (int key)
{
return false;
}
/// <summary>
/// This method can be overwritten by widgets that
/// want to provide accelerator functionality
/// (Alt-key for example), but without
/// interefering with normal ProcessKey behavior.
/// </summary>
/// <remarks>
/// <para>
/// After keys are sent to the widgets on the
/// current Container, all the widgets are
/// processed and the key is passed to the widgets
/// to allow some of them to process the keystroke
/// as a cold-key. </para>
/// <para>
/// This functionality is used, for example, by
/// default buttons to act on the enter key.
/// Processing this as a hot-key would prevent
/// non-default buttons from consuming the enter
/// keypress when they have the focus.
/// </para>
/// </remarks>
public virtual bool ProcessColdKey (int key)
{
return false;
}
/// <summary>
/// Moves inside the first location inside the container
/// </summary>
/// <remarks>
/// <para>
/// A helper routine that positions the cursor at
/// the logical beginning of the widget. The
/// default implementation merely puts the cursor at
/// the beginning, but derived classes should find a
/// suitable spot for the cursor to be shown.
/// </para>
/// <para>
/// This method must be overwritten by most
/// widgets since screen repaints can happen at
/// any point and it is important to leave the
/// cursor in a position that would make sense for
/// the user (as not all terminals support hiding
/// the cursor), and give the user an impression of
/// where the cursor is. For a button, that
/// would be the position where the hotkey is, for
/// an entry the location of the editing cursor
/// and so on.
/// </para>
/// </remarks>
public virtual void PositionCursor ()
{
Move (y, x);
}
/// <summary>
/// Method to relayout on size changes.
/// </summary>
/// <remarks>
/// <para>
/// This method can be overwritten by widgets that
/// might be interested in adjusting their
/// contents or children (if they are
/// containers).
/// </para>
/// </remarks>
public virtual void DoSizeChanged ()
{
}
/// <summary>
/// Utility function to draw frames
/// </summary>
/// <remarks>
/// Draws a frame with the current color in the
/// specified coordinates.
/// </remarks>
static public void DrawFrame (int col, int line, int width, int height)
{
DrawFrame (col, line, width, height, false);
}
/// <summary>
/// Utility function to draw strings that contain a hotkey
/// </summary>
/// <remarks>
/// Draws a string with the given color. If a character "_" is
/// found, then the next character is drawn using the hotcolor.
/// </remarks>
static public void DrawHotString (string s, int hotcolor, int color)
{
Curses.attrset (color);
foreach (char c in s){
if (c == '_'){
Curses.attrset (hotcolor);
continue;
}
Curses.addch (c);
Curses.attrset (color);
}
}
/// <summary>
/// Utility function to draw frames
/// </summary>
/// <remarks>
/// Draws a frame with the current color in the
/// specified coordinates.
/// </remarks>
static public void DrawFrame (int col, int line, int width, int height, bool fill)
{
int b;
Curses.move (line, col);
Curses.addch (Curses.ACS_ULCORNER);
for (b = 0; b < width-2; b++)
Curses.addch (Curses.ACS_HLINE);
Curses.addch (Curses.ACS_URCORNER);
for (b = 1; b < height-1; b++){
Curses.move (line+b, col);
Curses.addch (Curses.ACS_VLINE);
if (fill){
for (int x = 1; x < width-1; x++)
Curses.addch (' ');
} else
Curses.move (line+b, col+width-1);
Curses.addch (Curses.ACS_VLINE);
}
Curses.move (line + height-1, col);
Curses.addch (Curses.ACS_LLCORNER);
for (b = 0; b < width-2; b++)
Curses.addch (Curses.ACS_HLINE);
Curses.addch (Curses.ACS_LRCORNER);
}
/// <summary>
/// The color used for rendering an unfocused widget.
/// </summary>
public int ColorNormal {
get {
return Container.ContainerColorNormal;
}
}
/// <summary>
/// The color used for rendering a focused widget.
/// </summary>
public int ColorFocus {
get {
return Container.ContainerColorFocus;
}
}
/// <summary>
/// The color used for rendering the hotkey on an
/// unfocused widget.
/// </summary>
public int ColorHotNormal {
get {
return Container.ContainerColorHotNormal;
}
}
/// <summary>
/// The color used to render a hotkey in a focused widget.
/// </summary>
public int ColorHotFocus {
get {
return Container.ContainerColorHotFocus;
}
}
}
/// <summary>
/// Label widget, displays a string at a given position.
/// </summary>
public class Label : Widget {
protected string text;
public int Color = -1;
/// <summary>
/// Public constructor: creates a label at the given
/// coordinate with the given string.
/// </summary>
public Label (int x, int y, string s) : base (x, y, s.Length, 1)
{
text = s;
}
public Label (int x, int y, string s, params object [] args) : base (x, y, String.Format (s, args).Length, 1)
{
text = String.Format (s, args);
}
public override void Redraw ()
{
if (Color != -1)
Curses.attrset (Color);
else
Curses.attrset (ColorNormal);
Move (y, x);
Curses.addstr (text);
}
/// <summary>
/// The text displayed by this widget.
/// </summary>
public virtual string Text {
get {
return text;
}
set {
Curses.attrset (ColorNormal);
Move (y, x);
for (int i = 0; i < text.Length; i++)
Curses.addch (' ');
text = value;
Redraw ();
}
}
}
/// <summary>
/// A label that can be trimmed to a given position
/// </summary>
/// <remarks>
/// Just like a label, but it can be trimmed to a given
/// position if the text being displayed overflows the
/// specified width.
/// </remarks>
public class TrimLabel : Label {
string original;
/// <summary>
/// Public constructor.
/// </summary>
/// <remarks>
/// </remarks>
public TrimLabel (int x, int y, int w, string s) : base (x, y, s)
{
original = s;
SetString (w, s);
}
void SetString (int w, string s)
{
if ((Fill & Fill.Horizontal) != 0)
w = Container.w - Container.Border * 2 - x;
this.w = w;
if (s.Length > w){
if (w < 5)
text = s.Substring (0, w);
else {
text = s.Substring (0, w/2-2) + "..." + s.Substring (s.Length - w/2+1);
}
} else
text = s;
}
public override void DoSizeChanged ()
{
if ((Fill & Fill.Horizontal) != 0)
SetString (0, original);
}
/// <summary>
/// The text displayed by this widget.
/// </summary>
public override string Text {
get {
return original;
}
set {
SetString (w, value);
base.Text = text;
}
}
}
/// <summary>
/// Text data entry widget
/// </summary>
/// <remarks>
/// The Entry widget provides Emacs-like editing
/// functionality, and mouse support.
/// </remarks>
public class Entry : Widget {
string text, kill;
int first, point;
int color;
bool used;
/// <summary>
/// Changed event, raised when the text has clicked.
/// </summary>
/// <remarks>
/// Client code can hook up to this event, it is
/// raised when the text in the entry changes.
/// </remarks>
public event EventHandler Changed;
/// <summary>
/// Public constructor.
/// </summary>
/// <remarks>
/// </remarks>
public Entry (int x, int y, int w, string s) : base (x, y, w, 1)
{
if (s == null)
s = "";
text = s;
point = s.Length;
first = point > w ? point - w : 0;
CanFocus = true;
Color = Application.ColorDialogFocus;
}
/// <summary>
/// Sets or gets the text in the entry.
/// </summary>
/// <remarks>
/// </remarks>
public string Text {
get {
return text;
}
set {
text = value;
if (point > text.Length)
point = text.Length;
first = point > w ? point - w : 0;
Redraw ();
}
}
/// <summary>
/// Sets the secret property.
/// </summary>
/// <remarks>
/// This makes the text entry suitable for entering passwords.
/// </remarks>
public bool Secret { get; set; }
/// <summary>
/// The color used to display the text
/// </summary>
public int Color {
get { return color; }
set { color = value; Container.Redraw (); }
}
/// <summary>
/// The current cursor position.
/// </summary>
public int CursorPosition { get { return point; }}
/// <summary>
/// Sets the cursor position.
/// </summary>
public override void PositionCursor ()
{
Move (y, x+point-first);
}
public override void Redraw ()
{
Curses.attrset (Color);
Move (y, x);
for (int i = 0; i < w; i++){
int p = first + i;
if (p < text.Length){
Curses.addch (Secret ? '*' : text [p]);
} else
Curses.addch (' ' );
}
PositionCursor ();
}
void Adjust ()
{
if (point < first)
first = point;
else if (first + point >= w)
first = point - (w / 3);
Redraw ();
Curses.refresh ();
}
void SetText (string new_text)
{
text = new_text;
if (Changed != null)
Changed (this, EventArgs.Empty);
}
public override bool ProcessKey (int key)
{
switch (key){
case 127:
case Curses.KeyBackspace:
if (point == 0)
return true;
SetText (text.Substring (0, point - 1) + text.Substring (point));
point--;
Adjust ();
break;
case Curses.KeyHome:
case 1: // Control-a, Home
point = 0;
Adjust ();
break;
case Curses.KeyLeft:
case 2: // Control-b, back character
if (point > 0){
point--;
Adjust ();
}
break;
case 4: // Control-d, Delete
if (point == text.Length)
break;
SetText (text.Substring (0, point) + text.Substring (point+1));
Adjust ();
break;
case 5: // Control-e, End
point = text.Length;
Adjust ();
break;
case Curses.KeyRight:
case 6: // Control-f, forward char
if (point == text.Length)
break;
point++;
Adjust ();
break;
case 11: // Control-k, kill-to-end
kill = text.Substring (point);
SetText (text.Substring (0, point));
Adjust ();
break;
case 25: // Control-y, yank
if (kill == null)
return true;
if (point == text.Length){
SetText (text + kill);
point = text.Length;
} else {
SetText (text.Substring (0, point) + kill + text.Substring (point));
point += kill.Length;
}
Adjust ();
break;
case (int) 'b' + Curses.KeyAlt:
int bw = WordBackward (point);
if (bw != -1)
point = bw;
Adjust ();
break;
case (int) 'f' + Curses.KeyAlt:
int fw = WordForward (point);
if (fw != -1)
point = fw;
Adjust ();
break;
default:
// Ignore other control characters.
if (key < 32 || key > 255)
return false;
if (used){
if (point == text.Length){
SetText (text + (char) key);
} else {
SetText (text.Substring (0, point) + (char) key + text.Substring (point));
}
point++;
} else {
SetText ("" + (char) key);
first = 0;
point = 1;
}
used = true;
Adjust ();
return true;
}
used = true;
return true;
}
int WordForward (int p)
{
if (p >= text.Length)
return -1;
int i = p;
if (Char.IsPunctuation (text [p]) || Char.IsWhiteSpace (text[p])){
for (; i < text.Length; i++){
if (Char.IsLetterOrDigit (text [i]))
break;
}
for (; i < text.Length; i++){
if (!Char.IsLetterOrDigit (text [i]))
break;
}
} else {
for (; i < text.Length; i++){
if (!Char.IsLetterOrDigit (text [i]))
break;
}
}
if (i != p)
return i;
return -1;
}
int WordBackward (int p)
{
if (p == 0)
return -1;
int i = p-1;
if (i == 0)
return 0;
if (Char.IsPunctuation (text [i]) || Char.IsSymbol (text [i]) || Char.IsWhiteSpace (text[i])){
for (; i >= 0; i--){
if (Char.IsLetterOrDigit (text [i]))
break;
}
for (; i >= 0; i--){
if (!Char.IsLetterOrDigit (text[i]))
break;
}
} else {
for (; i >= 0; i--){
if (!Char.IsLetterOrDigit (text [i]))
break;
}
}
i++;
if (i != p)
return i;
return -1;
}
public override void ProcessMouse (Curses.MouseEvent ev)
{
if ((ev.ButtonState & Curses.Event.Button1Clicked) == 0)
return;
Container.SetFocus (this);
// We could also set the cursor position.
point = first + (ev.X - x);
if (point > text.Length)
point = text.Length;
if (point < first)
point = 0;
Container.Redraw ();
Container.PositionCursor ();
}
}
/// <summary>
/// Button widget
/// </summary>
/// <remarks>
/// Provides a button that can be clicked, or pressed with
/// the enter key and processes hotkeys (the first uppercase
/// letter in the button becomes the hotkey).
/// </remarks>
public class Button : Widget {
string text;
string shown_text;
char hot_key;
int hot_pos = -1;
bool is_default;
/// <summary>
/// Clicked event, raised when the button is clicked.
/// </summary>
/// <remarks>
/// Client code can hook up to this event, it is
/// raised when the button is activated either with
/// the mouse or the keyboard.
/// </remarks>
public event EventHandler Clicked;
/// <summary>
/// Public constructor, creates a button based on
/// the given text at position 0,0
/// </summary>
/// <remarks>
/// The size of the button is computed based on the
/// text length. This button is not a default button.
/// </remarks>
public Button (string s) : this (0, 0, s) {}
/// <summary>
/// Public constructor, creates a button based on
/// the given text.
/// </summary>
/// <remarks>
/// If the value for is_default is true, a special
/// decoration is used, and the enter key on a
/// dialog would implicitly activate this button.
/// </remarks>
public Button (string s, bool is_default) : this (0, 0, s, is_default) {}
/// <summary>
/// Public constructor, creates a button based on
/// the given text at the given position.
/// </summary>
/// <remarks>
/// The size of the button is computed based on the
/// text length. This button is not a default button.
/// </remarks>
public Button (int x, int y, string s) : this (x, y, s, false) {}
/// <summary>
/// The text displayed by this widget.
/// </summary>
public string Text {
get {
return text;
}
set {
text = value;
if (is_default)
shown_text = "[< " + value + " >]";
else
shown_text = "[ " + value + " ]";
hot_pos = -1;
hot_key = (char) 0;
int i = 0;