Border.cs 30 KB

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