Window.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 {
  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-relative 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-relative 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. AddMenuStatusBar (view);
  131. }
  132. /// <inheritdoc/>
  133. public override void Remove (View view)
  134. {
  135. if (view == null) {
  136. return;
  137. }
  138. SetNeedsDisplay ();
  139. var touched = view.Frame;
  140. contentView.Remove (view);
  141. if (contentView.InternalSubviews.Count < 1) {
  142. CanFocus = false;
  143. }
  144. RemoveMenuStatusBar (view);
  145. }
  146. /// <inheritdoc/>
  147. public override void RemoveAll ()
  148. {
  149. contentView.RemoveAll ();
  150. }
  151. ///<inheritdoc/>
  152. public override void Redraw (Rect bounds)
  153. {
  154. //var padding = 0;
  155. Application.CurrentView = this;
  156. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  157. // 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?
  158. if (!NeedDisplay.IsEmpty) {
  159. Driver.SetAttribute (ColorScheme.Normal);
  160. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  161. }
  162. var savedClip = ClipToBounds ();
  163. // Redraw our contenetView
  164. // TODO: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
  165. contentView.Redraw (contentView.Bounds);
  166. Driver.Clip = savedClip;
  167. ClearNeedsDisplay ();
  168. Driver.SetAttribute (ColorScheme.Normal);
  169. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  170. if (HasFocus)
  171. Driver.SetAttribute (ColorScheme.HotNormal);
  172. Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  173. Driver.SetAttribute (ColorScheme.Normal);
  174. // Checks if there are any SuperView view which intersect with this window.
  175. if (SuperView != null) {
  176. foreach (var view in SuperView.Subviews) {
  177. if (view != this && view.Frame.IntersectsWith (Bounds)) {
  178. view.SetNeedsLayout ();
  179. view.SetNeedsDisplay (view.Bounds);
  180. }
  181. }
  182. }
  183. }
  184. //
  185. // FIXED:It does not look like the event is raised on clicked-drag
  186. // need to figure that out.
  187. //
  188. internal static Point? dragPosition;
  189. Point start;
  190. ///<inheritdoc/>
  191. public override bool MouseEvent (MouseEvent mouseEvent)
  192. {
  193. // FIXED:The code is currently disabled, because the
  194. // Driver.UncookMouse does not seem to have an effect if there is
  195. // a pending mouse event activated.
  196. int nx, ny;
  197. if (!dragPosition.HasValue && mouseEvent.Flags == (MouseFlags.Button1Pressed)) {
  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. } else if (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) ||
  210. mouseEvent.Flags == MouseFlags.Button3Pressed) {
  211. if (dragPosition.HasValue) {
  212. if (SuperView == null) {
  213. Application.Top.SetNeedsDisplay (Frame);
  214. // Redraw the entire app window using just our Frame. Since we are
  215. // Application.Top, and our Frame always == our Bounds (Location is always (0,0))
  216. // our Frame is actually view-relative (which is what Redraw takes).
  217. Application.Top.Redraw (Frame);
  218. } else {
  219. SuperView.SetNeedsDisplay (Frame);
  220. }
  221. EnsureVisibleBounds (this, mouseEvent.X + (SuperView == null ? mouseEvent.OfX - start.X : Frame.X - start.X),
  222. mouseEvent.Y + (SuperView == null ? mouseEvent.OfY : Frame.Y), out nx, out ny);
  223. dragPosition = new Point (nx, ny);
  224. Frame = new Rect (nx, ny, Frame.Width, Frame.Height);
  225. X = nx;
  226. Y = ny;
  227. //Demo.ml2.Text = $"{dx},{dy}";
  228. // FIXED: optimize, only SetNeedsDisplay on the before/after regions.
  229. SetNeedsDisplay ();
  230. return true;
  231. }
  232. }
  233. if (mouseEvent.Flags == MouseFlags.Button1Released && dragPosition.HasValue) {
  234. Application.UngrabMouse ();
  235. Driver.UncookMouse ();
  236. dragPosition = null;
  237. }
  238. //Demo.ml.Text = me.ToString ();
  239. return false;
  240. }
  241. /// <summary>
  242. /// The text displayed by the <see cref="Label"/>.
  243. /// </summary>
  244. public override ustring Text {
  245. get => contentView.Text;
  246. set {
  247. base.Text = value;
  248. if (contentView != null) {
  249. contentView.Text = value;
  250. }
  251. }
  252. }
  253. /// <summary>
  254. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  255. /// </summary>
  256. /// <value>The text alignment.</value>
  257. public override TextAlignment TextAlignment {
  258. get => contentView.TextAlignment;
  259. set {
  260. base.TextAlignment = contentView.TextAlignment = value;
  261. }
  262. }
  263. }
  264. }