Window.cs 6.7 KB

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