Border.cs 33 KB

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