ConsoleDriver.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. //
  2. // Driver.cs: Definition for the Console Driver API
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Runtime.CompilerServices;
  10. using System.Runtime.InteropServices;
  11. using System.Security.Cryptography;
  12. using Mono.Terminal;
  13. using NStack;
  14. using Unix.Terminal;
  15. namespace Terminal.Gui {
  16. /// <summary>
  17. /// Basic colors that can be used to set the foreground and background colors in console applications. These can only be
  18. /// </summary>
  19. public enum Color {
  20. /// <summary>
  21. /// The black color.
  22. /// </summary>
  23. Black,
  24. /// <summary>
  25. /// The blue color.
  26. /// </summary>
  27. Blue,
  28. /// <summary>
  29. /// The green color.
  30. /// </summary>
  31. Green,
  32. /// <summary>
  33. /// The cyan color.
  34. /// </summary>
  35. Cyan,
  36. /// <summary>
  37. /// The red color.
  38. /// </summary>
  39. Red,
  40. /// <summary>
  41. /// The magenta color.
  42. /// </summary>
  43. Magenta,
  44. /// <summary>
  45. /// The brown color.
  46. /// </summary>
  47. Brown,
  48. /// <summary>
  49. /// The gray color.
  50. /// </summary>
  51. Gray,
  52. /// <summary>
  53. /// The dark gray color.
  54. /// </summary>
  55. DarkGray,
  56. /// <summary>
  57. /// The bright bBlue color.
  58. /// </summary>
  59. BrightBlue,
  60. /// <summary>
  61. /// The bright green color.
  62. /// </summary>
  63. BrightGreen,
  64. /// <summary>
  65. /// The brigh cyan color.
  66. /// </summary>
  67. BrighCyan,
  68. /// <summary>
  69. /// The bright red color.
  70. /// </summary>
  71. BrightRed,
  72. /// <summary>
  73. /// The bright magenta color.
  74. /// </summary>
  75. BrightMagenta,
  76. /// <summary>
  77. /// The bright yellow color.
  78. /// </summary>
  79. BrightYellow,
  80. /// <summary>
  81. /// The White color.
  82. /// </summary>
  83. White
  84. }
  85. /// <summary>
  86. /// Attributes are used as elements that contain both a foreground and a background or platform specific features
  87. /// </summary>
  88. /// <remarks>
  89. /// Attributes are needed to map colors to terminal capabilities that might lack colors, on color
  90. /// scenarios, they encode both the foreground and the background color and are used in the ColorScheme
  91. /// class to define color schemes that can be used in your application.
  92. /// </remarks>
  93. public struct Attribute {
  94. internal int value;
  95. internal Color foreground;
  96. internal Color background;
  97. /// <summary>
  98. /// Initializes a new instance of the <see cref="Attribute"/> struct.
  99. /// </summary>
  100. /// <param name="value">Value.</param>
  101. /// <param name="foreground">Foreground</param>
  102. /// <param name="background">Background</param>
  103. public Attribute (int value, Color foreground = new Color(), Color background = new Color())
  104. {
  105. this.value = value;
  106. this.foreground = foreground;
  107. this.background = background;
  108. }
  109. /// <summary>
  110. /// Initializes a new instance of the <see cref="Attribute"/> struct.
  111. /// </summary>
  112. /// <param name="foreground">Foreground</param>
  113. /// <param name="background">Background</param>
  114. public Attribute (Color foreground = new Color (), Color background = new Color ())
  115. {
  116. this.value = value = ((int)foreground | (int)background << 4);
  117. this.foreground = foreground;
  118. this.background = background;
  119. }
  120. /// <summary>
  121. /// Implicit conversion from an attribute to the underlying Int32 representation
  122. /// </summary>
  123. /// <returns>The integer value stored in the attribute.</returns>
  124. /// <param name="c">The attribute to convert</param>
  125. public static implicit operator int (Attribute c) => c.value;
  126. /// <summary>
  127. /// Implicitly convert an integer value into an attribute
  128. /// </summary>
  129. /// <returns>An attribute with the specified integer value.</returns>
  130. /// <param name="v">value</param>
  131. public static implicit operator Attribute (int v) => new Attribute (v);
  132. /// <summary>
  133. /// Creates an attribute from the specified foreground and background.
  134. /// </summary>
  135. /// <returns>The make.</returns>
  136. /// <param name="foreground">Foreground color to use.</param>
  137. /// <param name="background">Background color to use.</param>
  138. public static Attribute Make (Color foreground, Color background)
  139. {
  140. if (Application.Driver == null)
  141. throw new InvalidOperationException ("The Application has not been initialized");
  142. return Application.Driver.MakeAttribute (foreground, background);
  143. }
  144. }
  145. /// <summary>
  146. /// Color scheme definitions, they cover some common scenarios and are used
  147. /// typically in toplevel containers to set the scheme that is used by all the
  148. /// views contained inside.
  149. /// </summary>
  150. public class ColorScheme {
  151. Attribute _normal;
  152. Attribute _focus;
  153. Attribute _hotNormal;
  154. Attribute _hotFocus;
  155. Attribute _disabled;
  156. internal string caller = "";
  157. /// <summary>
  158. /// The default color for text, when the view is not focused.
  159. /// </summary>
  160. public Attribute Normal { get { return _normal; } set { _normal = SetAttribute (value); } }
  161. /// <summary>
  162. /// The color for text when the view has the focus.
  163. /// </summary>
  164. public Attribute Focus { get { return _focus; } set { _focus = SetAttribute (value); } }
  165. /// <summary>
  166. /// The color for the hotkey when a view is not focused
  167. /// </summary>
  168. public Attribute HotNormal { get { return _hotNormal; } set { _hotNormal = SetAttribute (value); } }
  169. /// <summary>
  170. /// The color for the hotkey when the view is focused.
  171. /// </summary>
  172. public Attribute HotFocus { get { return _hotFocus; } set { _hotFocus = SetAttribute (value); } }
  173. /// <summary>
  174. /// The default color for text, when the view is disabled.
  175. /// </summary>
  176. public Attribute Disabled { get { return _disabled; } set { _disabled = SetAttribute (value); } }
  177. bool preparingScheme = false;
  178. Attribute SetAttribute (Attribute attribute, [CallerMemberName]string callerMemberName = null)
  179. {
  180. if (!Application._initialized && !preparingScheme)
  181. return attribute;
  182. if (preparingScheme)
  183. return attribute;
  184. preparingScheme = true;
  185. switch (caller) {
  186. case "TopLevel":
  187. switch (callerMemberName) {
  188. case "Normal":
  189. HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
  190. break;
  191. case "Focus":
  192. HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
  193. break;
  194. case "HotNormal":
  195. HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
  196. break;
  197. case "HotFocus":
  198. HotNormal = Application.Driver.MakeAttribute (attribute.foreground, HotNormal.background);
  199. if (Focus.foreground != attribute.background)
  200. Focus = Application.Driver.MakeAttribute (Focus.foreground, attribute.background);
  201. break;
  202. }
  203. break;
  204. case "Base":
  205. switch (callerMemberName) {
  206. case "Normal":
  207. HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
  208. break;
  209. case "Focus":
  210. HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
  211. break;
  212. case "HotNormal":
  213. HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
  214. Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
  215. break;
  216. case "HotFocus":
  217. HotNormal = Application.Driver.MakeAttribute (attribute.foreground, HotNormal.background);
  218. if (Focus.foreground != attribute.background)
  219. Focus = Application.Driver.MakeAttribute (Focus.foreground, attribute.background);
  220. break;
  221. }
  222. break;
  223. case "Menu":
  224. switch (callerMemberName) {
  225. case "Normal":
  226. if (Focus.background != attribute.background)
  227. Focus = Application.Driver.MakeAttribute (attribute.foreground, Focus.background);
  228. HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
  229. Disabled = Application.Driver.MakeAttribute (Disabled.foreground, attribute.background);
  230. break;
  231. case "Focus":
  232. Normal = Application.Driver.MakeAttribute (attribute.foreground, Normal.background);
  233. HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
  234. break;
  235. case "HotNormal":
  236. if (Focus.background != attribute.background)
  237. HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
  238. Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
  239. Disabled = Application.Driver.MakeAttribute (Disabled.foreground, attribute.background);
  240. break;
  241. case "HotFocus":
  242. HotNormal = Application.Driver.MakeAttribute (attribute.foreground, HotNormal.background);
  243. if (Focus.foreground != attribute.background)
  244. Focus = Application.Driver.MakeAttribute (Focus.foreground, attribute.background);
  245. break;
  246. case "Disabled":
  247. if (Focus.background != attribute.background)
  248. HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
  249. Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
  250. HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
  251. break;
  252. }
  253. break;
  254. case "Dialog":
  255. switch (callerMemberName) {
  256. case "Normal":
  257. if (Focus.background != attribute.background)
  258. Focus = Application.Driver.MakeAttribute (attribute.foreground, Focus.background);
  259. HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
  260. break;
  261. case "Focus":
  262. Normal = Application.Driver.MakeAttribute (attribute.foreground, Normal.background);
  263. HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
  264. break;
  265. case "HotNormal":
  266. if (Focus.background != attribute.background)
  267. HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
  268. if (Normal.foreground != attribute.background)
  269. Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
  270. break;
  271. case "HotFocus":
  272. HotNormal = Application.Driver.MakeAttribute (attribute.foreground, HotNormal.background);
  273. if (Focus.foreground != attribute.background)
  274. Focus = Application.Driver.MakeAttribute (Focus.foreground, attribute.background);
  275. break;
  276. }
  277. break;
  278. case "Error":
  279. switch (callerMemberName) {
  280. case "Normal":
  281. HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
  282. HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
  283. break;
  284. case "HotNormal":
  285. case "HotFocus":
  286. HotFocus = Application.Driver.MakeAttribute (attribute.foreground, attribute.background);
  287. Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
  288. break;
  289. }
  290. break;
  291. }
  292. preparingScheme = false;
  293. return attribute;
  294. }
  295. }
  296. /// <summary>
  297. /// The default ColorSchemes for the application.
  298. /// </summary>
  299. public static class Colors {
  300. static ColorScheme _toplevel;
  301. static ColorScheme _base;
  302. static ColorScheme _dialog;
  303. static ColorScheme _menu;
  304. static ColorScheme _error;
  305. /// <summary>
  306. /// The application toplevel color scheme, for the default toplevel views.
  307. /// </summary>
  308. public static ColorScheme TopLevel { get { return _toplevel; } set { _toplevel = SetColorScheme (value); } }
  309. /// <summary>
  310. /// The base color scheme, for the default toplevel views.
  311. /// </summary>
  312. public static ColorScheme Base { get { return _base; } set { _base = SetColorScheme (value); } }
  313. /// <summary>
  314. /// The dialog color scheme, for standard popup dialog boxes
  315. /// </summary>
  316. public static ColorScheme Dialog { get { return _dialog; } set { _dialog = SetColorScheme (value); } }
  317. /// <summary>
  318. /// The menu bar color
  319. /// </summary>
  320. public static ColorScheme Menu { get { return _menu; } set { _menu = SetColorScheme (value); } }
  321. /// <summary>
  322. /// The color scheme for showing errors.
  323. /// </summary>
  324. public static ColorScheme Error { get { return _error; } set { _error = SetColorScheme (value); } }
  325. static ColorScheme SetColorScheme (ColorScheme colorScheme, [CallerMemberName]string callerMemberName = null)
  326. {
  327. colorScheme.caller = callerMemberName;
  328. return colorScheme;
  329. }
  330. }
  331. /// <summary>
  332. /// Special characters that can be drawn with Driver.AddSpecial.
  333. /// </summary>
  334. public enum SpecialChar {
  335. /// <summary>
  336. /// Horizontal line character.
  337. /// </summary>
  338. HLine,
  339. /// <summary>
  340. /// Vertical line character.
  341. /// </summary>
  342. VLine,
  343. /// <summary>
  344. /// Stipple pattern
  345. /// </summary>
  346. Stipple,
  347. /// <summary>
  348. /// Diamond character
  349. /// </summary>
  350. Diamond,
  351. /// <summary>
  352. /// Upper left corner
  353. /// </summary>
  354. ULCorner,
  355. /// <summary>
  356. /// Lower left corner
  357. /// </summary>
  358. LLCorner,
  359. /// <summary>
  360. /// Upper right corner
  361. /// </summary>
  362. URCorner,
  363. /// <summary>
  364. /// Lower right corner
  365. /// </summary>
  366. LRCorner,
  367. /// <summary>
  368. /// Left tee
  369. /// </summary>
  370. LeftTee,
  371. /// <summary>
  372. /// Right tee
  373. /// </summary>
  374. RightTee,
  375. /// <summary>
  376. /// Top tee
  377. /// </summary>
  378. TopTee,
  379. /// <summary>
  380. /// The bottom tee.
  381. /// </summary>
  382. BottomTee,
  383. }
  384. /// <summary>
  385. /// ConsoleDriver is an abstract class that defines the requirements for a console driver. One implementation if the CursesDriver, and another one uses the .NET Console one.
  386. /// </summary>
  387. public abstract class ConsoleDriver {
  388. /// <summary>
  389. /// The handler fired when the terminal is resized.
  390. /// </summary>
  391. protected Action TerminalResized;
  392. /// <summary>
  393. /// The current number of columns in the terminal.
  394. /// </summary>
  395. public abstract int Cols { get; }
  396. /// <summary>
  397. /// The current number of rows in the terminal.
  398. /// </summary>
  399. public abstract int Rows { get; }
  400. /// <summary>
  401. /// Initializes the driver
  402. /// </summary>
  403. /// <param name="terminalResized">Method to invoke when the terminal is resized.</param>
  404. public abstract void Init (Action terminalResized);
  405. /// <summary>
  406. /// Moves the cursor to the specified column and row.
  407. /// </summary>
  408. /// <param name="col">Column to move the cursor to.</param>
  409. /// <param name="row">Row to move the cursor to.</param>
  410. public abstract void Move (int col, int row);
  411. /// <summary>
  412. /// Adds the specified rune to the display at the current cursor position
  413. /// </summary>
  414. /// <param name="rune">Rune to add.</param>
  415. public abstract void AddRune (Rune rune);
  416. /// <summary>
  417. /// Adds the specified
  418. /// </summary>
  419. /// <param name="str">String.</param>
  420. public abstract void AddStr (ustring str);
  421. /// <summary>
  422. /// Prepare the driver and set the key and mouse events handlers.
  423. /// </summary>
  424. /// <param name="mainLoop">The main loop.</param>
  425. /// <param name="keyHandler">The handler for ProcessKey</param>
  426. /// <param name="keyDownHandler">The handler for key down events</param>
  427. /// <param name="keyUpHandler">The handler for key up events</param>
  428. /// <param name="mouseHandler">The handler for mouse events</param>
  429. public abstract void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler);
  430. /// <summary>
  431. /// Updates the screen to reflect all the changes that have been done to the display buffer
  432. /// </summary>
  433. public abstract void Refresh ();
  434. /// <summary>
  435. /// Updates the location of the cursor position
  436. /// </summary>
  437. public abstract void UpdateCursor ();
  438. /// <summary>
  439. /// Ends the execution of the console driver.
  440. /// </summary>
  441. public abstract void End ();
  442. /// <summary>
  443. /// Redraws the physical screen with the contents that have been queued up via any of the printing commands.
  444. /// </summary>
  445. public abstract void UpdateScreen ();
  446. /// <summary>
  447. /// Selects the specified attribute as the attribute to use for future calls to AddRune, AddString.
  448. /// </summary>
  449. /// <param name="c">C.</param>
  450. public abstract void SetAttribute (Attribute c);
  451. /// <summary>
  452. /// Set Colors from limit sets of colors.
  453. /// </summary>
  454. /// <param name="foreground">Foreground.</param>
  455. /// <param name="background">Background.</param>
  456. public abstract void SetColors (ConsoleColor foreground, ConsoleColor background);
  457. // Advanced uses - set colors to any pre-set pairs, you would need to init_color
  458. // that independently with the R, G, B values.
  459. /// <summary>
  460. /// Advanced uses - set colors to any pre-set pairs, you would need to init_color
  461. /// that independently with the R, G, B values.
  462. /// </summary>
  463. /// <param name="foregroundColorId">Foreground color identifier.</param>
  464. /// <param name="backgroundColorId">Background color identifier.</param>
  465. public abstract void SetColors (short foregroundColorId, short backgroundColorId);
  466. /// <summary>
  467. /// Set the handler when the terminal is resized.
  468. /// </summary>
  469. /// <param name="terminalResized"></param>
  470. public void SetTerminalResized(Action terminalResized)
  471. {
  472. TerminalResized = terminalResized;
  473. }
  474. // Useful for debugging (e.g. change to `*`
  475. const char clearChar = ' ';
  476. /// <summary>
  477. /// Draws the title for a Window-style view incorporating padding.
  478. /// </summary>
  479. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  480. /// <param name="title">The title for the window. The title will only be drawn if <c>title</c> is not null or empty and paddingTop is greater than 0.</param>
  481. /// <param name="paddingLeft">Number of columns to pad on the left (if 0 the border will not appear on the left).</param>
  482. /// <param name="paddingTop">Number of rows to pad on the top (if 0 the border and title will not appear on the top).</param>
  483. /// <param name="paddingRight">Number of columns to pad on the right (if 0 the border will not appear on the right).</param>
  484. /// <param name="paddingBottom">Number of rows to pad on the bottom (if 0 the border will not appear on the bottom).</param>
  485. /// <param name="textAlignment">Not yet immplemented.</param>
  486. /// <remarks></remarks>
  487. public void DrawWindowTitle (Rect region, ustring title, int paddingLeft, int paddingTop, int paddingRight, int paddingBottom, TextAlignment textAlignment = TextAlignment.Left)
  488. {
  489. var width = region.Width - (paddingLeft + 2) * 2;
  490. if (!ustring.IsNullOrEmpty(title) && width > 4 && region.Y + paddingTop <= region.Y + paddingBottom) {
  491. Move (region.X + 1 + paddingLeft, region.Y + paddingTop);
  492. AddRune (' ');
  493. var str = title.Length >= width ? title [0, width - 2] : title;
  494. AddStr (str);
  495. AddRune (' ');
  496. }
  497. }
  498. /// <summary>
  499. /// Draws a frame for a window with padding aand n optional visible border inside the padding.
  500. /// </summary>
  501. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  502. /// <param name="paddingLeft">Number of columns to pad on the left (if 0 the border will not appear on the left).</param>
  503. /// <param name="paddingTop">Number of rows to pad on the top (if 0 the border and title will not appear on the top).</param>
  504. /// <param name="paddingRight">Number of columns to pad on the right (if 0 the border will not appear on the right).</param>
  505. /// <param name="paddingBottom">Number of rows to pad on the bottom (if 0 the border will not appear on the bottom).</param>
  506. /// <param name="border">If set to <c>true</c> and any padding dimension is > 0 the border will be drawn.</param>
  507. /// <param name="fill">If set to <c>true</c> it will clear the content area (the area inside the padding) with the current color, otherwise the content area will be left untouched.</param>
  508. public void DrawWindowFrame (Rect region, int paddingLeft = 0, int paddingTop = 0, int paddingRight = 0, int paddingBottom = 0, bool border = true, bool fill = false)
  509. {
  510. void AddRuneAt (int col, int row, Rune ch)
  511. {
  512. Move (col, row);
  513. AddRune (ch);
  514. }
  515. int fwidth = (int)(region.Width - (paddingRight + paddingLeft));
  516. int fheight = (int)(region.Height - (paddingBottom + paddingTop));
  517. int fleft = region.X + paddingLeft;
  518. int fright = fleft + fwidth + 1;
  519. int ftop = region.Y + paddingTop;
  520. int fbottom = ftop + fheight + 1;
  521. Rune hLine = border ? HLine : clearChar;
  522. Rune vLine = border ? VLine : clearChar;
  523. Rune uRCorner = border ? URCorner : clearChar;
  524. Rune uLCorner = border ? ULCorner : clearChar;
  525. Rune lLCorner = border ? LLCorner : clearChar;
  526. Rune lRCorner = border ? LRCorner : clearChar;
  527. // Outside top
  528. if (paddingTop > 1) {
  529. for (int r = region.Y; r < ftop; r++) {
  530. for (int c = region.X; c <= fright + paddingRight; c++) {
  531. AddRuneAt (c, r, clearChar);
  532. }
  533. }
  534. }
  535. // Outside top-left
  536. for (int c = region.X; c <= fleft; c++) {
  537. AddRuneAt (c, ftop, clearChar);
  538. }
  539. // Frame top-left corner
  540. AddRuneAt (fleft, ftop, paddingTop >= 0 ? (paddingLeft >= 0 ? uLCorner : hLine) : clearChar);
  541. // Frame top
  542. for (int c = fleft + 1; c <= fright; c++) {
  543. AddRuneAt (c, ftop, paddingTop > 0 ? hLine : clearChar);
  544. }
  545. // Frame top-right corner
  546. if (fright > fleft) {
  547. AddRuneAt (fright, ftop, paddingTop >= 0 ? (paddingRight >= 0 ? uRCorner : hLine) : clearChar);
  548. }
  549. // Outside top-right corner
  550. for (int c = fright + 1; c < fright + paddingRight; c++) {
  551. AddRuneAt (c, ftop, clearChar);
  552. }
  553. // Left, Fill, Right
  554. if (fbottom > ftop) {
  555. for (int r = ftop + 1; r < fbottom; r++) {
  556. // Outside left
  557. for (int c = region.X; c < fleft; c++) {
  558. AddRuneAt (c, r, clearChar);
  559. }
  560. // Frame left
  561. AddRuneAt (fleft, r, paddingLeft > 0 ? vLine : clearChar);
  562. // Fill
  563. if (fill) {
  564. for (int x = fleft + 1; x < fright; x++) {
  565. AddRuneAt (x, r, clearChar);
  566. }
  567. }
  568. // Frame right
  569. if (fright > fleft) {
  570. AddRuneAt (fright, r, paddingRight > 0 ? vLine : clearChar);
  571. }
  572. // Outside right
  573. for (int c = fright + 1; c < fright + paddingRight; c++) {
  574. AddRuneAt (c, r, clearChar);
  575. }
  576. }
  577. // Outside Bottom
  578. for (int c = region.X; c < fleft; c++) {
  579. AddRuneAt (c, fbottom, clearChar);
  580. }
  581. // Frame bottom-left
  582. AddRuneAt (fleft, fbottom, paddingLeft > 0 ? lLCorner : clearChar);
  583. if (fright > fleft) {
  584. // Frame bottom
  585. for (int c = fleft + 1; c < fright; c++) {
  586. AddRuneAt (c, fbottom, paddingBottom > 0 ? hLine : clearChar);
  587. }
  588. // Frame bottom-right
  589. AddRuneAt (fright, fbottom, paddingRight > 0 ? (paddingBottom > 0 ? lRCorner : hLine) : clearChar);
  590. }
  591. // Outside right
  592. for (int c = fright + 1; c < fright + paddingRight; c++) {
  593. AddRuneAt (c, fbottom, clearChar);
  594. }
  595. }
  596. // Out bottom - ensure top is always drawn if we overlap
  597. if (paddingBottom > 0) {
  598. for (int r = fbottom + 1; r < fbottom + paddingBottom; r++) {
  599. for (int c = region.X; c <= fright + paddingRight; c++) {
  600. AddRuneAt (c, r, clearChar);
  601. }
  602. }
  603. }
  604. }
  605. /// <summary>
  606. /// Draws a frame on the specified region with the specified padding around the frame.
  607. /// </summary>
  608. /// <param name="region">Region where the frame will be drawn..</param>
  609. /// <param name="padding">Padding to add on the sides.</param>
  610. /// <param name="fill">If set to <c>true</c> it will clear the contents with the current color, otherwise the contents will be left untouched.</param>
  611. /// <remarks>This is a legacy/depcrecated API. Use <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool)"/>.</remarks>
  612. /// <remarks>A padding value of 0 means there is actually a 1 cell border.</remarks>
  613. public virtual void DrawFrame (Rect region, int padding, bool fill)
  614. {
  615. // DrawFrame assumes the frame is always at least one row/col thick
  616. // DrawWindowFrame assumes a padding of 0 means NO padding
  617. padding++;
  618. DrawWindowFrame (new Rect (region.X - 1, region.Y - 1, region.Width, region.Height), padding, padding, padding, padding, fill: fill);
  619. }
  620. /// <summary>
  621. /// Suspend the application, typically needs to save the state, suspend the app and upon return, reset the console driver.
  622. /// </summary>
  623. public abstract void Suspend ();
  624. Rect clip;
  625. /// <summary>
  626. /// Controls the current clipping region that AddRune/AddStr is subject to.
  627. /// </summary>
  628. /// <value>The clip.</value>
  629. public Rect Clip {
  630. get => clip;
  631. set => this.clip = value;
  632. }
  633. /// <summary>
  634. /// Start of mouse moves.
  635. /// </summary>
  636. public abstract void StartReportingMouseMoves ();
  637. /// <summary>
  638. /// Stop reporting mouses moves.
  639. /// </summary>
  640. public abstract void StopReportingMouseMoves ();
  641. /// <summary>
  642. /// Disables the cooked event processing from the mouse driver. At startup, it is assumed mouse events are cooked.
  643. /// </summary>
  644. public abstract void UncookMouse ();
  645. /// <summary>
  646. /// Enables the cooked event processing from the mouse driver
  647. /// </summary>
  648. public abstract void CookMouse ();
  649. /// <summary>
  650. /// Horizontal line character.
  651. /// </summary>
  652. public Rune HLine;
  653. /// <summary>
  654. /// Vertical line character.
  655. /// </summary>
  656. public Rune VLine;
  657. /// <summary>
  658. /// Stipple pattern
  659. /// </summary>
  660. public Rune Stipple;
  661. /// <summary>
  662. /// Diamond character
  663. /// </summary>
  664. public Rune Diamond;
  665. /// <summary>
  666. /// Upper left corner
  667. /// </summary>
  668. public Rune ULCorner;
  669. /// <summary>
  670. /// Lower left corner
  671. /// </summary>
  672. public Rune LLCorner;
  673. /// <summary>
  674. /// Upper right corner
  675. /// </summary>
  676. public Rune URCorner;
  677. /// <summary>
  678. /// Lower right corner
  679. /// </summary>
  680. public Rune LRCorner;
  681. /// <summary>
  682. /// Left tee
  683. /// </summary>
  684. public Rune LeftTee;
  685. /// <summary>
  686. /// Right tee
  687. /// </summary>
  688. public Rune RightTee;
  689. /// <summary>
  690. /// Top tee
  691. /// </summary>
  692. public Rune TopTee;
  693. /// <summary>
  694. /// The bottom tee.
  695. /// </summary>
  696. public Rune BottomTee;
  697. /// <summary>
  698. /// Make the attribute for the foreground and background colors.
  699. /// </summary>
  700. /// <param name="fore">Foreground.</param>
  701. /// <param name="back">Background.</param>
  702. /// <returns></returns>
  703. public abstract Attribute MakeAttribute (Color fore, Color back);
  704. }
  705. }