ConsoleDriver.cs 11 KB

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