ConsoleDriver.cs 27 KB

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