protected override void OnLostFocus(EventArgs e) { if (this.input == Keys.Left || this.input == Keys.Right || (this.input == Keys.Up || this.input == Keys.Down) || this.Selecting) { this.Focus(); } else { WPTextBox.DestroyCaret(); } }
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); } }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (this.Focused) { this.CalculateCaretPos(e.Graphics, this.input); this.input = Keys.F24; this.SetCaretPosAtMouse(e.Graphics); } else { WPTextBox.HideCaret(this.Handle); this.input = Keys.F24; } e.Graphics.DrawString(this.Text, this.Font, (Brush) new SolidBrush(this.ForeColor), new RectangleF(0.0f, (float)this.startY, (float)this.Width, (float)(this.Height - this.startY)), StringFormat.GenericDefault); }
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 CalculateLines(Graphics g) { if (!this.Modified) { return; } this.lines.Clear(); this.lines.Add(0); StringFormat stringFormat = new StringFormat(); stringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; RectangleF layoutRect = new RectangleF(0.0f, (float)this.startY, (float)this.Width, (float)(this.Height - this.startY + this.lineHeight)); int First; for (First = 0; First < this.Text.Length; ++First) { if (this.Text[First] == '\r') { this.lines.Add(++First); } else { CharacterRange[] ranges = new CharacterRange[1] { new CharacterRange(First, 1) }; stringFormat.SetMeasurableCharacterRanges(ranges); RectangleF bounds = g.MeasureCharacterRanges(this.Text, this.Font, layoutRect, stringFormat)[0].GetBounds(g); if (this.input == Keys.End && (double)bounds.Height == 0.0 && this.Text[First] != ' ') { layoutRect.Height += (float)this.lineHeight; --First; } else if ((double)bounds.Y > (double)(this.lineHeight * this.lines.Count + this.startY) - (double)bounds.Height / 2.0) { this.lines.Add(First - 1); } } } if ((double)layoutRect.Height > (double)this.maxHeight) { this.maxHeight = (int)layoutRect.Height; } if (First == this.Text.Length && this.lines[this.lines.Count - 1] != this.Text.Length - 1) { this.lines.Add(this.Text.Length - 1); } Point empty = Point.Empty; WPTextBox.GetCaretPos(ref empty); this.lineIndex = (int)((double)(empty.Y - this.startY) / (double)this.lineHeight + 1.5); if (this.lineIndex < 1) { this.lineIndex = 1; this.startY = 0; } if (this.CharIndex == this.Text.Length || this.lineIndex >= this.lines.Count) { this.lineIndex = this.Text[this.Text.Length - 1] != '\n' ? this.lines.Count - 1 : this.lines.Count; } if (this.input == Keys.End) { this.startY = this.Height - this.lineHeight * this.lines.Count; if (this.startY > 0) { this.startY = 0; } } this.Modified = false; }
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; } }
protected override void OnGotFocus(EventArgs e) { this.lineHeight = this.Font.Height; WPTextBox.CreateCaret(this.Handle, new IntPtr(0), 1, this.lineHeight); WPTextBox.ShowCaret(this.Handle); }