ConsoleDriver.cs 28 KB

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