Frame.cs 16 KB

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