Frame.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Xml.Linq;
  7. using static Terminal.Gui.TileView;
  8. namespace Terminal.Gui {
  9. // TODO: v2 - Missing 3D effect - 3D effects will be drawn by a mechanism separate from Frames
  10. // TODO: v2 - If a Frame has focus, navigation keys (e.g Command.NextView) should cycle through SubViews of the Frame
  11. // QUESTION: How does a user navigate out of a Frame to another Frame, or back into the Parent's SubViews?
  12. /// <summary>
  13. /// Frames are a special form of <see cref="View"/> that act as adornments; they appear outside of the <see cref="View.Bounds"/>
  14. /// enabling borders, menus, etc...
  15. /// </summary>
  16. public class Frame : View {
  17. private Thickness _thickness = Thickness.Empty;
  18. internal override void CreateFrames () { /* Do nothing - Frames do not have Frames */ }
  19. internal override void LayoutFrames () { /* Do nothing - Frames do not have Frames */ }
  20. /// <summary>
  21. /// The Parent of this Frame (the View this Frame surrounds).
  22. /// </summary>
  23. public View Parent { get; set; }
  24. /// <summary>
  25. /// Frames cannot be used as sub-views, so this method always throws an <see cref="InvalidOperationException"/>.
  26. /// TODO: Are we sure?
  27. /// </summary>
  28. public override View SuperView {
  29. get {
  30. return null;
  31. }
  32. set {
  33. throw new NotImplementedException ();
  34. }
  35. }
  36. /// <inheritdoc/>
  37. public override void ViewToScreen (int col, int row, out int rcol, out int rrow, bool clipped = true)
  38. {
  39. // Frames are *Children* of a View, not SubViews. Thus View.ViewToScreen will not work.
  40. // To get the screen-relative coordinates of a Frame, we need to know who
  41. // the Parent is
  42. var parentFrame = Parent?.Frame ?? Frame;
  43. rrow = row + parentFrame.Y;
  44. rcol = col + parentFrame.X;
  45. // We now have rcol/rrow in coordinates relative to our View's SuperView. If our View's SuperView has
  46. // a SuperView, keep going...
  47. Parent?.SuperView?.ViewToScreen (rcol, rrow, out rcol, out rrow, clipped);
  48. }
  49. /// <summary>
  50. /// Frames only render to their Parent or Parent's SuperView's LineCanvas,
  51. /// so this always throws an <see cref="InvalidOperationException"/>.
  52. /// </summary>
  53. public override LineCanvas LineCanvas {
  54. get {
  55. throw new NotImplementedException ();
  56. }
  57. set {
  58. throw new NotImplementedException ();
  59. }
  60. }
  61. /// <summary>
  62. /// Does nothing for Frame
  63. /// </summary>
  64. /// <returns></returns>
  65. public override bool OnDrawFrames () => false;
  66. /// <summary>
  67. /// Frames only render to their Parent or Parent's SuperView's LineCanvas,
  68. /// so this always throws an <see cref="InvalidOperationException"/>.
  69. /// </summary>
  70. public override bool SuperViewRendersLineCanvas {
  71. get {
  72. return false;// throw new NotImplementedException ();
  73. }
  74. set {
  75. throw new NotImplementedException ();
  76. }
  77. }
  78. /// <summary>
  79. ///
  80. /// </summary>
  81. /// <param name="clipRect"></param>
  82. public virtual void OnDrawSubViews (Rect clipRect)
  83. {
  84. // TODO: Enable subviews of Frames (adornments).
  85. // if (Subviews == null) {
  86. // return;
  87. // }
  88. // foreach (var view in Subviews) {
  89. // // BUGBUG: v2 - shouldn't this be !view.LayoutNeeded? Why draw if layout is going to happen and we'll just draw again?
  90. // if (view.LayoutNeeded) {
  91. // view.LayoutSubviews ();
  92. // }
  93. // if ((view.Visible && !view.NeedDisplay.IsEmpty && view.Frame.Width > 0 && view.Frame.Height > 0) || view.ChildNeedsDisplay) {
  94. // view.Redraw (view.Bounds);
  95. // view.NeedDisplay = Rect.Empty;
  96. // // BUGBUG - v2 why does this need to be set to false?
  97. // // Shouldn't it be set when the subviews draw?
  98. // view.ChildNeedsDisplay = false;
  99. // }
  100. // }
  101. }
  102. /// <summary>
  103. /// Redraws the Frames that comprise the <see cref="Frame"/>.
  104. /// </summary>
  105. /// <param name="bounds"></param>
  106. public override void Redraw (Rect bounds)
  107. {
  108. if (Thickness == Thickness.Empty) return;
  109. if (ColorScheme != null) {
  110. Driver.SetAttribute (ColorScheme.Normal);
  111. } else {
  112. Driver.SetAttribute (Parent.GetNormalColor ());
  113. }
  114. //Driver.SetAttribute (Colors.Error.Normal);
  115. var prevClip = SetClip (Frame);
  116. var screenBounds = ViewToScreen (Frame);
  117. // This just draws/clears the thickness, not the insides.
  118. Thickness.Draw (screenBounds, (string)(Data != null ? Data : string.Empty));
  119. //OnDrawSubviews (bounds);
  120. // TODO: v2 - this will eventually be two controls: "BorderView" and "Label" (for the title)
  121. // The border frame (and title) are drawn at the outermost edge of border;
  122. // For Border
  123. // ...thickness extends outward (border/title is always as far in as possible)
  124. var borderBounds = new Rect (
  125. screenBounds.X + Math.Max (0, Thickness.Left - 1),
  126. screenBounds.Y + Math.Max (0, Thickness.Top - 1),
  127. Math.Max (0, screenBounds.Width - Math.Max (0, Math.Max (0, Thickness.Left - 1) + Math.Max (0, Thickness.Right - 1))),
  128. Math.Max (0, screenBounds.Height - Math.Max (0, Math.Max (0, Thickness.Top - 1) + Math.Max (0, Thickness.Bottom - 1))));
  129. var topTitleLineY = borderBounds.Y;
  130. var titleY = borderBounds.Y;
  131. var titleBarsLength = 0; // the little vertical thingies
  132. var maxTitleWidth = Math.Min (Parent.Title.ConsoleWidth, screenBounds.Width - 4);
  133. var sideLineLength = borderBounds.Height;
  134. if (Thickness.Top == 2) {
  135. topTitleLineY = borderBounds.Y - 1;
  136. titleY = topTitleLineY + 1;
  137. titleBarsLength = 2;
  138. }
  139. // ┌────┐
  140. //┌┘View└
  141. //│
  142. if (Thickness.Top == 3) {
  143. topTitleLineY = borderBounds.Y - (Thickness.Top - 1);
  144. titleY = topTitleLineY + 1;
  145. titleBarsLength = 3;
  146. sideLineLength++;
  147. }
  148. // ┌────┐
  149. //┌┘View└
  150. //│
  151. if (Thickness.Top > 3) {
  152. topTitleLineY = borderBounds.Y - 2;
  153. titleY = topTitleLineY + 1;
  154. titleBarsLength = 3;
  155. sideLineLength++;
  156. }
  157. if (Id == "Border" && Thickness.Top > 0 && maxTitleWidth > 0 && !ustring.IsNullOrEmpty (Parent?.Title)) {
  158. var prevAttr = Driver.GetAttribute ();
  159. Driver.SetAttribute (Parent.HasFocus ? Parent.GetHotNormalColor () : Parent.GetNormalColor ());
  160. DrawTitle (new Rect (borderBounds.X, titleY, Math.Min (borderBounds.Width - 4, Parent.Title.ConsoleWidth), 1), Parent?.Title);
  161. Driver.SetAttribute (prevAttr);
  162. }
  163. if (Id == "Border" && BorderStyle != LineStyle.None) {
  164. LineCanvas lc = Parent?.LineCanvas;
  165. var drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height > 1;
  166. var drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  167. var drawBottom = Thickness.Bottom > 0 && Frame.Width > 1;
  168. var drawRight = Thickness.Right > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  169. if (drawTop) {
  170. // ╔╡Title╞═════╗
  171. // ╔╡╞═════╗
  172. if (Frame.Width < 4 || ustring.IsNullOrEmpty (Parent?.Title)) {
  173. // ╔╡╞╗ should be ╔══╗
  174. lc.AddLine (new Point (borderBounds.Location.X, titleY), borderBounds.Width, Orientation.Horizontal, BorderStyle);
  175. } else {
  176. // ┌────┐
  177. //┌┘View└
  178. //│
  179. if (Thickness.Top == 2) {
  180. lc.AddLine (new Point (borderBounds.X + 1, topTitleLineY), Math.Min (borderBounds.Width - 2, maxTitleWidth + 2), Orientation.Horizontal, BorderStyle);
  181. }
  182. // ┌────┐
  183. //┌┘View└
  184. //│
  185. if (Thickness.Top > 2) {
  186. lc.AddLine (new Point (borderBounds.X + 1, topTitleLineY), Math.Min (borderBounds.Width - 2, maxTitleWidth + 2), Orientation.Horizontal, BorderStyle);
  187. lc.AddLine (new Point (borderBounds.X + 1, topTitleLineY + 2), Math.Min (borderBounds.Width - 2, maxTitleWidth + 2), Orientation.Horizontal, BorderStyle);
  188. }
  189. // ╔╡Title╞═════╗
  190. // Add a short horiz line for ╔╡
  191. lc.AddLine (new Point (borderBounds.Location.X, titleY), 2, Orientation.Horizontal, BorderStyle);
  192. // Add a vert line for ╔╡
  193. lc.AddLine (new Point (borderBounds.X + 1, topTitleLineY), titleBarsLength, Orientation.Vertical, LineStyle.Single);
  194. // Add a vert line for ╞
  195. lc.AddLine (new Point (borderBounds.X + 1 + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2) - 1, topTitleLineY), titleBarsLength, Orientation.Vertical, LineStyle.Single);
  196. // Add the right hand line for ╞═════╗
  197. lc.AddLine (new Point (borderBounds.X + 1 + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2) - 1, titleY), borderBounds.Width - Math.Min (borderBounds.Width - 2, maxTitleWidth + 2), Orientation.Horizontal, BorderStyle);
  198. }
  199. }
  200. if (drawLeft) {
  201. lc.AddLine (new Point (borderBounds.Location.X, titleY), sideLineLength, Orientation.Vertical, BorderStyle);
  202. }
  203. if (drawBottom) {
  204. lc.AddLine (new Point (borderBounds.X, borderBounds.Y + borderBounds.Height - 1), borderBounds.Width, Orientation.Horizontal, BorderStyle);
  205. }
  206. if (drawRight) {
  207. lc.AddLine (new Point (borderBounds.X + borderBounds.Width - 1, titleY), sideLineLength, Orientation.Vertical, BorderStyle);
  208. }
  209. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  210. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
  211. // Top
  212. var hruler = new Ruler () { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  213. if (drawTop) {
  214. hruler.Draw (new Point (screenBounds.X, screenBounds.Y));
  215. }
  216. // Redraw title
  217. if (drawTop && Id == "Border" && maxTitleWidth > 0 && !ustring.IsNullOrEmpty (Parent?.Title)) {
  218. var prevAttr = Driver.GetAttribute ();
  219. Driver.SetAttribute (Parent.HasFocus ? Parent.GetHotNormalColor () : Parent.GetNormalColor ());
  220. DrawTitle (new Rect (borderBounds.X, titleY, Parent.Title.ConsoleWidth, 1), Parent?.Title);
  221. Driver.SetAttribute (prevAttr);
  222. }
  223. //Left
  224. var vruler = new Ruler () { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  225. if (drawLeft) {
  226. vruler.Draw (new Point (screenBounds.X, screenBounds.Y + 1), 1);
  227. }
  228. // Bottom
  229. if (drawBottom) {
  230. hruler.Draw (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  231. }
  232. // Right
  233. if (drawRight) {
  234. vruler.Draw (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  235. }
  236. }
  237. }
  238. Driver.Clip = prevClip;
  239. }
  240. // TODO: v2 - Frame.BorderStyle is temporary - Eventually the border will be drawn by a "BorderView" that is a subview of the Frame.
  241. /// <summary>
  242. ///
  243. /// </summary>
  244. public new LineStyle BorderStyle { get; set; } = LineStyle.None;
  245. /// <summary>
  246. /// Defines the rectangle that the <see cref="Frame"/> will use to draw its content.
  247. /// </summary>
  248. public Thickness Thickness {
  249. get { return _thickness; }
  250. set {
  251. var prev = _thickness;
  252. _thickness = value;
  253. if (prev != _thickness) {
  254. Parent?.LayoutFrames ();
  255. OnThicknessChanged (prev);
  256. }
  257. }
  258. }
  259. /// <summary>
  260. /// Called whenever the <see cref="Thickness"/> property changes.
  261. /// </summary>
  262. public virtual void OnThicknessChanged (Thickness previousThickness)
  263. {
  264. ThicknessChanged?.Invoke (this, new ThicknessEventArgs () { Thickness = Thickness, PreviousThickness = previousThickness });
  265. }
  266. /// <summary>
  267. /// Fired whenever the <see cref="Thickness"/> property changes.
  268. /// </summary>
  269. public event EventHandler<ThicknessEventArgs> ThicknessChanged;
  270. /// <summary>
  271. /// Gets the rectangle that describes the inner area of the frame. The Location is always (0,0).
  272. /// </summary>
  273. public override Rect Bounds {
  274. get {
  275. return Thickness?.GetInside (new Rect (Point.Empty, Frame.Size)) ?? new Rect (Point.Empty, Frame.Size);
  276. }
  277. set {
  278. throw new InvalidOperationException ("It makes no sense to set Bounds of a Thickness.");
  279. }
  280. }
  281. /// <summary>
  282. /// Draws the title for a Window-style view.
  283. /// </summary>
  284. /// <param name="region">Screen relative region where the title will be drawn.</param>
  285. /// <param name="title">The title.</param>
  286. public void DrawTitle (Rect region, ustring title)
  287. {
  288. var width = region.Width;
  289. if (!ustring.IsNullOrEmpty (title)) {
  290. Driver.Move (region.X + 2, region.Y);
  291. //Driver.AddRune (' ');
  292. var str = title.Sum (r => Math.Max (Rune.ColumnWidth (r), 1)) >= width
  293. ? TextFormatter.Format (title, width, false, false) [0] : title;
  294. Driver.AddStr (str);
  295. }
  296. }
  297. /// <summary>
  298. /// Draws a frame in the current view, clipped by the boundary of this view
  299. /// </summary>
  300. /// <param name="region">View-relative region for the frame to be drawn.</param>
  301. /// <param name="clear">If set to <see langword="true"/> it clear the region.</param>
  302. [ObsoleteAttribute ("This method is obsolete in v2. Use use LineCanvas or Frame instead.", false)]
  303. public void DrawFrame (Rect region, bool clear)
  304. {
  305. var savedClip = ClipToBounds ();
  306. var screenBounds = ViewToScreen (region);
  307. if (clear) {
  308. Driver.FillRect (region);
  309. }
  310. var lc = new LineCanvas ();
  311. var drawTop = region.Width > 1 && region.Height > 1;
  312. var drawLeft = region.Width > 1 && region.Height > 1;
  313. var drawBottom = region.Width > 1 && region.Height > 1;
  314. var drawRight = region.Width > 1 && region.Height > 1;
  315. if (drawTop) {
  316. lc.AddLine (screenBounds.Location, screenBounds.Width, Orientation.Horizontal, BorderStyle);
  317. }
  318. if (drawLeft) {
  319. lc.AddLine (screenBounds.Location, screenBounds.Height, Orientation.Vertical, BorderStyle);
  320. }
  321. if (drawBottom) {
  322. lc.AddLine (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1), screenBounds.Width, Orientation.Horizontal, BorderStyle);
  323. }
  324. if (drawRight) {
  325. lc.AddLine (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y), screenBounds.Height, Orientation.Vertical, BorderStyle);
  326. }
  327. foreach (var p in lc.GetMap ()) {
  328. Driver.Move (p.Key.X, p.Key.Y);
  329. Driver.AddRune (p.Value);
  330. }
  331. lc.Clear ();
  332. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  333. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
  334. // Top
  335. var hruler = new Ruler () { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  336. if (drawTop) {
  337. hruler.Draw (new Point (screenBounds.X, screenBounds.Y));
  338. }
  339. //Left
  340. var vruler = new Ruler () { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  341. if (drawLeft) {
  342. vruler.Draw (new Point (screenBounds.X, screenBounds.Y + 1), 1);
  343. }
  344. // Bottom
  345. if (drawBottom) {
  346. hruler.Draw (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  347. }
  348. // Right
  349. if (drawRight) {
  350. vruler.Draw (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  351. }
  352. }
  353. Driver.Clip = savedClip;
  354. }
  355. }
  356. }