FrameView.cs 8.1 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. 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 wb = new Size ();
  116. if (frame == Rect.Empty) {
  117. wb.Width = borderLength + sumPadding.Right;
  118. wb.Height = borderLength + sumPadding.Bottom;
  119. if (contentView == null) {
  120. contentView = new ContentView () {
  121. X = borderLength + sumPadding.Left,
  122. Y = borderLength + sumPadding.Top,
  123. Width = Dim.Fill (wb.Width),
  124. Height = Dim.Fill (wb.Height)
  125. };
  126. } else {
  127. contentView.X = borderLength + sumPadding.Left;
  128. contentView.Y = borderLength + sumPadding.Top;
  129. contentView.Width = Dim.Fill (wb.Width);
  130. contentView.Height = Dim.Fill (wb.Height);
  131. }
  132. } else {
  133. wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  134. wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  135. var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  136. if (contentView == null) {
  137. contentView = new ContentView (cFrame);
  138. } else {
  139. contentView.Frame = cFrame;
  140. }
  141. }
  142. if (views != null) {
  143. foreach (var view in views) {
  144. contentView.Add (view);
  145. }
  146. }
  147. if (Subviews?.Count == 0) {
  148. base.Add (contentView);
  149. contentView.Text = base.Text;
  150. }
  151. Border.Child = contentView;
  152. }
  153. void DrawFrame ()
  154. {
  155. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  156. }
  157. /// <summary>
  158. /// Add the specified <see cref="View"/> to this container.
  159. /// </summary>
  160. /// <param name="view"><see cref="View"/> to add to this container</param>
  161. public override void Add (View view)
  162. {
  163. contentView.Add (view);
  164. if (view.CanFocus)
  165. CanFocus = true;
  166. }
  167. /// <summary>
  168. /// Removes a <see cref="View"/> from this container.
  169. /// </summary>
  170. /// <remarks>
  171. /// </remarks>
  172. public override void Remove (View view)
  173. {
  174. if (view == null)
  175. return;
  176. SetNeedsDisplay ();
  177. var touched = view.Frame;
  178. contentView.Remove (view);
  179. if (contentView.InternalSubviews.Count < 1)
  180. this.CanFocus = false;
  181. }
  182. /// <summary>
  183. /// Removes all <see cref="View"/>s from this container.
  184. /// </summary>
  185. /// <remarks>
  186. /// </remarks>
  187. public override void RemoveAll ()
  188. {
  189. contentView.RemoveAll ();
  190. }
  191. ///<inheritdoc/>
  192. public override void Redraw (Rect bounds)
  193. {
  194. var padding = Border.GetSumThickness ();
  195. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  196. if (!NeedDisplay.IsEmpty) {
  197. Driver.SetAttribute (GetNormalColor ());
  198. //Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  199. Clear ();
  200. }
  201. var savedClip = contentView.ClipToBounds ();
  202. contentView.Redraw (!NeedDisplay.IsEmpty ? contentView.Bounds : bounds);
  203. Driver.Clip = savedClip;
  204. ClearNeedsDisplay ();
  205. if (Border.BorderStyle != BorderStyle.None) {
  206. Driver.SetAttribute (GetNormalColor ());
  207. //Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  208. Border.DrawContent (this, false);
  209. if (HasFocus)
  210. Driver.SetAttribute (ColorScheme.HotNormal);
  211. //Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  212. Driver.DrawWindowTitle (scrRect, Title, padding.Left, padding.Top, padding.Right, padding.Bottom);
  213. }
  214. Driver.SetAttribute (GetNormalColor ());
  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. }