FrameView.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. this.title = title;
  101. if (border == null) {
  102. Border = new Border () {
  103. BorderStyle = BorderStyle.Single
  104. };
  105. } else {
  106. Border = border;
  107. }
  108. AdjustContentView (frame, views);
  109. }
  110. void AdjustContentView (Rect frame, View [] views = null)
  111. {
  112. var borderLength = Border.DrawMarginFrame ? 1 : 0;
  113. var sumPadding = Border.GetSumThickness ();
  114. var wb = new Size ();
  115. if (frame == Rect.Empty) {
  116. wb.Width = borderLength + sumPadding.Right;
  117. wb.Height = borderLength + sumPadding.Bottom;
  118. if (contentView == null) {
  119. contentView = new ContentView () {
  120. X = borderLength + sumPadding.Left,
  121. Y = borderLength + sumPadding.Top,
  122. Width = Dim.Fill (wb.Width),
  123. Height = Dim.Fill (wb.Height)
  124. };
  125. } else {
  126. contentView.X = borderLength + sumPadding.Left;
  127. contentView.Y = borderLength + sumPadding.Top;
  128. contentView.Width = Dim.Fill (wb.Width);
  129. contentView.Height = Dim.Fill (wb.Height);
  130. }
  131. } else {
  132. wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  133. wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  134. var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  135. if (contentView == null) {
  136. contentView = new ContentView (cFrame);
  137. } else {
  138. contentView.Frame = cFrame;
  139. }
  140. }
  141. if (views != null) {
  142. foreach (var view in views) {
  143. contentView.Add (view);
  144. }
  145. }
  146. if (Subviews?.Count == 0) {
  147. base.Add (contentView);
  148. contentView.Text = base.Text;
  149. }
  150. Border.Child = contentView;
  151. }
  152. void DrawFrame ()
  153. {
  154. DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
  155. }
  156. /// <summary>
  157. /// Add the specified <see cref="View"/> to this container.
  158. /// </summary>
  159. /// <param name="view"><see cref="View"/> to add to this container</param>
  160. public override void Add (View view)
  161. {
  162. contentView.Add (view);
  163. if (view.CanFocus)
  164. CanFocus = true;
  165. }
  166. /// <summary>
  167. /// Removes a <see cref="View"/> from this container.
  168. /// </summary>
  169. /// <remarks>
  170. /// </remarks>
  171. public override void Remove (View view)
  172. {
  173. if (view == null)
  174. return;
  175. SetNeedsDisplay ();
  176. var touched = view.Frame;
  177. contentView.Remove (view);
  178. if (contentView.InternalSubviews.Count < 1)
  179. this.CanFocus = false;
  180. }
  181. /// <summary>
  182. /// Removes all <see cref="View"/>s from this container.
  183. /// </summary>
  184. /// <remarks>
  185. /// </remarks>
  186. public override void RemoveAll ()
  187. {
  188. contentView.RemoveAll ();
  189. }
  190. ///<inheritdoc/>
  191. public override void Redraw (Rect bounds)
  192. {
  193. var padding = Border.GetSumThickness ();
  194. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  195. if (!NeedDisplay.IsEmpty) {
  196. Driver.SetAttribute (GetNormalColor ());
  197. //Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  198. Clear ();
  199. }
  200. var savedClip = contentView.ClipToBounds ();
  201. contentView.Redraw (!NeedDisplay.IsEmpty ? contentView.Bounds : bounds);
  202. Driver.Clip = savedClip;
  203. ClearNeedsDisplay ();
  204. if (Border.BorderStyle != BorderStyle.None) {
  205. Driver.SetAttribute (GetNormalColor ());
  206. //Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  207. Border.DrawContent (this, false);
  208. if (HasFocus)
  209. Driver.SetAttribute (ColorScheme.HotNormal);
  210. //Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  211. Driver.DrawWindowTitle (scrRect, Title, padding.Left, padding.Top, padding.Right, padding.Bottom);
  212. }
  213. Driver.SetAttribute (GetNormalColor ());
  214. }
  215. /// <summary>
  216. /// The text displayed by the <see cref="Label"/>.
  217. /// </summary>
  218. public override ustring Text {
  219. get => contentView.Text;
  220. set {
  221. base.Text = value;
  222. if (contentView != null) {
  223. contentView.Text = value;
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  229. /// </summary>
  230. /// <value>The text alignment.</value>
  231. public override TextAlignment TextAlignment {
  232. get => contentView.TextAlignment;
  233. set {
  234. base.TextAlignment = contentView.TextAlignment = value;
  235. }
  236. }
  237. ///<inheritdoc/>
  238. public override bool OnEnter (View view)
  239. {
  240. if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
  241. Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  242. }
  243. return base.OnEnter (view);
  244. }
  245. /// <inheritdoc/>
  246. public override void OnCanFocusChanged ()
  247. {
  248. if (contentView != null) {
  249. contentView.CanFocus = CanFocus;
  250. }
  251. base.OnCanFocusChanged ();
  252. }
  253. }
  254. }