FrameView.cs 10 KB

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