Window.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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;
  12. using System.Collections;
  13. using NStack;
  14. namespace Terminal.Gui {
  15. /// <summary>
  16. /// 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.
  17. /// </summary>
  18. /// <remarks>
  19. /// 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
  20. /// API to determine this rectangle.
  21. /// </remarks>
  22. public class Window : Toplevel {
  23. View contentView;
  24. ustring title = ustring.Empty;
  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. if (!OnTitleChanging (title, value)) {
  33. var old = title;
  34. title = value;
  35. OnTitleChanged (old, title);
  36. }
  37. SetNeedsDisplay ();
  38. }
  39. }
  40. /// <inheritdoc/>
  41. public override Border Border {
  42. get => base.Border;
  43. set {
  44. if (base.Border != null && base.Border.Child != null && value.Child == null) {
  45. value.Child = base.Border.Child;
  46. }
  47. base.Border = value;
  48. if (value == null) {
  49. return;
  50. }
  51. Rect frame;
  52. if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
  53. frame = Rect.Empty;
  54. } else {
  55. frame = Frame;
  56. }
  57. AdjustContentView (frame);
  58. Border.BorderChanged += Border_BorderChanged;
  59. }
  60. }
  61. void Border_BorderChanged (Border border)
  62. {
  63. Rect frame;
  64. if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
  65. frame = Rect.Empty;
  66. } else {
  67. frame = Frame;
  68. }
  69. AdjustContentView (frame);
  70. }
  71. /// <summary>
  72. /// ContentView is an internal implementation detail of Window. It is used to host Views added with <see cref="Add(View)"/>.
  73. /// Its ONLY reason for being is to provide a simple way for Window to expose to those SubViews that the Window's Bounds
  74. /// are actually deflated due to the border.
  75. /// </summary>
  76. class ContentView : View {
  77. Window instance;
  78. public ContentView (Rect frame, Window instance) : base (frame)
  79. {
  80. this.instance = instance;
  81. }
  82. public ContentView (Window instance) : base ()
  83. {
  84. this.instance = instance;
  85. }
  86. public override void OnCanFocusChanged ()
  87. {
  88. if (MostFocused == null && CanFocus && Visible) {
  89. EnsureFocus ();
  90. }
  91. base.OnCanFocusChanged ();
  92. }
  93. public override bool OnMouseEvent (MouseEvent mouseEvent)
  94. {
  95. return instance.OnMouseEvent (mouseEvent);
  96. }
  97. }
  98. /// <summary>
  99. /// Initializes a new instance of the <see cref="Gui.Window"/> class with an optional title using <see cref="LayoutStyle.Absolute"/> positioning.
  100. /// </summary>
  101. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  102. /// <param name="title">Title</param>
  103. /// <remarks>
  104. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  105. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle.Computed"/>.
  106. /// </remarks>
  107. public Window (Rect frame, ustring title = null) : this (frame, title, padding: 0, border: null)
  108. {
  109. }
  110. /// <summary>
  111. /// Initializes a new instance of the <see cref="Window"/> class with an optional title using <see cref="LayoutStyle.Computed"/> positioning.
  112. /// </summary>
  113. /// <param name="title">Title.</param>
  114. /// <remarks>
  115. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  116. /// 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.
  117. /// </remarks>
  118. public Window (ustring title = null) : this (title, padding: 0, border: null)
  119. {
  120. }
  121. /// <summary>
  122. /// Initializes a new instance of the <see cref="Window"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  123. /// </summary>
  124. public Window () : this (title: null) { }
  125. /// <summary>
  126. /// 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,
  127. /// and an optional title.
  128. /// </summary>
  129. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  130. /// <param name="title">Title</param>
  131. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  132. /// <param name="border">The <see cref="Border"/>.</param>
  133. /// <remarks>
  134. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  135. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>
  136. /// </remarks>
  137. public Window (Rect frame, ustring title = null, int padding = 0, Border border = null) : base (frame)
  138. {
  139. Initialize (title, frame, padding, border);
  140. }
  141. /// <summary>
  142. /// Initializes a new instance of the <see cref="Window"/> using <see cref="LayoutStyle.Computed"/> positioning,
  143. /// and an optional title.
  144. /// </summary>
  145. /// <param name="title">Title.</param>
  146. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  147. /// <param name="border">The <see cref="Border"/>.</param>
  148. /// <remarks>
  149. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  150. /// 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.
  151. /// </remarks>
  152. public Window (ustring title = null, int padding = 0, Border border = null) : base ()
  153. {
  154. Initialize (title, Rect.Empty, padding, border);
  155. }
  156. void Initialize (ustring title, Rect frame, int padding = 0, Border border = null)
  157. {
  158. CanFocus = true;
  159. ColorScheme = Colors.Base;
  160. if (title == null) title = ustring.Empty;
  161. Title = title;
  162. if (border == null) {
  163. Border = new Border () {
  164. BorderStyle = BorderStyle.Single,
  165. Padding = new Thickness (padding),
  166. BorderBrush = ColorScheme.Normal.Background
  167. };
  168. } else {
  169. Border = border;
  170. }
  171. AdjustContentView (frame);
  172. }
  173. void AdjustContentView (Rect frame)
  174. {
  175. var borderLength = Border.DrawMarginFrame ? 1 : 0;
  176. var sumPadding = Border.GetSumThickness ();
  177. var wp = new Point ();
  178. var wb = new Size ();
  179. if (frame == Rect.Empty) {
  180. wp.X = borderLength + sumPadding.Left;
  181. wp.Y = borderLength + sumPadding.Top;
  182. wb.Width = borderLength + sumPadding.Right;
  183. wb.Height = borderLength + sumPadding.Bottom;
  184. if (contentView == null) {
  185. contentView = new ContentView (this) {
  186. X = wp.X,
  187. Y = wp.Y,
  188. Width = Dim.Fill (wb.Width),
  189. Height = Dim.Fill (wb.Height)
  190. };
  191. } else {
  192. contentView.X = wp.X;
  193. contentView.Y = wp.Y;
  194. contentView.Width = Dim.Fill (wb.Width);
  195. contentView.Height = Dim.Fill (wb.Height);
  196. }
  197. } else {
  198. wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  199. wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  200. var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  201. if (contentView == null) {
  202. contentView = new ContentView (cFrame, this);
  203. } else {
  204. contentView.Frame = cFrame;
  205. }
  206. }
  207. if (Subviews?.Count == 0)
  208. base.Add (contentView);
  209. Border.Child = contentView;
  210. }
  211. ///// <summary>
  212. ///// Enumerates the various <see cref="View"/>s in the embedded <see cref="ContentView"/>.
  213. ///// </summary>
  214. ///// <returns>The enumerator.</returns>
  215. //public new IEnumerator GetEnumerator ()
  216. //{
  217. // return contentView.GetEnumerator ();
  218. //}
  219. /// <inheritdoc/>
  220. public override void Add (View view)
  221. {
  222. contentView.Add (view);
  223. if (view.CanFocus) {
  224. CanFocus = true;
  225. }
  226. AddMenuStatusBar (view);
  227. }
  228. /// <inheritdoc/>
  229. public override void Remove (View view)
  230. {
  231. if (view == null) {
  232. return;
  233. }
  234. SetNeedsDisplay ();
  235. contentView.Remove (view);
  236. if (contentView.InternalSubviews.Count < 1) {
  237. CanFocus = false;
  238. }
  239. RemoveMenuStatusBar (view);
  240. if (view != contentView && Focused == null) {
  241. FocusFirst ();
  242. }
  243. }
  244. /// <inheritdoc/>
  245. public override void RemoveAll ()
  246. {
  247. contentView.RemoveAll ();
  248. }
  249. ///<inheritdoc/>
  250. public override void Redraw (Rect bounds)
  251. {
  252. var padding = Border.GetSumThickness ();
  253. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  254. if (!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded) {
  255. Driver.SetAttribute (GetNormalColor ());
  256. Clear ();
  257. contentView.SetNeedsDisplay ();
  258. }
  259. var savedClip = contentView.ClipToBounds ();
  260. // Redraw our contentView
  261. // DONE: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
  262. contentView.Redraw (!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded ? contentView.Bounds : bounds);
  263. Driver.Clip = savedClip;
  264. ClearLayoutNeeded ();
  265. ClearNeedsDisplay ();
  266. Driver.SetAttribute (GetNormalColor ());
  267. //Driver.DrawWindowFrame (scrRect, padding.Left + borderLength, padding.Top + borderLength, padding.Right + borderLength, padding.Bottom + borderLength,
  268. // Border.BorderStyle != BorderStyle.None, fill: true, Border.BorderStyle);
  269. Border.DrawContent (this, false);
  270. if (HasFocus)
  271. Driver.SetAttribute (ColorScheme.HotNormal);
  272. if (Border.DrawMarginFrame)
  273. Driver.DrawWindowTitle (scrRect, Title, padding.Left, padding.Top, padding.Right, padding.Bottom);
  274. Driver.SetAttribute (GetNormalColor ());
  275. }
  276. /// <inheritdoc/>
  277. public override void OnCanFocusChanged ()
  278. {
  279. if (contentView != null) {
  280. contentView.CanFocus = CanFocus;
  281. }
  282. base.OnCanFocusChanged ();
  283. }
  284. /// <summary>
  285. /// The text displayed by the <see cref="Label"/>.
  286. /// </summary>
  287. public override ustring Text {
  288. get => contentView?.Text;
  289. set {
  290. base.Text = value;
  291. if (contentView != null) {
  292. contentView.Text = value;
  293. }
  294. }
  295. }
  296. /// <summary>
  297. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  298. /// </summary>
  299. /// <value>The text alignment.</value>
  300. public override TextAlignment TextAlignment {
  301. get => contentView.TextAlignment;
  302. set {
  303. base.TextAlignment = contentView.TextAlignment = value;
  304. }
  305. }
  306. /// <summary>
  307. /// An <see cref="EventArgs"/> which allows passing a cancelable new <see cref="Title"/> value event.
  308. /// </summary>
  309. public class TitleEventArgs : EventArgs {
  310. /// <summary>
  311. /// The new Window Title.
  312. /// </summary>
  313. public ustring NewTitle { get; set; }
  314. /// <summary>
  315. /// The old Window Title.
  316. /// </summary>
  317. public ustring OldTitle { get; set; }
  318. /// <summary>
  319. /// Flag which allows cancelling the Title change.
  320. /// </summary>
  321. public bool Cancel { get; set; }
  322. /// <summary>
  323. /// Initializes a new instance of <see cref="TitleEventArgs"/>
  324. /// </summary>
  325. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  326. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  327. public TitleEventArgs (ustring oldTitle, ustring newTitle)
  328. {
  329. OldTitle = oldTitle;
  330. NewTitle = newTitle;
  331. }
  332. }
  333. /// <summary>
  334. /// Called before the <see cref="Window.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be cancelled.
  335. /// </summary>
  336. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  337. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  338. /// <returns>`true` if an event handler cancelled the Title change.</returns>
  339. public virtual bool OnTitleChanging (ustring oldTitle, ustring newTitle)
  340. {
  341. var args = new TitleEventArgs (oldTitle, newTitle);
  342. TitleChanging?.Invoke (args);
  343. return args.Cancel;
  344. }
  345. /// <summary>
  346. /// Event fired when the <see cref="Window.Title"/> is changing. Set <see cref="TitleEventArgs.Cancel"/> to
  347. /// `true` to cancel the Title change.
  348. /// </summary>
  349. public event Action<TitleEventArgs> TitleChanging;
  350. /// <summary>
  351. /// Called when the <see cref="Window.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
  352. /// </summary>
  353. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  354. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  355. public virtual void OnTitleChanged (ustring oldTitle, ustring newTitle)
  356. {
  357. var args = new TitleEventArgs (oldTitle, newTitle);
  358. TitleChanged?.Invoke (args);
  359. }
  360. /// <summary>
  361. /// Event fired after the <see cref="Window.Title"/> has been changed.
  362. /// </summary>
  363. public event Action<TitleEventArgs> TitleChanged;
  364. }
  365. }