Border.cs 32 KB

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