FrameView.cs 8.0 KB

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