FrameView.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // FrameView.cs: Frame control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using NStack;
  11. namespace Terminal.Gui {
  12. /// <summary>
  13. /// The FrameView is a container frame that draws a frame around the contents
  14. /// </summary>
  15. public class FrameView : View {
  16. View contentView;
  17. ustring title;
  18. /// <summary>
  19. /// The title to be displayed for this window.
  20. /// </summary>
  21. /// <value>The title.</value>
  22. public ustring Title {
  23. get => title;
  24. set {
  25. title = value;
  26. SetNeedsDisplay ();
  27. }
  28. }
  29. class ContentView : View {
  30. public ContentView (Rect frame) : base (frame) { }
  31. public ContentView () : base () { }
  32. }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="T:Terminal.Gui.Gui.FrameView"/> class with
  35. /// an absolute position and a title.
  36. /// </summary>
  37. /// <param name="frame">Frame.</param>
  38. /// <param name="title">Title.</param>
  39. public FrameView (Rect frame, ustring title) : base (frame)
  40. {
  41. var cFrame = new Rect (1, 1 , frame.Width - 2, frame.Height - 2);
  42. contentView = new ContentView (cFrame);
  43. base.Add (contentView);
  44. Title = title;
  45. }
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="T:Terminal.Gui.Gui.FrameView"/> class with
  48. /// a title and the result is suitable to have its X, Y, Width and Height properties computed.
  49. /// </summary>
  50. /// <param name="title">Title.</param>
  51. public FrameView (ustring title)
  52. {
  53. contentView = new ContentView () {
  54. X = 1,
  55. Y = 1,
  56. Width = Dim.Fill (2),
  57. Height = Dim.Fill (2)
  58. };
  59. base.Add (contentView);
  60. Title = title;
  61. }
  62. void DrawFrame ()
  63. {
  64. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  65. }
  66. /// <summary>
  67. /// Add the specified view to the ContentView.
  68. /// </summary>
  69. /// <param name="view">View to add to the window.</param>
  70. public override void Add (View view)
  71. {
  72. contentView.Add (view);
  73. if (view.CanFocus)
  74. CanFocus = true;
  75. }
  76. /// <summary>
  77. /// Removes a widget from this container.
  78. /// </summary>
  79. /// <remarks>
  80. /// </remarks>
  81. public override void Remove (View view)
  82. {
  83. if (view == null)
  84. return;
  85. SetNeedsDisplay ();
  86. var touched = view.Frame;
  87. contentView.Remove (view);
  88. if (contentView.InternalSubviews.Count < 1)
  89. this.CanFocus = false;
  90. }
  91. /// <summary>
  92. /// Removes all widgets from this container.
  93. /// </summary>
  94. /// <remarks>
  95. /// </remarks>
  96. public override void RemoveAll()
  97. {
  98. contentView.RemoveAll();
  99. }
  100. public override void Redraw (Rect bounds)
  101. {
  102. if (!NeedDisplay.IsEmpty) {
  103. Driver.SetAttribute (ColorScheme.Normal);
  104. DrawFrame ();
  105. if (HasFocus)
  106. Driver.SetAttribute (ColorScheme.Normal);
  107. var width = Frame.Width;
  108. if (Title != null && width > 4) {
  109. Move (1, 0);
  110. Driver.AddRune (' ');
  111. var str = Title.Length > width ? Title [0, width - 4] : Title;
  112. Driver.AddStr (str);
  113. Driver.AddRune (' ');
  114. }
  115. Driver.SetAttribute (ColorScheme.Normal);
  116. }
  117. contentView.Redraw (contentView.Bounds);
  118. ClearNeedsDisplay ();
  119. }
  120. }
  121. }