using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace LB_VisionControls
{
///
/// Debug emulator.
///
public class DebugTextBox : FastColoredTextBox
{
private volatile bool isReadLineMode;
private volatile bool isUpdating;
private Place StartReadPlace { get; set; }
///
/// Control is waiting for line entering.
///
public bool IsReadLineMode
{
get { return isReadLineMode; }
set { isReadLineMode = value; }
}
///
/// Append line to end of text.
///
///
public void WriteLine(string text)
{
IsReadLineMode = false;
isUpdating = true;
try
{
AppendText(text);
GoEnd();
}
finally
{
isUpdating = false;
ClearUndo();
}
}
///
/// Wait for line entering.
/// Set IsReadLineMode to false for break of waiting.
///
///
public string ReadLine()
{
GoEnd();
StartReadPlace = Range.End;
IsReadLineMode = true;
try
{
while (IsReadLineMode)
{
Application.DoEvents();
Thread.Sleep(5);
}
}
finally
{
IsReadLineMode = false;
ClearUndo();
}
return new Range(this, StartReadPlace, Range.End).Text.TrimEnd('\r', '\n');
}
public override void OnTextChanging(ref string text)
{
if (!IsReadLineMode && !isUpdating)
{
text = ""; //cancel changing
return;
}
if (IsReadLineMode)
{
if (Selection.Start < StartReadPlace || Selection.End < StartReadPlace)
GoEnd();//move caret to entering position
if (Selection.Start == StartReadPlace || Selection.End == StartReadPlace)
if (text == "\b") //backspace
{
text = ""; //cancel deleting of last char of readonly text
return;
}
if (text != null && text.Contains('\n'))
{
text = text.Substring(0, text.IndexOf('\n') + 1);
IsReadLineMode = false;
}
}
base.OnTextChanging(ref text);
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// DebugTextBox
//
this.AutoScrollMinSize = new System.Drawing.Size(27, 14);
this.BackColor = System.Drawing.SystemColors.Control;
this.Name = "DebugTextBox";
this.ResumeLayout(false);
}
}
}