32 changed files with 434 additions and 50 deletions
@ -0,0 +1,69 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="TextBox.cs" company="Steven Kirk">
|
||||
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Controls |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using Perspex.Styling; |
||||
|
|
||||
|
public class TextBox : TemplatedControl |
||||
|
{ |
||||
|
public static readonly PerspexProperty<string> TextProperty = |
||||
|
TextBlock.TextProperty.AddOwner<TextBox>(); |
||||
|
|
||||
|
private int caretIndex; |
||||
|
|
||||
|
private TextBoxView textBoxView; |
||||
|
|
||||
|
public TextBox() |
||||
|
{ |
||||
|
this.GetObservable(TextProperty).Subscribe(_ => this.InvalidateVisual()); |
||||
|
FocusableProperty.OverrideDefaultValue(typeof(TextBox), true); |
||||
|
} |
||||
|
|
||||
|
public int CaretIndex |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
return this.caretIndex; |
||||
|
} |
||||
|
|
||||
|
set |
||||
|
{ |
||||
|
value = Math.Min(Math.Max(value, 0), this.Text.Length); |
||||
|
|
||||
|
if (this.caretIndex != value) |
||||
|
{ |
||||
|
this.caretIndex = value; |
||||
|
this.textBoxView.CaretMoved(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public string Text |
||||
|
{ |
||||
|
get { return this.GetValue(TextProperty); } |
||||
|
set { this.SetValue(TextProperty, value); } |
||||
|
} |
||||
|
|
||||
|
protected override void OnTemplateApplied() |
||||
|
{ |
||||
|
Decorator textContainer = this.GetVisualDescendents() |
||||
|
.OfType<Decorator>() |
||||
|
.FirstOrDefault(x => x.Id == "textContainer"); |
||||
|
|
||||
|
if (textContainer == null) |
||||
|
{ |
||||
|
throw new Exception( |
||||
|
"TextBox template doesn't contain a textContainer " + |
||||
|
"or textContainer is not a Decorator."); |
||||
|
} |
||||
|
|
||||
|
textContainer.Content = this.textBoxView = new TextBoxView(this); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,125 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="TextBoxView.cs" company="Steven Kirk">
|
||||
|
// Copyright 2013 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Controls |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Globalization; |
||||
|
using Perspex.Media; |
||||
|
|
||||
|
internal class TextBoxView : Control |
||||
|
{ |
||||
|
private TextBox parent; |
||||
|
|
||||
|
private FormattedText formattedText; |
||||
|
|
||||
|
//private DispatcherTimer caretTimer;
|
||||
|
|
||||
|
private bool caretBlink; |
||||
|
|
||||
|
public TextBoxView(TextBox parent) |
||||
|
{ |
||||
|
//this.caretTimer = new DispatcherTimer();
|
||||
|
//this.caretTimer.Interval = PlatformInterface.Instance.CaretBlinkTime;
|
||||
|
//this.caretTimer.Tick += this.CaretTimerTick;
|
||||
|
this.parent = parent; |
||||
|
} |
||||
|
|
||||
|
public FormattedText FormattedText |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
if (this.formattedText == null) |
||||
|
{ |
||||
|
this.formattedText = this.CreateFormattedText(); |
||||
|
} |
||||
|
|
||||
|
return this.formattedText; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void GotFocus() |
||||
|
{ |
||||
|
this.caretBlink = true; |
||||
|
//this.caretTimer.Start();
|
||||
|
} |
||||
|
|
||||
|
public void LostFocus() |
||||
|
{ |
||||
|
//this.caretTimer.Stop();
|
||||
|
this.InvalidateVisual(); |
||||
|
} |
||||
|
|
||||
|
public void InvalidateText() |
||||
|
{ |
||||
|
this.formattedText = null; |
||||
|
this.InvalidateMeasure(); |
||||
|
} |
||||
|
|
||||
|
internal void CaretMoved() |
||||
|
{ |
||||
|
//this.caretBlink = true;
|
||||
|
//this.caretTimer.Stop();
|
||||
|
//this.caretTimer.Start();
|
||||
|
this.InvalidateVisual(); |
||||
|
} |
||||
|
|
||||
|
public override void Render(IDrawingContext context) |
||||
|
{ |
||||
|
Rect rect = new Rect(this.ActualSize); |
||||
|
|
||||
|
context.DrawText(Brushes.Black, rect, this.FormattedText); |
||||
|
|
||||
|
//if (this.parent.IsKeyboardFocused)
|
||||
|
//{
|
||||
|
// Point caretPos = this.FormattedText.GetCaretPosition(this.parent.CaretIndex);
|
||||
|
// Brush caretBrush = this.parent.CaretBrush;
|
||||
|
|
||||
|
// if (caretBrush == null)
|
||||
|
// {
|
||||
|
// Color color = Colors.Black;
|
||||
|
// SolidColorBrush background = this.parent.Background as SolidColorBrush;
|
||||
|
|
||||
|
// if (background != null)
|
||||
|
// {
|
||||
|
// color = Color.FromUInt32(0x00ffffffu ^ background.Color.ToUint32());
|
||||
|
// }
|
||||
|
|
||||
|
// caretBrush = new SolidColorBrush(color);
|
||||
|
// }
|
||||
|
|
||||
|
// if (this.caretBlink)
|
||||
|
// {
|
||||
|
// drawingContext.DrawLine(
|
||||
|
// new Pen(caretBrush, 1),
|
||||
|
// caretPos,
|
||||
|
// caretPos + new Vector(0, this.FormattedText.Height));
|
||||
|
// }
|
||||
|
//}
|
||||
|
} |
||||
|
|
||||
|
protected override Size MeasureOverride(Size constraint) |
||||
|
{ |
||||
|
return new Size(this.FormattedText.Size.Width, this.FormattedText.Size.Height); |
||||
|
} |
||||
|
|
||||
|
private FormattedText CreateFormattedText() |
||||
|
{ |
||||
|
return new FormattedText |
||||
|
{ |
||||
|
FontFamilyName = "Segoe UI", |
||||
|
FontSize = this.FontSize, |
||||
|
Text = this.parent.Text, |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
private void CaretTimerTick(object sender, EventArgs e) |
||||
|
{ |
||||
|
this.caretBlink = !this.caretBlink; |
||||
|
this.InvalidateVisual(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="FocusEventArgs.cs" company="Steven Kirk">
|
||||
|
// Copyright 2013 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Input |
||||
|
{ |
||||
|
using System; |
||||
|
|
||||
|
public class FocusEventArgs : RoutedEventArgs |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="IFocusable.cs" company="Steven Kirk">
|
||||
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Input |
||||
|
{ |
||||
|
public interface IFocusable |
||||
|
{ |
||||
|
bool Focusable { get; } |
||||
|
|
||||
|
bool IsFocused { get; } |
||||
|
|
||||
|
void Focus(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,58 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="TextBoxStyle.cs" company="Steven Kirk">
|
||||
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Themes.Default |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using Perspex.Controls; |
||||
|
using Perspex.Media; |
||||
|
using Perspex.Shapes; |
||||
|
using Perspex.Styling; |
||||
|
|
||||
|
public class TextBoxStyle : Styles |
||||
|
{ |
||||
|
public TextBoxStyle() |
||||
|
{ |
||||
|
this.AddRange(new[] |
||||
|
{ |
||||
|
new Style(x => x.OfType<TextBox>()) |
||||
|
{ |
||||
|
Setters = new[] |
||||
|
{ |
||||
|
new Setter(Button.TemplateProperty, ControlTemplate.Create<TextBox>(this.Template)), |
||||
|
new Setter(TextBox.BorderBrushProperty, new SolidColorBrush(0xff707070)), |
||||
|
new Setter(TextBox.BorderThicknessProperty, 2.0), |
||||
|
}, |
||||
|
}, |
||||
|
new Style(x => x.OfType<TextBox>().Class(":focus").Template().Id("border")) |
||||
|
{ |
||||
|
Setters = new[] |
||||
|
{ |
||||
|
new Setter(TextBox.BorderBrushProperty, Brushes.Black), |
||||
|
}, |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
private Control Template(TextBox control) |
||||
|
{ |
||||
|
Border result = new Border |
||||
|
{ |
||||
|
Id = "border", |
||||
|
[~Border.BackgroundProperty] = control[~TextBox.BackgroundProperty], |
||||
|
[~Border.BorderBrushProperty] = control[~TextBox.BorderBrushProperty], |
||||
|
[~Border.BorderThicknessProperty] = control[~TextBox.BorderThicknessProperty], |
||||
|
Content = new Decorator |
||||
|
{ |
||||
|
Id = "textContainer", |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue