Window.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. 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. }
  238. AddMenuStatusBar (view);
  239. }
  240. /// <inheritdoc/>
  241. public override void Remove (View view)
  242. {
  243. if (view == null) {
  244. return;
  245. }
  246. SetNeedsDisplay ();
  247. contentView.Remove (view);
  248. RemoveMenuStatusBar (view);
  249. if (view != contentView && Focused == null) {
  250. FocusFirst ();
  251. }
  252. }
  253. /// <inheritdoc/>
  254. public override void RemoveAll ()
  255. {
  256. contentView.RemoveAll ();
  257. }
  258. ///<inheritdoc/>
  259. public override void Redraw (Rect bounds)
  260. {
  261. if (!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded) {
  262. Driver.SetAttribute (GetNormalColor ());
  263. Clear ();
  264. contentView.SetNeedsDisplay ();
  265. }
  266. var savedClip = contentView.ClipToBounds ();
  267. // Redraw our contentView
  268. contentView.Redraw (!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded ? contentView.Bounds : bounds);
  269. Driver.Clip = savedClip;
  270. ClearLayoutNeeded ();
  271. ClearNeedsDisplay ();
  272. Driver.SetAttribute (GetNormalColor ());
  273. Border.DrawContent (this, false);
  274. }
  275. /// <inheritdoc/>
  276. public override void OnCanFocusChanged ()
  277. {
  278. if (contentView != null) {
  279. contentView.CanFocus = CanFocus;
  280. }
  281. base.OnCanFocusChanged ();
  282. }
  283. /// <summary>
  284. /// The text displayed by the <see cref="Label"/>.
  285. /// </summary>
  286. public override ustring Text {
  287. get => contentView?.Text;
  288. set {
  289. base.Text = value;
  290. if (contentView != null) {
  291. contentView.Text = value;
  292. }
  293. }
  294. }
  295. /// <summary>
  296. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  297. /// </summary>
  298. /// <value>The text alignment.</value>
  299. public override TextAlignment TextAlignment {
  300. get => contentView.TextAlignment;
  301. set {
  302. base.TextAlignment = contentView.TextAlignment = value;
  303. }
  304. }
  305. /// <summary>
  306. /// An <see cref="EventArgs"/> which allows passing a cancelable new <see cref="Title"/> value event.
  307. /// </summary>
  308. public class TitleEventArgs : EventArgs {
  309. /// <summary>
  310. /// The new Window Title.
  311. /// </summary>
  312. public ustring NewTitle { get; set; }
  313. /// <summary>
  314. /// The old Window Title.
  315. /// </summary>
  316. public ustring OldTitle { get; set; }
  317. /// <summary>
  318. /// Flag which allows cancelling the Title change.
  319. /// </summary>
  320. public bool Cancel { get; set; }
  321. /// <summary>
  322. /// Initializes a new instance of <see cref="TitleEventArgs"/>
  323. /// </summary>
  324. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  325. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  326. public TitleEventArgs (ustring oldTitle, ustring newTitle)
  327. {
  328. OldTitle = oldTitle;
  329. NewTitle = newTitle;
  330. }
  331. }
  332. /// <summary>
  333. /// Called before the <see cref="Window.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be cancelled.
  334. /// </summary>
  335. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  336. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  337. /// <returns>`true` if an event handler cancelled the Title change.</returns>
  338. public virtual bool OnTitleChanging (ustring oldTitle, ustring newTitle)
  339. {
  340. var args = new TitleEventArgs (oldTitle, newTitle);
  341. TitleChanging?.Invoke (args);
  342. return args.Cancel;
  343. }
  344. /// <summary>
  345. /// Event fired when the <see cref="Window.Title"/> is changing. Set <see cref="TitleEventArgs.Cancel"/> to
  346. /// `true` to cancel the Title change.
  347. /// </summary>
  348. public event Action<TitleEventArgs> TitleChanging;
  349. /// <summary>
  350. /// Called when the <see cref="Window.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
  351. /// </summary>
  352. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  353. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  354. public virtual void OnTitleChanged (ustring oldTitle, ustring newTitle)
  355. {
  356. var args = new TitleEventArgs (oldTitle, newTitle);
  357. TitleChanged?.Invoke (args);
  358. }
  359. /// <summary>
  360. /// Event fired after the <see cref="Window.Title"/> has been changed.
  361. /// </summary>
  362. public event Action<TitleEventArgs> TitleChanged;
  363. }
  364. }