Border.cs 28 KB

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