Border.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. BorderBrush = default;
  409. Background = default;
  410. child.Removed -= Parent_Removed;
  411. }
  412. /// <summary>
  413. /// Gets the parent <see cref="Child"/> parent if any.
  414. /// </summary>
  415. public View Parent { get => Child?.SuperView; }
  416. /// <summary>
  417. /// Gets or private sets by the <see cref="ToplevelContainer"/>
  418. /// </summary>
  419. public ToplevelContainer ChildContainer { get; private set; }
  420. /// <summary>
  421. /// Gets or sets the 3D effect around the <see cref="Border"/>.
  422. /// </summary>
  423. public bool Effect3D {
  424. get => effect3D;
  425. set {
  426. effect3D = value;
  427. OnBorderChanged ();
  428. }
  429. }
  430. /// <summary>
  431. /// Get or sets the offset start position for the <see cref="Effect3D"/>
  432. /// </summary>
  433. public Point Effect3DOffset {
  434. get => effect3DOffset;
  435. set {
  436. effect3DOffset = value;
  437. OnBorderChanged ();
  438. }
  439. }
  440. /// <summary>
  441. /// Gets or sets the color for the <see cref="Border"/>
  442. /// </summary>
  443. public Attribute? Effect3DBrush {
  444. get => effect3DBrush;
  445. set {
  446. effect3DBrush = value;
  447. OnBorderChanged ();
  448. }
  449. }
  450. /// <summary>
  451. /// The title to be displayed for this view.
  452. /// </summary>
  453. public ustring Title {
  454. get => title;
  455. set {
  456. title = value;
  457. OnBorderChanged ();
  458. }
  459. }
  460. /// <summary>
  461. /// Calculate the sum of the <see cref="Padding"/> and the <see cref="BorderThickness"/>
  462. /// </summary>
  463. /// <returns>The total of the <see cref="Border"/> <see cref="Thickness"/></returns>
  464. public Thickness GetSumThickness ()
  465. {
  466. return new Thickness () {
  467. Left = Padding.Left + BorderThickness.Left,
  468. Top = Padding.Top + BorderThickness.Top,
  469. Right = Padding.Right + BorderThickness.Right,
  470. Bottom = Padding.Bottom + BorderThickness.Bottom
  471. };
  472. }
  473. /// <summary>
  474. /// Drawn the <see cref="BorderThickness"/> more the <see cref="Padding"/>
  475. /// more the <see cref="Border.BorderStyle"/> and the <see cref="Effect3D"/>.
  476. /// </summary>
  477. /// <param name="view">The view to draw.</param>
  478. /// <param name="fill">If it will clear or not the content area.</param>
  479. public void DrawContent (View view = null, bool fill = true)
  480. {
  481. if (Child == null) {
  482. Child = view;
  483. }
  484. if (Parent?.Border != null) {
  485. DrawParentBorder (Parent.ViewToScreen (Parent.Bounds), fill);
  486. } else {
  487. DrawChildBorder (Child.ViewToScreen (Child.Bounds), fill);
  488. }
  489. }
  490. /// <summary>
  491. /// Same as <see cref="DrawContent"/> but drawing full frames for all borders.
  492. /// </summary>
  493. public void DrawFullContent ()
  494. {
  495. var borderThickness = BorderThickness;
  496. var padding = Padding;
  497. var marginFrame = DrawMarginFrame ? 1 : 0;
  498. var driver = Application.Driver;
  499. Rect scrRect;
  500. if (Parent?.Border != null) {
  501. scrRect = Parent.ViewToScreen (Parent.Bounds);
  502. } else {
  503. scrRect = Child.ViewToScreen (Child.Bounds);
  504. }
  505. Rect borderRect;
  506. if (Parent?.Border != null) {
  507. borderRect = scrRect;
  508. } else {
  509. borderRect = new Rect () {
  510. X = scrRect.X - marginFrame - padding.Left - borderThickness.Left,
  511. Y = scrRect.Y - marginFrame - padding.Top - borderThickness.Top,
  512. Width = ActualWidth,
  513. Height = ActualHeight
  514. };
  515. }
  516. var savedAttribute = driver.GetAttribute ();
  517. // Draw 3D effects
  518. if (Effect3D) {
  519. driver.SetAttribute (GetEffect3DBrush ());
  520. var effectBorder = new Rect () {
  521. X = borderRect.X + Effect3DOffset.X,
  522. Y = borderRect.Y + Effect3DOffset.Y,
  523. Width = ActualWidth,
  524. Height = ActualHeight
  525. };
  526. //Child.Clear (effectBorder);
  527. for (int r = effectBorder.Y; r < Math.Min (effectBorder.Bottom, driver.Rows); r++) {
  528. for (int c = effectBorder.X; c < Math.Min (effectBorder.Right, driver.Cols); c++) {
  529. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  530. }
  531. }
  532. }
  533. // Draw border thickness
  534. SetBorderBrush (driver);
  535. Child.Clear (borderRect);
  536. borderRect = new Rect () {
  537. X = borderRect.X + borderThickness.Left,
  538. Y = borderRect.Y + borderThickness.Top,
  539. Width = Math.Max (borderRect.Width - borderThickness.Right - borderThickness.Left, 0),
  540. Height = Math.Max (borderRect.Height - borderThickness.Bottom - borderThickness.Top, 0)
  541. };
  542. if (borderRect != scrRect) {
  543. // Draw padding
  544. driver.SetAttribute (new Attribute (Background));
  545. Child.Clear (borderRect);
  546. }
  547. SetBorderBrushBackground (driver);
  548. // Draw margin frame
  549. if (DrawMarginFrame) {
  550. if (Parent?.Border != null) {
  551. var sumPadding = GetSumThickness ();
  552. borderRect = new Rect () {
  553. X = scrRect.X + sumPadding.Left,
  554. Y = scrRect.Y + sumPadding.Top,
  555. Width = Math.Max (scrRect.Width - sumPadding.Right - sumPadding.Left, 0),
  556. Height = Math.Max (scrRect.Height - sumPadding.Bottom - sumPadding.Top, 0)
  557. };
  558. } else {
  559. borderRect = new Rect () {
  560. X = borderRect.X + padding.Left,
  561. Y = borderRect.Y + padding.Top,
  562. Width = Math.Max (borderRect.Width - padding.Right - padding.Left, 0),
  563. Height = Math.Max (borderRect.Height - padding.Bottom - padding.Top, 0)
  564. };
  565. }
  566. if (borderRect.Width > 0 && borderRect.Height > 0) {
  567. driver.DrawWindowFrame (borderRect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill: true, this);
  568. }
  569. }
  570. driver.SetAttribute (savedAttribute);
  571. }
  572. private void DrawChildBorder (Rect frame, bool fill = true)
  573. {
  574. var drawMarginFrame = DrawMarginFrame ? 1 : 0;
  575. var sumThickness = GetSumThickness ();
  576. var padding = Padding;
  577. var effect3DOffset = Effect3DOffset;
  578. var driver = Application.Driver;
  579. var savedAttribute = driver.GetAttribute ();
  580. SetBorderBrush (driver);
  581. // Draw the upper BorderThickness
  582. for (int r = frame.Y - drawMarginFrame - sumThickness.Top;
  583. r < frame.Y - drawMarginFrame - padding.Top; r++) {
  584. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  585. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  586. AddRuneAt (driver, c, r, ' ');
  587. }
  588. }
  589. // Draw the left BorderThickness
  590. for (int r = frame.Y - drawMarginFrame - padding.Top;
  591. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  592. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  593. c < frame.X - drawMarginFrame - padding.Left; c++) {
  594. AddRuneAt (driver, c, r, ' ');
  595. }
  596. }
  597. // Draw the right BorderThickness
  598. for (int r = frame.Y - drawMarginFrame - padding.Top;
  599. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  600. for (int c = frame.Right + drawMarginFrame + padding.Right;
  601. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  602. AddRuneAt (driver, c, r, ' ');
  603. }
  604. }
  605. // Draw the lower BorderThickness
  606. for (int r = frame.Bottom + drawMarginFrame + padding.Bottom;
  607. r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom, driver.Rows); 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. SetBackground (driver);
  614. // Draw the upper Padding
  615. for (int r = frame.Y - drawMarginFrame - padding.Top;
  616. r < frame.Y - drawMarginFrame; r++) {
  617. for (int c = frame.X - drawMarginFrame - padding.Left;
  618. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  619. AddRuneAt (driver, c, r, ' ');
  620. }
  621. }
  622. // Draw the left Padding
  623. for (int r = frame.Y - drawMarginFrame;
  624. r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  625. for (int c = frame.X - drawMarginFrame - padding.Left;
  626. c < frame.X - drawMarginFrame; c++) {
  627. AddRuneAt (driver, c, r, ' ');
  628. }
  629. }
  630. // Draw the right Padding
  631. for (int r = frame.Y - drawMarginFrame;
  632. r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  633. for (int c = frame.Right + drawMarginFrame;
  634. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  635. AddRuneAt (driver, c, r, ' ');
  636. }
  637. }
  638. // Draw the lower Padding
  639. for (int r = frame.Bottom + drawMarginFrame;
  640. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); 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. SetBorderBrushBackground (driver);
  647. // Draw the MarginFrame
  648. if (DrawMarginFrame) {
  649. var rect = new Rect () {
  650. X = frame.X - drawMarginFrame,
  651. Y = frame.Y - drawMarginFrame,
  652. Width = frame.Width + (2 * drawMarginFrame),
  653. Height = frame.Height + (2 * drawMarginFrame)
  654. };
  655. if (rect.Width > 0 && rect.Height > 0) {
  656. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  657. DrawTitle (Child);
  658. }
  659. }
  660. if (Effect3D) {
  661. driver.SetAttribute (GetEffect3DBrush ());
  662. // Draw the upper Effect3D
  663. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  664. r >= 0 && r < frame.Y - drawMarginFrame - sumThickness.Top; r++) {
  665. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  666. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  667. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  668. }
  669. }
  670. // Draw the left Effect3D
  671. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  672. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  673. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  674. c >= 0 && c < frame.X - drawMarginFrame - sumThickness.Left; c++) {
  675. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  676. }
  677. }
  678. // Draw the right Effect3D
  679. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  680. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  681. for (int c = frame.Right + drawMarginFrame + sumThickness.Right;
  682. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  683. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  684. }
  685. }
  686. // Draw the lower Effect3D
  687. for (int r = frame.Bottom + drawMarginFrame + sumThickness.Bottom;
  688. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); 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. }
  695. driver.SetAttribute (savedAttribute);
  696. }
  697. private void DrawParentBorder (Rect frame, bool fill = true)
  698. {
  699. var sumThickness = GetSumThickness ();
  700. var borderThickness = BorderThickness;
  701. var effect3DOffset = Effect3DOffset;
  702. var driver = Application.Driver;
  703. var savedAttribute = driver.GetAttribute ();
  704. SetBorderBrush (driver);
  705. // Draw the upper BorderThickness
  706. for (int r = frame.Y;
  707. r < Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r++) {
  708. for (int c = frame.X;
  709. c < Math.Min (frame.Right, driver.Cols); c++) {
  710. AddRuneAt (driver, c, r, ' ');
  711. }
  712. }
  713. // Draw the left BorderThickness
  714. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  715. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  716. for (int c = frame.X;
  717. c < Math.Min (frame.X + borderThickness.Left, frame.Right); c++) {
  718. AddRuneAt (driver, c, r, ' ');
  719. }
  720. }
  721. // Draw the right BorderThickness
  722. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  723. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  724. for (int c = Math.Max (frame.Right - borderThickness.Right, frame.X);
  725. c < Math.Min (frame.Right, driver.Cols); c++) {
  726. AddRuneAt (driver, c, r, ' ');
  727. }
  728. }
  729. // Draw the lower BorderThickness
  730. for (int r = Math.Max (frame.Bottom - borderThickness.Bottom, frame.Y);
  731. r < Math.Min (frame.Bottom, driver.Rows); r++) {
  732. for (int c = frame.X;
  733. c < Math.Min (frame.Right, driver.Cols); c++) {
  734. AddRuneAt (driver, c, r, ' ');
  735. }
  736. }
  737. SetBackground (driver);
  738. // Draw the upper Padding
  739. for (int r = frame.Y + borderThickness.Top;
  740. r < Math.Min (frame.Y + sumThickness.Top, frame.Bottom - borderThickness.Bottom); r++) {
  741. for (int c = frame.X + borderThickness.Left;
  742. c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  743. AddRuneAt (driver, c, r, ' ');
  744. }
  745. }
  746. // Draw the left Padding
  747. for (int r = frame.Y + sumThickness.Top;
  748. r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  749. for (int c = frame.X + borderThickness.Left;
  750. c < Math.Min (frame.X + sumThickness.Left, frame.Right - borderThickness.Right); c++) {
  751. AddRuneAt (driver, c, r, ' ');
  752. }
  753. }
  754. // Draw the right Padding
  755. for (int r = frame.Y + sumThickness.Top;
  756. r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  757. for (int c = Math.Max (frame.Right - sumThickness.Right, frame.X + sumThickness.Left);
  758. c < Math.Max (frame.Right - borderThickness.Right, frame.X + sumThickness.Left); c++) {
  759. AddRuneAt (driver, c, r, ' ');
  760. }
  761. }
  762. // Draw the lower Padding
  763. for (int r = Math.Max (frame.Bottom - sumThickness.Bottom, frame.Y + borderThickness.Top);
  764. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); 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. SetBorderBrushBackground (driver);
  771. // Draw the MarginFrame
  772. if (DrawMarginFrame) {
  773. var rect = new Rect () {
  774. X = frame.X + sumThickness.Left,
  775. Y = frame.Y + sumThickness.Top,
  776. Width = Math.Max (frame.Width - sumThickness.Right - sumThickness.Left, 0),
  777. Height = Math.Max (frame.Height - sumThickness.Bottom - sumThickness.Top, 0)
  778. };
  779. if (rect.Width > 0 && rect.Height > 0) {
  780. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  781. DrawTitle (Parent);
  782. }
  783. }
  784. if (Effect3D) {
  785. driver.SetAttribute (GetEffect3DBrush ());
  786. // Draw the upper Effect3D
  787. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  788. r < frame.Y; r++) {
  789. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  790. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  791. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  792. }
  793. }
  794. // Draw the left Effect3D
  795. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  796. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  797. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  798. c < frame.X; c++) {
  799. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  800. }
  801. }
  802. // Draw the right Effect3D
  803. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  804. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  805. for (int c = frame.Right;
  806. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  807. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  808. }
  809. }
  810. // Draw the lower Effect3D
  811. for (int r = frame.Bottom;
  812. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); 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. }
  819. driver.SetAttribute (savedAttribute);
  820. }
  821. private void SetBorderBrushBackground (ConsoleDriver driver)
  822. {
  823. if (borderBrush != null && background != null) {
  824. driver.SetAttribute (new Attribute (BorderBrush, Background));
  825. } else if (borderBrush != null && background == null) {
  826. driver.SetAttribute (new Attribute (BorderBrush, Parent.ColorScheme.Normal.Background));
  827. } else if (borderBrush == null && background != null) {
  828. driver.SetAttribute (new Attribute (Parent.ColorScheme.Normal.Foreground, Background));
  829. } else {
  830. driver.SetAttribute (Parent.ColorScheme.Normal);
  831. }
  832. }
  833. private void SetBackground (ConsoleDriver driver)
  834. {
  835. if (background != null) {
  836. driver.SetAttribute (new Attribute (Background));
  837. } else {
  838. driver.SetAttribute (new Attribute (Parent.ColorScheme.Normal.Background));
  839. }
  840. }
  841. private void SetBorderBrush (ConsoleDriver driver)
  842. {
  843. if (borderBrush != null) {
  844. driver.SetAttribute (new Attribute (BorderBrush));
  845. } else {
  846. driver.SetAttribute (new Attribute (Parent.ColorScheme.Normal.Foreground));
  847. }
  848. }
  849. private Attribute GetEffect3DBrush ()
  850. {
  851. return Effect3DBrush == null
  852. ? new Attribute (Color.Gray, Color.DarkGray)
  853. : (Attribute)Effect3DBrush;
  854. }
  855. private void AddRuneAt (ConsoleDriver driver, int col, int row, Rune ch)
  856. {
  857. if (col < driver.Cols && row < driver.Rows && col > 0 && driver.Contents [row, col, 2] == 0
  858. && Rune.ColumnWidth ((char)driver.Contents [row, col - 1, 0]) > 1) {
  859. driver.Contents [row, col, 1] = driver.GetAttribute ();
  860. return;
  861. }
  862. driver.Move (col, row);
  863. driver.AddRune (ch);
  864. }
  865. /// <summary>
  866. /// Draws the view <see cref="Title"/> to the screen.
  867. /// </summary>
  868. /// <param name="view">The view.</param>
  869. public void DrawTitle (View view)
  870. {
  871. var driver = Application.Driver;
  872. if (DrawMarginFrame) {
  873. SetBorderBrushBackground (driver);
  874. SetHotNormalBackground (view, driver);
  875. var padding = view.Border.GetSumThickness ();
  876. Rect scrRect;
  877. if (view == Child) {
  878. scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width + 2, view.Frame.Height + 2));
  879. scrRect = new Rect (scrRect.X - 1, scrRect.Y - 1, scrRect.Width, scrRect.Height);
  880. driver.DrawWindowTitle (scrRect, Title, 0, 0, 0, 0);
  881. } else {
  882. scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width, view.Frame.Height));
  883. driver.DrawWindowTitle (scrRect, Parent.Border.Title,
  884. padding.Left, padding.Top, padding.Right, padding.Bottom);
  885. }
  886. }
  887. driver.SetAttribute (Child.GetNormalColor ());
  888. }
  889. private void SetHotNormalBackground (View view, ConsoleDriver driver)
  890. {
  891. if (view.HasFocus) {
  892. if (background != null) {
  893. driver.SetAttribute (new Attribute (Child.ColorScheme.HotNormal.Foreground, Background));
  894. } else {
  895. driver.SetAttribute (Child.ColorScheme.HotNormal);
  896. }
  897. }
  898. }
  899. /// <summary>
  900. /// Draws the <see cref="View.Text"/> to the screen.
  901. /// </summary>
  902. /// <param name="view">The view.</param>
  903. /// <param name="rect">The frame.</param>
  904. public void DrawTitle (View view, Rect rect)
  905. {
  906. var driver = Application.Driver;
  907. if (DrawMarginFrame) {
  908. SetBorderBrushBackground (driver);
  909. SetHotNormalBackground (view, driver);
  910. var padding = Parent.Border.GetSumThickness ();
  911. var scrRect = Parent.ViewToScreen (new Rect (0, 0, rect.Width, rect.Height));
  912. driver.DrawWindowTitle (scrRect, view.Text,
  913. padding.Left, padding.Top, padding.Right, padding.Bottom);
  914. }
  915. driver.SetAttribute (view.GetNormalColor ());
  916. }
  917. /// <summary>
  918. /// Invoke the <see cref="BorderChanged"/> event.
  919. /// </summary>
  920. public virtual void OnBorderChanged ()
  921. {
  922. BorderChanged?.Invoke (this);
  923. }
  924. }
  925. }