using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Design; using System.Globalization; using System.Linq; using System.Windows.Forms; namespace JD.Winforms.XmlGrid { #region BoolConverter public class BoolVarConverter : StringConverter { public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return new StandardValuesCollection(new string[] { "1", "0" }); } public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType) { if (destinationType == typeof(string)) return true; return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType) { if (destinationType == typeof(System.String) && value is bool) { bool so = (bool)value; return so.ToString(); } return base.ConvertTo(context, culture, value, destinationType); } public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType) { if (sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { bool ret; string val = (string)value; if (val == "1") { ret = true; } else { ret = false; } return ret; } return base.ConvertFrom(context, culture, value); } } #endregion #region CharConverter public class CharVarConverter : StringConverter { public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType) { if (destinationType == typeof(string)) return true; return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType) { if (destinationType == typeof(System.String) && value is char) { char so = (char)value; return so.ToString(); } return base.ConvertTo(context, culture, value, destinationType); } public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType) { if (sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string val = (string)value; char ret; if (val != null && val.Length > 0) { ret = val[0]; } else { ret = ' '; } return ret; } return base.ConvertFrom(context, culture, value); } } #endregion #region Int16Converter public class Int16VarConverter : StringConverter { public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType) { if (destinationType == typeof(string)) return true; return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType) { if (destinationType == typeof(System.String) && value is Int16) { Int16 so = (Int16)value; return so.ToString(); } return base.ConvertTo(context, culture, value, destinationType); } public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType) { if (sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string val = (string)value; Int16 ret; if (val != null) { ret = Int16.Parse(val); } else { ret = 0; } return ret; } return base.ConvertFrom(context, culture, value); } } #endregion #region Int32Converter public class Int32VarConverter : StringConverter { public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType) { if (destinationType == typeof(string)) return true; return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType) { if (destinationType == typeof(System.String) && value is Int32) { Int32 so = (Int32)value; return so.ToString(); } return base.ConvertTo(context, culture, value, destinationType); } public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType) { if (sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string val = (string)value; Int32 ret; if (val != null) { ret = Int32.Parse(val); } else { ret = 0; } return ret; } return base.ConvertFrom(context, culture, value); } } #endregion #region UInt32Converter public class UInt32VarConverter : StringConverter { public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType) { if (destinationType == typeof(string)) return true; return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType) { if (destinationType == typeof(System.String) && value is UInt32) { UInt32 so = (UInt32)value; return so.ToString(); } return base.ConvertTo(context, culture, value, destinationType); } public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType) { if (sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string val = (string)value; UInt32 ret; if (val != null) { ret = UInt32.Parse(val); } else { ret = 0; } return ret; } return base.ConvertFrom(context, culture, value); } } #endregion #region DecimalValConverter public class DecimalVarConverter : StringConverter { public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType) { if (destinationType == typeof(string)) return true; return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType) { if (destinationType == typeof(System.String) && value is Decimal) { Decimal so = (Decimal)value; return so.ToString(); } return base.ConvertTo(context, culture, value, destinationType); } public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType) { if (sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string val = (string)value; Decimal ret; if (val != null) { CultureInfo ci = new CultureInfo(CultureInfo.CurrentCulture.Name, false); NumberFormatInfo formatInfo = ci.NumberFormat; formatInfo.NumberDecimalSeparator = "."; formatInfo.NumberGroupSeparator = ","; try { ret = Decimal.Parse(val, formatInfo); } catch (OverflowException) { ret = Decimal.MaxValue; } } else { ret = 0; } return ret; } return base.ConvertFrom(context, culture, value); } } #endregion #region ColorVarConverter public class ColorVarConverter : TypeConverter { public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType) { if (destinationType == typeof(string)) return true; return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType) { if (destinationType == typeof(System.String) && value is Color) { Color so = ((Color)value); byte red = so.R; byte green = so.G; byte blue = so.B; byte alpha = so.A; return "r=" + red.ToString() + " g=" + green.ToString() + " b=" + blue.ToString() + " a=" + alpha.ToString(); } return base.ConvertTo(context, culture, value, destinationType); } public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType) { if (sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string val = (string)value; if (val.Contains('=')) { string[] splitted = val.Trim().Split(' '); byte red = 255; byte green = 255; byte blue = 255; byte alpha = 255; for (int i = 0; i < splitted.Length; i++) { if (splitted[i].Trim().Length == 0) continue; string[] col = splitted[i].Split('='); // "r=255" [0]:r [1]:255 string type = col[0].Trim(); byte valueColor = byte.Parse(col[1]); if (type == "r") red = valueColor; else if (type == "g") green = valueColor; else if (type == "b") blue = valueColor; else if (type == "a") alpha = valueColor; } Color ret = Color.FromArgb(alpha, red, green, blue); return ret; } else { string[] splitted = val.Split(' '); if (splitted.Count() == 4) { CultureInfo ci = new CultureInfo(CultureInfo.CurrentCulture.Name, false); NumberFormatInfo formatInfo = ci.NumberFormat; formatInfo.NumberDecimalSeparator = "."; formatInfo.NumberGroupSeparator = ","; int red = (int)Math.Round(float.Parse(splitted[0], formatInfo) * 255); int green = (int)Math.Round(float.Parse(splitted[1], formatInfo) * 255); int blue = (int)Math.Round(float.Parse(splitted[2], formatInfo) * 255); int alpha = (int)Math.Round(float.Parse(splitted[3], formatInfo) * 255); Color ret = Color.FromArgb(alpha, red, green, blue); return ret; } else { return Color.White; } } } return base.ConvertFrom(context, culture, value); } } #endregion #region UIFilenameEditor public class UIFilenameEditor : System.Drawing.Design.UITypeEditor { static private string strDataRoot = null; static private string strDataRootSubst = null; static public void setDataRoot(string _root) { strDataRoot = _root; strDataRootSubst = strDataRoot; ProcessStartInfo cmdStartInfo = new ProcessStartInfo(); cmdStartInfo.FileName = @"cmd.exe"; cmdStartInfo.RedirectStandardOutput = true; cmdStartInfo.RedirectStandardInput = true; cmdStartInfo.UseShellExecute = false; cmdStartInfo.CreateNoWindow = true; Process cmdProcess = new Process(); cmdProcess.StartInfo = cmdStartInfo; cmdProcess.OutputDataReceived += cmd_DataReceived; cmdProcess.Start(); cmdProcess.BeginOutputReadLine(); cmdProcess.EnableRaisingEvents = true; cmdProcess.StandardInput.WriteLine("subst"); cmdProcess.EnableRaisingEvents = false; cmdProcess.StandardInput.WriteLine("exit"); cmdProcess.WaitForExit(); } static void cmd_DataReceived(object sender, DataReceivedEventArgs e) { // sample usefull line 'X:\\: => E:\\RL_DATA' (without quotes) string line = e.Data; string prefix = strDataRoot.Substring(0, 3) + ": => "; if (line != null && line.StartsWith(prefix)) { strDataRootSubst = line.Substring(prefix.Length); strDataRootSubst += strDataRoot.Substring(2); } } public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { if (context != null && context.Instance != null) { if (!context.PropertyDescriptor.IsReadOnly) { return UITypeEditorEditStyle.Modal; } } return UITypeEditorEditStyle.None; } private static string ReplaceEx(string original, string pattern, string replacement) { int count, position0, position1; count = position0 = position1 = 0; string upperString = original.ToUpper(); string upperPattern = pattern.ToUpper(); int inc = (original.Length / pattern.Length) * (replacement.Length - pattern.Length); char[] chars = new char[original.Length + Math.Max(0, inc)]; while ((position1 = upperString.IndexOf(upperPattern, position0)) != -1) { for (int i = position0; i < position1; ++i) chars[count++] = original[i]; for (int i = 0; i < replacement.Length; ++i) chars[count++] = replacement[i]; position0 = position1 + pattern.Length; } if (position0 == 0) return original; for (int i = position0; i < original.Length; ++i) chars[count++] = original[i]; return new string(chars, 0, count); } [RefreshProperties(RefreshProperties.All)] public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value) { if (context == null || provider == null || context.Instance == null) { return base.EditValue(provider, value); } FileDialog fileDlg; if (context.PropertyDescriptor.Attributes[typeof(SaveFileAttribute)] == null) { fileDlg = new OpenFileDialog(); } else { fileDlg = new SaveFileDialog(); } fileDlg.Title = "Select " + context.PropertyDescriptor.DisplayName; String fileValue = (String)value; int last = fileValue.LastIndexOf('/'); if (last != -1) { fileDlg.InitialDirectory = strDataRoot + fileValue.Substring(0, last + 1); fileDlg.InitialDirectory = fileDlg.InitialDirectory.Replace("/", "\\"); fileDlg.FileName = fileValue.Substring(last + 1); } else { fileDlg.InitialDirectory = strDataRoot; fileDlg.FileName = ""; } FileDialogFilterAttribute filterAtt = (FileDialogFilterAttribute)context.PropertyDescriptor.Attributes[typeof(FileDialogFilterAttribute)]; if (filterAtt != null) { fileDlg.Filter = filterAtt.Filter; } if (fileDlg.ShowDialog() == DialogResult.OK) { fileValue = fileDlg.FileName; fileValue = ReplaceEx(fileValue, strDataRootSubst, ""); fileValue = ReplaceEx(fileValue, strDataRoot, ""); fileValue = fileValue.Replace("\\", "/"); fileValue = fileValue.ToLower(); value = fileValue; } fileDlg.Dispose(); return value; } [AttributeUsage(AttributeTargets.Property)] public class FileDialogFilterAttribute : Attribute { private string _filter; public string Filter { get { return this._filter; } } public FileDialogFilterAttribute(string filter) { this._filter = filter; } } [AttributeUsage(AttributeTargets.Property)] public class SaveFileAttribute : Attribute { } public enum FileDialogType { LoadFileDialog, SaveFileDialog } } #endregion }