Frame.cs 16 KB

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