Window.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. }
  172. void AdjustContentView (Rect frame)
  173. {
  174. var borderLength = Border.DrawMarginFrame ? 1 : 0;
  175. var sumPadding = Border.GetSumThickness ();
  176. var wb = new Size ();
  177. if (frame == Rect.Empty) {
  178. wb.Width = borderLength + sumPadding.Right;
  179. wb.Height = borderLength + sumPadding.Bottom;
  180. if (contentView == null) {
  181. contentView = new ContentView (this) {
  182. X = borderLength + sumPadding.Left,
  183. Y = borderLength + sumPadding.Top,
  184. Width = Dim.Fill (wb.Width),
  185. Height = Dim.Fill (wb.Height)
  186. };
  187. } else {
  188. contentView.X = borderLength + sumPadding.Left;
  189. contentView.Y = borderLength + sumPadding.Top;
  190. contentView.Width = Dim.Fill (wb.Width);
  191. contentView.Height = Dim.Fill (wb.Height);
  192. }
  193. } else {
  194. wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  195. wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  196. var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  197. if (contentView == null) {
  198. contentView = new ContentView (cFrame, this);
  199. } else {
  200. contentView.Frame = cFrame;
  201. }
  202. }
  203. base.Add (contentView);
  204. Border.Child = contentView;
  205. }
  206. ///// <summary>
  207. ///// Enumerates the various <see cref="View"/>s in the embedded <see cref="ContentView"/>.
  208. ///// </summary>
  209. ///// <returns>The enumerator.</returns>
  210. //public new IEnumerator GetEnumerator ()
  211. //{
  212. // return contentView.GetEnumerator ();
  213. //}
  214. /// <inheritdoc/>
  215. public override void Add (View view)
  216. {
  217. contentView.Add (view);
  218. if (view.CanFocus) {
  219. CanFocus = true;
  220. }
  221. AddMenuStatusBar (view);
  222. }
  223. /// <inheritdoc/>
  224. public override void Remove (View view)
  225. {
  226. if (view == null) {
  227. return;
  228. }
  229. SetNeedsDisplay ();
  230. contentView.Remove (view);
  231. if (contentView.InternalSubviews.Count < 1) {
  232. CanFocus = false;
  233. }
  234. RemoveMenuStatusBar (view);
  235. if (view != contentView && Focused == null) {
  236. FocusFirst ();
  237. }
  238. }
  239. /// <inheritdoc/>
  240. public override void RemoveAll ()
  241. {
  242. contentView.RemoveAll ();
  243. }
  244. ///<inheritdoc/>
  245. public override void Redraw (Rect bounds)
  246. {
  247. var padding = Border.GetSumThickness ();
  248. var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  249. //var borderLength = Border.DrawMarginFrame ? 1 : 0;
  250. // 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?
  251. if (!NeedDisplay.IsEmpty) {
  252. Driver.SetAttribute (GetNormalColor ());
  253. Clear ();
  254. }
  255. var savedClip = contentView.ClipToBounds ();
  256. // Redraw our contentView
  257. // DONE: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
  258. contentView.Redraw (!NeedDisplay.IsEmpty ? contentView.Bounds : bounds);
  259. Driver.Clip = savedClip;
  260. ClearLayoutNeeded ();
  261. ClearNeedsDisplay ();
  262. if (Border.BorderStyle != BorderStyle.None) {
  263. Driver.SetAttribute (GetNormalColor ());
  264. //Driver.DrawWindowFrame (scrRect, padding.Left + borderLength, padding.Top + borderLength, padding.Right + borderLength, padding.Bottom + borderLength,
  265. // Border.BorderStyle != BorderStyle.None, fill: true, Border.BorderStyle);
  266. Border.DrawContent (this, false);
  267. if (HasFocus)
  268. Driver.SetAttribute (ColorScheme.HotNormal);
  269. Driver.DrawWindowTitle (scrRect, Title, padding.Left, padding.Top, padding.Right, padding.Bottom);
  270. }
  271. Driver.SetAttribute (GetNormalColor ());
  272. // Checks if there are any SuperView view which intersect with this window.
  273. if (SuperView != null) {
  274. SuperView.SetNeedsLayout ();
  275. SuperView.SetNeedsDisplay ();
  276. }
  277. }
  278. /// <inheritdoc/>
  279. public override void OnCanFocusChanged ()
  280. {
  281. if (contentView != null) {
  282. contentView.CanFocus = CanFocus;
  283. }
  284. base.OnCanFocusChanged ();
  285. }
  286. /// <summary>
  287. /// The text displayed by the <see cref="Label"/>.
  288. /// </summary>
  289. public override ustring Text {
  290. get => contentView.Text;
  291. set {
  292. base.Text = value;
  293. if (contentView != null) {
  294. contentView.Text = value;
  295. }
  296. }
  297. }
  298. /// <summary>
  299. /// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  300. /// </summary>
  301. /// <value>The text alignment.</value>
  302. public override TextAlignment TextAlignment {
  303. get => contentView.TextAlignment;
  304. set {
  305. base.TextAlignment = contentView.TextAlignment = value;
  306. }
  307. }
  308. /// <summary>
  309. /// An <see cref="EventArgs"/> which allows passing a cancelable new <see cref="Title"/> value event.
  310. /// </summary>
  311. public class TitleEventArgs : EventArgs {
  312. /// <summary>
  313. /// The new Window Title.
  314. /// </summary>
  315. public ustring NewTitle { get; set; }
  316. /// <summary>
  317. /// The old Window Title.
  318. /// </summary>
  319. public ustring OldTitle { get; set; }
  320. /// <summary>
  321. /// Flag which allows cancelling the Title change.
  322. /// </summary>
  323. public bool Cancel { get; set; }
  324. /// <summary>
  325. /// Initializes a new instance of <see cref="TitleEventArgs"/>
  326. /// </summary>
  327. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  328. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  329. public TitleEventArgs (ustring oldTitle, ustring newTitle)
  330. {
  331. OldTitle = oldTitle;
  332. NewTitle = newTitle;
  333. }
  334. }
  335. /// <summary>
  336. /// Called before the <see cref="Window.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be cancelled.
  337. /// </summary>
  338. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  339. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  340. /// <returns>`true` if an event handler cancelled the Title change.</returns>
  341. public virtual bool OnTitleChanging (ustring oldTitle, ustring newTitle)
  342. {
  343. var args = new TitleEventArgs (oldTitle, newTitle);
  344. TitleChanging?.Invoke (args);
  345. return args.Cancel;
  346. }
  347. /// <summary>
  348. /// Event fired when the <see cref="Window.Title"/> is changing. Set <see cref="TitleEventArgs.Cancel"/> to
  349. /// `true` to cancel the Title change.
  350. /// </summary>
  351. public event Action<TitleEventArgs> TitleChanging;
  352. /// <summary>
  353. /// Called when the <see cref="Window.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
  354. /// </summary>
  355. /// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
  356. /// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
  357. public virtual void OnTitleChanged (ustring oldTitle, ustring newTitle)
  358. {
  359. var args = new TitleEventArgs (oldTitle, newTitle);
  360. TitleChanged?.Invoke (args);
  361. }
  362. /// <summary>
  363. /// Event fired after the <see cref="Window.Title"/> has been changed.
  364. /// </summary>
  365. public event Action<TitleEventArgs> TitleChanged;
  366. }
  367. }