Label.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // Label.cs: Label control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text.RegularExpressions;
  11. using NStack;
  12. namespace Terminal.Gui {
  13. /// <summary>
  14. /// The Label <see cref="View"/> displays a string at a given position and supports multiple lines separated by newline characters.
  15. /// Multi-line Labels support word wrap.
  16. /// </summary>
  17. /// <remarks>
  18. /// The <see cref="Label"/> view is functionality identical to <see cref="View"/> and is included for API backwards compatibility.
  19. /// </remarks>
  20. public class Label : View {
  21. /// <inheritdoc/>
  22. public Label ()
  23. {
  24. Initialize ();
  25. }
  26. /// <inheritdoc/>
  27. public Label (Rect frame, bool autosize = false) : base (frame)
  28. {
  29. Initialize (autosize);
  30. }
  31. /// <inheritdoc/>
  32. public Label (ustring text, bool autosize = true) : base (text)
  33. {
  34. Initialize (autosize);
  35. }
  36. /// <inheritdoc/>
  37. public Label (Rect rect, ustring text, bool autosize = false) : base (rect, text)
  38. {
  39. Initialize (autosize);
  40. }
  41. /// <inheritdoc/>
  42. public Label (int x, int y, ustring text, bool autosize = true) : base (x, y, text)
  43. {
  44. Initialize (autosize);
  45. }
  46. /// <inheritdoc/>
  47. public Label (ustring text, TextDirection direction, bool autosize = true)
  48. : base (text, direction)
  49. {
  50. Initialize (autosize);
  51. }
  52. void Initialize (bool autosize = true)
  53. {
  54. AutoSize = autosize;
  55. }
  56. /// <summary>
  57. /// Clicked <see cref="Action"/>, raised when the user clicks the primary mouse button within the Bounds of this <see cref="View"/>
  58. /// or if the user presses the action key while this view is focused. (TODO: IsDefault)
  59. /// </summary>
  60. /// <remarks>
  61. /// Client code can hook up to this event, it is
  62. /// raised when the button is activated either with
  63. /// the mouse or the keyboard.
  64. /// </remarks>
  65. public event Action Clicked;
  66. ///// <inheritdoc/>
  67. //public new ustring Text {
  68. // get => base.Text;
  69. // set {
  70. // base.Text = value;
  71. // // This supports Label auto-sizing when Text changes (preserving backwards compat behavior)
  72. // if (Frame.Height == 1 && !ustring.IsNullOrEmpty (value)) {
  73. // int w = Text.RuneCount;
  74. // Width = w;
  75. // Frame = new Rect (Frame.Location, new Size (w, Frame.Height));
  76. // }
  77. // SetNeedsDisplay ();
  78. // }
  79. //}
  80. /// <summary>
  81. /// Method invoked when a mouse event is generated
  82. /// </summary>
  83. /// <param name="mouseEvent"></param>
  84. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  85. public override bool OnMouseEvent (MouseEvent mouseEvent)
  86. {
  87. MouseEventArgs args = new MouseEventArgs (mouseEvent);
  88. if (OnMouseClick (args))
  89. return true;
  90. if (MouseEvent (mouseEvent))
  91. return true;
  92. if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
  93. if (!HasFocus && SuperView != null) {
  94. if (!SuperView.HasFocus) {
  95. SuperView.SetFocus ();
  96. }
  97. SetFocus ();
  98. SetNeedsDisplay ();
  99. }
  100. Clicked?.Invoke ();
  101. return true;
  102. }
  103. return false;
  104. }
  105. ///<inheritdoc/>
  106. public override bool OnEnter (View view)
  107. {
  108. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  109. return base.OnEnter (view);
  110. }
  111. }
  112. }