Window.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 updates done here should probably be done in FrameView as well; TODO: Merge these classes
  11. using System;
  12. using System.Collections;
  13. using System.Text.Json.Serialization;
  14. using NStack;
  15. using Terminal.Gui.Configuration;
  16. using static Terminal.Gui.Configuration.ConfigurationManager;
  17. namespace Terminal.Gui {
  18. /// <summary>
  19. /// A <see cref="Toplevel"/> <see cref="View"/> that draws a border around its <see cref="View.Frame"/> with a Title at the top.
  20. /// </summary>
  21. /// <remarks>
  22. /// 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
  23. /// API to determine this rectangle.
  24. /// </remarks>
  25. public class Window : Toplevel {
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="Gui.Window"/> class with an optional title using <see cref="LayoutStyle.Absolute"/> positioning.
  28. /// </summary>
  29. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  30. /// <param name="title">Title</param>
  31. /// <remarks>
  32. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  33. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle.Computed"/>.
  34. /// </remarks>
  35. public Window (Rect frame, ustring title = null) : this (frame, title, padding: 0, border: null)
  36. {
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="Window"/> class with an optional title using <see cref="LayoutStyle.Computed"/> positioning.
  40. /// </summary>
  41. /// <param name="title">Title.</param>
  42. /// <remarks>
  43. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  44. /// 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.
  45. /// </remarks>
  46. public Window (ustring title = null) : this (title, padding: 0, border: null)
  47. {
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="Window"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  51. /// </summary>
  52. public Window () : this (title: null) { }
  53. /// <summary>
  54. /// 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,
  55. /// and an optional title.
  56. /// </summary>
  57. /// <param name="frame">Superview-relative rectangle specifying the location and size</param>
  58. /// <param name="title">Title</param>
  59. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  60. /// <param name="border">The <see cref="Border"/>.</param>
  61. /// <remarks>
  62. /// This constructor initializes a Window with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Absolute"/>. Use constructors
  63. /// that do not take <c>Rect</c> parameters to initialize a Window with <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>
  64. /// </remarks>
  65. public Window (Rect frame, ustring title = null, int padding = 0, Border border = null) : base (frame)
  66. {
  67. Initialize (title, frame, padding, border);
  68. }
  69. /// <summary>
  70. /// Initializes a new instance of the <see cref="Window"/> using <see cref="LayoutStyle.Computed"/> positioning,
  71. /// and an optional title.
  72. /// </summary>
  73. /// <param name="title">Title.</param>
  74. /// <param name="padding">Number of characters to use for padding of the drawn frame.</param>
  75. /// <param name="border">The <see cref="Border"/>.</param>
  76. /// <remarks>
  77. /// This constructor initializes a View with a <see cref="LayoutStyle"/> of <see cref="LayoutStyle.Computed"/>.
  78. /// 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.
  79. /// </remarks>
  80. public Window (ustring title = null, int padding = 0, Border border = null) : base ()
  81. {
  82. Initialize (title, Rect.Empty, padding, border);
  83. }
  84. /// <summary>
  85. /// The default <see cref="BorderStyle"/> for <see cref="FrameView"/>. The default is <see cref="BorderStyle.Single"/>.
  86. /// </summary>
  87. /// <remarks>
  88. /// This property can be set in a Theme to change the default <see cref="BorderStyle"/> for all <see cref="Window"/>s.
  89. /// </remarks>
  90. ///[SerializableConfigurationProperty (Scope = typeof (ThemeScope)), JsonConverter (typeof (JsonStringEnumConverter))]
  91. public static BorderStyle DefaultBorderStyle { get; set; } = BorderStyle.Single;
  92. void Initialize (ustring title, Rect frame, int padding = 0, Border border = null)
  93. {
  94. CanFocus = true;
  95. ColorScheme = Colors.Base;
  96. if (title == null) title = ustring.Empty;
  97. Title = title;
  98. if (border == null) {
  99. Border = new Border () {
  100. BorderStyle = DefaultBorderStyle,
  101. Padding = new Thickness (padding),
  102. //Title = title
  103. };
  104. } else {
  105. Border = border;
  106. }
  107. }
  108. /// <inheritdoc/>
  109. public override void Add (View view)
  110. {
  111. base.Add (view);
  112. if (view.CanFocus) {
  113. CanFocus = true;
  114. }
  115. AddMenuStatusBar (view);
  116. }
  117. /// <inheritdoc/>
  118. public override void Remove (View view)
  119. {
  120. if (view == null) {
  121. return;
  122. }
  123. SetNeedsDisplay ();
  124. base.Remove (view);
  125. RemoveMenuStatusBar (view);
  126. }
  127. ///// <inheritdoc/>
  128. //public override void RemoveAll ()
  129. //{
  130. // contentView.RemoveAll ();
  131. //}
  132. /////<inheritdoc/>
  133. //public override void Redraw (Rect bounds)
  134. //{
  135. // var padding = Border.GetSumThickness ();
  136. // var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
  137. // if (!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded) {
  138. // Driver.SetAttribute (GetNormalColor ());
  139. // Clear ();
  140. // contentView.SetNeedsDisplay ();
  141. // }
  142. // var savedClip = contentView.ClipToBounds ();
  143. // // Redraw our contentView
  144. // // DONE: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
  145. // contentView.Redraw (!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded ? contentView.Bounds : bounds);
  146. // Driver.Clip = savedClip;
  147. // ClearLayoutNeeded ();
  148. // ClearNeedsDisplay ();
  149. // Driver.SetAttribute (GetNormalColor ());
  150. // //Driver.DrawWindowFrame (scrRect, padding.Left + borderLength, padding.Top + borderLength, padding.Right + borderLength, padding.Bottom + borderLength,
  151. // // Border.BorderStyle != BorderStyle.None, fill: true, Border.BorderStyle);
  152. // Border.DrawContent (this, false);
  153. // if (HasFocus)
  154. // Driver.SetAttribute (ColorScheme.HotNormal);
  155. // if (Border.DrawMarginFrame)
  156. // Driver.DrawWindowTitle (scrRect, Title, padding.Left, padding.Top, padding.Right, padding.Bottom);
  157. // Driver.SetAttribute (GetNormalColor ());
  158. //}
  159. ///// <inheritdoc/>
  160. //public override void OnCanFocusChanged ()
  161. //{
  162. // if (contentView != null) {
  163. // contentView.CanFocus = CanFocus;
  164. // }
  165. // base.OnCanFocusChanged ();
  166. //}
  167. ///// <summary>
  168. ///// The text displayed by the <see cref="Label"/>.
  169. ///// </summary>
  170. //public override ustring Text {
  171. // get => contentView?.Text;
  172. // set {
  173. // base.Text = value;
  174. // if (contentView != null) {
  175. // contentView.Text = value;
  176. // }
  177. // }
  178. //}
  179. ///// <summary>
  180. ///// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
  181. ///// </summary>
  182. ///// <value>The text alignment.</value>
  183. //public override TextAlignment TextAlignment {
  184. // get => contentView.TextAlignment;
  185. // set {
  186. // base.TextAlignment = contentView.TextAlignment = value;
  187. // }
  188. //}
  189. /// <summary>
  190. /// Event arguments for <see cref="View.Title"/> change events.
  191. /// </summary>
  192. public class TitleEventArgs : EventArgs {
  193. /// <summary>
  194. /// The new Window Title.
  195. /// </summary>
  196. public ustring NewTitle { get; set; }
  197. /// <summary>
  198. /// The old Window Title.
  199. /// </summary>
  200. public ustring OldTitle { get; set; }
  201. /// <summary>
  202. /// Flag which allows canceling the Title change.
  203. /// </summary>
  204. public bool Cancel { get; set; }
  205. /// <summary>
  206. /// Initializes a new instance of <see cref="TitleEventArgs"/>
  207. /// </summary>
  208. /// <param name="oldTitle">The <see cref="View.Title"/> that is/has been replaced.</param>
  209. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  210. public TitleEventArgs (ustring oldTitle, ustring newTitle)
  211. {
  212. OldTitle = oldTitle;
  213. NewTitle = newTitle;
  214. }
  215. }
  216. /// <summary>
  217. /// Called before the <see cref="View.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be cancelled.
  218. /// </summary>
  219. /// <param name="oldTitle">The <see cref="View.Title"/> that is/has been replaced.</param>
  220. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  221. /// <returns>`true` if an event handler canceled the Title change.</returns>
  222. public virtual bool OnTitleChanging (ustring oldTitle, ustring newTitle)
  223. {
  224. var args = new TitleEventArgs (oldTitle, newTitle);
  225. TitleChanging?.Invoke (args);
  226. return args.Cancel;
  227. }
  228. /// <summary>
  229. /// Event fired when the <see cref="View.Title"/> is changing. Set <see cref="TitleEventArgs.Cancel"/> to
  230. /// `true` to cancel the Title change.
  231. /// </summary>
  232. public event Action<TitleEventArgs> TitleChanging;
  233. /// <summary>
  234. /// Called when the <see cref="View.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
  235. /// </summary>
  236. /// <param name="oldTitle">The <see cref="View.Title"/> that is/has been replaced.</param>
  237. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  238. public virtual void OnTitleChanged (ustring oldTitle, ustring newTitle)
  239. {
  240. var args = new TitleEventArgs (oldTitle, newTitle);
  241. TitleChanged?.Invoke (args);
  242. }
  243. /// <summary>
  244. /// Event fired after the <see cref="View.Title"/> has been changed.
  245. /// </summary>
  246. public event Action<TitleEventArgs> TitleChanged;
  247. }
  248. }