// This code is from Microsoft
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace SqlBuilder.Controls
{
/// <summary>
/// This is a copy of DataGridTextBoxColumn
/// The fucking bastards from Microsoft made the TextBox property read-only.
/// So it is impossible to attach a custom DataGridTextBox which works correctly
/// The Microsoft Textbox is unusable because it does not allow to enter the "[" key
/// Additionally it blocks the F5 key
/// </summary>
public class CrackedTextBoxColumn : DataGridColumnStyle
{
const int LINEWIDTH = 1;
private Brush backBrush = new SolidBrush(SystemColors.Window);
private Brush foreBrush = new SolidBrush(SystemColors.WindowText);
private DataGridEx grid;
private DataGridTextBox edit = new DataGridTextBox();
private int editRow = -1;
private string format;
private IFormatProvider formatInfo;
private string oldValue;
private MethodInfo parseMethod;
private Rectangle rect = new Rectangle();
private TypeConverter typeConverter;
private int xMargin = 2;
private int yMargin = 1;
public CrackedTextBoxColumn(DataGridEx iGrid)
{
grid = iGrid;
}
public DataGridTextBox TextBox
{
get { return edit; }
set { edit = value; } // This is what the fucking Microsoft bastards did not implement
}
protected override void Abort(int rowNum)
{
this.RollBack();
this.HideEditBox();
this.EndEdit();
}
protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
this.edit.Bounds = Rectangle.Empty;
if (!this.edit.IsInEditOrNavigateMode)
{
try
{
object text = this.edit.Text;
if (this.NullText.Equals(text))
{
text = Convert.DBNull;
this.edit.Text = this.NullText;
}
else if (((this.format != null) && (this.format.Length != 0)) && ((this.parseMethod != null) && (this.FormatInfo != null)))
{
text = this.parseMethod.Invoke(null, new object[] { this.edit.Text, this.FormatInfo });
if (text is IFormattable)
{
this.edit.Text = ((IFormattable) text).ToString(this.format, this.formatInfo);
}
else
{
this.edit.Text = text.ToString();
}
}
else if ((this.typeConverter != null) && this.typeConverter.CanConvertFrom(typeof(string)))
{
text = this.typeConverter.ConvertFromString(this.edit.Text);
this.edit.Text = this.typeConverter.ConvertToString(text);
}
this.SetColumnValueAtRow(dataSource, rowNum, text);
}
catch (Exception)
{
this.RollBack();
return false;
}
this.EndEdit();
}
return true;
}
protected override void ConcedeFocus()
{
this.edit.Bounds = Rectangle.Empty;
}
protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
Rectangle rc = bounds;
this.edit.ReadOnly = (readOnly || this.ReadOnly) || this.DataGridTableStyle.ReadOnly;
this.edit.Text = this.GetText(this.GetColumnValueAtRow(source, rowNum));
if (!this.edit.ReadOnly && (instantText != null))
{
grid.ColumnStartedEditingPublic(bounds);
this.edit.IsInEditOrNavigateMode = false;
this.edit.Text = instantText;
}
if (cellIsVisible)
{
bounds.Offset(this.xMargin, 2 * this.yMargin);
bounds.Width -= this.xMargin;
bounds.Height -= 2 * this.yMargin;
this.edit.Bounds = bounds;
this.edit.Visible = true;
this.edit.TextAlign = this.Alignment;
}
else
{
this.edit.Bounds = Rectangle.Empty;
}
this.edit.RightToLeft = grid.RightToLeft;
this.edit.Focus();
this.editRow = rowNum;
if (!this.edit.ReadOnly)
{
this.oldValue = this.edit.Text;
}
if (instantText == null)
{
this.edit.SelectAll();
}
else
{
int length = this.edit.Text.Length;
this.edit.Select(length, 0);
}
if (this.edit.Visible)
{
grid.Invalidate(rc);
}
}
protected void EndEdit()
{
this.edit.IsInEditOrNavigateMode = true;
this.Invalidate();
}
protected override void EnterNullValue()
{
if ((!this.ReadOnly && this.edit.Visible) && this.edit.IsInEditOrNavigateMode)
{
this.edit.Text = this.NullText;
this.edit.IsInEditOrNavigateMode = false;
if ((this.DataGridTableStyle != null) && (grid != null))
{
grid.ColumnStartedEditingPublic(this.edit.Bounds);
}
}
}
protected override int GetMinimumHeight()
{
return ((base.FontHeight + this.yMargin) + 3);
}
protected override int GetPreferredHeight(Graphics g, object value)
{
int index = 0;
int num2 = 0;
string text = this.GetText(value);
while ((index != -1) && (index < text.Length))
{
index = text.IndexOf("\r\n", index + 1);
num2++;
}
return ((base.FontHeight * num2) + this.yMargin);
}
protected override Size GetPreferredSize(Graphics g, object value)
{
Size size = Size.Ceiling(g.MeasureString(this.GetText(value), grid.Font));
size.Width += (this.xMargin * 2) + LINEWIDTH;
size.Height += this.yMargin;
return size;
}
private string GetText(object value)
{
if (value is DBNull)
{
return this.NullText;
}
if (((this.format != null) && (this.format.Length != 0)) && (value is IFormattable))
{
try
{
return ((IFormattable) value).ToString(this.format, this.formatInfo);
}
catch (Exception)
{
goto _Error;
}
}
if ((this.typeConverter != null) && this.typeConverter.CanConvertTo(typeof(string)))
{
return (string) this.typeConverter.ConvertTo(value, typeof(string));
}
_Error:
if (value == null)
{
return "";
}
return value.ToString();
}
protected void HideEditBox()
{
bool focused = this.edit.Focused;
this.edit.Visible = false;
if ((focused && (this.DataGridTableStyle != null)) && ((grid != null) && grid.CanFocus))
{
grid.Focus();
}
}
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum)
{
// painting is done in derived class
}
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight)
{
// painting is done in derived class
}
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
// painting is done in derived class
}
protected void PaintText(Graphics g, Rectangle bounds, string text, bool alignToRight)
{
// painting is done in derived class
}
protected void PaintText(Graphics g, Rectangle textBounds, string text, Brush backBrush, Brush foreBrush, bool alignToRight)
{
// painting is done in derived class
}
protected override void ReleaseHostedControl()
{
if (this.edit.Parent!= null)
{
this.edit.Parent.Controls.Remove(this.edit);
}
}
private void RollBack()
{
this.edit.Text = this.oldValue;
}
protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
if (this.edit.Parent!= null)
{
this.edit.Parent.Controls.Remove(this.edit);
}
if (value != null)
{
value.Controls.Add(this.edit);
}
this.edit.SetDataGrid(value);
}
protected override void UpdateUI(CurrencyManager source, int rowNum, string instantText)
{
this.edit.Text = this.GetText(this.GetColumnValueAtRow(source, rowNum));
if (!this.edit.ReadOnly && (instantText != null))
{
this.edit.Text = instantText;
}
}
public string Format
{
get
{
return this.format;
}
set
{
if (value == null)
{
value = "";
}
if ((this.format == null) || !this.format.Equals(value))
{
this.format = value;
if (((this.format.Length == 0) && (this.typeConverter != null)) && !this.typeConverter.CanConvertFrom(typeof(string)))
{
this.ReadOnly = true;
}
this.Invalidate();
}
}
}
public IFormatProvider FormatInfo
{
get
{
return this.formatInfo;
}
set
{
if ((this.formatInfo == null) || !this.formatInfo.Equals(value))
{
this.formatInfo = value;
}
}
}
public override PropertyDescriptor PropertyDescriptor
{
set
{
base.PropertyDescriptor = value;
if ((this.PropertyDescriptor != null) && (this.PropertyDescriptor.PropertyType != typeof(object)))
{
this.typeConverter = TypeDescriptor.GetConverter(this.PropertyDescriptor.PropertyType);
this.parseMethod = this.PropertyDescriptor.PropertyType.GetMethod("Parse", new Type[] { typeof(string), typeof(IFormatProvider) });
}
}
}
public override bool ReadOnly
{
get
{
return base.ReadOnly;
}
set
{
if ((value || !"".Equals(this.format)) || ((this.typeConverter == null) || this.typeConverter.CanConvertFrom(typeof(string))))
{
base.ReadOnly = value;
}
}
}
}
}