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
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.InteropServices;
35 namespace System.Windows.Forms {
40 using System.Runtime.Remoting;
41 using System.ComponentModel;
43 // Beta specs do not specify what class to defrive from.
44 // Using CommonDialog because
45 public class FolderBrowserDialog : CommonDialog {
49 Environment.SpecialFolder folder;
50 bool bShowNewFolderButton;
52 private IntPtr SpecialFolderConv(Environment.SpecialFolder fldr) {
54 IntPtr nRslt = IntPtr.Zero;
58 case Environment.SpecialFolder.ApplicationData:
59 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_APPDATA;
62 case Environment.SpecialFolder.CommonApplicationData:
63 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COMMON_APPDATA;
66 case Environment.SpecialFolder.CommonProgramFiles:
67 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COMMON_PROGRAMS;
70 case Environment.SpecialFolder.Cookies:
71 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COOKIES;
75 case Environment.SpecialFolder.Desktop:
76 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_DESKTOP;
79 //case Environment.SpecialFolder.MyComputer: //TODO: Which value?
80 //nRslt = (IntPtr) ShellSpecialFolder.;
84 case Environment.SpecialFolder.DesktopDirectory:
85 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_DESKTOPDIRECTORY;
88 case Environment.SpecialFolder.Favorites:
89 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_FAVORITES;
92 case Environment.SpecialFolder.History:
93 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_HISTORY;
96 case Environment.SpecialFolder.InternetCache:
97 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_INTERNET_CACHE;
100 case Environment.SpecialFolder.LocalApplicationData:
101 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_LOCAL_APPDATA;
104 case Environment.SpecialFolder.MyMusic:
105 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_MYMUSIC;
108 case Environment.SpecialFolder.MyPictures:
109 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_MYPICTURES;
112 case Environment.SpecialFolder.Personal:
113 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PERSONAL;
116 case Environment.SpecialFolder.ProgramFiles:
117 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PROGRAM_FILES;
120 case Environment.SpecialFolder.Programs:
121 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PROGRAMS;
124 case Environment.SpecialFolder.Recent:
125 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_RECENT;
128 case Environment.SpecialFolder.SendTo:
129 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_SENDTO;
132 case Environment.SpecialFolder.StartMenu:
133 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_STARTMENU;
136 case Environment.SpecialFolder.Startup:
137 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_STARTUP;
140 case Environment.SpecialFolder.System:
141 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_SYSTEM;
144 case Environment.SpecialFolder.Templates:
145 nRslt = (IntPtr) ShellSpecialFolder.CSIDL_TEMPLATES;
149 throw new InvalidEnumArgumentException();
158 public FolderBrowserDialog() {
164 public override void Reset(){
169 folder = Environment.SpecialFolder.Desktop;
171 folder = Environment.SpecialFolder.DesktopDirectory;
173 bShowNewFolderButton = true;
177 protected override bool RunDialog(IntPtr hWndOwner){
179 BROWSEINFO bi = new BROWSEINFO();
180 IntPtr pidl = IntPtr.Zero;
181 IntPtr olePath = Marshal.AllocHGlobal(2048);
184 bi.hwndOwner = hWndOwner;
185 bi.pidlRoot = (IntPtr) SpecialFolderConv(RootFolder);
186 bi.lpszTitle = Description;
187 bi.ulFlags = (uint) (BrowseDirFlags.BIF_RETURNONLYFSDIRS | BrowseDirFlags.BIF_STATUSTEXT);
188 bi.lpfn = IntPtr.Zero;
189 bi.lParam = IntPtr.Zero;
191 bi.pszDisplayName = olePath;
192 pidl = Win32.SHBrowseForFolder(ref bi);
194 Marshal.FreeHGlobal(olePath);
196 if (pidl==IntPtr.Zero) return false;
198 StringBuilder sBuilder = new StringBuilder();
200 nRet = Win32.SHGetPathFromIDList(pidl, sBuilder);
202 // TODO: Dealocate the pidl returned by Win32.SHBrowseForFolder
203 // Win32.SHFreeMalloc(pidl);
205 if (nRet==0) return false;
207 selectedPath = sBuilder.ToString();
212 // --- Public Properties
215 public string Description {
216 get {return description;}
217 set {description = value;}
220 //beta docs do not have accessor.
221 //protected bool DesignMode {
224 //protected EventHandlerList Events {
227 public Environment.SpecialFolder RootFolder {
229 set {folder = value;}
232 public string SelectedPath {
233 get {return selectedPath;}
234 set {selectedPath = value;}
237 public bool ShowNewFolderButton {
238 get {return bShowNewFolderButton;}
239 set {bShowNewFolderButton = value;}
242 //public virtual System.ComponentModel.IContainer Container {
244 // throw new NotImplementedException ();
248 // FIXME: beta 1.1 says the following should be public virtual ISite Site {
249 // but the compiler gives warning that it must be new.
250 // Probably system.component needs to change to be beta 1.1 compliant
251 // looks fixed on 9/28/2003
252 public virtual ISite Site {
254 throw new NotImplementedException ();
261 //Specs seem to say they need to be here, but adding them conflicts with commondialog : component.disposed/helprequest
262 //public event EventHandler Disposed;
263 //public event EventHandler HelpRequest;