//
// Driver.cs: Definition for the Console Driver API
//
// Authors:
// Miguel de Icaza (miguel@gnome.org)
//
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 {
///
/// Basic colors that can be used to set the foreground and background colors in console applications. These can only be
///
public enum Color {
///
/// The black color.
///
Black,
///
/// The blue color.
///
Blue,
///
/// The green color.
///
Green,
///
/// The cyan color.
///
Cyan,
///
/// The red color.
///
Red,
///
/// The magenta color.
///
Magenta,
///
/// The brown color.
///
Brown,
///
/// The gray color.
///
Gray,
///
/// The dark gray color.
///
DarkGray,
///
/// The bright bBlue color.
///
BrightBlue,
///
/// The bright green color.
///
BrightGreen,
///
/// The brigh cyan color.
///
BrighCyan,
///
/// The bright red color.
///
BrightRed,
///
/// The bright magenta color.
///
BrightMagenta,
///
/// The bright yellow color.
///
BrightYellow,
///
/// The White color.
///
White
}
///
/// Attributes are used as elements that contain both a foreground and a background or platform specific features
///
///
/// 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.
///
public struct Attribute {
internal int value;
internal Color foreground;
internal Color background;
///
/// Initializes a new instance of the struct.
///
/// Value.
/// Foreground
/// Background
public Attribute (int value, Color foreground = new Color(), Color background = new Color())
{
this.value = value;
this.foreground = foreground;
this.background = background;
}
///
/// Initializes a new instance of the struct.
///
/// Foreground
/// Background
public Attribute (Color foreground = new Color (), Color background = new Color ())
{
this.value = value = ((int)foreground | (int)background << 4);
this.foreground = foreground;
this.background = background;
}
///
/// Implicit conversion from an attribute to the underlying Int32 representation
///
/// The integer value stored in the attribute.
/// The attribute to convert
public static implicit operator int (Attribute c) => c.value;
///
/// Implicitly convert an integer value into an attribute
///
/// An attribute with the specified integer value.
/// value
public static implicit operator Attribute (int v) => new Attribute (v);
///
/// Creates an attribute from the specified foreground and background.
///
/// The make.
/// Foreground color to use.
/// Background color to use.
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);
}
}
///
/// 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.
///
public class ColorScheme {
Attribute _normal;
Attribute _focus;
Attribute _hotNormal;
Attribute _hotFocus;
Attribute _disabled;
internal string caller = "";
///
/// The default color for text, when the view is not focused.
///
public Attribute Normal { get { return _normal; } set { _normal = SetAttribute (value); } }
///
/// The color for text when the view has the focus.
///
public Attribute Focus { get { return _focus; } set { _focus = SetAttribute (value); } }
///
/// The color for the hotkey when a view is not focused
///
public Attribute HotNormal { get { return _hotNormal; } set { _hotNormal = SetAttribute (value); } }
///
/// The color for the hotkey when the view is focused.
///
public Attribute HotFocus { get { return _hotFocus; } set { _hotFocus = SetAttribute (value); } }
///
/// The default color for text, when the view is disabled.
///
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;
}
}
///
/// The default ColorSchemes for the application.
///
public static class Colors {
static ColorScheme _toplevel;
static ColorScheme _base;
static ColorScheme _dialog;
static ColorScheme _menu;
static ColorScheme _error;
///
/// The application toplevel color scheme, for the default toplevel views.
///
public static ColorScheme TopLevel { get { return _toplevel; } set { _toplevel = SetColorScheme (value); } }
///
/// The base color scheme, for the default toplevel views.
///
public static ColorScheme Base { get { return _base; } set { _base = SetColorScheme (value); } }
///
/// The dialog color scheme, for standard popup dialog boxes
///
public static ColorScheme Dialog { get { return _dialog; } set { _dialog = SetColorScheme (value); } }
///
/// The menu bar color
///
public static ColorScheme Menu { get { return _menu; } set { _menu = SetColorScheme (value); } }
///
/// The color scheme for showing errors.
///
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;
}
}
///
/// Special characters that can be drawn with Driver.AddSpecial.
///
public enum SpecialChar {
///
/// Horizontal line character.
///
HLine,
///
/// Vertical line character.
///
VLine,
///
/// Stipple pattern
///
Stipple,
///
/// Diamond character
///
Diamond,
///
/// Upper left corner
///
ULCorner,
///
/// Lower left corner
///
LLCorner,
///
/// Upper right corner
///
URCorner,
///
/// Lower right corner
///
LRCorner,
///
/// Left tee
///
LeftTee,
///
/// Right tee
///
RightTee,
///
/// Top tee
///
TopTee,
///
/// The bottom tee.
///
BottomTee,
}
///
/// 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.
///
public abstract class ConsoleDriver {
///
/// The handler fired when the terminal is resized.
///
protected Action TerminalResized;
///
/// The current number of columns in the terminal.
///
public abstract int Cols { get; }
///
/// The current number of rows in the terminal.
///
public abstract int Rows { get; }
///
/// Initializes the driver
///
/// Method to invoke when the terminal is resized.
public abstract void Init (Action terminalResized);
///
/// Moves the cursor to the specified column and row.
///
/// Column to move the cursor to.
/// Row to move the cursor to.
public abstract void Move (int col, int row);
///
/// Adds the specified rune to the display at the current cursor position
///
/// Rune to add.
public abstract void AddRune (Rune rune);
///
/// Adds the specified
///
/// String.
public abstract void AddStr (ustring str);
///
/// Prepare the driver and set the key and mouse events handlers.
///
/// The main loop.
/// The handler for ProcessKey
/// The handler for key down events
/// The handler for key up events
/// The handler for mouse events
public abstract void PrepareToRun (MainLoop mainLoop, Action keyHandler, Action keyDownHandler, Action keyUpHandler, Action mouseHandler);
///
/// Updates the screen to reflect all the changes that have been done to the display buffer
///
public abstract void Refresh ();
///
/// Updates the location of the cursor position
///
public abstract void UpdateCursor ();
///
/// Ends the execution of the console driver.
///
public abstract void End ();
///
/// Redraws the physical screen with the contents that have been queued up via any of the printing commands.
///
public abstract void UpdateScreen ();
///
/// Selects the specified attribute as the attribute to use for future calls to AddRune, AddString.
///
/// C.
public abstract void SetAttribute (Attribute c);
///
/// Set Colors from limit sets of colors.
///
/// Foreground.
/// Background.
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.
///
/// Advanced uses - set colors to any pre-set pairs, you would need to init_color
/// that independently with the R, G, B values.
///
/// Foreground color identifier.
/// Background color identifier.
public abstract void SetColors (short foregroundColorId, short backgroundColorId);
///
/// Set the handler when the terminal is resized.
///
///
public void SetTerminalResized(Action terminalResized)
{
TerminalResized = terminalResized;
}
///
/// Draws a frame on the specified region with the specified padding around the frame.
///
/// Region where the frame will be drawn..
/// Padding to add on the sides.
/// If set to true it will clear the contents with the current color, otherwise the contents will be left untouched.
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);
}
}
}
}
///
/// Suspend the application, typically needs to save the state, suspend the app and upon return, reset the console driver.
///
public abstract void Suspend ();
Rect clip;
///
/// Controls the current clipping region that AddRune/AddStr is subject to.
///
/// The clip.
public Rect Clip {
get => clip;
set => this.clip = value;
}
///
/// Start of mouse moves.
///
public abstract void StartReportingMouseMoves ();
///
/// Stop reporting mouses moves.
///
public abstract void StopReportingMouseMoves ();
///
/// Disables the cooked event processing from the mouse driver. At startup, it is assumed mouse events are cooked.
///
public abstract void UncookMouse ();
///
/// Enables the cooked event processing from the mouse driver
///
public abstract void CookMouse ();
///
/// Horizontal line character.
///
public Rune HLine;
///
/// Vertical line character.
///
public Rune VLine;
///
/// Stipple pattern
///
public Rune Stipple;
///
/// Diamond character
///
public Rune Diamond;
///
/// Upper left corner
///
public Rune ULCorner;
///
/// Lower left corner
///
public Rune LLCorner;
///
/// Upper right corner
///
public Rune URCorner;
///
/// Lower right corner
///
public Rune LRCorner;
///
/// Left tee
///
public Rune LeftTee;
///
/// Right tee
///
public Rune RightTee;
///
/// Top tee
///
public Rune TopTee;
///
/// The bottom tee.
///
public Rune BottomTee;
///
/// Make the attribute for the foreground and background colors.
///
/// Foreground.
/// Background.
///
public abstract Attribute MakeAttribute (Color fore, Color back);
}
}