123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720 |
- //
- // Driver.cs: Definition for the Console Driver API
- //
- // Authors:
- // Miguel de Icaza ([email protected])
- //
- using System;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using Mono.Terminal;
- using NStack;
- using Unix.Terminal;
- namespace Terminal.Gui {
- /// <summary>
- /// Basic colors that can be used to set the foreground and background colors in console applications. These can only be
- /// </summary>
- public enum Color {
- /// <summary>
- /// The black color.
- /// </summary>
- Black,
- /// <summary>
- /// The blue color.
- /// </summary>
- Blue,
- /// <summary>
- /// The green color.
- /// </summary>
- Green,
- /// <summary>
- /// The cyan color.
- /// </summary>
- Cyan,
- /// <summary>
- /// The red color.
- /// </summary>
- Red,
- /// <summary>
- /// The magenta color.
- /// </summary>
- Magenta,
- /// <summary>
- /// The brown color.
- /// </summary>
- Brown,
- /// <summary>
- /// The gray color.
- /// </summary>
- Gray,
- /// <summary>
- /// The dark gray color.
- /// </summary>
- DarkGray,
- /// <summary>
- /// The bright bBlue color.
- /// </summary>
- BrightBlue,
- /// <summary>
- /// The bright green color.
- /// </summary>
- BrightGreen,
- /// <summary>
- /// The brigh cyan color.
- /// </summary>
- BrighCyan,
- /// <summary>
- /// The bright red color.
- /// </summary>
- BrightRed,
- /// <summary>
- /// The bright magenta color.
- /// </summary>
- BrightMagenta,
- /// <summary>
- /// The bright yellow color.
- /// </summary>
- BrightYellow,
- /// <summary>
- /// The White color.
- /// </summary>
- White
- }
- /// <summary>
- /// Attributes are used as elements that contain both a foreground and a background or platform specific features
- /// </summary>
- /// <remarks>
- /// Attributes are needed to map colors to terminal capabilities that might lack colors, on color
- /// scenarios, they encode both the foreground and the background color and are used in the ColorScheme
- /// class to define color schemes that can be used in your application.
- /// </remarks>
- public struct Attribute {
- internal int value;
- internal Color foreground;
- internal Color background;
- /// <summary>
- /// Initializes a new instance of the <see cref="T:Terminal.Gui.Attribute"/> struct.
- /// </summary>
- /// <param name="value">Value.</param>
- /// <param name="foreground">Foreground</param>
- /// <param name="background">Background</param>
- public Attribute (int value, Color foreground = new Color(), Color background = new Color())
- {
- this.value = value;
- this.foreground = foreground;
- this.background = background;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="T:Terminal.Gui.Attribute"/> struct.
- /// </summary>
- /// <param name="foreground">Foreground</param>
- /// <param name="background">Background</param>
- public Attribute (Color foreground = new Color (), Color background = new Color ())
- {
- this.value = value = ((int)foreground | (int)background << 4);
- this.foreground = foreground;
- this.background = background;
- }
- /// <summary>
- /// Implicit conversion from an attribute to the underlying Int32 representation
- /// </summary>
- /// <returns>The integer value stored in the attribute.</returns>
- /// <param name="c">The attribute to convert</param>
- public static implicit operator int (Attribute c) => c.value;
- /// <summary>
- /// Implicitly convert an integer value into an attribute
- /// </summary>
- /// <returns>An attribute with the specified integer value.</returns>
- /// <param name="v">value</param>
- public static implicit operator Attribute (int v) => new Attribute (v);
- /// <summary>
- /// Creates an attribute from the specified foreground and background.
- /// </summary>
- /// <returns>The make.</returns>
- /// <param name="foreground">Foreground color to use.</param>
- /// <param name="background">Background color to use.</param>
- public static Attribute Make (Color foreground, Color background)
- {
- if (Application.Driver == null)
- throw new InvalidOperationException ("The Application has not been initialized");
- return Application.Driver.MakeAttribute (foreground, background);
- }
- }
- /// <summary>
- /// Color scheme definitions, they cover some common scenarios and are used
- /// typically in toplevel containers to set the scheme that is used by all the
- /// views contained inside.
- /// </summary>
- public class ColorScheme {
- Attribute _normal;
- Attribute _focus;
- Attribute _hotNormal;
- Attribute _hotFocus;
- Attribute _disabled;
- internal string caller = "";
- /// <summary>
- /// The default color for text, when the view is not focused.
- /// </summary>
- public Attribute Normal { get { return _normal; } set { _normal = SetAttribute (value); } }
- /// <summary>
- /// The color for text when the view has the focus.
- /// </summary>
- public Attribute Focus { get { return _focus; } set { _focus = SetAttribute (value); } }
- /// <summary>
- /// The color for the hotkey when a view is not focused
- /// </summary>
- public Attribute HotNormal { get { return _hotNormal; } set { _hotNormal = SetAttribute (value); } }
- /// <summary>
- /// The color for the hotkey when the view is focused.
- /// </summary>
- public Attribute HotFocus { get { return _hotFocus; } set { _hotFocus = SetAttribute (value); } }
- /// <summary>
- /// The default color for text, when the view is disabled.
- /// </summary>
- public Attribute Disabled { get { return _disabled; } set { _disabled = SetAttribute (value); } }
- bool preparingScheme = false;
- Attribute SetAttribute (Attribute attribute, [CallerMemberName]string callerMemberName = null)
- {
- if (!Application._initialized && !preparingScheme)
- return attribute;
- if (preparingScheme)
- return attribute;
- preparingScheme = true;
- switch (caller) {
- case "TopLevel":
- switch (callerMemberName) {
- case "Normal":
- HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
- break;
- case "Focus":
- HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
- break;
- case "HotNormal":
- HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
- break;
- case "HotFocus":
- HotNormal = Application.Driver.MakeAttribute (attribute.foreground, HotNormal.background);
- if (Focus.foreground != attribute.background)
- Focus = Application.Driver.MakeAttribute (Focus.foreground, attribute.background);
- break;
- }
- break;
- case "Base":
- switch (callerMemberName) {
- case "Normal":
- HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
- break;
- case "Focus":
- HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
- break;
- case "HotNormal":
- HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
- Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
- break;
- case "HotFocus":
- HotNormal = Application.Driver.MakeAttribute (attribute.foreground, HotNormal.background);
- if (Focus.foreground != attribute.background)
- Focus = Application.Driver.MakeAttribute (Focus.foreground, attribute.background);
- break;
- }
- break;
- case "Menu":
- switch (callerMemberName) {
- case "Normal":
- if (Focus.background != attribute.background)
- Focus = Application.Driver.MakeAttribute (attribute.foreground, Focus.background);
- HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
- Disabled = Application.Driver.MakeAttribute (Disabled.foreground, attribute.background);
- break;
- case "Focus":
- Normal = Application.Driver.MakeAttribute (attribute.foreground, Normal.background);
- HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
- break;
- case "HotNormal":
- if (Focus.background != attribute.background)
- HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
- Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
- Disabled = Application.Driver.MakeAttribute (Disabled.foreground, attribute.background);
- break;
- case "HotFocus":
- HotNormal = Application.Driver.MakeAttribute (attribute.foreground, HotNormal.background);
- if (Focus.foreground != attribute.background)
- Focus = Application.Driver.MakeAttribute (Focus.foreground, attribute.background);
- break;
- case "Disabled":
- if (Focus.background != attribute.background)
- HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
- Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
- HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
- break;
- }
- break;
- case "Dialog":
- switch (callerMemberName) {
- case "Normal":
- if (Focus.background != attribute.background)
- Focus = Application.Driver.MakeAttribute (attribute.foreground, Focus.background);
- HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
- break;
- case "Focus":
- Normal = Application.Driver.MakeAttribute (attribute.foreground, Normal.background);
- HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
- break;
- case "HotNormal":
- if (Focus.background != attribute.background)
- HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
- if (Normal.foreground != attribute.background)
- Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
- break;
- case "HotFocus":
- HotNormal = Application.Driver.MakeAttribute (attribute.foreground, HotNormal.background);
- if (Focus.foreground != attribute.background)
- Focus = Application.Driver.MakeAttribute (Focus.foreground, attribute.background);
- break;
- }
- break;
- case "Error":
- switch (callerMemberName) {
- case "Normal":
- HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
- HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
- break;
- case "HotNormal":
- case "HotFocus":
- HotFocus = Application.Driver.MakeAttribute (attribute.foreground, attribute.background);
- Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
- break;
- }
- break;
- }
- preparingScheme = false;
- return attribute;
- }
- }
- /// <summary>
- /// The default ColorSchemes for the application.
- /// </summary>
- public static class Colors {
- static ColorScheme _toplevel;
- static ColorScheme _base;
- static ColorScheme _dialog;
- static ColorScheme _menu;
- static ColorScheme _error;
- /// <summary>
- /// The application toplevel color scheme, for the default toplevel views.
- /// </summary>
- public static ColorScheme TopLevel { get { return _toplevel; } set { _toplevel = SetColorScheme (value); } }
- /// <summary>
- /// The base color scheme, for the default toplevel views.
- /// </summary>
- public static ColorScheme Base { get { return _base; } set { _base = SetColorScheme (value); } }
- /// <summary>
- /// The dialog color scheme, for standard popup dialog boxes
- /// </summary>
- public static ColorScheme Dialog { get { return _dialog; } set { _dialog = SetColorScheme (value); } }
- /// <summary>
- /// The menu bar color
- /// </summary>
- public static ColorScheme Menu { get { return _menu; } set { _menu = SetColorScheme (value); } }
- /// <summary>
- /// The color scheme for showing errors.
- /// </summary>
- public static ColorScheme Error { get { return _error; } set { _error = SetColorScheme (value); } }
- static ColorScheme SetColorScheme (ColorScheme colorScheme, [CallerMemberName]string callerMemberName = null)
- {
- colorScheme.caller = callerMemberName;
- return colorScheme;
- }
- }
- /// <summary>
- /// Special characters that can be drawn with Driver.AddSpecial.
- /// </summary>
- public enum SpecialChar {
- /// <summary>
- /// Horizontal line character.
- /// </summary>
- HLine,
- /// <summary>
- /// Vertical line character.
- /// </summary>
- VLine,
- /// <summary>
- /// Stipple pattern
- /// </summary>
- Stipple,
- /// <summary>
- /// Diamond character
- /// </summary>
- Diamond,
- /// <summary>
- /// Upper left corner
- /// </summary>
- ULCorner,
- /// <summary>
- /// Lower left corner
- /// </summary>
- LLCorner,
- /// <summary>
- /// Upper right corner
- /// </summary>
- URCorner,
- /// <summary>
- /// Lower right corner
- /// </summary>
- LRCorner,
- /// <summary>
- /// Left tee
- /// </summary>
- LeftTee,
- /// <summary>
- /// Right tee
- /// </summary>
- RightTee,
- /// <summary>
- /// Top tee
- /// </summary>
- TopTee,
- /// <summary>
- /// The bottom tee.
- /// </summary>
- BottomTee,
- }
- /// <summary>
- /// 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.
- /// </summary>
- public abstract class ConsoleDriver {
- /// <summary>
- /// The handler fired when the terminal is resized.
- /// </summary>
- protected Action TerminalResized;
- /// <summary>
- /// The current number of columns in the terminal.
- /// </summary>
- public abstract int Cols { get; }
- /// <summary>
- /// The current number of rows in the terminal.
- /// </summary>
- public abstract int Rows { get; }
- /// <summary>
- /// Initializes the driver
- /// </summary>
- /// <param name="terminalResized">Method to invoke when the terminal is resized.</param>
- public abstract void Init (Action terminalResized);
- /// <summary>
- /// Moves the cursor to the specified column and row.
- /// </summary>
- /// <param name="col">Column to move the cursor to.</param>
- /// <param name="row">Row to move the cursor to.</param>
- public abstract void Move (int col, int row);
- /// <summary>
- /// Adds the specified rune to the display at the current cursor position
- /// </summary>
- /// <param name="rune">Rune to add.</param>
- public abstract void AddRune (Rune rune);
- /// <summary>
- /// Adds the specified
- /// </summary>
- /// <param name="str">String.</param>
- public abstract void AddStr (ustring str);
- /// <summary>
- /// Prepare the driver and set the key and mouse events handlers.
- /// </summary>
- /// <param name="mainLoop">The main loop.</param>
- /// <param name="keyHandler">The handler for ProcessKey</param>
- /// <param name="keyDownHandler">The handler for key down events</param>
- /// <param name="keyUpHandler">The handler for key up events</param>
- /// <param name="mouseHandler">The handler for mouse events</param>
- public abstract void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler);
- /// <summary>
- /// Updates the screen to reflect all the changes that have been done to the display buffer
- /// </summary>
- public abstract void Refresh ();
- /// <summary>
- /// Updates the location of the cursor position
- /// </summary>
- public abstract void UpdateCursor ();
- /// <summary>
- /// Ends the execution of the console driver.
- /// </summary>
- public abstract void End ();
- /// <summary>
- /// Redraws the physical screen with the contents that have been queued up via any of the printing commands.
- /// </summary>
- public abstract void UpdateScreen ();
- /// <summary>
- /// Selects the specified attribute as the attribute to use for future calls to AddRune, AddString.
- /// </summary>
- /// <param name="c">C.</param>
- public abstract void SetAttribute (Attribute c);
- /// <summary>
- /// Set Colors from limit sets of colors.
- /// </summary>
- /// <param name="foreground">Foreground.</param>
- /// <param name="background">Background.</param>
- public abstract void SetColors (ConsoleColor foreground, ConsoleColor background);
- // Advanced uses - set colors to any pre-set pairs, you would need to init_color
- // that independently with the R, G, B values.
- /// <summary>
- /// Advanced uses - set colors to any pre-set pairs, you would need to init_color
- /// that independently with the R, G, B values.
- /// </summary>
- /// <param name="foregroundColorId">Foreground color identifier.</param>
- /// <param name="backgroundColorId">Background color identifier.</param>
- public abstract void SetColors (short foregroundColorId, short backgroundColorId);
- /// <summary>
- /// Set the handler when the terminal is resized.
- /// </summary>
- /// <param name="terminalResized"></param>
- public void SetTerminalResized(Action terminalResized)
- {
- TerminalResized = terminalResized;
- }
- /// <summary>
- /// Draws a frame on the specified region with the specified padding around the frame.
- /// </summary>
- /// <param name="region">Region where the frame will be drawn..</param>
- /// <param name="padding">Padding to add on the sides.</param>
- /// <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>
- public virtual void DrawFrame (Rect region, int padding, bool fill)
- {
- int width = region.Width;
- int height = region.Height;
- int b;
- int fwidth = width - padding * 2;
- int fheight = height - 1 - padding;
- Move (region.X, region.Y);
- if (padding > 0) {
- for (int l = 0; l < padding; l++)
- for (b = region.X; b < region.X + width; b++) {
- AddRune (' ');
- Move (b + 1, region.Y);
- }
- }
- Move (region.X, region.Y + padding);
- for (int c = 0; c < padding; c++) {
- AddRune (' ');
- Move (region.X + 1, region.Y + padding);
- }
- AddRune (ULCorner);
- for (b = region.X; b < region.X + fwidth - 2; b++) {
- AddRune (HLine);
- Move (b + (padding > 0 ? padding + 2 : 2), region.Y + padding);
- }
- AddRune (URCorner);
- for (int c = 0; c < padding; c++) {
- AddRune (' ');
- Move (region.X + 1, region.Y + padding);
- }
- for (b = 1 + padding; b < fheight; b++) {
- Move (region.X, region.Y + b);
- for (int c = 0; c < padding; c++) {
- AddRune (' ');
- Move (region.X + 1, region.Y + b);
- }
- AddRune (VLine);
- if (fill) {
- for (int x = region.X + 1; x < region.X + fwidth - 1; x++) {
- AddRune (' ');
- Move (x + (padding > 0 ? padding + 1 : 1), region.Y + b);
- }
- } else {
- if (padding > 0)
- Move (region.X + fwidth, region.Y + b);
- else
- Move (region.X + fwidth - 1, region.Y + b);
- }
- AddRune (VLine);
- for (int c = 0; c < padding; c++) {
- AddRune (' ');
- Move (region.X + 1, region.Y + b);
- }
- }
- Move (region.X, region.Y + fheight);
- for (int c = 0; c < padding; c++) {
- AddRune (' ');
- Move (region.X + 1, region.Y + b);
- }
- AddRune (LLCorner);
- for (b = region.X; b < region.X + fwidth - 2; b++) {
- AddRune (HLine);
- Move (b + (padding > 0 ? padding + 2 : 2), region.Y + fheight);
- }
- AddRune (LRCorner);
- for (int c = 0; c < padding; c++) {
- AddRune (' ');
- Move (region.X + 1, region.Y);
- }
- if (padding > 0) {
- Move (region.X, region.Y + height - padding);
- for (int l = 0; l < padding; l++) {
- for (b = region.X; b < region.X + width; b++) {
- AddRune (' ');
- Move (b + 1, region.Y + height - padding);
- }
- }
- }
- }
- /// <summary>
- /// Suspend the application, typically needs to save the state, suspend the app and upon return, reset the console driver.
- /// </summary>
- public abstract void Suspend ();
- Rect clip;
- /// <summary>
- /// Controls the current clipping region that AddRune/AddStr is subject to.
- /// </summary>
- /// <value>The clip.</value>
- public Rect Clip {
- get => clip;
- set => this.clip = value;
- }
- /// <summary>
- /// Start of mouse moves.
- /// </summary>
- public abstract void StartReportingMouseMoves ();
- /// <summary>
- /// Stop reporting mouses moves.
- /// </summary>
- public abstract void StopReportingMouseMoves ();
- /// <summary>
- /// Disables the cooked event processing from the mouse driver. At startup, it is assumed mouse events are cooked.
- /// </summary>
- public abstract void UncookMouse ();
- /// <summary>
- /// Enables the cooked event processing from the mouse driver
- /// </summary>
- public abstract void CookMouse ();
- /// <summary>
- /// Horizontal line character.
- /// </summary>
- public Rune HLine;
- /// <summary>
- /// Vertical line character.
- /// </summary>
- public Rune VLine;
- /// <summary>
- /// Stipple pattern
- /// </summary>
- public Rune Stipple;
- /// <summary>
- /// Diamond character
- /// </summary>
- public Rune Diamond;
- /// <summary>
- /// Upper left corner
- /// </summary>
- public Rune ULCorner;
- /// <summary>
- /// Lower left corner
- /// </summary>
- public Rune LLCorner;
- /// <summary>
- /// Upper right corner
- /// </summary>
- public Rune URCorner;
- /// <summary>
- /// Lower right corner
- /// </summary>
- public Rune LRCorner;
- /// <summary>
- /// Left tee
- /// </summary>
- public Rune LeftTee;
- /// <summary>
- /// Right tee
- /// </summary>
- public Rune RightTee;
- /// <summary>
- /// Top tee
- /// </summary>
- public Rune TopTee;
- /// <summary>
- /// The bottom tee.
- /// </summary>
- public Rune BottomTee;
- /// <summary>
- /// Make the attribute for the foreground and background colors.
- /// </summary>
- /// <param name="fore">Foreground.</param>
- /// <param name="back">Background.</param>
- /// <returns></returns>
- public abstract Attribute MakeAttribute (Color fore, Color back);
- }
- }
|