Frame.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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?.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. /// <summary>
  75. /// Redraws the Frames that comprise the <see cref="Frame"/>.
  76. /// </summary>
  77. /// <param name="clipRect"></param>
  78. public override void Redraw (Rect clipRect)
  79. {
  80. //OnDrawContent (bounds);
  81. //OnDrawSubViews (bounds);
  82. //OnDrawContentComplete (bounds);
  83. if (ColorScheme != null) {
  84. Driver.SetAttribute (ColorScheme.Normal);
  85. }
  86. var prevClip = SetClip (Frame);
  87. var screenBounds = ViewToScreen (Frame);
  88. Thickness.Draw (screenBounds, (string)(Data != null ? Data : string.Empty));
  89. //OnDrawContent (bounds);
  90. if (BorderStyle != BorderStyle.None) {
  91. var lc = new LineCanvas ();
  92. lc.AddLine (screenBounds.Location, Frame.Width - 1, Orientation.Horizontal, BorderStyle);
  93. lc.AddLine (screenBounds.Location, Frame.Height - 1, Orientation.Vertical, BorderStyle);
  94. lc.AddLine (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1), screenBounds.Width - 1, Orientation.Horizontal, BorderStyle);
  95. lc.AddLine (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y), screenBounds.Height - 1, Orientation.Vertical, BorderStyle);
  96. foreach (var p in lc.GenerateImage (screenBounds)) {
  97. Driver.Move (p.Key.X, p.Key.Y);
  98. Driver.AddRune (p.Value);
  99. }
  100. if (!ustring.IsNullOrEmpty (Parent?.Title)) {
  101. Driver.SetAttribute (Parent.HasFocus ? Parent.GetHotNormalColor () : Parent.GetNormalColor ());
  102. Driver.DrawWindowTitle (screenBounds, Parent?.Title, 0, 0, 0, 0);
  103. }
  104. }
  105. Driver.Clip = prevClip;
  106. }
  107. //public Label DiagnosticsLabel { get; set; }
  108. // TODO: v2 = This is teporary; need to also enable (or not) simple way of setting
  109. // other border properties
  110. // TOOD: v2 - Missing 3D effect
  111. /// <summary>
  112. ///
  113. /// </summary>
  114. public BorderStyle BorderStyle { get; set; } = BorderStyle.None;
  115. /// <summary>
  116. /// Defines the rectangle that the <see cref="Frame"/> will use to draw its content.
  117. /// </summary>
  118. public Thickness Thickness { get; set; }
  119. // TODO: v2 - This is confusing. It is a read-only property and actually only returns a size, so
  120. // should not be a Rect. However, it may make sense to keep it a Rect and support negative Location
  121. // for scrolling. Still noodling this.
  122. /// <summary>
  123. /// Gets the rectangle that describes the inner area of the frame. The Location is always 0, 0.
  124. /// </summary>
  125. public override Rect Bounds {
  126. get {
  127. if (Thickness == null) {
  128. return new Rect (Point.Empty, Frame.Size);
  129. }
  130. // Return the frame-relative bounds
  131. return Thickness.GetInnerRect (new Rect (Point.Empty, Frame.Size));
  132. }
  133. set {
  134. throw new InvalidOperationException ("It makes no sense to explicitly set Bounds.");
  135. }
  136. }
  137. }
  138. }