ConsoleDriver.cs 31 KB

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