Label.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. Height = 1;
  52. AutoSize = autosize;
  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 EventHandler Clicked;
  64. /// <summary>
  65. /// Method invoked when a mouse event is generated
  66. /// </summary>
  67. /// <param name="mouseEvent"></param>
  68. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  69. public override bool OnMouseEvent (MouseEvent mouseEvent)
  70. {
  71. MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
  72. if (OnMouseClick (args))
  73. return true;
  74. if (MouseEvent (mouseEvent))
  75. return true;
  76. if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
  77. if (!HasFocus && SuperView != null) {
  78. if (!SuperView.HasFocus) {
  79. SuperView.SetFocus ();
  80. }
  81. SetFocus ();
  82. SetNeedsDisplay ();
  83. }
  84. OnClicked ();
  85. return true;
  86. }
  87. return false;
  88. }
  89. ///<inheritdoc/>
  90. public override bool OnEnter (View view)
  91. {
  92. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  93. return base.OnEnter (view);
  94. }
  95. ///<inheritdoc/>
  96. public override bool ProcessHotKey (KeyEvent ke)
  97. {
  98. if (ke.Key == (Key.AltMask | HotKey)) {
  99. if (!HasFocus) {
  100. SetFocus ();
  101. }
  102. OnClicked ();
  103. return true;
  104. }
  105. return base.ProcessHotKey (ke);
  106. }
  107. /// <summary>
  108. /// Virtual method to invoke the <see cref="Clicked"/> event.
  109. /// </summary>
  110. public virtual void OnClicked ()
  111. {
  112. Clicked?.Invoke (this, EventArgs.Empty);
  113. }
  114. }
  115. }