FrameView.cs 7.6 KB

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