Window.cs 8.0 KB

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