Make ScrollView work, still needs scroll bar indicators

This commit is contained in:
Miguel de Icaza
2018-01-25 22:22:40 -05:00
parent 6025594f61
commit 8ef6c140c5
4 changed files with 89 additions and 6 deletions

View File

@@ -150,6 +150,13 @@ namespace Terminal.Gui {
/// Using ColorSchemes has the advantage that your application will work both
/// in color as well as black and white displays.
/// </para>
/// <para>
/// Views that are focusable should implement the PositionCursor to make sure that
/// the cursor is placed in a location that makes sense. Unix terminals do not have
/// a way of hiding the cursor, so it can be distracting to have the cursor left at
/// the last focused view. So views should make sure that they place the cursor
/// in a visually sensible place.
/// </para>
/// </remarks>
public class View : Responder, IEnumerable {
View container = null;
@@ -441,6 +448,28 @@ namespace Terminal.Gui {
return new Rect (x, y, w, h);
}
/// <summary>
/// Sets the Console driver's clip region to the current View's Bounds.
/// </summary>
/// <returns>The existing driver's Clip region, which can be then set by setting the Driver.Clip property.</returns>
public Rect ClipToBounds ()
{
return SetClip (Bounds);
}
/// <summary>
/// Sets the clipping region to the specified region, the region is view-relative
/// </summary>
/// <returns>The previous clip region.</returns>
/// <param name="rect">Rectangle region to clip into, the region is view-relative.</param>
public Rect SetClip (Rect rect)
{
var bscreen = RectToScreen (rect);
var previous = Driver.Clip;
Driver.Clip = ScreenClip (RectToScreen (Bounds));
return previous;
}
/// <summary>
/// Draws a frame in the current view, clipped by the boundary of this view
/// </summary>
@@ -592,8 +621,12 @@ namespace Terminal.Gui {
/// <summary>
/// Performs a redraw of this view and its subviews, only redraws the views that have been flagged for a re-display.
/// </summary>
/// <param name="region">The region to redraw, this is relative to the view itself.</param>
/// <remarks>
/// The region argument is relative to the view itself.
/// <para>
/// Views should set the color that they want to use on entry, as otherwise this will inherit
/// the last color that was set globaly on the driver.
/// </para>
/// </remarks>
public virtual void Redraw (Rect region)
{

View File

@@ -276,7 +276,7 @@ namespace Terminal.Gui {
}
}
static bool sync = false;
static bool sync = true;
public override void AddRune (Rune rune)
{
if (Clip.Contains (ccol, crow)) {

View File

@@ -14,11 +14,15 @@ namespace Terminal.Gui {
/// </para>
/// </remarks>
public class ScrollView : View {
View contentView;
public ScrollView (Rect frame) : base (frame)
{
contentView = new View (frame);
base.Add (contentView);
}
Rect contentSize;
Size contentSize;
Point contentOffset;
bool showHorizontalScrollIndicator;
bool showVerticalScrollIndicator;
@@ -27,12 +31,13 @@ namespace Terminal.Gui {
/// Represents the contents of the data shown inside the scrolview
/// </summary>
/// <value>The size of the content.</value>
public Rect ContentSize {
public Size ContentSize {
get {
return contentSize;
}
set {
contentSize = value;
contentView.Frame = new Rect (contentOffset, value);
}
}
@@ -45,10 +50,20 @@ namespace Terminal.Gui {
return contentOffset;
}
set {
contentOffset = value;
contentOffset = new Point (-value.X, -value.Y);
contentView.Frame = new Rect (contentOffset, contentSize);
}
}
/// <summary>
/// Adds the view to the scrollview.
/// </summary>
/// <param name="view">The view to add to the scrollview.</param>
public override void Add (View view)
{
contentView.Add (view);
}
/// <summary>
/// Gets or sets the visibility for the horizontal scroll indicator.
/// </summary>
@@ -81,7 +96,12 @@ namespace Terminal.Gui {
public override void Redraw(Rect region)
{
var oldClip = ClipToBounds ();
base.Redraw(region);
Driver.SetAttribute (ColorScheme.Normal);
DrawFrame (Bounds);
Driver.Clip = oldClip;
}
}
}

32
demo.cs
View File

@@ -1,6 +1,28 @@
using Terminal.Gui;
using System;
class Demo {
class MyView : View {
public MyView (int x, int y) : base (new Rect (x, y, 10, 10))
{
}
public override void Redraw(Rect region)
{
Driver.SetAttribute (ColorScheme.Focus);
for (int y = 0; y < 10; y++) {
Move (0, y);
for (int x = 0; x < 10; x++) {
Driver.AddRune ((Rune)('0' + (x+y)%10));
}
}
}
}
static void ShowTextAlignments (View container)
{
container.Add (
@@ -12,6 +34,13 @@ class Demo {
static void ShowEntries (View container)
{
var scrollView = new ScrollView (new Rect (50, 10, 20, 8)) {
ContentSize = new Size (100, 100),
ContentOffset = new Point (-1, -1)
};
scrollView.Add (new MyView (0, 0));
container.Add (
new Label (3, 6, "Login: "),
new TextField (14, 6, 40, ""),
@@ -21,6 +50,7 @@ class Demo {
new CheckBox (1, 0, "Remember me"),
new RadioGroup (1, 2, new [] { "_Personal", "_Company" }),
},
scrollView,
new Button (3, 19, "Ok"),
new Button (10, 19, "Cancel"),
new Label (3, 22, "Press ESC and 9 to activate the menubar")
@@ -75,7 +105,7 @@ class Demo {
ShowEntries (win);
int count = 0;
ml = new Label (new Rect (3, 17, 50, 1), "Mouse: ");
ml = new Label (new Rect (3, 17, 47, 1), "Mouse: ");
Application.RootMouseEvent += delegate (MouseEvent me) {
ml.Text = $"Mouse: ({me.X},{me.Y}) - {me.Flags} {count++}";