2 // System.Windows.Forms.FolderBrowserDialog.cs
5 // Dennis Hayes (dennish@raytek.com)
6 // Implemented by Jordi Mas i Hernàndez (jmas@softcatala.org)
8 // (C) 2002-3 Ximian, Inc
11 using System.Runtime.InteropServices;
14 namespace System.Windows.Forms {
19 using System.Runtime.Remoting;
20 using System.ComponentModel;
22 // Beta specs do not specify what class to defrive from.
23 // Using CommonDialog because
24 public class FolderBrowserDialog : CommonDialog {
28 Environment.SpecialFolder folder;
29 bool bShowNewFolderButton;
31 private IntPtr SpecialFolderConv(Environment.SpecialFolder fldr) {
33 IntPtr nRslt = IntPtr.Zero;
37 case Environment.SpecialFolder.ApplicationData:
38 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_APPDATA;
41 case Environment.SpecialFolder.CommonApplicationData:
42 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COMMON_APPDATA;
45 case Environment.SpecialFolder.CommonProgramFiles:
46 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COMMON_PROGRAMS;
49 case Environment.SpecialFolder.Cookies:
50 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COOKIES;
54 case Environment.SpecialFolder.Desktop:
55 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_DESKTOP;
58 //case Environment.SpecialFolder.MyComputer: //TODO: Which value?
59 //nRslt = (IntPtr) ShellSpecialFolder.;
63 case Environment.SpecialFolder.DesktopDirectory:
64 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_DESKTOPDIRECTORY;
67 case Environment.SpecialFolder.Favorites:
68 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_FAVORITES;
71 case Environment.SpecialFolder.History:
72 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_HISTORY;
75 case Environment.SpecialFolder.InternetCache:
76 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_INTERNET_CACHE;
79 case Environment.SpecialFolder.LocalApplicationData:
80 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_LOCAL_APPDATA;
83 case Environment.SpecialFolder.MyMusic:
84 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_MYMUSIC;
87 case Environment.SpecialFolder.MyPictures:
88 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_MYPICTURES;
91 case Environment.SpecialFolder.Personal:
92 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PERSONAL;
95 case Environment.SpecialFolder.ProgramFiles:
96 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PROGRAM_FILES;
99 case Environment.SpecialFolder.Programs:
100 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PROGRAMS;
103 case Environment.SpecialFolder.Recent:
104 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_RECENT;
107 case Environment.SpecialFolder.SendTo:
108 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_SENDTO;
111 case Environment.SpecialFolder.StartMenu:
112 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_STARTMENU;
115 case Environment.SpecialFolder.Startup:
116 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_STARTUP;
119 case Environment.SpecialFolder.System:
120 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_SYSTEM;
123 case Environment.SpecialFolder.Templates:
124 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_TEMPLATES;
128 throw new InvalidEnumArgumentException();
137 public FolderBrowserDialog() {
143 public override void Reset(){
148 folder = Environment.SpecialFolder.Desktop;
150 folder = Environment.SpecialFolder.DesktopDirectory;
152 bShowNewFolderButton = true;
156 protected override bool RunDialog(IntPtr hWndOwner){
158 BROWSEINFO bi = new BROWSEINFO();
159 IntPtr pidl = IntPtr.Zero;
160 IntPtr olePath = Marshal.AllocHGlobal(2048);
163 bi.hwndOwner = hWndOwner;
164 bi.pidlRoot = (IntPtr) SpecialFolderConv(RootFolder);
165 bi.lpszTitle = Description;
166 bi.ulFlags = (uint) (BrowseDirFlags.BIF_RETURNONLYFSDIRS | BrowseDirFlags.BIF_STATUSTEXT);
167 bi.lpfn = IntPtr.Zero;
168 bi.lParam = IntPtr.Zero;
170 bi.pszDisplayName = olePath;
171 pidl = Win32.SHBrowseForFolder(ref bi);
173 Marshal.FreeHGlobal(olePath);
175 if (pidl==IntPtr.Zero) return false;
177 StringBuilder sBuilder = new StringBuilder();
179 nRet = Win32.SHGetPathFromIDList(pidl, sBuilder);
181 // TODO: Dealocate the pidl returned by Win32.SHBrowseForFolder
182 // Win32.SHFreeMalloc(pidl);
184 if (nRet==0) return false;
186 selectedPath = sBuilder.ToString();
191 // --- Public Properties
194 public string Description {
195 get {return description;}
196 set {description = value;}
199 //beta docs do not have accessor.
200 //protected bool DesignMode {
203 //protected EventHandlerList Events {
206 public Environment.SpecialFolder RootFolder {
208 set {folder = value;}
211 public string SelectedPath {
212 get {return selectedPath;}
213 set {selectedPath = value;}
216 public bool ShowNewFolderButton {
217 get {return bShowNewFolderButton;}
218 set {bShowNewFolderButton = value;}
221 //public virtual System.ComponentModel.IContainer Container {
223 // throw new NotImplementedException ();
227 // FIXME: beta 1.1 says the following should be public virtual ISite Site {
228 // but the compiler gives warning that it must be new.
229 // Probably system.component needs to change to be beta 1.1 compliant
230 // looks fixed on 9/28/2003
231 public virtual ISite Site {
233 throw new NotImplementedException ();
240 //Specs seem to say they need to be here, but adding them conflicts with commondialog : component.disposed/helprequest
241 //public event EventHandler Disposed;
242 //public event EventHandler HelpRequest;