Label.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Label.cs: Label control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Text;
  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 (string text, bool autosize = true) : base (text)
  30. {
  31. SetInitialProperties (autosize);
  32. }
  33. /// <inheritdoc/>
  34. public Label (Rect rect, string text, bool autosize = false) : base (rect, text)
  35. {
  36. SetInitialProperties (autosize);
  37. }
  38. /// <inheritdoc/>
  39. public Label (int x, int y, string text, bool autosize = true) : base (x, y, text)
  40. {
  41. SetInitialProperties (autosize);
  42. }
  43. /// <inheritdoc/>
  44. public Label (string 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. // Things this view knows how to do
  54. AddCommand (Command.Default, () => {
  55. // BUGBUG: This is a hack, but it does work.
  56. var can = CanFocus;
  57. CanFocus = true;
  58. SetFocus ();
  59. SuperView.FocusNext ();
  60. CanFocus = can;
  61. return true;
  62. });
  63. AddCommand (Command.Accept, () => AcceptKey ());
  64. // Default key bindings for this view
  65. KeyBindings.Add (KeyCode.Space, Command.Accept);
  66. }
  67. bool AcceptKey ()
  68. {
  69. if (!HasFocus) {
  70. SetFocus ();
  71. }
  72. OnClicked ();
  73. return true;
  74. }
  75. /// <summary>
  76. /// The event fired when the user clicks the primary mouse button within the Bounds of this <see cref="View"/>
  77. /// or if the user presses the action key while this view is focused. (TODO: IsDefault)
  78. /// </summary>
  79. /// <remarks>
  80. /// Client code can hook up to this event, it is
  81. /// raised when the button is activated either with
  82. /// the mouse or the keyboard.
  83. /// </remarks>
  84. public event EventHandler Clicked;
  85. /// <summary>
  86. /// Method invoked when a mouse event is generated
  87. /// </summary>
  88. /// <param name="mouseEvent"></param>
  89. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  90. public override bool OnMouseEvent (MouseEvent mouseEvent)
  91. {
  92. MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
  93. if (OnMouseClick (args))
  94. return true;
  95. if (MouseEvent (mouseEvent))
  96. return true;
  97. if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
  98. if (!HasFocus && SuperView != null) {
  99. if (!SuperView.HasFocus) {
  100. SuperView.SetFocus ();
  101. }
  102. SetFocus ();
  103. SetNeedsDisplay ();
  104. }
  105. OnClicked ();
  106. return true;
  107. }
  108. return false;
  109. }
  110. ///<inheritdoc/>
  111. public override bool OnEnter (View view)
  112. {
  113. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  114. return base.OnEnter (view);
  115. }
  116. /// <summary>
  117. /// Virtual method to invoke the <see cref="Clicked"/> event.
  118. /// </summary>
  119. public virtual void OnClicked ()
  120. {
  121. Clicked?.Invoke (this, EventArgs.Empty);
  122. }
  123. }
  124. }