Window.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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-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. }
  175. //
  176. // FIXED:It does not look like the event is raised on clicked-drag
  177. // need to figure that out.
  178. //
  179. internal static Point? dragPosition;
  180. Point start;
  181. ///<inheritdoc/>
  182. public override bool MouseEvent (MouseEvent mouseEvent)
  183. {
  184. // FIXED:The code is currently disabled, because the
  185. // Driver.UncookMouse does not seem to have an effect if there is
  186. // a pending mouse event activated.
  187. int nx, ny;
  188. if (!dragPosition.HasValue && mouseEvent.Flags == (MouseFlags.Button1Pressed)) {
  189. // Only start grabbing if the user clicks on the title bar.
  190. if (mouseEvent.Y == 0) {
  191. start = new Point (mouseEvent.X, mouseEvent.Y);
  192. dragPosition = new Point ();
  193. nx = mouseEvent.X - mouseEvent.OfX;
  194. ny = mouseEvent.Y - mouseEvent.OfY;
  195. dragPosition = new Point (nx, ny);
  196. Application.GrabMouse (this);
  197. }
  198. //Demo.ml2.Text = $"Starting at {dragPosition}";
  199. return true;
  200. } else if (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) ||
  201. mouseEvent.Flags == MouseFlags.Button3Pressed) {
  202. if (dragPosition.HasValue) {
  203. if (SuperView == null) {
  204. Application.Top.SetNeedsDisplay (Frame);
  205. // Redraw the entire app window using just our Frame. Since we are
  206. // Application.Top, and our Frame always == our Bounds (Location is always (0,0))
  207. // our Frame is actually view-relative (which is what Redraw takes).
  208. Application.Top.Redraw (Frame);
  209. } else {
  210. SuperView.SetNeedsDisplay (Frame);
  211. }
  212. EnsureVisibleBounds (this, mouseEvent.X + (SuperView == null ? mouseEvent.OfX - start.X : Frame.X - start.X),
  213. mouseEvent.Y + (SuperView == null ? mouseEvent.OfY : Frame.Y), out nx, out ny);
  214. dragPosition = new Point (nx, ny);
  215. Frame = new Rect (nx, ny, Frame.Width, Frame.Height);
  216. X = nx;
  217. Y = ny;
  218. //Demo.ml2.Text = $"{dx},{dy}";
  219. // FIXED: optimize, only SetNeedsDisplay on the before/after regions.
  220. SetNeedsDisplay ();
  221. return true;
  222. }
  223. }
  224. if (mouseEvent.Flags == MouseFlags.Button1Released && dragPosition.HasValue) {
  225. Application.UngrabMouse ();
  226. Driver.UncookMouse ();
  227. dragPosition = null;
  228. }
  229. //Demo.ml.Text = me.ToString ();
  230. return false;
  231. }
  232. /// <summary>
  233. /// The text displayed by the <see cref="Label"/>.
  234. /// </summary>
  235. public override ustring Text {
  236. get => contentView.Text;
  237. set {
  238. base.Text = value;
  239. if (contentView != null) {
  240. contentView.Text = value;
  241. }
  242. }
  243. }
  244. /// <summary>
  245. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  246. /// </summary>
  247. /// <value>The text alignment.</value>
  248. public override TextAlignment TextAlignment {
  249. get => contentView.TextAlignment;
  250. set {
  251. base.TextAlignment = contentView.TextAlignment = value;
  252. }
  253. }
  254. }
  255. }