//
// Driver.cs: Definition for the Console Driver API
//
// Authors:
// Miguel de Icaza (miguel@gnome.org)
//
using System;
using System.Collections.Generic;
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;
///
/// Initializes a new instance of the struct.
///
/// Value.
public Attribute (int value)
{
this.value = value;
}
public static implicit operator int (Attribute c) => c.value;
public static implicit operator Attribute (int v) => new Attribute (v);
}
///
/// 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 {
///
/// The default color for text, when the view is not focused.
///
public Attribute Normal;
///
/// The color for text when the view has the focus.
///
public Attribute Focus;
///
/// The color for the hotkey when a view is not focused
///
public Attribute HotNormal;
///
/// The color for the hotkey when the view is focused.
///
public Attribute HotFocus;
}
///
/// The default ColorSchemes for the application.
///
public static class Colors {
///
/// The base color scheme, for the default toplevel views.
///
public static ColorScheme Base;
///
/// The dialog color scheme, for standard popup dialog boxes
///
public static ColorScheme Dialog;
///
/// The menu bar color
///
public static ColorScheme Menu;
///
/// The color scheme for showing errors.
///
public static ColorScheme Error;
}
///
/// 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 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);
public abstract void PrepareToRun (MainLoop mainLoop, Action keyHandler, 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
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);
///
/// 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 = 0; b < width; b++)
AddRune (' ');
}
Move (region.X, region.Y + padding);
for (int c = 0; c < padding; c++)
AddRune (' ');
AddRune (ULCorner);
for (b = 0; b < fwidth - 2; b++)
AddRune (HLine);
AddRune (URCorner);
for (int c = 0; c < padding; c++)
AddRune (' ');
for (b = 1 + padding; b < fheight; b++) {
Move (region.X, region.Y + b);
for (int c = 0; c < padding; c++)
AddRune (' ');
AddRune (VLine);
if (fill) {
for (int x = 1; x < fwidth - 1; x++)
AddRune (' ');
} else
Move (region.X + fwidth - 1, region.Y + b);
AddRune (VLine);
for (int c = 0; c < padding; c++)
AddRune (' ');
}
Move (region.X, region.Y + fheight);
for (int c = 0; c < padding; c++)
AddRune (' ');
AddRune (LLCorner);
for (b = 0; b < fwidth - 2; b++)
AddRune (HLine);
AddRune (LRCorner);
for (int c = 0; c < padding; c++)
AddRune (' ');
if (padding > 0) {
Move (region.X, region.Y + height - padding);
for (int l = 0; l < padding; l++)
for (b = 0; b < width; b++)
AddRune (' ');
}
}
///
/// 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;
}
public abstract void StartReportingMouseMoves ();
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;
}
}