FrameView.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. this.title = title;
  43. contentView = new ContentView (cFrame);
  44. Initialize ();
  45. }
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="T:Terminal.Gui.Gui.FrameView"/> class with
  48. /// an absolute position, a title and views.
  49. /// </summary>
  50. /// <param name="frame">Frame.</param>
  51. /// <param name="title">Title.</param>
  52. /// /// <param name="views">Views.</param>
  53. public FrameView (Rect frame, ustring title, View[] views) : this (frame, title)
  54. {
  55. foreach (var view in views) {
  56. contentView.Add (view);
  57. }
  58. Initialize ();
  59. }
  60. /// <summary>
  61. /// Initializes a new instance of the <see cref="T:Terminal.Gui.Gui.FrameView"/> class with
  62. /// a title and the result is suitable to have its X, Y, Width and Height properties computed.
  63. /// </summary>
  64. /// <param name="title">Title.</param>
  65. public FrameView (ustring title)
  66. {
  67. this.title = title;
  68. contentView = new ContentView () {
  69. X = 1,
  70. Y = 1,
  71. Width = Dim.Fill (2),
  72. Height = Dim.Fill (2)
  73. };
  74. Initialize ();
  75. }
  76. void Initialize ()
  77. {
  78. base.Add (contentView);
  79. }
  80. void DrawFrame ()
  81. {
  82. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  83. }
  84. /// <summary>
  85. /// Add the specified view to the ContentView.
  86. /// </summary>
  87. /// <param name="view">View to add to the window.</param>
  88. public override void Add (View view)
  89. {
  90. contentView.Add (view);
  91. if (view.CanFocus)
  92. CanFocus = true;
  93. }
  94. /// <summary>
  95. /// Removes a widget from this container.
  96. /// </summary>
  97. /// <remarks>
  98. /// </remarks>
  99. public override void Remove (View view)
  100. {
  101. if (view == null)
  102. return;
  103. SetNeedsDisplay ();
  104. var touched = view.Frame;
  105. contentView.Remove (view);
  106. if (contentView.InternalSubviews.Count < 1)
  107. this.CanFocus = false;
  108. }
  109. /// <summary>
  110. /// Removes all widgets from this container.
  111. /// </summary>
  112. /// <remarks>
  113. /// </remarks>
  114. public override void RemoveAll()
  115. {
  116. contentView.RemoveAll();
  117. }
  118. public override void Redraw (Rect bounds)
  119. {
  120. if (!NeedDisplay.IsEmpty) {
  121. Driver.SetAttribute (ColorScheme.Normal);
  122. DrawFrame ();
  123. if (HasFocus)
  124. Driver.SetAttribute (ColorScheme.Normal);
  125. var width = Frame.Width;
  126. if (Title != null && width > 4) {
  127. Move (1, 0);
  128. Driver.AddRune (' ');
  129. var str = Title.Length > width ? Title [0, width - 4] : Title;
  130. Driver.AddStr (str);
  131. Driver.AddRune (' ');
  132. }
  133. Driver.SetAttribute (ColorScheme.Normal);
  134. }
  135. contentView.Redraw (contentView.Bounds);
  136. ClearNeedsDisplay ();
  137. }
  138. }
  139. }