Label.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // Label.cs: Label control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text.RegularExpressions;
  11. using NStack;
  12. namespace Terminal.Gui {
  13. /// <summary>
  14. /// The Label <see cref="View"/> displays a string at a given position and supports multiple lines separted by newline characters. Multi-line Labels support word wrap.
  15. /// </summary>
  16. /// <remarks>
  17. /// The <see cref="Label"/> view is functionality identical to <see cref="View"/> and is included for API backwards compatibility.
  18. /// </remarks>
  19. public class Label : View {
  20. /// <inheritdoc/>
  21. public Label ()
  22. {
  23. }
  24. /// <inheritdoc/>
  25. public Label (Rect frame) : base (frame)
  26. {
  27. }
  28. /// <inheritdoc/>
  29. public Label (ustring text) : base (text)
  30. {
  31. }
  32. /// <inheritdoc/>
  33. public Label (Rect rect, ustring text) : base (rect, text)
  34. {
  35. }
  36. /// <inheritdoc/>
  37. public Label (int x, int y, ustring text) : base (x, y, text)
  38. {
  39. }
  40. /// <summary>
  41. /// Clicked <see cref="Action"/>, raised when the user clicks the primary mouse button within the Bounds of this <see cref="View"/>
  42. /// or if the user presses the action key while this view is focused. (TODO: IsDefault)
  43. /// </summary>
  44. /// <remarks>
  45. /// Client code can hook up to this event, it is
  46. /// raised when the button is activated either with
  47. /// the mouse or the keyboard.
  48. /// </remarks>
  49. public event Action Clicked;
  50. ///// <inheritdoc/>
  51. //public new ustring Text {
  52. // get => base.Text;
  53. // set {
  54. // base.Text = value;
  55. // // This supports Label auto-sizing when Text changes (preserving backwards compat behavior)
  56. // if (Frame.Height == 1 && !ustring.IsNullOrEmpty (value)) {
  57. // int w = Text.RuneCount;
  58. // Width = w;
  59. // Frame = new Rect (Frame.Location, new Size (w, Frame.Height));
  60. // }
  61. // SetNeedsDisplay ();
  62. // }
  63. //}
  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. MouseEventArgs args = new MouseEventArgs (mouseEvent);
  72. OnMouseClick (args);
  73. if (args.Handled)
  74. return true;
  75. if (MouseEvent (mouseEvent))
  76. return true;
  77. if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
  78. if (!HasFocus && SuperView != null) {
  79. SetFocus ();
  80. SetNeedsDisplay ();
  81. }
  82. Clicked?.Invoke ();
  83. return true;
  84. }
  85. return false;
  86. }
  87. }
  88. }