Browse Source

Make Home key work in multi-line text boxes.

pull/10/head
Steven Kirk 12 years ago
parent
commit
050e9cc6e8
  1. 5
      Cairo/Perspex.Cairo/Media/FormattedTextImpl.cs
  2. 43
      Perspex.Controls/TextBox.cs
  3. 5
      Perspex.Controls/TextBoxView.cs
  4. 5
      Perspex.SceneGraph/Media/FormattedText.cs
  5. 21
      Perspex.SceneGraph/Media/FormattedTextLine.cs
  6. 1
      Perspex.SceneGraph/Perspex.SceneGraph.csproj
  7. 2
      Perspex.SceneGraph/Platform/IFormattedTextImpl.cs
  8. 6
      Windows/Perspex.Direct2D1/Media/FormattedTextImpl.cs

5
Cairo/Perspex.Cairo/Media/FormattedTextImpl.cs

@ -56,6 +56,11 @@ namespace Perspex.Cairo.Media
this.Layout.Dispose();
}
public IEnumerable<FormattedTextLine> GetLines()
{
return new FormattedTextLine[0];
}
public TextHitTestResult HitTestPoint(Point point)
{
int textPosition;

43
Perspex.Controls/TextBox.cs

@ -137,6 +137,44 @@ namespace Perspex.Controls
}
}
private void MoveHome(ModifierKeys modifiers)
{
var caretIndex = this.CaretIndex;
if ((modifiers & ModifierKeys.Control) != 0)
{
caretIndex = 0;
}
else
{
var lines = this.textBoxView.FormattedText.GetLines();
var pos = 0;
foreach (var line in lines)
{
if (pos + line.Length > caretIndex)
{
break;
}
pos += line.Length;
}
caretIndex = pos;
}
this.CaretIndex = caretIndex;
if ((modifiers & ModifierKeys.Shift) != 0)
{
this.SelectionEnd = caretIndex;
}
else
{
this.SelectionStart = this.SelectionEnd = caretIndex;
}
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
string text = this.Text ?? string.Empty;
@ -162,7 +200,7 @@ namespace Perspex.Controls
break;
case Key.Home:
this.CaretIndex = 0;
this.MoveHome(e.Device.Modifiers);
break;
case Key.End:
@ -257,7 +295,8 @@ namespace Perspex.Controls
private void OnPointerPressed(object sender, PointerEventArgs e)
{
var point = e.GetPosition(this.textBoxView);
this.CaretIndex = this.textBoxView.GetCaretIndex(point);
this.CaretIndex = this.SelectionStart = this.SelectionEnd =
this.textBoxView.GetCaretIndex(point);
}
}
}

5
Perspex.Controls/TextBoxView.cs

@ -36,6 +36,11 @@ namespace Perspex.Controls
parent.GetObservable(TextBox.CaretIndexProperty).Subscribe(_ => this.CaretMoved());
}
public new FormattedText FormattedText
{
get { return base.FormattedText; }
}
public int GetCaretIndex(Point point)
{
var hit = this.FormattedText.HitTestPoint(point);

5
Perspex.SceneGraph/Media/FormattedText.cs

@ -69,6 +69,11 @@ namespace Perspex.Media
this.PlatformImpl.Dispose();
}
public IEnumerable<FormattedTextLine> GetLines()
{
return this.PlatformImpl.GetLines();
}
public TextHitTestResult HitTestPoint(Point point)
{
return this.PlatformImpl.HitTestPoint(point);

21
Perspex.SceneGraph/Media/FormattedTextLine.cs

@ -0,0 +1,21 @@
// -----------------------------------------------------------------------
// <copyright file="FormattedTextLine.cs" company="Steven Kirk">
// Copyright 2013 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Media
{
public class FormattedTextLine
{
public FormattedTextLine(int length, double height)
{
this.Length = length;
this.Height = height;
}
public int Length { get; private set; }
public double Height { get; private set; }
}
}

1
Perspex.SceneGraph/Perspex.SceneGraph.csproj

@ -46,6 +46,7 @@
<Compile Include="Media\Color.cs" />
<Compile Include="Media\Colors.cs" />
<Compile Include="Media\FontStyle.cs" />
<Compile Include="Media\FormattedTextLine.cs" />
<Compile Include="Media\FormattedText.cs" />
<Compile Include="Media\Geometry.cs" />
<Compile Include="Media\IDrawingContext.cs" />

2
Perspex.SceneGraph/Platform/IFormattedTextImpl.cs

@ -14,6 +14,8 @@ namespace Perspex.Platform
{
Size Constraint { get; set; }
IEnumerable<FormattedTextLine> GetLines();
TextHitTestResult HitTestPoint(Point point);
Rect HitTestTextPosition(int index);

6
Windows/Perspex.Direct2D1/Media/FormattedTextImpl.cs

@ -57,6 +57,12 @@ namespace Perspex.Direct2D1.Media
this.TextLayout.Dispose();
}
public IEnumerable<FormattedTextLine> GetLines()
{
var result = this.TextLayout.GetLineMetrics();
return from line in result select new FormattedTextLine(line.Length, line.Height);
}
public TextHitTestResult HitTestPoint(Point point)
{
SharpDX.Bool isTrailingHit;

Loading…
Cancel
Save