Label.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. //HotKeySpecifier = new Rune ('_');
  54. //if (HotKey != Key.Null) {
  55. // AddKeyBinding (Key.Space | HotKey, Command.Accept);
  56. //}
  57. }
  58. /// <summary>
  59. /// The event fired when the user clicks the primary mouse button within the Bounds of this <see cref="View"/>
  60. /// or if the user presses the action key while this view is focused. (TODO: IsDefault)
  61. /// </summary>
  62. /// <remarks>
  63. /// Client code can hook up to this event, it is
  64. /// raised when the button is activated either with
  65. /// the mouse or the keyboard.
  66. /// </remarks>
  67. public event EventHandler Clicked;
  68. /// <summary>
  69. /// Method invoked when a mouse event is generated
  70. /// </summary>
  71. /// <param name="mouseEvent"></param>
  72. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  73. public override bool OnMouseEvent (MouseEvent mouseEvent)
  74. {
  75. MouseEventEventArgs args = new MouseEventEventArgs (mouseEvent);
  76. if (OnMouseClick (args))
  77. return true;
  78. if (MouseEvent (mouseEvent))
  79. return true;
  80. if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
  81. if (!HasFocus && SuperView != null) {
  82. if (!SuperView.HasFocus) {
  83. SuperView.SetFocus ();
  84. }
  85. SetFocus ();
  86. SetNeedsDisplay ();
  87. }
  88. OnClicked ();
  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. ///<inheritdoc/>
  100. public override bool ProcessHotKey (KeyEvent ke)
  101. {
  102. if (ke.Key == (Key.AltMask | HotKey)) {
  103. if (!HasFocus) {
  104. SetFocus ();
  105. }
  106. OnClicked ();
  107. return true;
  108. }
  109. return base.ProcessHotKey (ke);
  110. }
  111. /// <summary>
  112. /// Virtual method to invoke the <see cref="Clicked"/> event.
  113. /// </summary>
  114. public virtual void OnClicked ()
  115. {
  116. Clicked?.Invoke (this, EventArgs.Empty);
  117. }
  118. }
  119. }