Border.cs 33 KB

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