FrameView.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. this.title = title;
  51. contentView = new ContentView (cFrame);
  52. Initialize ();
  53. }
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  56. /// </summary>
  57. /// <param name="frame">Frame.</param>
  58. /// <param name="title">Title.</param>
  59. /// /// <param name="views">Views.</param>
  60. public FrameView (Rect frame, ustring title, View [] views) : this (frame, title)
  61. {
  62. foreach (var view in views) {
  63. contentView.Add (view);
  64. }
  65. Initialize ();
  66. }
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  69. /// </summary>
  70. /// <param name="title">Title.</param>
  71. public FrameView (ustring title)
  72. {
  73. this.title = title;
  74. contentView = new ContentView () {
  75. X = 1,
  76. Y = 1,
  77. Width = Dim.Fill (1),
  78. Height = Dim.Fill (1)
  79. };
  80. Initialize ();
  81. }
  82. /// <summary>
  83. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  84. /// </summary>
  85. public FrameView () : this (title: string.Empty) { }
  86. void Initialize ()
  87. {
  88. if (Subviews?.Count == 0) {
  89. base.Add (contentView);
  90. contentView.Text = base.Text;
  91. }
  92. }
  93. void DrawFrame ()
  94. {
  95. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  96. }
  97. /// <summary>
  98. /// Add the specified <see cref="View"/> to this container.
  99. /// </summary>
  100. /// <param name="view"><see cref="View"/> to add to this container</param>
  101. public override void Add (View view)
  102. {
  103. contentView.Add (view);
  104. if (view.CanFocus)
  105. CanFocus = true;
  106. }
  107. /// <summary>
  108. /// Removes a <see cref="View"/> from this container.
  109. /// </summary>
  110. /// <remarks>
  111. /// </remarks>
  112. public override void Remove (View view)
  113. {
  114. if (view == null)
  115. return;
  116. SetNeedsDisplay ();
  117. var touched = view.Frame;
  118. contentView.Remove (view);
  119. if (contentView.InternalSubviews.Count < 1)
  120. this.CanFocus = false;
  121. }
  122. /// <summary>
  123. /// Removes all <see cref="View"/>s from this container.
  124. /// </summary>
  125. /// <remarks>
  126. /// </remarks>
  127. public override void RemoveAll ()
  128. {
  129. contentView.RemoveAll ();
  130. }
  131. ///<inheritdoc/>
  132. public override void Redraw (Rect bounds)
  133. {
  134. var padding = 0;
  135. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  136. if (!NeedDisplay.IsEmpty) {
  137. Driver.SetAttribute (ColorScheme.Normal);
  138. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  139. }
  140. var savedClip = ClipToBounds ();
  141. contentView.Redraw (contentView.Bounds);
  142. Driver.Clip = savedClip;
  143. ClearNeedsDisplay ();
  144. Driver.SetAttribute (ColorScheme.Normal);
  145. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  146. if (HasFocus)
  147. Driver.SetAttribute (ColorScheme.HotNormal);
  148. Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  149. Driver.SetAttribute (ColorScheme.Normal);
  150. }
  151. /// <summary>
  152. /// The text displayed by the <see cref="Label"/>.
  153. /// </summary>
  154. public override ustring Text {
  155. get => contentView.Text;
  156. set {
  157. base.Text = value;
  158. if (contentView != null) {
  159. contentView.Text = value;
  160. }
  161. }
  162. }
  163. /// <summary>
  164. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  165. /// </summary>
  166. /// <value>The text alignment.</value>
  167. public override TextAlignment TextAlignment {
  168. get => contentView.TextAlignment;
  169. set {
  170. base.TextAlignment = contentView.TextAlignment = value;
  171. }
  172. }
  173. ///<inheritdoc/>
  174. public override bool OnEnter (View view)
  175. {
  176. if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
  177. Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  178. }
  179. return base.OnEnter (view);
  180. }
  181. }
  182. }