Window.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // Authors:
  3. // Miguel de Icaza ([email protected])
  4. //
  5. using System.Collections;
  6. using NStack;
  7. namespace Terminal.Gui {
  8. /// <summary>
  9. /// A <see cref="Toplevel"/> <see cref="View"/> that draws a border around its <see cref="View.Frame"/> with a <see cref="Title"/> at the top.
  10. /// </summary>
  11. /// <remarks>
  12. /// The 'client area' of a <see cref="Window"/> is a rectangle deflated by one or more rows/columns from <see cref="View.Bounds"/>. A this time there is no
  13. /// API to determine this rectangle.
  14. /// </remarks>
  15. public class Window : Toplevel, IEnumerable {
  16. View contentView;
  17. ustring title;
  18. /// <summary>
  19. /// The title to be displayed for this window.
  20. /// </summary>
  21. /// <value>The title</value>
  22. public ustring Title {
  23. get => title;
  24. set {
  25. title = value;
  26. SetNeedsDisplay ();
  27. }
  28. }
  29. /// <summary>
  30. /// ContentView is an internal implementation detail of Window. It is used to host Views added with <see cref="Add(View)"/>.
  31. /// Its ONLY reason for being is to provide a simple way for Window to expose to those SubViews that the Window's Bounds
  32. /// are actually deflated due to the border.
  33. /// </summary>
  34. class ContentView : View {
  35. public ContentView (Rect frame) : base (frame) { }
  36. public ContentView () : base () { }
  37. #if false
  38. public override void Redraw (Rect bounds)
  39. {
  40. Driver.SetAttribute (ColorScheme.Focus);
  41. for (int y = 0; y < Frame.Height; y++) {
  42. Move (0, y);
  43. for (int x = 0; x < Frame.Width; x++) {
  44. Driver.AddRune ('x');
  45. }
  46. }
  47. base.Redraw (region);
  48. }
  49. #endif
  50. }
  51. /// <summary>
  52. /// Initializes a new instance of the <see cref="Gui.Window"/> class with an optional title using <see cref="LayoutStyle.Absolute"/> positioning.
  53. /// </summary>
  54. /// <param name="frame">Superview-relatie rectangle specifying the location and size</param>
  55. /// <param name="title">Title</param>
  56. /// <remarks>
  57. /// This constructor intitalizes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  58. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle.Computed"/>.
  59. /// </remarks>
  60. public Window (Rect frame, ustring title = null) : this (frame, title, padding: 0)
  61. {
  62. }
  63. /// <summary>
  64. /// Initializes a new instance of the <see cref="Window"/> class with an optional title.
  65. /// </summary>
  66. /// <param name="title">Title.</param>
  67. /// <remarks>
  68. /// This constructor intitalize a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  69. /// Use <see cref="View.X"/>, <see cref="View.Y"/>, <see cref="View.Width"/>, and <see cref="View.Height"/> properties to dynamically control the size and location of the view.
  70. /// </remarks>
  71. public Window (ustring title = null) : this (title, padding: 0)
  72. {
  73. }
  74. int padding;
  75. /// <summary>
  76. /// Initializes a new instance of the <see cref="Window"/> with the specified frame for its location, with the specified border,
  77. /// and an optional title.
  78. /// </summary>
  79. /// <param name="frame">Superview-relatie rectangle specifying the location and size</param>
  80. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  81. /// <param name="title">Title</param>
  82. /// <remarks>
  83. /// This constructor intitalizes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  84. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>
  85. /// </remarks>
  86. public Window (Rect frame, ustring title = null, int padding = 0) : base (frame)
  87. {
  88. this.Title = title;
  89. int wb = 2 * (1 + padding);
  90. this.padding = padding;
  91. var cFrame = new Rect (1 + padding, 1 + padding, frame.Width - wb, frame.Height - wb);
  92. contentView = new ContentView (cFrame);
  93. base.Add (contentView);
  94. }
  95. /// <summary>
  96. /// Initializes a new instance of the <see cref="Window"/> with the specified frame for its location, with the specified border,
  97. /// and an optional title.
  98. /// </summary>
  99. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  100. /// <param name="title">Title.</param>
  101. /// <remarks>
  102. /// This constructor intitalize a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  103. /// Use <see cref="View.X"/>, <see cref="View.Y"/>, <see cref="View.Width"/>, and <see cref="View.Height"/> properties to dynamically control the size and location of the view.
  104. /// </remarks>
  105. public Window (ustring title = null, int padding = 0) : base ()
  106. {
  107. this.Title = title;
  108. int wb = 1 + padding;
  109. this.padding = padding;
  110. contentView = new ContentView () {
  111. X = wb,
  112. Y = wb,
  113. Width = Dim.Fill (wb),
  114. Height = Dim.Fill (wb)
  115. };
  116. base.Add (contentView);
  117. }
  118. /// <summary>
  119. /// Enumerates the various <see cref="View"/>s in the embedded <see cref="ContentView"/>.
  120. /// </summary>
  121. /// <returns>The enumerator.</returns>
  122. public new IEnumerator GetEnumerator ()
  123. {
  124. return contentView.GetEnumerator ();
  125. }
  126. /// <inheritdoc/>
  127. public override void Add (View view)
  128. {
  129. contentView.Add (view);
  130. if (view.CanFocus)
  131. CanFocus = true;
  132. }
  133. /// <inheritdoc/>
  134. public override void Remove (View view)
  135. {
  136. if (view == null)
  137. return;
  138. SetNeedsDisplay ();
  139. var touched = view.Frame;
  140. contentView.Remove (view);
  141. if (contentView.InternalSubviews.Count < 1)
  142. this.CanFocus = false;
  143. }
  144. /// <inheritdoc/>
  145. public override void RemoveAll ()
  146. {
  147. contentView.RemoveAll ();
  148. }
  149. ///<inheritdoc/>
  150. public override void Redraw (Rect bounds)
  151. {
  152. //var padding = 0;
  153. Application.CurrentView = this;
  154. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  155. // BUGBUG: Why do we draw the frame twice? This call is here to clear the content area, I think. Why not just clear that area?
  156. if (NeedDisplay != null && !NeedDisplay.IsEmpty) {
  157. Driver.SetAttribute (ColorScheme.Normal);
  158. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  159. }
  160. var savedClip = ClipToBounds ();
  161. // Redraw our contenetView
  162. // TODO: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
  163. contentView.Redraw (contentView.Bounds);
  164. Driver.Clip = savedClip;
  165. ClearNeedsDisplay ();
  166. Driver.SetAttribute (ColorScheme.Normal);
  167. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  168. if (HasFocus)
  169. Driver.SetAttribute (ColorScheme.HotNormal);
  170. Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  171. Driver.SetAttribute (ColorScheme.Normal);
  172. }
  173. //
  174. // FIXED:It does not look like the event is raised on clicked-drag
  175. // need to figure that out.
  176. //
  177. internal static Point? dragPosition;
  178. Point start;
  179. ///<inheritdoc/>
  180. public override bool MouseEvent (MouseEvent mouseEvent)
  181. {
  182. // FIXED:The code is currently disabled, because the
  183. // Driver.UncookMouse does not seem to have an effect if there is
  184. // a pending mouse event activated.
  185. int nx, ny;
  186. if ((mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) ||
  187. mouseEvent.Flags == MouseFlags.Button3Pressed)) {
  188. if (dragPosition.HasValue) {
  189. if (SuperView == null) {
  190. Application.Top.SetNeedsDisplay (Frame);
  191. // Redraw the entire app window using just our Frame. Since we are
  192. // Application.Top, and our Frame always == our Bounds (Location is always (0,0))
  193. // our Frame is actually view-relative (which is what Redraw takes).
  194. Application.Top.Redraw (Frame);
  195. } else {
  196. SuperView.SetNeedsDisplay (Frame);
  197. }
  198. EnsureVisibleBounds (this, mouseEvent.X + mouseEvent.OfX - start.X,
  199. mouseEvent.Y + mouseEvent.OfY, out nx, out ny);
  200. dragPosition = new Point (nx, ny);
  201. Frame = new Rect (nx, ny, Frame.Width, Frame.Height);
  202. X = nx;
  203. Y = ny;
  204. //Demo.ml2.Text = $"{dx},{dy}";
  205. // FIXED: optimize, only SetNeedsDisplay on the before/after regions.
  206. SetNeedsDisplay ();
  207. return true;
  208. } else {
  209. // Only start grabbing if the user clicks on the title bar.
  210. if (mouseEvent.Y == 0) {
  211. start = new Point (mouseEvent.X, mouseEvent.Y);
  212. dragPosition = new Point ();
  213. nx = mouseEvent.X - mouseEvent.OfX;
  214. ny = mouseEvent.Y - mouseEvent.OfY;
  215. dragPosition = new Point (nx, ny);
  216. Application.GrabMouse (this);
  217. }
  218. //Demo.ml2.Text = $"Starting at {dragPosition}";
  219. return true;
  220. }
  221. }
  222. if (mouseEvent.Flags == MouseFlags.Button1Released && dragPosition.HasValue) {
  223. Application.UngrabMouse ();
  224. Driver.UncookMouse ();
  225. dragPosition = null;
  226. }
  227. //Demo.ml.Text = me.ToString ();
  228. return false;
  229. }
  230. }
  231. }