Border.cs 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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. BorderBrush = ColorScheme.Normal.Background,
  157. Title = (ustring)title
  158. };
  159. } else {
  160. Border = border;
  161. }
  162. AdjustContentView (frame);
  163. }
  164. void AdjustContentView (Rect frame)
  165. {
  166. var borderLength = Border.DrawMarginFrame ? 1 : 0;
  167. var sumPadding = Border.GetSumThickness ();
  168. var wp = new Point ();
  169. var wb = new Size ();
  170. if (frame == Rect.Empty) {
  171. wp.X = borderLength + sumPadding.Left;
  172. wp.Y = borderLength + sumPadding.Top;
  173. wb.Width = borderLength + sumPadding.Right;
  174. wb.Height = borderLength + sumPadding.Bottom;
  175. if (Border.Child == null) {
  176. Border.Child = new ChildContentView (this) {
  177. X = wp.X,
  178. Y = wp.Y,
  179. Width = Dim.Fill (wb.Width),
  180. Height = Dim.Fill (wb.Height)
  181. };
  182. } else {
  183. Border.Child.X = wp.X;
  184. Border.Child.Y = wp.Y;
  185. Border.Child.Width = Dim.Fill (wb.Width);
  186. Border.Child.Height = Dim.Fill (wb.Height);
  187. }
  188. } else {
  189. wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
  190. wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
  191. var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
  192. if (Border.Child == null) {
  193. Border.Child = new ChildContentView (cFrame, this);
  194. } else {
  195. Border.Child.Frame = cFrame;
  196. }
  197. }
  198. if (Subviews?.Count == 0)
  199. base.Add (Border.Child);
  200. Border.ChildContainer = this;
  201. }
  202. /// <inheritdoc/>
  203. public override void Add (View view)
  204. {
  205. Border.Child.Add (view);
  206. if (view.CanFocus) {
  207. CanFocus = true;
  208. }
  209. AddMenuStatusBar (view);
  210. }
  211. /// <inheritdoc/>
  212. public override void Remove (View view)
  213. {
  214. if (view == null) {
  215. return;
  216. }
  217. SetNeedsDisplay ();
  218. var touched = view.Frame;
  219. Border.Child.Remove (view);
  220. if (Border.Child.InternalSubviews.Count < 1) {
  221. CanFocus = false;
  222. }
  223. RemoveMenuStatusBar (view);
  224. }
  225. /// <inheritdoc/>
  226. public override void RemoveAll ()
  227. {
  228. Border.Child.RemoveAll ();
  229. }
  230. /// <inheritdoc/>
  231. public override void Redraw (Rect bounds)
  232. {
  233. if (!NeedDisplay.IsEmpty) {
  234. Driver.SetAttribute (GetNormalColor ());
  235. Clear ();
  236. }
  237. var savedClip = Border.Child.ClipToBounds ();
  238. Border.Child.Redraw (Border.Child.Bounds);
  239. Driver.Clip = savedClip;
  240. ClearLayoutNeeded ();
  241. ClearNeedsDisplay ();
  242. Driver.SetAttribute (GetNormalColor ());
  243. Border.DrawContent (this, false);
  244. if (HasFocus)
  245. Driver.SetAttribute (ColorScheme.HotNormal);
  246. if (Border.DrawMarginFrame) {
  247. if (!ustring.IsNullOrEmpty (Border.Title))
  248. Border.DrawTitle (this);
  249. else
  250. Border.DrawTitle (this, Frame);
  251. }
  252. Driver.SetAttribute (GetNormalColor ());
  253. // Checks if there are any SuperView view which intersect with this window.
  254. if (SuperView != null) {
  255. SuperView.SetNeedsLayout ();
  256. SuperView.SetNeedsDisplay ();
  257. }
  258. }
  259. /// <inheritdoc/>
  260. public override void OnCanFocusChanged ()
  261. {
  262. if (Border.Child != null) {
  263. Border.Child.CanFocus = CanFocus;
  264. }
  265. base.OnCanFocusChanged ();
  266. }
  267. }
  268. private class ChildContentView : View {
  269. View instance;
  270. public ChildContentView (Rect frame, View instance) : base (frame)
  271. {
  272. this.instance = instance;
  273. }
  274. public ChildContentView (View instance)
  275. {
  276. this.instance = instance;
  277. }
  278. public override bool MouseEvent (MouseEvent mouseEvent)
  279. {
  280. return instance.MouseEvent (mouseEvent);
  281. }
  282. }
  283. /// <summary>
  284. /// Invoked when any property of Border changes (except <see cref="Child"/>).
  285. /// </summary>
  286. public event Action<Border> BorderChanged;
  287. private BorderStyle borderStyle;
  288. private bool drawMarginFrame;
  289. private Thickness borderThickness;
  290. private Color borderBrush;
  291. private Color background;
  292. private Thickness padding;
  293. private bool effect3D;
  294. private Point effect3DOffset = new Point (1, 1);
  295. private Attribute? effect3DBrush;
  296. private ustring title = ustring.Empty;
  297. private View child;
  298. /// <summary>
  299. /// Specifies the <see cref="Gui.BorderStyle"/> for a view.
  300. /// </summary>
  301. public BorderStyle BorderStyle {
  302. get => borderStyle;
  303. set {
  304. if (value != BorderStyle.None && !drawMarginFrame) {
  305. // Ensures drawn the border lines.
  306. drawMarginFrame = true;
  307. }
  308. borderStyle = value;
  309. OnBorderChanged ();
  310. }
  311. }
  312. /// <summary>
  313. /// Gets or sets if a margin frame is drawn around the <see cref="Child"/> regardless the <see cref="BorderStyle"/>
  314. /// </summary>
  315. public bool DrawMarginFrame {
  316. get => drawMarginFrame;
  317. set {
  318. if (borderStyle != BorderStyle.None
  319. && (!value || !drawMarginFrame)) {
  320. // Ensures drawn the border lines.
  321. drawMarginFrame = true;
  322. } else {
  323. drawMarginFrame = value;
  324. }
  325. OnBorderChanged ();
  326. }
  327. }
  328. /// <summary>
  329. /// Gets or sets the relative <see cref="Thickness"/> of a <see cref="Border"/>.
  330. /// </summary>
  331. public Thickness BorderThickness {
  332. get => borderThickness;
  333. set {
  334. borderThickness = value;
  335. OnBorderChanged ();
  336. }
  337. }
  338. /// <summary>
  339. /// Gets or sets the <see cref="Color"/> that draws the outer border color.
  340. /// </summary>
  341. public Color BorderBrush {
  342. get => borderBrush;
  343. set {
  344. borderBrush = value;
  345. OnBorderChanged ();
  346. }
  347. }
  348. /// <summary>
  349. /// Gets or sets the <see cref="Color"/> that fills the area between the bounds of a <see cref="Border"/>.
  350. /// </summary>
  351. public Color Background {
  352. get => background;
  353. set {
  354. background = value;
  355. OnBorderChanged ();
  356. }
  357. }
  358. /// <summary>
  359. /// Gets or sets a <see cref="Thickness"/> value that describes the amount of space between a
  360. /// <see cref="Border"/> and its child element.
  361. /// </summary>
  362. public Thickness Padding {
  363. get => padding;
  364. set {
  365. padding = value;
  366. OnBorderChanged ();
  367. }
  368. }
  369. /// <summary>
  370. /// Gets the rendered width of this element.
  371. /// </summary>
  372. public int ActualWidth {
  373. get {
  374. var driver = Application.Driver;
  375. if (Parent?.Border == null) {
  376. return Math.Min (Child?.Frame.Width + (2 * marginFrame) + Padding.Right
  377. + BorderThickness.Right + Padding.Left + BorderThickness.Left ?? 0, driver.Cols);
  378. }
  379. return Math.Min (Parent.Frame.Width, driver.Cols);
  380. }
  381. }
  382. /// <summary>
  383. /// Gets the rendered height of this element.
  384. /// </summary>
  385. public int ActualHeight {
  386. get {
  387. var driver = Application.Driver;
  388. if (Parent?.Border == null) {
  389. return Math.Min (Child?.Frame.Height + (2 * marginFrame) + Padding.Bottom
  390. + BorderThickness.Bottom + Padding.Top + BorderThickness.Top ?? 0, driver.Rows);
  391. }
  392. return Math.Min (Parent.Frame.Height, driver.Rows);
  393. }
  394. }
  395. /// <summary>
  396. /// Gets or sets the single child element of a <see cref="View"/>.
  397. /// </summary>
  398. public View Child {
  399. get => child;
  400. set {
  401. child = value;
  402. if (child != null && Parent != null) {
  403. Parent.Initialized += Parent_Initialized;
  404. Parent.Removed += Parent_Removed;
  405. }
  406. }
  407. }
  408. private void Parent_Removed (View obj)
  409. {
  410. BorderBrush = default;
  411. Background = default;
  412. child.Removed -= Parent_Removed;
  413. }
  414. private void Parent_Initialized (object s, EventArgs e)
  415. {
  416. SetMarginFrameTitleBrush ();
  417. child.Initialized -= Parent_Initialized;
  418. }
  419. private void SetMarginFrameTitleBrush ()
  420. {
  421. if (child != null) {
  422. var view = Parent?.Border != null ? Parent : child;
  423. if (view.ColorScheme != null) {
  424. if (borderBrush == default) {
  425. BorderBrush = view.GetNormalColor ().Foreground;
  426. }
  427. if (background == default) {
  428. Background = view.GetNormalColor ().Background;
  429. }
  430. return;
  431. }
  432. }
  433. BorderBrush = default;
  434. Background = default;
  435. }
  436. /// <summary>
  437. /// Gets the parent <see cref="Child"/> parent if any.
  438. /// </summary>
  439. public View Parent { get => Child?.SuperView; }
  440. /// <summary>
  441. /// Gets or private sets by the <see cref="ToplevelContainer"/>
  442. /// </summary>
  443. public ToplevelContainer ChildContainer { get; private set; }
  444. /// <summary>
  445. /// Gets or sets the 3D effect around the <see cref="Border"/>.
  446. /// </summary>
  447. public bool Effect3D {
  448. get => effect3D;
  449. set {
  450. effect3D = value;
  451. OnBorderChanged ();
  452. }
  453. }
  454. /// <summary>
  455. /// Get or sets the offset start position for the <see cref="Effect3D"/>
  456. /// </summary>
  457. public Point Effect3DOffset {
  458. get => effect3DOffset;
  459. set {
  460. effect3DOffset = value;
  461. OnBorderChanged ();
  462. }
  463. }
  464. /// <summary>
  465. /// Gets or sets the color for the <see cref="Border"/>
  466. /// </summary>
  467. public Attribute? Effect3DBrush {
  468. get => effect3DBrush;
  469. set {
  470. effect3DBrush = value;
  471. OnBorderChanged ();
  472. }
  473. }
  474. /// <summary>
  475. /// The title to be displayed for this view.
  476. /// </summary>
  477. public ustring Title {
  478. get => title;
  479. set {
  480. title = value;
  481. OnBorderChanged ();
  482. }
  483. }
  484. /// <summary>
  485. /// Calculate the sum of the <see cref="Padding"/> and the <see cref="BorderThickness"/>
  486. /// </summary>
  487. /// <returns>The total of the <see cref="Border"/> <see cref="Thickness"/></returns>
  488. public Thickness GetSumThickness ()
  489. {
  490. return new Thickness () {
  491. Left = Padding.Left + BorderThickness.Left,
  492. Top = Padding.Top + BorderThickness.Top,
  493. Right = Padding.Right + BorderThickness.Right,
  494. Bottom = Padding.Bottom + BorderThickness.Bottom
  495. };
  496. }
  497. /// <summary>
  498. /// Drawn the <see cref="BorderThickness"/> more the <see cref="Padding"/>
  499. /// more the <see cref="Border.BorderStyle"/> and the <see cref="Effect3D"/>.
  500. /// </summary>
  501. /// <param name="view">The view to draw.</param>
  502. /// <param name="fill">If it will clear or not the content area.</param>
  503. public void DrawContent (View view = null, bool fill = true)
  504. {
  505. if (Child == null) {
  506. Child = view;
  507. }
  508. if (Parent?.Border != null) {
  509. DrawParentBorder (Parent.ViewToScreen (Parent.Bounds), fill);
  510. } else {
  511. DrawChildBorder (Child.ViewToScreen (Child.Bounds), fill);
  512. }
  513. }
  514. /// <summary>
  515. /// Same as <see cref="DrawContent"/> but drawing full frames for all borders.
  516. /// </summary>
  517. public void DrawFullContent ()
  518. {
  519. var borderThickness = BorderThickness;
  520. var padding = Padding;
  521. var marginFrame = DrawMarginFrame ? 1 : 0;
  522. var driver = Application.Driver;
  523. Rect scrRect;
  524. if (Parent?.Border != null) {
  525. scrRect = Parent.ViewToScreen (Parent.Bounds);
  526. } else {
  527. scrRect = Child.ViewToScreen (Child.Bounds);
  528. }
  529. Rect borderRect;
  530. if (Parent?.Border != null) {
  531. borderRect = scrRect;
  532. } else {
  533. borderRect = new Rect () {
  534. X = scrRect.X - marginFrame - padding.Left - borderThickness.Left,
  535. Y = scrRect.Y - marginFrame - padding.Top - borderThickness.Top,
  536. Width = ActualWidth,
  537. Height = ActualHeight
  538. };
  539. }
  540. var savedAttribute = driver.GetAttribute ();
  541. // Draw 3D effects
  542. if (Effect3D) {
  543. driver.SetAttribute (GetEffect3DBrush ());
  544. var effectBorder = new Rect () {
  545. X = borderRect.X + Effect3DOffset.X,
  546. Y = borderRect.Y + Effect3DOffset.Y,
  547. Width = ActualWidth,
  548. Height = ActualHeight
  549. };
  550. //Child.Clear (effectBorder);
  551. for (int r = effectBorder.Y; r < Math.Min (effectBorder.Bottom, driver.Rows); r++) {
  552. for (int c = effectBorder.X; c < Math.Min (effectBorder.Right, driver.Cols); c++) {
  553. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  554. }
  555. }
  556. }
  557. // Draw border thickness
  558. driver.SetAttribute (new Attribute (BorderBrush));
  559. Child.Clear (borderRect);
  560. borderRect = new Rect () {
  561. X = borderRect.X + borderThickness.Left,
  562. Y = borderRect.Y + borderThickness.Top,
  563. Width = Math.Max (borderRect.Width - borderThickness.Right - borderThickness.Left, 0),
  564. Height = Math.Max (borderRect.Height - borderThickness.Bottom - borderThickness.Top, 0)
  565. };
  566. if (borderRect != scrRect) {
  567. // Draw padding
  568. driver.SetAttribute (new Attribute (Background));
  569. Child.Clear (borderRect);
  570. }
  571. driver.SetAttribute (new Attribute (BorderBrush, Background));
  572. // Draw margin frame
  573. if (DrawMarginFrame) {
  574. if (Parent?.Border != null) {
  575. var sumPadding = GetSumThickness ();
  576. borderRect = new Rect () {
  577. X = scrRect.X + sumPadding.Left,
  578. Y = scrRect.Y + sumPadding.Top,
  579. Width = Math.Max (scrRect.Width - sumPadding.Right - sumPadding.Left, 0),
  580. Height = Math.Max (scrRect.Height - sumPadding.Bottom - sumPadding.Top, 0)
  581. };
  582. } else {
  583. borderRect = new Rect () {
  584. X = borderRect.X + padding.Left,
  585. Y = borderRect.Y + padding.Top,
  586. Width = Math.Max (borderRect.Width - padding.Right - padding.Left, 0),
  587. Height = Math.Max (borderRect.Height - padding.Bottom - padding.Top, 0)
  588. };
  589. }
  590. if (borderRect.Width > 0 && borderRect.Height > 0) {
  591. driver.DrawWindowFrame (borderRect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill: true, this);
  592. }
  593. }
  594. driver.SetAttribute (savedAttribute);
  595. }
  596. private void DrawChildBorder (Rect frame, bool fill = true)
  597. {
  598. var drawMarginFrame = DrawMarginFrame ? 1 : 0;
  599. var sumThickness = GetSumThickness ();
  600. var padding = Padding;
  601. var effect3DOffset = Effect3DOffset;
  602. var driver = Application.Driver;
  603. var savedAttribute = driver.GetAttribute ();
  604. driver.SetAttribute (new Attribute (BorderBrush));
  605. // Draw the upper BorderThickness
  606. for (int r = frame.Y - drawMarginFrame - sumThickness.Top;
  607. r < frame.Y - drawMarginFrame - padding.Top; r++) {
  608. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  609. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  610. AddRuneAt (driver, c, r, ' ');
  611. }
  612. }
  613. // Draw the left BorderThickness
  614. for (int r = frame.Y - drawMarginFrame - padding.Top;
  615. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  616. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  617. c < frame.X - drawMarginFrame - padding.Left; c++) {
  618. AddRuneAt (driver, c, r, ' ');
  619. }
  620. }
  621. // Draw the right BorderThickness
  622. for (int r = frame.Y - drawMarginFrame - padding.Top;
  623. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  624. for (int c = frame.Right + drawMarginFrame + padding.Right;
  625. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  626. AddRuneAt (driver, c, r, ' ');
  627. }
  628. }
  629. // Draw the lower BorderThickness
  630. for (int r = frame.Bottom + drawMarginFrame + padding.Bottom;
  631. r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom, driver.Rows); r++) {
  632. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  633. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  634. AddRuneAt (driver, c, r, ' ');
  635. }
  636. }
  637. driver.SetAttribute (new Attribute (Background));
  638. // Draw the upper Padding
  639. for (int r = frame.Y - drawMarginFrame - padding.Top;
  640. r < frame.Y - drawMarginFrame; r++) {
  641. for (int c = frame.X - drawMarginFrame - padding.Left;
  642. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  643. AddRuneAt (driver, c, r, ' ');
  644. }
  645. }
  646. // Draw the left Padding
  647. for (int r = frame.Y - drawMarginFrame;
  648. r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  649. for (int c = frame.X - drawMarginFrame - padding.Left;
  650. c < frame.X - drawMarginFrame; c++) {
  651. AddRuneAt (driver, c, r, ' ');
  652. }
  653. }
  654. // Draw the right Padding
  655. for (int r = frame.Y - drawMarginFrame;
  656. r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  657. for (int c = frame.Right + drawMarginFrame;
  658. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  659. AddRuneAt (driver, c, r, ' ');
  660. }
  661. }
  662. // Draw the lower Padding
  663. for (int r = frame.Bottom + drawMarginFrame;
  664. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  665. for (int c = frame.X - drawMarginFrame - padding.Left;
  666. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  667. AddRuneAt (driver, c, r, ' ');
  668. }
  669. }
  670. driver.SetAttribute (new Attribute (BorderBrush, Background));
  671. // Draw the MarginFrame
  672. if (DrawMarginFrame) {
  673. var rect = new Rect () {
  674. X = frame.X - drawMarginFrame,
  675. Y = frame.Y - drawMarginFrame,
  676. Width = frame.Width + (2 * drawMarginFrame),
  677. Height = frame.Height + (2 * drawMarginFrame)
  678. };
  679. if (rect.Width > 0 && rect.Height > 0) {
  680. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  681. DrawTitle (Child);
  682. }
  683. }
  684. if (Effect3D) {
  685. driver.SetAttribute (GetEffect3DBrush ());
  686. // Draw the upper Effect3D
  687. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  688. r >= 0 && r < frame.Y - drawMarginFrame - sumThickness.Top; r++) {
  689. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  690. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  691. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  692. }
  693. }
  694. // Draw the left Effect3D
  695. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  696. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  697. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  698. c >= 0 && c < frame.X - drawMarginFrame - sumThickness.Left; c++) {
  699. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  700. }
  701. }
  702. // Draw the right Effect3D
  703. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  704. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  705. for (int c = frame.Right + drawMarginFrame + sumThickness.Right;
  706. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  707. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  708. }
  709. }
  710. // Draw the lower Effect3D
  711. for (int r = frame.Bottom + drawMarginFrame + sumThickness.Bottom;
  712. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  713. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  714. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  715. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  716. }
  717. }
  718. }
  719. driver.SetAttribute (savedAttribute);
  720. }
  721. private void DrawParentBorder (Rect frame, bool fill = true)
  722. {
  723. var sumThickness = GetSumThickness ();
  724. var borderThickness = BorderThickness;
  725. var effect3DOffset = Effect3DOffset;
  726. var driver = Application.Driver;
  727. var savedAttribute = driver.GetAttribute ();
  728. driver.SetAttribute (new Attribute (BorderBrush));
  729. // Draw the upper BorderThickness
  730. for (int r = frame.Y;
  731. r < Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r++) {
  732. for (int c = frame.X;
  733. c < Math.Min (frame.Right, driver.Cols); c++) {
  734. AddRuneAt (driver, c, r, ' ');
  735. }
  736. }
  737. // Draw the left BorderThickness
  738. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  739. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  740. for (int c = frame.X;
  741. c < Math.Min (frame.X + borderThickness.Left, frame.Right); c++) {
  742. AddRuneAt (driver, c, r, ' ');
  743. }
  744. }
  745. // Draw the right BorderThickness
  746. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  747. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  748. for (int c = Math.Max (frame.Right - borderThickness.Right, frame.X);
  749. c < Math.Min (frame.Right, driver.Cols); c++) {
  750. AddRuneAt (driver, c, r, ' ');
  751. }
  752. }
  753. // Draw the lower BorderThickness
  754. for (int r = Math.Max (frame.Bottom - borderThickness.Bottom, frame.Y);
  755. r < Math.Min (frame.Bottom, driver.Rows); r++) {
  756. for (int c = frame.X;
  757. c < Math.Min (frame.Right, driver.Cols); c++) {
  758. AddRuneAt (driver, c, r, ' ');
  759. }
  760. }
  761. driver.SetAttribute (new Attribute (Background));
  762. // Draw the upper Padding
  763. for (int r = frame.Y + borderThickness.Top;
  764. r < Math.Min (frame.Y + sumThickness.Top, frame.Bottom - borderThickness.Bottom); r++) {
  765. for (int c = frame.X + borderThickness.Left;
  766. c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  767. AddRuneAt (driver, c, r, ' ');
  768. }
  769. }
  770. // Draw the left Padding
  771. for (int r = frame.Y + sumThickness.Top;
  772. r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  773. for (int c = frame.X + borderThickness.Left;
  774. c < Math.Min (frame.X + sumThickness.Left, frame.Right - borderThickness.Right); c++) {
  775. AddRuneAt (driver, c, r, ' ');
  776. }
  777. }
  778. // Draw the right Padding
  779. for (int r = frame.Y + sumThickness.Top;
  780. r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  781. for (int c = Math.Max (frame.Right - sumThickness.Right, frame.X + sumThickness.Left);
  782. c < Math.Max (frame.Right - borderThickness.Right, frame.X + sumThickness.Left); c++) {
  783. AddRuneAt (driver, c, r, ' ');
  784. }
  785. }
  786. // Draw the lower Padding
  787. for (int r = Math.Max (frame.Bottom - sumThickness.Bottom, frame.Y + borderThickness.Top);
  788. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  789. for (int c = frame.X + borderThickness.Left;
  790. c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  791. AddRuneAt (driver, c, r, ' ');
  792. }
  793. }
  794. driver.SetAttribute (new Attribute (BorderBrush, Background));
  795. // Draw the MarginFrame
  796. if (DrawMarginFrame) {
  797. var rect = new Rect () {
  798. X = frame.X + sumThickness.Left,
  799. Y = frame.Y + sumThickness.Top,
  800. Width = Math.Max (frame.Width - sumThickness.Right - sumThickness.Left, 0),
  801. Height = Math.Max (frame.Height - sumThickness.Bottom - sumThickness.Top, 0)
  802. };
  803. if (rect.Width > 0 && rect.Height > 0) {
  804. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  805. DrawTitle (Parent);
  806. }
  807. }
  808. if (Effect3D) {
  809. driver.SetAttribute (GetEffect3DBrush ());
  810. // Draw the upper Effect3D
  811. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  812. r < frame.Y; r++) {
  813. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  814. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  815. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  816. }
  817. }
  818. // Draw the left Effect3D
  819. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  820. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  821. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  822. c < frame.X; c++) {
  823. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  824. }
  825. }
  826. // Draw the right Effect3D
  827. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  828. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  829. for (int c = frame.Right;
  830. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  831. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  832. }
  833. }
  834. // Draw the lower Effect3D
  835. for (int r = frame.Bottom;
  836. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  837. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  838. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  839. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  840. }
  841. }
  842. }
  843. driver.SetAttribute (savedAttribute);
  844. }
  845. private Attribute GetEffect3DBrush ()
  846. {
  847. return Effect3DBrush == null
  848. ? new Attribute (Color.Gray, Color.DarkGray)
  849. : (Attribute)Effect3DBrush;
  850. }
  851. private void AddRuneAt (ConsoleDriver driver, int col, int row, Rune ch)
  852. {
  853. if (col < driver.Cols && row < driver.Rows && col > 0 && driver.Contents [row, col, 2] == 0
  854. && Rune.ColumnWidth ((char)driver.Contents [row, col - 1, 0]) > 1) {
  855. driver.Contents [row, col, 1] = driver.GetAttribute ();
  856. return;
  857. }
  858. driver.Move (col, row);
  859. driver.AddRune (ch);
  860. }
  861. /// <summary>
  862. /// Draws the view <see cref="Title"/> to the screen.
  863. /// </summary>
  864. /// <param name="view">The view.</param>
  865. public void DrawTitle (View view)
  866. {
  867. var driver = Application.Driver;
  868. if (DrawMarginFrame) {
  869. driver.SetAttribute (new Attribute (BorderBrush, Background));
  870. if (view.HasFocus)
  871. driver.SetAttribute (new Attribute (Child.ColorScheme.HotNormal.Foreground, Background));
  872. var padding = view.Border.GetSumThickness ();
  873. Rect scrRect;
  874. if (view == Child) {
  875. scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width + 2, view.Frame.Height + 2));
  876. scrRect = new Rect (scrRect.X - 1, scrRect.Y - 1, scrRect.Width, scrRect.Height);
  877. driver.DrawWindowTitle (scrRect, Title, 0, 0, 0, 0);
  878. } else {
  879. scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width, view.Frame.Height));
  880. driver.DrawWindowTitle (scrRect, Parent.Border.Title,
  881. padding.Left, padding.Top, padding.Right, padding.Bottom);
  882. }
  883. }
  884. driver.SetAttribute (Child.GetNormalColor ());
  885. }
  886. /// <summary>
  887. /// Draws the <see cref="View.Text"/> to the screen.
  888. /// </summary>
  889. /// <param name="view">The view.</param>
  890. /// <param name="rect">The frame.</param>
  891. public void DrawTitle (View view, Rect rect)
  892. {
  893. var driver = Application.Driver;
  894. if (DrawMarginFrame) {
  895. driver.SetAttribute (new Attribute (BorderBrush, Background));
  896. if (view.HasFocus) {
  897. driver.SetAttribute (new Attribute (view.ColorScheme.HotNormal.Foreground, Background));
  898. }
  899. var padding = Parent.Border.GetSumThickness ();
  900. var scrRect = Parent.ViewToScreen (new Rect (0, 0, rect.Width, rect.Height));
  901. driver.DrawWindowTitle (scrRect, view.Text,
  902. padding.Left, padding.Top, padding.Right, padding.Bottom);
  903. }
  904. driver.SetAttribute (view.GetNormalColor ());
  905. }
  906. /// <summary>
  907. /// Invoke the <see cref="BorderChanged"/> event.
  908. /// </summary>
  909. public virtual void OnBorderChanged ()
  910. {
  911. BorderChanged?.Invoke (this);
  912. }
  913. }
  914. }