Border.cs 32 KB

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