Border.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. var driver = Application.Driver;
  325. if (Parent?.Border == null) {
  326. return Math.Min (Child?.Frame.Width + (2 * marginFrame) + Padding.Right
  327. + BorderThickness.Right + Padding.Left + BorderThickness.Left ?? 0, driver.Cols);
  328. }
  329. return Math.Min (Parent.Frame.Width, driver.Cols);
  330. }
  331. }
  332. /// <summary>
  333. /// Gets the rendered height of this element.
  334. /// </summary>
  335. public int ActualHeight {
  336. get {
  337. var driver = Application.Driver;
  338. if (Parent?.Border == null) {
  339. return Math.Min (Child?.Frame.Height + (2 * marginFrame) + Padding.Bottom
  340. + BorderThickness.Bottom + Padding.Top + BorderThickness.Top ?? 0, driver.Rows);
  341. }
  342. return Math.Min (Parent.Frame.Height, driver.Rows);
  343. }
  344. }
  345. /// <summary>
  346. /// Gets or sets the single child element of a <see cref="View"/>.
  347. /// </summary>
  348. public View Child { get; set; }
  349. /// <summary>
  350. /// Gets the parent <see cref="Child"/> parent if any.
  351. /// </summary>
  352. public View Parent { get => Child?.SuperView; }
  353. /// <summary>
  354. /// Gets or private sets by the <see cref="ToplevelContainer"/>
  355. /// </summary>
  356. public ToplevelContainer ChildContainer { get; private set; }
  357. /// <summary>
  358. /// Gets or sets the 3D effect around the <see cref="Border"/>.
  359. /// </summary>
  360. public bool Effect3D { get; set; }
  361. /// <summary>
  362. /// Get or sets the offset start position for the <see cref="Effect3D"/>
  363. /// </summary>
  364. public Point Effect3DOffset { get; set; } = new Point (1, 1);
  365. /// <summary>
  366. /// Gets or sets the color for the <see cref="Border"/>
  367. /// </summary>
  368. public Attribute? Effect3DBrush { get; set; }
  369. /// <summary>
  370. /// Calculate the sum of the <see cref="Padding"/> and the <see cref="BorderThickness"/>
  371. /// </summary>
  372. /// <returns>The total of the <see cref="Border"/> <see cref="Thickness"/></returns>
  373. public Thickness GetSumThickness ()
  374. {
  375. return new Thickness () {
  376. Left = Padding.Left + BorderThickness.Left,
  377. Top = Padding.Top + BorderThickness.Top,
  378. Right = Padding.Right + BorderThickness.Right,
  379. Bottom = Padding.Bottom + BorderThickness.Bottom
  380. };
  381. }
  382. /// <summary>
  383. /// Drawn the <see cref="BorderThickness"/> more the <see cref="Padding"/>
  384. /// more the <see cref="Border.BorderStyle"/> and the <see cref="Effect3D"/>.
  385. /// </summary>
  386. public void DrawContent ()
  387. {
  388. if (Parent?.Border != null) {
  389. DrawParentBorder (Parent.ViewToScreen (Parent.Bounds));
  390. } else {
  391. DrawChildBorder (Child.ViewToScreen (Child.Bounds));
  392. }
  393. }
  394. /// <summary>
  395. /// Same as <see cref="DrawContent"/> but drawing full frames for all borders.
  396. /// </summary>
  397. public void DrawFullContent ()
  398. {
  399. var borderThickness = BorderThickness;
  400. var padding = Padding;
  401. var marginFrame = DrawMarginFrame ? 1 : 0;
  402. var driver = Application.Driver;
  403. Rect scrRect;
  404. if (Parent?.Border != null) {
  405. scrRect = Parent.ViewToScreen (Parent.Bounds);
  406. } else {
  407. scrRect = Child.ViewToScreen (Child.Bounds);
  408. }
  409. Rect borderRect;
  410. if (Parent?.Border != null) {
  411. borderRect = scrRect;
  412. } else {
  413. borderRect = new Rect () {
  414. X = scrRect.X - marginFrame - padding.Left - borderThickness.Left,
  415. Y = scrRect.Y - marginFrame - padding.Top - borderThickness.Top,
  416. Width = ActualWidth,
  417. Height = ActualHeight
  418. };
  419. }
  420. var savedAttribute = driver.GetAttribute ();
  421. // Draw 3D effects
  422. if (Effect3D) {
  423. driver.SetAttribute (GetEffect3DBrush ());
  424. var effectBorder = new Rect () {
  425. X = borderRect.X + Effect3DOffset.X,
  426. Y = borderRect.Y + Effect3DOffset.Y,
  427. Width = ActualWidth,
  428. Height = ActualHeight
  429. };
  430. //Child.Clear (effectBorder);
  431. for (int r = effectBorder.Y; r < Math.Min (effectBorder.Bottom, driver.Rows); r++) {
  432. for (int c = effectBorder.X; c < Math.Min (effectBorder.Right, driver.Cols); c++) {
  433. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  434. }
  435. }
  436. }
  437. // Draw border thickness
  438. driver.SetAttribute (new Attribute (BorderBrush));
  439. Child.Clear (borderRect);
  440. borderRect = new Rect () {
  441. X = borderRect.X + borderThickness.Left,
  442. Y = borderRect.Y + borderThickness.Top,
  443. Width = Math.Max (borderRect.Width - borderThickness.Right - borderThickness.Left, 0),
  444. Height = Math.Max (borderRect.Height - borderThickness.Bottom - borderThickness.Top, 0)
  445. };
  446. if (borderRect != scrRect) {
  447. // Draw padding
  448. driver.SetAttribute (new Attribute (Background));
  449. Child.Clear (borderRect);
  450. }
  451. driver.SetAttribute (savedAttribute);
  452. // Draw margin frame
  453. if (Parent?.Border != null) {
  454. var sumPadding = GetSumThickness ();
  455. borderRect = new Rect () {
  456. X = scrRect.X + sumPadding.Left,
  457. Y = scrRect.Y + sumPadding.Top,
  458. Width = Math.Max (scrRect.Width - sumPadding.Right - sumPadding.Left, 0),
  459. Height = Math.Max (scrRect.Height - sumPadding.Bottom - sumPadding.Top, 0)
  460. };
  461. } else {
  462. borderRect = new Rect () {
  463. X = borderRect.X + padding.Left,
  464. Y = borderRect.Y + padding.Top,
  465. Width = Math.Max (borderRect.Width - padding.Right - padding.Left, 0),
  466. Height = Math.Max (borderRect.Height - padding.Bottom - padding.Top, 0)
  467. };
  468. }
  469. if (borderRect.Width > 0 && borderRect.Height > 0) {
  470. driver.DrawWindowFrame (borderRect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill: true, this);
  471. }
  472. }
  473. private void DrawChildBorder (Rect frame)
  474. {
  475. var drawMarginFrame = DrawMarginFrame ? 1 : 0;
  476. var sumThickness = GetSumThickness ();
  477. var padding = Padding;
  478. var effect3DOffset = Effect3DOffset;
  479. var driver = Application.Driver;
  480. var savedAttribute = driver.GetAttribute ();
  481. driver.SetAttribute (new Attribute (BorderBrush));
  482. // Draw the upper BorderThickness
  483. for (int r = frame.Y - drawMarginFrame - sumThickness.Top;
  484. r < frame.Y - drawMarginFrame - padding.Top; r++) {
  485. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  486. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  487. AddRuneAt (driver, c, r, ' ');
  488. }
  489. }
  490. // Draw the left BorderThickness
  491. for (int r = frame.Y - drawMarginFrame - padding.Top;
  492. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  493. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  494. c < frame.X - drawMarginFrame - padding.Left; c++) {
  495. AddRuneAt (driver, c, r, ' ');
  496. }
  497. }
  498. // Draw the right 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.Right + drawMarginFrame + padding.Right;
  502. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  503. AddRuneAt (driver, c, r, ' ');
  504. }
  505. }
  506. // Draw the lower BorderThickness
  507. for (int r = frame.Bottom + drawMarginFrame + padding.Bottom;
  508. r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom, driver.Rows); r++) {
  509. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  510. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  511. AddRuneAt (driver, c, r, ' ');
  512. }
  513. }
  514. driver.SetAttribute (new Attribute (Background));
  515. // Draw the upper Padding
  516. for (int r = frame.Y - drawMarginFrame - padding.Top;
  517. r < frame.Y - drawMarginFrame; r++) {
  518. for (int c = frame.X - drawMarginFrame - padding.Left;
  519. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  520. AddRuneAt (driver, c, r, ' ');
  521. }
  522. }
  523. // Draw the left Padding
  524. for (int r = frame.Y - drawMarginFrame;
  525. r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  526. for (int c = frame.X - drawMarginFrame - padding.Left;
  527. c < frame.X - drawMarginFrame; c++) {
  528. AddRuneAt (driver, c, r, ' ');
  529. }
  530. }
  531. // Draw the right Padding
  532. for (int r = frame.Y - drawMarginFrame;
  533. r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  534. for (int c = frame.Right + drawMarginFrame;
  535. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  536. AddRuneAt (driver, c, r, ' ');
  537. }
  538. }
  539. // Draw the lower Padding
  540. for (int r = frame.Bottom + drawMarginFrame;
  541. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  542. for (int c = frame.X - drawMarginFrame - padding.Left;
  543. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  544. AddRuneAt (driver, c, r, ' ');
  545. }
  546. }
  547. driver.SetAttribute (savedAttribute);
  548. // Draw the MarginFrame
  549. var rect = new Rect () {
  550. X = frame.X - drawMarginFrame,
  551. Y = frame.Y - drawMarginFrame,
  552. Width = frame.Width + (2 * drawMarginFrame),
  553. Height = frame.Height + (2 * drawMarginFrame)
  554. };
  555. if (rect.Width > 0 && rect.Height > 0) {
  556. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill: true, this);
  557. }
  558. if (Effect3D) {
  559. driver.SetAttribute (GetEffect3DBrush ());
  560. // Draw the upper Effect3D
  561. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  562. r < frame.Y - drawMarginFrame - sumThickness.Top; r++) {
  563. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  564. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  565. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  566. }
  567. }
  568. // Draw the left Effect3D
  569. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  570. r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  571. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  572. c < frame.X - drawMarginFrame - sumThickness.Left; c++) {
  573. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  574. }
  575. }
  576. // Draw the right 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.Right + drawMarginFrame + sumThickness.Right;
  580. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  581. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  582. }
  583. }
  584. // Draw the lower Effect3D
  585. for (int r = frame.Bottom + drawMarginFrame + sumThickness.Bottom;
  586. r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  587. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  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. }
  593. }
  594. private void DrawParentBorder (Rect frame)
  595. {
  596. var sumThickness = GetSumThickness ();
  597. var borderThickness = BorderThickness;
  598. var effect3DOffset = Effect3DOffset;
  599. var driver = Application.Driver;
  600. var savedAttribute = driver.GetAttribute ();
  601. driver.SetAttribute (new Attribute (BorderBrush));
  602. // Draw the upper BorderThickness
  603. for (int r = frame.Y;
  604. r < Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r++) {
  605. for (int c = frame.X;
  606. c < Math.Min (frame.Right, driver.Cols); c++) {
  607. AddRuneAt (driver, c, r, ' ');
  608. }
  609. }
  610. // Draw the left BorderThickness
  611. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  612. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  613. for (int c = frame.X;
  614. c < Math.Min (frame.X + borderThickness.Left, frame.Right); c++) {
  615. AddRuneAt (driver, c, r, ' ');
  616. }
  617. }
  618. // Draw the right 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 = Math.Max (frame.Right - borderThickness.Right, frame.X);
  622. c < Math.Min (frame.Right, driver.Cols); c++) {
  623. AddRuneAt (driver, c, r, ' ');
  624. }
  625. }
  626. // Draw the lower BorderThickness
  627. for (int r = Math.Max (frame.Bottom - borderThickness.Bottom, frame.Y);
  628. r < Math.Min (frame.Bottom, driver.Rows); r++) {
  629. for (int c = frame.X;
  630. c < Math.Min (frame.Right, driver.Cols); c++) {
  631. AddRuneAt (driver, c, r, ' ');
  632. }
  633. }
  634. driver.SetAttribute (new Attribute (Background));
  635. // Draw the upper Padding
  636. for (int r = frame.Y + borderThickness.Top;
  637. r < Math.Min (frame.Y + sumThickness.Top, frame.Bottom - borderThickness.Bottom); r++) {
  638. for (int c = frame.X + borderThickness.Left;
  639. c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  640. AddRuneAt (driver, c, r, ' ');
  641. }
  642. }
  643. // Draw the left Padding
  644. for (int r = frame.Y + sumThickness.Top;
  645. r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  646. for (int c = frame.X + borderThickness.Left;
  647. c < Math.Min (frame.X + sumThickness.Left, frame.Right - borderThickness.Right); c++) {
  648. AddRuneAt (driver, c, r, ' ');
  649. }
  650. }
  651. // Draw the right Padding
  652. for (int r = frame.Y + sumThickness.Top;
  653. r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  654. for (int c = Math.Max (frame.Right - sumThickness.Right, frame.X + sumThickness.Left);
  655. c < Math.Max (frame.Right - borderThickness.Right, frame.X + sumThickness.Left); c++) {
  656. AddRuneAt (driver, c, r, ' ');
  657. }
  658. }
  659. // Draw the lower Padding
  660. for (int r = Math.Max (frame.Bottom - sumThickness.Bottom, frame.Y + borderThickness.Top);
  661. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  662. for (int c = frame.X + borderThickness.Left;
  663. c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  664. AddRuneAt (driver, c, r, ' ');
  665. }
  666. }
  667. driver.SetAttribute (savedAttribute);
  668. // Draw the MarginFrame
  669. var rect = new Rect () {
  670. X = frame.X + sumThickness.Left,
  671. Y = frame.Y + sumThickness.Top,
  672. Width = Math.Max (frame.Width - sumThickness.Right - sumThickness.Left, 0),
  673. Height = Math.Max (frame.Height - sumThickness.Bottom - sumThickness.Top, 0)
  674. };
  675. if (rect.Width > 0 && rect.Height > 0) {
  676. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill: true, this);
  677. }
  678. if (Effect3D) {
  679. driver.SetAttribute (GetEffect3DBrush ());
  680. // Draw the upper Effect3D
  681. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  682. r < frame.Y; r++) {
  683. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  684. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  685. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  686. }
  687. }
  688. // Draw the left Effect3D
  689. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  690. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  691. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  692. c < frame.X; c++) {
  693. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  694. }
  695. }
  696. // Draw the right 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 = frame.Right;
  700. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  701. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  702. }
  703. }
  704. // Draw the lower Effect3D
  705. for (int r = frame.Bottom;
  706. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  707. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  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. }
  713. }
  714. private Attribute GetEffect3DBrush ()
  715. {
  716. return Effect3DBrush == null
  717. ? new Attribute (Color.Gray, Color.DarkGray)
  718. : (Attribute)Effect3DBrush;
  719. }
  720. private void AddRuneAt (ConsoleDriver driver, int col, int row, Rune ch)
  721. {
  722. driver.Move (col, row);
  723. driver.AddRune (ch);
  724. }
  725. /// <summary>
  726. /// Drawn the view text from a <see cref="View"/>.
  727. /// </summary>
  728. public void DrawTitle (View view, Rect rect)
  729. {
  730. var driver = Application.Driver;
  731. if (BorderStyle != BorderStyle.None) {
  732. driver.SetAttribute (view.GetNormalColor ());
  733. if (view.HasFocus) {
  734. driver.SetAttribute (view.ColorScheme.HotNormal);
  735. }
  736. var padding = GetSumThickness ();
  737. driver.DrawWindowTitle (rect, view.Text,
  738. padding.Left, padding.Top, padding.Right, padding.Bottom);
  739. }
  740. driver.SetAttribute (view.GetNormalColor ());
  741. }
  742. /// <summary>
  743. /// Invoke the <see cref="BorderChanged"/> event.
  744. /// </summary>
  745. public virtual void OnBorderChanged ()
  746. {
  747. BorderChanged?.Invoke (this);
  748. }
  749. }
  750. }