Window.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 bool MouseEvent (MouseEvent mouseEvent)
  82. {
  83. return instance.MouseEvent (mouseEvent);
  84. }
  85. }
  86. /// <summary>
  87. /// Initializes a new instance of the <see cref="Gui.Window"/> class with an optional title using <see cref="LayoutStyle.Absolute"/> positioning.
  88. /// </summary>
  89. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  90. /// <param name="title">Title</param>
  91. /// <remarks>
  92. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  93. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle.Computed"/>.
  94. /// </remarks>
  95. public Window (Rect frame, ustring title = null) : this (frame, title, padding: 0, border: null)
  96. {
  97. }
  98. /// <summary>
  99. /// Initializes a new instance of the <see cref="Window"/> class with an optional title using <see cref="LayoutStyle.Computed"/> positioning.
  100. /// </summary>
  101. /// <param name="title">Title.</param>
  102. /// <remarks>
  103. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  104. /// 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.
  105. /// </remarks>
  106. public Window (ustring title = null) : this (title, padding: 0, border: null)
  107. {
  108. }
  109. /// <summary>
  110. /// Initializes a new instance of the <see cref="Window"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  111. /// </summary>
  112. public Window () : this (title: null) { }
  113. /// <summary>
  114. /// 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,
  115. /// and an optional title.
  116. /// </summary>
  117. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  118. /// <param name="title">Title</param>
  119. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  120. /// <param name="border">The <see cref="Border"/>.</param>
  121. /// <remarks>
  122. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  123. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>
  124. /// </remarks>
  125. public Window (Rect frame, ustring title = null, int padding = 0, Border border = null) : base (frame)
  126. {
  127. Initialize (title, frame, padding, border);
  128. }
  129. /// <summary>
  130. /// Initializes a new instance of the <see cref="Window"/> using <see cref="LayoutStyle.Computed"/> positioning,
  131. /// and an optional title.
  132. /// </summary>
  133. /// <param name="title">Title.</param>
  134. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  135. /// <param name="border">The <see cref="Border"/>.</param>
  136. /// <remarks>
  137. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  138. /// 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.
  139. /// </remarks>
  140. public Window (ustring title = null, int padding = 0, Border border = null) : base ()
  141. {
  142. Initialize (title, Rect.Empty, padding, border);
  143. }
  144. void Initialize (ustring title, Rect frame, int padding = 0, Border border = null)
  145. {
  146. ColorScheme = Colors.Base;
  147. Title = title;
  148. if (border == null) {
  149. Border = new Border () {
  150. BorderStyle = BorderStyle.Single,
  151. Padding = new Thickness (padding),
  152. BorderBrush = ColorScheme.Normal.Background
  153. };
  154. } else {
  155. Border = border;
  156. }
  157. }
  158. void AdjustContentView (Rect frame)
  159. {
  160. var borderLength = Border.DrawMarginFrame ? 1 : 0;
  161. var sumPadding = Border.GetSumThickness ();
  162. var wb = new Size ();
  163. if (frame == Rect.Empty) {
  164. wb.Width = borderLength + sumPadding.Right;
  165. wb.Height = borderLength + sumPadding.Bottom;
  166. if (contentView == null) {
  167. contentView = new ContentView (this) {
  168. X = borderLength + sumPadding.Left,
  169. Y = borderLength + sumPadding.Top,
  170. Width = Dim.Fill (wb.Width),
  171. Height = Dim.Fill (wb.Height)
  172. };
  173. } else {
  174. contentView.X = borderLength + sumPadding.Left;
  175. contentView.Y = borderLength + sumPadding.Top;
  176. contentView.Width = Dim.Fill (wb.Width);
  177. contentView.Height = Dim.Fill (wb.Height);
  178. }
  179. } else {
  180. wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  181. wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  182. var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  183. if (contentView == null) {
  184. contentView = new ContentView (cFrame, this);
  185. } else {
  186. contentView.Frame = cFrame;
  187. }
  188. }
  189. base.Add (contentView);
  190. Border.Child = contentView;
  191. }
  192. ///// <summary>
  193. ///// Enumerates the various <see cref="View"/>s in the embedded <see cref="ContentView"/>.
  194. ///// </summary>
  195. ///// <returns>The enumerator.</returns>
  196. //public new IEnumerator GetEnumerator ()
  197. //{
  198. // return contentView.GetEnumerator ();
  199. //}
  200. /// <inheritdoc/>
  201. public override void Add (View view)
  202. {
  203. contentView.Add (view);
  204. if (view.CanFocus) {
  205. CanFocus = true;
  206. }
  207. AddMenuStatusBar (view);
  208. }
  209. /// <inheritdoc/>
  210. public override void Remove (View view)
  211. {
  212. if (view == null) {
  213. return;
  214. }
  215. SetNeedsDisplay ();
  216. var touched = view.Frame;
  217. contentView.Remove (view);
  218. if (contentView.InternalSubviews.Count < 1) {
  219. CanFocus = false;
  220. }
  221. RemoveMenuStatusBar (view);
  222. }
  223. /// <inheritdoc/>
  224. public override void RemoveAll ()
  225. {
  226. contentView.RemoveAll ();
  227. }
  228. ///<inheritdoc/>
  229. public override void Redraw (Rect bounds)
  230. {
  231. var padding = Border.GetSumThickness ();
  232. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  233. //var borderLength = Border.DrawMarginFrame ? 1 : 0;
  234. // 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?
  235. if (!NeedDisplay.IsEmpty) {
  236. Driver.SetAttribute (GetNormalColor ());
  237. //Driver.DrawWindowFrame (scrRect, padding.Left + borderLength, padding.Top + borderLength, padding.Right + borderLength, padding.Bottom + borderLength,
  238. // Border.BorderStyle != BorderStyle.None, fill: true, Border);
  239. Border.DrawContent ();
  240. }
  241. var savedClip = contentView.ClipToBounds ();
  242. // Redraw our contentView
  243. // TODO: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
  244. contentView.Redraw (contentView.Bounds);
  245. Driver.Clip = savedClip;
  246. ClearLayoutNeeded ();
  247. ClearNeedsDisplay ();
  248. if (Border.BorderStyle != BorderStyle.None) {
  249. Driver.SetAttribute (GetNormalColor ());
  250. //Driver.DrawWindowFrame (scrRect, padding.Left + borderLength, padding.Top + borderLength, padding.Right + borderLength, padding.Bottom + borderLength,
  251. // Border.BorderStyle != BorderStyle.None, fill: true, Border.BorderStyle);
  252. if (HasFocus)
  253. Driver.SetAttribute (ColorScheme.HotNormal);
  254. Driver.DrawWindowTitle (scrRect, Title, padding.Left, padding.Top, padding.Right, padding.Bottom);
  255. }
  256. Driver.SetAttribute (GetNormalColor ());
  257. // Checks if there are any SuperView view which intersect with this window.
  258. if (SuperView != null) {
  259. SuperView.SetNeedsLayout ();
  260. SuperView.SetNeedsDisplay ();
  261. }
  262. }
  263. /// <summary>
  264. /// The text displayed by the <see cref="Label"/>.
  265. /// </summary>
  266. public override ustring Text {
  267. get => contentView.Text;
  268. set {
  269. base.Text = value;
  270. if (contentView != null) {
  271. contentView.Text = value;
  272. }
  273. }
  274. }
  275. /// <summary>
  276. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  277. /// </summary>
  278. /// <value>The text alignment.</value>
  279. public override TextAlignment TextAlignment {
  280. get => contentView.TextAlignment;
  281. set {
  282. base.TextAlignment = contentView.TextAlignment = value;
  283. }
  284. }
  285. }
  286. }