Label.cs 3.5 KB

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