From 2274866c47d98e2f7b164cf13d996228289106a4 Mon Sep 17 00:00:00 2001 From: Miguel de Icaza Date: Fri, 15 Dec 2017 23:05:02 -0500 Subject: [PATCH] Add beginning of label --- Application.cs => Core.cs | 9 ++-- driver.cs => Driver.cs | 1 + Terminal.csproj | 8 +++- Views/Label.cs | 98 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 5 deletions(-) rename Application.cs => Core.cs (98%) rename driver.cs => Driver.cs (98%) create mode 100644 Views/Label.cs diff --git a/Application.cs b/Core.cs similarity index 98% rename from Application.cs rename to Core.cs index dd8bf75b9..2d92c0cff 100644 --- a/Application.cs +++ b/Core.cs @@ -66,6 +66,8 @@ namespace Terminal { public void SetNeedsDisplay () { NeedDisplay = true; + if (container != null) + container.SetNeedsDisplay (); } /// @@ -426,7 +428,7 @@ namespace Terminal { Driver.AddStr ("Line: " + i); } } -#endif +#endif } /// @@ -630,9 +632,10 @@ namespace Terminal { MainLoop.MainIteration(); if (Iteration != null) Iteration(null, EventArgs.Empty); - } - else if (wait == false) + } else if (wait == false) return; + if (state.Toplevel.NeedDisplay) + state.Toplevel.Redraw (); } } diff --git a/driver.cs b/Driver.cs similarity index 98% rename from driver.cs rename to Driver.cs index 74ec9fabb..d795d5f71 100644 --- a/driver.cs +++ b/Driver.cs @@ -85,6 +85,7 @@ namespace Terminal { public override void AddStr (string str) { + // TODO; optimize this to determine if the str fits in the clip region, and if so, use Curses.addstr directly foreach (var c in str) AddCh ((int) c); } diff --git a/Terminal.csproj b/Terminal.csproj index 13221072c..28bad5a41 100644 --- a/Terminal.csproj +++ b/Terminal.csproj @@ -32,18 +32,22 @@ - - + + + $(MSBuildProjectDirectory)/../mono-curses/mono-curses.dll + + + diff --git a/Views/Label.cs b/Views/Label.cs new file mode 100644 index 000000000..056acd0ed --- /dev/null +++ b/Views/Label.cs @@ -0,0 +1,98 @@ + +using System; + +namespace Terminal { + public enum TextAlignment { + Left, Right, Centered, Justified + } + + /// + /// Label widget, displays a string at a given position, can include multiple lines. + /// + public class Label : View { + string text; + TextAlignment textAlignment; + + static Rect CalcRect (int x, int y, string s) + { + int mw = 0; + int ml = 1; + + int cols = 0; + foreach (var c in s) { + if (c == '\n'){ + ml++; + if (cols > mw) + mw = cols; + cols = 0; + } else + cols++; + } + return new Rect (x, y, cols, ml); + } + + /// + /// Public constructor: creates a label at the given + /// coordinate with the given string, computes the bounding box + /// based on the size of the string, assumes that the string contains + /// newlines for multiple lines, no special breaking rules are used. + /// + public Label (int x, int y, string text) : this (CalcRect (x, y, text), text) + { + } + + /// + /// Public constructor: creates a label at the given + /// coordinate with the given string and uses the specified + /// frame for the string. + /// + public Label (Rect rect, string text) : base (rect) + { + this.text = text; + } + + public override void Redraw () + { + if (TextColor != -1) + Driver.SetColor (TextColor); + else + Driver.SetColor(Colors.Base.Normal); + + Clear (); + Move (Frame.X, Frame.Y); + Driver.AddStr (text); + } + + /// + /// The text displayed by this widget. + /// + public virtual string Text { + get => text; + set { + text = value; + SetNeedsDisplay (); + } + } + + public TextAlignment TextAlignment { + get => textAlignment; + set { + textAlignment = value; + SetNeedsDisplay (); + } + } + + /// + /// The color used for the label + /// + Color textColor = -1; + public Color TextColor { + get => textColor; + set { + textColor = value; + SetNeedsDisplay (); + } + } + } + +}