Frame.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Xml.Linq;
  6. namespace Terminal.Gui {
  7. // TODO: v2 - Missing 3D effect - 3D effects will be drawn by a mechanism separate from Frames
  8. // TODO: v2 - If a Frame has focus, navigation keys (e.g Command.NextView) should cycle through SubViews of the Frame
  9. // QUESTION: How does a user navigate out of a Frame to another Frame, or back into the Parent's SubViews?
  10. /// <summary>
  11. /// Frames are a special form of <see cref="View"/> that act as adornments; they appear outside of the <see cref="View.Bounds"/>
  12. /// enabling borders, menus, etc...
  13. /// </summary>
  14. public class Frame : View {
  15. private Thickness _thickness = Thickness.Empty;
  16. internal override void CreateFrames () { /* Do nothing - Frames do not have Frames */ }
  17. internal override void LayoutFrames () { /* Do nothing - Frames do not have Frames */ }
  18. /// <summary>
  19. /// The Parent of this Frame (the View this Frame surrounds).
  20. /// </summary>
  21. public View Parent { get; set; }
  22. /// <summary>
  23. /// Frames cannot be used as sub-views, so this method always throws an <see cref="InvalidOperationException"/>.
  24. /// TODO: Are we sure?
  25. /// </summary>
  26. public override View SuperView {
  27. get {
  28. return null;
  29. }
  30. set {
  31. throw new NotImplementedException ();
  32. }
  33. }
  34. /// <inheritdoc/>
  35. public override void ViewToScreen (int col, int row, out int rcol, out int rrow, bool clipped = true)
  36. {
  37. // Frames are *Children* of a View, not SubViews. Thus View.ViewToScreen will not work.
  38. // To get the screen-relative coordinates of a Frame, we need to know who
  39. // the Parent is
  40. var parentFrame = Parent?.Frame ?? Frame;
  41. rrow = row + parentFrame.Y;
  42. rcol = col + parentFrame.X;
  43. // We now have rcol/rrow in coordinates relative to our SuperView. If our SuperView has
  44. // a SuperView, keep going...
  45. Parent?.SuperView?.ViewToScreen (rcol, rrow, out rcol, out rrow, clipped);
  46. }
  47. /// <summary>
  48. ///
  49. /// </summary>
  50. /// <param name="clipRect"></param>
  51. public virtual void OnDrawSubViews (Rect clipRect)
  52. {
  53. // TODO: Enable subviews of Frames (adornments).
  54. // if (Subviews == null) {
  55. // return;
  56. // }
  57. // foreach (var view in Subviews) {
  58. // // BUGBUG: v2 - shouldn't this be !view.LayoutNeeded? Why draw if layout is going to happen and we'll just draw again?
  59. // if (view.LayoutNeeded) {
  60. // view.LayoutSubviews ();
  61. // }
  62. // if ((view.Visible && !view.NeedDisplay.IsEmpty && view.Frame.Width > 0 && view.Frame.Height > 0) || view.ChildNeedsDisplay) {
  63. // view.Redraw (view.Bounds);
  64. // view.NeedDisplay = Rect.Empty;
  65. // // BUGBUG - v2 why does this need to be set to false?
  66. // // Shouldn't it be set when the subviews draw?
  67. // view.ChildNeedsDisplay = false;
  68. // }
  69. // }
  70. }
  71. /// <summary>
  72. /// Redraws the Frames that comprise the <see cref="Frame"/>.
  73. /// </summary>
  74. /// <param name="bounds"></param>
  75. public override void Redraw (Rect bounds)
  76. {
  77. if (Thickness == Thickness.Empty) return;
  78. if (ColorScheme != null) {
  79. Driver.SetAttribute (ColorScheme.Normal);
  80. } else {
  81. Driver.SetAttribute (Parent.GetNormalColor ());
  82. }
  83. var prevClip = SetClip (Frame);
  84. var screenBounds = ViewToScreen (Frame);
  85. Thickness.Draw (screenBounds, (string)(Data != null ? Data : string.Empty));
  86. //OnDrawSubviews (bounds);
  87. // TODO: v2 - this will eventually be two controls: "BorderView" and "Label" (for the title)
  88. if (Id == "BorderFrame" && Thickness.Top > 0 && Frame.Width > 1 && !ustring.IsNullOrEmpty (Parent?.Title)) {
  89. var prevAttr = Driver.GetAttribute ();
  90. Driver.SetAttribute (Parent.HasFocus ? Parent.GetHotNormalColor () : Parent.GetNormalColor ());
  91. Driver.DrawWindowTitle (screenBounds, Parent?.Title, 0, 0, 0, 0);
  92. Driver.SetAttribute (prevAttr);
  93. }
  94. if (Id == "BorderFrame" && BorderStyle != BorderStyle.None) {
  95. var lc = new LineCanvas ();
  96. var drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height > 1;
  97. var drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  98. var drawBottom = Thickness.Bottom > 0 && Frame.Width > 1;
  99. var drawRight = Thickness.Right > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  100. if (drawTop) {
  101. // ╔╡Title╞═════╗
  102. // ╔╡╞═════╗
  103. if (Frame.Width < 4 || ustring.IsNullOrEmpty (Parent?.Title)) {
  104. // ╔╡╞╗ should be ╔══╗
  105. lc.AddLine (screenBounds.Location, Frame.Width - 1, Orientation.Horizontal, BorderStyle);
  106. } else {
  107. var titleWidth = Math.Min (Parent.Title.ConsoleWidth, Frame.Width - 4);
  108. // ╔╡Title╞═════╗
  109. // Add a short horiz line for ╔╡
  110. lc.AddLine (screenBounds.Location, 1, Orientation.Horizontal, BorderStyle);
  111. // Add a short vert line for ╔╡
  112. lc.AddLine (new Point (screenBounds.X + 1, screenBounds.Location.Y), 0, Orientation.Vertical, BorderStyle.Single);
  113. // Add a short vert line for ╞
  114. lc.AddLine (new Point (screenBounds.X + 1 + (titleWidth + 1), screenBounds.Location.Y), 0, Orientation.Vertical, BorderStyle.Single);
  115. // Add the right hand line for ╞═════╗
  116. lc.AddLine (new Point (screenBounds.X + 1 + (titleWidth + 1), screenBounds.Location.Y), Frame.Width - (titleWidth + 3), Orientation.Horizontal, BorderStyle);
  117. }
  118. }
  119. if (drawLeft) {
  120. lc.AddLine (screenBounds.Location, Frame.Height - 1, Orientation.Vertical, BorderStyle);
  121. }
  122. if (drawBottom) {
  123. lc.AddLine (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1), screenBounds.Width - 1, Orientation.Horizontal, BorderStyle);
  124. }
  125. if (drawRight) {
  126. lc.AddLine (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y), screenBounds.Height - 1, Orientation.Vertical, BorderStyle);
  127. }
  128. foreach (var p in lc.GenerateImage (screenBounds)) {
  129. Driver.Move (p.Key.X, p.Key.Y);
  130. Driver.AddRune (p.Value);
  131. }
  132. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  133. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
  134. // Top
  135. var hruler = new Ruler () { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  136. if (drawTop) {
  137. hruler.Draw (new Point (screenBounds.X, screenBounds.Y));
  138. }
  139. // Redraw title
  140. if (drawTop && Id == "BorderFrame" && !ustring.IsNullOrEmpty (Parent?.Title)) {
  141. var prevAttr = Driver.GetAttribute ();
  142. Driver.SetAttribute (Parent.HasFocus ? Parent.GetHotNormalColor () : Parent.GetNormalColor ());
  143. Driver.DrawWindowTitle (screenBounds, Parent?.Title, 0, 0, 0, 0);
  144. Driver.SetAttribute (prevAttr);
  145. }
  146. //Left
  147. var vruler = new Ruler () { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  148. if (drawLeft) {
  149. vruler.Draw (new Point (screenBounds.X, screenBounds.Y + 1), 1);
  150. }
  151. // Bottom
  152. if (drawBottom) {
  153. hruler.Draw (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  154. }
  155. // Right
  156. if (drawRight) {
  157. vruler.Draw (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  158. }
  159. }
  160. }
  161. Driver.Clip = prevClip;
  162. }
  163. // TODO: v2 - Frame.BorderStyle is temporary - Eventually the border will be drawn by a "BorderView" that is a subview of the Frame.
  164. /// <summary>
  165. ///
  166. /// </summary>
  167. public BorderStyle BorderStyle { get; set; } = BorderStyle.None;
  168. /// <summary>
  169. /// Defines the rectangle that the <see cref="Frame"/> will use to draw its content.
  170. /// </summary>
  171. public Thickness Thickness {
  172. get { return _thickness; }
  173. set {
  174. var prev = _thickness;
  175. _thickness = value;
  176. if (prev != _thickness) {
  177. OnThicknessChanged ();
  178. }
  179. }
  180. }
  181. /// <summary>
  182. /// Called whenever the <see cref="Thickness"/> property changes.
  183. /// </summary>
  184. public virtual void OnThicknessChanged ()
  185. {
  186. ThicknessChanged?.Invoke (this, new ThicknessEventArgs () { Thickness = Thickness });
  187. }
  188. /// <summary>
  189. /// Fired whenever the <see cref="Thickness"/> property changes.
  190. /// </summary>
  191. public event EventHandler<ThicknessEventArgs> ThicknessChanged;
  192. /// <summary>
  193. /// Gets the rectangle that describes the inner area of the frame. The Location is always (0,0).
  194. /// </summary>
  195. public override Rect Bounds {
  196. get {
  197. return Thickness?.GetInside (new Rect (Point.Empty, Frame.Size)) ?? new Rect (Point.Empty, Frame.Size);
  198. }
  199. set {
  200. throw new InvalidOperationException ("It makes no sense to set Bounds of a Thickness.");
  201. }
  202. }
  203. }
  204. }