Border.cs 27 KB

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