This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System.Runtime.InteropServices;
33 using System.Text;
34
35 namespace System.Windows.Forms {
36
37         // <summary>
38         //
39         // </summary>
40         using System.Runtime.Remoting;
41         using System.ComponentModel;
42
43         // Beta specs do not specify what class to defrive from.
44         // Using CommonDialog because 
45         public class FolderBrowserDialog : CommonDialog  {
46
47                 string description;
48                 string selectedPath;
49                 Environment.SpecialFolder folder;
50                 bool bShowNewFolderButton;              
51                 
52                 private IntPtr SpecialFolderConv(Environment.SpecialFolder fldr) {
53                         
54                         IntPtr nRslt = IntPtr.Zero;
55                         
56                         switch (fldr) 
57                         {
58                                 case Environment.SpecialFolder.ApplicationData:
59                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_APPDATA;
60                                         break;
61                                 
62                                 case Environment.SpecialFolder.CommonApplicationData:
63                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COMMON_APPDATA;
64                                         break;
65                                         
66                                 case Environment.SpecialFolder.CommonProgramFiles:
67                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COMMON_PROGRAMS;
68                                         break;
69                                 
70                                 case Environment.SpecialFolder.Cookies:
71                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_COOKIES;
72                                         break;
73                                 
74 #if NET_1_1
75                                 case Environment.SpecialFolder.Desktop:
76                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_DESKTOP;
77                                         break;
78                                         
79                                 //case Environment.SpecialFolder.MyComputer: //TODO: Which value?
80                                         //nRslt = (IntPtr) ShellSpecialFolder.;
81                                         //break;
82 #endif
83                                 
84                                 case Environment.SpecialFolder.DesktopDirectory:
85                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_DESKTOPDIRECTORY;
86                                         break;
87                                         
88                                 case Environment.SpecialFolder.Favorites:
89                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_FAVORITES;
90                                         break;
91                                 
92                                 case Environment.SpecialFolder.History:
93                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_HISTORY;
94                                         break;
95                                         
96                                 case Environment.SpecialFolder.InternetCache:
97                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_INTERNET_CACHE;
98                                         break;
99                                 
100                                 case Environment.SpecialFolder.LocalApplicationData:
101                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_LOCAL_APPDATA;
102                                         break;
103                                         
104                                 case Environment.SpecialFolder.MyMusic:
105                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_MYMUSIC;
106                                         break;
107                                         
108                                 case Environment.SpecialFolder.MyPictures:
109                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_MYPICTURES;
110                                         break;
111                                 
112                                 case Environment.SpecialFolder.Personal:
113                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PERSONAL;
114                                         break;
115                                         
116                                 case Environment.SpecialFolder.ProgramFiles:
117                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PROGRAM_FILES;
118                                         break;
119                                 
120                                 case Environment.SpecialFolder.Programs:
121                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_PROGRAMS;
122                                         break;
123                                         
124                                 case Environment.SpecialFolder.Recent:
125                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_RECENT;
126                                         break;
127                                 
128                                 case Environment.SpecialFolder.SendTo:
129                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_SENDTO;
130                                         break;
131                                         
132                                 case Environment.SpecialFolder.StartMenu:
133                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_STARTMENU;
134                                         break;
135                                         
136                                 case Environment.SpecialFolder.Startup:
137                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_STARTUP;
138                                         break;
139                                         
140                                 case Environment.SpecialFolder.System:
141                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_SYSTEM;
142                                         break;
143                                         
144                                 case Environment.SpecialFolder.Templates:
145                                         nRslt = (IntPtr) ShellSpecialFolder.CSIDL_TEMPLATES;
146                                         break;                                                                                                  
147                                         
148                                 default:
149                                         throw new  InvalidEnumArgumentException();      
150                         }       
151                         
152                         return nRslt;
153                 }
154                 //
155                 //  --- Constructor
156                 //
157                 
158                 public FolderBrowserDialog() {          
159                                 
160                                 Reset();                
161                 }
162
163                 
164                 public override void Reset(){
165                         
166                         description = "";
167                         selectedPath  = "";
168 #if NET_1_1
169                         folder = Environment.SpecialFolder.Desktop;             
170 #else
171                         folder = Environment.SpecialFolder.DesktopDirectory;            
172 #endif
173                         bShowNewFolderButton = true;            
174                 }
175
176                 
177                 protected override bool RunDialog(IntPtr hWndOwner){
178                         
179                         BROWSEINFO              bi = new BROWSEINFO();
180                         IntPtr                  pidl = IntPtr.Zero;                     
181                         IntPtr olePath = Marshal.AllocHGlobal(2048);                                            
182                         int nRet = 0;                   
183                         
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;
190                         bi.iImage  = 0;                                 
191                         bi.pszDisplayName = olePath;
192                         pidl = Win32.SHBrowseForFolder(ref bi);
193                         
194                     Marshal.FreeHGlobal(olePath);
195                     
196                         if (pidl==IntPtr.Zero) return false;
197                         
198                         StringBuilder sBuilder = new StringBuilder();
199                         
200                         nRet = Win32.SHGetPathFromIDList(pidl, sBuilder);
201                                 
202                         // TODO: Dealocate the pidl returned by Win32.SHBrowseForFolder
203                         //      Win32.SHFreeMalloc(pidl);                                               
204                                                     
205             if (nRet==0) return false;   
206                         
207                         selectedPath = sBuilder.ToString();
208                         return true;
209                 }
210
211                 //
212                 //  --- Public Properties
213                 //
214
215                 public string Description {
216                         get {return description;}
217                         set {description = value;}
218                 }
219
220                 //beta docs do not have accessor.
221                 //protected bool DesignMode {
222                 //}
223
224                 //protected EventHandlerList Events {
225                 //}
226
227                 public Environment.SpecialFolder RootFolder {
228                         get {return folder;}
229                         set {folder = value;}
230                 }
231
232                 public string SelectedPath {
233                         get {return selectedPath;}
234                         set {selectedPath = value;}
235                 }
236
237                 public bool ShowNewFolderButton {
238                         get {return bShowNewFolderButton;}
239                         set {bShowNewFolderButton = value;}
240                 }
241
242                 //public virtual System.ComponentModel.IContainer Container {
243                 //      get {
244                 //              throw new NotImplementedException ();
245                 //      }
246                 //}
247
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 {
253                         get {
254                                 throw new NotImplementedException ();
255                         }
256                         set {
257                                 //FIXME:
258                         }
259                 }
260
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;
264
265         }
266 }
267
268
269
270
271
272
273