Border.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. }
  237. private class ChildContentView : View {
  238. View instance;
  239. public ChildContentView (Rect frame, View instance) : base (frame)
  240. {
  241. this.instance = instance;
  242. }
  243. public ChildContentView (View instance)
  244. {
  245. this.instance = instance;
  246. }
  247. public override bool MouseEvent (MouseEvent mouseEvent)
  248. {
  249. return instance.MouseEvent (mouseEvent);
  250. }
  251. }
  252. /// <summary>
  253. /// Event to be invoked when any border property change.
  254. /// </summary>
  255. public event Action<Border> BorderChanged;
  256. private BorderStyle borderStyle;
  257. private bool drawMarginFrame;
  258. private Thickness borderThickness;
  259. private Thickness padding;
  260. /// <summary>
  261. /// Specifies the <see cref="Gui.BorderStyle"/> for a view.
  262. /// </summary>
  263. public BorderStyle BorderStyle {
  264. get => borderStyle;
  265. set {
  266. if (value != BorderStyle.None && !drawMarginFrame) {
  267. // Ensures drawn the border lines.
  268. drawMarginFrame = true;
  269. }
  270. borderStyle = value;
  271. OnBorderChanged ();
  272. }
  273. }
  274. /// <summary>
  275. /// Gets or sets if a margin frame is drawn around the <see cref="Child"/> regardless the <see cref="BorderStyle"/>
  276. /// </summary>
  277. public bool DrawMarginFrame {
  278. get => drawMarginFrame;
  279. set {
  280. if (borderStyle != BorderStyle.None
  281. && (!value || !drawMarginFrame)) {
  282. // Ensures drawn the border lines.
  283. drawMarginFrame = true;
  284. } else {
  285. drawMarginFrame = value;
  286. }
  287. OnBorderChanged ();
  288. }
  289. }
  290. /// <summary>
  291. /// Gets or sets the relative <see cref="Thickness"/> of a <see cref="Border"/>.
  292. /// </summary>
  293. public Thickness BorderThickness {
  294. get => borderThickness;
  295. set {
  296. borderThickness = value;
  297. OnBorderChanged ();
  298. }
  299. }
  300. /// <summary>
  301. /// Gets or sets the <see cref="Color"/> that draws the outer border color.
  302. /// </summary>
  303. public Color BorderBrush { get; set; }
  304. /// <summary>
  305. /// Gets or sets the <see cref="Color"/> that fills the area between the bounds of a <see cref="Border"/>.
  306. /// </summary>
  307. public Color Background { get; set; }
  308. /// <summary>
  309. /// Gets or sets a <see cref="Thickness"/> value that describes the amount of space between a
  310. /// <see cref="Border"/> and its child element.
  311. /// </summary>
  312. public Thickness Padding {
  313. get => padding;
  314. set {
  315. padding = value;
  316. OnBorderChanged ();
  317. }
  318. }
  319. /// <summary>
  320. /// Gets the rendered width of this element.
  321. /// </summary>
  322. public int ActualWidth {
  323. get {
  324. if (Parent?.Border == null) {
  325. return Child?.Frame.Width + (2 * marginFrame) + Padding.Right
  326. + BorderThickness.Right + Padding.Left + BorderThickness.Left ?? 0;
  327. }
  328. return Parent.Frame.Width;
  329. }
  330. }
  331. /// <summary>
  332. /// Gets the rendered height of this element.
  333. /// </summary>
  334. public int ActualHeight {
  335. get {
  336. if (Parent?.Border == null) {
  337. return Child?.Frame.Height + (2 * marginFrame) + Padding.Bottom
  338. + BorderThickness.Bottom + Padding.Top + BorderThickness.Top ?? 0;
  339. }
  340. return Parent.Frame.Height;
  341. }
  342. }
  343. /// <summary>
  344. /// Gets or sets the single child element of a <see cref="View"/>.
  345. /// </summary>
  346. public View Child { get; set; }
  347. /// <summary>
  348. /// Gets the parent <see cref="Child"/> parent if any.
  349. /// </summary>
  350. public View Parent { get => Child?.SuperView; }
  351. /// <summary>
  352. /// Gets or private sets by the <see cref="ToplevelContainer"/>
  353. /// </summary>
  354. public ToplevelContainer ChildContainer { get; private set; }
  355. /// <summary>
  356. /// Gets or sets the 3D effect around the <see cref="Border"/>.
  357. /// </summary>
  358. public bool Effect3D { get; set; }
  359. /// <summary>
  360. /// Get or sets the offset start position for the <see cref="Effect3D"/>
  361. /// </summary>
  362. public Point Effect3DOffset { get; set; } = new Point (1, 1);
  363. /// <summary>
  364. /// Gets or sets the color for the <see cref="Border"/>
  365. /// </summary>
  366. public Color Effect3DBrush { get; set; } = Color.DarkGray;
  367. /// <summary>
  368. /// Calculate the sum of the <see cref="Padding"/> and the <see cref="BorderThickness"/>
  369. /// </summary>
  370. /// <returns>The total of the <see cref="Border"/> <see cref="Thickness"/></returns>
  371. public Thickness GetSumThickness ()
  372. {
  373. return new Thickness () {
  374. Left = Padding.Left + BorderThickness.Left,
  375. Top = Padding.Top + BorderThickness.Top,
  376. Right = Padding.Right + BorderThickness.Right,
  377. Bottom = Padding.Bottom + BorderThickness.Bottom
  378. };
  379. }
  380. /// <summary>
  381. /// Drawn the <see cref="BorderThickness"/> more the <see cref="Padding"/>
  382. /// more the <see cref="Border.BorderStyle"/> and the <see cref="Effect3D"/>.
  383. /// </summary>
  384. public void DrawContent ()
  385. {
  386. if (Parent?.Border != null) {
  387. DrawParentBorder (Parent.ViewToScreen (new Rect (0, 0, Parent.Frame.Width, Parent.Frame.Height)));
  388. } else {
  389. DrawChildBorder (Child.ViewToScreen (new Rect (0, 0, Child.Frame.Width, Child.Frame.Height)));
  390. }
  391. }
  392. /// <summary>
  393. /// Same as <see cref="DrawContent"/> but drawing full frames for all borders.
  394. /// </summary>
  395. public void DrawFullContent ()
  396. {
  397. var borderThickness = BorderThickness;
  398. var padding = Padding;
  399. var marginFrame = DrawMarginFrame ? 1 : 0;
  400. var driver = Application.Driver;
  401. Rect scrRect;
  402. if (Parent?.Border != null) {
  403. scrRect = Parent.ViewToScreen (new Rect (0, 0, Parent.Frame.Width, Parent.Frame.Height));
  404. } else {
  405. scrRect = Child.ViewToScreen (new Rect (0, 0, Child.Frame.Width, Child.Frame.Height));
  406. }
  407. Rect borderRect;
  408. if (Parent?.Border != null) {
  409. borderRect = scrRect;
  410. } else {
  411. borderRect = new Rect () {
  412. X = scrRect.X - marginFrame - padding.Left - borderThickness.Left,
  413. Y = scrRect.Y - marginFrame - padding.Top - borderThickness.Top,
  414. Width = ActualWidth,
  415. Height = ActualHeight
  416. };
  417. }
  418. var savedAttribute = driver.GetAttribute ();
  419. // Draw 3D effects
  420. if (Effect3D) {
  421. driver.SetAttribute (new Attribute (Effect3DBrush));
  422. var effectBorder = new Rect () {
  423. X = borderRect.X + Effect3DOffset.X,
  424. Y = borderRect.Y + Effect3DOffset.Y,
  425. Width = ActualWidth,
  426. Height = ActualHeight
  427. };
  428. Child.Clear (effectBorder);
  429. }
  430. // Draw border thickness
  431. driver.SetAttribute (new Attribute (BorderBrush));
  432. Child.Clear (borderRect);
  433. borderRect = new Rect () {
  434. X = borderRect.X + borderThickness.Left,
  435. Y = borderRect.Y + borderThickness.Top,
  436. Width = Math.Max (borderRect.Width - borderThickness.Right - borderThickness.Left, 0),
  437. Height = Math.Max (borderRect.Height - borderThickness.Bottom - borderThickness.Top, 0)
  438. };
  439. if (borderRect != scrRect) {
  440. // Draw padding
  441. driver.SetAttribute (new Attribute (Background));
  442. Child.Clear (borderRect);
  443. }
  444. driver.SetAttribute (savedAttribute);
  445. // Draw margin frame
  446. if (Parent?.Border != null) {
  447. var sumPadding = GetSumThickness ();
  448. borderRect = new Rect () {
  449. X = scrRect.X + sumPadding.Left,
  450. Y = scrRect.Y + sumPadding.Top,
  451. Width = Math.Max (scrRect.Width - sumPadding.Right - sumPadding.Left, 0),
  452. Height = Math.Max (scrRect.Height - sumPadding.Bottom - sumPadding.Top, 0)
  453. };
  454. } else {
  455. borderRect = new Rect () {
  456. X = borderRect.X + padding.Left,
  457. Y = borderRect.Y + padding.Top,
  458. Width = Math.Max (borderRect.Width - padding.Right - padding.Left, 0),
  459. Height = Math.Max (borderRect.Height - padding.Bottom - padding.Top, 0)
  460. };
  461. }
  462. if (borderRect.Width > 0 && borderRect.Height > 0) {
  463. driver.DrawWindowFrame (borderRect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill: true, this);
  464. }
  465. }
  466. private void DrawChildBorder (Rect frame)
  467. {
  468. var drawMarginFrame = DrawMarginFrame ? 1 : 0;
  469. var sumThickness = GetSumThickness ();
  470. var padding = Padding;
  471. var effect3DOffset = Effect3DOffset;
  472. var driver = Application.Driver;
  473. var savedAttribute = driver.GetAttribute ();
  474. driver.SetAttribute (new Attribute (BorderBrush));
  475. // Draw the upper BorderThickness
  476. for (int r = frame.Y - drawMarginFrame - sumThickness.Top;
  477. r < frame.Y - drawMarginFrame - padding.Top; r++) {
  478. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  479. c < frame.Right + drawMarginFrame + sumThickness.Right; c++) {
  480. AddRuneAt (driver, c, r, ' ');
  481. }
  482. }
  483. // Draw the left BorderThickness
  484. for (int r = frame.Y - drawMarginFrame - padding.Top;
  485. r < frame.Bottom + drawMarginFrame + padding.Bottom; r++) {
  486. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  487. c < frame.X - drawMarginFrame - padding.Left; c++) {
  488. AddRuneAt (driver, c, r, ' ');
  489. }
  490. }
  491. // Draw the right BorderThickness
  492. for (int r = frame.Y - drawMarginFrame - padding.Top;
  493. r < frame.Bottom + drawMarginFrame + padding.Bottom; r++) {
  494. for (int c = frame.Right + drawMarginFrame + padding.Right;
  495. c < frame.Right + drawMarginFrame + sumThickness.Right; c++) {
  496. AddRuneAt (driver, c, r, ' ');
  497. }
  498. }
  499. // Draw the lower BorderThickness
  500. for (int r = frame.Bottom + drawMarginFrame + padding.Bottom;
  501. r < frame.Bottom + drawMarginFrame + sumThickness.Bottom; r++) {
  502. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  503. c < frame.Right + drawMarginFrame + sumThickness.Right; c++) {
  504. AddRuneAt (driver, c, r, ' ');
  505. }
  506. }
  507. driver.SetAttribute (new Attribute (Background));
  508. // Draw the upper Padding
  509. for (int r = frame.Y - drawMarginFrame - padding.Top;
  510. r < frame.Y - drawMarginFrame; r++) {
  511. for (int c = frame.X - drawMarginFrame - padding.Left;
  512. c < frame.Right + drawMarginFrame + padding.Right; c++) {
  513. AddRuneAt (driver, c, r, ' ');
  514. }
  515. }
  516. // Draw the left Padding
  517. for (int r = frame.Y - drawMarginFrame;
  518. r < frame.Bottom + drawMarginFrame; r++) {
  519. for (int c = frame.X - drawMarginFrame - padding.Left;
  520. c < frame.X - drawMarginFrame; c++) {
  521. AddRuneAt (driver, c, r, ' ');
  522. }
  523. }
  524. // Draw the right Padding
  525. for (int r = frame.Y - drawMarginFrame;
  526. r < frame.Bottom + drawMarginFrame; r++) {
  527. for (int c = frame.Right + drawMarginFrame;
  528. c < frame.Right + drawMarginFrame + padding.Right; c++) {
  529. AddRuneAt (driver, c, r, ' ');
  530. }
  531. }
  532. // Draw the lower Padding
  533. for (int r = frame.Bottom + drawMarginFrame;
  534. r < frame.Bottom + drawMarginFrame + padding.Bottom; r++) {
  535. for (int c = frame.X - drawMarginFrame - padding.Left;
  536. c < frame.Right + drawMarginFrame + padding.Right; c++) {
  537. AddRuneAt (driver, c, r, ' ');
  538. }
  539. }
  540. driver.SetAttribute (savedAttribute);
  541. // Draw the MarginFrame
  542. var rect = new Rect () {
  543. X = frame.X - drawMarginFrame,
  544. Y = frame.Y - drawMarginFrame,
  545. Width = frame.Width + (2 * drawMarginFrame),
  546. Height = frame.Height + (2 * drawMarginFrame)
  547. };
  548. if (rect.Width > 0 && rect.Height > 0) {
  549. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill: true, this);
  550. }
  551. if (Effect3D) {
  552. driver.SetAttribute (new Attribute (Effect3DBrush));
  553. // Draw the upper Effect3D
  554. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  555. r < frame.Y - drawMarginFrame - sumThickness.Top; r++) {
  556. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  557. c < frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X; c++) {
  558. AddRuneAt (driver, c, r, ' ');
  559. }
  560. }
  561. // Draw the left Effect3D
  562. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  563. r < frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y; r++) {
  564. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  565. c < frame.X - drawMarginFrame - sumThickness.Left; c++) {
  566. AddRuneAt (driver, c, r, ' ');
  567. }
  568. }
  569. // Draw the right Effect3D
  570. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  571. r < frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y; r++) {
  572. for (int c = frame.Right + drawMarginFrame + sumThickness.Right;
  573. c < frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X; c++) {
  574. AddRuneAt (driver, c, r, ' ');
  575. }
  576. }
  577. // Draw the lower Effect3D
  578. for (int r = frame.Bottom + drawMarginFrame + sumThickness.Bottom;
  579. r < frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y; r++) {
  580. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  581. c < frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X; c++) {
  582. AddRuneAt (driver, c, r, ' ');
  583. }
  584. }
  585. }
  586. }
  587. private void DrawParentBorder (Rect frame)
  588. {
  589. var drawMarginFrame = DrawMarginFrame ? 1 : 0;
  590. var sumThickness = GetSumThickness ();
  591. var borderThickness = BorderThickness;
  592. var effect3DOffset = Effect3DOffset;
  593. var driver = Application.Driver;
  594. var savedAttribute = driver.GetAttribute ();
  595. driver.SetAttribute (new Attribute (BorderBrush));
  596. // Draw the upper BorderThickness
  597. for (int r = frame.Y;
  598. r < Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r++) {
  599. for (int c = frame.X;
  600. c < frame.Right; c++) {
  601. AddRuneAt (driver, c, r, ' ');
  602. }
  603. }
  604. // Draw the left BorderThickness
  605. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  606. r < frame.Bottom - borderThickness.Bottom; r++) {
  607. for (int c = frame.X;
  608. c < Math.Min (frame.X + borderThickness.Left, frame.Right); c++) {
  609. AddRuneAt (driver, c, r, ' ');
  610. }
  611. }
  612. // Draw the right BorderThickness
  613. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  614. r < frame.Bottom - borderThickness.Bottom; r++) {
  615. for (int c = Math.Max (frame.Right - borderThickness.Right, frame.X);
  616. c < frame.Right; c++) {
  617. AddRuneAt (driver, c, r, ' ');
  618. }
  619. }
  620. // Draw the lower BorderThickness
  621. for (int r = Math.Max (frame.Bottom - borderThickness.Bottom, frame.Y);
  622. r < frame.Bottom; r++) {
  623. for (int c = frame.X;
  624. c < frame.Right; c++) {
  625. AddRuneAt (driver, c, r, ' ');
  626. }
  627. }
  628. driver.SetAttribute (new Attribute (Background));
  629. // Draw the upper Padding
  630. for (int r = frame.Y + borderThickness.Top;
  631. r < Math.Min (frame.Y + sumThickness.Top, frame.Bottom - borderThickness.Bottom); r++) {
  632. for (int c = frame.X + borderThickness.Left;
  633. c < frame.Right - borderThickness.Right; c++) {
  634. AddRuneAt (driver, c, r, ' ');
  635. }
  636. }
  637. // Draw the left Padding
  638. for (int r = frame.Y + sumThickness.Top;
  639. r < frame.Bottom - sumThickness.Bottom; r++) {
  640. for (int c = frame.X + borderThickness.Left;
  641. c < Math.Min (frame.X + sumThickness.Left, frame.Right - borderThickness.Right); c++) {
  642. AddRuneAt (driver, c, r, ' ');
  643. }
  644. }
  645. // Draw the right Padding
  646. for (int r = frame.Y + sumThickness.Top;
  647. r < frame.Bottom - sumThickness.Bottom; r++) {
  648. for (int c = Math.Max (frame.Right - sumThickness.Right, frame.X + sumThickness.Left);
  649. c < Math.Max (frame.Right - borderThickness.Right, frame.X + sumThickness.Left); c++) {
  650. AddRuneAt (driver, c, r, ' ');
  651. }
  652. }
  653. // Draw the lower Padding
  654. for (int r = Math.Max (frame.Bottom - sumThickness.Bottom, frame.Y + borderThickness.Top);
  655. r < frame.Bottom - borderThickness.Bottom; r++) {
  656. for (int c = frame.X + borderThickness.Left;
  657. c < frame.Right - borderThickness.Right; c++) {
  658. AddRuneAt (driver, c, r, ' ');
  659. }
  660. }
  661. driver.SetAttribute (savedAttribute);
  662. // Draw the MarginFrame
  663. var rect = new Rect () {
  664. X = frame.X + sumThickness.Left,
  665. Y = frame.Y + sumThickness.Top,
  666. Width = Math.Max (frame.Width - sumThickness.Right - sumThickness.Left, 0),
  667. Height = Math.Max (frame.Height - sumThickness.Bottom - sumThickness.Top, 0)
  668. };
  669. if (rect.Width > 0 && rect.Height > 0) {
  670. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill: true, this);
  671. }
  672. if (Effect3D) {
  673. driver.SetAttribute (new Attribute (Effect3DBrush));
  674. // Draw the upper Effect3D
  675. for (int r = frame.Y + effect3DOffset.Y;
  676. r < frame.Y; r++) {
  677. for (int c = frame.X + effect3DOffset.X;
  678. c < frame.Right + effect3DOffset.X; c++) {
  679. AddRuneAt (driver, c, r, ' ');
  680. }
  681. }
  682. // Draw the left Effect3D
  683. for (int r = frame.Y + effect3DOffset.Y;
  684. r < frame.Bottom + effect3DOffset.Y; r++) {
  685. for (int c = frame.X + effect3DOffset.X;
  686. c < frame.X; c++) {
  687. AddRuneAt (driver, c, r, ' ');
  688. }
  689. }
  690. // Draw the right Effect3D
  691. for (int r = frame.Y + effect3DOffset.Y;
  692. r < frame.Bottom + effect3DOffset.Y; r++) {
  693. for (int c = frame.Right;
  694. c < frame.Right + effect3DOffset.X; c++) {
  695. AddRuneAt (driver, c, r, ' ');
  696. }
  697. }
  698. // Draw the lower Effect3D
  699. for (int r = frame.Bottom;
  700. r < frame.Bottom + effect3DOffset.Y; r++) {
  701. for (int c = frame.X + effect3DOffset.X;
  702. c < frame.Right + effect3DOffset.X; c++) {
  703. AddRuneAt (driver, c, r, ' ');
  704. }
  705. }
  706. }
  707. }
  708. private void AddRuneAt (ConsoleDriver driver, int col, int row, Rune ch)
  709. {
  710. driver.Move (col, row);
  711. driver.AddRune (ch);
  712. }
  713. /// <summary>
  714. /// Drawn the view text from a <see cref="View"/>.
  715. /// </summary>
  716. public void DrawTitle (View view, Rect rect)
  717. {
  718. var driver = Application.Driver;
  719. if (BorderStyle != BorderStyle.None) {
  720. driver.SetAttribute (view.GetNormalColor ());
  721. if (view.HasFocus) {
  722. driver.SetAttribute (view.ColorScheme.HotNormal);
  723. }
  724. var padding = GetSumThickness ();
  725. driver.DrawWindowTitle (rect, view.Text,
  726. padding.Left, padding.Top, padding.Right, padding.Bottom);
  727. }
  728. driver.SetAttribute (view.GetNormalColor ());
  729. }
  730. /// <summary>
  731. /// Invoke the <see cref="BorderChanged"/> event.
  732. /// </summary>
  733. public virtual void OnBorderChanged ()
  734. {
  735. BorderChanged?.Invoke (this);
  736. }
  737. }
  738. }