FrameView.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. if (Border != null) {
  31. Border.Title = title;
  32. }
  33. SetNeedsDisplay ();
  34. }
  35. }
  36. /// <inheritdoc/>
  37. public override Border Border {
  38. get => base.Border;
  39. set {
  40. if (base.Border != null && base.Border.Child != null && value.Child == null) {
  41. value.Child = base.Border.Child;
  42. }
  43. base.Border = value;
  44. if (value == null) {
  45. return;
  46. }
  47. Rect frame;
  48. if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
  49. frame = Rect.Empty;
  50. } else {
  51. frame = Frame;
  52. }
  53. AdjustContentView (frame);
  54. Border.BorderChanged += Border_BorderChanged;
  55. }
  56. }
  57. void Border_BorderChanged (Border border)
  58. {
  59. Rect frame;
  60. if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
  61. frame = Rect.Empty;
  62. } else {
  63. frame = Frame;
  64. }
  65. AdjustContentView (frame);
  66. }
  67. /// <summary>
  68. /// ContentView is an internal implementation detail of Window. It is used to host Views added with <see cref="Add(View)"/>.
  69. /// Its ONLY reason for being is to provide a simple way for Window to expose to those SubViews that the Window's Bounds
  70. /// are actually deflated due to the border.
  71. /// </summary>
  72. class ContentView : View {
  73. public ContentView (Rect frame) : base (frame) { }
  74. public ContentView () : base () { }
  75. }
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  78. /// </summary>
  79. /// <param name="frame">Frame.</param>
  80. /// <param name="title">Title.</param>
  81. /// <param name="views">Views.</param>
  82. /// <param name="border">The <see cref="Border"/>.</param>
  83. public FrameView (Rect frame, ustring title = null, View [] views = null, Border border = null) : base (frame)
  84. {
  85. //var cFrame = new Rect (1, 1, Math.Max (frame.Width - 2, 0), Math.Max (frame.Height - 2, 0));
  86. Initialize (frame, title, views, border);
  87. }
  88. /// <summary>
  89. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  90. /// </summary>
  91. /// <param name="title">Title.</param>
  92. /// <param name="border">The <see cref="Border"/>.</param>
  93. public FrameView (ustring title, Border border = null)
  94. {
  95. Initialize (Rect.Empty, title, null, border);
  96. }
  97. /// <summary>
  98. /// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  99. /// </summary>
  100. public FrameView () : this (title: string.Empty) { }
  101. void Initialize (Rect frame, ustring title, View [] views = null, Border border = null)
  102. {
  103. if (title == null) title = ustring.Empty;
  104. this.Title = title;
  105. if (border == null) {
  106. Border = new Border () {
  107. BorderStyle = BorderStyle.Single,
  108. Title = title
  109. };
  110. } else {
  111. Border = border;
  112. if (ustring.IsNullOrEmpty (border.Title)) {
  113. border.Title = title;
  114. }
  115. }
  116. AdjustContentView (frame, views);
  117. }
  118. void AdjustContentView (Rect frame, View [] views = null)
  119. {
  120. var borderLength = Border.DrawMarginFrame ? 1 : 0;
  121. var sumPadding = Border.GetSumThickness ();
  122. var wp = new Point ();
  123. var wb = new Size ();
  124. if (frame == Rect.Empty) {
  125. wp.X = borderLength + sumPadding.Left;
  126. wp.Y = borderLength + sumPadding.Top;
  127. wb.Width = borderLength + sumPadding.Right;
  128. wb.Height = borderLength + sumPadding.Bottom;
  129. if (contentView == null) {
  130. contentView = new ContentView () {
  131. X = wp.X,
  132. Y = wp.Y,
  133. Width = Dim.Fill (wb.Width),
  134. Height = Dim.Fill (wb.Height)
  135. };
  136. } else {
  137. contentView.X = wp.X;
  138. contentView.Y = wp.Y;
  139. contentView.Width = Dim.Fill (wb.Width);
  140. contentView.Height = Dim.Fill (wb.Height);
  141. }
  142. } else {
  143. wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  144. wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  145. var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  146. if (contentView == null) {
  147. contentView = new ContentView (cFrame);
  148. } else {
  149. contentView.Frame = cFrame;
  150. }
  151. }
  152. if (views != null) {
  153. foreach (var view in views) {
  154. contentView.Add (view);
  155. }
  156. }
  157. if (Subviews?.Count == 0) {
  158. base.Add (contentView);
  159. contentView.Text = base.Text;
  160. }
  161. Border.Child = contentView;
  162. }
  163. void DrawFrame ()
  164. {
  165. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  166. }
  167. /// <summary>
  168. /// Add the specified <see cref="View"/> to this container.
  169. /// </summary>
  170. /// <param name="view"><see cref="View"/> to add to this container</param>
  171. public override void Add (View view)
  172. {
  173. contentView.Add (view);
  174. if (view.CanFocus)
  175. CanFocus = true;
  176. }
  177. /// <summary>
  178. /// Removes a <see cref="View"/> from this container.
  179. /// </summary>
  180. /// <remarks>
  181. /// </remarks>
  182. public override void Remove (View view)
  183. {
  184. if (view == null)
  185. return;
  186. SetNeedsDisplay ();
  187. var touched = view.Frame;
  188. contentView.Remove (view);
  189. if (contentView.InternalSubviews.Count < 1)
  190. this.CanFocus = false;
  191. }
  192. /// <summary>
  193. /// Removes all <see cref="View"/>s from this container.
  194. /// </summary>
  195. /// <remarks>
  196. /// </remarks>
  197. public override void RemoveAll ()
  198. {
  199. contentView.RemoveAll ();
  200. }
  201. ///<inheritdoc/>
  202. public override void Redraw (Rect bounds)
  203. {
  204. if (!NeedDisplay.IsEmpty) {
  205. Driver.SetAttribute (GetNormalColor ());
  206. Clear ();
  207. }
  208. var savedClip = contentView.ClipToBounds ();
  209. contentView.Redraw (!NeedDisplay.IsEmpty ? contentView.Bounds : bounds);
  210. Driver.Clip = savedClip;
  211. ClearLayoutNeeded ();
  212. ClearNeedsDisplay ();
  213. Driver.SetAttribute (GetNormalColor ());
  214. Border.DrawContent (this, false);
  215. }
  216. /// <summary>
  217. /// The text displayed by the <see cref="Label"/>.
  218. /// </summary>
  219. public override ustring Text {
  220. get => contentView?.Text;
  221. set {
  222. base.Text = value;
  223. if (contentView != null) {
  224. contentView.Text = value;
  225. }
  226. }
  227. }
  228. /// <summary>
  229. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  230. /// </summary>
  231. /// <value>The text alignment.</value>
  232. public override TextAlignment TextAlignment {
  233. get => contentView.TextAlignment;
  234. set {
  235. base.TextAlignment = contentView.TextAlignment = value;
  236. }
  237. }
  238. ///<inheritdoc/>
  239. public override bool OnEnter (View view)
  240. {
  241. if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
  242. Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  243. }
  244. return base.OnEnter (view);
  245. }
  246. /// <inheritdoc/>
  247. public override void OnCanFocusChanged ()
  248. {
  249. if (contentView != null) {
  250. contentView.CanFocus = CanFocus;
  251. }
  252. base.OnCanFocusChanged ();
  253. }
  254. }
  255. }