2004-04-08 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / FolderBrowserDialog.cs
1 //
2 // System.Windows.Forms.FolderBrowserDialog.cs
3 //
4 // Author:
5 //   Dennis Hayes (dennish@raytek.com)
6 //       Implemented by Jordi Mas i Hernàndez (jmas@softcatala.org)
7 //
8 // (C) 2002-3 Ximian, Inc
9 //
10
11 using System.Runtime.InteropServices;
12 using System.Text;
13
14 namespace System.Windows.Forms {
15
16         // <summary>
17         //
18         // </summary>
19         using System.Runtime.Remoting;
20         using System.ComponentModel;
21
22         // Beta specs do not specify what class to defrive from.
23         // Using CommonDialog because 
24         public class FolderBrowserDialog : CommonDialog  {
25
26                 string description;
27                 string selectedPath;
28                 Environment.SpecialFolder folder;
29                 bool bShowNewFolderButton;              
30                 
31                 private IntPtr SpecialFolderConv(Environment.SpecialFolder fldr) {
32                         
33                         IntPtr nRslt = IntPtr.Zero;
34                         
35                         switch (fldr) 
36                         {
37                                 case Environment.SpecialFolder.ApplicationData:
38                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_APPDATA;
39                                         break;
40                                 
41                                 case Environment.SpecialFolder.CommonApplicationData:
42                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COMMON_APPDATA;
43                                         break;
44                                         
45                                 case Environment.SpecialFolder.CommonProgramFiles:
46                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COMMON_PROGRAMS;
47                                         break;
48                                 
49                                 case Environment.SpecialFolder.Cookies:
50                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COOKIES;
51                                         break;
52                                 
53 #if NET_1_1
54                                 case Environment.SpecialFolder.Desktop:
55                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_DESKTOP;
56                                         break;
57                                         
58                                 //case Environment.SpecialFolder.MyComputer: //TODO: Which value?
59                                         //nRslt = (IntPtr) ShellSpecialFolder.;
60                                         //break;
61 #endif
62                                 
63                                 case Environment.SpecialFolder.DesktopDirectory:
64                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_DESKTOPDIRECTORY;
65                                         break;
66                                         
67                                 case Environment.SpecialFolder.Favorites:
68                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_FAVORITES;
69                                         break;
70                                 
71                                 case Environment.SpecialFolder.History:
72                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_HISTORY;
73                                         break;
74                                         
75                                 case Environment.SpecialFolder.InternetCache:
76                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_INTERNET_CACHE;
77                                         break;
78                                 
79                                 case Environment.SpecialFolder.LocalApplicationData:
80                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_LOCAL_APPDATA;
81                                         break;
82                                         
83                                 case Environment.SpecialFolder.MyMusic:
84                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_MYMUSIC;
85                                         break;
86                                         
87                                 case Environment.SpecialFolder.MyPictures:
88                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_MYPICTURES;
89                                         break;
90                                 
91                                 case Environment.SpecialFolder.Personal:
92                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PERSONAL;
93                                         break;
94                                         
95                                 case Environment.SpecialFolder.ProgramFiles:
96                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PROGRAM_FILES;
97                                         break;
98                                 
99                                 case Environment.SpecialFolder.Programs:
100                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PROGRAMS;
101                                         break;
102                                         
103                                 case Environment.SpecialFolder.Recent:
104                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_RECENT;
105                                         break;
106                                 
107                                 case Environment.SpecialFolder.SendTo:
108                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_SENDTO;
109                                         break;
110                                         
111                                 case Environment.SpecialFolder.StartMenu:
112                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_STARTMENU;
113                                         break;
114                                         
115                                 case Environment.SpecialFolder.Startup:
116                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_STARTUP;
117                                         break;
118                                         
119                                 case Environment.SpecialFolder.System:
120                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_SYSTEM;
121                                         break;
122                                         
123                                 case Environment.SpecialFolder.Templates:
124                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_TEMPLATES;
125                                         break;                                                                                                  
126                                         
127                                 default:
128                                         throw new  InvalidEnumArgumentException();      
129                         }       
130                         
131                         return nRslt;
132                 }
133                 //
134                 //  --- Constructor
135                 //
136                 
137                 public FolderBrowserDialog() {          
138                                 
139                                 Reset();                
140                 }
141
142                 
143                 public override void Reset(){
144                         
145                         description = "";
146                         selectedPath  = "";
147 #if NET_1_1
148                         folder = Environment.SpecialFolder.Desktop;             
149 #else
150                         folder = Environment.SpecialFolder.DesktopDirectory;            
151 #endif
152                         bShowNewFolderButton = true;            
153                 }
154
155                 
156                 protected override bool RunDialog(IntPtr hWndOwner){
157                         
158                         BROWSEINFO              bi = new BROWSEINFO();
159                         IntPtr                  pidl = IntPtr.Zero;                     
160                         IntPtr olePath = Marshal.AllocHGlobal(2048);                                            
161                         int nRet = 0;                   
162                         
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;
169                         bi.iImage  = 0;                                 
170                         bi.pszDisplayName = olePath;
171                         pidl = Win32.SHBrowseForFolder(ref bi);
172                         
173                     Marshal.FreeHGlobal(olePath);
174                     
175                         if (pidl==IntPtr.Zero) return false;
176                         
177                         StringBuilder sBuilder = new StringBuilder();
178                         
179                         nRet = Win32.SHGetPathFromIDList(pidl, sBuilder);
180                                 
181                         // TODO: Dealocate the pidl returned by Win32.SHBrowseForFolder
182                         //      Win32.SHFreeMalloc(pidl);                                               
183                                                     
184             if (nRet==0) return false;   
185                         
186                         selectedPath = sBuilder.ToString();
187                         return true;
188                 }
189
190                 //
191                 //  --- Public Properties
192                 //
193
194                 public string Description {
195                         get {return description;}
196                         set {description = value;}
197                 }
198
199                 //beta docs do not have accessor.
200                 //protected bool DesignMode {
201                 //}
202
203                 //protected EventHandlerList Events {
204                 //}
205
206                 public Environment.SpecialFolder RootFolder {
207                         get {return folder;}
208                         set {folder = value;}
209                 }
210
211                 public string SelectedPath {
212                         get {return selectedPath;}
213                         set {selectedPath = value;}
214                 }
215
216                 public bool ShowNewFolderButton {
217                         get {return bShowNewFolderButton;}
218                         set {bShowNewFolderButton = value;}
219                 }
220
221                 //public virtual System.ComponentModel.IContainer Container {
222                 //      get {
223                 //              throw new NotImplementedException ();
224                 //      }
225                 //}
226
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 {
232                         get {
233                                 throw new NotImplementedException ();
234                         }
235                         set {
236                                 //FIXME:
237                         }
238                 }
239
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;
243
244         }
245 }
246
247
248
249
250
251
252