Border.cs 30 KB

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