FrameView.cs 12 KB

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