Window.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "ContentView" 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 region)
  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. public Window (Rect frame, ustring title = null) : this (frame, title, padding: 0)
  48. {
  49. }
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="Window"/> class with an optional title.
  52. /// </summary>
  53. /// <param name="title">Title.</param>
  54. public Window (ustring title = null) : this (title, padding: 0)
  55. {
  56. }
  57. int padding;
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="Window"/> with
  60. /// the specified frame for its location, with the specified border
  61. /// an optional title.
  62. /// </summary>
  63. /// <param name="frame">Frame.</param>
  64. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  65. /// <param name="title">Title.</param>
  66. public Window (Rect frame, ustring title = null, int padding = 0) : base (frame)
  67. {
  68. this.Title = title;
  69. int wb = 2 * (1 + padding);
  70. this.padding = padding;
  71. var cFrame = new Rect (1 + padding, 1 + padding, frame.Width - wb, frame.Height - wb);
  72. contentView = new ContentView (cFrame);
  73. base.Add (contentView);
  74. }
  75. /// <summary>
  76. /// Initializes a new instance of the <see cref="Window"/> with
  77. /// the specified frame for its location, with the specified border
  78. /// an optional title.
  79. /// </summary>
  80. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  81. /// <param name="title">Title.</param>
  82. public Window (ustring title = null, int padding = 0) : base ()
  83. {
  84. this.Title = title;
  85. int wb = 1 + padding;
  86. this.padding = padding;
  87. contentView = new ContentView () {
  88. X = wb,
  89. Y = wb,
  90. Width = Dim.Fill (wb),
  91. Height = Dim.Fill (wb)
  92. };
  93. base.Add (contentView);
  94. }
  95. /// <summary>
  96. /// Enumerates the various <see cref="View"/>s in the embedded <see cref="ContentView"/>.
  97. /// </summary>
  98. /// <returns>The enumerator.</returns>
  99. public new IEnumerator GetEnumerator ()
  100. {
  101. return contentView.GetEnumerator ();
  102. }
  103. /// <summary>
  104. /// Add the specified view to the <see cref="ContentView"/>.
  105. /// </summary>
  106. /// <param name="view">View to add to the window.</param>
  107. public override void Add (View view)
  108. {
  109. contentView.Add (view);
  110. if (view.CanFocus)
  111. CanFocus = true;
  112. }
  113. /// <summary>
  114. /// Removes a widget from this container.
  115. /// </summary>
  116. /// <remarks>
  117. /// </remarks>
  118. public override void Remove (View view)
  119. {
  120. if (view == null)
  121. return;
  122. SetNeedsDisplay ();
  123. var touched = view.Frame;
  124. contentView.Remove (view);
  125. if (contentView.InternalSubviews.Count < 1)
  126. this.CanFocus = false;
  127. }
  128. /// <summary>
  129. /// Removes all widgets from this container.
  130. /// </summary>
  131. /// <remarks>
  132. /// </remarks>
  133. public override void RemoveAll ()
  134. {
  135. contentView.RemoveAll ();
  136. }
  137. ///<inheritdoc cref="Redraw"/>
  138. public override void Redraw (Rect bounds)
  139. {
  140. //var padding = 0;
  141. Application.CurrentView = this;
  142. var scrRect = RectToScreen (new Rect(0, 0, Frame.Width, Frame.Height));
  143. // 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?
  144. if (NeedDisplay != null && !NeedDisplay.IsEmpty) {
  145. Driver.SetAttribute (ColorScheme.Normal);
  146. Driver.DrawFrame (scrRect, padding, true);
  147. }
  148. if (Driver.Clip.IsEmpty || Driver.Clip.Contains (contentView.RectToScreen (contentView.Frame))) {
  149. var savedClip = ClipToBounds ();
  150. contentView.Redraw (contentView.Bounds);
  151. Driver.Clip = savedClip;
  152. } else {
  153. contentView.Redraw (contentView.Bounds);
  154. }
  155. ClearNeedsDisplay ();
  156. Driver.SetAttribute (ColorScheme.Normal);
  157. Driver.DrawFrame (scrRect, padding, false);
  158. if (HasFocus)
  159. Driver.SetAttribute (ColorScheme.HotNormal);
  160. Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  161. Driver.SetAttribute (ColorScheme.Normal);
  162. }
  163. //
  164. // FIXED:It does not look like the event is raised on clicked-drag
  165. // need to figure that out.
  166. //
  167. internal static Point? dragPosition;
  168. Point start;
  169. ///<inheritdoc cref="MouseEvent(Gui.MouseEvent)"/>
  170. public override bool MouseEvent (MouseEvent mouseEvent)
  171. {
  172. // FIXED:The code is currently disabled, because the
  173. // Driver.UncookMouse does not seem to have an effect if there is
  174. // a pending mouse event activated.
  175. int nx, ny;
  176. if ((mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) ||
  177. mouseEvent.Flags == MouseFlags.Button3Pressed)) {
  178. if (dragPosition.HasValue) {
  179. if (SuperView == null) {
  180. Application.Top.SetNeedsDisplay (Frame);
  181. Application.Top.Redraw (Frame);
  182. } else {
  183. SuperView.SetNeedsDisplay (Frame);
  184. }
  185. EnsureVisibleBounds (this, mouseEvent.X + mouseEvent.OfX - start.X,
  186. mouseEvent.Y + mouseEvent.OfY, out nx, out ny);
  187. dragPosition = new Point (nx, ny);
  188. Frame = new Rect (nx, ny, Frame.Width, Frame.Height);
  189. X = nx;
  190. Y = ny;
  191. //Demo.ml2.Text = $"{dx},{dy}";
  192. // FIXED: optimize, only SetNeedsDisplay on the before/after regions.
  193. SetNeedsDisplay ();
  194. return true;
  195. } else {
  196. // Only start grabbing if the user clicks on the title bar.
  197. if (mouseEvent.Y == 0) {
  198. start = new Point (mouseEvent.X, mouseEvent.Y);
  199. dragPosition = new Point ();
  200. nx = mouseEvent.X - mouseEvent.OfX;
  201. ny = mouseEvent.Y - mouseEvent.OfY;
  202. dragPosition = new Point (nx, ny);
  203. Application.GrabMouse (this);
  204. }
  205. //Demo.ml2.Text = $"Starting at {dragPosition}";
  206. return true;
  207. }
  208. }
  209. if (mouseEvent.Flags == MouseFlags.Button1Released && dragPosition.HasValue) {
  210. Application.UngrabMouse ();
  211. Driver.UncookMouse ();
  212. dragPosition = null;
  213. }
  214. //Demo.ml.Text = me.ToString ();
  215. return false;
  216. }
  217. }
  218. }