private void SetCaretAtEnd(Graphics g) { this.CharIndex = this.Text.Length; this.CalculateLines(g); if (this.Text[this.Text.Length - 1] == '\n') { this.lineIndex = this.lines.Count; WPTextBox.SetCaretPos(1, (this.lineIndex - 1) * this.lineHeight); } else { this.lineIndex = this.lines.Count - 1; this.SetCaretAtNormalChar(g, false); } }
private void SetCaretAtPosX(Graphics g, int posX) { if (this.lineIndex == 1 && (this.Text[0] == '\r' || posX < 3)) { this.CharIndex = 0; WPTextBox.SetCaretPos(1, 0); } else if (this.lineIndex != 1 && this.lines[this.lineIndex - 1] + 1 < this.Text.Length && this.Text[this.lines[this.lineIndex - 1] + 1] == '\r') { this.CharIndex = this.lines[this.lineIndex - 1] + 1; WPTextBox.SetCaretPos(1, (this.lineIndex - 1) * this.lineHeight + this.startY); } else { int First; for (First = this.lines[this.lineIndex - 1] + 1; First <= this.lines[this.lineIndex]; ++First) { if (this.Text[First] == '\r') { this.CharIndex = First; this.SetCaretAtNormalChar(g, false); break; } CharacterRange[] ranges = new CharacterRange[1] { new CharacterRange(First, 1) }; StringFormat stringFormat = new StringFormat(); stringFormat.SetMeasurableCharacterRanges(ranges); stringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; RectangleF bounds = g.MeasureCharacterRanges(this.Text, this.Font, new RectangleF(0.0f, (float)this.startY, (float)this.Width, (float)this.maxHeight), stringFormat)[0].GetBounds(g); if ((double)posX <= (double)bounds.X + (double)bounds.Width / 2.0) { this.CharIndex = First; WPTextBox.SetCaretPos((int)bounds.Left, (int)bounds.Y); break; } } if (First <= this.lines[this.lineIndex]) { return; } this.CharIndex = First; this.SetCaretAtNormalChar(g, false); } }
private void SetCaretAtNormalChar(Graphics g, bool afterReturn) { if (this.CharIndex == 0) { WPTextBox.SetCaretPos(1, 0); } else { if (this.maxHeight == 0) { this.maxHeight = this.Height; } if (afterReturn) { CharacterRange[] ranges = new CharacterRange[1] { new CharacterRange(this.CharIndex, 1) }; StringFormat stringFormat = new StringFormat(); stringFormat.SetMeasurableCharacterRanges(ranges); stringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; RectangleF bounds = g.MeasureCharacterRanges(this.Text, this.Font, new RectangleF(0.0f, (float)this.startY, (float)this.Width, (float)this.maxHeight), stringFormat)[0].GetBounds(g); WPTextBox.SetCaretPos((int)bounds.Left, (int)bounds.Top); } else { int num = (int)this.Text[this.CharIndex - 1]; CharacterRange[] ranges = new CharacterRange[1] { new CharacterRange(this.CharIndex - 1, 1) }; StringFormat stringFormat = new StringFormat(); stringFormat.SetMeasurableCharacterRanges(ranges); stringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; RectangleF bounds = g.MeasureCharacterRanges(this.Text, this.Font, new RectangleF(0.0f, (float)this.startY, (float)this.Width, (float)this.maxHeight), stringFormat)[0].GetBounds(g); WPTextBox.SetCaretPos((int)bounds.Right, (int)bounds.Top); } } }
private void CalculateCaretPos(Graphics g, Keys input) { if (input == Keys.F24) { return; } if (this.Text.Length == 0) { WPTextBox.SetCaretPos(1, 1); } else { bool flag = false; bool afterReturn = false; Point empty1 = Point.Empty; switch (input) { case Keys.Back: this.Modified = true; this.AutoScrollDown(); if (this.SelectionLength > 0) { this.Text = this.Text.Remove(this.SelectionStart, this.SelectionLength); this.CharIndex = this.SelectionStart; flag = false; this.Selecting = false; break; } this.Selecting = false; --this.CharIndex; if (this.CharIndex > 0) { if (this.Text[this.CharIndex] == '\n') { this.Text = this.Text.Remove(--this.CharIndex, 2); if (this.CharIndex <= 0) { WPTextBox.SetCaretPos(1, 0); this.CharIndex = 0; flag = true; break; } if (this.Text[this.CharIndex - 1] == '\n') { WPTextBox.GetCaretPos(ref empty1); WPTextBox.SetCaretPos(1, empty1.Y - this.lineHeight); flag = true; break; } flag = false; break; } this.Text = this.Text.Remove(this.CharIndex, 1); if (this.Text[this.CharIndex - 1] == '\n') { WPTextBox.GetCaretPos(ref empty1); WPTextBox.SetCaretPos(1, empty1.Y); flag = true; break; } flag = false; break; } if (this.CharIndex == 0) { this.Text = this.Text.Remove(this.CharIndex, 1); WPTextBox.SetCaretPos(1, 0); flag = true; break; } this.CharIndex = 0; this.lineIndex = 1; flag = true; break; case Keys.Return: this.Modified = true; this.AutoScrollUp(); WPTextBox.GetCaretPos(ref empty1); WPTextBox.SetCaretPos(1, empty1.Y + this.lineHeight); flag = true; break; case Keys.End: this.Modified = true; this.CalculateLines(g); this.SetCaretAtEnd(g); flag = true; break; case Keys.Home: this.CharIndex = 0; this.startY = 0; this.lineIndex = 1; WPTextBox.SetCaretPos(1, 1); flag = true; break; case Keys.Left: this.Modified = true; this.AutoScrollDown(); if (--this.CharIndex <= 0) { this.CharIndex = 0; this.lineIndex = 1; WPTextBox.SetCaretPos(1, 0); flag = true; break; } if (this.Text[this.CharIndex - 1] != '\n' && this.Text[this.CharIndex - 1] != '\r') { flag = false; break; } if (this.Text[this.CharIndex - 1] == '\n') { afterReturn = true; flag = false; break; } if (this.Text[this.CharIndex] == '\n') { --this.CharIndex; if (this.CharIndex <= 0) { WPTextBox.SetCaretPos(1, 0); this.CharIndex = 0; flag = true; break; } if (this.Text[this.CharIndex - 1] == '\n') { WPTextBox.GetCaretPos(ref empty1); WPTextBox.SetCaretPos(1, empty1.Y - this.lineHeight); flag = true; break; } flag = false; break; } break; case Keys.Up: this.CalculateLines(g); this.AutoScrollDown(); if (this.lineIndex > 1) { --this.lineIndex; WPTextBox.GetCaretPos(ref empty1); this.SetCaretAtPosX(g, empty1.X); } flag = true; break; case Keys.Right: this.Modified = true; if (++this.CharIndex > this.Text.Length) { this.CharIndex = this.Text.Length; flag = true; break; } this.AutoScrollUp(); if (this.CharIndex < this.Text.Length && (this.Text[this.CharIndex - 1] == '\r' || this.Text[this.CharIndex - 1] == '\n')) { if (this.Text[this.CharIndex - 1] == '\r') { ++this.CharIndex; } WPTextBox.GetCaretPos(ref empty1); WPTextBox.SetCaretPos(1, empty1.Y + this.lineHeight); flag = true; break; } flag = false; break; case Keys.Down: this.CalculateLines(g); this.AutoScrollUp(); if (this.lineIndex < this.lines.Count) { ++this.lineIndex; if (this.lineIndex == this.lines.Count) { this.lineIndex = this.lines.Count - 1; this.SetCaretAtEnd(g); } else { Point empty2 = Point.Empty; WPTextBox.GetCaretPos(ref empty2); this.SetCaretAtPosX(g, empty2.X); } } flag = true; break; case Keys.Delete: this.Modified = true; if (this.SelectionLength > 0) { this.Text = this.Text.Remove(this.SelectionStart, this.SelectionLength); this.CharIndex = this.SelectionStart; flag = false; this.Selecting = false; break; } if (this.CharIndex == this.Text.Length) { flag = true; break; } if (this.Text[this.CharIndex] == '\r') { this.Text = this.Text.Remove(this.CharIndex, 2); } else { this.Text = this.Text.Remove(this.CharIndex, 1); } flag = true; break; default: if (this.DialogChar) { this.DialogChar = false; flag = false; break; } break; } if (!flag) { this.SetCaretAtNormalChar(g, afterReturn); } input = Keys.F24; } }