Label.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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) : base (frame)
  28. {
  29. }
  30. /// <inheritdoc/>
  31. public Label (ustring text) : base (text)
  32. {
  33. Initialize ();
  34. }
  35. /// <inheritdoc/>
  36. public Label (Rect rect, ustring text) : base (rect, text)
  37. {
  38. }
  39. /// <inheritdoc/>
  40. public Label (int x, int y, ustring text) : base (x, y, text)
  41. {
  42. Initialize ();
  43. }
  44. /// <inheritdoc/>
  45. public Label (ustring text, TextDirection direction)
  46. : base (text, direction)
  47. {
  48. Initialize ();
  49. }
  50. void Initialize ()
  51. {
  52. AutoSize = true;
  53. }
  54. /// <summary>
  55. /// Clicked <see cref="Action"/>, raised when the user clicks the primary mouse button within the Bounds of this <see cref="View"/>
  56. /// or if the user presses the action key while this view is focused. (TODO: IsDefault)
  57. /// </summary>
  58. /// <remarks>
  59. /// Client code can hook up to this event, it is
  60. /// raised when the button is activated either with
  61. /// the mouse or the keyboard.
  62. /// </remarks>
  63. public event Action Clicked;
  64. ///// <inheritdoc/>
  65. //public new ustring Text {
  66. // get => base.Text;
  67. // set {
  68. // base.Text = value;
  69. // // This supports Label auto-sizing when Text changes (preserving backwards compat behavior)
  70. // if (Frame.Height == 1 && !ustring.IsNullOrEmpty (value)) {
  71. // int w = Text.RuneCount;
  72. // Width = w;
  73. // Frame = new Rect (Frame.Location, new Size (w, Frame.Height));
  74. // }
  75. // SetNeedsDisplay ();
  76. // }
  77. //}
  78. /// <summary>
  79. /// Method invoked when a mouse event is generated
  80. /// </summary>
  81. /// <param name="mouseEvent"></param>
  82. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  83. public override bool OnMouseEvent (MouseEvent mouseEvent)
  84. {
  85. MouseEventArgs args = new MouseEventArgs (mouseEvent);
  86. OnMouseClick (args);
  87. if (args.Handled)
  88. return true;
  89. if (MouseEvent (mouseEvent))
  90. return true;
  91. if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
  92. if (!HasFocus && SuperView != null) {
  93. SetFocus ();
  94. SetNeedsDisplay ();
  95. }
  96. Clicked?.Invoke ();
  97. return true;
  98. }
  99. return false;
  100. }
  101. ///<inheritdoc/>
  102. public override bool OnEnter (View view)
  103. {
  104. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  105. return base.OnEnter (view);
  106. }
  107. }
  108. }