Window.cs 14 KB

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