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