FrameView.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // Authors:
  3. // Miguel de Icaza ([email protected])
  4. //
  5. // NOTE: FrameView is functionally identical to Window with the following exceptions.
  6. // - Is not a Toplevel
  7. // - Does not support mouse dragging
  8. // - Does not support padding (but should)
  9. // - Does not support IEnumerable
  10. // Any udpates done here should probably be done in Window as well; TODO: Merge these classes
  11. using System;
  12. using System.Linq;
  13. using NStack;
  14. namespace Terminal.Gui {
  15. /// <summary>
  16. /// The FrameView is a container frame that draws a frame around the contents. It is similar to
  17. /// a GroupBox in Windows.
  18. /// </summary>
  19. public class FrameView : View {
  20. View contentView;
  21. ustring title;
  22. /// <summary>
  23. /// The title to be displayed for this <see cref="FrameView"/>.
  24. /// </summary>
  25. /// <value>The title.</value>
  26. public ustring Title {
  27. get => title;
  28. set {
  29. title = value;
  30. SetNeedsDisplay ();
  31. }
  32. }
  33. /// <summary>
  34. /// ContentView is an internal implementation detail of Window. It is used to host Views added with <see cref="Add(View)"/>.
  35. /// Its ONLY reason for being is to provide a simple way for Window to expose to those SubViews that the Window's Bounds
  36. /// are actually deflated due to the border.
  37. /// </summary>
  38. class ContentView : View {
  39. public ContentView (Rect frame) : base (frame) { }
  40. public ContentView () : base () { }
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  44. /// </summary>
  45. /// <param name="frame">Frame.</param>
  46. /// <param name="title">Title.</param>
  47. public FrameView (Rect frame, ustring title = null) : base (frame)
  48. {
  49. var cFrame = new Rect (1, 1, Math.Max (frame.Width - 2, 0), Math.Max (frame.Height - 2, 0));
  50. Initialize (title, cFrame);
  51. }
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  54. /// </summary>
  55. /// <param name="frame">Frame.</param>
  56. /// <param name="title">Title.</param>
  57. /// /// <param name="views">Views.</param>
  58. public FrameView (Rect frame, ustring title, View [] views) : this (frame, title)
  59. {
  60. Initialize (title, frame, views);
  61. }
  62. /// <summary>
  63. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  64. /// </summary>
  65. /// <param name="title">Title.</param>
  66. public FrameView (ustring title)
  67. {
  68. Initialize (title, Rect.Empty);
  69. }
  70. /// <summary>
  71. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  72. /// </summary>
  73. public FrameView () : this (title: string.Empty) { }
  74. void Initialize (ustring title, Rect frame, View [] views = null)
  75. {
  76. this.title = title;
  77. if (frame == Rect.Empty) {
  78. const int wb = 1;
  79. contentView = new ContentView () {
  80. X = wb,
  81. Y = wb,
  82. Width = Dim.Fill (wb),
  83. Height = Dim.Fill (wb)
  84. };
  85. } else {
  86. contentView = new ContentView (frame);
  87. }
  88. if (views != null) {
  89. foreach (var view in views) {
  90. contentView.Add (view);
  91. }
  92. }
  93. if (Subviews?.Count == 0) {
  94. base.Add (contentView);
  95. contentView.Text = base.Text;
  96. }
  97. }
  98. void DrawFrame ()
  99. {
  100. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  101. }
  102. /// <summary>
  103. /// Add the specified <see cref="View"/> to this container.
  104. /// </summary>
  105. /// <param name="view"><see cref="View"/> to add to this container</param>
  106. public override void Add (View view)
  107. {
  108. contentView.Add (view);
  109. if (view.CanFocus)
  110. CanFocus = true;
  111. }
  112. /// <summary>
  113. /// Removes a <see cref="View"/> from this container.
  114. /// </summary>
  115. /// <remarks>
  116. /// </remarks>
  117. public override void Remove (View view)
  118. {
  119. if (view == null)
  120. return;
  121. SetNeedsDisplay ();
  122. var touched = view.Frame;
  123. contentView.Remove (view);
  124. if (contentView.InternalSubviews.Count < 1)
  125. this.CanFocus = false;
  126. }
  127. /// <summary>
  128. /// Removes all <see cref="View"/>s from this container.
  129. /// </summary>
  130. /// <remarks>
  131. /// </remarks>
  132. public override void RemoveAll ()
  133. {
  134. contentView.RemoveAll ();
  135. }
  136. ///<inheritdoc/>
  137. public override void Redraw (Rect bounds)
  138. {
  139. var padding = 0;
  140. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  141. if (!NeedDisplay.IsEmpty) {
  142. Driver.SetAttribute (ColorScheme.Normal);
  143. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  144. }
  145. var savedClip = ClipToBounds ();
  146. contentView.Redraw (contentView.Bounds);
  147. Driver.Clip = savedClip;
  148. ClearNeedsDisplay ();
  149. Driver.SetAttribute (ColorScheme.Normal);
  150. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  151. if (HasFocus)
  152. Driver.SetAttribute (ColorScheme.HotNormal);
  153. Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  154. Driver.SetAttribute (ColorScheme.Normal);
  155. }
  156. /// <summary>
  157. /// The text displayed by the <see cref="Label"/>.
  158. /// </summary>
  159. public override ustring Text {
  160. get => contentView.Text;
  161. set {
  162. base.Text = value;
  163. if (contentView != null) {
  164. contentView.Text = value;
  165. }
  166. }
  167. }
  168. /// <summary>
  169. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  170. /// </summary>
  171. /// <value>The text alignment.</value>
  172. public override TextAlignment TextAlignment {
  173. get => contentView.TextAlignment;
  174. set {
  175. base.TextAlignment = contentView.TextAlignment = value;
  176. }
  177. }
  178. ///<inheritdoc/>
  179. public override bool OnEnter (View view)
  180. {
  181. if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
  182. Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  183. }
  184. return base.OnEnter (view);
  185. }
  186. }
  187. }