/
View.cs
3160 lines (2811 loc) · 98.7 KB
/
View.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using NStack;
namespace Terminal.Gui {
/// <summary>
/// Determines the LayoutStyle for a <see cref="View"/>, if Absolute, during <see cref="View.LayoutSubviews"/>, the
/// value from the <see cref="View.Frame"/> will be used, if the value is Computed, then <see cref="View.Frame"/>
/// will be updated from the X, Y <see cref="Pos"/> objects and the Width and Height <see cref="Dim"/> objects.
/// </summary>
public enum LayoutStyle {
/// <summary>
/// The position and size of the view are based on the Frame value.
/// </summary>
Absolute,
/// <summary>
/// The position and size of the view will be computed based on the
/// X, Y, Width and Height properties and set on the Frame.
/// </summary>
Computed
}
/// <summary>
/// View is the base class for all views on the screen and represents a visible element that can render itself and
/// contains zero or more nested views.
/// </summary>
/// <remarks>
/// <para>
/// The View defines the base functionality for user interface elements in Terminal.Gui. Views
/// can contain one or more subviews, can respond to user input and render themselves on the screen.
/// </para>
/// <para>
/// Views supports two layout styles: <see cref="LayoutStyle.Absolute"/> or <see cref="LayoutStyle.Computed"/>.
/// The choice as to which layout style is used by the View
/// is determined when the View is initialized. To create a View using Absolute layout, call a constructor that takes a
/// Rect parameter to specify the absolute position and size (the View.<see cref="View.Frame "/>). To create a View
/// using Computed layout use a constructor that does not take a Rect parameter and set the X, Y, Width and Height
/// properties on the view. Both approaches use coordinates that are relative to the container they are being added to.
/// </para>
/// <para>
/// To switch between Absolute and Computed layout, use the <see cref="LayoutStyle"/> property.
/// </para>
/// <para>
/// Computed layout is more flexible and supports dynamic console apps where controls adjust layout
/// as the terminal resizes or other Views change size or position. The X, Y, Width and Height
/// properties are Dim and Pos objects that dynamically update the position of a view.
/// The X and Y properties are of type <see cref="Pos"/>
/// and you can use either absolute positions, percentages or anchor
/// points. The Width and Height properties are of type
/// <see cref="Dim"/> and can use absolute position,
/// percentages and anchors. These are useful as they will take
/// care of repositioning views when view's frames are resized or
/// if the terminal size changes.
/// </para>
/// <para>
/// Absolute layout requires specifying coordinates and sizes of Views explicitly, and the
/// View will typically stay in a fixed position and size. To change the position and size use the
/// <see cref="Frame"/> property.
/// </para>
/// <para>
/// Subviews (child views) can be added to a View by calling the <see cref="Add(View)"/> method.
/// The container of a View can be accessed with the <see cref="SuperView"/> property.
/// </para>
/// <para>
/// To flag a region of the View's <see cref="Bounds"/> to be redrawn call <see cref="SetNeedsDisplay(Rect)"/>.
/// To flag the entire view for redraw call <see cref="SetNeedsDisplay()"/>.
/// </para>
/// <para>
/// Views have a <see cref="ColorScheme"/> property that defines the default colors that subviews
/// should use for rendering. This ensures that the views fit in the context where
/// they are being used, and allows for themes to be plugged in. For example, the
/// default colors for windows and toplevels uses a blue background, while it uses
/// a white background for dialog boxes and a red background for errors.
/// </para>
/// <para>
/// Subclasses should not rely on <see cref="ColorScheme"/> being
/// set at construction time. If a <see cref="ColorScheme"/> is not set on a view, the view will inherit the
/// value from its <see cref="SuperView"/> and the value might only be valid once a view has been
/// added to a SuperView.
/// </para>
/// <para>
/// By using <see cref="ColorScheme"/> applications will work both
/// in color as well as black and white displays.
/// </para>
/// <para>
/// Views that are focusable should implement the <see cref="PositionCursor"/> to make sure that
/// the cursor is placed in a location that makes sense. Unix terminals do not have
/// a way of hiding the cursor, so it can be distracting to have the cursor left at
/// the last focused view. So views should make sure that they place the cursor
/// in a visually sensible place.
/// </para>
/// <para>
/// The <see cref="LayoutSubviews"/> method is invoked when the size or layout of a view has
/// changed. The default processing system will keep the size and dimensions
/// for views that use the <see cref="LayoutStyle.Absolute"/>, and will recompute the
/// frames for the vies that use <see cref="LayoutStyle.Computed"/>.
/// </para>
/// </remarks>
public partial class View : Responder, ISupportInitializeNotification {
internal enum Direction {
Forward,
Backward
}
// container == SuperView
View container = null;
View focused = null;
Direction focusDirection;
bool autoSize;
ShortcutHelper shortcutHelper;
/// <summary>
/// Event fired when a subview is being added to this view.
/// </summary>
public event Action<View> Added;
/// <summary>
/// Event fired when a subview is being removed from this view.
/// </summary>
public event Action<View> Removed;
/// <summary>
/// Event fired when the view gets focus.
/// </summary>
public event Action<FocusEventArgs> Enter;
/// <summary>
/// Event fired when the view looses focus.
/// </summary>
public event Action<FocusEventArgs> Leave;
/// <summary>
/// Event fired when the view receives the mouse event for the first time.
/// </summary>
public event Action<MouseEventArgs> MouseEnter;
/// <summary>
/// Event fired when the view receives a mouse event for the last time.
/// </summary>
public event Action<MouseEventArgs> MouseLeave;
/// <summary>
/// Event fired when a mouse event is generated.
/// </summary>
public event Action<MouseEventArgs> MouseClick;
/// <summary>
/// Event fired when the <see cref="CanFocus"/> value is being changed.
/// </summary>
public event Action CanFocusChanged;
/// <summary>
/// Event fired when the <see cref="Enabled"/> value is being changed.
/// </summary>
public event Action EnabledChanged;
/// <summary>
/// Event fired when the <see cref="Visible"/> value is being changed.
/// </summary>
public event Action VisibleChanged;
/// <summary>
/// Event invoked when the <see cref="HotKey"/> is changed.
/// </summary>
public event Action<Key> HotKeyChanged;
Key hotKey = Key.Null;
/// <summary>
/// Gets or sets the HotKey defined for this view. A user pressing HotKey on the keyboard while this view has focus will cause the Clicked event to fire.
/// </summary>
public virtual Key HotKey {
get => hotKey;
set {
if (hotKey != value) {
hotKey = TextFormatter.HotKey = (value == Key.Unknown ? Key.Null : value);
}
}
}
/// <summary>
/// Gets or sets the specifier character for the hotkey (e.g. '_'). Set to '\xffff' to disable hotkey support for this View instance. The default is '\xffff'.
/// </summary>
public virtual Rune HotKeySpecifier {
get => TextFormatter.HotKeySpecifier;
set {
TextFormatter.HotKeySpecifier = value;
SetHotKey ();
}
}
/// <summary>
/// This is the global setting that can be used as a global shortcut to invoke an action if provided.
/// </summary>
public Key Shortcut {
get => shortcutHelper.Shortcut;
set {
if (shortcutHelper.Shortcut != value && (ShortcutHelper.PostShortcutValidation (value) || value == Key.Null)) {
shortcutHelper.Shortcut = value;
}
}
}
/// <summary>
/// The keystroke combination used in the <see cref="Shortcut"/> as string.
/// </summary>
public ustring ShortcutTag => ShortcutHelper.GetShortcutTag (shortcutHelper.Shortcut);
/// <summary>
/// The action to run if the <see cref="Shortcut"/> is defined.
/// </summary>
public virtual Action ShortcutAction { get; set; }
/// <summary>
/// Gets or sets arbitrary data for the view.
/// </summary>
/// <remarks>This property is not used internally.</remarks>
public object Data { get; set; }
internal Direction FocusDirection {
get => SuperView?.FocusDirection ?? focusDirection;
set {
if (SuperView != null)
SuperView.FocusDirection = value;
else
focusDirection = value;
}
}
/// <summary>
/// Points to the current driver in use by the view, it is a convenience property
/// for simplifying the development of new views.
/// </summary>
public static ConsoleDriver Driver => Application.Driver;
static readonly IList<View> empty = new List<View> (0).AsReadOnly ();
// This is null, and allocated on demand.
List<View> subviews;
/// <summary>
/// This returns a list of the subviews contained by this view.
/// </summary>
/// <value>The subviews.</value>
public IList<View> Subviews => subviews?.AsReadOnly () ?? empty;
// Internally, we use InternalSubviews rather than subviews, as we do not expect us
// to make the same mistakes our users make when they poke at the Subviews.
internal IList<View> InternalSubviews => subviews ?? empty;
// This is null, and allocated on demand.
List<View> tabIndexes;
/// <summary>
/// Configurable keybindings supported by the control
/// </summary>
private Dictionary<Key, Command []> KeyBindings { get; set; } = new Dictionary<Key, Command []> ();
private Dictionary<Command, Func<bool?>> CommandImplementations { get; set; } = new Dictionary<Command, Func<bool?>> ();
/// <summary>
/// This returns a tab index list of the subviews contained by this view.
/// </summary>
/// <value>The tabIndexes.</value>
public IList<View> TabIndexes => tabIndexes?.AsReadOnly () ?? empty;
int tabIndex = -1;
/// <summary>
/// Indicates the index of the current <see cref="View"/> from the <see cref="TabIndexes"/> list.
/// </summary>
public int TabIndex {
get { return tabIndex; }
set {
if (!CanFocus) {
tabIndex = -1;
return;
} else if (SuperView?.tabIndexes == null || SuperView?.tabIndexes.Count == 1) {
tabIndex = 0;
return;
} else if (tabIndex == value) {
return;
}
tabIndex = value > SuperView.tabIndexes.Count - 1 ? SuperView.tabIndexes.Count - 1 : value < 0 ? 0 : value;
tabIndex = GetTabIndex (tabIndex);
if (SuperView.tabIndexes.IndexOf (this) != tabIndex) {
SuperView.tabIndexes.Remove (this);
SuperView.tabIndexes.Insert (tabIndex, this);
SetTabIndex ();
}
}
}
int GetTabIndex (int idx)
{
var i = 0;
foreach (var v in SuperView.tabIndexes) {
if (v.tabIndex == -1 || v == this) {
continue;
}
i++;
}
return Math.Min (i, idx);
}
void SetTabIndex ()
{
var i = 0;
foreach (var v in SuperView.tabIndexes) {
if (v.tabIndex == -1) {
continue;
}
v.tabIndex = i;
i++;
}
}
bool tabStop = true;
/// <summary>
/// This only be <see langword="true"/> if the <see cref="CanFocus"/> is also <see langword="true"/>
/// and the focus can be avoided by setting this to <see langword="false"/>
/// </summary>
public bool TabStop {
get => tabStop;
set {
if (tabStop == value) {
return;
}
tabStop = CanFocus && value;
}
}
bool oldCanFocus;
int oldTabIndex;
/// <inheritdoc/>
public override bool CanFocus {
get => base.CanFocus;
set {
if (!addingView && IsInitialized && SuperView?.CanFocus == false && value) {
throw new InvalidOperationException ("Cannot set CanFocus to true if the SuperView CanFocus is false!");
}
if (base.CanFocus != value) {
base.CanFocus = value;
switch (value) {
case false when tabIndex > -1:
TabIndex = -1;
break;
case true when SuperView?.CanFocus == false && addingView:
SuperView.CanFocus = true;
break;
}
if (value && tabIndex == -1) {
TabIndex = SuperView != null ? SuperView.tabIndexes.IndexOf (this) : -1;
}
TabStop = value;
if (!value && SuperView?.Focused == this) {
SuperView.focused = null;
}
if (!value && HasFocus) {
SetHasFocus (false, this);
SuperView?.EnsureFocus ();
if (SuperView != null && SuperView.Focused == null) {
SuperView.FocusNext ();
if (SuperView.Focused == null) {
Application.Current.FocusNext ();
}
Application.EnsuresTopOnFront ();
}
}
if (subviews != null && IsInitialized) {
foreach (var view in subviews) {
if (view.CanFocus != value) {
if (!value) {
view.oldCanFocus = view.CanFocus;
view.oldTabIndex = view.tabIndex;
view.CanFocus = false;
view.tabIndex = -1;
} else {
if (addingView) {
view.addingView = true;
}
view.CanFocus = view.oldCanFocus;
view.tabIndex = view.oldTabIndex;
view.addingView = false;
}
}
}
}
OnCanFocusChanged ();
SetNeedsDisplay ();
}
}
}
internal Rect NeedDisplay { get; private set; } = Rect.Empty;
// The frame for the object. Superview relative.
Rect frame;
/// <summary>
/// Gets or sets an identifier for the view;
/// </summary>
/// <value>The identifier.</value>
/// <remarks>The id should be unique across all Views that share a SuperView.</remarks>
public ustring Id { get; set; } = "";
/// <summary>
/// Returns a value indicating if this View is currently on Top (Active)
/// </summary>
public bool IsCurrentTop => Application.Current == this;
/// <summary>
/// Gets or sets a value indicating whether this <see cref="View"/> wants mouse position reports.
/// </summary>
/// <value><see langword="true"/> if want mouse position reports; otherwise, <see langword="false"/>.</value>
public virtual bool WantMousePositionReports { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="View"/> want continuous button pressed event.
/// </summary>
public virtual bool WantContinuousButtonPressed { get; set; }
/// <summary>
/// Gets or sets the frame for the view. The frame is relative to the view's container (<see cref="SuperView"/>).
/// </summary>
/// <value>The frame.</value>
/// <remarks>
/// <para>
/// Change the Frame when using the <see cref="Terminal.Gui.LayoutStyle.Absolute"/> layout style to move or resize views.
/// </para>
/// <para>
/// Altering the Frame of a view will trigger the redrawing of the
/// view as well as the redrawing of the affected regions of the <see cref="SuperView"/>.
/// </para>
/// </remarks>
public virtual Rect Frame {
get => frame;
set {
var rect = GetMaxNeedDisplay (frame, value);
frame = new Rect (value.X, value.Y, Math.Max (value.Width, 0), Math.Max (value.Height, 0));
TextFormatter.Size = GetBoundsTextFormatterSize ();
SetNeedsLayout ();
SetNeedsDisplay (rect);
}
}
///// <summary>
///// Gets an enumerator that enumerates the subviews in this view.
///// </summary>
///// <returns>The enumerator.</returns>
//public IEnumerator GetEnumerator ()
//{
// foreach (var v in InternalSubviews)
// yield return v;
//}
LayoutStyle layoutStyle;
/// <summary>
/// Controls how the View's <see cref="Frame"/> is computed during the LayoutSubviews method, if the style is set to
/// <see cref="Terminal.Gui.LayoutStyle.Absolute"/>,
/// LayoutSubviews does not change the <see cref="Frame"/>. If the style is <see cref="Terminal.Gui.LayoutStyle.Computed"/>
/// the <see cref="Frame"/> is updated using
/// the <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties.
/// </summary>
/// <value>The layout style.</value>
public LayoutStyle LayoutStyle {
get => layoutStyle;
set {
layoutStyle = value;
SetNeedsLayout ();
}
}
/// <summary>
/// The bounds represent the View-relative rectangle used for this view; the area inside of the view.
/// </summary>
/// <value>The bounds.</value>
/// <remarks>
/// <para>
/// Updates to the Bounds update the <see cref="Frame"/>,
/// and has the same side effects as updating the <see cref="Frame"/>.
/// </para>
/// <para>
/// Because <see cref="Bounds"/> coordinates are relative to the upper-left corner of the <see cref="View"/>,
/// the coordinates of the upper-left corner of the rectangle returned by this property are (0,0).
/// Use this property to obtain the size and coordinates of the client area of the
/// control for tasks such as drawing on the surface of the control.
/// </para>
/// </remarks>
public Rect Bounds {
get => new Rect (Point.Empty, Frame.Size);
set => Frame = new Rect (frame.Location, value.Size);
}
Pos x, y;
/// <summary>
/// Gets or sets the X position for the view (the column). Only used if the <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Computed"/>.
/// </summary>
/// <value>The X Position.</value>
/// <remarks>
/// If <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
/// </remarks>
public Pos X {
get => x;
set {
if (ForceValidatePosDim && !ValidatePosDim (x, value)) {
throw new ArgumentException ();
}
x = value;
ProcessResizeView ();
}
}
/// <summary>
/// Gets or sets the Y position for the view (the row). Only used if the <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Computed"/>.
/// </summary>
/// <value>The y position (line).</value>
/// <remarks>
/// If <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
/// </remarks>
public Pos Y {
get => y;
set {
if (ForceValidatePosDim && !ValidatePosDim (y, value)) {
throw new ArgumentException ();
}
y = value;
ProcessResizeView ();
}
}
Dim width, height;
/// <summary>
/// Gets or sets the width of the view. Only used the <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Computed"/>.
/// </summary>
/// <value>The width.</value>
/// <remarks>
/// If <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
/// </remarks>
public Dim Width {
get => width;
set {
if (ForceValidatePosDim && !ValidatePosDim (width, value)) {
throw new ArgumentException ("ForceValidatePosDim is enabled", nameof (Width));
}
width = value;
if (ForceValidatePosDim) {
var isValidNewAutSize = autoSize && IsValidAutoSizeWidth (width);
if (IsAdded && autoSize && !isValidNewAutSize) {
throw new InvalidOperationException ("Must set AutoSize to false before set the Width.");
}
}
ProcessResizeView ();
}
}
/// <summary>
/// Gets or sets the height of the view. Only used the <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Computed"/>.
/// </summary>
/// <value>The height.</value>
/// If <see cref="LayoutStyle"/> is <see cref="Terminal.Gui.LayoutStyle.Absolute"/> changing this property has no effect and its value is indeterminate.
public Dim Height {
get => height;
set {
if (ForceValidatePosDim && !ValidatePosDim (height, value)) {
throw new ArgumentException ("ForceValidatePosDim is enabled", nameof (Height));
}
height = value;
if (ForceValidatePosDim) {
var isValidNewAutSize = autoSize && IsValidAutoSizeHeight (height);
if (IsAdded && autoSize && !isValidNewAutSize) {
throw new InvalidOperationException ("Must set AutoSize to false before set the Height.");
}
}
ProcessResizeView ();
}
}
/// <summary>
/// Forces validation with <see cref="Terminal.Gui.LayoutStyle.Computed"/> layout
/// to avoid breaking the <see cref="Pos"/> and <see cref="Dim"/> settings.
/// </summary>
public bool ForceValidatePosDim { get; set; }
bool ValidatePosDim (object oldValue, object newValue)
{
if (!IsInitialized || layoutStyle == LayoutStyle.Absolute || oldValue == null || oldValue.GetType () == newValue.GetType () || this is Toplevel) {
return true;
}
if (layoutStyle == LayoutStyle.Computed) {
if (oldValue.GetType () != newValue.GetType () && !(newValue is Pos.PosAbsolute || newValue is Dim.DimAbsolute)) {
return true;
}
}
return false;
}
/// <summary>
/// Verifies if the minimum width or height can be sets in the view.
/// </summary>
/// <param name="size">The size.</param>
/// <returns><see langword="true"/> if the size can be set, <see langword="false"/> otherwise.</returns>
public bool GetMinWidthHeight (out Size size)
{
size = Size.Empty;
if (!AutoSize && !ustring.IsNullOrEmpty (TextFormatter.Text)) {
switch (TextFormatter.IsVerticalDirection (TextDirection)) {
case true:
var colWidth = TextFormatter.GetSumMaxCharWidth (new List<ustring> { TextFormatter.Text }, 0, 1);
if (frame.Width < colWidth && (Width == null || (Bounds.Width >= 0 && Width is Dim.DimAbsolute
&& Width.Anchor (0) >= 0 && Width.Anchor (0) < colWidth))) {
size = new Size (colWidth, Bounds.Height);
return true;
}
break;
default:
if (frame.Height < 1 && (Height == null || (Height is Dim.DimAbsolute && Height.Anchor (0) == 0))) {
size = new Size (Bounds.Width, 1);
return true;
}
break;
}
}
return false;
}
/// <summary>
/// Sets the minimum width or height if the view can be resized.
/// </summary>
/// <returns><see langword="true"/> if the size can be set, <see langword="false"/> otherwise.</returns>
public bool SetMinWidthHeight ()
{
if (GetMinWidthHeight (out Size size)) {
Bounds = new Rect (Bounds.Location, size);
TextFormatter.Size = GetBoundsTextFormatterSize ();
return true;
}
return false;
}
/// <summary>
/// Gets or sets the <see cref="Terminal.Gui.TextFormatter"/> which can be handled differently by any derived class.
/// </summary>
public TextFormatter TextFormatter { get; set; }
/// <summary>
/// Returns the container for this view, or null if this view has not been added to a container.
/// </summary>
/// <value>The super view.</value>
public View SuperView => container;
/// <summary>
/// Initializes a new instance of a <see cref="Terminal.Gui.LayoutStyle.Absolute"/> <see cref="View"/> class with the absolute
/// dimensions specified in the <see langword="frame"/> parameter.
/// </summary>
/// <param name="frame">The region covered by this view.</param>
/// <remarks>
/// This constructor initialize a View with a <see cref="LayoutStyle"/> of <see cref="Terminal.Gui.LayoutStyle.Absolute"/>.
/// Use <see cref="View"/> to initialize a View with <see cref="LayoutStyle"/> of <see cref="Terminal.Gui.LayoutStyle.Computed"/>
/// </remarks>
public View (Rect frame)
{
Initialize (ustring.Empty, frame, LayoutStyle.Absolute, TextDirection.LeftRight_TopBottom);
}
/// <summary>
/// Initializes a new instance of <see cref="View"/> using <see cref="Terminal.Gui.LayoutStyle.Computed"/> layout.
/// </summary>
/// <remarks>
/// <para>
/// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically control the size and location of the view.
/// The <see cref="View"/> will be created using <see cref="Terminal.Gui.LayoutStyle.Computed"/>
/// coordinates. The initial size (<see cref="View.Frame"/>) will be
/// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
/// </para>
/// <para>
/// If <see cref="Height"/> is greater than one, word wrapping is provided.
/// </para>
/// <para>
/// This constructor initialize a View with a <see cref="LayoutStyle"/> of <see cref="Terminal.Gui.LayoutStyle.Computed"/>.
/// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically control the size and location of the view.
/// </para>
/// </remarks>
public View () : this (text: string.Empty, direction: TextDirection.LeftRight_TopBottom) { }
/// <summary>
/// Initializes a new instance of <see cref="View"/> using <see cref="Terminal.Gui.LayoutStyle.Absolute"/> layout.
/// </summary>
/// <remarks>
/// <para>
/// The <see cref="View"/> will be created at the given
/// coordinates with the given string. The size (<see cref="View.Frame"/>) will be
/// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
/// </para>
/// <para>
/// No line wrapping is provided.
/// </para>
/// </remarks>
/// <param name="x">column to locate the View.</param>
/// <param name="y">row to locate the View.</param>
/// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
public View (int x, int y, ustring text) : this (TextFormatter.CalcRect (x, y, text), text) { }
/// <summary>
/// Initializes a new instance of <see cref="View"/> using <see cref="Terminal.Gui.LayoutStyle.Absolute"/> layout.
/// </summary>
/// <remarks>
/// <para>
/// The <see cref="View"/> will be created at the given
/// coordinates with the given string. The initial size (<see cref="View.Frame"/>) will be
/// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
/// </para>
/// <para>
/// If <c>rect.Height</c> is greater than one, word wrapping is provided.
/// </para>
/// </remarks>
/// <param name="rect">Location.</param>
/// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
/// <param name="border">The <see cref="Border"/>.</param>
public View (Rect rect, ustring text, Border border = null)
{
Initialize (text, rect, LayoutStyle.Absolute, TextDirection.LeftRight_TopBottom, border);
}
/// <summary>
/// Initializes a new instance of <see cref="View"/> using <see cref="Terminal.Gui.LayoutStyle.Computed"/> layout.
/// </summary>
/// <remarks>
/// <para>
/// The <see cref="View"/> will be created using <see cref="Terminal.Gui.LayoutStyle.Computed"/>
/// coordinates with the given string. The initial size (<see cref="View.Frame"/>) will be
/// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
/// </para>
/// <para>
/// If <see cref="Height"/> is greater than one, word wrapping is provided.
/// </para>
/// </remarks>
/// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
/// <param name="direction">The text direction.</param>
/// <param name="border">The <see cref="Border"/>.</param>
public View (ustring text, TextDirection direction = TextDirection.LeftRight_TopBottom, Border border = null)
{
Initialize (text, Rect.Empty, LayoutStyle.Computed, direction, border);
}
void Initialize (ustring text, Rect rect, LayoutStyle layoutStyle = LayoutStyle.Computed,
TextDirection direction = TextDirection.LeftRight_TopBottom, Border border = null)
{
TextFormatter = new TextFormatter ();
TextFormatter.HotKeyChanged += TextFormatter_HotKeyChanged;
TextDirection = direction;
Border = border;
if (Border != null) {
Border.Child = this;
}
shortcutHelper = new ShortcutHelper ();
CanFocus = false;
TabIndex = -1;
TabStop = false;
LayoutStyle = layoutStyle;
// BUGBUG: CalcRect doesn't account for line wrapping
var r = rect.IsEmpty ? TextFormatter.CalcRect (0, 0, text, direction) : rect;
Frame = r;
Text = text;
UpdateTextFormatterText ();
ProcessResizeView ();
}
/// <summary>
/// Can be overridden if the <see cref="Terminal.Gui.TextFormatter.Text"/> has
/// different format than the default.
/// </summary>
protected virtual void UpdateTextFormatterText ()
{
TextFormatter.Text = text;
}
/// <summary>
/// Can be overridden if the view resize behavior is
/// different than the default.
/// </summary>
protected virtual void ProcessResizeView ()
{
var actX = x is Pos.PosAbsolute ? x.Anchor (0) : frame.X;
var actY = y is Pos.PosAbsolute ? y.Anchor (0) : frame.Y;
Rect oldFrame = frame;
if (AutoSize) {
var s = GetAutoSize ();
var w = width is Dim.DimAbsolute && width.Anchor (0) > s.Width ? width.Anchor (0) : s.Width;
var h = height is Dim.DimAbsolute && height.Anchor (0) > s.Height ? height.Anchor (0) : s.Height;
frame = new Rect (new Point (actX, actY), new Size (w, h));
} else {
var w = width is Dim.DimAbsolute ? width.Anchor (0) : frame.Width;
var h = height is Dim.DimAbsolute ? height.Anchor (0) : frame.Height;
frame = new Rect (new Point (actX, actY), new Size (w, h));
SetMinWidthHeight ();
}
TextFormatter.Size = GetBoundsTextFormatterSize ();
SetNeedsLayout ();
SetNeedsDisplay (GetMaxNeedDisplay (oldFrame, frame));
}
Rect GetMaxNeedDisplay (Rect oldFrame, Rect newFrame)
{
var rect = new Rect () {
X = Math.Min (oldFrame.X, newFrame.X),
Y = Math.Min (oldFrame.Y, newFrame.Y),
Width = Math.Max (oldFrame.Width, newFrame.Width),
Height = Math.Max (oldFrame.Height, newFrame.Height)
};
rect.Width += Math.Max (oldFrame.X - newFrame.X, 0);
rect.Height += Math.Max (oldFrame.Y - newFrame.Y, 0);
return rect;
}
void TextFormatter_HotKeyChanged (Key obj)
{
HotKeyChanged?.Invoke (obj);
}
/// <summary>
/// Sets a flag indicating this view needs to be redisplayed because its state has changed.
/// </summary>
public void SetNeedsDisplay ()
{
SetNeedsDisplay (Bounds);
}
internal bool LayoutNeeded { get; private set; } = true;
internal void SetNeedsLayout ()
{
if (LayoutNeeded)
return;
LayoutNeeded = true;
if (SuperView == null)
return;
SuperView.SetNeedsLayout ();
foreach (var view in Subviews) {
view.SetNeedsLayout ();
}
TextFormatter.NeedsFormat = true;
}
/// <summary>
/// Removes the <see cref="SetNeedsLayout"/> setting on this view.
/// </summary>
protected void ClearLayoutNeeded ()
{
LayoutNeeded = false;
}
/// <summary>
/// Flags the view-relative region on this View as needing to be repainted.
/// </summary>
/// <param name="region">The view-relative region that must be flagged for repaint.</param>
public void SetNeedsDisplay (Rect region)
{
if (NeedDisplay.IsEmpty)
NeedDisplay = region;
else {
var x = Math.Min (NeedDisplay.X, region.X);
var y = Math.Min (NeedDisplay.Y, region.Y);
var w = Math.Max (NeedDisplay.Width, region.Width);
var h = Math.Max (NeedDisplay.Height, region.Height);
NeedDisplay = new Rect (x, y, w, h);
}
container?.SetChildNeedsDisplay ();
if (subviews == null)
return;
foreach (var view in subviews)
if (view.Frame.IntersectsWith (region)) {
var childRegion = Rect.Intersect (view.Frame, region);
childRegion.X -= view.Frame.X;
childRegion.Y -= view.Frame.Y;
view.SetNeedsDisplay (childRegion);
}
}
internal bool ChildNeedsDisplay { get; private set; }
/// <summary>
/// Indicates that any child views (in the <see cref="Subviews"/> list) need to be repainted.
/// </summary>
public void SetChildNeedsDisplay ()
{
ChildNeedsDisplay = true;
if (container != null)
container.SetChildNeedsDisplay ();
}
internal bool addingView;
/// <summary>
/// Adds a subview (child) to this view.
/// </summary>
/// <remarks>
/// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property.
/// See also <seealso cref="Remove(View)"/> <seealso cref="RemoveAll"/>
/// </remarks>
public virtual void Add (View view)
{
if (view == null)
return;
if (subviews == null) {
subviews = new List<View> ();
}
if (tabIndexes == null) {
tabIndexes = new List<View> ();
}
subviews.Add (view);
tabIndexes.Add (view);
view.container = this;
if (view.CanFocus) {
addingView = true;
if (SuperView?.CanFocus == false) {
SuperView.addingView = true;
SuperView.CanFocus = true;
SuperView.addingView = false;
}
CanFocus = true;
view.tabIndex = tabIndexes.IndexOf (view);
addingView = false;
}
if (view.Enabled && !Enabled) {
view.oldEnabled = true;
view.Enabled = false;
}
SetNeedsLayout ();
SetNeedsDisplay ();
OnAdded (view);
if (IsInitialized) {
view.BeginInit ();
view.EndInit ();
}
}
/// <summary>
/// Adds the specified views (children) to the view.
/// </summary>
/// <param name="views">Array of one or more views (can be optional parameter).</param>
/// <remarks>
/// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property.
/// See also <seealso cref="Remove(View)"/> <seealso cref="RemoveAll"/>
/// </remarks>
public void Add (params View [] views)
{
if (views == null)
return;
foreach (var view in views)
Add (view);
}
/// <summary>
/// Removes all subviews (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
/// </summary>
public virtual void RemoveAll ()
{
if (subviews == null)
return;
while (subviews.Count > 0) {
Remove (subviews [0]);
}
}
/// <summary>
/// Removes a subview added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
/// </summary>
/// <remarks>
/// </remarks>