Border.cs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. /// <summary>
  304. /// Specifies the <see cref="Gui.BorderStyle"/> for a view.
  305. /// </summary>
  306. [JsonInclude, JsonConverter (typeof (JsonStringEnumConverter))]
  307. public BorderStyle BorderStyle {
  308. get => borderStyle;
  309. set {
  310. if (value != BorderStyle.None && !drawMarginFrame) {
  311. // Ensures drawn the border lines.
  312. drawMarginFrame = true;
  313. }
  314. borderStyle = value;
  315. OnBorderChanged ();
  316. }
  317. }
  318. /// <summary>
  319. /// Gets or sets if a margin frame is drawn around the <see cref="Child"/> regardless the <see cref="BorderStyle"/>
  320. /// </summary>
  321. [JsonInclude]
  322. public bool DrawMarginFrame {
  323. get => drawMarginFrame;
  324. set {
  325. if (borderStyle != BorderStyle.None
  326. && (!value || !drawMarginFrame)) {
  327. // Ensures drawn the border lines.
  328. drawMarginFrame = true;
  329. } else {
  330. drawMarginFrame = value;
  331. }
  332. OnBorderChanged ();
  333. }
  334. }
  335. /// <summary>
  336. /// Gets or sets the relative <see cref="Thickness"/> of a <see cref="Border"/>.
  337. /// </summary>
  338. [JsonInclude]
  339. public Thickness BorderThickness {
  340. get => borderThickness;
  341. set {
  342. borderThickness = value;
  343. OnBorderChanged ();
  344. }
  345. }
  346. /// <summary>
  347. /// Gets or sets the <see cref="Color"/> that draws the outer border color.
  348. /// </summary>
  349. [JsonInclude, JsonConverter (typeof (Configuration.ColorJsonConverter))]
  350. public Color BorderBrush {
  351. get => borderBrush;
  352. set {
  353. borderBrush = value;
  354. OnBorderChanged ();
  355. }
  356. }
  357. /// <summary>
  358. /// Gets or sets the <see cref="Color"/> that fills the area between the bounds of a <see cref="Border"/>.
  359. /// </summary>
  360. [JsonInclude, JsonConverter (typeof (Configuration.ColorJsonConverter))]
  361. public Color Background {
  362. get => background;
  363. set {
  364. background = value;
  365. OnBorderChanged ();
  366. }
  367. }
  368. /// <summary>
  369. /// Gets or sets a <see cref="Thickness"/> value that describes the amount of space between a
  370. /// <see cref="Border"/> and its child element.
  371. /// </summary>
  372. [JsonInclude]
  373. public Thickness Padding {
  374. get => padding;
  375. set {
  376. padding = value;
  377. OnBorderChanged ();
  378. }
  379. }
  380. /// <summary>
  381. /// Gets the rendered width of this element.
  382. /// </summary>
  383. [JsonIgnore]
  384. public int ActualWidth {
  385. get {
  386. var driver = Application.Driver;
  387. if (Parent?.Border == null) {
  388. return Math.Min (Child?.Frame.Width + (2 * marginFrame) + Padding.Right
  389. + BorderThickness.Right + Padding.Left + BorderThickness.Left ?? 0, driver.Cols);
  390. }
  391. return Math.Min (Parent.Frame.Width, driver.Cols);
  392. }
  393. }
  394. /// <summary>
  395. /// Gets the rendered height of this element.
  396. /// </summary>
  397. [JsonIgnore]
  398. public int ActualHeight {
  399. get {
  400. var driver = Application.Driver;
  401. if (Parent?.Border == null) {
  402. return Math.Min (Child?.Frame.Height + (2 * marginFrame) + Padding.Bottom
  403. + BorderThickness.Bottom + Padding.Top + BorderThickness.Top ?? 0, driver.Rows);
  404. }
  405. return Math.Min (Parent.Frame.Height, driver.Rows);
  406. }
  407. }
  408. /// <summary>
  409. /// Gets or sets the single child element of a <see cref="View"/>.
  410. /// </summary>
  411. [JsonIgnore]
  412. public View Child { get; set; }
  413. /// <summary>
  414. /// Gets the parent <see cref="Child"/> parent if any.
  415. /// </summary>
  416. [JsonIgnore]
  417. public View Parent { get => Child?.SuperView; }
  418. /// <summary>
  419. /// Gets or private sets by the <see cref="ToplevelContainer"/>
  420. /// </summary>
  421. [JsonIgnore]
  422. public ToplevelContainer ChildContainer { get; private set; }
  423. /// <summary>
  424. /// Gets or sets the 3D effect around the <see cref="Border"/>.
  425. /// </summary>
  426. [JsonInclude]
  427. public bool Effect3D {
  428. get => effect3D;
  429. set {
  430. effect3D = value;
  431. OnBorderChanged ();
  432. }
  433. }
  434. /// <summary>
  435. /// Get or sets the offset start position for the <see cref="Effect3D"/>
  436. /// </summary>
  437. [JsonInclude]
  438. public Point Effect3DOffset {
  439. get => effect3DOffset;
  440. set {
  441. effect3DOffset = value;
  442. OnBorderChanged ();
  443. }
  444. }
  445. /// <summary>
  446. /// Gets or sets the color for the <see cref="Border"/>
  447. /// </summary>
  448. [JsonInclude, JsonConverter (typeof (Configuration.AttributeJsonConverter))]
  449. public Attribute? Effect3DBrush {
  450. get {
  451. if (effect3DBrush == null && effect3D) {
  452. return effect3DBrush = new Attribute (Color.Gray, Color.DarkGray);
  453. } else {
  454. return effect3DBrush;
  455. }
  456. }
  457. set {
  458. effect3DBrush = value;
  459. OnBorderChanged ();
  460. }
  461. }
  462. /// <summary>
  463. /// The title to be displayed for this view.
  464. /// </summary>
  465. [JsonIgnore]
  466. public ustring Title {
  467. get => title;
  468. set {
  469. title = value;
  470. OnBorderChanged ();
  471. }
  472. }
  473. /// <summary>
  474. /// Calculate the sum of the <see cref="Padding"/> and the <see cref="BorderThickness"/>
  475. /// </summary>
  476. /// <returns>The total of the <see cref="Border"/> <see cref="Thickness"/></returns>
  477. public Thickness GetSumThickness ()
  478. {
  479. return new Thickness () {
  480. Left = Padding.Left + BorderThickness.Left,
  481. Top = Padding.Top + BorderThickness.Top,
  482. Right = Padding.Right + BorderThickness.Right,
  483. Bottom = Padding.Bottom + BorderThickness.Bottom
  484. };
  485. }
  486. /// <summary>
  487. /// Drawn the <see cref="BorderThickness"/> more the <see cref="Padding"/>
  488. /// more the <see cref="Border.BorderStyle"/> and the <see cref="Effect3D"/>.
  489. /// </summary>
  490. /// <param name="view">The view to draw.</param>
  491. /// <param name="fill">If it will clear or not the content area.</param>
  492. public void DrawContent (View view = null, bool fill = true)
  493. {
  494. if (Child == null) {
  495. Child = view;
  496. }
  497. if (Parent?.Border != null) {
  498. DrawParentBorder (Parent.ViewToScreen (Parent.Bounds), fill);
  499. } else {
  500. DrawChildBorder (Child.ViewToScreen (Child.Bounds), fill);
  501. }
  502. }
  503. /// <summary>
  504. /// Same as <see cref="DrawContent"/> but drawing full frames for all borders.
  505. /// </summary>
  506. public void DrawFullContent ()
  507. {
  508. var borderThickness = BorderThickness;
  509. var padding = Padding;
  510. var marginFrame = DrawMarginFrame ? 1 : 0;
  511. var driver = Application.Driver;
  512. Rect scrRect;
  513. if (Parent?.Border != null) {
  514. scrRect = Parent.ViewToScreen (Parent.Bounds);
  515. } else {
  516. scrRect = Child.ViewToScreen (Child.Bounds);
  517. }
  518. Rect borderRect;
  519. if (Parent?.Border != null) {
  520. borderRect = scrRect;
  521. } else {
  522. borderRect = new Rect () {
  523. X = scrRect.X - marginFrame - padding.Left - borderThickness.Left,
  524. Y = scrRect.Y - marginFrame - padding.Top - borderThickness.Top,
  525. Width = ActualWidth,
  526. Height = ActualHeight
  527. };
  528. }
  529. var savedAttribute = driver.GetAttribute ();
  530. // Draw 3D effects
  531. if (Effect3D) {
  532. driver.SetAttribute ((Attribute)Effect3DBrush);
  533. var effectBorder = new Rect () {
  534. X = borderRect.X + Effect3DOffset.X,
  535. Y = borderRect.Y + Effect3DOffset.Y,
  536. Width = ActualWidth,
  537. Height = ActualHeight
  538. };
  539. //Child.Clear (effectBorder);
  540. for (int r = effectBorder.Y; r < Math.Min (effectBorder.Bottom, driver.Rows); r++) {
  541. for (int c = effectBorder.X; c < Math.Min (effectBorder.Right, driver.Cols); c++) {
  542. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  543. }
  544. }
  545. }
  546. // Draw border thickness
  547. driver.SetAttribute (new Attribute (BorderBrush));
  548. Child.Clear (borderRect);
  549. borderRect = new Rect () {
  550. X = borderRect.X + borderThickness.Left,
  551. Y = borderRect.Y + borderThickness.Top,
  552. Width = Math.Max (borderRect.Width - borderThickness.Right - borderThickness.Left, 0),
  553. Height = Math.Max (borderRect.Height - borderThickness.Bottom - borderThickness.Top, 0)
  554. };
  555. if (borderRect != scrRect) {
  556. // Draw padding
  557. driver.SetAttribute (new Attribute (Background));
  558. Child.Clear (borderRect);
  559. }
  560. driver.SetAttribute (savedAttribute);
  561. // Draw margin frame
  562. if (DrawMarginFrame) {
  563. if (Parent?.Border != null) {
  564. var sumPadding = GetSumThickness ();
  565. borderRect = new Rect () {
  566. X = scrRect.X + sumPadding.Left,
  567. Y = scrRect.Y + sumPadding.Top,
  568. Width = Math.Max (scrRect.Width - sumPadding.Right - sumPadding.Left, 0),
  569. Height = Math.Max (scrRect.Height - sumPadding.Bottom - sumPadding.Top, 0)
  570. };
  571. } else {
  572. borderRect = new Rect () {
  573. X = borderRect.X + padding.Left,
  574. Y = borderRect.Y + padding.Top,
  575. Width = Math.Max (borderRect.Width - padding.Right - padding.Left, 0),
  576. Height = Math.Max (borderRect.Height - padding.Bottom - padding.Top, 0)
  577. };
  578. }
  579. if (borderRect.Width > 0 && borderRect.Height > 0) {
  580. driver.DrawWindowFrame (borderRect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill: true, this);
  581. }
  582. }
  583. }
  584. private void DrawChildBorder (Rect frame, bool fill = true)
  585. {
  586. var drawMarginFrame = DrawMarginFrame ? 1 : 0;
  587. var sumThickness = GetSumThickness ();
  588. var padding = Padding;
  589. var effect3DOffset = Effect3DOffset;
  590. var driver = Application.Driver;
  591. var savedAttribute = driver.GetAttribute ();
  592. driver.SetAttribute (new Attribute (BorderBrush));
  593. // Draw the upper BorderThickness
  594. for (int r = frame.Y - drawMarginFrame - sumThickness.Top;
  595. r > 0 && r < frame.Y - drawMarginFrame - padding.Top; r++) {
  596. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  597. c > 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  598. AddRuneAt (driver, c, r, ' ');
  599. }
  600. }
  601. // Draw the left BorderThickness
  602. for (int r = frame.Y - drawMarginFrame - padding.Top;
  603. r > 0 && r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  604. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  605. c > 0 && c < frame.X - drawMarginFrame - padding.Left; c++) {
  606. AddRuneAt (driver, c, r, ' ');
  607. }
  608. }
  609. // Draw the right BorderThickness
  610. for (int r = frame.Y - drawMarginFrame - padding.Top;
  611. r > 0 && r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  612. for (int c = frame.Right + drawMarginFrame + padding.Right;
  613. c > 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  614. AddRuneAt (driver, c, r, ' ');
  615. }
  616. }
  617. // Draw the lower BorderThickness
  618. for (int r = frame.Bottom + drawMarginFrame + padding.Bottom;
  619. r > 0 && r > 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom, driver.Rows); r++) {
  620. for (int c = frame.X - drawMarginFrame - sumThickness.Left;
  621. c > 0 && c > 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right, driver.Cols); c++) {
  622. AddRuneAt (driver, c, r, ' ');
  623. }
  624. }
  625. driver.SetAttribute (new Attribute (Background));
  626. // Draw the upper Padding
  627. for (int r = frame.Y - drawMarginFrame - padding.Top;
  628. r > 0 && r < frame.Y - drawMarginFrame; r++) {
  629. for (int c = frame.X - drawMarginFrame - padding.Left;
  630. c > 0 && c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  631. AddRuneAt (driver, c, r, ' ');
  632. }
  633. }
  634. // Draw the left Padding
  635. for (int r = frame.Y - drawMarginFrame;
  636. r > 0 && r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  637. for (int c = frame.X - drawMarginFrame - padding.Left;
  638. c > 0 && c < frame.X - drawMarginFrame; c++) {
  639. AddRuneAt (driver, c, r, ' ');
  640. }
  641. }
  642. // Draw the right Padding
  643. for (int r = frame.Y - drawMarginFrame;
  644. r > 0 && r < Math.Min (frame.Bottom + drawMarginFrame, driver.Rows); r++) {
  645. for (int c = frame.Right + drawMarginFrame;
  646. c > 0 && c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  647. AddRuneAt (driver, c, r, ' ');
  648. }
  649. }
  650. // Draw the lower Padding
  651. for (int r = frame.Bottom + drawMarginFrame;
  652. r > 0 && r < Math.Min (frame.Bottom + drawMarginFrame + padding.Bottom, driver.Rows); r++) {
  653. for (int c = frame.X - drawMarginFrame - padding.Left;
  654. c > 0 && c < Math.Min (frame.Right + drawMarginFrame + padding.Right, driver.Cols); c++) {
  655. AddRuneAt (driver, c, r, ' ');
  656. }
  657. }
  658. driver.SetAttribute (savedAttribute);
  659. // Draw the MarginFrame
  660. if (DrawMarginFrame) {
  661. var rect = new Rect () {
  662. X = frame.X - drawMarginFrame,
  663. Y = frame.Y - drawMarginFrame,
  664. Width = frame.Width + (2 * drawMarginFrame),
  665. Height = frame.Height + (2 * drawMarginFrame)
  666. };
  667. if (rect.Width > 0 && rect.Height > 0) {
  668. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  669. DrawTitle (Child);
  670. }
  671. //var rect = Child.ViewToScreen (new Rect (-1, -1, Child.Frame.Width + 2, Child.Frame.Height + 2));
  672. //if (rect.Width > 0 && rect.Height > 0) {
  673. // var lc = new LineCanvas ();
  674. // lc.AddLine (rect.Location, rect.Width-1, Orientation.Horizontal, BorderStyle);
  675. // lc.AddLine (rect.Location, rect.Height-1, Orientation.Vertical, BorderStyle);
  676. // lc.AddLine (new Point (rect.X, rect.Y + rect.Height-1), rect.Width, Orientation.Horizontal, BorderStyle);
  677. // lc.AddLine (new Point (rect.X + rect.Width-1, rect.Y), rect.Height, Orientation.Vertical, BorderStyle);
  678. // //driver.SetAttribute (new Attribute(Color.Red, Color.BrightYellow));
  679. // foreach (var p in lc.GenerateImage (rect)) {
  680. // AddRuneAt (driver, p.Key.X, p.Key.Y, p.Value);
  681. // }
  682. // DrawTitle (Child);
  683. //}
  684. }
  685. if (Effect3D) {
  686. driver.SetAttribute ((Attribute)Effect3DBrush);
  687. // Draw the upper Effect3D
  688. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  689. r >= 0 && r < frame.Y - drawMarginFrame - sumThickness.Top; r++) {
  690. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  691. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  692. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  693. }
  694. }
  695. // Draw the left Effect3D
  696. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  697. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  698. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  699. c >= 0 && c < frame.X - drawMarginFrame - sumThickness.Left; c++) {
  700. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  701. }
  702. }
  703. // Draw the right Effect3D
  704. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  705. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  706. for (int c = frame.Right + drawMarginFrame + sumThickness.Right;
  707. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  708. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  709. }
  710. }
  711. // Draw the lower Effect3D
  712. for (int r = frame.Bottom + drawMarginFrame + sumThickness.Bottom;
  713. r >= 0 && r < Math.Min (frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  714. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  715. c >= 0 && c < Math.Min (frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X, driver.Cols); c++) {
  716. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  717. }
  718. }
  719. }
  720. driver.SetAttribute (savedAttribute);
  721. }
  722. private void DrawParentBorder (Rect frame, bool fill = true)
  723. {
  724. var sumThickness = GetSumThickness ();
  725. var borderThickness = BorderThickness;
  726. var effect3DOffset = Effect3DOffset;
  727. var driver = Application.Driver;
  728. var savedAttribute = driver.GetAttribute ();
  729. driver.SetAttribute (new Attribute (BorderBrush));
  730. // Draw the upper BorderThickness
  731. for (int r = frame.Y;
  732. r > 0 && r < Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r++) {
  733. for (int c = frame.X;
  734. c > 0 && c < Math.Min (frame.Right, driver.Cols); c++) {
  735. AddRuneAt (driver, c, r, ' ');
  736. }
  737. }
  738. // Draw the left BorderThickness
  739. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  740. r > 0 && r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  741. for (int c = frame.X;
  742. c > 0 && c < Math.Min (frame.X + borderThickness.Left, frame.Right); c++) {
  743. AddRuneAt (driver, c, r, ' ');
  744. }
  745. }
  746. // Draw the right BorderThickness
  747. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  748. r > 0 && r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  749. for (int c = Math.Max (frame.Right - borderThickness.Right, frame.X);
  750. c > 0 && c < Math.Min (frame.Right, driver.Cols); c++) {
  751. AddRuneAt (driver, c, r, ' ');
  752. }
  753. }
  754. // Draw the lower BorderThickness
  755. for (int r = Math.Max (frame.Bottom - borderThickness.Bottom, frame.Y);
  756. r > 0 && r < Math.Min (frame.Bottom, driver.Rows); r++) {
  757. for (int c = frame.X;
  758. c > 0 && c < Math.Min (frame.Right, driver.Cols); c++) {
  759. AddRuneAt (driver, c, r, ' ');
  760. }
  761. }
  762. driver.SetAttribute (new Attribute (Background));
  763. // Draw the upper Padding
  764. for (int r = frame.Y + borderThickness.Top;
  765. r > 0 && r < Math.Min (frame.Y + sumThickness.Top, frame.Bottom - borderThickness.Bottom); r++) {
  766. for (int c = frame.X + borderThickness.Left;
  767. c > 0 && c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  768. AddRuneAt (driver, c, r, ' ');
  769. }
  770. }
  771. // Draw the left Padding
  772. for (int r = frame.Y + sumThickness.Top;
  773. r > 0 && r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  774. for (int c = frame.X + borderThickness.Left;
  775. c > 0 && c < Math.Min (frame.X + sumThickness.Left, frame.Right - borderThickness.Right); c++) {
  776. AddRuneAt (driver, c, r, ' ');
  777. }
  778. }
  779. // Draw the right Padding
  780. for (int r = frame.Y + sumThickness.Top;
  781. r > 0 && r < Math.Min (frame.Bottom - sumThickness.Bottom, driver.Rows); r++) {
  782. for (int c = Math.Max (frame.Right - sumThickness.Right, frame.X + sumThickness.Left);
  783. c > 0 && c < Math.Max (frame.Right - borderThickness.Right, frame.X + sumThickness.Left); c++) {
  784. AddRuneAt (driver, c, r, ' ');
  785. }
  786. }
  787. // Draw the lower Padding
  788. for (int r = Math.Max (frame.Bottom - sumThickness.Bottom, frame.Y + borderThickness.Top);
  789. r > 0 && r < Math.Min (frame.Bottom - borderThickness.Bottom, driver.Rows); r++) {
  790. for (int c = frame.X + borderThickness.Left;
  791. c > 0 && c < Math.Min (frame.Right - borderThickness.Right, driver.Cols); c++) {
  792. AddRuneAt (driver, c, r, ' ');
  793. }
  794. }
  795. driver.SetAttribute (savedAttribute);
  796. // Draw the MarginFrame
  797. if (DrawMarginFrame) {
  798. var rect = new Rect () {
  799. X = frame.X + sumThickness.Left,
  800. Y = frame.Y + sumThickness.Top,
  801. Width = Math.Max (frame.Width - sumThickness.Right - sumThickness.Left, 0),
  802. Height = Math.Max (frame.Height - sumThickness.Bottom - sumThickness.Top, 0)
  803. };
  804. if (rect.Width > 0 && rect.Height > 0) {
  805. driver.DrawWindowFrame (rect, 1, 1, 1, 1, BorderStyle != BorderStyle.None, fill, this);
  806. DrawTitle (Parent);
  807. }
  808. }
  809. if (Effect3D) {
  810. driver.SetAttribute ((Attribute)Effect3DBrush);
  811. // Draw the upper Effect3D
  812. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  813. r > 0 && r < frame.Y; r++) {
  814. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  815. c > 0 && c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  816. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  817. }
  818. }
  819. // Draw the left Effect3D
  820. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  821. r > 0 && r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  822. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  823. c > 0 && c < frame.X; c++) {
  824. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  825. }
  826. }
  827. // Draw the right Effect3D
  828. for (int r = Math.Max (frame.Y + effect3DOffset.Y, 0);
  829. r > 0 && r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  830. for (int c = frame.Right;
  831. c > 0 && c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  832. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  833. }
  834. }
  835. // Draw the lower Effect3D
  836. for (int r = frame.Bottom;
  837. r > 0 && r < Math.Min (frame.Bottom + effect3DOffset.Y, driver.Rows); r++) {
  838. for (int c = Math.Max (frame.X + effect3DOffset.X, 0);
  839. c > 0 && c < Math.Min (frame.Right + effect3DOffset.X, driver.Cols); c++) {
  840. AddRuneAt (driver, c, r, (Rune)driver.Contents [r, c, 0]);
  841. }
  842. }
  843. }
  844. driver.SetAttribute (savedAttribute);
  845. }
  846. private void AddRuneAt (ConsoleDriver driver, int col, int row, Rune ch)
  847. {
  848. if (col < driver.Cols && row < driver.Rows && col > 0 && driver.Contents [row, col, 2] == 0
  849. && Rune.ColumnWidth ((char)driver.Contents [row, col - 1, 0]) > 1) {
  850. driver.Contents [row, col, 1] = driver.GetAttribute ();
  851. return;
  852. }
  853. driver.Move (col, row);
  854. driver.AddRune (ch);
  855. }
  856. /// <summary>
  857. /// Draws the view <see cref="Title"/> to the screen.
  858. /// </summary>
  859. /// <param name="view">The view.</param>
  860. public void DrawTitle (View view)
  861. {
  862. var driver = Application.Driver;
  863. if (DrawMarginFrame) {
  864. driver.SetAttribute (Child.GetNormalColor ());
  865. if (Child.HasFocus)
  866. driver.SetAttribute (Child.ColorScheme.HotNormal);
  867. var padding = view.Border.GetSumThickness ();
  868. Rect scrRect;
  869. if (view == Child) {
  870. scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width + 2, view.Frame.Height + 2));
  871. scrRect = new Rect (scrRect.X - 1, scrRect.Y - 1, scrRect.Width, scrRect.Height);
  872. driver.DrawWindowTitle (scrRect, Title, 0, 0, 0, 0);
  873. } else {
  874. scrRect = view.ViewToScreen (new Rect (0, 0, view.Frame.Width, view.Frame.Height));
  875. driver.DrawWindowTitle (scrRect, Title,
  876. padding.Left, padding.Top, padding.Right, padding.Bottom);
  877. }
  878. }
  879. driver.SetAttribute (Child.GetNormalColor ());
  880. }
  881. /// <summary>
  882. /// Draws the <see cref="View.Text"/> to the screen.
  883. /// </summary>
  884. /// <param name="view">The view.</param>
  885. /// <param name="rect">The frame.</param>
  886. public void DrawTitle (View view, Rect rect)
  887. {
  888. var driver = Application.Driver;
  889. if (DrawMarginFrame) {
  890. driver.SetAttribute (view.GetNormalColor ());
  891. if (view.HasFocus) {
  892. driver.SetAttribute (view.ColorScheme.HotNormal);
  893. }
  894. var padding = Parent.Border.GetSumThickness ();
  895. var scrRect = Parent.ViewToScreen (new Rect (0, 0, rect.Width, rect.Height));
  896. driver.DrawWindowTitle (scrRect, view.Text,
  897. padding.Left, padding.Top, padding.Right, padding.Bottom);
  898. }
  899. driver.SetAttribute (view.GetNormalColor ());
  900. }
  901. /// <summary>
  902. /// Invoke the <see cref="BorderChanged"/> event.
  903. /// </summary>
  904. public virtual void OnBorderChanged ()
  905. {
  906. BorderChanged?.Invoke (this);
  907. }
  908. }
  909. }