Window.cs 6.9 KB

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