Label.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. SetInitialProperties ();
  22. }
  23. /// <inheritdoc/>
  24. public Label (Rect frame, bool autosize = false) : base (frame)
  25. {
  26. SetInitialProperties (autosize);
  27. }
  28. /// <inheritdoc/>
  29. public Label (ustring text, bool autosize = true) : base (text)
  30. {
  31. SetInitialProperties (autosize);
  32. }
  33. /// <inheritdoc/>
  34. public Label (Rect rect, ustring text, bool autosize = false) : base (rect, text)
  35. {
  36. SetInitialProperties (autosize);
  37. }
  38. /// <inheritdoc/>
  39. public Label (int x, int y, ustring text, bool autosize = true) : base (x, y, text)
  40. {
  41. SetInitialProperties (autosize);
  42. }
  43. /// <inheritdoc/>
  44. public Label (ustring text, TextDirection direction, bool autosize = true)
  45. : base (text, direction)
  46. {
  47. SetInitialProperties (autosize);
  48. }
  49. void SetInitialProperties (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 Action Clicked;
  63. /// <summary>
  64. /// Method invoked when a mouse event is generated
  65. /// </summary>
  66. /// <param name="mouseEvent"></param>
  67. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  68. public override bool OnMouseEvent (MouseEvent mouseEvent)
  69. {
  70. MouseEventArgs args = new MouseEventArgs (mouseEvent);
  71. if (OnMouseClick (args))
  72. return true;
  73. if (MouseEvent (mouseEvent))
  74. return true;
  75. if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
  76. if (!HasFocus && SuperView != null) {
  77. if (!SuperView.HasFocus) {
  78. SuperView.SetFocus ();
  79. }
  80. SetFocus ();
  81. SetNeedsDisplay ();
  82. }
  83. OnClicked ();
  84. return true;
  85. }
  86. return false;
  87. }
  88. ///<inheritdoc/>
  89. public override bool OnEnter (View view)
  90. {
  91. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  92. return base.OnEnter (view);
  93. }
  94. ///<inheritdoc/>
  95. public override bool ProcessHotKey (KeyEvent ke)
  96. {
  97. if (ke.Key == (Key.AltMask | HotKey)) {
  98. if (!HasFocus) {
  99. SetFocus ();
  100. }
  101. OnClicked ();
  102. return true;
  103. }
  104. return base.ProcessHotKey (ke);
  105. }
  106. /// <summary>
  107. /// Virtual method to invoke the <see cref="Clicked"/> event.
  108. /// </summary>
  109. public virtual void OnClicked ()
  110. {
  111. Clicked?.Invoke ();
  112. }
  113. }
  114. }