Border.cs 30 KB

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