Window.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. /// <inheritdoc/>
  36. public override Border Border {
  37. get => base.Border;
  38. set {
  39. if (base.Border != null && base.Border.Child != null && value.Child == null) {
  40. value.Child = base.Border.Child;
  41. }
  42. base.Border = value;
  43. if (value == null) {
  44. return;
  45. }
  46. Rect frame;
  47. if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
  48. frame = Rect.Empty;
  49. } else {
  50. frame = Frame;
  51. }
  52. AdjustContentView (frame);
  53. Border.BorderChanged += Border_BorderChanged;
  54. }
  55. }
  56. void Border_BorderChanged (Border border)
  57. {
  58. Rect frame;
  59. if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
  60. frame = Rect.Empty;
  61. } else {
  62. frame = Frame;
  63. }
  64. AdjustContentView (frame);
  65. }
  66. /// <summary>
  67. /// ContentView is an internal implementation detail of Window. It is used to host Views added with <see cref="Add(View)"/>.
  68. /// Its ONLY reason for being is to provide a simple way for Window to expose to those SubViews that the Window's Bounds
  69. /// are actually deflated due to the border.
  70. /// </summary>
  71. class ContentView : View {
  72. Window instance;
  73. public ContentView (Rect frame, Window instance) : base (frame)
  74. {
  75. this.instance = instance;
  76. }
  77. public ContentView (Window instance) : base ()
  78. {
  79. this.instance = instance;
  80. }
  81. public override void OnCanFocusChanged ()
  82. {
  83. if (MostFocused == null && CanFocus && Visible) {
  84. EnsureFocus ();
  85. }
  86. base.OnCanFocusChanged ();
  87. }
  88. public override bool OnMouseEvent (MouseEvent mouseEvent)
  89. {
  90. return instance.OnMouseEvent (mouseEvent);
  91. }
  92. }
  93. /// <summary>
  94. /// Initializes a new instance of the <see cref="Gui.Window"/> class with an optional title using <see cref="LayoutStyle.Absolute"/> positioning.
  95. /// </summary>
  96. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  97. /// <param name="title">Title</param>
  98. /// <remarks>
  99. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  100. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle.Computed"/>.
  101. /// </remarks>
  102. public Window (Rect frame, ustring title = null) : this (frame, title, padding: 0, border: null)
  103. {
  104. }
  105. /// <summary>
  106. /// Initializes a new instance of the <see cref="Window"/> class with an optional title using <see cref="LayoutStyle.Computed"/> positioning.
  107. /// </summary>
  108. /// <param name="title">Title.</param>
  109. /// <remarks>
  110. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  111. /// 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.
  112. /// </remarks>
  113. public Window (ustring title = null) : this (title, padding: 0, border: null)
  114. {
  115. }
  116. /// <summary>
  117. /// Initializes a new instance of the <see cref="Window"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  118. /// </summary>
  119. public Window () : this (title: null) { }
  120. /// <summary>
  121. /// 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,
  122. /// and an optional title.
  123. /// </summary>
  124. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  125. /// <param name="title">Title</param>
  126. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  127. /// <param name="border">The <see cref="Border"/>.</param>
  128. /// <remarks>
  129. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  130. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>
  131. /// </remarks>
  132. public Window (Rect frame, ustring title = null, int padding = 0, Border border = null) : base (frame)
  133. {
  134. Initialize (title, frame, padding, border);
  135. }
  136. /// <summary>
  137. /// Initializes a new instance of the <see cref="Window"/> using <see cref="LayoutStyle.Computed"/> positioning,
  138. /// and an optional title.
  139. /// </summary>
  140. /// <param name="title">Title.</param>
  141. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  142. /// <param name="border">The <see cref="Border"/>.</param>
  143. /// <remarks>
  144. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  145. /// 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.
  146. /// </remarks>
  147. public Window (ustring title = null, int padding = 0, Border border = null) : base ()
  148. {
  149. Initialize (title, Rect.Empty, padding, border);
  150. }
  151. void Initialize (ustring title, Rect frame, int padding = 0, Border border = null)
  152. {
  153. CanFocus = true;
  154. ColorScheme = Colors.Base;
  155. Title = title;
  156. if (border == null) {
  157. Border = new Border () {
  158. BorderStyle = BorderStyle.Single,
  159. Padding = new Thickness (padding),
  160. BorderBrush = ColorScheme.Normal.Background
  161. };
  162. } else {
  163. Border = border;
  164. }
  165. }
  166. void AdjustContentView (Rect frame)
  167. {
  168. var borderLength = Border.DrawMarginFrame ? 1 : 0;
  169. var sumPadding = Border.GetSumThickness ();
  170. var wb = new Size ();
  171. if (frame == Rect.Empty) {
  172. wb.Width = borderLength + sumPadding.Right;
  173. wb.Height = borderLength + sumPadding.Bottom;
  174. if (contentView == null) {
  175. contentView = new ContentView (this) {
  176. X = borderLength + sumPadding.Left,
  177. Y = borderLength + sumPadding.Top,
  178. Width = Dim.Fill (wb.Width),
  179. Height = Dim.Fill (wb.Height)
  180. };
  181. } else {
  182. contentView.X = borderLength + sumPadding.Left;
  183. contentView.Y = borderLength + sumPadding.Top;
  184. contentView.Width = Dim.Fill (wb.Width);
  185. contentView.Height = Dim.Fill (wb.Height);
  186. }
  187. } else {
  188. wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  189. wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  190. var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  191. if (contentView == null) {
  192. contentView = new ContentView (cFrame, this);
  193. } else {
  194. contentView.Frame = cFrame;
  195. }
  196. }
  197. base.Add (contentView);
  198. Border.Child = contentView;
  199. }
  200. ///// <summary>
  201. ///// Enumerates the various <see cref="View"/>s in the embedded <see cref="ContentView"/>.
  202. ///// </summary>
  203. ///// <returns>The enumerator.</returns>
  204. //public new IEnumerator GetEnumerator ()
  205. //{
  206. // return contentView.GetEnumerator ();
  207. //}
  208. /// <inheritdoc/>
  209. public override void Add (View view)
  210. {
  211. contentView.Add (view);
  212. if (view.CanFocus) {
  213. CanFocus = true;
  214. }
  215. AddMenuStatusBar (view);
  216. }
  217. /// <inheritdoc/>
  218. public override void Remove (View view)
  219. {
  220. if (view == null) {
  221. return;
  222. }
  223. SetNeedsDisplay ();
  224. contentView.Remove (view);
  225. if (contentView.InternalSubviews.Count < 1) {
  226. CanFocus = false;
  227. }
  228. RemoveMenuStatusBar (view);
  229. if (view != contentView && Focused == null) {
  230. FocusFirst ();
  231. }
  232. }
  233. /// <inheritdoc/>
  234. public override void RemoveAll ()
  235. {
  236. contentView.RemoveAll ();
  237. }
  238. ///<inheritdoc/>
  239. public override void Redraw (Rect bounds)
  240. {
  241. var padding = Border.GetSumThickness ();
  242. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  243. //var borderLength = Border.DrawMarginFrame ? 1 : 0;
  244. // FIXED: Why do we draw the frame twice? This call is here to clear the content area, I think. Why not just clear that area?
  245. if (!NeedDisplay.IsEmpty) {
  246. Driver.SetAttribute (GetNormalColor ());
  247. Clear ();
  248. }
  249. var savedClip = contentView.ClipToBounds ();
  250. // Redraw our contentView
  251. // DONE: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
  252. contentView.Redraw (!NeedDisplay.IsEmpty ? contentView.Bounds : bounds);
  253. Driver.Clip = savedClip;
  254. ClearLayoutNeeded ();
  255. ClearNeedsDisplay ();
  256. if (Border.BorderStyle != BorderStyle.None) {
  257. Driver.SetAttribute (GetNormalColor ());
  258. //Driver.DrawWindowFrame (scrRect, padding.Left + borderLength, padding.Top + borderLength, padding.Right + borderLength, padding.Bottom + borderLength,
  259. // Border.BorderStyle != BorderStyle.None, fill: true, Border.BorderStyle);
  260. Border.DrawContent (this, false);
  261. if (HasFocus)
  262. Driver.SetAttribute (ColorScheme.HotNormal);
  263. Driver.DrawWindowTitle (scrRect, Title, padding.Left, padding.Top, padding.Right, padding.Bottom);
  264. }
  265. Driver.SetAttribute (GetNormalColor ());
  266. // Checks if there are any SuperView view which intersect with this window.
  267. if (SuperView != null) {
  268. SuperView.SetNeedsLayout ();
  269. SuperView.SetNeedsDisplay ();
  270. }
  271. }
  272. /// <inheritdoc/>
  273. public override void OnCanFocusChanged ()
  274. {
  275. if (contentView != null) {
  276. contentView.CanFocus = CanFocus;
  277. }
  278. base.OnCanFocusChanged ();
  279. }
  280. /// <summary>
  281. /// The text displayed by the <see cref="Label"/>.
  282. /// </summary>
  283. public override ustring Text {
  284. get => contentView.Text;
  285. set {
  286. base.Text = value;
  287. if (contentView != null) {
  288. contentView.Text = value;
  289. }
  290. }
  291. }
  292. /// <summary>
  293. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  294. /// </summary>
  295. /// <value>The text alignment.</value>
  296. public override TextAlignment TextAlignment {
  297. get => contentView.TextAlignment;
  298. set {
  299. base.TextAlignment = contentView.TextAlignment = value;
  300. }
  301. }
  302. }
  303. }