Border.cs 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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 (new Attribute (BorderBrush, Background));
  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. driver.SetAttribute (savedAttribute);
  621. }
  622. private void DrawChildBorder (Rect frame, bool fill = true)
  623. {
  624. var drawMarginFrame = DrawMarginFrame ? 1 : 0;
  625. var sumThickness = GetSumThickness ();
  626. var padding = Padding;
  627. var effect3DOffset = Effect3DOffset;
  628. var driver = Application.Driver;
  629. var savedAttribute = driver.GetAttribute ();
  630. driver.SetAttribute (new Attribute (BorderBrush));
  631. // Draw the upper BorderThickness
  632. for (int r = frame.Y - drawMarginFrame - sumThickness.Top;
  633. r < frame.Y - drawMarginFrame - padding.Top; r++) {
  634. if (r < 0) {
  635. continue;
  636. }
  637. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  638. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  639. AddRuneAt (driver, c, r, ' ');
  640. }
  641. }
  642. // Draw the left BorderThickness
  643. for (int r = frame.Y - drawMarginFrame - padding.Top;
  644. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  645. if (r < 0) {
  646. continue;
  647. }
  648. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  649. c < frame.X - drawMarginFrame - padding.Left; c++) {
  650. AddRuneAt (driver, c, r, ' ');
  651. }
  652. }
  653. // Draw the right BorderThickness
  654. for (int r = frame.Y - drawMarginFrame - padding.Top;
  655. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  656. if (r < 0) {
  657. continue;
  658. }
  659. for (int c = frame.Right + drawMarginFrame + padding.Right;
  660. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  661. AddRuneAt (driver, c, r, ' ');
  662. }
  663. }
  664. // Draw the lower BorderThickness
  665. for (int r = frame.Bottom + drawMarginFrame + padding.Bottom;
  666. r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom, driver.Rows); r++) {
  667. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  668. c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  669. AddRuneAt (driver, c, r, ' ');
  670. }
  671. }
  672. driver.SetAttribute (new Attribute (Background));
  673. // Draw the upper Padding
  674. for (int r = frame.Y - drawMarginFrame - padding.Top;
  675. r < frame.Y - drawMarginFrame; r++) {
  676. if (r < 0) {
  677. continue;
  678. }
  679. for (int c = frame.X - drawMarginFrame - padding.Left;
  680. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  681. AddRuneAt (driver, c, r, ' ');
  682. }
  683. }
  684. // Draw the left Padding
  685. for (int r = frame.Y - drawMarginFrame;
  686. r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  687. for (int c = frame.X - drawMarginFrame - padding.Left;
  688. c < frame.X - drawMarginFrame; c++) {
  689. AddRuneAt (driver, c, r, ' ');
  690. }
  691. }
  692. // Draw the right Padding
  693. for (int r = frame.Y - drawMarginFrame;
  694. r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  695. for (int c = frame.Right + drawMarginFrame;
  696. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  697. AddRuneAt (driver, c, r, ' ');
  698. }
  699. }
  700. // Draw the lower Padding
  701. for (int r = frame.Bottom + drawMarginFrame;
  702. r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  703. for (int c = frame.X - drawMarginFrame - padding.Left;
  704. c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  705. AddRuneAt (driver, c, r, ' ');
  706. }
  707. }
  708. driver.SetAttribute (new Attribute (BorderBrush, Background));
  709. // Draw the MarginFrame
  710. if (DrawMarginFrame) {
  711. var rect = new Rect () {
  712. X = frame.X - drawMarginFrame,
  713. Y = frame.Y - drawMarginFrame,
  714. Width = frame.Width + (2 * drawMarginFrame),
  715. Height = frame.Height + (2 * drawMarginFrame)
  716. };
  717. if (rect.Width > 0 && rect.Height > 0) {
  718. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  719. DrawTitle (Child);
  720. }
  721. //var rect = Child.ViewToScreen (new Rect (-1, -1, Child.Frame.Width + 2, Child.Frame.Height + 2));
  722. //if (rect.Width > 0 && rect.Height > 0) {
  723. // var lc = new LineCanvas ();
  724. // lc.AddLine (rect.Location, rect.Width-1, Orientation.Horizontal, BorderStyle);
  725. // lc.AddLine (rect.Location, rect.Height-1, Orientation.Vertical, BorderStyle);
  726. // lc.AddLine (new Point (rect.X, rect.Y + rect.Height-1), rect.Width, Orientation.Horizontal, BorderStyle);
  727. // lc.AddLine (new Point (rect.X + rect.Width-1, rect.Y), rect.Height, Orientation.Vertical, BorderStyle);
  728. // //driver.SetAttribute (new Attribute(Color.Red, Color.BrightYellow));
  729. // foreach (var p in lc.GenerateImage (rect)) {
  730. // AddRuneAt (driver, p.Key.X, p.Key.Y, p.Value);
  731. // }
  732. // DrawTitle (Child);
  733. //}
  734. }
  735. if (Effect3D) {
  736. driver.SetAttribute ((Attribute)Effect3DBrush);
  737. // Draw the upper Effect3D
  738. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  739. r >= 0 && r < frame.Y - drawMarginFrame - sumThickness.Top; r++) {
  740. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  741. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  742. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  743. }
  744. }
  745. // Draw the left Effect3D
  746. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  747. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  748. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  749. c >= 0 && c < frame.X - drawMarginFrame - sumThickness.Left; c++) {
  750. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  751. }
  752. }
  753. // Draw the right Effect3D
  754. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  755. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  756. for (int c = frame.Right + drawMarginFrame + sumThickness.Right;
  757. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  758. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  759. }
  760. }
  761. // Draw the lower Effect3D
  762. for (int r = frame.Bottom + drawMarginFrame + sumThickness.Bottom;
  763. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  764. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  765. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  766. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  767. }
  768. }
  769. }
  770. driver.SetAttribute (savedAttribute);
  771. }
  772. private void DrawParentBorder (Rect frame, bool fill = true)
  773. {
  774. var sumThickness = GetSumThickness ();
  775. var borderThickness = BorderThickness;
  776. var effect3DOffset = Effect3DOffset;
  777. var driver = Application.Driver;
  778. var savedAttribute = driver.GetAttribute ();
  779. driver.SetAttribute (new Attribute (BorderBrush));
  780. // Draw the upper BorderThickness
  781. for (int r = frame.Y;
  782. r < Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r++) {
  783. if (r < 0) {
  784. continue;
  785. }
  786. for (int c = frame.X;
  787. c < Math.Min (frame.Right, driver.Cols); c++) {
  788. AddRuneAt (driver, c, r, ' ');
  789. }
  790. }
  791. // Draw the left BorderThickness
  792. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  793. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  794. if (r < 0) {
  795. continue;
  796. }
  797. for (int c = frame.X;
  798. c < Math.Min (frame.X + borderThickness.Left, frame.Right); c++) {
  799. AddRuneAt (driver, c, r, ' ');
  800. }
  801. }
  802. // Draw the right BorderThickness
  803. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  804. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  805. if (r < 0) {
  806. continue;
  807. }
  808. for (int c = Math.Max (frame.Right - borderThickness.Right, frame.X);
  809. c < Math.Min (frame.Right, driver.Cols); c++) {
  810. AddRuneAt (driver, c, r, ' ');
  811. }
  812. }
  813. // Draw the lower BorderThickness
  814. for (int r = Math.Max (frame.Bottom - borderThickness.Bottom, frame.Y);
  815. r < Math.Min (frame.Bottom, driver.Rows); r++) {
  816. for (int c = frame.X;
  817. c < Math.Min (frame.Right, driver.Cols); c++) {
  818. AddRuneAt (driver, c, r, ' ');
  819. }
  820. }
  821. driver.SetAttribute (new Attribute (Background));
  822. // Draw the upper Padding
  823. for (int r = frame.Y + borderThickness.Top;
  824. r < Math.Min (frame.Y + sumThickness.Top, frame.Bottom - borderThickness.Bottom); r++) {
  825. if (r < 0) {
  826. continue;
  827. }
  828. for (int c = frame.X + borderThickness.Left;
  829. c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  830. AddRuneAt (driver, c, r, ' ');
  831. }
  832. }
  833. // Draw the left Padding
  834. for (int r = frame.Y + sumThickness.Top;
  835. r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  836. if (r < 0) {
  837. continue;
  838. }
  839. for (int c = frame.X + borderThickness.Left;
  840. c < Math.Min (frame.X + sumThickness.Left, frame.Right - borderThickness.Right); c++) {
  841. AddRuneAt (driver, c, r, ' ');
  842. }
  843. }
  844. // Draw the right Padding
  845. for (int r = frame.Y + sumThickness.Top;
  846. r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  847. if (r < 0) {
  848. continue;
  849. }
  850. for (int c = Math.Max (frame.Right - sumThickness.Right, frame.X + sumThickness.Left);
  851. c < Math.Max (frame.Right - borderThickness.Right, frame.X + sumThickness.Left); c++) {
  852. AddRuneAt (driver, c, r, ' ');
  853. }
  854. }
  855. // Draw the lower Padding
  856. for (int r = Math.Max (frame.Bottom - sumThickness.Bottom, frame.Y + borderThickness.Top);
  857. r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  858. for (int c = frame.X + borderThickness.Left;
  859. c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  860. AddRuneAt (driver, c, r, ' ');
  861. }
  862. }
  863. driver.SetAttribute (new Attribute (BorderBrush, Background));
  864. // Draw the MarginFrame
  865. if (DrawMarginFrame) {
  866. var rect = new Rect () {
  867. X = frame.X + sumThickness.Left,
  868. Y = frame.Y + sumThickness.Top,
  869. Width = Math.Max (frame.Width - sumThickness.Right - sumThickness.Left, 0),
  870. Height = Math.Max (frame.Height - sumThickness.Bottom - sumThickness.Top, 0)
  871. };
  872. if (rect.Width > 0 && rect.Height > 0) {
  873. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  874. DrawTitle (Parent);
  875. }
  876. }
  877. if (Effect3D) {
  878. driver.SetAttribute ((Attribute)Effect3DBrush);
  879. // Draw the upper Effect3D
  880. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  881. r < frame.Y; r++) {
  882. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  883. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  884. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  885. }
  886. }
  887. // Draw the left Effect3D
  888. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  889. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  890. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  891. c < frame.X; c++) {
  892. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  893. }
  894. }
  895. // Draw the right Effect3D
  896. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  897. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  898. for (int c = frame.Right;
  899. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  900. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  901. }
  902. }
  903. // Draw the lower Effect3D
  904. for (int r = frame.Bottom;
  905. r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  906. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  907. c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  908. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  909. }
  910. }
  911. }
  912. driver.SetAttribute (savedAttribute);
  913. }
  914. private void AddRuneAt (ConsoleDriver driver, int col, int row, Rune ch)
  915. {
  916. if (col < driver.Cols && row < driver.Rows && col > 0 && driver.Contents [row, col, 2] == 0
  917. && Rune.ColumnWidth ((char)driver.Contents [row, col - 1, 0]) > 1) {
  918. driver.Contents [row, col, 1] = driver.GetAttribute ();
  919. return;
  920. }
  921. driver.Move (col, row);
  922. driver.AddRune (ch);
  923. }
  924. /// <summary>
  925. /// Draws the view <see cref="Title"/> to the screen.
  926. /// </summary>
  927. /// <param name="view">The view.</param>
  928. public void DrawTitle (View view)
  929. {
  930. var driver = Application.Driver;
  931. if (DrawMarginFrame) {
  932. driver.SetAttribute (new Attribute (BorderBrush, Background));
  933. if (view.HasFocus) {
  934. driver.SetAttribute (new Attribute (Child.ColorScheme.HotNormal.Foreground, Background));
  935. }
  936. var padding = view.Border.GetSumThickness ();
  937. Rect scrRect;
  938. if (view == Child) {
  939. scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width + 2, view.Frame.Height + 2));
  940. scrRect = new Rect (scrRect.X - 1, scrRect.Y - 1, scrRect.Width, scrRect.Height);
  941. driver.DrawWindowTitle (scrRect, Title, 0, 0, 0, 0);
  942. } else {
  943. scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width, view.Frame.Height));
  944. driver.DrawWindowTitle (scrRect, Parent.Border.Title,
  945. padding.Left, padding.Top, padding.Right, padding.Bottom);
  946. }
  947. }
  948. driver.SetAttribute (Child.GetNormalColor ());
  949. }
  950. /// <summary>
  951. /// Draws the <see cref="View.Text"/> to the screen.
  952. /// </summary>
  953. /// <param name="view">The view.</param>
  954. /// <param name="rect">The frame.</param>
  955. public void DrawTitle (View view, Rect rect)
  956. {
  957. var driver = Application.Driver;
  958. if (DrawMarginFrame) {
  959. driver.SetAttribute (new Attribute (BorderBrush, Background));
  960. if (view.HasFocus) {
  961. driver.SetAttribute (new Attribute (view.ColorScheme.HotNormal.Foreground, Background));
  962. }
  963. var padding = Parent.Border.GetSumThickness ();
  964. var scrRect = Parent.ViewToScreen (new Rect (0, 0, rect.Width, rect.Height));
  965. driver.DrawWindowTitle (scrRect, view.Text,
  966. padding.Left, padding.Top, padding.Right, padding.Bottom);
  967. }
  968. driver.SetAttribute (view.GetNormalColor ());
  969. }
  970. /// <summary>
  971. /// Invoke the <see cref="BorderChanged"/> event.
  972. /// </summary>
  973. public virtual void OnBorderChanged ()
  974. {
  975. BorderChanged?.Invoke (this);
  976. }
  977. }
  978. }