Border.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using Terminal.Gui.Graphs;
  5. using System.Text.Json.Serialization;
  6. namespace Terminal.Gui {
  7. /// <summary>
  8. /// Specifies the border style for a <see cref="View"/> and to be used by the <see cref="Border"/> class.
  9. /// </summary>
  10. public enum BorderStyle {
  11. /// <summary>
  12. /// No border is drawn.
  13. /// </summary>
  14. None,
  15. /// <summary>
  16. /// The border is drawn with a single line limits.
  17. /// </summary>
  18. Single,
  19. /// <summary>
  20. /// The border is drawn with a double line limits.
  21. /// </summary>
  22. Double,
  23. /// <summary>
  24. /// The border is drawn with a single line and rounded corners limits.
  25. /// </summary>
  26. Rounded
  27. }
  28. /// <summary>
  29. /// Describes the thickness of a frame around a rectangle. Four <see cref="int"/> values describe
  30. /// the <see cref="Left"/>, <see cref="Top"/>, <see cref="Right"/>, and <see cref="Bottom"/> sides
  31. /// of the rectangle, respectively.
  32. /// </summary>
  33. public class Thickness : IEquatable<Thickness> {
  34. private int validate (int width)
  35. {
  36. if (width < 0) {
  37. throw new ArgumentException ("Thickness widths cannot be negative.");
  38. }
  39. return width;
  40. }
  41. /// <summary>
  42. /// Gets or sets the width of the left side of the rectangle.
  43. /// </summary>
  44. [JsonInclude]
  45. public int Left;
  46. /// <summary>
  47. /// Gets or sets the width of the upper side of the rectangle.
  48. /// </summary>
  49. [JsonInclude]
  50. public int Top;
  51. /// <summary>
  52. /// Gets or sets the width of the right side of the rectangle.
  53. /// </summary>
  54. [JsonInclude]
  55. public int Right;
  56. /// <summary>
  57. /// Gets or sets the width of the lower side of the rectangle.
  58. /// </summary>
  59. [JsonInclude]
  60. public int Bottom;
  61. /// <summary>
  62. /// Initializes a new instance of the <see cref="Thickness"/> class with all widths
  63. /// set to 0.
  64. /// </summary>
  65. public Thickness () { }
  66. /// <summary>
  67. /// Initializes a new instance of the <see cref="Thickness"/> class with a uniform width to each side.
  68. /// </summary>
  69. /// <param name="width"></param>
  70. public Thickness (int width) : this (width, width, width, width) { }
  71. /// <summary>
  72. /// Initializes a new instance of the <see cref="Thickness"/> class that has specific
  73. /// widths applied to each side of the rectangle.
  74. /// </summary>
  75. /// <param name="left"></param>
  76. /// <param name="top"></param>
  77. /// <param name="right"></param>
  78. /// <param name="bottom"></param>
  79. public Thickness (int left, int top, int right, int bottom)
  80. {
  81. Left = left;
  82. Top = top;
  83. Right = right;
  84. Bottom = bottom;
  85. }
  86. /// <summary>
  87. /// Returns a rectangle describing the location and size of the inner area of <paramref name="rect"/>
  88. /// with the thickness widths subracted. The height and width of the retunred rect may be zero.
  89. /// </summary>
  90. /// <param name="rect">The source rectangle</param>
  91. /// <returns></returns>
  92. public Rect GetInnerRect (Rect rect)
  93. {
  94. var width = rect.Size.Width - (Left + Right);
  95. var height = rect.Size.Height - (Top + Bottom);
  96. var size = new Size (Math.Max (0, width), Math.Max (0, height));
  97. return new Rect (new Point (rect.X + Left, rect.Y + Top), size);
  98. }
  99. /// <summary>
  100. /// Draws the thickness rectangle with an optional diagnostics label.
  101. /// </summary>
  102. /// <param name="rect">The location and size of the rectangle that bounds the thickness rectangle, in
  103. /// screen coordinates.</param>
  104. /// <param name="label">The diagnostics label to draw on the bottom of the <see cref="Bottom"/>.</param>
  105. /// <returns>The inner rectangle remaining to be drawn.</returns>
  106. public Rect Draw (Rect rect, string label = null)
  107. {
  108. // Draw the Top side
  109. for (var r = rect.Y; r < Math.Min (rect.Y + rect.Height, rect.Y + Top); r++) {
  110. for (var c = rect.X; c < rect.X + rect.Width; c++) {
  111. Application.Driver.Move (c, r);
  112. Application.Driver.AddRune (' ');
  113. }
  114. }
  115. // Draw the Left side
  116. for (var r = rect.Y; r < rect.Y + rect.Height; r++) {
  117. for (var c = rect.X; c < Math.Min (rect.X + rect.Width, rect.X + Left); c++) {
  118. Application.Driver.Move (c, r);
  119. Application.Driver.AddRune (' ');
  120. }
  121. }
  122. // Draw the Right side
  123. for (var r = rect.Y; r < rect.Y + rect.Height; r++) {
  124. for (var c = rect.X + Math.Max (0, rect.Width - Right); c < rect.X + rect.Width; c++) {
  125. Application.Driver.Move (c, r);
  126. Application.Driver.AddRune (' ');
  127. }
  128. }
  129. // Draw the Bottom side
  130. for (var r = rect.Y + Math.Max (0, rect.Height - Bottom); r < rect.Y + rect.Height; r++) {
  131. for (var c = rect.X; c < rect.X + rect.Width; c++) {
  132. Application.Driver.Move (c, r);
  133. Application.Driver.AddRune (' ');
  134. }
  135. }
  136. // Draw the diagnostics label on the bottom
  137. var tf = new TextFormatter () {
  138. Text = label == null ? string.Empty : $"{label} {this}",
  139. Alignment = TextAlignment.Centered,
  140. VerticalAlignment = VerticalTextAlignment.Bottom
  141. };
  142. tf.Draw (rect, Application.Driver.CurrentAttribute, Application.Driver.CurrentAttribute);
  143. return GetInnerRect (rect);
  144. }
  145. // TODO: add operator overloads
  146. /// <summary>
  147. /// Gets an empty thickness.
  148. /// </summary>
  149. public static Thickness Empty => new Thickness (0);
  150. public override bool Equals (object obj)
  151. {
  152. //Check for null and compare run-time types.
  153. if ((obj == null) || !this.GetType ().Equals (obj.GetType ())) {
  154. return false;
  155. } else {
  156. return Equals ((Thickness)obj);
  157. }
  158. }
  159. /// <summary>Returns the thickness widths of the Thickness formatted as a string.</summary>
  160. /// <returns>The thickness widths as a string.</returns>
  161. public override string ToString ()
  162. {
  163. return $"(Left={Left},Top={Top},Right={Right},Bottom={Bottom})";
  164. }
  165. // IEquitable
  166. public bool Equals (Thickness other)
  167. {
  168. return other is not null &&
  169. Left == other.Left &&
  170. Right == other.Right &&
  171. Top == other.Top &&
  172. Bottom == other.Bottom;
  173. }
  174. public override int GetHashCode ()
  175. {
  176. int hashCode = 1380952125;
  177. hashCode = hashCode * -1521134295 + Left.GetHashCode ();
  178. hashCode = hashCode * -1521134295 + Right.GetHashCode ();
  179. hashCode = hashCode * -1521134295 + Top.GetHashCode ();
  180. hashCode = hashCode * -1521134295 + Bottom.GetHashCode ();
  181. return hashCode;
  182. }
  183. public static bool operator == (Thickness left, Thickness right)
  184. {
  185. return EqualityComparer<Thickness>.Default.Equals (left, right);
  186. }
  187. public static bool operator != (Thickness left, Thickness right)
  188. {
  189. return !(left == right);
  190. }
  191. }
  192. /// <summary>
  193. /// Draws a border, background, or both around another element.
  194. /// </summary>
  195. public class Border {
  196. private int marginFrame => DrawMarginFrame ? 1 : 0;
  197. ///// <summary>
  198. ///// A sealed <see cref="Toplevel"/> derived class to implement <see cref="Border"/> feature.
  199. ///// This is only a wrapper to get borders on a toplevel and is recommended using another
  200. ///// derived, like <see cref="Window"/> where is possible to have borders with or without
  201. ///// border line or spacing around.
  202. ///// </summary>
  203. //public sealed class ToplevelContainer : Toplevel {
  204. // /// <inheritdoc/>
  205. // public override Border Border {
  206. // get => base.Border;
  207. // set {
  208. // if (base.Border != null && base.Border.Child != null && value.Child == null) {
  209. // value.Child = base.Border.Child;
  210. // }
  211. // base.Border = value;
  212. // if (value == null) {
  213. // return;
  214. // }
  215. // Rect frame;
  216. // if (Border.Child != null && (Border.Child.Width is Dim || Border.Child.Height is Dim)) {
  217. // frame = Rect.Empty;
  218. // } else {
  219. // frame = Frame;
  220. // }
  221. // AdjustContentView (frame);
  222. // Border.BorderChanged += Border_BorderChanged;
  223. // }
  224. // }
  225. // void Border_BorderChanged (Border border)
  226. // {
  227. // Rect frame;
  228. // if (Border.Child != null && (Border.Child.Width is Dim || Border.Child.Height is Dim)) {
  229. // frame = Rect.Empty;
  230. // } else {
  231. // frame = Frame;
  232. // }
  233. // AdjustContentView (frame);
  234. // }
  235. // /// <summary>
  236. // /// Initializes with default null values.
  237. // /// </summary>
  238. // public ToplevelContainer () : this (null, string.Empty) { }
  239. // /// <summary>
  240. // /// Initializes a <see cref="ToplevelContainer"/> with a <see cref="LayoutStyle.Computed"/>
  241. // /// </summary>
  242. // /// <param name="border">The border.</param>
  243. // /// <param name="title">The title.</param>
  244. // public ToplevelContainer (Border border, string title = null)
  245. // {
  246. // Initialize (Rect.Empty, border, title ?? string.Empty);
  247. // }
  248. // /// <summary>
  249. // /// Initializes a <see cref="ToplevelContainer"/> with a <see cref="LayoutStyle.Absolute"/>
  250. // /// </summary>
  251. // /// <param name="frame">The frame.</param>
  252. // /// <param name="border">The border.</param>
  253. // /// <param name="title">The title.</param>
  254. // public ToplevelContainer (Rect frame, Border border, string title = null) : base (frame)
  255. // {
  256. // Initialize (frame, border, title ?? string.Empty);
  257. // }
  258. // private void Initialize (Rect frame, Border border, string title)
  259. // {
  260. // ColorScheme = Colors.TopLevel;
  261. // if (border == null) {
  262. // Border = new Border () {
  263. // BorderStyle = BorderStyle.Single,
  264. // BorderBrush = ColorScheme.Normal.Background,
  265. // Title = (ustring)title
  266. // };
  267. // } else {
  268. // Border = border;
  269. // }
  270. // AdjustContentView (frame);
  271. // }
  272. // void AdjustContentView (Rect frame)
  273. // {
  274. // var borderLength = Border.DrawMarginFrame ? 1 : 0;
  275. // var sumPadding = Border.GetSumThickness ();
  276. // var wp = new Point ();
  277. // var wb = new Size ();
  278. // if (frame == Rect.Empty) {
  279. // wp.X = borderLength + sumPadding.Left;
  280. // wp.Y = borderLength + sumPadding.Top;
  281. // wb.Width = borderLength + sumPadding.Right;
  282. // wb.Height = borderLength + sumPadding.Bottom;
  283. // if (Border.Child == null) {
  284. // Border.Child = new ChildContentView (this) {
  285. // X = wp.X,
  286. // Y = wp.Y,
  287. // Width = Dim.Fill (wb.Width),
  288. // Height = Dim.Fill (wb.Height)
  289. // };
  290. // } else {
  291. // Border.Child.X = wp.X;
  292. // Border.Child.Y = wp.Y;
  293. // Border.Child.Width = Dim.Fill (wb.Width);
  294. // Border.Child.Height = Dim.Fill (wb.Height);
  295. // }
  296. // } else {
  297. // wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  298. // wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  299. // var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  300. // if (Border.Child == null) {
  301. // Border.Child = new ChildContentView (cFrame, this);
  302. // } else {
  303. // Border.Child.Frame = cFrame;
  304. // }
  305. // }
  306. // if (Subviews?.Count == 0)
  307. // base.Add (Border.Child);
  308. // Border.ChildContainer = this;
  309. // }
  310. // /// <inheritdoc/>
  311. // public override void Add (View view)
  312. // {
  313. // Border.Child.Add (view);
  314. // if (view.CanFocus) {
  315. // CanFocus = true;
  316. // }
  317. // AddMenuStatusBar (view);
  318. // }
  319. // /// <inheritdoc/>
  320. // public override void Remove (View view)
  321. // {
  322. // if (view == null) {
  323. // return;
  324. // }
  325. // SetNeedsDisplay ();
  326. // var touched = view.Frame;
  327. // Border.Child.Remove (view);
  328. // if (Border.Child.InternalSubviews.Count < 1) {
  329. // CanFocus = false;
  330. // }
  331. // RemoveMenuStatusBar (view);
  332. // }
  333. // /// <inheritdoc/>
  334. // public override void RemoveAll ()
  335. // {
  336. // Border.Child.RemoveAll ();
  337. // }
  338. // /// <inheritdoc/>
  339. // public override void Redraw (Rect bounds)
  340. // {
  341. // if (!NeedDisplay.IsEmpty) {
  342. // Driver.SetAttribute (GetNormalColor ());
  343. // Clear ();
  344. // }
  345. // var savedClip = Border.Child.ClipToBounds ();
  346. // Border.Child.Redraw (Border.Child.Bounds);
  347. // Driver.Clip = savedClip;
  348. // ClearLayoutNeeded ();
  349. // ClearNeedsDisplay ();
  350. // Driver.SetAttribute (GetNormalColor ());
  351. // Border.DrawContent (this, false);
  352. // if (HasFocus)
  353. // Driver.SetAttribute (ColorScheme.HotNormal);
  354. // if (Border.DrawMarginFrame) {
  355. // if (!ustring.IsNullOrEmpty (Border.Title))
  356. // Border.DrawTitle (this);
  357. // else
  358. // Border.DrawTitle (this, Frame);
  359. // }
  360. // Driver.SetAttribute (GetNormalColor ());
  361. // // Checks if there are any SuperView view which intersect with this window.
  362. // if (SuperView != null) {
  363. // SuperView.SetNeedsLayout ();
  364. // SuperView.SetNeedsDisplay ();
  365. // }
  366. // }
  367. // /// <inheritdoc/>
  368. // public override void OnCanFocusChanged ()
  369. // {
  370. // if (Border.Child != null) {
  371. // Border.Child.CanFocus = CanFocus;
  372. // }
  373. // base.OnCanFocusChanged ();
  374. // }
  375. //}
  376. //private class ChildContentView : View {
  377. // View instance;
  378. // public ChildContentView (Rect frame, View instance) : base (frame)
  379. // {
  380. // this.instance = instance;
  381. // }
  382. // public ChildContentView (View instance)
  383. // {
  384. // this.instance = instance;
  385. // }
  386. // public override bool MouseEvent (MouseEvent mouseEvent)
  387. // {
  388. // return instance.MouseEvent (mouseEvent);
  389. // }
  390. //}
  391. /// <summary>
  392. /// Invoked when any property of Border changes (except <see cref="Child"/>).
  393. /// </summary>
  394. public event Action<Border> BorderChanged;
  395. private BorderStyle borderStyle;
  396. private bool drawMarginFrame;
  397. private Thickness borderThickness = new Thickness (0);
  398. private Color borderBrush;
  399. private Color background;
  400. private Thickness padding = new Thickness (0);
  401. private bool effect3D;
  402. private Point effect3DOffset = new Point (1, 1);
  403. private Attribute? effect3DBrush;
  404. private ustring title = ustring.Empty;
  405. /// <summary>
  406. /// Specifies the <see cref="Gui.BorderStyle"/> for a view.
  407. /// </summary>
  408. [JsonInclude, JsonConverter (typeof(JsonStringEnumConverter))]
  409. public BorderStyle BorderStyle {
  410. get => borderStyle;
  411. set {
  412. if (value != BorderStyle.None && !drawMarginFrame) {
  413. // Ensures drawn the border lines.
  414. drawMarginFrame = true;
  415. }
  416. borderStyle = value;
  417. OnBorderChanged ();
  418. }
  419. }
  420. /// <summary>
  421. /// Gets or sets if a margin frame is drawn around the <see cref="Child"/> regardless the <see cref="BorderStyle"/>
  422. /// </summary>
  423. [JsonInclude]
  424. public bool DrawMarginFrame {
  425. get => drawMarginFrame;
  426. set {
  427. if (borderStyle != BorderStyle.None
  428. && (!value || !drawMarginFrame)) {
  429. // Ensures drawn the border lines.
  430. drawMarginFrame = true;
  431. } else {
  432. drawMarginFrame = value;
  433. }
  434. OnBorderChanged ();
  435. }
  436. }
  437. /// <summary>
  438. /// Gets or sets the relative <see cref="Thickness"/> of a <see cref="Border"/>.
  439. /// </summary>
  440. [JsonInclude]
  441. public Thickness BorderThickness {
  442. get => borderThickness;
  443. set {
  444. borderThickness = value;
  445. OnBorderChanged ();
  446. }
  447. }
  448. /// <summary>
  449. /// Gets or sets the <see cref="Color"/> that draws the outer border color.
  450. /// </summary>
  451. [JsonInclude, JsonConverter (typeof (Configuration.ColorJsonConverter))]
  452. public Color BorderBrush {
  453. get => borderBrush;
  454. set {
  455. borderBrush = value;
  456. OnBorderChanged ();
  457. }
  458. }
  459. /// <summary>
  460. /// Gets or sets the <see cref="Color"/> that fills the area between the bounds of a <see cref="Border"/>.
  461. /// </summary>
  462. [JsonInclude, JsonConverter (typeof (Configuration.ColorJsonConverter))]
  463. public Color Background {
  464. get => background;
  465. set {
  466. background = value;
  467. OnBorderChanged ();
  468. }
  469. }
  470. /// <summary>
  471. /// Gets or sets a <see cref="Thickness"/> value that describes the amount of space between a
  472. /// <see cref="Border"/> and its child element.
  473. /// </summary>
  474. [JsonInclude]
  475. public Thickness Padding {
  476. get => padding;
  477. set {
  478. padding = value;
  479. OnBorderChanged ();
  480. }
  481. }
  482. ///// <summary>
  483. ///// Gets the rendered width of this element.
  484. ///// </summary>
  485. //[JsonIgnore]
  486. //public int ActualWidth {
  487. // get {
  488. // var driver = Application.Driver;
  489. // if (Parent?.Border == null) {
  490. // return Math.Min (Child?.Frame.Width + (2 * marginFrame) + Padding.Right
  491. // + BorderThickness.Right + Padding.Left + BorderThickness.Left ?? 0, driver.Cols);
  492. // }
  493. // return Math.Min (Parent.Frame.Width, driver.Cols);
  494. // }
  495. //}
  496. ///// <summary>
  497. ///// Gets the rendered height of this element.
  498. ///// </summary>
  499. //[JsonIgnore]
  500. //public int ActualHeight {
  501. // get {
  502. // var driver = Application.Driver;
  503. // if (Parent?.Border == null) {
  504. // return Math.Min (Child?.Frame.Height + (2 * marginFrame) + Padding.Bottom
  505. // + BorderThickness.Bottom + Padding.Top + BorderThickness.Top ?? 0, driver.Rows);
  506. // }
  507. // return Math.Min (Parent.Frame.Height, driver.Rows);
  508. // }
  509. //}
  510. ///// <summary>
  511. ///// Gets or sets the single child element of a <see cref="View"/>.
  512. ///// </summary>
  513. //[JsonIgnore]
  514. //public View Child { get; set; }
  515. ///// <summary>
  516. ///// Gets the parent <see cref="Child"/> parent if any.
  517. ///// </summary>
  518. //[JsonIgnore]
  519. //public View Parent { get => Child?.SuperView; }
  520. ///// <summary>
  521. ///// Gets or private sets by the <see cref="ToplevelContainer"/>
  522. ///// </summary>
  523. //[JsonIgnore]
  524. //public ToplevelContainer ChildContainer { get; private set; }
  525. /// <summary>
  526. /// Gets or sets the 3D effect around the <see cref="Border"/>.
  527. /// </summary>
  528. [JsonInclude]
  529. public bool Effect3D {
  530. get => effect3D;
  531. set {
  532. effect3D = value;
  533. OnBorderChanged ();
  534. }
  535. }
  536. /// <summary>
  537. /// Get or sets the offset start position for the <see cref="Effect3D"/>
  538. /// </summary>
  539. [JsonInclude]
  540. public Point Effect3DOffset {
  541. get => effect3DOffset;
  542. set {
  543. effect3DOffset = value;
  544. OnBorderChanged ();
  545. }
  546. }
  547. /// <summary>
  548. /// Gets or sets the color for the <see cref="Border"/>
  549. /// </summary>
  550. [JsonInclude, JsonConverter (typeof (Configuration.AttributeJsonConverter))]
  551. public Attribute? Effect3DBrush {
  552. get {
  553. if (effect3DBrush == null && effect3D) {
  554. return effect3DBrush = new Attribute (Color.Gray, Color.DarkGray);
  555. } else {
  556. return effect3DBrush;
  557. }
  558. }
  559. set {
  560. effect3DBrush = value;
  561. OnBorderChanged ();
  562. }
  563. }
  564. ///// <summary>
  565. ///// The title to be displayed for this view.
  566. ///// </summary>
  567. //[JsonIgnore]
  568. //public ustring Title {
  569. // get => title;
  570. // set {
  571. // title = value;
  572. // OnBorderChanged ();
  573. // }
  574. //}
  575. ///// <summary>
  576. ///// Calculate the sum of the <see cref="Padding"/> and the <see cref="BorderThickness"/>
  577. ///// </summary>
  578. ///// <returns>The total of the <see cref="Border"/> <see cref="Thickness"/></returns>
  579. //public Thickness GetSumThickness ()
  580. //{
  581. // return new Thickness () {
  582. // Left = Padding.Left + BorderThickness.Left,
  583. // Top = Padding.Top + BorderThickness.Top,
  584. // Right = Padding.Right + BorderThickness.Right,
  585. // Bottom = Padding.Bottom + BorderThickness.Bottom
  586. // };
  587. //}
  588. ///// <summary>
  589. ///// Drawn the <see cref="BorderThickness"/> more the <see cref="Padding"/>
  590. ///// more the <see cref="Border.BorderStyle"/> and the <see cref="Effect3D"/>.
  591. ///// </summary>
  592. ///// <param name="view">The view to draw.</param>
  593. ///// <param name="fill">If it will clear or not the content area.</param>
  594. //public void DrawContent (View view = null, bool fill = true)
  595. //{
  596. // if (Child == null) {
  597. // Child = view;
  598. // }
  599. // if (Parent?.Border != null) {
  600. // DrawParentBorder (Parent.ViewToScreen (Parent.Bounds), fill);
  601. // } else {
  602. // DrawChildBorder (Child.ViewToScreen (Child.Bounds), fill);
  603. // }
  604. //}
  605. ///// <summary>
  606. ///// Same as <see cref="DrawContent"/> but drawing full frames for all borders.
  607. ///// </summary>
  608. //public void DrawFullContent ()
  609. //{
  610. // var borderThickness = BorderThickness;
  611. // var padding = Padding;
  612. // var marginFrame = DrawMarginFrame ? 1 : 0;
  613. // var driver = Application.Driver;
  614. // Rect scrRect;
  615. // if (Parent?.Border != null) {
  616. // scrRect = Parent.ViewToScreen (Parent.Bounds);
  617. // } else {
  618. // scrRect = Child.ViewToScreen (Child.Bounds);
  619. // }
  620. // Rect borderRect;
  621. // if (Parent?.Border != null) {
  622. // borderRect = scrRect;
  623. // } else {
  624. // borderRect = new Rect () {
  625. // X = scrRect.X - marginFrame - padding.Left - borderThickness.Left,
  626. // Y = scrRect.Y - marginFrame - padding.Top - borderThickness.Top,
  627. // Width = ActualWidth,
  628. // Height = ActualHeight
  629. // };
  630. // }
  631. // var savedAttribute = driver.GetAttribute ();
  632. // // Draw 3D effects
  633. // if (Effect3D) {
  634. // driver.SetAttribute ((Attribute)Effect3DBrush);
  635. // var effectBorder = new Rect () {
  636. // X = borderRect.X + Effect3DOffset.X,
  637. // Y = borderRect.Y + Effect3DOffset.Y,
  638. // Width = ActualWidth,
  639. // Height = ActualHeight
  640. // };
  641. // //Child.Clear (effectBorder);
  642. // for (int r = effectBorder.Y; r < Math.Min (effectBorder.Bottom, driver.Rows); r++) {
  643. // for (int c = effectBorder.X; c < Math.Min (effectBorder.Right, driver.Cols); c++) {
  644. // AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  645. // }
  646. // }
  647. // }
  648. // // Draw border thickness
  649. // driver.SetAttribute (new Attribute (BorderBrush));
  650. // Child.Clear (borderRect);
  651. // borderRect = new Rect () {
  652. // X = borderRect.X + borderThickness.Left,
  653. // Y = borderRect.Y + borderThickness.Top,
  654. // Width = Math.Max (borderRect.Width - borderThickness.Right - borderThickness.Left, 0),
  655. // Height = Math.Max (borderRect.Height - borderThickness.Bottom - borderThickness.Top, 0)
  656. // };
  657. // if (borderRect != scrRect) {
  658. // // Draw padding
  659. // driver.SetAttribute (new Attribute (Background));
  660. // Child.Clear (borderRect);
  661. // }
  662. // driver.SetAttribute (savedAttribute);
  663. // // Draw margin frame
  664. // if (DrawMarginFrame) {
  665. // if (Parent?.Border != null) {
  666. // var sumPadding = GetSumThickness ();
  667. // borderRect = new Rect () {
  668. // X = scrRect.X + sumPadding.Left,
  669. // Y = scrRect.Y + sumPadding.Top,
  670. // Width = Math.Max (scrRect.Width - sumPadding.Right - sumPadding.Left, 0),
  671. // Height = Math.Max (scrRect.Height - sumPadding.Bottom - sumPadding.Top, 0)
  672. // };
  673. // } else {
  674. // borderRect = new Rect () {
  675. // X = borderRect.X + padding.Left,
  676. // Y = borderRect.Y + padding.Top,
  677. // Width = Math.Max (borderRect.Width - padding.Right - padding.Left, 0),
  678. // Height = Math.Max (borderRect.Height - padding.Bottom - padding.Top, 0)
  679. // };
  680. // }
  681. // if (borderRect.Width > 0 && borderRect.Height > 0) {
  682. // driver.DrawWindowFrame (borderRect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill: true, this);
  683. // }
  684. // }
  685. //}
  686. //private void DrawChildBorder (Rect frame, bool fill = true)
  687. //{
  688. // var drawMarginFrame = DrawMarginFrame ? 1 : 0;
  689. // var sumThickness = GetSumThickness ();
  690. // var padding = Padding;
  691. // var effect3DOffset = Effect3DOffset;
  692. // var driver = Application.Driver;
  693. // var savedAttribute = driver.GetAttribute ();
  694. // driver.SetAttribute (new Attribute (BorderBrush));
  695. // // Draw the upper BorderThickness
  696. // for (int r = frame.Y - drawMarginFrame - sumThickness.Top;
  697. // r < frame.Y - drawMarginFrame - padding.Top; r++) {
  698. // for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  699. // c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  700. // AddRuneAt (driver, c, r, ' ');
  701. // }
  702. // }
  703. // // Draw the left BorderThickness
  704. // for (int r = frame.Y - drawMarginFrame - padding.Top;
  705. // r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  706. // for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  707. // c < frame.X - drawMarginFrame - padding.Left; c++) {
  708. // AddRuneAt (driver, c, r, ' ');
  709. // }
  710. // }
  711. // // Draw the right BorderThickness
  712. // for (int r = frame.Y - drawMarginFrame - padding.Top;
  713. // r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  714. // for (int c = frame.Right + drawMarginFrame + padding.Right;
  715. // c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  716. // AddRuneAt (driver, c, r, ' ');
  717. // }
  718. // }
  719. // // Draw the lower BorderThickness
  720. // for (int r = frame.Bottom + drawMarginFrame + padding.Bottom;
  721. // r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom, driver.Rows); r++) {
  722. // for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  723. // c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  724. // AddRuneAt (driver, c, r, ' ');
  725. // }
  726. // }
  727. // driver.SetAttribute (new Attribute (Background));
  728. // // Draw the upper Padding
  729. // for (int r = frame.Y - drawMarginFrame - padding.Top;
  730. // r < frame.Y - drawMarginFrame; r++) {
  731. // for (int c = frame.X - drawMarginFrame - padding.Left;
  732. // c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  733. // AddRuneAt (driver, c, r, ' ');
  734. // }
  735. // }
  736. // // Draw the left Padding
  737. // for (int r = frame.Y - drawMarginFrame;
  738. // r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  739. // for (int c = frame.X - drawMarginFrame - padding.Left;
  740. // c < frame.X - drawMarginFrame; c++) {
  741. // AddRuneAt (driver, c, r, ' ');
  742. // }
  743. // }
  744. // // Draw the right Padding
  745. // for (int r = frame.Y - drawMarginFrame;
  746. // r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  747. // for (int c = frame.Right + drawMarginFrame;
  748. // c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  749. // AddRuneAt (driver, c, r, ' ');
  750. // }
  751. // }
  752. // // Draw the lower Padding
  753. // for (int r = frame.Bottom + drawMarginFrame;
  754. // r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  755. // for (int c = frame.X - drawMarginFrame - padding.Left;
  756. // c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  757. // AddRuneAt (driver, c, r, ' ');
  758. // }
  759. // }
  760. // driver.SetAttribute (savedAttribute);
  761. // // Draw the MarginFrame
  762. // if (DrawMarginFrame) {
  763. // var rect = new Rect () {
  764. // X = frame.X - drawMarginFrame,
  765. // Y = frame.Y - drawMarginFrame,
  766. // Width = frame.Width + (2 * drawMarginFrame),
  767. // Height = frame.Height + (2 * drawMarginFrame)
  768. // };
  769. // if (rect.Width > 0 && rect.Height > 0) {
  770. // driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  771. // DrawTitle (Child);
  772. // }
  773. // //var rect = Child.ViewToScreen (new Rect (-1, -1, Child.Frame.Width + 2, Child.Frame.Height + 2));
  774. // //if (rect.Width > 0 && rect.Height > 0) {
  775. // // var lc = new LineCanvas ();
  776. // // lc.AddLine (rect.Location, rect.Width-1, Orientation.Horizontal, BorderStyle);
  777. // // lc.AddLine (rect.Location, rect.Height-1, Orientation.Vertical, BorderStyle);
  778. // // lc.AddLine (new Point (rect.X, rect.Y + rect.Height-1), rect.Width, Orientation.Horizontal, BorderStyle);
  779. // // lc.AddLine (new Point (rect.X + rect.Width-1, rect.Y), rect.Height, Orientation.Vertical, BorderStyle);
  780. // // //driver.SetAttribute (new Attribute(Color.Red, Color.BrightYellow));
  781. // // foreach (var p in lc.GenerateImage (rect)) {
  782. // // AddRuneAt (driver, p.Key.X, p.Key.Y, p.Value);
  783. // // }
  784. // // DrawTitle (Child);
  785. // //}
  786. // }
  787. // if (Effect3D) {
  788. // driver.SetAttribute ((Attribute)Effect3DBrush);
  789. // // Draw the upper Effect3D
  790. // for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  791. // r >= 0 && r < frame.Y - drawMarginFrame - sumThickness.Top; r++) {
  792. // for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  793. // c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  794. // AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  795. // }
  796. // }
  797. // // Draw the left Effect3D
  798. // for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  799. // r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  800. // for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  801. // c >= 0 && c < frame.X - drawMarginFrame - sumThickness.Left; c++) {
  802. // AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  803. // }
  804. // }
  805. // // Draw the right Effect3D
  806. // for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  807. // r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  808. // for (int c = frame.Right + drawMarginFrame + sumThickness.Right;
  809. // c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  810. // AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  811. // }
  812. // }
  813. // // Draw the lower Effect3D
  814. // for (int r = frame.Bottom + drawMarginFrame + sumThickness.Bottom;
  815. // r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  816. // for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  817. // c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  818. // AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  819. // }
  820. // }
  821. // }
  822. // driver.SetAttribute (savedAttribute);
  823. //}
  824. //private void DrawParentBorder (Rect frame, bool fill = true)
  825. //{
  826. // var sumThickness = GetSumThickness ();
  827. // var borderThickness = BorderThickness;
  828. // var effect3DOffset = Effect3DOffset;
  829. // var driver = Application.Driver;
  830. // var savedAttribute = driver.GetAttribute ();
  831. // driver.SetAttribute (new Attribute (BorderBrush));
  832. // // Draw the upper BorderThickness
  833. // for (int r = frame.Y;
  834. // r < Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r++) {
  835. // for (int c = frame.X;
  836. // c < Math.Min (frame.Right, driver.Cols); c++) {
  837. // AddRuneAt (driver, c, r, ' ');
  838. // }
  839. // }
  840. // // Draw the left BorderThickness
  841. // for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  842. // r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  843. // for (int c = frame.X;
  844. // c < Math.Min (frame.X + borderThickness.Left, frame.Right); c++) {
  845. // AddRuneAt (driver, c, r, ' ');
  846. // }
  847. // }
  848. // // Draw the right BorderThickness
  849. // for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  850. // r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  851. // for (int c = Math.Max (frame.Right - borderThickness.Right, frame.X);
  852. // c < Math.Min (frame.Right, driver.Cols); c++) {
  853. // AddRuneAt (driver, c, r, ' ');
  854. // }
  855. // }
  856. // // Draw the lower BorderThickness
  857. // for (int r = Math.Max (frame.Bottom - borderThickness.Bottom, frame.Y);
  858. // r < Math.Min (frame.Bottom, driver.Rows); r++) {
  859. // for (int c = frame.X;
  860. // c < Math.Min (frame.Right, driver.Cols); c++) {
  861. // AddRuneAt (driver, c, r, ' ');
  862. // }
  863. // }
  864. // driver.SetAttribute (new Attribute (Background));
  865. // // Draw the upper Padding
  866. // for (int r = frame.Y + borderThickness.Top;
  867. // r < Math.Min (frame.Y + sumThickness.Top, frame.Bottom - borderThickness.Bottom); r++) {
  868. // for (int c = frame.X + borderThickness.Left;
  869. // c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  870. // AddRuneAt (driver, c, r, ' ');
  871. // }
  872. // }
  873. // // Draw the left Padding
  874. // for (int r = frame.Y + sumThickness.Top;
  875. // r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  876. // for (int c = frame.X + borderThickness.Left;
  877. // c < Math.Min (frame.X + sumThickness.Left, frame.Right - borderThickness.Right); c++) {
  878. // AddRuneAt (driver, c, r, ' ');
  879. // }
  880. // }
  881. // // Draw the right Padding
  882. // for (int r = frame.Y + sumThickness.Top;
  883. // r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  884. // for (int c = Math.Max (frame.Right - sumThickness.Right, frame.X + sumThickness.Left);
  885. // c < Math.Max (frame.Right - borderThickness.Right, frame.X + sumThickness.Left); c++) {
  886. // AddRuneAt (driver, c, r, ' ');
  887. // }
  888. // }
  889. // // Draw the lower Padding
  890. // for (int r = Math.Max (frame.Bottom - sumThickness.Bottom, frame.Y + borderThickness.Top);
  891. // r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  892. // for (int c = frame.X + borderThickness.Left;
  893. // c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  894. // AddRuneAt (driver, c, r, ' ');
  895. // }
  896. // }
  897. // driver.SetAttribute (savedAttribute);
  898. // // Draw the MarginFrame
  899. // if (DrawMarginFrame) {
  900. // var rect = new Rect () {
  901. // X = frame.X + sumThickness.Left,
  902. // Y = frame.Y + sumThickness.Top,
  903. // Width = Math.Max (frame.Width - sumThickness.Right - sumThickness.Left, 0),
  904. // Height = Math.Max (frame.Height - sumThickness.Bottom - sumThickness.Top, 0)
  905. // };
  906. // if (rect.Width > 0 && rect.Height > 0) {
  907. // driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  908. // DrawTitle (Parent);
  909. // }
  910. // }
  911. // if (Effect3D) {
  912. // driver.SetAttribute ((Attribute)Effect3DBrush);
  913. // // Draw the upper Effect3D
  914. // for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  915. // r < frame.Y; r++) {
  916. // for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  917. // c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  918. // AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  919. // }
  920. // }
  921. // // Draw the left Effect3D
  922. // for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  923. // r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  924. // for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  925. // c < frame.X; c++) {
  926. // AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  927. // }
  928. // }
  929. // // Draw the right Effect3D
  930. // for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  931. // r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  932. // for (int c = frame.Right;
  933. // c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  934. // AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  935. // }
  936. // }
  937. // // Draw the lower Effect3D
  938. // for (int r = frame.Bottom;
  939. // r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  940. // for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  941. // c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  942. // AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  943. // }
  944. // }
  945. // }
  946. // driver.SetAttribute (savedAttribute);
  947. //}
  948. //private void AddRuneAt (ConsoleDriver driver, int col, int row, Rune ch)
  949. //{
  950. // if (col < driver.Cols && row < driver.Rows && col > 0 && driver.Contents [row, col, 2] == 0
  951. // && Rune.ColumnWidth ((char)driver.Contents [row, col - 1, 0]) > 1) {
  952. // driver.Contents [row, col, 1] = driver.GetAttribute ();
  953. // return;
  954. // }
  955. // driver.Move (col, row);
  956. // driver.AddRune (ch);
  957. //}
  958. ///// <summary>
  959. ///// Draws the view <see cref="Title"/> to the screen.
  960. ///// </summary>
  961. ///// <param name="view">The view.</param>
  962. //public void DrawTitle (View view)
  963. //{
  964. // var driver = Application.Driver;
  965. // if (DrawMarginFrame) {
  966. // driver.SetAttribute (Child.GetNormalColor ());
  967. // if (Child.HasFocus)
  968. // driver.SetAttribute (Child.ColorScheme.HotNormal);
  969. // var padding = view.Border.GetSumThickness ();
  970. // Rect scrRect;
  971. // if (view == Child) {
  972. // scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width + 2, view.Frame.Height + 2));
  973. // scrRect = new Rect (scrRect.X - 1, scrRect.Y - 1, scrRect.Width, scrRect.Height);
  974. // driver.DrawWindowTitle (scrRect, Title, 0, 0, 0, 0);
  975. // } else {
  976. // scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width, view.Frame.Height));
  977. // driver.DrawWindowTitle (scrRect, Title,
  978. // padding.Left, padding.Top, padding.Right, padding.Bottom);
  979. // }
  980. // }
  981. // driver.SetAttribute (Child.GetNormalColor ());
  982. //}
  983. ///// <summary>
  984. ///// Draws the <see cref="View.Text"/> to the screen.
  985. ///// </summary>
  986. ///// <param name="view">The view.</param>
  987. ///// <param name="rect">The frame.</param>
  988. //public void DrawTitle (View view, Rect rect)
  989. //{
  990. // var driver = Application.Driver;
  991. // if (DrawMarginFrame) {
  992. // driver.SetAttribute (view.GetNormalColor ());
  993. // if (view.HasFocus) {
  994. // driver.SetAttribute (view.ColorScheme.HotNormal);
  995. // }
  996. // var padding = Parent.Border.GetSumThickness ();
  997. // var scrRect = Parent.ViewToScreen (new Rect (0, 0, rect.Width, rect.Height));
  998. // driver.DrawWindowTitle (scrRect, view.Text,
  999. // padding.Left, padding.Top, padding.Right, padding.Bottom);
  1000. // }
  1001. // driver.SetAttribute (view.GetNormalColor ());
  1002. //}
  1003. /// <summary>
  1004. /// Invoke the <see cref="BorderChanged"/> event.
  1005. /// </summary>
  1006. public virtual void OnBorderChanged ()
  1007. {
  1008. BorderChanged?.Invoke (this);
  1009. }
  1010. }
  1011. }