Label.cs 2.8 KB

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