ConsoleDriver.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. //
  2. // Driver.cs: Definition for the Console Driver API
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Runtime.InteropServices;
  10. using Mono.Terminal;
  11. using NStack;
  12. using Unix.Terminal;
  13. namespace Terminal.Gui {
  14. /// <summary>
  15. /// Basic colors that can be used to set the foreground and background colors in console applications. These can only be
  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. /// Attributes 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 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. /// <summary>
  94. /// Initializes a new instance of the <see cref="T:Terminal.Gui.Attribute"/> struct.
  95. /// </summary>
  96. /// <param name="value">Value.</param>
  97. public Attribute (int value)
  98. {
  99. this.value = value;
  100. }
  101. /// <summary>
  102. /// Implicit conversion from an attribute to the underlying Int32 representation
  103. /// </summary>
  104. /// <returns>The integer value stored in the attribute.</returns>
  105. /// <param name="c">The attribute to convert</param>
  106. public static implicit operator int (Attribute c) => c.value;
  107. /// <summary>
  108. /// Implicitly convert an integer value into an attribute
  109. /// </summary>
  110. /// <returns>An attribute with the specified integer value.</returns>
  111. /// <param name="v">value</param>
  112. public static implicit operator Attribute (int v) => new Attribute (v);
  113. /// <summary>
  114. /// Creates an attribute from the specified foreground and background.
  115. /// </summary>
  116. /// <returns>The make.</returns>
  117. /// <param name="foreground">Foreground color to use.</param>
  118. /// <param name="background">Background color to use.</param>
  119. public static Attribute Make (Color foreground, Color background)
  120. {
  121. if (Application.Driver == null)
  122. throw new InvalidOperationException ("The Application has not been initialized");
  123. return Application.Driver.MakeAttribute (foreground, background);
  124. }
  125. }
  126. /// <summary>
  127. /// Color scheme definitions, they cover some common scenarios and are used
  128. /// typically in toplevel containers to set the scheme that is used by all the
  129. /// views contained inside.
  130. /// </summary>
  131. public class ColorScheme {
  132. /// <summary>
  133. /// The default color for text, when the view is not focused.
  134. /// </summary>
  135. public Attribute Normal;
  136. /// <summary>
  137. /// The color for text when the view has the focus.
  138. /// </summary>
  139. public Attribute Focus;
  140. /// <summary>
  141. /// The color for the hotkey when a view is not focused
  142. /// </summary>
  143. public Attribute HotNormal;
  144. /// <summary>
  145. /// The color for the hotkey when the view is focused.
  146. /// </summary>
  147. public Attribute HotFocus;
  148. }
  149. /// <summary>
  150. /// The default ColorSchemes for the application.
  151. /// </summary>
  152. public static class Colors {
  153. /// <summary>
  154. /// The base color scheme, for the default toplevel views.
  155. /// </summary>
  156. public static ColorScheme Base;
  157. /// <summary>
  158. /// The dialog color scheme, for standard popup dialog boxes
  159. /// </summary>
  160. public static ColorScheme Dialog;
  161. /// <summary>
  162. /// The menu bar color
  163. /// </summary>
  164. public static ColorScheme Menu;
  165. /// <summary>
  166. /// The color scheme for showing errors.
  167. /// </summary>
  168. public static ColorScheme Error;
  169. }
  170. /// <summary>
  171. /// Special characters that can be drawn with Driver.AddSpecial.
  172. /// </summary>
  173. public enum SpecialChar {
  174. /// <summary>
  175. /// Horizontal line character.
  176. /// </summary>
  177. HLine,
  178. /// <summary>
  179. /// Vertical line character.
  180. /// </summary>
  181. VLine,
  182. /// <summary>
  183. /// Stipple pattern
  184. /// </summary>
  185. Stipple,
  186. /// <summary>
  187. /// Diamond character
  188. /// </summary>
  189. Diamond,
  190. /// <summary>
  191. /// Upper left corner
  192. /// </summary>
  193. ULCorner,
  194. /// <summary>
  195. /// Lower left corner
  196. /// </summary>
  197. LLCorner,
  198. /// <summary>
  199. /// Upper right corner
  200. /// </summary>
  201. URCorner,
  202. /// <summary>
  203. /// Lower right corner
  204. /// </summary>
  205. LRCorner,
  206. /// <summary>
  207. /// Left tee
  208. /// </summary>
  209. LeftTee,
  210. /// <summary>
  211. /// Right tee
  212. /// </summary>
  213. RightTee,
  214. /// <summary>
  215. /// Top tee
  216. /// </summary>
  217. TopTee,
  218. /// <summary>
  219. /// The bottom tee.
  220. /// </summary>
  221. BottomTee,
  222. }
  223. /// <summary>
  224. /// ConsoleDriver is an abstract class that defines the requirements for a console driver. One implementation if the CursesDriver, and another one uses the .NET Console one.
  225. /// </summary>
  226. public abstract class ConsoleDriver {
  227. /// <summary>
  228. /// The current number of columns in the terminal.
  229. /// </summary>
  230. public abstract int Cols { get; }
  231. /// <summary>
  232. /// The current number of rows in the terminal.
  233. /// </summary>
  234. public abstract int Rows { get; }
  235. /// <summary>
  236. /// Initializes the driver
  237. /// </summary>
  238. /// <param name="terminalResized">Method to invoke when the terminal is resized.</param>
  239. public abstract void Init (Action terminalResized);
  240. /// <summary>
  241. /// Moves the cursor to the specified column and row.
  242. /// </summary>
  243. /// <param name="col">Column to move the cursor to.</param>
  244. /// <param name="row">Row to move the cursor to.</param>
  245. public abstract void Move (int col, int row);
  246. /// <summary>
  247. /// Adds the specified rune to the display at the current cursor position
  248. /// </summary>
  249. /// <param name="rune">Rune to add.</param>
  250. public abstract void AddRune (Rune rune);
  251. /// <summary>
  252. /// Adds the specified
  253. /// </summary>
  254. /// <param name="str">String.</param>
  255. public abstract void AddStr (ustring str);
  256. public abstract void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<MouseEvent> mouseHandler);
  257. /// <summary>
  258. /// Updates the screen to reflect all the changes that have been done to the display buffer
  259. /// </summary>
  260. public abstract void Refresh ();
  261. /// <summary>
  262. /// Updates the location of the cursor position
  263. /// </summary>
  264. public abstract void UpdateCursor ();
  265. /// <summary>
  266. /// Ends the execution of the console driver.
  267. /// </summary>
  268. public abstract void End ();
  269. /// <summary>
  270. /// Redraws the physical screen with the contents that have been queued up via any of the printing commands.
  271. /// </summary>
  272. public abstract void UpdateScreen ();
  273. /// <summary>
  274. /// Selects the specified attribute as the attribute to use for future calls to AddRune, AddString.
  275. /// </summary>
  276. /// <param name="c">C.</param>
  277. public abstract void SetAttribute (Attribute c);
  278. // Set Colors from limit sets of colors
  279. public abstract void SetColors (ConsoleColor foreground, ConsoleColor background);
  280. // Advanced uses - set colors to any pre-set pairs, you would need to init_color
  281. // that independently with the R, G, B values.
  282. /// <summary>
  283. /// Advanced uses - set colors to any pre-set pairs, you would need to init_color
  284. /// that independently with the R, G, B values.
  285. /// </summary>
  286. /// <param name="foregroundColorId">Foreground color identifier.</param>
  287. /// <param name="backgroundColorId">Background color identifier.</param>
  288. public abstract void SetColors (short foregroundColorId, short backgroundColorId);
  289. /// <summary>
  290. /// Draws a frame on the specified region with the specified padding around the frame.
  291. /// </summary>
  292. /// <param name="region">Region where the frame will be drawn..</param>
  293. /// <param name="padding">Padding to add on the sides.</param>
  294. /// <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>
  295. public virtual void DrawFrame (Rect region, int padding, bool fill)
  296. {
  297. int width = region.Width;
  298. int height = region.Height;
  299. int b;
  300. int fwidth = width - padding * 2;
  301. int fheight = height - 1 - padding;
  302. Move (region.X, region.Y);
  303. if (padding > 0) {
  304. for (int l = 0; l < padding; l++)
  305. for (b = 0; b < width; b++)
  306. AddRune (' ');
  307. }
  308. Move (region.X, region.Y + padding);
  309. for (int c = 0; c < padding; c++)
  310. AddRune (' ');
  311. AddRune (ULCorner);
  312. for (b = 0; b < fwidth - 2; b++)
  313. AddRune (HLine);
  314. AddRune (URCorner);
  315. for (int c = 0; c < padding; c++)
  316. AddRune (' ');
  317. for (b = 1 + padding; b < fheight; b++) {
  318. Move (region.X, region.Y + b);
  319. for (int c = 0; c < padding; c++)
  320. AddRune (' ');
  321. AddRune (VLine);
  322. if (fill) {
  323. for (int x = 1; x < fwidth - 1; x++)
  324. AddRune (' ');
  325. } else
  326. Move (region.X + fwidth - 1, region.Y + b);
  327. AddRune (VLine);
  328. for (int c = 0; c < padding; c++)
  329. AddRune (' ');
  330. }
  331. Move (region.X, region.Y + fheight);
  332. for (int c = 0; c < padding; c++)
  333. AddRune (' ');
  334. AddRune (LLCorner);
  335. for (b = 0; b < fwidth - 2; b++)
  336. AddRune (HLine);
  337. AddRune (LRCorner);
  338. for (int c = 0; c < padding; c++)
  339. AddRune (' ');
  340. if (padding > 0) {
  341. Move (region.X, region.Y + height - padding);
  342. for (int l = 0; l < padding; l++)
  343. for (b = 0; b < width; b++)
  344. AddRune (' ');
  345. }
  346. }
  347. /// <summary>
  348. /// Suspend the application, typically needs to save the state, suspend the app and upon return, reset the console driver.
  349. /// </summary>
  350. public abstract void Suspend ();
  351. Rect clip;
  352. /// <summary>
  353. /// Controls the current clipping region that AddRune/AddStr is subject to.
  354. /// </summary>
  355. /// <value>The clip.</value>
  356. public Rect Clip {
  357. get => clip;
  358. set => this.clip = value;
  359. }
  360. public abstract void StartReportingMouseMoves ();
  361. public abstract void StopReportingMouseMoves ();
  362. /// <summary>
  363. /// Disables the cooked event processing from the mouse driver. At startup, it is assumed mouse events are cooked.
  364. /// </summary>
  365. public abstract void UncookMouse ();
  366. /// <summary>
  367. /// Enables the cooked event processing from the mouse driver
  368. /// </summary>
  369. public abstract void CookMouse ();
  370. /// <summary>
  371. /// Horizontal line character.
  372. /// </summary>
  373. public Rune HLine;
  374. /// <summary>
  375. /// Vertical line character.
  376. /// </summary>
  377. public Rune VLine;
  378. /// <summary>
  379. /// Stipple pattern
  380. /// </summary>
  381. public Rune Stipple;
  382. /// <summary>
  383. /// Diamond character
  384. /// </summary>
  385. public Rune Diamond;
  386. /// <summary>
  387. /// Upper left corner
  388. /// </summary>
  389. public Rune ULCorner;
  390. /// <summary>
  391. /// Lower left corner
  392. /// </summary>
  393. public Rune LLCorner;
  394. /// <summary>
  395. /// Upper right corner
  396. /// </summary>
  397. public Rune URCorner;
  398. /// <summary>
  399. /// Lower right corner
  400. /// </summary>
  401. public Rune LRCorner;
  402. /// <summary>
  403. /// Left tee
  404. /// </summary>
  405. public Rune LeftTee;
  406. /// <summary>
  407. /// Right tee
  408. /// </summary>
  409. public Rune RightTee;
  410. /// <summary>
  411. /// Top tee
  412. /// </summary>
  413. public Rune TopTee;
  414. /// <summary>
  415. /// The bottom tee.
  416. /// </summary>
  417. public Rune BottomTee;
  418. public abstract Attribute MakeAttribute (Color fore, Color back);
  419. }
  420. }