Frame.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /// enabling borders, menus, etc...
  11. /// </summary>
  12. public class Frame : View {
  13. // TODO: v2 - If a Frame has focus, navigation keys (e.g Command.NextView) should cycle through SubViews of the Frame
  14. // QUESTION: How does a user navigate out of a Frame to another Frame, or back into the Parent's SubViews?
  15. /// <summary>
  16. /// Frames are a special form of <see cref="View"/> that act as adornments; they appear outside of the <see cref="View.Bounds"/>
  17. /// enabling borders, menus, etc...
  18. /// </summary>
  19. public Frame ()
  20. {
  21. }
  22. /// <summary>
  23. /// The Parent of this Frame.
  24. /// </summary>
  25. public View Parent { get; set; }
  26. /// <summary>
  27. /// Frames cannot be used as sub-views, so this method always throws an <see cref="InvalidOperationException"/>.
  28. /// TODO: Are we sure?
  29. /// </summary>
  30. public override View SuperView {
  31. get {
  32. return null;
  33. }
  34. set {
  35. throw new NotImplementedException ();
  36. }
  37. }
  38. /// <inheritdoc/>
  39. public override void ViewToScreen (int col, int row, out int rcol, out int rrow, bool clipped = true)
  40. {
  41. // Frames are *Children* of a View, not SubViews. Thus View.ViewToScreen will not work.
  42. // To get the screen-relative coordinates of a Frame, we need to know who
  43. // the Parent is
  44. var parentFrame = Parent?.Frame ?? Frame;
  45. rrow = row + parentFrame.Y;
  46. rcol = col + parentFrame.X;
  47. // We now have rcol/rrow in coordinates relative to our SuperView. If our SuperView has
  48. // a SuperView, keep going...
  49. Parent?.SuperView?.SuperView?.ViewToScreen (rcol, rrow, out rcol, out rrow, clipped);
  50. }
  51. /// <summary>
  52. ///
  53. /// </summary>
  54. /// <param name="clipRect"></param>
  55. public virtual void OnDrawSubViews (Rect clipRect)
  56. {
  57. // if (Subviews == null) {
  58. // return;
  59. // }
  60. // foreach (var view in Subviews) {
  61. // // BUGBUG: v2 - shouldn't this be !view.LayoutNeeded? Why draw if layout is going to happen and we'll just draw again?
  62. // if (view.LayoutNeeded) {
  63. // view.LayoutSubviews ();
  64. // }
  65. // if ((view.Visible && !view.NeedDisplay.IsEmpty && view.Frame.Width > 0 && view.Frame.Height > 0) || view.ChildNeedsDisplay) {
  66. // view.Redraw (view.Bounds);
  67. // view.NeedDisplay = Rect.Empty;
  68. // // BUGBUG - v2 why does this need to be set to false?
  69. // // Shouldn't it be set when the subviews draw?
  70. // view.ChildNeedsDisplay = false;
  71. // }
  72. // }
  73. }
  74. /// <inheritdoc/>
  75. public override void OnDrawContent (Rect viewport)
  76. {
  77. if (!ustring.IsNullOrEmpty (TextFormatter.Text)) {
  78. Clear (viewport);
  79. SetSubViewNeedsDisplay ();
  80. // Draw any Text
  81. if (TextFormatter != null) {
  82. TextFormatter.NeedsFormat = true;
  83. Rect containerBounds = GetContainerBounds ();
  84. TextFormatter?.Draw (ViewToScreen (viewport), HasFocus ? ColorScheme.Focus : GetNormalColor (),
  85. HasFocus ? ColorScheme.HotFocus : Enabled ? ColorScheme.HotNormal : ColorScheme.Disabled,
  86. containerBounds);
  87. }
  88. }
  89. }
  90. /// <summary>
  91. /// Redraws the Frames that comprise the <see cref="Frame"/>.
  92. /// </summary>
  93. /// <param name="clipRect"></param>
  94. public override void Redraw (Rect clipRect)
  95. {
  96. //OnDrawContent (bounds);
  97. //OnDrawSubViews (bounds);
  98. //OnDrawContentComplete (bounds);
  99. if (ColorScheme != null) {
  100. Driver.SetAttribute (ColorScheme.Normal);
  101. }
  102. var screenBounds = ViewToScreen (Frame);
  103. Thickness.Draw (screenBounds, (string)Data);
  104. //OnDrawContent (bounds);
  105. if (BorderStyle != BorderStyle.None) {
  106. var lc = new LineCanvas ();
  107. lc.AddLine (screenBounds.Location, Frame.Width - 1, Orientation.Horizontal, BorderStyle);
  108. lc.AddLine (screenBounds.Location, Frame.Height - 1, Orientation.Vertical, BorderStyle);
  109. lc.AddLine (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1), screenBounds.Width - 1, Orientation.Horizontal, BorderStyle);
  110. lc.AddLine (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y), screenBounds.Height - 1, Orientation.Vertical, BorderStyle);
  111. foreach (var p in lc.GenerateImage (screenBounds)) {
  112. Driver.Move (p.Key.X, p.Key.Y);
  113. Driver.AddRune (p.Value);
  114. }
  115. if (!ustring.IsNullOrEmpty (Parent?.Title)) {
  116. Driver.DrawWindowTitle (screenBounds, Parent?.Title, 0, 0, 0, 0);
  117. }
  118. }
  119. }
  120. //public Label DiagnosticsLabel { get; set; }
  121. // TODO: v2 = This is teporary; need to also enable (or not) simple way of setting
  122. // other border properties
  123. // TOOD: v2 - Missing 3D effect
  124. /// <summary>
  125. ///
  126. /// </summary>
  127. public BorderStyle BorderStyle { get; set; } = BorderStyle.None;
  128. /// <summary>
  129. /// Defines the rectangle that the <see cref="Frame"/> will use to draw its content.
  130. /// </summary>
  131. public Thickness Thickness { get; set; }
  132. // TODO: v2 - This is confusing. It is a read-only property and actually only returns a size, so
  133. // should not be a Rect. However, it may make sense to keep it a Rect and support negative Location
  134. // for scrolling. Still noodling this.
  135. /// <summary>
  136. /// Gets the rectangle that describes the inner area of the frame. The Location is always 0, 0.
  137. /// </summary>
  138. public override Rect Bounds {
  139. get {
  140. if (Thickness == null) {
  141. return new Rect (Point.Empty, Frame.Size);
  142. }
  143. // Return the frame-relative bounds
  144. return Thickness.GetInnerRect (new Rect (Point.Empty, Frame.Size));
  145. }
  146. set {
  147. throw new InvalidOperationException ("It makes no sense to explicitly set Bounds.");
  148. }
  149. }
  150. }
  151. }