Window.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. 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. this.instance = instance;
  84. }
  85. public ContentView (Window instance) : base ()
  86. {
  87. this.instance = instance;
  88. }
  89. public override void OnCanFocusChanged ()
  90. {
  91. if (MostFocused == null && CanFocus && Visible) {
  92. EnsureFocus ();
  93. }
  94. base.OnCanFocusChanged ();
  95. }
  96. public override bool OnMouseEvent (MouseEvent mouseEvent)
  97. {
  98. return instance.OnMouseEvent (mouseEvent);
  99. }
  100. }
  101. /// <summary>
  102. /// Initializes a new instance of the <see cref="Gui.Window"/> class with an optional title using <see cref="LayoutStyle.Absolute"/> positioning.
  103. /// </summary>
  104. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  105. /// <param name="title">Title</param>
  106. /// <remarks>
  107. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  108. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle.Computed"/>.
  109. /// </remarks>
  110. public Window (Rect frame, ustring title = null) : this (frame, title, padding: 0, border: null)
  111. {
  112. }
  113. /// <summary>
  114. /// Initializes a new instance of the <see cref="Window"/> class with an optional title using <see cref="LayoutStyle.Computed"/> positioning.
  115. /// </summary>
  116. /// <param name="title">Title.</param>
  117. /// <remarks>
  118. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  119. /// 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.
  120. /// </remarks>
  121. public Window (ustring title = null) : this (title, padding: 0, border: null)
  122. {
  123. }
  124. /// <summary>
  125. /// Initializes a new instance of the <see cref="Window"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  126. /// </summary>
  127. public Window () : this (title: null) { }
  128. /// <summary>
  129. /// 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,
  130. /// and an optional title.
  131. /// </summary>
  132. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  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 Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  138. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>
  139. /// </remarks>
  140. public Window (Rect frame, ustring title = null, int padding = 0, Border border = null) : base (frame)
  141. {
  142. Initialize (title, frame, padding, border);
  143. }
  144. /// <summary>
  145. /// Initializes a new instance of the <see cref="Window"/> using <see cref="LayoutStyle.Computed"/> positioning,
  146. /// and an optional title.
  147. /// </summary>
  148. /// <param name="title">Title.</param>
  149. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  150. /// <param name="border">The <see cref="Border"/>.</param>
  151. /// <remarks>
  152. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  153. /// 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.
  154. /// </remarks>
  155. public Window (ustring title = null, int padding = 0, Border border = null) : base ()
  156. {
  157. Initialize (title, Rect.Empty, padding, border);
  158. }
  159. void Initialize (ustring title, Rect frame, int padding = 0, Border border = null)
  160. {
  161. CanFocus = true;
  162. ColorScheme = Colors.Base;
  163. if (title == null) title = ustring.Empty;
  164. Title = title;
  165. if (border == null) {
  166. Border = new Border () {
  167. BorderStyle = BorderStyle.Single,
  168. Padding = new Thickness (padding),
  169. Title = title
  170. };
  171. } else {
  172. Border = border;
  173. if (ustring.IsNullOrEmpty (border.Title)) {
  174. border.Title = title;
  175. }
  176. }
  177. AdjustContentView (frame);
  178. }
  179. void AdjustContentView (Rect frame)
  180. {
  181. var borderLength = Border.DrawMarginFrame ? 1 : 0;
  182. var sumPadding = Border.GetSumThickness ();
  183. var wp = new Point ();
  184. var wb = new Size ();
  185. if (frame == Rect.Empty) {
  186. wp.X = borderLength + sumPadding.Left;
  187. wp.Y = borderLength + sumPadding.Top;
  188. wb.Width = borderLength + sumPadding.Right;
  189. wb.Height = borderLength + sumPadding.Bottom;
  190. if (contentView == null) {
  191. contentView = new ContentView (this) {
  192. X = wp.X,
  193. Y = wp.Y,
  194. Width = Dim.Fill (wb.Width),
  195. Height = Dim.Fill (wb.Height)
  196. };
  197. } else {
  198. contentView.X = wp.X;
  199. contentView.Y = wp.Y;
  200. contentView.Width = Dim.Fill (wb.Width);
  201. contentView.Height = Dim.Fill (wb.Height);
  202. }
  203. } else {
  204. wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  205. wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  206. var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  207. if (contentView == null) {
  208. contentView = new ContentView (cFrame, this);
  209. } else {
  210. contentView.Frame = cFrame;
  211. }
  212. }
  213. if (Subviews?.Count == 0)
  214. base.Add (contentView);
  215. Border.Child = contentView;
  216. }
  217. ///// <summary>
  218. ///// Enumerates the various <see cref="View"/>s in the embedded <see cref="ContentView"/>.
  219. ///// </summary>
  220. ///// <returns>The enumerator.</returns>
  221. //public new IEnumerator GetEnumerator ()
  222. //{
  223. // return contentView.GetEnumerator ();
  224. //}
  225. /// <inheritdoc/>
  226. public override void Add (View view)
  227. {
  228. contentView.Add (view);
  229. if (view.CanFocus) {
  230. CanFocus = true;
  231. }
  232. AddMenuStatusBar (view);
  233. }
  234. /// <inheritdoc/>
  235. public override void Remove (View view)
  236. {
  237. if (view == null) {
  238. return;
  239. }
  240. SetNeedsDisplay ();
  241. contentView.Remove (view);
  242. if (contentView.InternalSubviews.Count < 1) {
  243. CanFocus = false;
  244. }
  245. RemoveMenuStatusBar (view);
  246. if (view != contentView && Focused == null) {
  247. FocusFirst ();
  248. }
  249. }
  250. /// <inheritdoc/>
  251. public override void RemoveAll ()
  252. {
  253. contentView.RemoveAll ();
  254. }
  255. ///<inheritdoc/>
  256. public override void Redraw (Rect bounds)
  257. {
  258. if (!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded) {
  259. Driver.SetAttribute (GetNormalColor ());
  260. Clear ();
  261. contentView.SetNeedsDisplay ();
  262. }
  263. var savedClip = contentView.ClipToBounds ();
  264. // Redraw our contentView
  265. contentView.Redraw (!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded ? contentView.Bounds : bounds);
  266. Driver.Clip = savedClip;
  267. ClearLayoutNeeded ();
  268. ClearNeedsDisplay ();
  269. Driver.SetAttribute (GetNormalColor ());
  270. Border.DrawContent (this, false);
  271. }
  272. /// <inheritdoc/>
  273. public override void OnCanFocusChanged ()
  274. {
  275. if (contentView != null) {
  276. contentView.CanFocus = CanFocus;
  277. }
  278. base.OnCanFocusChanged ();
  279. }
  280. /// <summary>
  281. /// The text displayed by the <see cref="Label"/>.
  282. /// </summary>
  283. public override ustring Text {
  284. get => contentView?.Text;
  285. set {
  286. base.Text = value;
  287. if (contentView != null) {
  288. contentView.Text = value;
  289. }
  290. }
  291. }
  292. /// <summary>
  293. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  294. /// </summary>
  295. /// <value>The text alignment.</value>
  296. public override TextAlignment TextAlignment {
  297. get => contentView.TextAlignment;
  298. set {
  299. base.TextAlignment = contentView.TextAlignment = value;
  300. }
  301. }
  302. /// <summary>
  303. /// An <see cref="EventArgs"/> which allows passing a cancelable new <see cref="Title"/> value event.
  304. /// </summary>
  305. public class TitleEventArgs : EventArgs {
  306. /// <summary>
  307. /// The new Window Title.
  308. /// </summary>
  309. public ustring NewTitle { get; set; }
  310. /// <summary>
  311. /// The old Window Title.
  312. /// </summary>
  313. public ustring OldTitle { get; set; }
  314. /// <summary>
  315. /// Flag which allows cancelling the Title change.
  316. /// </summary>
  317. public bool Cancel { get; set; }
  318. /// <summary>
  319. /// Initializes a new instance of <see cref="TitleEventArgs"/>
  320. /// </summary>
  321. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  322. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  323. public TitleEventArgs (ustring oldTitle, ustring newTitle)
  324. {
  325. OldTitle = oldTitle;
  326. NewTitle = newTitle;
  327. }
  328. }
  329. /// <summary>
  330. /// Called before the <see cref="Window.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be cancelled.
  331. /// </summary>
  332. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  333. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  334. /// <returns>`true` if an event handler cancelled the Title change.</returns>
  335. public virtual bool OnTitleChanging (ustring oldTitle, ustring newTitle)
  336. {
  337. var args = new TitleEventArgs (oldTitle, newTitle);
  338. TitleChanging?.Invoke (args);
  339. return args.Cancel;
  340. }
  341. /// <summary>
  342. /// Event fired when the <see cref="Window.Title"/> is changing. Set <see cref="TitleEventArgs.Cancel"/> to
  343. /// `true` to cancel the Title change.
  344. /// </summary>
  345. public event Action<TitleEventArgs> TitleChanging;
  346. /// <summary>
  347. /// Called when the <see cref="Window.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
  348. /// </summary>
  349. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  350. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  351. public virtual void OnTitleChanged (ustring oldTitle, ustring newTitle)
  352. {
  353. var args = new TitleEventArgs (oldTitle, newTitle);
  354. TitleChanged?.Invoke (args);
  355. }
  356. /// <summary>
  357. /// Event fired after the <see cref="Window.Title"/> has been changed.
  358. /// </summary>
  359. public event Action<TitleEventArgs> TitleChanged;
  360. }
  361. }