Add beginning of label

This commit is contained in:
Miguel de Icaza
2017-12-15 23:05:02 -05:00
parent cc88cb44e1
commit 2274866c47
4 changed files with 111 additions and 5 deletions

View File

@@ -66,6 +66,8 @@ namespace Terminal {
public void SetNeedsDisplay ()
{
NeedDisplay = true;
if (container != null)
container.SetNeedsDisplay ();
}
/// <summary>
@@ -426,7 +428,7 @@ namespace Terminal {
Driver.AddStr ("Line: " + i);
}
}
#endif
#endif
}
/// <summary>
@@ -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 ();
}
}

View File

@@ -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);
}

View File

@@ -32,18 +32,22 @@
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Application.cs" />
<Compile Include="driver.cs" />
<Compile Include="Core.cs" />
<Compile Include="Driver.cs" />
<Compile Include="Event.cs" />
<Compile Include="Types\Point.cs" />
<Compile Include="Types\Rect.cs" />
<Compile Include="Types\Size.cs" />
<Compile Include="demo.cs" />
<Compile Include="Views\Label.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="mono-curses.dll">
<HintPath>$(MSBuildProjectDirectory)/../mono-curses/mono-curses.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="Views\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

98
Views/Label.cs Normal file
View File

@@ -0,0 +1,98 @@

using System;
namespace Terminal {
public enum TextAlignment {
Left, Right, Centered, Justified
}
/// <summary>
/// Label widget, displays a string at a given position, can include multiple lines.
/// </summary>
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);
}
/// <summary>
/// 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.
/// </summary>
public Label (int x, int y, string text) : this (CalcRect (x, y, text), text)
{
}
/// <summary>
/// Public constructor: creates a label at the given
/// coordinate with the given string and uses the specified
/// frame for the string.
/// </summary>
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);
}
/// <summary>
/// The text displayed by this widget.
/// </summary>
public virtual string Text {
get => text;
set {
text = value;
SetNeedsDisplay ();
}
}
public TextAlignment TextAlignment {
get => textAlignment;
set {
textAlignment = value;
SetNeedsDisplay ();
}
}
/// <summary>
/// The color used for the label
/// </summary>
Color textColor = -1;
public Color TextColor {
get => textColor;
set {
textColor = value;
SetNeedsDisplay ();
}
}
}
}