ConsoleDriver.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. public override bool Equals (object obj)
  294. {
  295. return Equals (obj as ColorScheme);
  296. }
  297. public bool Equals (ColorScheme other)
  298. {
  299. return other != null &&
  300. EqualityComparer<Attribute>.Default.Equals (_normal, other._normal) &&
  301. EqualityComparer<Attribute>.Default.Equals (_focus, other._focus) &&
  302. EqualityComparer<Attribute>.Default.Equals (_hotNormal, other._hotNormal) &&
  303. EqualityComparer<Attribute>.Default.Equals (_hotFocus, other._hotFocus) &&
  304. EqualityComparer<Attribute>.Default.Equals (_disabled, other._disabled);
  305. }
  306. public override int GetHashCode ()
  307. {
  308. int hashCode = -1242460230;
  309. hashCode = hashCode * -1521134295 + _normal.GetHashCode ();
  310. hashCode = hashCode * -1521134295 + _focus.GetHashCode ();
  311. hashCode = hashCode * -1521134295 + _hotNormal.GetHashCode ();
  312. hashCode = hashCode * -1521134295 + _hotFocus.GetHashCode ();
  313. hashCode = hashCode * -1521134295 + _disabled.GetHashCode ();
  314. return hashCode;
  315. }
  316. public static bool operator == (ColorScheme left, ColorScheme right)
  317. {
  318. return EqualityComparer<ColorScheme>.Default.Equals (left, right);
  319. }
  320. public static bool operator != (ColorScheme left, ColorScheme right)
  321. {
  322. return !(left == right);
  323. }
  324. }
  325. /// <summary>
  326. /// The default <see cref="ColorScheme"/>s for the application.
  327. /// </summary>
  328. public static class Colors {
  329. static Colors ()
  330. {
  331. // Use reflection to dynamically create the default set of ColorSchemes from the list defiined
  332. // by the class.
  333. ColorSchemes = typeof (Colors).GetProperties ()
  334. .Where(p => p.PropertyType == typeof(ColorScheme))
  335. .Select (p => new KeyValuePair<string, ColorScheme> (p.Name, new ColorScheme())) // (ColorScheme)p.GetValue (p)))
  336. .ToDictionary (t => t.Key, t => t.Value);
  337. }
  338. /// <summary>
  339. /// The application toplevel color scheme, for the default toplevel views.
  340. /// </summary>
  341. /// <remarks>
  342. /// <para>
  343. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["TopLevel"];</c>
  344. /// </para>
  345. /// </remarks>
  346. public static ColorScheme TopLevel { get => GetColorScheme (); set => SetColorScheme (value); }
  347. /// <summary>
  348. /// The base color scheme, for the default toplevel views.
  349. /// </summary>
  350. /// <remarks>
  351. /// <para>
  352. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Base"];</c>
  353. /// </para>
  354. /// </remarks>
  355. public static ColorScheme Base { get => GetColorScheme (); set => SetColorScheme (value); }
  356. /// <summary>
  357. /// The dialog color scheme, for standard popup dialog boxes
  358. /// </summary>
  359. /// <remarks>
  360. /// <para>
  361. /// This API will be deprecated in the future. Use <see cref="Colors.ColorSchemes"/> instead (e.g. <c>edit.ColorScheme = Colors.ColorSchemes["Dialog"];</c>
  362. /// </para>
  363. /// </remarks>
  364. public static ColorScheme Dialog { get => GetColorScheme (); set => SetColorScheme (value); }
  365. /// <summary>
  366. /// The menu bar color
  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["Menu"];</c>
  371. /// </para>
  372. /// </remarks>
  373. public static ColorScheme Menu { get => GetColorScheme (); set => SetColorScheme (value); }
  374. /// <summary>
  375. /// The color scheme for showing errors.
  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["Error"];</c>
  380. /// </para>
  381. /// </remarks>
  382. public static ColorScheme Error { get => GetColorScheme (); set => SetColorScheme (value); }
  383. static ColorScheme GetColorScheme([CallerMemberName] string callerMemberName = null)
  384. {
  385. return ColorSchemes [callerMemberName];
  386. }
  387. static void SetColorScheme (ColorScheme colorScheme, [CallerMemberName] string callerMemberName = null)
  388. {
  389. ColorSchemes [callerMemberName] = colorScheme;
  390. colorScheme.caller = callerMemberName;
  391. }
  392. /// <summary>
  393. /// Provides the defined <see cref="ColorScheme"/>s.
  394. /// </summary>
  395. public static Dictionary<string, ColorScheme> ColorSchemes { get; }
  396. }
  397. ///// <summary>
  398. ///// Special characters that can be drawn with
  399. ///// </summary>
  400. //public enum SpecialChar {
  401. // /// <summary>
  402. // /// Horizontal line character.
  403. // /// </summary>
  404. // HLine,
  405. // /// <summary>
  406. // /// Vertical line character.
  407. // /// </summary>
  408. // VLine,
  409. // /// <summary>
  410. // /// Stipple pattern
  411. // /// </summary>
  412. // Stipple,
  413. // /// <summary>
  414. // /// Diamond character
  415. // /// </summary>
  416. // Diamond,
  417. // /// <summary>
  418. // /// Upper left corner
  419. // /// </summary>
  420. // ULCorner,
  421. // /// <summary>
  422. // /// Lower left corner
  423. // /// </summary>
  424. // LLCorner,
  425. // /// <summary>
  426. // /// Upper right corner
  427. // /// </summary>
  428. // URCorner,
  429. // /// <summary>
  430. // /// Lower right corner
  431. // /// </summary>
  432. // LRCorner,
  433. // /// <summary>
  434. // /// Left tee
  435. // /// </summary>
  436. // LeftTee,
  437. // /// <summary>
  438. // /// Right tee
  439. // /// </summary>
  440. // RightTee,
  441. // /// <summary>
  442. // /// Top tee
  443. // /// </summary>
  444. // TopTee,
  445. // /// <summary>
  446. // /// The bottom tee.
  447. // /// </summary>
  448. // BottomTee,
  449. //}
  450. /// <summary>
  451. /// ConsoleDriver is an abstract class that defines the requirements for a console driver.
  452. /// 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.
  453. /// </summary>
  454. public abstract class ConsoleDriver {
  455. /// <summary>
  456. /// The handler fired when the terminal is resized.
  457. /// </summary>
  458. protected Action TerminalResized;
  459. /// <summary>
  460. /// The current number of columns in the terminal.
  461. /// </summary>
  462. public abstract int Cols { get; }
  463. /// <summary>
  464. /// The current number of rows in the terminal.
  465. /// </summary>
  466. public abstract int Rows { get; }
  467. /// <summary>
  468. /// Initializes the driver
  469. /// </summary>
  470. /// <param name="terminalResized">Method to invoke when the terminal is resized.</param>
  471. public abstract void Init (Action terminalResized);
  472. /// <summary>
  473. /// Moves the cursor to the specified column and row.
  474. /// </summary>
  475. /// <param name="col">Column to move the cursor to.</param>
  476. /// <param name="row">Row to move the cursor to.</param>
  477. public abstract void Move (int col, int row);
  478. /// <summary>
  479. /// Adds the specified rune to the display at the current cursor position
  480. /// </summary>
  481. /// <param name="rune">Rune to add.</param>
  482. public abstract void AddRune (Rune rune);
  483. /// <summary>
  484. /// Adds the specified
  485. /// </summary>
  486. /// <param name="str">String.</param>
  487. public abstract void AddStr (ustring str);
  488. /// <summary>
  489. /// Prepare the driver and set the key and mouse events handlers.
  490. /// </summary>
  491. /// <param name="mainLoop">The main loop.</param>
  492. /// <param name="keyHandler">The handler for ProcessKey</param>
  493. /// <param name="keyDownHandler">The handler for key down events</param>
  494. /// <param name="keyUpHandler">The handler for key up events</param>
  495. /// <param name="mouseHandler">The handler for mouse events</param>
  496. public abstract void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler);
  497. /// <summary>
  498. /// Updates the screen to reflect all the changes that have been done to the display buffer
  499. /// </summary>
  500. public abstract void Refresh ();
  501. /// <summary>
  502. /// Updates the location of the cursor position
  503. /// </summary>
  504. public abstract void UpdateCursor ();
  505. /// <summary>
  506. /// Ends the execution of the console driver.
  507. /// </summary>
  508. public abstract void End ();
  509. /// <summary>
  510. /// Redraws the physical screen with the contents that have been queued up via any of the printing commands.
  511. /// </summary>
  512. public abstract void UpdateScreen ();
  513. /// <summary>
  514. /// Selects the specified attribute as the attribute to use for future calls to AddRune, AddString.
  515. /// </summary>
  516. /// <param name="c">C.</param>
  517. public abstract void SetAttribute (Attribute c);
  518. /// <summary>
  519. /// Set Colors from limit sets of colors.
  520. /// </summary>
  521. /// <param name="foreground">Foreground.</param>
  522. /// <param name="background">Background.</param>
  523. public abstract void SetColors (ConsoleColor foreground, ConsoleColor background);
  524. // Advanced uses - set colors to any pre-set pairs, you would need to init_color
  525. // that independently with the R, G, B values.
  526. /// <summary>
  527. /// Advanced uses - set colors to any pre-set pairs, you would need to init_color
  528. /// that independently with the R, G, B values.
  529. /// </summary>
  530. /// <param name="foregroundColorId">Foreground color identifier.</param>
  531. /// <param name="backgroundColorId">Background color identifier.</param>
  532. public abstract void SetColors (short foregroundColorId, short backgroundColorId);
  533. /// <summary>
  534. /// Set the handler when the terminal is resized.
  535. /// </summary>
  536. /// <param name="terminalResized"></param>
  537. public void SetTerminalResized (Action terminalResized)
  538. {
  539. TerminalResized = terminalResized;
  540. }
  541. /// <summary>
  542. /// Draws the title for a Window-style view incorporating padding.
  543. /// </summary>
  544. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  545. /// <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>
  546. /// <param name="paddingLeft">Number of columns to pad on the left (if 0 the border will not appear on the left).</param>
  547. /// <param name="paddingTop">Number of rows to pad on the top (if 0 the border and title will not appear on the top).</param>
  548. /// <param name="paddingRight">Number of columns to pad on the right (if 0 the border will not appear on the right).</param>
  549. /// <param name="paddingBottom">Number of rows to pad on the bottom (if 0 the border will not appear on the bottom).</param>
  550. /// <param name="textAlignment">Not yet immplemented.</param>
  551. /// <remarks></remarks>
  552. public virtual void DrawWindowTitle (Rect region, ustring title, int paddingLeft, int paddingTop, int paddingRight, int paddingBottom, TextAlignment textAlignment = TextAlignment.Left)
  553. {
  554. var width = region.Width - (paddingLeft + 2) * 2;
  555. if (!ustring.IsNullOrEmpty (title) && width > 4 && region.Y + paddingTop <= region.Y + paddingBottom) {
  556. Move (region.X + 1 + paddingLeft, region.Y + paddingTop);
  557. AddRune (' ');
  558. var str = title.Length >= width ? title [0, width - 2] : title;
  559. AddStr (str);
  560. AddRune (' ');
  561. }
  562. }
  563. /// <summary>
  564. /// Enables diagnostic funcions
  565. /// </summary>
  566. [Flags]
  567. public enum DiagnosticFlags : uint {
  568. /// <summary>
  569. /// All diagnostics off
  570. /// </summary>
  571. Off = 0b_0000_0000,
  572. /// <summary>
  573. /// When enabled, <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool)"/> will draw a
  574. /// ruler in the frame for any side with a padding value greater than 0.
  575. /// </summary>
  576. FrameRuler = 0b_0000_0001,
  577. /// <summary>
  578. /// When Enabled, <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool)"/> will use
  579. /// 'L', 'R', 'T', and 'B' for padding instead of ' '.
  580. /// </summary>
  581. FramePadding = 0b_0000_0010,
  582. }
  583. /// <summary>
  584. /// Set flags to enable/disable <see cref="ConsoleDriver"/> diagnostics.
  585. /// </summary>
  586. public static DiagnosticFlags Diagnostics { get; set; }
  587. /// <summary>
  588. /// Draws a frame for a window with padding and an optional visible border inside the padding.
  589. /// </summary>
  590. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  591. /// <param name="paddingLeft">Number of columns to pad on the left (if 0 the border will not appear on the left).</param>
  592. /// <param name="paddingTop">Number of rows to pad on the top (if 0 the border and title will not appear on the top).</param>
  593. /// <param name="paddingRight">Number of columns to pad on the right (if 0 the border will not appear on the right).</param>
  594. /// <param name="paddingBottom">Number of rows to pad on the bottom (if 0 the border will not appear on the bottom).</param>
  595. /// <param name="border">If set to <c>true</c> and any padding dimension is > 0 the border will be drawn.</param>
  596. /// <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>
  597. public virtual void DrawWindowFrame (Rect region, int paddingLeft = 0, int paddingTop = 0, int paddingRight = 0, int paddingBottom = 0, bool border = true, bool fill = false)
  598. {
  599. char clearChar = ' ';
  600. char leftChar = clearChar;
  601. char rightChar = clearChar;
  602. char topChar = clearChar;
  603. char bottomChar = clearChar;
  604. if ((Diagnostics & DiagnosticFlags.FramePadding) == DiagnosticFlags.FramePadding) {
  605. leftChar = 'L';
  606. rightChar = 'R';
  607. topChar = 'T';
  608. bottomChar = 'B';
  609. clearChar = 'C';
  610. }
  611. void AddRuneAt (int col, int row, Rune ch)
  612. {
  613. Move (col, row);
  614. AddRune (ch);
  615. }
  616. // fwidth is count of hLine chars
  617. int fwidth = (int)(region.Width - (paddingRight + paddingLeft));
  618. // fheight is count of vLine chars
  619. int fheight = (int)(region.Height - (paddingBottom + paddingTop));
  620. // fleft is location of left frame line
  621. int fleft = region.X + paddingLeft - 1;
  622. // fright is location of right frame line
  623. int fright = fleft + fwidth + 1;
  624. // ftop is location of top frame line
  625. int ftop = region.Y + paddingTop - 1;
  626. // fbottom is locaiton of bottom frame line
  627. int fbottom = ftop + fheight + 1;
  628. Rune hLine = border ? HLine : clearChar;
  629. Rune vLine = border ? VLine : clearChar;
  630. Rune uRCorner = border ? URCorner : clearChar;
  631. Rune uLCorner = border ? ULCorner : clearChar;
  632. Rune lLCorner = border ? LLCorner : clearChar;
  633. Rune lRCorner = border ? LRCorner : clearChar;
  634. // Outside top
  635. if (paddingTop > 1) {
  636. for (int r = region.Y; r < ftop; r++) {
  637. for (int c = region.X; c < region.X + region.Width; c++) {
  638. AddRuneAt (c, r, topChar);
  639. }
  640. }
  641. }
  642. // Outside top-left
  643. for (int c = region.X; c < fleft; c++) {
  644. AddRuneAt (c, ftop, leftChar);
  645. }
  646. // Frame top-left corner
  647. AddRuneAt (fleft, ftop, paddingTop >= 0 ? (paddingLeft >= 0 ? uLCorner : hLine) : leftChar);
  648. // Frame top
  649. for (int c = fleft + 1; c < fleft + 1 + fwidth; c++) {
  650. AddRuneAt (c, ftop, paddingTop > 0 ? hLine : topChar);
  651. }
  652. // Frame top-right corner
  653. if (fright > fleft) {
  654. AddRuneAt (fright, ftop, paddingTop >= 0 ? (paddingRight >= 0 ? uRCorner : hLine) : rightChar);
  655. }
  656. // Outside top-right corner
  657. for (int c = fright + 1; c < fright + paddingRight; c++) {
  658. AddRuneAt (c, ftop, rightChar);
  659. }
  660. // Left, Fill, Right
  661. if (fbottom > ftop) {
  662. for (int r = ftop + 1; r < fbottom; r++) {
  663. // Outside left
  664. for (int c = region.X; c < fleft; c++) {
  665. AddRuneAt (c, r, leftChar);
  666. }
  667. // Frame left
  668. AddRuneAt (fleft, r, paddingLeft > 0 ? vLine : leftChar);
  669. // Fill
  670. if (fill) {
  671. for (int x = fleft + 1; x < fright; x++) {
  672. AddRuneAt (x, r, clearChar);
  673. }
  674. }
  675. // Frame right
  676. if (fright > fleft) {
  677. var v = vLine;
  678. if ((Diagnostics & DiagnosticFlags.FrameRuler) == DiagnosticFlags.FrameRuler) {
  679. v = (char)(((int)'0') + ((r - ftop) % 10)); // vLine;
  680. }
  681. AddRuneAt (fright, r, paddingRight > 0 ? v : rightChar);
  682. }
  683. // Outside right
  684. for (int c = fright + 1; c < fright + paddingRight; c++) {
  685. AddRuneAt (c, r, rightChar);
  686. }
  687. }
  688. // Outside Bottom
  689. for (int c = region.X; c < region.X + region.Width; c++) {
  690. AddRuneAt (c, fbottom, leftChar);
  691. }
  692. // Frame bottom-left
  693. AddRuneAt (fleft, fbottom, paddingLeft > 0 ? lLCorner : leftChar);
  694. if (fright > fleft) {
  695. // Frame bottom
  696. for (int c = fleft + 1; c < fright; c++) {
  697. var h = hLine;
  698. if ((Diagnostics & DiagnosticFlags.FrameRuler) == DiagnosticFlags.FrameRuler) {
  699. h = (char)(((int)'0') + ((c - fleft) % 10)); // hLine;
  700. }
  701. AddRuneAt (c, fbottom, paddingBottom > 0 ? h : bottomChar);
  702. }
  703. // Frame bottom-right
  704. AddRuneAt (fright, fbottom, paddingRight > 0 ? (paddingBottom > 0 ? lRCorner : hLine) : rightChar);
  705. }
  706. // Outside right
  707. for (int c = fright + 1; c < fright + paddingRight; c++) {
  708. AddRuneAt (c, fbottom, rightChar);
  709. }
  710. }
  711. // Out bottom - ensure top is always drawn if we overlap
  712. if (paddingBottom > 0) {
  713. for (int r = fbottom + 1; r < fbottom + paddingBottom; r++) {
  714. for (int c = region.X; c < region.X + region.Width; c++) {
  715. AddRuneAt (c, r, bottomChar);
  716. }
  717. }
  718. }
  719. }
  720. /// <summary>
  721. /// Draws a frame on the specified region with the specified padding around the frame.
  722. /// </summary>
  723. /// <param name="region">Screen relative region where the frame will be drawn.</param>
  724. /// <param name="padding">Padding to add on the sides.</param>
  725. /// <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>
  726. /// <remarks>This API has been superceded by <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool)"/>.</remarks>
  727. /// <remarks>This API is equivlalent to calling <c>DrawWindowFrame(Rect, p - 1, p - 1, p - 1, p - 1)</c>. In other words,
  728. /// A padding value of 0 means there is actually a one cell border.
  729. /// </remarks>
  730. public virtual void DrawFrame (Rect region, int padding, bool fill)
  731. {
  732. // DrawFrame assumes the border is always at least one row/col thick
  733. // DrawWindowFrame assumes a padding of 0 means NO padding and no frame
  734. DrawWindowFrame (new Rect (region.X, region.Y, region.Width, region.Height),
  735. padding + 1, padding + 1, padding + 1, padding + 1, border: false, fill: fill);
  736. }
  737. /// <summary>
  738. /// Suspend the application, typically needs to save the state, suspend the app and upon return, reset the console driver.
  739. /// </summary>
  740. public abstract void Suspend ();
  741. Rect clip;
  742. /// <summary>
  743. /// Controls the current clipping region that AddRune/AddStr is subject to.
  744. /// </summary>
  745. /// <value>The clip.</value>
  746. public Rect Clip {
  747. get => clip;
  748. set => this.clip = value;
  749. }
  750. /// <summary>
  751. /// Start of mouse moves.
  752. /// </summary>
  753. public abstract void StartReportingMouseMoves ();
  754. /// <summary>
  755. /// Stop reporting mouses moves.
  756. /// </summary>
  757. public abstract void StopReportingMouseMoves ();
  758. /// <summary>
  759. /// Disables the cooked event processing from the mouse driver. At startup, it is assumed mouse events are cooked.
  760. /// </summary>
  761. public abstract void UncookMouse ();
  762. /// <summary>
  763. /// Enables the cooked event processing from the mouse driver
  764. /// </summary>
  765. public abstract void CookMouse ();
  766. /// <summary>
  767. /// Horizontal line character.
  768. /// </summary>
  769. public Rune HLine;
  770. /// <summary>
  771. /// Vertical line character.
  772. /// </summary>
  773. public Rune VLine;
  774. /// <summary>
  775. /// Stipple pattern
  776. /// </summary>
  777. public Rune Stipple;
  778. /// <summary>
  779. /// Diamond character
  780. /// </summary>
  781. public Rune Diamond;
  782. /// <summary>
  783. /// Upper left corner
  784. /// </summary>
  785. public Rune ULCorner;
  786. /// <summary>
  787. /// Lower left corner
  788. /// </summary>
  789. public Rune LLCorner;
  790. /// <summary>
  791. /// Upper right corner
  792. /// </summary>
  793. public Rune URCorner;
  794. /// <summary>
  795. /// Lower right corner
  796. /// </summary>
  797. public Rune LRCorner;
  798. /// <summary>
  799. /// Left tee
  800. /// </summary>
  801. public Rune LeftTee;
  802. /// <summary>
  803. /// Right tee
  804. /// </summary>
  805. public Rune RightTee;
  806. /// <summary>
  807. /// Top tee
  808. /// </summary>
  809. public Rune TopTee;
  810. /// <summary>
  811. /// The bottom tee.
  812. /// </summary>
  813. public Rune BottomTee;
  814. /// <summary>
  815. /// Make the attribute for the foreground and background colors.
  816. /// </summary>
  817. /// <param name="fore">Foreground.</param>
  818. /// <param name="back">Background.</param>
  819. /// <returns></returns>
  820. public abstract Attribute MakeAttribute (Color fore, Color back);
  821. }
  822. }