From 76fe2b4376ce36adbbf5e16b465036ba923b5555 Mon Sep 17 00:00:00 2001 From: jp Date: Mon, 4 Apr 2016 00:24:02 +0200 Subject: [PATCH] add textbox text facilities and clipboard --- Crow.csproj | 4 +- Styles/TextBox.style | 5 ++ src/GraphicObjects/Label.cs | 43 ++++++++++--- src/GraphicObjects/TextBox.cs | 113 ++++++++++++++++++++++++---------- src/Interface.cs | 2 +- 5 files changed, 124 insertions(+), 43 deletions(-) create mode 100644 Styles/TextBox.style diff --git a/Crow.csproj b/Crow.csproj index 3579f9a1..1276874c 100644 --- a/Crow.csproj +++ b/Crow.csproj @@ -21,8 +21,9 @@ OnBuildSuccess v4.5 AnyCPU - 0.4 Crow project description + 8.0.30703 + 2.0 true @@ -262,6 +263,7 @@ + diff --git a/Styles/TextBox.style b/Styles/TextBox.style new file mode 100644 index 00000000..2ad617a5 --- /dev/null +++ b/Styles/TextBox.style @@ -0,0 +1,5 @@ +Fit = true +Focusable = true +Background = White +Foreground = Black + diff --git a/src/GraphicObjects/Label.cs b/src/GraphicObjects/Label.cs index 64c37ff8..6d0bf471 100644 --- a/src/GraphicObjects/Label.cs +++ b/src/GraphicObjects/Label.cs @@ -9,8 +9,7 @@ using System.Xml.Serialization; using System.ComponentModel; namespace Crow -{ - [Serializable] +{ [DefaultStyle("#Crow.Styles.Label.style")] public class Label : GraphicObject { @@ -155,8 +154,8 @@ namespace Crow return; if (value < 0) _currentCol = 0; - else if (value > lines [_currentLine].Count ()) - _currentCol = lines [_currentLine].Count (); + else if (value > lines [_currentLine].Length) + _currentCol = lines [_currentLine].Length; else _currentCol = value; NotifyValueChanged ("CurrentColumn", _currentCol); @@ -173,7 +172,11 @@ namespace Crow else if (value < 0) _currentLine = 0; else - _currentLine = value; + _currentLine = value; + //force recheck of currentCol for bounding + int cc = _currentCol; + _currentCol = 0; + CurrentColumn = cc; NotifyValueChanged ("CurrentLine", _currentLine); } } @@ -205,6 +208,12 @@ namespace Crow } } + [XmlIgnore]protected Char CurrentChar //ordered selection start and end positions + { + get { + return lines [CurrentLine] [CurrentColumn]; + } + } [XmlIgnore]protected Point selectionStart //ordered selection start and end positions { get { @@ -250,6 +259,25 @@ namespace Crow } } + public void GotoWordStart(){ + CurrentColumn--; + //skip white spaces + while (char.IsWhiteSpace (this.CurrentChar) && CurrentColumn > 0) + CurrentColumn--; + while (!char.IsWhiteSpace (lines [CurrentLine] [CurrentColumn]) && CurrentColumn > 0) + CurrentColumn--; + if (char.IsWhiteSpace (this.CurrentChar)) + CurrentColumn++; + } + public void GotoWordEnd(){ + //skip white spaces + while (char.IsWhiteSpace (this.CurrentChar) && CurrentColumn < lines [CurrentLine].Length-1) + CurrentColumn++; + while (!char.IsWhiteSpace (this.CurrentChar) && CurrentColumn < lines [CurrentLine].Length-1) + CurrentColumn++; + if (!char.IsWhiteSpace (this.CurrentChar)) + CurrentColumn++; + } public void DeleteChar() { if (selectionIsEmpty) { @@ -257,7 +285,7 @@ namespace Crow if (CurrentLine == 0) return; CurrentLine--; - CurrentColumn = lines [CurrentLine].Count (); + CurrentColumn = lines [CurrentLine].Length; lines [CurrentLine] += lines [CurrentLine + 1]; lines.RemoveAt (CurrentLine + 1); NotifyValueChanged ("Text", Text); @@ -265,8 +293,7 @@ namespace Crow } CurrentColumn--; lines [CurrentLine] = lines [CurrentLine].Remove (CurrentColumn, 1); - } else { - Debug.WriteLine (selectionEnd.ToString()); + } else { int linesToRemove = selectionEnd.Y - selectionStart.Y; int l = selectionStart.Y; diff --git a/src/GraphicObjects/TextBox.cs b/src/GraphicObjects/TextBox.cs index 08ec1f69..a042198b 100644 --- a/src/GraphicObjects/TextBox.cs +++ b/src/GraphicObjects/TextBox.cs @@ -11,6 +11,7 @@ using System.Runtime.InteropServices; namespace Crow { + [DefaultStyle("#Crow.Styles.TextBox.style")] public class TextBox : Label { #region CTOR @@ -37,22 +38,6 @@ namespace Crow RegisterForGraphicUpdate(); } } - [XmlAttributeAttribute()][DefaultValue(true)] - public override bool Focusable - { - get { return base.Focusable; } - set { base.Focusable = value; } - } - [XmlAttributeAttribute()][DefaultValue("White")] - public override Fill Background { - get { return base.Background; } - set { base.Background = value; } - } - [XmlAttributeAttribute()][DefaultValue("Black")] - public override Fill Foreground { - get { return base.Foreground; } - set { base.Foreground = value; } - } protected override void onDraw (Context gr) { @@ -78,24 +63,21 @@ namespace Crow switch (key) { case Key.Back: - if (!selectionIsEmpty) - { - // Text = Text.Remove(selectionStart, selectionEnd - selectionStart); - // selReleasePos = -1; - // currentCol = selBeginPos; - } - else - this.DeleteChar(); + this.DeleteChar(); break; case Key.Clear: break; case Key.Delete: if (selectionIsEmpty) CurrentColumn++; + else if (e.Shift) + Interface.CurrentInterface.Clipboard = this.SelectedText; this.DeleteChar (); break; case Key.Enter: case Key.KeypadEnter: + if (!selectionIsEmpty) + this.DeleteChar (); if (Multiline) this.InsertLineBreak (); else @@ -107,35 +89,100 @@ namespace Crow SelRelease = -1; break; case Key.Home: - //TODO + if (e.Shift) { + if (selectionIsEmpty) + SelBegin = new Point (CurrentColumn, CurrentLine); + if (e.Control) + CurrentLine = 0; + CurrentColumn = 0; + SelRelease = new Point (CurrentColumn, CurrentLine); + break; + } + SelRelease = -1; if (e.Control) CurrentLine = 0; CurrentColumn = 0; break; case Key.End: + if (e.Shift) { + if (selectionIsEmpty) + SelBegin = new Point (CurrentColumn, CurrentLine); + if (e.Control) + CurrentLine = int.MaxValue; + CurrentColumn = int.MaxValue; + SelRelease = new Point (CurrentColumn, CurrentLine); + break; + } + SelRelease = -1; if (e.Control) CurrentLine = int.MaxValue; CurrentColumn = int.MaxValue; break; case Key.Insert: - break; - case Key.Left: - CurrentColumn--; - break; - case Key.Right: - CurrentColumn++; + if (e.Shift) + this.Insert (Interface.CurrentInterface.Clipboard); + break; + case Key.Left: + if (e.Shift) { + if (selectionIsEmpty) + SelBegin = new Point(CurrentColumn, CurrentLine); + if (e.Control) + GotoWordStart (); + else + CurrentColumn--; + SelRelease = new Point(CurrentColumn, CurrentLine); + break; + } + SelRelease = -1; + if (e.Control) + GotoWordStart (); + else + CurrentColumn--; + break; + case Key.Right: + if (e.Shift) { + if (selectionIsEmpty) + SelBegin = new Point(CurrentColumn, CurrentLine); + if (e.Control) + GotoWordEnd (); + else + CurrentColumn++; + SelRelease = new Point(CurrentColumn, CurrentLine); + break; + } + SelRelease = -1; + if (e.Control) + GotoWordEnd (); + else + CurrentColumn++; break; case Key.Up: + if (e.Shift) { + if (selectionIsEmpty) + SelBegin = new Point(CurrentColumn, CurrentLine); + CurrentLine--; + SelRelease = new Point(CurrentColumn, CurrentLine); + break; + } + SelRelease = -1; CurrentLine--; break; case Key.Down: - CurrentLine++; + if (e.Shift) { + if (selectionIsEmpty) + SelBegin = new Point(CurrentColumn, CurrentLine); + CurrentLine++; + SelRelease = new Point(CurrentColumn, CurrentLine); + break; + } + SelRelease = -1; + CurrentLine++; break; case Key.Menu: break; case Key.NumLock: break; - case Key.PageDown: + case Key.PageDown: break; case Key.PageUp: break; diff --git a/src/Interface.cs b/src/Interface.cs index 97ab1c9d..41199fcc 100644 --- a/src/Interface.cs +++ b/src/Interface.cs @@ -72,7 +72,7 @@ namespace Crow public Queue LayoutingQueue; public Queue GraphicUpdateQueue = new Queue(); - + public string Clipboard; public static void RegisterForGraphicUpdate(GraphicObject g) { lock (CurrentInterface.GraphicUpdateQueue) { -- 2.47.3