Container.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /// <summary>
  9. /// Frames are a special form of <see cref="View"/> that act as adornments; they appear outside of the <see cref="View.Bounds"/>
  10. /// eanbling borders, menus, etc...
  11. /// </summary>
  12. public class Frame : View {
  13. /// <summary>
  14. /// Frames are a special form of <see cref="View"/> that act as adornments; they appear outside of the <see cref="View.Bounds"/>
  15. /// eanbling borders, menus, etc...
  16. /// </summary>
  17. public Frame ()
  18. {
  19. IgnoreBorderPropertyOnRedraw = true;
  20. }
  21. /// <summary>
  22. /// The Parent of this Frame (Adornment).
  23. /// </summary>
  24. public View Parent { get; set; }
  25. /// <summary>
  26. /// Frames cannot be used as sub-views, so this method always throws an <see cref="InvalidOperationException"/>.
  27. /// TODO: Are we sure?
  28. /// </summary>
  29. public override View SuperView {
  30. get {
  31. return null;
  32. }
  33. set {
  34. throw new NotImplementedException ();
  35. }
  36. }
  37. /// <inheritdoc/>
  38. public override void ViewToScreen (int col, int row, out int rcol, out int rrow, bool clipped = true)
  39. {
  40. // Frames are children of a View, not SubViews. Thus ViewToScreen will not work.
  41. // To get the screen-relative coordinates of a Frame, we need to know who
  42. // the Parent is
  43. // Computes the real row, col relative to the screen.
  44. var inner = Parent?.Bounds ?? Bounds;
  45. rrow = row - inner.Y;
  46. rcol = col - inner.X;
  47. Parent?.ViewToScreen (rcol, rrow, out rcol, out rrow, clipped);
  48. }
  49. /// <summary>
  50. ///
  51. /// </summary>
  52. /// <param name="clipRect"></param>
  53. public virtual void OnDrawSubViews (Rect clipRect)
  54. {
  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. /// <inheritdoc/>
  73. public override void OnDrawContent (Rect viewport)
  74. {
  75. if (!ustring.IsNullOrEmpty (TextFormatter.Text)) {
  76. Clear (viewport);
  77. SetChildNeedsDisplay ();
  78. // Draw any Text
  79. if (TextFormatter != null) {
  80. TextFormatter.NeedsFormat = true;
  81. Rect containerBounds = GetContainerBounds ();
  82. TextFormatter?.Draw (ViewToScreen (viewport), HasFocus ? ColorScheme.Focus : GetNormalColor (),
  83. HasFocus ? ColorScheme.HotFocus : Enabled ? ColorScheme.HotNormal : ColorScheme.Disabled,
  84. containerBounds);
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// Redraws the Frames that comprise the <see cref="Frame"/>.
  90. /// </summary>
  91. /// <param name="clipRect"></param>
  92. public override void Redraw (Rect clipRect)
  93. {
  94. //OnDrawContent (bounds);
  95. //OnDrawSubViews (bounds);
  96. //OnDrawContentComplete (bounds);
  97. if (ColorScheme != null) {
  98. Driver.SetAttribute (ColorScheme.Normal);
  99. }
  100. var screenBounds = ViewToScreen (Frame);
  101. Thickness.Draw (screenBounds, (string)Data);
  102. //OnDrawContent (bounds);
  103. if (BorderStyle != BorderStyle.None) {
  104. var lc = new LineCanvas ();
  105. lc.AddLine (screenBounds.Location, Frame.Width - 1, Orientation.Horizontal, BorderStyle);
  106. lc.AddLine (screenBounds.Location, Frame.Height - 1, Orientation.Vertical, BorderStyle);
  107. lc.AddLine (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1), screenBounds.Width - 1, Orientation.Horizontal, BorderStyle);
  108. lc.AddLine (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y), screenBounds.Height - 1, Orientation.Vertical, BorderStyle);
  109. foreach (var p in lc.GenerateImage (screenBounds)) {
  110. Driver.Move (p.Key.X, p.Key.Y);
  111. Driver.AddRune (p.Value);
  112. }
  113. if (!ustring.IsNullOrEmpty (Title)) {
  114. Driver.DrawWindowTitle (screenBounds, Title, 0, 0, 0, 0);
  115. }
  116. }
  117. }
  118. //public Label DiagnosticsLabel { get; set; }
  119. // TODO: v2 = This is teporary; need to also enable (or not) simple way of setting
  120. // other border properties
  121. // TOOD: v2 - Missing 3D effect
  122. /// <summary>
  123. ///
  124. /// </summary>
  125. public BorderStyle BorderStyle { get; set; } = BorderStyle.None;
  126. /// <summary>
  127. ///
  128. /// </summary>
  129. public Thickness Thickness { get; set; }
  130. // TODO: v2 - This is confusing. It is a read-only property and actually only returns a size, so
  131. // should not be a Rect. However, it may make sense to keep it a Rect and support negative Location
  132. // for scrolling. Still noodling this.
  133. /// <summary>
  134. /// Gets the rectangle that describes the inner area of the frame. The Location is always 0, 0.
  135. /// </summary>
  136. public override Rect Bounds {
  137. get {
  138. if (Thickness == null) {
  139. return new Rect (Point.Empty, Frame.Size);
  140. }
  141. // Return the frame-relative bounds
  142. return Thickness.GetInnerRect (new Rect (Point.Empty, Frame.Size));
  143. }
  144. set {
  145. throw new InvalidOperationException ("It makes no sense to explicitly set Bounds.");
  146. }
  147. }
  148. //public override void OnDrawContent (Rect viewport)
  149. //{
  150. // // do nothing
  151. //}
  152. //public override void Redraw (Rect bounds)
  153. //{
  154. // if (ColorScheme != null) {
  155. // Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  156. // }
  157. // //if (Text != null) {
  158. // // Thickness?.Draw (Frame, $"{Text} {DiagnosticsLabel?.Text}");
  159. // //}
  160. // if (BorderStyle != BorderStyle.None) {
  161. // var lc = new LineCanvas ();
  162. // lc.AddLine (Frame.Location, Frame.Width - 1, Orientation.Horizontal, BorderStyle);
  163. // lc.AddLine (Frame.Location, Frame.Height - 1, Orientation.Vertical, BorderStyle);
  164. // lc.AddLine (new Point (Frame.X, Frame.Y + Frame.Height - 1), Frame.Width - 1, Orientation.Horizontal, BorderStyle);
  165. // lc.AddLine (new Point (Frame.X + Frame.Width - 1, Frame.Y), Frame.Height - 1, Orientation.Vertical, BorderStyle);
  166. // foreach (var p in lc.GenerateImage (Frame)) {
  167. // Driver.Move (p.Key.X, p.Key.Y);
  168. // Driver.AddRune (p.Value);
  169. // }
  170. // if (!ustring.IsNullOrEmpty (Title)) {
  171. // Driver.DrawWindowTitle (Frame, Title, 0, 0, 0, 0);
  172. // }
  173. // }
  174. // base.Redraw (bounds);
  175. //}
  176. }
  177. }