Frame.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 (GetNormalColor ());
  111. } else {
  112. if (Id == "Padding") {
  113. Driver.SetAttribute (new Attribute (Parent.ColorScheme.HotNormal.Background, Parent.ColorScheme.HotNormal.Foreground));
  114. } else {
  115. Driver.SetAttribute (Parent.GetNormalColor ());
  116. }
  117. }
  118. //Driver.SetAttribute (Colors.Error.Normal);
  119. var prevClip = SetClip (Frame);
  120. var screenBounds = ViewToScreen (Frame);
  121. // This just draws/clears the thickness, not the insides.
  122. Thickness.Draw (screenBounds, (string)(Data != null ? Data : string.Empty));
  123. //OnDrawSubviews (bounds);
  124. // TODO: v2 - this will eventually be two controls: "BorderView" and "Label" (for the title)
  125. // The border frame (and title) are drawn at the outermost edge of border;
  126. // For Border
  127. // ...thickness extends outward (border/title is always as far in as possible)
  128. var borderBounds = new Rect (
  129. screenBounds.X + Math.Max (0, Thickness.Left - 1),
  130. screenBounds.Y + Math.Max (0, Thickness.Top - 1),
  131. Math.Max (0, screenBounds.Width - Math.Max (0, Math.Max (0, Thickness.Left - 1) + Math.Max (0, Thickness.Right - 1))),
  132. Math.Max (0, screenBounds.Height - Math.Max (0, Math.Max (0, Thickness.Top - 1) + Math.Max (0, Thickness.Bottom - 1))));
  133. var topTitleLineY = borderBounds.Y;
  134. var titleY = borderBounds.Y;
  135. var titleBarsLength = 0; // the little vertical thingies
  136. var maxTitleWidth = Math.Min (Parent.Title.ConsoleWidth, Math.Min (screenBounds.Width - 4, borderBounds.Width - 4));
  137. var sideLineLength = borderBounds.Height;
  138. var canDrawBorder = borderBounds.Width > 0 && borderBounds.Height > 0;
  139. if (!ustring.IsNullOrEmpty (Parent?.Title)) {
  140. if (Thickness.Top == 2) {
  141. topTitleLineY = borderBounds.Y - 1;
  142. titleY = topTitleLineY + 1;
  143. titleBarsLength = 2;
  144. }
  145. // ┌────┐
  146. //┌┘View└
  147. //│
  148. if (Thickness.Top == 3) {
  149. topTitleLineY = borderBounds.Y - (Thickness.Top - 1);
  150. titleY = topTitleLineY + 1;
  151. titleBarsLength = 3;
  152. sideLineLength++;
  153. }
  154. // ┌────┐
  155. //┌┘View└
  156. //│
  157. if (Thickness.Top > 3) {
  158. topTitleLineY = borderBounds.Y - 2;
  159. titleY = topTitleLineY + 1;
  160. titleBarsLength = 3;
  161. sideLineLength++;
  162. }
  163. }
  164. if (Id == "Border" && canDrawBorder && Thickness.Top > 0 && maxTitleWidth > 0 && !ustring.IsNullOrEmpty (Parent?.Title)) {
  165. var prevAttr = Driver.GetAttribute ();
  166. if (ColorScheme != null) {
  167. Driver.SetAttribute (HasFocus ? GetHotNormalColor () : GetNormalColor ());
  168. } else {
  169. Driver.SetAttribute (Parent.HasFocus ? Parent.GetHotNormalColor () : Parent.GetNormalColor ());
  170. }
  171. DrawTitle (new Rect (borderBounds.X, titleY, maxTitleWidth, 1), Parent?.Title);
  172. Driver.SetAttribute (prevAttr);
  173. }
  174. if (Id == "Border" && canDrawBorder && BorderStyle != LineStyle.None) {
  175. LineCanvas lc = Parent?.LineCanvas;
  176. var drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height > 1;
  177. var drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  178. var drawBottom = Thickness.Bottom > 0 && Frame.Width > 1;
  179. var drawRight = Thickness.Right > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  180. var prevAttr = Driver.GetAttribute ();
  181. if (ColorScheme != null) {
  182. Driver.SetAttribute (GetNormalColor ());
  183. } else {
  184. Driver.SetAttribute (Parent.GetNormalColor ());
  185. }
  186. if (drawTop) {
  187. // ╔╡Title╞═════╗
  188. // ╔╡╞═════╗
  189. if (borderBounds.Width < 4 || ustring.IsNullOrEmpty (Parent?.Title)) {
  190. // ╔╡╞╗ should be ╔══╗
  191. lc.AddLine (new Point (borderBounds.Location.X, titleY), borderBounds.Width, Orientation.Horizontal, BorderStyle, Driver.GetAttribute ());
  192. } else {
  193. // ┌────┐
  194. //┌┘View└
  195. //│
  196. if (Thickness.Top == 2) {
  197. lc.AddLine (new Point (borderBounds.X + 1, topTitleLineY), Math.Min (borderBounds.Width - 2, maxTitleWidth + 2), Orientation.Horizontal, BorderStyle, Driver.GetAttribute ());
  198. }
  199. // ┌────┐
  200. //┌┘View└
  201. //│
  202. if (borderBounds.Width >= 4 && Thickness.Top > 2) {
  203. lc.AddLine (new Point (borderBounds.X + 1, topTitleLineY), Math.Min (borderBounds.Width - 2, maxTitleWidth + 2), Orientation.Horizontal, BorderStyle, Driver.GetAttribute ());
  204. lc.AddLine (new Point (borderBounds.X + 1, topTitleLineY + 2), Math.Min (borderBounds.Width - 2, maxTitleWidth + 2), Orientation.Horizontal, BorderStyle, Driver.GetAttribute ());
  205. }
  206. // ╔╡Title╞═════╗
  207. // Add a short horiz line for ╔╡
  208. lc.AddLine (new Point (borderBounds.Location.X, titleY), 2, Orientation.Horizontal, BorderStyle, Driver.GetAttribute ());
  209. // Add a vert line for ╔╡
  210. lc.AddLine (new Point (borderBounds.X + 1, topTitleLineY), titleBarsLength, Orientation.Vertical, LineStyle.Single, Driver.GetAttribute ());
  211. // Add a vert line for ╞
  212. lc.AddLine (new Point (borderBounds.X + 1 + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2) - 1, topTitleLineY), titleBarsLength, Orientation.Vertical, LineStyle.Single, Driver.GetAttribute ());
  213. // Add the right hand line for ╞═════╗
  214. 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, Driver.GetAttribute ());
  215. }
  216. }
  217. if (drawLeft) {
  218. lc.AddLine (new Point (borderBounds.Location.X, titleY), sideLineLength, Orientation.Vertical, BorderStyle, Driver.GetAttribute ());
  219. }
  220. if (drawBottom) {
  221. lc.AddLine (new Point (borderBounds.X, borderBounds.Y + borderBounds.Height - 1), borderBounds.Width, Orientation.Horizontal, BorderStyle, Driver.GetAttribute ());
  222. }
  223. if (drawRight) {
  224. lc.AddLine (new Point (borderBounds.X + borderBounds.Width - 1, titleY), sideLineLength, Orientation.Vertical, BorderStyle, Driver.GetAttribute ());
  225. }
  226. Driver.SetAttribute (prevAttr);
  227. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  228. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
  229. // Top
  230. var hruler = new Ruler () { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  231. if (drawTop) {
  232. hruler.Draw (new Point (screenBounds.X, screenBounds.Y));
  233. }
  234. // Redraw title
  235. if (drawTop && Id == "Border" && maxTitleWidth > 0 && !ustring.IsNullOrEmpty (Parent?.Title)) {
  236. prevAttr = Driver.GetAttribute ();
  237. if (ColorScheme != null) {
  238. Driver.SetAttribute (HasFocus ? GetHotNormalColor () : GetNormalColor ());
  239. } else {
  240. Driver.SetAttribute (Parent.HasFocus ? Parent.GetHotNormalColor () : Parent.GetNormalColor ());
  241. }
  242. DrawTitle (new Rect (borderBounds.X, titleY, Parent.Title.ConsoleWidth, 1), Parent?.Title);
  243. Driver.SetAttribute (prevAttr);
  244. }
  245. //Left
  246. var vruler = new Ruler () { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  247. if (drawLeft) {
  248. vruler.Draw (new Point (screenBounds.X, screenBounds.Y + 1), 1);
  249. }
  250. // Bottom
  251. if (drawBottom) {
  252. hruler.Draw (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  253. }
  254. // Right
  255. if (drawRight) {
  256. vruler.Draw (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  257. }
  258. }
  259. }
  260. Driver.Clip = prevClip;
  261. }
  262. // TODO: v2 - Frame.BorderStyle is temporary - Eventually the border will be drawn by a "BorderView" that is a subview of the Frame.
  263. /// <summary>
  264. ///
  265. /// </summary>
  266. public new LineStyle BorderStyle { get; set; } = LineStyle.None;
  267. /// <summary>
  268. /// Defines the rectangle that the <see cref="Frame"/> will use to draw its content.
  269. /// </summary>
  270. public Thickness Thickness {
  271. get { return _thickness; }
  272. set {
  273. var prev = _thickness;
  274. _thickness = value;
  275. if (prev != _thickness) {
  276. Parent?.LayoutFrames ();
  277. OnThicknessChanged (prev);
  278. }
  279. }
  280. }
  281. /// <summary>
  282. /// Called whenever the <see cref="Thickness"/> property changes.
  283. /// </summary>
  284. public virtual void OnThicknessChanged (Thickness previousThickness)
  285. {
  286. ThicknessChanged?.Invoke (this, new ThicknessEventArgs () { Thickness = Thickness, PreviousThickness = previousThickness });
  287. }
  288. /// <summary>
  289. /// Fired whenever the <see cref="Thickness"/> property changes.
  290. /// </summary>
  291. public event EventHandler<ThicknessEventArgs> ThicknessChanged;
  292. /// <summary>
  293. /// Gets the rectangle that describes the inner area of the frame. The Location is always (0,0).
  294. /// </summary>
  295. public override Rect Bounds {
  296. get {
  297. return Thickness?.GetInside (new Rect (Point.Empty, Frame.Size)) ?? new Rect (Point.Empty, Frame.Size);
  298. }
  299. set {
  300. throw new InvalidOperationException ("It makes no sense to set Bounds of a Thickness.");
  301. }
  302. }
  303. /// <summary>
  304. /// Draws the title for a Window-style view.
  305. /// </summary>
  306. /// <param name="region">Screen relative region where the title will be drawn.</param>
  307. /// <param name="title">The title.</param>
  308. public void DrawTitle (Rect region, ustring title)
  309. {
  310. var width = region.Width;
  311. if (!ustring.IsNullOrEmpty (title)) {
  312. Driver.Move (region.X + 2, region.Y);
  313. //Driver.AddRune (' ');
  314. var str = title.Sum (r => Math.Max (Rune.ColumnWidth (r), 1)) >= width
  315. ? TextFormatter.Format (title, width, false, false) [0] : title;
  316. Driver.AddStr (str);
  317. }
  318. }
  319. /// <summary>
  320. /// Draws a frame in the current view, clipped by the boundary of this view
  321. /// </summary>
  322. /// <param name="region">View-relative region for the frame to be drawn.</param>
  323. /// <param name="clear">If set to <see langword="true"/> it clear the region.</param>
  324. [ObsoleteAttribute ("This method is obsolete in v2. Use use LineCanvas or Frame instead.", false)]
  325. public void DrawFrame (Rect region, bool clear)
  326. {
  327. var savedClip = ClipToBounds ();
  328. var screenBounds = ViewToScreen (region);
  329. if (clear) {
  330. Driver.FillRect (region);
  331. }
  332. var lc = new LineCanvas ();
  333. var drawTop = region.Width > 1 && region.Height > 1;
  334. var drawLeft = region.Width > 1 && region.Height > 1;
  335. var drawBottom = region.Width > 1 && region.Height > 1;
  336. var drawRight = region.Width > 1 && region.Height > 1;
  337. if (drawTop) {
  338. lc.AddLine (screenBounds.Location, screenBounds.Width, Orientation.Horizontal, BorderStyle);
  339. }
  340. if (drawLeft) {
  341. lc.AddLine (screenBounds.Location, screenBounds.Height, Orientation.Vertical, BorderStyle);
  342. }
  343. if (drawBottom) {
  344. lc.AddLine (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1), screenBounds.Width, Orientation.Horizontal, BorderStyle);
  345. }
  346. if (drawRight) {
  347. lc.AddLine (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y), screenBounds.Height, Orientation.Vertical, BorderStyle);
  348. }
  349. foreach (var p in lc.GetMap ()) {
  350. Driver.Move (p.Key.X, p.Key.Y);
  351. Driver.AddRune (p.Value);
  352. }
  353. lc.Clear ();
  354. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  355. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
  356. // Top
  357. var hruler = new Ruler () { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  358. if (drawTop) {
  359. hruler.Draw (new Point (screenBounds.X, screenBounds.Y));
  360. }
  361. //Left
  362. var vruler = new Ruler () { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  363. if (drawLeft) {
  364. vruler.Draw (new Point (screenBounds.X, screenBounds.Y + 1), 1);
  365. }
  366. // Bottom
  367. if (drawBottom) {
  368. hruler.Draw (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  369. }
  370. // Right
  371. if (drawRight) {
  372. vruler.Draw (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  373. }
  374. }
  375. Driver.Clip = savedClip;
  376. }
  377. }
  378. }