Border.cs 31 KB

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