Window.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. int padding;
  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. /// <summary>
  37. /// ContentView is an internal implementation detail of Window. It is used to host Views added with <see cref="Add(View)"/>.
  38. /// Its ONLY reason for being is to provide a simple way for Window to expose to those SubViews that the Window's Bounds
  39. /// are actually deflated due to the border.
  40. /// </summary>
  41. class ContentView : View {
  42. public ContentView (Rect frame) : base (frame) { }
  43. public ContentView () : base () { }
  44. }
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="Gui.Window"/> class with an optional title using <see cref="LayoutStyle.Absolute"/> positioning.
  47. /// </summary>
  48. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  49. /// <param name="title">Title</param>
  50. /// <remarks>
  51. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  52. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle.Computed"/>.
  53. /// </remarks>
  54. public Window (Rect frame, ustring title = null) : this (frame, title, padding: 0)
  55. {
  56. }
  57. /// <summary>
  58. /// Initializes a new instance of the <see cref="Window"/> class with an optional title using <see cref="LayoutStyle.Computed"/> positioning.
  59. /// </summary>
  60. /// <param name="title">Title.</param>
  61. /// <remarks>
  62. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  63. /// 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.
  64. /// </remarks>
  65. public Window (ustring title = null) : this (title, padding: 0)
  66. {
  67. }
  68. /// <summary>
  69. /// Initializes a new instance of the <see cref="Window"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  70. /// </summary>
  71. public Window () : this (title: null) { }
  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 initializes 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. Initialize (title, frame, padding);
  86. }
  87. /// <summary>
  88. /// Initializes a new instance of the <see cref="Window"/> using <see cref="LayoutStyle.Computed"/> positioning,
  89. /// and an optional title.
  90. /// </summary>
  91. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  92. /// <param name="title">Title.</param>
  93. /// <remarks>
  94. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  95. /// 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.
  96. /// </remarks>
  97. public Window (ustring title = null, int padding = 0) : base ()
  98. {
  99. Initialize (title, Rect.Empty, padding);
  100. }
  101. void Initialize (ustring title, Rect frame, int padding = 0)
  102. {
  103. ColorScheme = Colors.Base;
  104. Title = title;
  105. int wb;
  106. if (frame == Rect.Empty) {
  107. wb = 1 + padding;
  108. contentView = new ContentView () {
  109. X = wb,
  110. Y = wb,
  111. Width = Dim.Fill (wb),
  112. Height = Dim.Fill (wb)
  113. };
  114. } else {
  115. wb = 2 * (1 + padding);
  116. var cFrame = new Rect (1 + padding, 1 + padding, frame.Width - wb, frame.Height - wb);
  117. contentView = new ContentView (cFrame);
  118. }
  119. this.padding = padding;
  120. base.Add (contentView);
  121. }
  122. ///// <summary>
  123. ///// Enumerates the various <see cref="View"/>s in the embedded <see cref="ContentView"/>.
  124. ///// </summary>
  125. ///// <returns>The enumerator.</returns>
  126. //public new IEnumerator GetEnumerator ()
  127. //{
  128. // return contentView.GetEnumerator ();
  129. //}
  130. /// <inheritdoc/>
  131. public override void Add (View view)
  132. {
  133. contentView.Add (view);
  134. if (view.CanFocus) {
  135. CanFocus = true;
  136. }
  137. AddMenuStatusBar (view);
  138. }
  139. /// <inheritdoc/>
  140. public override void Remove (View view)
  141. {
  142. if (view == null) {
  143. return;
  144. }
  145. SetNeedsDisplay ();
  146. var touched = view.Frame;
  147. contentView.Remove (view);
  148. if (contentView.InternalSubviews.Count < 1) {
  149. CanFocus = false;
  150. }
  151. RemoveMenuStatusBar (view);
  152. }
  153. /// <inheritdoc/>
  154. public override void RemoveAll ()
  155. {
  156. contentView.RemoveAll ();
  157. }
  158. ///<inheritdoc/>
  159. public override void Redraw (Rect bounds)
  160. {
  161. //var padding = 0;
  162. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  163. // 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?
  164. if (!NeedDisplay.IsEmpty) {
  165. Driver.SetAttribute (ColorScheme.Normal);
  166. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
  167. }
  168. var savedClip = ClipToBounds ();
  169. // Redraw our contentView
  170. // TODO: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
  171. contentView.Redraw (contentView.Bounds);
  172. Driver.Clip = savedClip;
  173. ClearLayoutNeeded ();
  174. ClearNeedsDisplay ();
  175. Driver.SetAttribute (ColorScheme.Normal);
  176. Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
  177. if (HasFocus)
  178. Driver.SetAttribute (ColorScheme.HotNormal);
  179. Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
  180. Driver.SetAttribute (ColorScheme.Normal);
  181. // Checks if there are any SuperView view which intersect with this window.
  182. if (SuperView != null) {
  183. SuperView.SetNeedsLayout ();
  184. SuperView.SetNeedsDisplay ();
  185. }
  186. }
  187. //
  188. // FIXED:It does not look like the event is raised on clicked-drag
  189. // need to figure that out.
  190. //
  191. internal static Point? dragPosition;
  192. Point start;
  193. ///<inheritdoc/>
  194. public override bool MouseEvent (MouseEvent mouseEvent)
  195. {
  196. // FIXED:The code is currently disabled, because the
  197. // Driver.UncookMouse does not seem to have an effect if there is
  198. // a pending mouse event activated.
  199. int nx, ny;
  200. if (!dragPosition.HasValue && (mouseEvent.Flags == MouseFlags.Button1Pressed
  201. || mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  202. // Only start grabbing if the user clicks on the title bar.
  203. if (mouseEvent.Y == 0) {
  204. start = new Point (mouseEvent.X, mouseEvent.Y);
  205. dragPosition = new Point ();
  206. nx = mouseEvent.X - mouseEvent.OfX;
  207. ny = mouseEvent.Y - mouseEvent.OfY;
  208. dragPosition = new Point (nx, ny);
  209. Application.GrabMouse (this);
  210. }
  211. //System.Diagnostics.Debug.WriteLine ($"Starting at {dragPosition}");
  212. return true;
  213. } else if (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) ||
  214. mouseEvent.Flags == MouseFlags.Button3Pressed) {
  215. if (dragPosition.HasValue) {
  216. if (SuperView == null) {
  217. Application.Top.SetNeedsDisplay (Frame);
  218. // Redraw the entire app window using just our Frame. Since we are
  219. // Application.Top, and our Frame always == our Bounds (Location is always (0,0))
  220. // our Frame is actually view-relative (which is what Redraw takes).
  221. Application.Top.Redraw (Frame);
  222. } else {
  223. SuperView.SetNeedsDisplay (Frame);
  224. }
  225. EnsureVisibleBounds (this, mouseEvent.X + (SuperView == null ? mouseEvent.OfX - start.X : Frame.X - start.X),
  226. mouseEvent.Y + (SuperView == null ? mouseEvent.OfY : Frame.Y), out nx, out ny);
  227. dragPosition = new Point (nx, ny);
  228. LayoutSubviews ();
  229. Frame = new Rect (nx, ny, Frame.Width, Frame.Height);
  230. if (X == null || X is Pos.PosAbsolute) {
  231. X = nx;
  232. }
  233. if (Y == null || Y is Pos.PosAbsolute) {
  234. Y = ny;
  235. }
  236. //System.Diagnostics.Debug.WriteLine ($"nx:{nx},ny:{ny}");
  237. // FIXED: optimize, only SetNeedsDisplay on the before/after regions.
  238. SetNeedsDisplay ();
  239. return true;
  240. }
  241. }
  242. if (mouseEvent.Flags == MouseFlags.Button1Released && dragPosition.HasValue) {
  243. Application.UngrabMouse ();
  244. Driver.UncookMouse ();
  245. dragPosition = null;
  246. }
  247. //System.Diagnostics.Debug.WriteLine (mouseEvent.ToString ());
  248. return false;
  249. }
  250. /// <summary>
  251. /// The text displayed by the <see cref="Label"/>.
  252. /// </summary>
  253. public override ustring Text {
  254. get => contentView.Text;
  255. set {
  256. base.Text = value;
  257. if (contentView != null) {
  258. contentView.Text = value;
  259. }
  260. }
  261. }
  262. /// <summary>
  263. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  264. /// </summary>
  265. /// <value>The text alignment.</value>
  266. public override TextAlignment TextAlignment {
  267. get => contentView.TextAlignment;
  268. set {
  269. base.TextAlignment = contentView.TextAlignment = value;
  270. }
  271. }
  272. }
  273. }