Frame.cs 7.2 KB

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