Frame.cs 16 KB

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