/*============================================================================= Project: SharpImage Module: siPropertyGridFilenameHelper.cs Language: C# Author: Dan Mueller Date: $Date: 2007-07-06 10:57:00 +1000 (Fri, 06 Jul 2007) $ Revision: $Revision: 2 $ Copyright (c) Queensland University of Technology (QUT) 2007. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =============================================================================*/ using System; using System.IO; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Design; using System.ComponentModel; using System.ComponentModel.Design; using System.Windows.Forms; namespace SharpImage.Forms { public class siOpenFilenameTypeConverter : TypeConverter { public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(System.String) && value is String) { String sFileName = value as String; return Path.GetFileName(sFileName); } return base.ConvertTo(context, culture, value, destinationType); } } public class siOpenFilenameEditor : System.Drawing.Design.UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { if (context != null && context.Instance != null) return UITypeEditorEditStyle.Modal; else return base.GetEditStyle(context); } public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { if (context == null || provider == null || context.Instance == null) return base.EditValue(provider, value); // Setup the dialog OpenFileDialog ofd = new OpenFileDialog(); ofd.CheckFileExists = true; ofd.CheckPathExists = true; ofd.Multiselect = false; ofd.ShowHelp = false; ofd.Title = "Select " + context.PropertyDescriptor.DisplayName; ofd.FileName = value as String; // Set the filter foreach (Attribute attrib in context.PropertyDescriptor.Attributes) { if (attrib is siFilenameEditorFilterAttribute) { ofd.Filter = (attrib as siFilenameEditorFilterAttribute).Filter; ofd.FilterIndex = (attrib as siFilenameEditorFilterAttribute).FilterIndex; } } // Show the dialog if (ofd.ShowDialog(provider as IWin32Window) == DialogResult.OK) value = ofd.FileName; // Clean up and return ofd.Dispose(); return value; } } [AttributeUsage(AttributeTargets.Property)] public class siFilenameEditorFilterAttribute : Attribute { private string m_Filter = string.Empty; private int m_FilterIndex = 0; public siFilenameEditorFilterAttribute(string filter) : base() { this.m_Filter = filter; } public siFilenameEditorFilterAttribute(string filter, int index) : base() { this.m_Filter = filter; this.m_FilterIndex = index; } public string Filter { get { return this.m_Filter; } } public int FilterIndex { get { return this.m_FilterIndex; } } } }