Window.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. CanFocus = true;
  147. ColorScheme = Colors.Base;
  148. Title = title;
  149. if (border == null) {
  150. Border = new Border () {
  151. BorderStyle = BorderStyle.Single,
  152. Padding = new Thickness (padding),
  153. BorderBrush = ColorScheme.Normal.Background
  154. };
  155. } else {
  156. Border = border;
  157. }
  158. }
  159. void AdjustContentView (Rect frame)
  160. {
  161. var borderLength = Border.DrawMarginFrame ? 1 : 0;
  162. var sumPadding = Border.GetSumThickness ();
  163. var wb = new Size ();
  164. if (frame == Rect.Empty) {
  165. wb.Width = borderLength + sumPadding.Right;
  166. wb.Height = borderLength + sumPadding.Bottom;
  167. if (contentView == null) {
  168. contentView = new ContentView (this) {
  169. X = borderLength + sumPadding.Left,
  170. Y = borderLength + sumPadding.Top,
  171. Width = Dim.Fill (wb.Width),
  172. Height = Dim.Fill (wb.Height)
  173. };
  174. } else {
  175. contentView.X = borderLength + sumPadding.Left;
  176. contentView.Y = borderLength + sumPadding.Top;
  177. contentView.Width = Dim.Fill (wb.Width);
  178. contentView.Height = Dim.Fill (wb.Height);
  179. }
  180. } else {
  181. wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  182. wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  183. var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  184. if (contentView == null) {
  185. contentView = new ContentView (cFrame, this);
  186. } else {
  187. contentView.Frame = cFrame;
  188. }
  189. }
  190. base.Add (contentView);
  191. Border.Child = contentView;
  192. }
  193. ///// <summary>
  194. ///// Enumerates the various <see cref="View"/>s in the embedded <see cref="ContentView"/>.
  195. ///// </summary>
  196. ///// <returns>The enumerator.</returns>
  197. //public new IEnumerator GetEnumerator ()
  198. //{
  199. // return contentView.GetEnumerator ();
  200. //}
  201. /// <inheritdoc/>
  202. public override void Add (View view)
  203. {
  204. contentView.Add (view);
  205. if (view.CanFocus) {
  206. CanFocus = true;
  207. }
  208. AddMenuStatusBar (view);
  209. }
  210. /// <inheritdoc/>
  211. public override void Remove (View view)
  212. {
  213. if (view == null) {
  214. return;
  215. }
  216. SetNeedsDisplay ();
  217. contentView.Remove (view);
  218. if (contentView.InternalSubviews.Count < 1) {
  219. CanFocus = false;
  220. }
  221. RemoveMenuStatusBar (view);
  222. if (view != contentView && Focused == null) {
  223. FocusFirst ();
  224. }
  225. }
  226. /// <inheritdoc/>
  227. public override void RemoveAll ()
  228. {
  229. contentView.RemoveAll ();
  230. }
  231. ///<inheritdoc/>
  232. public override void Redraw (Rect bounds)
  233. {
  234. var padding = Border.GetSumThickness ();
  235. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  236. //var borderLength = Border.DrawMarginFrame ? 1 : 0;
  237. // 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?
  238. if (!NeedDisplay.IsEmpty) {
  239. Driver.SetAttribute (GetNormalColor ());
  240. //Driver.DrawWindowFrame (scrRect, padding.Left + borderLength, padding.Top + borderLength, padding.Right + borderLength, padding.Bottom + borderLength,
  241. // Border.BorderStyle != BorderStyle.None, fill: true, Border);
  242. Border.DrawContent ();
  243. }
  244. var savedClip = contentView.ClipToBounds ();
  245. // Redraw our contentView
  246. // TODO: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
  247. contentView.Redraw (contentView.Bounds);
  248. Driver.Clip = savedClip;
  249. ClearLayoutNeeded ();
  250. ClearNeedsDisplay ();
  251. if (Border.BorderStyle != BorderStyle.None) {
  252. Driver.SetAttribute (GetNormalColor ());
  253. //Driver.DrawWindowFrame (scrRect, padding.Left + borderLength, padding.Top + borderLength, padding.Right + borderLength, padding.Bottom + borderLength,
  254. // Border.BorderStyle != BorderStyle.None, fill: true, Border.BorderStyle);
  255. if (HasFocus)
  256. Driver.SetAttribute (ColorScheme.HotNormal);
  257. Driver.DrawWindowTitle (scrRect, Title, padding.Left, padding.Top, padding.Right, padding.Bottom);
  258. }
  259. Driver.SetAttribute (GetNormalColor ());
  260. // Checks if there are any SuperView view which intersect with this window.
  261. if (SuperView != null) {
  262. SuperView.SetNeedsLayout ();
  263. SuperView.SetNeedsDisplay ();
  264. }
  265. }
  266. /// <inheritdoc/>
  267. public override void OnCanFocusChanged ()
  268. {
  269. if (contentView != null) {
  270. contentView.CanFocus = CanFocus;
  271. }
  272. base.OnCanFocusChanged ();
  273. }
  274. /// <summary>
  275. /// The text displayed by the <see cref="Label"/>.
  276. /// </summary>
  277. public override ustring Text {
  278. get => contentView.Text;
  279. set {
  280. base.Text = value;
  281. if (contentView != null) {
  282. contentView.Text = value;
  283. }
  284. }
  285. }
  286. /// <summary>
  287. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  288. /// </summary>
  289. /// <value>The text alignment.</value>
  290. public override TextAlignment TextAlignment {
  291. get => contentView.TextAlignment;
  292. set {
  293. base.TextAlignment = contentView.TextAlignment = value;
  294. }
  295. }
  296. }
  297. }