using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
namespace SourceGrid
{
/// <summary>
/// Summary description for PropertyGridEx.
/// </summary>
public class PropertyGridEx : System.Windows.Forms.UserControl
{
#region System
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public PropertyGridEx()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitForm call
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.propertyGrid = new SourceGrid.Grid();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.propertyGrid});
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(352, 280);
this.panel1.TabIndex = 0;
//
// propertyGrid
//
this.propertyGrid.AutoSizeMinHeight = 20;
this.propertyGrid.AutoSizeMinWidth = 20;
this.propertyGrid.BackColor = System.Drawing.Color.White;
this.propertyGrid.CellsContainerCursor = System.Windows.Forms.Cursors.Default;
this.propertyGrid.CellsContainerToolTipText = "";
this.propertyGrid.Cols = 0;
this.propertyGrid.CustomScrollArea = new System.Drawing.Size(0, 0);
this.propertyGrid.CustomScrollPosition = new System.Drawing.Point(0, 0);
this.propertyGrid.Dock = System.Windows.Forms.DockStyle.Fill;
this.propertyGrid.FixedCols = 0;
this.propertyGrid.FixedRows = 0;
this.propertyGrid.GridScrollPosition = new System.Drawing.Point(0, 0);
this.propertyGrid.Name = "propertyGrid";
this.propertyGrid.Rows = 0;
this.propertyGrid.Size = new System.Drawing.Size(350, 278);
this.propertyGrid.TabIndex = 5;
this.propertyGrid.ToolTipActive = true;
//
// PropertyGridEx
//
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.panel1});
this.Name = "PropertyGridEx";
this.Size = new System.Drawing.Size(352, 280);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
#endregion
private SourceGrid.Grid propertyGrid;
public void LoadProperties(string[] p_Caption, Type[] p_ObjectType, object[] p_DefaultValue, bool p_bUnloadCurrentObject)
{
if (p_bUnloadCurrentObject)
UnLoadProperties();
InitGrid();
for (int i = 0; i < p_ObjectType.Length; i++)
{
Cell l_CaptionCell;
Cell l_ValueCell;
if (p_DefaultValue!=null)
GetCells(p_Caption[i],p_ObjectType[i],p_DefaultValue[i],out l_CaptionCell, out l_ValueCell);
else
GetCells(p_Caption[i],p_ObjectType[i],null,out l_CaptionCell, out l_ValueCell);
int l_CurrentRow = propertyGrid.Rows;
propertyGrid.AddRow(l_CurrentRow);
propertyGrid[l_CurrentRow,0] = l_CaptionCell;
propertyGrid[l_CurrentRow,1] = l_ValueCell;
}
EndGrid();
}
public void LoadProperties(object p_ObjectInstance)
{
LoadProperties(p_ObjectInstance,p_ObjectInstance.GetType().GetProperties());
}
public void LoadProperties(object p_ObjectInstance, PropertyInfo[] p_Properties)
{
UnLoadProperties();
InitGrid();
for (int i = 0; i < p_Properties.Length; i++)
{
if (IsBrowsable(p_Properties[i]))
{
Cell l_CaptionCell;
Cell l_ValueCell;
GetCells(p_Properties[i],p_ObjectInstance,out l_CaptionCell, out l_ValueCell);
int l_CurrentRow = propertyGrid.Rows;
propertyGrid.AddRow(l_CurrentRow);
propertyGrid[l_CurrentRow,0] = l_CaptionCell;
propertyGrid[l_CurrentRow,1] = l_ValueCell;
}
}
EndGrid();
}
public void LoadProperties(ParameterInfo[] p_Parameters)
{
UnLoadProperties();
InitGrid();
for (int i = 0; i < p_Parameters.Length; i++)
{
if (IsBrowsable(p_Parameters[i]))
{
Cell l_CaptionCell;
Cell l_ValueCell;
GetCells(p_Parameters[i],out l_CaptionCell, out l_ValueCell);
int l_CurrentRow = propertyGrid.Rows;
propertyGrid.AddRow(l_CurrentRow);
propertyGrid[l_CurrentRow,0] = l_CaptionCell;
propertyGrid[l_CurrentRow,1] = l_ValueCell;
}
}
EndGrid();
}
public object[] GetValues()
{
object[] l_Objs = new object[propertyGrid.Rows];
for (int r = 0; r < l_Objs.Length; r++)
{
l_Objs[r] = propertyGrid[r,1].Value;
}
return l_Objs;
}
public void UnLoadProperties()
{
ClearObjectProperties();
}
protected virtual void ClearObjectProperties()
{
propertyGrid.Redim(0,0);
}
protected virtual void InitGrid()
{
propertyGrid.Cols = 2;
propertyGrid.Selection.SelectionMode = GridSelectionMode.Row;
}
protected virtual void EndGrid()
{
propertyGrid.SetColWidth(0,130);
propertyGrid.SetColWidth(1,Math.Max(100,propertyGrid.Width-propertyGrid.GetColWidth(0)-30));
}
private TypeCreator.TypeCreator m_Typereator = new TypeCreator.TypeCreator();
public TypeCreator.TypeCreator TypeCreator
{
get{return m_Typereator;}
set{m_Typereator = value;}
}
#region Property
protected virtual void GetCells(PropertyInfo p_Info, object p_ObjectInstance, out Cell p_CaptionCell, out Cell p_ValueCell)
{
p_CaptionCell = new Cell(p_Info.Name);
p_CaptionCell.ToolTipText = p_Info.Name + "("+ p_Info.PropertyType.ToString() + ")";
p_CaptionCell.TextAlignment = ContentAlignment.MiddleLeft;
p_CaptionCell.BackColor = Color.WhiteSmoke;
try
{
//se pu� leggere e puo scrivere o � un reference type
if (p_Info.CanWrite || p_Info.PropertyType.IsValueType==false )
{
// p_ValueCell = new CellTyped(p_Info.PropertyType,
// new CellFormatter.CellFormatterTypeConverter(System.ComponentModel.TypeDescriptor.GetConverter(p_Info.PropertyType)),
// (System.Drawing.Design.UITypeEditor)System.ComponentModel.TypeDescriptor.GetEditor(p_Info.PropertyType,typeof(System.Drawing.Design.UITypeEditor)),
// p_Info.GetValue(p_ObjectInstance,null),
// p_Info.GetValue(p_ObjectInstance,null),
// TypeCreator);
p_ValueCell = new Cell(p_Info.GetValue(p_ObjectInstance,null));
p_ValueCell.Editor = Utility.CreateCellEditor(p_Info.PropertyType, p_Info.GetValue(p_ObjectInstance,null) , !(p_Info.PropertyType.IsValueType), TypeCreator);
if (p_Info.CanWrite)
p_ValueCell.BindValueAtProperty(p_Info,p_ObjectInstance);
p_ValueCell.TextAlignment = ContentAlignment.MiddleLeft;
}
else
p_ValueCell = new Cell(p_Info.GetValue(p_ObjectInstance,null));
p_ValueCell.TextAlignment = ContentAlignment.MiddleLeft;
}
catch(Exception err)
{
p_ValueCell = new Cell(err.Message);
p_ValueCell.TextAlignment = ContentAlignment.MiddleLeft;
}
}
protected virtual bool IsBrowsable(PropertyInfo p_Info)
{
object[] l_browsable = p_Info.GetCustomAttributes(typeof(BrowsableAttribute),true);
for (int i = 0; i < l_browsable.Length; i++)
{
BrowsableAttribute l_attribute = (BrowsableAttribute)l_browsable[i];
if (l_attribute.Browsable == false)
return false;
}
if (p_Info.CanRead)
return true;
else
return false;
}
#endregion
#region Parameter
protected virtual void GetCells(ParameterInfo p_Info, out Cell p_CaptionCell, out Cell p_ValueCell)
{
p_CaptionCell = new Cell(p_Info.Name);
p_CaptionCell.ToolTipText = p_Info.Name + "("+ p_Info.ParameterType.ToString() + ")";
p_CaptionCell.TextAlignment = ContentAlignment.MiddleLeft;
p_CaptionCell.BackColor = Color.WhiteSmoke;
try
{
//if (p_Info.IsIn) //This method depends on an optional metadata flag. This flag can be inserted by compilers, but the compilers are not obligated to do so
object l_DefaultValue = null;
if (p_Info.ParameterType.IsValueType)
l_DefaultValue = Activator.CreateInstance(p_Info.ParameterType);
// p_ValueCell = new CellTyped(p_Info.ParameterType,
// new CellFormatter.CellFormatterTypeConverter(System.ComponentModel.TypeDescriptor.GetConverter(p_Info.ParameterType)),
// (System.Drawing.Design.UITypeEditor)System.ComponentModel.TypeDescriptor.GetEditor(p_Info.ParameterType,typeof(System.Drawing.Design.UITypeEditor)),
// l_DefaultValue,
// l_DefaultValue,
// TypeCreator);
p_ValueCell = new Cell(l_DefaultValue);
p_ValueCell.Editor = Utility.CreateCellEditor(p_Info.ParameterType,l_DefaultValue,!(p_Info.ParameterType.IsValueType),TypeCreator);
p_ValueCell.TextAlignment = ContentAlignment.MiddleLeft;
}
catch(Exception err)
{
p_ValueCell = new Cell(err.Message);
p_ValueCell.TextAlignment = ContentAlignment.MiddleLeft;
}
}
protected virtual bool IsBrowsable(ParameterInfo p_Info)
{
return true;
}
#endregion
#region CustomType
protected virtual void GetCells(string p_Caption, Type p_Type, object p_DefaultValue, out Cell p_CaptionCell, out Cell p_ValueCell)
{
p_CaptionCell = new Cell(p_Caption);
p_CaptionCell.ToolTipText = p_Caption + "("+ p_Type.ToString() + ")";
p_CaptionCell.TextAlignment = ContentAlignment.MiddleLeft;
p_CaptionCell.BackColor = Color.WhiteSmoke;
try
{
/*object l_DefaultValue = null;
if (p_Type.IsValueType)
l_DefaultValue = Activator.CreateInstance(p_Type);*/
// p_ValueCell = new CellTyped(p_Type,
// new CellFormatter.CellFormatterTypeConverter(System.ComponentModel.TypeDescriptor.GetConverter(p_Type)),
// (System.Drawing.Design.UITypeEditor)System.ComponentModel.TypeDescriptor.GetEditor(p_Type,typeof(System.Drawing.Design.UITypeEditor)),
// p_DefaultValue,
// p_DefaultValue,
// TypeCreator);
p_ValueCell = new Cell(p_DefaultValue);
p_ValueCell.Editor = Utility.CreateCellEditor(p_Type,p_DefaultValue,!(p_Type.IsValueType),TypeCreator);
p_ValueCell.TextAlignment = ContentAlignment.MiddleLeft;
}
catch(Exception err)
{
p_ValueCell = new Cell(err.Message);
p_ValueCell.TextAlignment = ContentAlignment.MiddleLeft;
}
}
#endregion
}
}