Mercurial > projects > dwt-linux
comparison dwt/browser.old/FilePicker.d @ 288:4ee8c4237614
old branches... commit by mistake
author | John Reimer<terminal.node@gmail.com> |
---|---|
date | Tue, 05 Aug 2008 18:00:50 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
287:9cbe6285f746 | 288:4ee8c4237614 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2003, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * John Reimer <terminal.node@gmail.com> | |
12 *******************************************************************************/ | |
13 | |
14 module dwt.browser.FilePicker; | |
15 | |
16 import tango.io.model.IFile; | |
17 import Utf = tango.text.convert.Utf; | |
18 | |
19 import dwt.dwthelper.utils; | |
20 | |
21 import dwt.DWT; | |
22 | |
23 import XPCOM = dwt.internal.mozilla.nsXPCOM; | |
24 | |
25 import dwt.browser.Mozilla; | |
26 import dwt.browser.MozillaDelegate; | |
27 | |
28 import dwt.internal.mozilla.nsEmbedString; | |
29 import dwt.internal.mozilla.nsIDOMWindow; | |
30 import dwt.internal.mozilla.nsID; | |
31 import dwt.internal.mozilla.nsIFilePicker; | |
32 import dwt.internal.mozilla.nsILocalFile; | |
33 import dwt.internal.mozilla.nsISupports; | |
34 import dwt.internal.mozilla.nsSimpleEnumerator; | |
35 import dwt.internal.mozilla.nsStringAPI; | |
36 import dwt.internal.mozilla.nsError; | |
37 | |
38 import dwt.widgets.DirectoryDialog; | |
39 import dwt.widgets.Display; | |
40 import dwt.widgets.FileDialog; | |
41 import dwt.widgets.Shell; | |
42 | |
43 | |
44 class FilePicker : nsISupports, nsIFilePicker | |
45 { | |
46 /************************************************************************** | |
47 | |
48 **************************************************************************/ | |
49 | |
50 private | |
51 { | |
52 int _refCount = 0; | |
53 short _mode; | |
54 | |
55 nsIDOMWindow _parent; | |
56 | |
57 String[] _files; | |
58 String _masks; | |
59 String _defaultFilename, | |
60 _directory, | |
61 _title; | |
62 } | |
63 /************************************************************************** | |
64 | |
65 **************************************************************************/ | |
66 | |
67 static final String SEPARATOR = FileConst.FileSeparatorChar; | |
68 | |
69 /************************************************************************** | |
70 | |
71 **************************************************************************/ | |
72 | |
73 this () | |
74 { | |
75 } | |
76 | |
77 /************************************************************************** | |
78 | |
79 **************************************************************************/ | |
80 | |
81 nsrefcnt AddRef () | |
82 { | |
83 _refCount++; | |
84 return _refCount; | |
85 } | |
86 | |
87 /************************************************************************** | |
88 | |
89 **************************************************************************/ | |
90 | |
91 nsresult QueryInterface ( inout nsID riid, void** ppvObject) | |
92 { | |
93 if (riid is null || ppvObject is null) | |
94 return NS_ERROR_NO_INTERFACE; | |
95 | |
96 if (riid == nsISupports.IID) | |
97 { | |
98 *ppvObject = cast(void*)cast(nsISupports) this; | |
99 AddRef (); | |
100 return NS_OK; | |
101 } | |
102 | |
103 if (riid == nsIFilePicker.IID)) | |
104 { | |
105 *ppvObject = cast(void*)cast(nsIFilePicker) this; | |
106 AddRef (); | |
107 return NS_OK; | |
108 } | |
109 | |
110 if (riid == nsIFilePicker_1_8.IID) | |
111 { | |
112 *ppvObject = cast(void*)cast(nsIFilePicker_1_8) this; | |
113 AddRef (); | |
114 return NS_OK; | |
115 } | |
116 | |
117 *ppvObject = null; | |
118 return NS_ERROR_NO_INTERFACE; | |
119 } | |
120 | |
121 /************************************************************************** | |
122 | |
123 **************************************************************************/ | |
124 | |
125 nsrefcnt Release () | |
126 { | |
127 _refCount--; | |
128 | |
129 if (_refCount is 0) | |
130 // No big deal here; just allow the GC to reap this object | |
131 // once all references are expired. -JJR | |
132 // delete this; | |
133 return 0; | |
134 | |
135 return _refCount; | |
136 } | |
137 | |
138 /************************************************************************** | |
139 | |
140 **************************************************************************/ | |
141 | |
142 /* SWT Comment: | |
143 * | |
144 * As of Mozilla 1.8 some of nsIFilePicker's string arguments changed type. This method | |
145 * answers a java string based on the type of string that is appropriate for the Mozilla | |
146 * version being used. | |
147 */ | |
148 | |
149 override String parseAString (nsEmbedString str) | |
150 { | |
151 return null; | |
152 } | |
153 | |
154 /************************************************************************** | |
155 | |
156 **************************************************************************/ | |
157 | |
158 /* nsIFilePicker */ | |
159 | |
160 nsresult Init (nsIDOMWindow parent, nsAString* title, PRInt16 mode) | |
161 { | |
162 _parent = parent; | |
163 _mode = mode; | |
164 _title = parseAString (title); | |
165 return XPCOM; | |
166 } | |
167 | |
168 /************************************************************************** | |
169 | |
170 **************************************************************************/ | |
171 | |
172 nsresult Show (PRUint32* retval) | |
173 { | |
174 if (_mode is nsIFilePicker.modeGetFolder) | |
175 { | |
176 /* picking a directory */ | |
177 int result = this.showDirectoryPicker (); | |
178 *retval = result; | |
179 return NS_OK; | |
180 } | |
181 | |
182 /* picking a file */ | |
183 int style = _mode is nsIFilePicker.modeSave ? DWT.SAVE : DWT.OPEN; | |
184 | |
185 if (_mode is nsIFilePicker.modeOpenMultiple) | |
186 style |= DWT.MULTI; | |
187 | |
188 Display display = Display.getCurrent (); | |
189 Shell parent = null; // TODO compute parent | |
190 | |
191 if (parent is null) | |
192 { | |
193 parent = new Shell (display); | |
194 } | |
195 | |
196 FileDialog dialog = new FileDialog (parent, style); | |
197 | |
198 if (_title !is null) | |
199 dialog.setText (_title); | |
200 | |
201 if (_directory !is null) | |
202 dialog.setFilterPath (_directory); | |
203 | |
204 if (_masks !is null) { | |
205 String[] str ~= _masks; | |
206 dialog.setFilterExtensions ( str ); | |
207 } | |
208 | |
209 if (_defaultFilename !is null) | |
210 dialog.setFileName (_defaultFilename); | |
211 | |
212 String filename = dialog.open (); | |
213 files = dialog.getFileNames (); | |
214 _directory = dialog.getFilterPath (); | |
215 _title = _defaultFilename = null; | |
216 _masks = null; | |
217 int result = filename is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK; | |
218 *retval = result; | |
219 return NS_OK; | |
220 } | |
221 | |
222 /************************************************************************** | |
223 | |
224 **************************************************************************/ | |
225 | |
226 int showDirectoryPicker () | |
227 { | |
228 Display display = Display.getCurrent (); | |
229 Shell parent = null; // TODO compute parent | |
230 | |
231 if (parent is null) | |
232 { | |
233 parent = new Shell (display); | |
234 } | |
235 | |
236 DirectoryDialog dialog = new DirectoryDialog (parent, DWT.NONE); | |
237 | |
238 if (_title !is null) | |
239 dialog.setText (_title); | |
240 if (_directory !is null) | |
241 dialog.setFilterPath (_directory); | |
242 _directory = dialog.open (); | |
243 _title = _defaultFilename = null; | |
244 _files = _masks = null; | |
245 return _directory is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK; | |
246 } | |
247 | |
248 /************************************************************************** | |
249 | |
250 **************************************************************************/ | |
251 | |
252 nsresult GetFiles ( nsISimpleEnumerator* aFiles) | |
253 { | |
254 return NS_ERROR_NOT_IMPLEMENTED; | |
255 } | |
256 | |
257 /************************************************************************** | |
258 | |
259 **************************************************************************/ | |
260 | |
261 nsresult GetFileURL (nsIFileURL aFileURL) | |
262 { | |
263 return NS_ERROR_NOT_IMPLEMENTED; | |
264 } | |
265 | |
266 /************************************************************************** | |
267 | |
268 **************************************************************************/ | |
269 | |
270 nsresult GetFile (nsILocalFile* aFile) | |
271 { | |
272 String filename = ""; //$NON-NLS-1$ | |
273 | |
274 if (_directory !is null) | |
275 filename ~= _directory ~ SEPARATOR; | |
276 if (_files !is null && _files.length > 0) | |
277 filename ~= _files[0]; | |
278 | |
279 // Create a nsEmbedString which will be automatically disposed | |
280 // at the end of scope. | |
281 scope auto path = new nsEmbedString( filename ); | |
282 | |
283 nsresult rc = XPCOM.NS_NewLocalFile ( cast(nsAString*)path, 1, aFile); | |
284 | |
285 if (rc !is NS_OK) | |
286 Mozilla.error (rc); | |
287 if (aFile is null) | |
288 Mozilla.error (NS_ERROR_NULL_POINTER); | |
289 | |
290 return NS_OK; | |
291 } | |
292 | |
293 /************************************************************************** | |
294 | |
295 **************************************************************************/ | |
296 | |
297 nsresult SetDisplayDirectory (nsILocalFile aDisplayDirectory) | |
298 { | |
299 if (aDisplayDirectory is null) | |
300 return NS_OK; | |
301 | |
302 scope auto pathname = new nsEmbedCString; | |
303 aDisplayDirectory.GetNativePath ( cast(nsACString*)pathname ); | |
304 // TODO: CHECK IF THIS DOES CORRECT STRING CONVERSION -JJR | |
305 char[] buffer = pathname.toString(); | |
306 char[] chars = MozillaDelegate.mbcsToWcs (null, buffer); | |
307 // "buffer" contains a dup'ed string so we can safely reuse | |
308 // it's memory for the _directory member. -JJR | |
309 _directory = buffer; | |
310 return NS_OK; | |
311 } | |
312 | |
313 /************************************************************************** | |
314 | |
315 **************************************************************************/ | |
316 | |
317 nsresult GetDisplayDirectory (nsILocalFile* aDisplayDirectory) | |
318 { | |
319 String directoryName = _directory !is null ? _directory : ""; //$NON-NLS-1$ | |
320 scope auto path = new nsEmbedString (directoryName); | |
321 nsresult rc = XPCOM.NS_NewLocalFile ( cast(nsAString*)path, 1, aDisplayDirectory); | |
322 | |
323 if (rc !is NS_OK) | |
324 Mozilla.error (rc); | |
325 if (aDisplayDirectory is null) | |
326 Mozilla.error (NS_ERROR_NULL_POINTER); | |
327 return NS_OK; | |
328 } | |
329 | |
330 /************************************************************************** | |
331 | |
332 **************************************************************************/ | |
333 | |
334 nsresult SetFilterIndex (PRInt32 aFilterIndex) | |
335 { | |
336 return NS_ERROR_NOT_IMPLEMENTED; | |
337 } | |
338 | |
339 /************************************************************************** | |
340 | |
341 **************************************************************************/ | |
342 | |
343 nresult GetFilterIndex (PRInt32* aFilterIndex) | |
344 { | |
345 return NS_ERROR_NOT_IMPLEMENTED; | |
346 } | |
347 | |
348 /************************************************************************** | |
349 | |
350 **************************************************************************/ | |
351 | |
352 int SetDefaultExtension (nsAString* aDefaultExtension) | |
353 { | |
354 /* note that the type of argument 1 changed as of Mozilla 1.8 */ | |
355 return NS_ERROR_NOT_IMPLEMENTED; | |
356 } | |
357 | |
358 /************************************************************************** | |
359 | |
360 **************************************************************************/ | |
361 | |
362 int GetDefaultExtension (nsAString* aDefaultExtension) | |
363 { | |
364 /* note that the type of argument 1 changed as of Mozilla 1.8 */ | |
365 return NS_ERROR_NOT_IMPLEMENTED; | |
366 } | |
367 | |
368 /************************************************************************** | |
369 | |
370 **************************************************************************/ | |
371 | |
372 nresult SetDefaultString (nsAString* aDefaultString) | |
373 { | |
374 defaultFilename = parseAString (aDefaultString); | |
375 return NS_OK; | |
376 } | |
377 | |
378 /************************************************************************** | |
379 | |
380 **************************************************************************/ | |
381 | |
382 nresult GetDefaultString (nsAString* aDefaultString) | |
383 { | |
384 /* note that the type of argument 1 changed as of Mozilla 1.8 */ | |
385 return NS_ERROR_NOT_IMPLEMENTED; | |
386 } | |
387 | |
388 /************************************************************************** | |
389 | |
390 **************************************************************************/ | |
391 | |
392 nresult AppendFilter (nsAString* title, nsAString* filter) | |
393 { | |
394 /* note that the type of arguments 1 and 2 changed as of Mozilla 1.8 */ | |
395 return NS_ERROR_NOT_IMPLEMENTED; | |
396 } | |
397 | |
398 /************************************************************************** | |
399 | |
400 **************************************************************************/ | |
401 | |
402 nresult AppendFilters (PRInt32 filterMask) | |
403 { | |
404 String addFilters; | |
405 | |
406 switch (filterMask) | |
407 { | |
408 case nsIFilePicker.filterAll: | |
409 case nsIFilePicker.filterApps: | |
410 _masks = null; /* this is equivalent to no filter */ | |
411 break; | |
412 case nsIFilePicker.filterHTML: | |
413 addFilters = "*.htm;*.html" ; | |
414 break; | |
415 case nsIFilePicker.filterImages: | |
416 addFilters = "*.gif;*.jpeg;*.jpg;*.png"; | |
417 break; | |
418 case nsIFilePicker.filterText: | |
419 addFilters = "*.txt"; | |
420 break; | |
421 case nsIFilePicker.filterXML: | |
422 addFilters = "*.xml"; | |
423 break; | |
424 case nsIFilePicker.filterXUL: | |
425 addFilters = "*.xul"; | |
426 break; | |
427 } | |
428 | |
429 if (_masks is null) | |
430 { | |
431 _masks = addFilters; | |
432 } else { | |
433 if (addFilters !is null) | |
434 _masks ~= addFilters; | |
435 } | |
436 return NS_OK; | |
437 } | |
438 | |
439 /****************************************************************************** | |
440 | |
441 FilePicker for Mozilla Version 1.8 | |
442 | |
443 ******************************************************************************/ | |
444 | |
445 class FilePicker_1_8 : FilePicker | |
446 { | |
447 String parseAString (nsEmbedString str) | |
448 { | |
449 if (str is null) | |
450 return null; | |
451 return Utf.toString( str.toString16() ); | |
452 } | |
453 } |