Window.cs 14 KB

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