Border.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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. Title = (ustring)title
  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. if (view == Border.Child) {
  219. base.Remove (view);
  220. } else {
  221. Border.Child.Remove (view);
  222. }
  223. if (Border.Child.InternalSubviews.Count < 1) {
  224. CanFocus = false;
  225. }
  226. RemoveMenuStatusBar (view);
  227. }
  228. /// <inheritdoc/>
  229. public override void RemoveAll ()
  230. {
  231. Border.Child.RemoveAll ();
  232. }
  233. /// <inheritdoc/>
  234. public override void Redraw (Rect bounds)
  235. {
  236. if (!NeedDisplay.IsEmpty) {
  237. Driver.SetAttribute (GetNormalColor ());
  238. Clear ();
  239. }
  240. var savedClip = Border.Child.ClipToBounds ();
  241. Border.Child.Redraw (Border.Child.Bounds);
  242. Driver.Clip = savedClip;
  243. ClearLayoutNeeded ();
  244. ClearNeedsDisplay ();
  245. Driver.SetAttribute (GetNormalColor ());
  246. Border.DrawContent (this, false);
  247. if (HasFocus)
  248. Driver.SetAttribute (ColorScheme.HotNormal);
  249. if (Border.DrawMarginFrame) {
  250. if (!ustring.IsNullOrEmpty (Border.Title))
  251. Border.DrawTitle (this);
  252. else
  253. Border.DrawTitle (this, Frame);
  254. }
  255. Driver.SetAttribute (GetNormalColor ());
  256. // Checks if there are any SuperView view which intersect with this window.
  257. if (SuperView != null) {
  258. SuperView.SetNeedsLayout ();
  259. SuperView.SetNeedsDisplay ();
  260. }
  261. }
  262. /// <inheritdoc/>
  263. public override void OnCanFocusChanged ()
  264. {
  265. if (Border?.Child != null) {
  266. Border.Child.CanFocus = CanFocus;
  267. }
  268. base.OnCanFocusChanged ();
  269. }
  270. }
  271. private class ChildContentView : View {
  272. View instance;
  273. public ChildContentView (Rect frame, View instance) : base (frame)
  274. {
  275. this.instance = instance;
  276. }
  277. public ChildContentView (View instance)
  278. {
  279. this.instance = instance;
  280. }
  281. public override bool MouseEvent (MouseEvent mouseEvent)
  282. {
  283. return instance.MouseEvent (mouseEvent);
  284. }
  285. }
  286. /// <summary>
  287. /// Invoked when any property of Border changes (except <see cref="Child"/>).
  288. /// </summary>
  289. public event Action<Border> BorderChanged;
  290. private BorderStyle borderStyle;
  291. private bool drawMarginFrame;
  292. private Thickness borderThickness;
  293. private Color? borderBrush;
  294. private Color? background;
  295. private Thickness padding;
  296. private bool effect3D;
  297. private Point effect3DOffset = new Point (1, 1);
  298. private Attribute? effect3DBrush;
  299. private ustring title = ustring.Empty;
  300. private View child;
  301. /// <summary>
  302. /// Specifies the <see cref="Gui.BorderStyle"/> for a view.
  303. /// </summary>
  304. public BorderStyle BorderStyle {
  305. get => borderStyle;
  306. set {
  307. if (value != BorderStyle.None && !drawMarginFrame) {
  308. // Ensures drawn the border lines.
  309. drawMarginFrame = true;
  310. }
  311. borderStyle = value;
  312. OnBorderChanged ();
  313. }
  314. }
  315. /// <summary>
  316. /// Gets or sets if a margin frame is drawn around the <see cref="Child"/> regardless the <see cref="BorderStyle"/>
  317. /// </summary>
  318. public bool DrawMarginFrame {
  319. get => drawMarginFrame;
  320. set {
  321. if (borderStyle != BorderStyle.None
  322. && (!value || !drawMarginFrame)) {
  323. // Ensures drawn the border lines.
  324. drawMarginFrame = true;
  325. } else {
  326. drawMarginFrame = value;
  327. }
  328. OnBorderChanged ();
  329. }
  330. }
  331. /// <summary>
  332. /// Gets or sets the relative <see cref="Thickness"/> of a <see cref="Border"/>.
  333. /// </summary>
  334. public Thickness BorderThickness {
  335. get => borderThickness;
  336. set {
  337. borderThickness = value;
  338. OnBorderChanged ();
  339. }
  340. }
  341. /// <summary>
  342. /// Gets or sets the <see cref="Color"/> that draws the outer border color.
  343. /// </summary>
  344. public Color BorderBrush {
  345. get => borderBrush != null ? (Color)borderBrush : (Color)(-1);
  346. set {
  347. if (Enum.IsDefined (typeof (Color), value)) {
  348. borderBrush = value;
  349. OnBorderChanged ();
  350. }
  351. }
  352. }
  353. /// <summary>
  354. /// Gets or sets the <see cref="Color"/> that fills the area between the bounds of a <see cref="Border"/>.
  355. /// </summary>
  356. public Color Background {
  357. get => background != null ? (Color)background : (Color)(-1);
  358. set {
  359. if (Enum.IsDefined (typeof (Color), value)) {
  360. background = value;
  361. OnBorderChanged ();
  362. }
  363. }
  364. }
  365. /// <summary>
  366. /// Gets or sets a <see cref="Thickness"/> value that describes the amount of space between a
  367. /// <see cref="Border"/> and its child element.
  368. /// </summary>
  369. public Thickness Padding {
  370. get => padding;
  371. set {
  372. padding = value;
  373. OnBorderChanged ();
  374. }
  375. }
  376. /// <summary>
  377. /// Gets the rendered width of this element.
  378. /// </summary>
  379. public int ActualWidth {
  380. get {
  381. var driver = Application.Driver;
  382. if (Parent?.Border == null) {
  383. return Math.Min (Child?.Frame.Width + (2 * marginFrame) + Padding.Right
  384. + BorderThickness.Right + Padding.Left + BorderThickness.Left ?? 0, driver.Cols);
  385. }
  386. return Math.Min (Parent.Frame.Width, driver.Cols);
  387. }
  388. }
  389. /// <summary>
  390. /// Gets the rendered height of this element.
  391. /// </summary>
  392. public int ActualHeight {
  393. get {
  394. var driver = Application.Driver;
  395. if (Parent?.Border == null) {
  396. return Math.Min (Child?.Frame.Height + (2 * marginFrame) + Padding.Bottom
  397. + BorderThickness.Bottom + Padding.Top + BorderThickness.Top ?? 0, driver.Rows);
  398. }
  399. return Math.Min (Parent.Frame.Height, driver.Rows);
  400. }
  401. }
  402. /// <summary>
  403. /// Gets or sets the single child element of a <see cref="View"/>.
  404. /// </summary>
  405. public View Child {
  406. get => child;
  407. set {
  408. child = value;
  409. if (child != null && Parent != null) {
  410. Parent.Removed += Parent_Removed;
  411. }
  412. }
  413. }
  414. private void Parent_Removed (View obj)
  415. {
  416. if (borderBrush != null) {
  417. BorderBrush = default;
  418. }
  419. if (background != null) {
  420. Background = default;
  421. }
  422. child.Removed -= Parent_Removed;
  423. }
  424. /// <summary>
  425. /// Gets the parent <see cref="Child"/> parent if any.
  426. /// </summary>
  427. public View Parent { get => Child?.SuperView; }
  428. /// <summary>
  429. /// Gets or private sets by the <see cref="ToplevelContainer"/>
  430. /// </summary>
  431. public ToplevelContainer ChildContainer { get; private set; }
  432. /// <summary>
  433. /// Gets or sets the 3D effect around the <see cref="Border"/>.
  434. /// </summary>
  435. public bool Effect3D {
  436. get => effect3D;
  437. set {
  438. effect3D = value;
  439. OnBorderChanged ();
  440. }
  441. }
  442. /// <summary>
  443. /// Get or sets the offset start position for the <see cref="Effect3D"/>
  444. /// </summary>
  445. public Point Effect3DOffset {
  446. get => effect3DOffset;
  447. set {
  448. effect3DOffset = value;
  449. OnBorderChanged ();
  450. }
  451. }
  452. /// <summary>
  453. /// Gets or sets the color for the <see cref="Border"/>
  454. /// </summary>
  455. public Attribute? Effect3DBrush {
  456. get => effect3DBrush;
  457. set {
  458. effect3DBrush = value;
  459. OnBorderChanged ();
  460. }
  461. }
  462. /// <summary>
  463. /// The title to be displayed for this view.
  464. /// </summary>
  465. public ustring Title {
  466. get => title;
  467. set {
  468. title = value;
  469. OnBorderChanged ();
  470. }
  471. }
  472. /// <summary>
  473. /// Calculate the sum of the <see cref="Padding"/> and the <see cref="BorderThickness"/>
  474. /// </summary>
  475. /// <returns>The total of the <see cref="Border"/> <see cref="Thickness"/></returns>
  476. public Thickness GetSumThickness ()
  477. {
  478. return new Thickness () {
  479. Left = Padding.Left + BorderThickness.Left,
  480. Top = Padding.Top + BorderThickness.Top,
  481. Right = Padding.Right + BorderThickness.Right,
  482. Bottom = Padding.Bottom + BorderThickness.Bottom
  483. };
  484. }
  485. /// <summary>
  486. /// Drawn the <see cref="BorderThickness"/> more the <see cref="Padding"/>
  487. /// more the <see cref="Border.BorderStyle"/> and the <see cref="Effect3D"/>.
  488. /// </summary>
  489. /// <param name="view">The view to draw.</param>
  490. /// <param name="fill">If it will clear or not the content area.</param>
  491. public void DrawContent (View view = null, bool fill = true)
  492. {
  493. if (Child == null) {
  494. Child = view;
  495. }
  496. if (Parent?.Border != null) {
  497. DrawParentBorder (Parent.ViewToScreen (Parent.Bounds), fill);
  498. } else {
  499. DrawChildBorder (Child.ViewToScreen (Child.Bounds), fill);
  500. }
  501. }
  502. /// <summary>
  503. /// Same as <see cref="DrawContent"/> but drawing full frames for all borders.
  504. /// </summary>
  505. public void DrawFullContent ()
  506. {
  507. var borderThickness = BorderThickness;
  508. var padding = Padding;
  509. var marginFrame = DrawMarginFrame ? 1 : 0;
  510. var driver = Application.Driver;
  511. Rect scrRect;
  512. if (Parent?.Border != null) {
  513. scrRect = Parent.ViewToScreen (Parent.Bounds);
  514. } else {
  515. scrRect = Child.ViewToScreen (Child.Bounds);
  516. }
  517. Rect borderRect;
  518. if (Parent?.Border != null) {
  519. borderRect = scrRect;
  520. } else {
  521. borderRect = new Rect () {
  522. X = scrRect.X - marginFrame - padding.Left - borderThickness.Left,
  523. Y = scrRect.Y - marginFrame - padding.Top - borderThickness.Top,
  524. Width = ActualWidth,
  525. Height = ActualHeight
  526. };
  527. }
  528. var savedAttribute = driver.GetAttribute ();
  529. // Draw 3D effects
  530. if (Effect3D) {
  531. driver.SetAttribute (GetEffect3DBrush ());
  532. var effectBorder = new Rect () {
  533. X = borderRect.X + Effect3DOffset.X,
  534. Y = borderRect.Y + Effect3DOffset.Y,
  535. Width = ActualWidth,
  536. Height = ActualHeight
  537. };
  538. //Child.Clear (effectBorder);
  539. for (int r = effectBorder.Y; r < Math.Min (effectBorder.Bottom, driver.Rows); r++) {
  540. for (int c = effectBorder.X; c < Math.Min (effectBorder.Right, driver.Cols); c++) {
  541. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  542. }
  543. }
  544. }
  545. // Draw border thickness
  546. SetBorderBrush (driver);
  547. Child.Clear (borderRect);
  548. borderRect = new Rect () {
  549. X = borderRect.X + borderThickness.Left,
  550. Y = borderRect.Y + borderThickness.Top,
  551. Width = Math.Max (borderRect.Width - borderThickness.Right - borderThickness.Left, 0),
  552. Height = Math.Max (borderRect.Height - borderThickness.Bottom - borderThickness.Top, 0)
  553. };
  554. if (borderRect != scrRect) {
  555. // Draw padding
  556. driver.SetAttribute (new Attribute (Background));
  557. Child.Clear (borderRect);
  558. }
  559. SetBorderBrushBackground (driver);
  560. // Draw margin frame
  561. if (DrawMarginFrame) {
  562. if (Parent?.Border != null) {
  563. var sumPadding = GetSumThickness ();
  564. borderRect = new Rect () {
  565. X = scrRect.X + sumPadding.Left,
  566. Y = scrRect.Y + sumPadding.Top,
  567. Width = Math.Max (scrRect.Width - sumPadding.Right - sumPadding.Left, 0),
  568. Height = Math.Max (scrRect.Height - sumPadding.Bottom - sumPadding.Top, 0)
  569. };
  570. } else {
  571. borderRect = new Rect () {
  572. X = borderRect.X + padding.Left,
  573. Y = borderRect.Y + padding.Top,
  574. Width = Math.Max (borderRect.Width - padding.Right - padding.Left, 0),
  575. Height = Math.Max (borderRect.Height - padding.Bottom - padding.Top, 0)
  576. };
  577. }
  578. if (borderRect.Width > 0 && borderRect.Height > 0) {
  579. driver.DrawWindowFrame (borderRect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill: true, this);
  580. }
  581. }
  582. driver.SetAttribute (savedAttribute);
  583. }
  584. private void DrawChildBorder (Rect frame, bool fill = true)
  585. {
  586. var drawMarginFrame = DrawMarginFrame ? 1 : 0;
  587. var sumThickness = GetSumThickness ();
  588. var padding = Padding;
  589. var effect3DOffset = Effect3DOffset;
  590. var driver = Application.Driver;
  591. var savedAttribute = driver.GetAttribute ();
  592. SetBorderBrush (driver);
  593. // Draw the upper BorderThickness
  594. for (int r = frame.Y - drawMarginFrame - sumThickness.Top;
  595. r < frame.Y - drawMarginFrame - padding.Top; r++) {
  596. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  597. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  598. AddRuneAt (driver, c, r, ' ');
  599. }
  600. }
  601. // Draw the left BorderThickness
  602. for (int r = frame.Y - drawMarginFrame - padding.Top;
  603. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  604. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  605. c < frame.X - drawMarginFrame - padding.Left; c++) {
  606. AddRuneAt (driver, c, r, ' ');
  607. }
  608. }
  609. // Draw the right BorderThickness
  610. for (int r = frame.Y - drawMarginFrame - padding.Top;
  611. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  612. for (int c = frame.Right + drawMarginFrame + padding.Right;
  613. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  614. AddRuneAt (driver, c, r, ' ');
  615. }
  616. }
  617. // Draw the lower BorderThickness
  618. for (int r = frame.Bottom + drawMarginFrame + padding.Bottom;
  619. r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom, driver.Rows); r++) {
  620. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  621. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  622. AddRuneAt (driver, c, r, ' ');
  623. }
  624. }
  625. SetBackground (driver);
  626. // Draw the upper Padding
  627. for (int r = frame.Y - drawMarginFrame - padding.Top;
  628. r < frame.Y - drawMarginFrame; r++) {
  629. for (int c = frame.X - drawMarginFrame - padding.Left;
  630. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  631. AddRuneAt (driver, c, r, ' ');
  632. }
  633. }
  634. // Draw the left Padding
  635. for (int r = frame.Y - drawMarginFrame;
  636. r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  637. for (int c = frame.X - drawMarginFrame - padding.Left;
  638. c < frame.X - drawMarginFrame; c++) {
  639. AddRuneAt (driver, c, r, ' ');
  640. }
  641. }
  642. // Draw the right Padding
  643. for (int r = frame.Y - drawMarginFrame;
  644. r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  645. for (int c = frame.Right + drawMarginFrame;
  646. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  647. AddRuneAt (driver, c, r, ' ');
  648. }
  649. }
  650. // Draw the lower Padding
  651. for (int r = frame.Bottom + drawMarginFrame;
  652. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  653. for (int c = frame.X - drawMarginFrame - padding.Left;
  654. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  655. AddRuneAt (driver, c, r, ' ');
  656. }
  657. }
  658. SetBorderBrushBackground (driver);
  659. // Draw the MarginFrame
  660. if (DrawMarginFrame) {
  661. var rect = new Rect () {
  662. X = frame.X - drawMarginFrame,
  663. Y = frame.Y - drawMarginFrame,
  664. Width = frame.Width + (2 * drawMarginFrame),
  665. Height = frame.Height + (2 * drawMarginFrame)
  666. };
  667. if (rect.Width > 0 && rect.Height > 0) {
  668. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  669. DrawTitle (Child);
  670. }
  671. }
  672. if (Effect3D) {
  673. driver.SetAttribute (GetEffect3DBrush ());
  674. // Draw the upper Effect3D
  675. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  676. r >= 0 && r < frame.Y - drawMarginFrame - sumThickness.Top; r++) {
  677. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  678. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  679. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  680. }
  681. }
  682. // Draw the left Effect3D
  683. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  684. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  685. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  686. c >= 0 && c < frame.X - drawMarginFrame - sumThickness.Left; c++) {
  687. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  688. }
  689. }
  690. // Draw the right Effect3D
  691. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  692. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  693. for (int c = frame.Right + drawMarginFrame + sumThickness.Right;
  694. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  695. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  696. }
  697. }
  698. // Draw the lower Effect3D
  699. for (int r = frame.Bottom + drawMarginFrame + sumThickness.Bottom;
  700. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  701. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  702. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  703. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  704. }
  705. }
  706. }
  707. driver.SetAttribute (savedAttribute);
  708. }
  709. private void DrawParentBorder (Rect frame, bool fill = true)
  710. {
  711. var sumThickness = GetSumThickness ();
  712. var borderThickness = BorderThickness;
  713. var effect3DOffset = Effect3DOffset;
  714. var driver = Application.Driver;
  715. var savedAttribute = driver.GetAttribute ();
  716. SetBorderBrush (driver);
  717. // Draw the upper BorderThickness
  718. for (int r = Math.Max (frame.Y, 0);
  719. r < Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r++) {
  720. for (int c = frame.X;
  721. c < Math.Min (frame.Right, driver.Cols); c++) {
  722. AddRuneAt (driver, c, r, ' ');
  723. }
  724. }
  725. // Draw the left BorderThickness
  726. for (int r = Math.Max (Math.Min (frame.Y + borderThickness.Top, frame.Bottom), 0);
  727. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  728. for (int c = frame.X;
  729. c < Math.Min (frame.X + borderThickness.Left, frame.Right); c++) {
  730. AddRuneAt (driver, c, r, ' ');
  731. }
  732. }
  733. // Draw the right BorderThickness
  734. for (int r = Math.Max (Math.Min (frame.Y + borderThickness.Top, frame.Bottom), 0);
  735. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  736. for (int c = Math.Max (frame.Right - borderThickness.Right, frame.X);
  737. c < Math.Min (frame.Right, driver.Cols); c++) {
  738. AddRuneAt (driver, c, r, ' ');
  739. }
  740. }
  741. // Draw the lower BorderThickness
  742. for (int r = Math.Max (frame.Bottom - borderThickness.Bottom, frame.Y);
  743. r < Math.Min (frame.Bottom, driver.Rows); r++) {
  744. for (int c = frame.X;
  745. c < Math.Min (frame.Right, driver.Cols); c++) {
  746. AddRuneAt (driver, c, r, ' ');
  747. }
  748. }
  749. SetBackground (driver);
  750. // Draw the upper Padding
  751. for (int r = Math.Max (frame.Y + borderThickness.Top, 0);
  752. r < Math.Min (frame.Y + sumThickness.Top, frame.Bottom - borderThickness.Bottom); r++) {
  753. for (int c = frame.X + borderThickness.Left;
  754. c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  755. AddRuneAt (driver, c, r, ' ');
  756. }
  757. }
  758. // Draw the left Padding
  759. for (int r = Math.Max (frame.Y + sumThickness.Top, 0);
  760. r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  761. for (int c = frame.X + borderThickness.Left;
  762. c < Math.Min (frame.X + sumThickness.Left, frame.Right - borderThickness.Right); c++) {
  763. AddRuneAt (driver, c, r, ' ');
  764. }
  765. }
  766. // Draw the right Padding
  767. for (int r = Math.Max (frame.Y + sumThickness.Top, 0);
  768. r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  769. for (int c = Math.Max (frame.Right - sumThickness.Right, frame.X + sumThickness.Left);
  770. c < Math.Max (frame.Right - borderThickness.Right, frame.X + sumThickness.Left); c++) {
  771. AddRuneAt (driver, c, r, ' ');
  772. }
  773. }
  774. // Draw the lower Padding
  775. for (int r = Math.Max (frame.Bottom - sumThickness.Bottom, frame.Y + borderThickness.Top);
  776. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  777. for (int c = frame.X + borderThickness.Left;
  778. c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  779. AddRuneAt (driver, c, r, ' ');
  780. }
  781. }
  782. SetBorderBrushBackground (driver);
  783. // Draw the MarginFrame
  784. if (DrawMarginFrame) {
  785. var rect = new Rect () {
  786. X = frame.X + sumThickness.Left,
  787. Y = frame.Y + sumThickness.Top,
  788. Width = Math.Max (frame.Width - sumThickness.Right - sumThickness.Left, 0),
  789. Height = Math.Max (frame.Height - sumThickness.Bottom - sumThickness.Top, 0)
  790. };
  791. if (rect.Width > 0 && rect.Height > 0) {
  792. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  793. DrawTitle (Parent);
  794. }
  795. }
  796. if (Effect3D) {
  797. driver.SetAttribute (GetEffect3DBrush ());
  798. // Draw the upper Effect3D
  799. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  800. r < frame.Y; r++) {
  801. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  802. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  803. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  804. }
  805. }
  806. // Draw the left Effect3D
  807. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  808. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  809. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  810. c < frame.X; c++) {
  811. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  812. }
  813. }
  814. // Draw the right Effect3D
  815. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  816. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  817. for (int c = frame.Right;
  818. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  819. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  820. }
  821. }
  822. // Draw the lower Effect3D
  823. for (int r = frame.Bottom;
  824. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  825. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  826. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  827. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  828. }
  829. }
  830. }
  831. driver.SetAttribute (savedAttribute);
  832. }
  833. private void SetBorderBrushBackground (ConsoleDriver driver)
  834. {
  835. if (borderBrush != null && background != null) {
  836. driver.SetAttribute (new Attribute (BorderBrush, Background));
  837. } else if (borderBrush != null && background == null) {
  838. driver.SetAttribute (new Attribute (BorderBrush, Parent.ColorScheme.Normal.Background));
  839. } else if (borderBrush == null && background != null) {
  840. driver.SetAttribute (new Attribute (Parent.ColorScheme.Normal.Foreground, Background));
  841. } else {
  842. driver.SetAttribute (Parent.ColorScheme.Normal);
  843. }
  844. }
  845. private void SetBackground (ConsoleDriver driver)
  846. {
  847. if (background != null) {
  848. driver.SetAttribute (new Attribute (Background));
  849. } else {
  850. driver.SetAttribute (new Attribute (Parent.ColorScheme.Normal.Background));
  851. }
  852. }
  853. private void SetBorderBrush (ConsoleDriver driver)
  854. {
  855. if (borderBrush != null) {
  856. driver.SetAttribute (new Attribute (BorderBrush));
  857. } else {
  858. driver.SetAttribute (new Attribute (Parent.ColorScheme.Normal.Foreground));
  859. }
  860. }
  861. private Attribute GetEffect3DBrush ()
  862. {
  863. return Effect3DBrush == null
  864. ? new Attribute (Color.Gray, Color.DarkGray)
  865. : (Attribute)Effect3DBrush;
  866. }
  867. private void AddRuneAt (ConsoleDriver driver, int col, int row, Rune ch)
  868. {
  869. if (col < driver.Cols && row < driver.Rows && col > 0 && driver.Contents [row, col, 2] == 0
  870. && Rune.ColumnWidth ((char)driver.Contents [row, col - 1, 0]) > 1) {
  871. driver.Contents [row, col, 1] = driver.GetAttribute ();
  872. return;
  873. }
  874. driver.Move (col, row);
  875. driver.AddRune (ch);
  876. }
  877. /// <summary>
  878. /// Draws the view <see cref="Title"/> to the screen.
  879. /// </summary>
  880. /// <param name="view">The view.</param>
  881. public void DrawTitle (View view)
  882. {
  883. var driver = Application.Driver;
  884. if (DrawMarginFrame) {
  885. SetBorderBrushBackground (driver);
  886. SetHotNormalBackground (view, driver);
  887. var padding = view.Border.GetSumThickness ();
  888. Rect scrRect;
  889. if (view == Child) {
  890. scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width + 2, view.Frame.Height + 2));
  891. scrRect = new Rect (scrRect.X - 1, scrRect.Y - 1, scrRect.Width, scrRect.Height);
  892. driver.DrawWindowTitle (scrRect, Title, 0, 0, 0, 0);
  893. } else {
  894. scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width, view.Frame.Height));
  895. driver.DrawWindowTitle (scrRect, Parent.Border.Title,
  896. padding.Left, padding.Top, padding.Right, padding.Bottom);
  897. }
  898. }
  899. driver.SetAttribute (Child.GetNormalColor ());
  900. }
  901. private void SetHotNormalBackground (View view, ConsoleDriver driver)
  902. {
  903. if (view.HasFocus) {
  904. if (background != null) {
  905. driver.SetAttribute (new Attribute (Child.ColorScheme.HotNormal.Foreground, Background));
  906. } else {
  907. driver.SetAttribute (Child.ColorScheme.HotNormal);
  908. }
  909. }
  910. }
  911. /// <summary>
  912. /// Draws the <see cref="View.Text"/> to the screen.
  913. /// </summary>
  914. /// <param name="view">The view.</param>
  915. /// <param name="rect">The frame.</param>
  916. public void DrawTitle (View view, Rect rect)
  917. {
  918. var driver = Application.Driver;
  919. if (DrawMarginFrame) {
  920. SetBorderBrushBackground (driver);
  921. SetHotNormalBackground (view, driver);
  922. var padding = Parent.Border.GetSumThickness ();
  923. var scrRect = Parent.ViewToScreen (new Rect (0, 0, rect.Width, rect.Height));
  924. driver.DrawWindowTitle (scrRect, view.Text,
  925. padding.Left, padding.Top, padding.Right, padding.Bottom);
  926. }
  927. driver.SetAttribute (view.GetNormalColor ());
  928. }
  929. /// <summary>
  930. /// Invoke the <see cref="BorderChanged"/> event.
  931. /// </summary>
  932. public virtual void OnBorderChanged ()
  933. {
  934. BorderChanged?.Invoke (this);
  935. }
  936. }
  937. }