Window.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // Authors:
  3. // Miguel de Icaza ([email protected])
  4. //
  5. // NOTE: Window is functionally identical to FrameView with the following exceptions.
  6. // - Window is a Toplevel
  7. // - FrameView Does not support padding (but should)
  8. // - FrameView Does not support mouse dragging
  9. // - FrameView Does not support IEnumerable
  10. // Any udpates done here should probably be done in FrameView as well; TODO: Merge these classes
  11. using System.Collections;
  12. using NStack;
  13. namespace Terminal.Gui {
  14. /// <summary>
  15. /// 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.
  16. /// </summary>
  17. /// <remarks>
  18. /// 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
  19. /// API to determine this rectangle.
  20. /// </remarks>
  21. public class Window : Toplevel, IEnumerable {
  22. View contentView;
  23. ustring title;
  24. /// <summary>
  25. /// The title to be displayed for this window.
  26. /// </summary>
  27. /// <value>The title</value>
  28. public ustring Title {
  29. get => title;
  30. set {
  31. title = value;
  32. SetNeedsDisplay ();
  33. }
  34. }
  35. /// <summary>
  36. /// ContentView is an internal implementation detail of Window. It is used to host Views added with <see cref="Add(View)"/>.
  37. /// Its ONLY reason for being is to provide a simple way for Window to expose to those SubViews that the Window's Bounds
  38. /// are actually deflated due to the border.
  39. /// </summary>
  40. class ContentView : View {
  41. public ContentView (Rect frame) : base (frame) { }
  42. public ContentView () : base () { }
  43. }
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="Gui.Window"/> class with an optional title using <see cref="LayoutStyle.Absolute"/> positioning.
  46. /// </summary>
  47. /// <param name="frame">Superview-relatie rectangle specifying the location and size</param>
  48. /// <param name="title">Title</param>
  49. /// <remarks>
  50. /// This constructor intitalizes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  51. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle.Computed"/>.
  52. /// </remarks>
  53. public Window (Rect frame, ustring title = null) : this (frame, title, padding: 0)
  54. {
  55. }
  56. /// <summary>
  57. /// Initializes a new instance of the <see cref="Window"/> class with an optional title using <see cref="LayoutStyle.Computed"/> positioning.
  58. /// </summary>
  59. /// <param name="title">Title.</param>
  60. /// <remarks>
  61. /// This constructor intitalize a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  62. /// 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.
  63. /// </remarks>
  64. public Window (ustring title = null) : this (title, padding: 0)
  65. {
  66. }
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="Window"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  69. /// </summary>
  70. public Window () : this (title: null) { }
  71. int padding;
  72. /// <summary>
  73. /// 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,
  74. /// and an optional title.
  75. /// </summary>
  76. /// <param name="frame">Superview-relatie rectangle specifying the location and size</param>
  77. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  78. /// <param name="title">Title</param>
  79. /// <remarks>
  80. /// This constructor intitalizes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  81. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>
  82. /// </remarks>
  83. public Window (Rect frame, ustring title = null, int padding = 0) : base (frame)
  84. {
  85. this.Title = title;
  86. int wb = 2 * (1 + padding);
  87. this.padding = padding;
  88. var cFrame = new Rect (1 + padding, 1 + padding, frame.Width - wb, frame.Height - wb);
  89. contentView = new ContentView (cFrame);
  90. base.Add (contentView);
  91. }
  92. /// <summary>
  93. /// 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,
  94. /// and an optional title.
  95. /// </summary>
  96. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  97. /// <param name="title">Title.</param>
  98. /// <remarks>
  99. /// This constructor intitalize a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  100. /// 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.
  101. /// </remarks>
  102. public Window (ustring title = null, int padding = 0) : base ()
  103. {
  104. this.Title = title;
  105. int wb = 1 + padding;
  106. this.padding = padding;
  107. contentView = new ContentView () {
  108. X = wb,
  109. Y = wb,
  110. Width = Dim.Fill (wb),
  111. Height = Dim.Fill (wb)
  112. };
  113. base.Add (contentView);
  114. }
  115. /// <summary>
  116. /// Enumerates the various <see cref="View"/>s in the embedded <see cref="ContentView"/>.
  117. /// </summary>
  118. /// <returns>The enumerator.</returns>
  119. public new IEnumerator GetEnumerator ()
  120. {
  121. return contentView.GetEnumerator ();
  122. }
  123. /// <inheritdoc/>
  124. public override void Add (View view)
  125. {
  126. contentView.Add (view);
  127. if (view.CanFocus)
  128. CanFocus = true;
  129. }
  130. /// <inheritdoc/>
  131. public override void Remove (View view)
  132. {
  133. if (view == null)
  134. return;
  135. SetNeedsDisplay ();
  136. var touched = view.Frame;
  137. contentView.Remove (view);
  138. if (contentView.InternalSubviews.Count < 1)
  139. this.CanFocus = false;
  140. }
  141. /// <inheritdoc/>
  142. public override void RemoveAll ()
  143. {
  144. contentView.RemoveAll ();
  145. }
  146. ///<inheritdoc/>
  147. public override void Redraw (Rect bounds)
  148. {
  149. //var padding = 0;
  150. Application.CurrentView = this;
  151. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  152. // 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?
  153. if (NeedDisplay != null && !NeedDisplay.IsEmpty) {
  154. Driver.SetAttribute (ColorScheme.Normal);
  155. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  156. }
  157. var savedClip = ClipToBounds ();
  158. // Redraw our contenetView
  159. // TODO: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
  160. contentView.Redraw (contentView.Bounds);
  161. Driver.Clip = savedClip;
  162. ClearNeedsDisplay ();
  163. Driver.SetAttribute (ColorScheme.Normal);
  164. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  165. if (HasFocus)
  166. Driver.SetAttribute (ColorScheme.HotNormal);
  167. Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  168. Driver.SetAttribute (ColorScheme.Normal);
  169. }
  170. //
  171. // FIXED:It does not look like the event is raised on clicked-drag
  172. // need to figure that out.
  173. //
  174. internal static Point? dragPosition;
  175. Point start;
  176. ///<inheritdoc/>
  177. public override bool MouseEvent (MouseEvent mouseEvent)
  178. {
  179. // FIXED:The code is currently disabled, because the
  180. // Driver.UncookMouse does not seem to have an effect if there is
  181. // a pending mouse event activated.
  182. int nx, ny;
  183. if ((mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) ||
  184. mouseEvent.Flags == MouseFlags.Button3Pressed)) {
  185. if (dragPosition.HasValue) {
  186. if (SuperView == null) {
  187. Application.Top.SetNeedsDisplay (Frame);
  188. // Redraw the entire app window using just our Frame. Since we are
  189. // Application.Top, and our Frame always == our Bounds (Location is always (0,0))
  190. // our Frame is actually view-relative (which is what Redraw takes).
  191. Application.Top.Redraw (Frame);
  192. } else {
  193. SuperView.SetNeedsDisplay (Frame);
  194. }
  195. EnsureVisibleBounds (this, mouseEvent.X + mouseEvent.OfX - start.X,
  196. mouseEvent.Y + mouseEvent.OfY, out nx, out ny);
  197. dragPosition = new Point (nx, ny);
  198. Frame = new Rect (nx, ny, Frame.Width, Frame.Height);
  199. X = nx;
  200. Y = ny;
  201. //Demo.ml2.Text = $"{dx},{dy}";
  202. // FIXED: optimize, only SetNeedsDisplay on the before/after regions.
  203. SetNeedsDisplay ();
  204. return true;
  205. } else {
  206. // Only start grabbing if the user clicks on the title bar.
  207. if (mouseEvent.Y == 0) {
  208. start = new Point (mouseEvent.X, mouseEvent.Y);
  209. dragPosition = new Point ();
  210. nx = mouseEvent.X - mouseEvent.OfX;
  211. ny = mouseEvent.Y - mouseEvent.OfY;
  212. dragPosition = new Point (nx, ny);
  213. Application.GrabMouse (this);
  214. }
  215. //Demo.ml2.Text = $"Starting at {dragPosition}";
  216. return true;
  217. }
  218. }
  219. if (mouseEvent.Flags == MouseFlags.Button1Released && dragPosition.HasValue) {
  220. Application.UngrabMouse ();
  221. Driver.UncookMouse ();
  222. dragPosition = null;
  223. }
  224. //Demo.ml.Text = me.ToString ();
  225. return false;
  226. }
  227. /// <summary>
  228. /// The text displayed by the <see cref="Label"/>.
  229. /// </summary>
  230. public override ustring Text {
  231. get => contentView.Text;
  232. set {
  233. base.Text = value;
  234. if (contentView != null) {
  235. contentView.Text = value;
  236. }
  237. }
  238. }
  239. /// <summary>
  240. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  241. /// </summary>
  242. /// <value>The text alignment.</value>
  243. public override TextAlignment TextAlignment {
  244. get => contentView.TextAlignment;
  245. set {
  246. base.TextAlignment = contentView.TextAlignment = value;
  247. }
  248. }
  249. }
  250. }