ConsoleDriver.cs 34 KB

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