Window.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 using <see cref="LayoutStyle.Computed"/> positioning.
  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. /// <summary>
  75. /// Initializes a new instance of the <see cref="Window"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  76. /// </summary>
  77. public Window () : this (title: null) { }
  78. int padding;
  79. /// <summary>
  80. /// Initializes a new instance of the <see cref="Window"/> using <see cref="LayoutStyle.Absolute"/> positioning with the specified frame for its location, with the specified frame padding,
  81. /// and an optional title.
  82. /// </summary>
  83. /// <param name="frame">Superview-relatie rectangle specifying the location and size</param>
  84. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  85. /// <param name="title">Title</param>
  86. /// <remarks>
  87. /// This constructor intitalizes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  88. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>
  89. /// </remarks>
  90. public Window (Rect frame, ustring title = null, int padding = 0) : base (frame)
  91. {
  92. this.Title = title;
  93. int wb = 2 * (1 + padding);
  94. this.padding = padding;
  95. var cFrame = new Rect (1 + padding, 1 + padding, frame.Width - wb, frame.Height - wb);
  96. contentView = new ContentView (cFrame);
  97. base.Add (contentView);
  98. }
  99. /// <summary>
  100. /// Initializes a new instance of the <see cref="Window"/> using <see cref="LayoutStyle.Absolute"/> positioning with the specified frame for its location, with the specified frame padding,
  101. /// and an optional title.
  102. /// </summary>
  103. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  104. /// <param name="title">Title.</param>
  105. /// <remarks>
  106. /// This constructor intitalize a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  107. /// 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.
  108. /// </remarks>
  109. public Window (ustring title = null, int padding = 0) : base ()
  110. {
  111. this.Title = title;
  112. int wb = 1 + padding;
  113. this.padding = padding;
  114. contentView = new ContentView () {
  115. X = wb,
  116. Y = wb,
  117. Width = Dim.Fill (wb),
  118. Height = Dim.Fill (wb)
  119. };
  120. base.Add (contentView);
  121. }
  122. /// <summary>
  123. /// Enumerates the various <see cref="View"/>s in the embedded <see cref="ContentView"/>.
  124. /// </summary>
  125. /// <returns>The enumerator.</returns>
  126. public new IEnumerator GetEnumerator ()
  127. {
  128. return contentView.GetEnumerator ();
  129. }
  130. /// <inheritdoc/>
  131. public override void Add (View view)
  132. {
  133. contentView.Add (view);
  134. if (view.CanFocus)
  135. CanFocus = true;
  136. }
  137. /// <inheritdoc/>
  138. public override void Remove (View view)
  139. {
  140. if (view == null)
  141. return;
  142. SetNeedsDisplay ();
  143. var touched = view.Frame;
  144. contentView.Remove (view);
  145. if (contentView.InternalSubviews.Count < 1)
  146. this.CanFocus = false;
  147. }
  148. /// <inheritdoc/>
  149. public override void RemoveAll ()
  150. {
  151. contentView.RemoveAll ();
  152. }
  153. ///<inheritdoc/>
  154. public override void Redraw (Rect bounds)
  155. {
  156. //var padding = 0;
  157. Application.CurrentView = this;
  158. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  159. // 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?
  160. if (NeedDisplay != null && !NeedDisplay.IsEmpty) {
  161. Driver.SetAttribute (ColorScheme.Normal);
  162. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  163. }
  164. var savedClip = ClipToBounds ();
  165. // Redraw our contenetView
  166. // TODO: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
  167. contentView.Redraw (contentView.Bounds);
  168. Driver.Clip = savedClip;
  169. ClearNeedsDisplay ();
  170. Driver.SetAttribute (ColorScheme.Normal);
  171. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  172. if (HasFocus)
  173. Driver.SetAttribute (ColorScheme.HotNormal);
  174. Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  175. Driver.SetAttribute (ColorScheme.Normal);
  176. }
  177. //
  178. // FIXED:It does not look like the event is raised on clicked-drag
  179. // need to figure that out.
  180. //
  181. internal static Point? dragPosition;
  182. Point start;
  183. ///<inheritdoc/>
  184. public override bool MouseEvent (MouseEvent mouseEvent)
  185. {
  186. // FIXED:The code is currently disabled, because the
  187. // Driver.UncookMouse does not seem to have an effect if there is
  188. // a pending mouse event activated.
  189. int nx, ny;
  190. if ((mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) ||
  191. mouseEvent.Flags == MouseFlags.Button3Pressed)) {
  192. if (dragPosition.HasValue) {
  193. if (SuperView == null) {
  194. Application.Top.SetNeedsDisplay (Frame);
  195. // Redraw the entire app window using just our Frame. Since we are
  196. // Application.Top, and our Frame always == our Bounds (Location is always (0,0))
  197. // our Frame is actually view-relative (which is what Redraw takes).
  198. Application.Top.Redraw (Frame);
  199. } else {
  200. SuperView.SetNeedsDisplay (Frame);
  201. }
  202. EnsureVisibleBounds (this, mouseEvent.X + mouseEvent.OfX - start.X,
  203. mouseEvent.Y + mouseEvent.OfY, out nx, out ny);
  204. dragPosition = new Point (nx, ny);
  205. Frame = new Rect (nx, ny, Frame.Width, Frame.Height);
  206. X = nx;
  207. Y = ny;
  208. //Demo.ml2.Text = $"{dx},{dy}";
  209. // FIXED: optimize, only SetNeedsDisplay on the before/after regions.
  210. SetNeedsDisplay ();
  211. return true;
  212. } else {
  213. // Only start grabbing if the user clicks on the title bar.
  214. if (mouseEvent.Y == 0) {
  215. start = new Point (mouseEvent.X, mouseEvent.Y);
  216. dragPosition = new Point ();
  217. nx = mouseEvent.X - mouseEvent.OfX;
  218. ny = mouseEvent.Y - mouseEvent.OfY;
  219. dragPosition = new Point (nx, ny);
  220. Application.GrabMouse (this);
  221. }
  222. //Demo.ml2.Text = $"Starting at {dragPosition}";
  223. return true;
  224. }
  225. }
  226. if (mouseEvent.Flags == MouseFlags.Button1Released && dragPosition.HasValue) {
  227. Application.UngrabMouse ();
  228. Driver.UncookMouse ();
  229. dragPosition = null;
  230. }
  231. //Demo.ml.Text = me.ToString ();
  232. return false;
  233. }
  234. }
  235. }