Border.cs 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. public View Child {
  413. get => child;
  414. set {
  415. child = value;
  416. if (child != null && Parent != null) {
  417. Parent.Initialized += Parent_Initialized;
  418. Parent.Removed += Parent_Removed;
  419. }
  420. }
  421. }
  422. private void Parent_Removed (View obj)
  423. {
  424. BorderBrush = default;
  425. Background = default;
  426. child.Removed -= Parent_Removed;
  427. }
  428. private void Parent_Initialized (object s, EventArgs e)
  429. {
  430. SetMarginFrameTitleBrush ();
  431. child.Initialized -= Parent_Initialized;
  432. }
  433. private void SetMarginFrameTitleBrush ()
  434. {
  435. if (child != null) {
  436. var view = Parent?.Border != null ? Parent : child;
  437. if (view.ColorScheme != null) {
  438. if (borderBrush == default) {
  439. BorderBrush = view.GetNormalColor ().Foreground;
  440. }
  441. if (background == default) {
  442. Background = view.GetNormalColor ().Background;
  443. }
  444. return;
  445. }
  446. }
  447. BorderBrush = default;
  448. Background = default;
  449. }
  450. /// <summary>
  451. /// Gets the parent <see cref="Child"/> parent if any.
  452. /// </summary>
  453. [JsonIgnore]
  454. public View Parent { get => Child?.SuperView; }
  455. /// <summary>
  456. /// Gets or private sets by the <see cref="ToplevelContainer"/>
  457. /// </summary>
  458. [JsonIgnore]
  459. public ToplevelContainer ChildContainer { get; private set; }
  460. /// <summary>
  461. /// Gets or sets the 3D effect around the <see cref="Border"/>.
  462. /// </summary>
  463. [JsonInclude]
  464. public bool Effect3D {
  465. get => effect3D;
  466. set {
  467. effect3D = value;
  468. OnBorderChanged ();
  469. }
  470. }
  471. /// <summary>
  472. /// Get or sets the offset start position for the <see cref="Effect3D"/>
  473. /// </summary>
  474. [JsonInclude]
  475. public Point Effect3DOffset {
  476. get => effect3DOffset;
  477. set {
  478. effect3DOffset = value;
  479. OnBorderChanged ();
  480. }
  481. }
  482. /// <summary>
  483. /// Gets or sets the color for the <see cref="Border"/>
  484. /// </summary>
  485. [JsonInclude, JsonConverter (typeof (Configuration.AttributeJsonConverter))]
  486. public Attribute? Effect3DBrush {
  487. get {
  488. if (effect3DBrush == null && effect3D) {
  489. return effect3DBrush = new Attribute (Color.Gray, Color.DarkGray);
  490. } else {
  491. return effect3DBrush;
  492. }
  493. }
  494. set {
  495. effect3DBrush = value;
  496. OnBorderChanged ();
  497. }
  498. }
  499. /// <summary>
  500. /// The title to be displayed for this view.
  501. /// </summary>
  502. [JsonIgnore]
  503. public ustring Title {
  504. get => title;
  505. set {
  506. title = value;
  507. OnBorderChanged ();
  508. }
  509. }
  510. /// <summary>
  511. /// Calculate the sum of the <see cref="Padding"/> and the <see cref="BorderThickness"/>
  512. /// </summary>
  513. /// <returns>The total of the <see cref="Border"/> <see cref="Thickness"/></returns>
  514. public Thickness GetSumThickness ()
  515. {
  516. return new Thickness () {
  517. Left = Padding.Left + BorderThickness.Left,
  518. Top = Padding.Top + BorderThickness.Top,
  519. Right = Padding.Right + BorderThickness.Right,
  520. Bottom = Padding.Bottom + BorderThickness.Bottom
  521. };
  522. }
  523. /// <summary>
  524. /// Drawn the <see cref="BorderThickness"/> more the <see cref="Padding"/>
  525. /// more the <see cref="Border.BorderStyle"/> and the <see cref="Effect3D"/>.
  526. /// </summary>
  527. /// <param name="view">The view to draw.</param>
  528. /// <param name="fill">If it will clear or not the content area.</param>
  529. public void DrawContent (View view = null, bool fill = true)
  530. {
  531. if (Child == null) {
  532. Child = view;
  533. }
  534. if (Parent?.Border != null) {
  535. DrawParentBorder (Parent.ViewToScreen (Parent.Bounds), fill);
  536. } else {
  537. DrawChildBorder (Child.ViewToScreen (Child.Bounds), fill);
  538. }
  539. }
  540. /// <summary>
  541. /// Same as <see cref="DrawContent"/> but drawing full frames for all borders.
  542. /// </summary>
  543. public void DrawFullContent ()
  544. {
  545. var borderThickness = BorderThickness;
  546. var padding = Padding;
  547. var marginFrame = DrawMarginFrame ? 1 : 0;
  548. var driver = Application.Driver;
  549. Rect scrRect;
  550. if (Parent?.Border != null) {
  551. scrRect = Parent.ViewToScreen (Parent.Bounds);
  552. } else {
  553. scrRect = Child.ViewToScreen (Child.Bounds);
  554. }
  555. Rect borderRect;
  556. if (Parent?.Border != null) {
  557. borderRect = scrRect;
  558. } else {
  559. borderRect = new Rect () {
  560. X = scrRect.X - marginFrame - padding.Left - borderThickness.Left,
  561. Y = scrRect.Y - marginFrame - padding.Top - borderThickness.Top,
  562. Width = ActualWidth,
  563. Height = ActualHeight
  564. };
  565. }
  566. var savedAttribute = driver.GetAttribute ();
  567. // Draw 3D effects
  568. if (Effect3D) {
  569. driver.SetAttribute ((Attribute)Effect3DBrush);
  570. var effectBorder = new Rect () {
  571. X = borderRect.X + Effect3DOffset.X,
  572. Y = borderRect.Y + Effect3DOffset.Y,
  573. Width = ActualWidth,
  574. Height = ActualHeight
  575. };
  576. //Child.Clear (effectBorder);
  577. for (int r = effectBorder.Y; r < Math.Min (effectBorder.Bottom, driver.Rows); r++) {
  578. for (int c = effectBorder.X; c < Math.Min (effectBorder.Right, driver.Cols); c++) {
  579. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  580. }
  581. }
  582. }
  583. // Draw border thickness
  584. driver.SetAttribute (new Attribute (BorderBrush));
  585. Child.Clear (borderRect);
  586. borderRect = new Rect () {
  587. X = borderRect.X + borderThickness.Left,
  588. Y = borderRect.Y + borderThickness.Top,
  589. Width = Math.Max (borderRect.Width - borderThickness.Right - borderThickness.Left, 0),
  590. Height = Math.Max (borderRect.Height - borderThickness.Bottom - borderThickness.Top, 0)
  591. };
  592. if (borderRect != scrRect) {
  593. // Draw padding
  594. driver.SetAttribute (new Attribute (Background));
  595. Child.Clear (borderRect);
  596. }
  597. driver.SetAttribute (savedAttribute);
  598. // Draw margin frame
  599. if (DrawMarginFrame) {
  600. if (Parent?.Border != null) {
  601. var sumPadding = GetSumThickness ();
  602. borderRect = new Rect () {
  603. X = scrRect.X + sumPadding.Left,
  604. Y = scrRect.Y + sumPadding.Top,
  605. Width = Math.Max (scrRect.Width - sumPadding.Right - sumPadding.Left, 0),
  606. Height = Math.Max (scrRect.Height - sumPadding.Bottom - sumPadding.Top, 0)
  607. };
  608. } else {
  609. borderRect = new Rect () {
  610. X = borderRect.X + padding.Left,
  611. Y = borderRect.Y + padding.Top,
  612. Width = Math.Max (borderRect.Width - padding.Right - padding.Left, 0),
  613. Height = Math.Max (borderRect.Height - padding.Bottom - padding.Top, 0)
  614. };
  615. }
  616. if (borderRect.Width > 0 && borderRect.Height > 0) {
  617. driver.DrawWindowFrame (borderRect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill: true, this);
  618. }
  619. }
  620. }
  621. private void DrawChildBorder (Rect frame, bool fill = true)
  622. {
  623. var drawMarginFrame = DrawMarginFrame ? 1 : 0;
  624. var sumThickness = GetSumThickness ();
  625. var padding = Padding;
  626. var effect3DOffset = Effect3DOffset;
  627. var driver = Application.Driver;
  628. var savedAttribute = driver.GetAttribute ();
  629. driver.SetAttribute (new Attribute (BorderBrush));
  630. // Draw the upper BorderThickness
  631. for (int r = frame.Y - drawMarginFrame - sumThickness.Top;
  632. r < frame.Y - drawMarginFrame - padding.Top; r++) {
  633. if (r < 0) {
  634. continue;
  635. }
  636. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  637. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  638. AddRuneAt (driver, c, r, ' ');
  639. }
  640. }
  641. // Draw the left BorderThickness
  642. for (int r = frame.Y - drawMarginFrame - padding.Top;
  643. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  644. if (r < 0) {
  645. continue;
  646. }
  647. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  648. c < frame.X - drawMarginFrame - padding.Left; c++) {
  649. AddRuneAt (driver, c, r, ' ');
  650. }
  651. }
  652. // Draw the right BorderThickness
  653. for (int r = frame.Y - drawMarginFrame - padding.Top;
  654. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  655. if (r < 0) {
  656. continue;
  657. }
  658. for (int c = frame.Right + drawMarginFrame + padding.Right;
  659. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  660. AddRuneAt (driver, c, r, ' ');
  661. }
  662. }
  663. // Draw the lower BorderThickness
  664. for (int r = frame.Bottom + drawMarginFrame + padding.Bottom;
  665. r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom, driver.Rows); r++) {
  666. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  667. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  668. AddRuneAt (driver, c, r, ' ');
  669. }
  670. }
  671. driver.SetAttribute (new Attribute (Background));
  672. // Draw the upper Padding
  673. for (int r = frame.Y - drawMarginFrame - padding.Top;
  674. r < frame.Y - drawMarginFrame; r++) {
  675. if (r < 0) {
  676. continue;
  677. }
  678. for (int c = frame.X - drawMarginFrame - padding.Left;
  679. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  680. AddRuneAt (driver, c, r, ' ');
  681. }
  682. }
  683. // Draw the left Padding
  684. for (int r = frame.Y - drawMarginFrame;
  685. r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  686. for (int c = frame.X - drawMarginFrame - padding.Left;
  687. c < frame.X - drawMarginFrame; c++) {
  688. AddRuneAt (driver, c, r, ' ');
  689. }
  690. }
  691. // Draw the right Padding
  692. for (int r = frame.Y - drawMarginFrame;
  693. r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  694. for (int c = frame.Right + drawMarginFrame;
  695. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  696. AddRuneAt (driver, c, r, ' ');
  697. }
  698. }
  699. // Draw the lower Padding
  700. for (int r = frame.Bottom + drawMarginFrame;
  701. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  702. for (int c = frame.X - drawMarginFrame - padding.Left;
  703. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  704. AddRuneAt (driver, c, r, ' ');
  705. }
  706. }
  707. driver.SetAttribute (savedAttribute);
  708. // Draw the MarginFrame
  709. if (DrawMarginFrame) {
  710. var rect = new Rect () {
  711. X = frame.X - drawMarginFrame,
  712. Y = frame.Y - drawMarginFrame,
  713. Width = frame.Width + (2 * drawMarginFrame),
  714. Height = frame.Height + (2 * drawMarginFrame)
  715. };
  716. if (rect.Width > 0 && rect.Height > 0) {
  717. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  718. DrawTitle (Child);
  719. }
  720. //var rect = Child.ViewToScreen (new Rect (-1, -1, Child.Frame.Width + 2, Child.Frame.Height + 2));
  721. //if (rect.Width > 0 && rect.Height > 0) {
  722. // var lc = new LineCanvas ();
  723. // lc.AddLine (rect.Location, rect.Width-1, Orientation.Horizontal, BorderStyle);
  724. // lc.AddLine (rect.Location, rect.Height-1, Orientation.Vertical, BorderStyle);
  725. // lc.AddLine (new Point (rect.X, rect.Y + rect.Height-1), rect.Width, Orientation.Horizontal, BorderStyle);
  726. // lc.AddLine (new Point (rect.X + rect.Width-1, rect.Y), rect.Height, Orientation.Vertical, BorderStyle);
  727. // //driver.SetAttribute (new Attribute(Color.Red, Color.BrightYellow));
  728. // foreach (var p in lc.GenerateImage (rect)) {
  729. // AddRuneAt (driver, p.Key.X, p.Key.Y, p.Value);
  730. // }
  731. // DrawTitle (Child);
  732. //}
  733. }
  734. if (Effect3D) {
  735. driver.SetAttribute ((Attribute)Effect3DBrush);
  736. // Draw the upper Effect3D
  737. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  738. r >= 0 && r < frame.Y - drawMarginFrame - sumThickness.Top; r++) {
  739. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  740. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  741. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  742. }
  743. }
  744. // Draw the left Effect3D
  745. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  746. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  747. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  748. c >= 0 && c < frame.X - drawMarginFrame - sumThickness.Left; c++) {
  749. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  750. }
  751. }
  752. // Draw the right Effect3D
  753. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  754. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  755. for (int c = frame.Right + drawMarginFrame + sumThickness.Right;
  756. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  757. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  758. }
  759. }
  760. // Draw the lower Effect3D
  761. for (int r = frame.Bottom + drawMarginFrame + sumThickness.Bottom;
  762. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  763. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  764. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  765. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  766. }
  767. }
  768. }
  769. driver.SetAttribute (savedAttribute);
  770. }
  771. private void DrawParentBorder (Rect frame, bool fill = true)
  772. {
  773. var sumThickness = GetSumThickness ();
  774. var borderThickness = BorderThickness;
  775. var effect3DOffset = Effect3DOffset;
  776. var driver = Application.Driver;
  777. var savedAttribute = driver.GetAttribute ();
  778. driver.SetAttribute (new Attribute (BorderBrush));
  779. // Draw the upper BorderThickness
  780. for (int r = frame.Y;
  781. r < Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r++) {
  782. if (r < 0) {
  783. continue;
  784. }
  785. for (int c = frame.X;
  786. c < Math.Min (frame.Right, driver.Cols); c++) {
  787. AddRuneAt (driver, c, r, ' ');
  788. }
  789. }
  790. // Draw the left BorderThickness
  791. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  792. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  793. if (r < 0) {
  794. continue;
  795. }
  796. for (int c = frame.X;
  797. c < Math.Min (frame.X + borderThickness.Left, frame.Right); c++) {
  798. AddRuneAt (driver, c, r, ' ');
  799. }
  800. }
  801. // Draw the right BorderThickness
  802. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  803. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  804. if (r < 0) {
  805. continue;
  806. }
  807. for (int c = Math.Max (frame.Right - borderThickness.Right, frame.X);
  808. c < Math.Min (frame.Right, driver.Cols); c++) {
  809. AddRuneAt (driver, c, r, ' ');
  810. }
  811. }
  812. // Draw the lower BorderThickness
  813. for (int r = Math.Max (frame.Bottom - borderThickness.Bottom, frame.Y);
  814. r < Math.Min (frame.Bottom, driver.Rows); r++) {
  815. for (int c = frame.X;
  816. c < Math.Min (frame.Right, driver.Cols); c++) {
  817. AddRuneAt (driver, c, r, ' ');
  818. }
  819. }
  820. driver.SetAttribute (new Attribute (Background));
  821. // Draw the upper Padding
  822. for (int r = frame.Y + borderThickness.Top;
  823. r < Math.Min (frame.Y + sumThickness.Top, frame.Bottom - borderThickness.Bottom); r++) {
  824. if (r < 0) {
  825. continue;
  826. }
  827. for (int c = frame.X + borderThickness.Left;
  828. c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  829. AddRuneAt (driver, c, r, ' ');
  830. }
  831. }
  832. // Draw the left Padding
  833. for (int r = frame.Y + sumThickness.Top;
  834. r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  835. if (r < 0) {
  836. continue;
  837. }
  838. for (int c = frame.X + borderThickness.Left;
  839. c < Math.Min (frame.X + sumThickness.Left, frame.Right - borderThickness.Right); c++) {
  840. AddRuneAt (driver, c, r, ' ');
  841. }
  842. }
  843. // Draw the right Padding
  844. for (int r = frame.Y + sumThickness.Top;
  845. r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  846. if (r < 0) {
  847. continue;
  848. }
  849. for (int c = Math.Max (frame.Right - sumThickness.Right, frame.X + sumThickness.Left);
  850. c < Math.Max (frame.Right - borderThickness.Right, frame.X + sumThickness.Left); c++) {
  851. AddRuneAt (driver, c, r, ' ');
  852. }
  853. }
  854. // Draw the lower Padding
  855. for (int r = Math.Max (frame.Bottom - sumThickness.Bottom, frame.Y + borderThickness.Top);
  856. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  857. for (int c = frame.X + borderThickness.Left;
  858. c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  859. AddRuneAt (driver, c, r, ' ');
  860. }
  861. }
  862. driver.SetAttribute (savedAttribute);
  863. // Draw the MarginFrame
  864. if (DrawMarginFrame) {
  865. var rect = new Rect () {
  866. X = frame.X + sumThickness.Left,
  867. Y = frame.Y + sumThickness.Top,
  868. Width = Math.Max (frame.Width - sumThickness.Right - sumThickness.Left, 0),
  869. Height = Math.Max (frame.Height - sumThickness.Bottom - sumThickness.Top, 0)
  870. };
  871. if (rect.Width > 0 && rect.Height > 0) {
  872. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  873. DrawTitle (Parent);
  874. }
  875. }
  876. if (Effect3D) {
  877. driver.SetAttribute ((Attribute)Effect3DBrush);
  878. // Draw the upper Effect3D
  879. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  880. r < frame.Y; r++) {
  881. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  882. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  883. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  884. }
  885. }
  886. // Draw the left Effect3D
  887. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  888. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  889. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  890. c < frame.X; c++) {
  891. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  892. }
  893. }
  894. // Draw the right Effect3D
  895. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  896. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  897. for (int c = frame.Right;
  898. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  899. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  900. }
  901. }
  902. // Draw the lower Effect3D
  903. for (int r = frame.Bottom;
  904. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  905. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  906. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  907. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  908. }
  909. }
  910. }
  911. driver.SetAttribute (savedAttribute);
  912. }
  913. private void AddRuneAt (ConsoleDriver driver, int col, int row, Rune ch)
  914. {
  915. if (col < driver.Cols && row < driver.Rows && col > 0 && driver.Contents [row, col, 2] == 0
  916. && Rune.ColumnWidth ((char)driver.Contents [row, col - 1, 0]) > 1) {
  917. driver.Contents [row, col, 1] = driver.GetAttribute ();
  918. return;
  919. }
  920. driver.Move (col, row);
  921. driver.AddRune (ch);
  922. }
  923. /// <summary>
  924. /// Draws the view <see cref="Title"/> to the screen.
  925. /// </summary>
  926. /// <param name="view">The view.</param>
  927. public void DrawTitle (View view)
  928. {
  929. var driver = Application.Driver;
  930. if (DrawMarginFrame) {
  931. driver.SetAttribute (new Attribute (BorderBrush, Background));
  932. if (view.HasFocus) {
  933. driver.SetAttribute (new Attribute (Child.ColorScheme.HotNormal.Foreground, Background));
  934. }
  935. var padding = view.Border.GetSumThickness ();
  936. Rect scrRect;
  937. if (view == Child) {
  938. scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width + 2, view.Frame.Height + 2));
  939. scrRect = new Rect (scrRect.X - 1, scrRect.Y - 1, scrRect.Width, scrRect.Height);
  940. driver.DrawWindowTitle (scrRect, Title, 0, 0, 0, 0);
  941. } else {
  942. scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width, view.Frame.Height));
  943. driver.DrawWindowTitle (scrRect, Title,
  944. padding.Left, padding.Top, padding.Right, padding.Bottom);
  945. }
  946. }
  947. driver.SetAttribute (Child.GetNormalColor ());
  948. }
  949. /// <summary>
  950. /// Draws the <see cref="View.Text"/> to the screen.
  951. /// </summary>
  952. /// <param name="view">The view.</param>
  953. /// <param name="rect">The frame.</param>
  954. public void DrawTitle (View view, Rect rect)
  955. {
  956. var driver = Application.Driver;
  957. if (DrawMarginFrame) {
  958. driver.SetAttribute (view.GetNormalColor ());
  959. if (view.HasFocus) {
  960. driver.SetAttribute (view.ColorScheme.HotNormal);
  961. }
  962. var padding = Parent.Border.GetSumThickness ();
  963. var scrRect = Parent.ViewToScreen (new Rect (0, 0, rect.Width, rect.Height));
  964. driver.DrawWindowTitle (scrRect, view.Text,
  965. padding.Left, padding.Top, padding.Right, padding.Bottom);
  966. }
  967. driver.SetAttribute (view.GetNormalColor ());
  968. }
  969. /// <summary>
  970. /// Invoke the <see cref="BorderChanged"/> event.
  971. /// </summary>
  972. public virtual void OnBorderChanged ()
  973. {
  974. BorderChanged?.Invoke (this);
  975. }
  976. }
  977. }