Window.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. //var borderLength = Border.DrawMarginFrame ? 1 : 0;
  255. // FIXED: Why do we draw the frame twice? This call is here to clear the content area, I think. Why not just clear that area?
  256. if (!NeedDisplay.IsEmpty) {
  257. Driver.SetAttribute (GetNormalColor ());
  258. Clear ();
  259. }
  260. var savedClip = contentView.ClipToBounds ();
  261. // Redraw our contentView
  262. // DONE: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
  263. contentView.Redraw (!NeedDisplay.IsEmpty ? contentView.Bounds : bounds);
  264. Driver.Clip = savedClip;
  265. ClearLayoutNeeded ();
  266. ClearNeedsDisplay ();
  267. Driver.SetAttribute (GetNormalColor ());
  268. //Driver.DrawWindowFrame (scrRect, padding.Left + borderLength, padding.Top + borderLength, padding.Right + borderLength, padding.Bottom + borderLength,
  269. // Border.BorderStyle != BorderStyle.None, fill: true, Border.BorderStyle);
  270. Border.DrawContent (this, false);
  271. if (HasFocus)
  272. Driver.SetAttribute (ColorScheme.HotNormal);
  273. if (Border.DrawMarginFrame)
  274. Driver.DrawWindowTitle (scrRect, Title, padding.Left, padding.Top, padding.Right, padding.Bottom);
  275. Driver.SetAttribute (GetNormalColor ());
  276. }
  277. /// <inheritdoc/>
  278. public override void OnCanFocusChanged ()
  279. {
  280. if (contentView != null) {
  281. contentView.CanFocus = CanFocus;
  282. }
  283. base.OnCanFocusChanged ();
  284. }
  285. /// <summary>
  286. /// The text displayed by the <see cref="Label"/>.
  287. /// </summary>
  288. public override ustring Text {
  289. get => contentView?.Text;
  290. set {
  291. base.Text = value;
  292. if (contentView != null) {
  293. contentView.Text = value;
  294. }
  295. }
  296. }
  297. /// <summary>
  298. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  299. /// </summary>
  300. /// <value>The text alignment.</value>
  301. public override TextAlignment TextAlignment {
  302. get => contentView.TextAlignment;
  303. set {
  304. base.TextAlignment = contentView.TextAlignment = value;
  305. }
  306. }
  307. /// <summary>
  308. /// An <see cref="EventArgs"/> which allows passing a cancelable new <see cref="Title"/> value event.
  309. /// </summary>
  310. public class TitleEventArgs : EventArgs {
  311. /// <summary>
  312. /// The new Window Title.
  313. /// </summary>
  314. public ustring NewTitle { get; set; }
  315. /// <summary>
  316. /// The old Window Title.
  317. /// </summary>
  318. public ustring OldTitle { get; set; }
  319. /// <summary>
  320. /// Flag which allows cancelling the Title change.
  321. /// </summary>
  322. public bool Cancel { get; set; }
  323. /// <summary>
  324. /// Initializes a new instance of <see cref="TitleEventArgs"/>
  325. /// </summary>
  326. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  327. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  328. public TitleEventArgs (ustring oldTitle, ustring newTitle)
  329. {
  330. OldTitle = oldTitle;
  331. NewTitle = newTitle;
  332. }
  333. }
  334. /// <summary>
  335. /// Called before the <see cref="Window.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be cancelled.
  336. /// </summary>
  337. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  338. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  339. /// <returns>`true` if an event handler cancelled the Title change.</returns>
  340. public virtual bool OnTitleChanging (ustring oldTitle, ustring newTitle)
  341. {
  342. var args = new TitleEventArgs (oldTitle, newTitle);
  343. TitleChanging?.Invoke (args);
  344. return args.Cancel;
  345. }
  346. /// <summary>
  347. /// Event fired when the <see cref="Window.Title"/> is changing. Set <see cref="TitleEventArgs.Cancel"/> to
  348. /// `true` to cancel the Title change.
  349. /// </summary>
  350. public event Action<TitleEventArgs> TitleChanging;
  351. /// <summary>
  352. /// Called when the <see cref="Window.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
  353. /// </summary>
  354. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  355. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  356. public virtual void OnTitleChanged (ustring oldTitle, ustring newTitle)
  357. {
  358. var args = new TitleEventArgs (oldTitle, newTitle);
  359. TitleChanged?.Invoke (args);
  360. }
  361. /// <summary>
  362. /// Event fired after the <see cref="Window.Title"/> has been changed.
  363. /// </summary>
  364. public event Action<TitleEventArgs> TitleChanged;
  365. }
  366. }