Mercurial > projects > dwt-mac
comparison dwt/browser/FilePicker.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 1a8b3cb347e0 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
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 *******************************************************************************/ | |
11 module dwt.browser.FilePicker; | |
12 | |
13 import dwt.dwthelper.utils; | |
14 | |
15 import dwt.DWT; | |
16 import dwt.internal.C; | |
17 import dwt.internal.mozilla.XPCOM; | |
18 import dwt.internal.mozilla.XPCOMObject; | |
19 import dwt.internal.mozilla.nsEmbedString; | |
20 import dwt.internal.mozilla.nsID; | |
21 import dwt.internal.mozilla.nsIFilePicker; | |
22 import dwt.internal.mozilla.nsIFilePicker_1_8; | |
23 import dwt.internal.mozilla.nsILocalFile; | |
24 import dwt.internal.mozilla.nsISupports; | |
25 import dwt.widgets.DirectoryDialog; | |
26 import dwt.widgets.Display; | |
27 import dwt.widgets.FileDialog; | |
28 import dwt.widgets.Shell; | |
29 | |
30 class FilePicker { | |
31 XPCOMObject supports; | |
32 XPCOMObject filePicker; | |
33 | |
34 int refCount = 0; | |
35 short mode; | |
36 int /*long*/ parentHandle; | |
37 String[] files, masks; | |
38 String defaultFilename, directory, title; | |
39 | |
40 static final String SEPARATOR = System.getProperty ("file.separator"); //$NON-NLS-1$ | |
41 | |
42 FilePicker () { | |
43 createCOMInterfaces (); | |
44 } | |
45 | |
46 int AddRef () { | |
47 refCount++; | |
48 return refCount; | |
49 } | |
50 | |
51 void createCOMInterfaces () { | |
52 /* Create each of the interfaces that this object implements */ | |
53 supports = new XPCOMObject (new int[] {2, 0, 0}) { | |
54 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);} | |
55 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();} | |
56 public int /*long*/ method2 (int /*long*/[] args) {return Release ();} | |
57 }; | |
58 | |
59 filePicker = new XPCOMObject (new int[] {2, 0, 0, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}) { | |
60 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);} | |
61 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();} | |
62 public int /*long*/ method2 (int /*long*/[] args) {return Release ();} | |
63 public int /*long*/ method3 (int /*long*/[] args) {return Init (args[0], args[1], (short)args[2]);} | |
64 public int /*long*/ method4 (int /*long*/[] args) {return AppendFilters ((int)/*64*/args[0]);} | |
65 public int /*long*/ method5 (int /*long*/[] args) {return AppendFilter (args[0], args[1]);} | |
66 public int /*long*/ method6 (int /*long*/[] args) {return GetDefaultString (args[0]);} | |
67 public int /*long*/ method7 (int /*long*/[] args) {return SetDefaultString (args[0]);} | |
68 public int /*long*/ method8 (int /*long*/[] args) {return GetDefaultExtension (args[0]);} | |
69 public int /*long*/ method9 (int /*long*/[] args) {return SetDefaultExtension (args[0]);} | |
70 public int /*long*/ method10 (int /*long*/[] args) {return GetFilterIndex (args[0]);} | |
71 public int /*long*/ method11 (int /*long*/[] args) {return SetFilterIndex ((int)/*64*/args[0]);} | |
72 public int /*long*/ method12 (int /*long*/[] args) {return GetDisplayDirectory (args[0]);} | |
73 public int /*long*/ method13 (int /*long*/[] args) {return SetDisplayDirectory (args[0]);} | |
74 public int /*long*/ method14 (int /*long*/[] args) {return GetFile (args[0]);} | |
75 public int /*long*/ method15 (int /*long*/[] args) {return GetFileURL (args[0]);} | |
76 public int /*long*/ method16 (int /*long*/[] args) {return GetFiles (args[0]);} | |
77 public int /*long*/ method17 (int /*long*/[] args) {return Show (args[0]);} | |
78 }; | |
79 } | |
80 | |
81 void disposeCOMInterfaces () { | |
82 if (supports !is null) { | |
83 supports.dispose (); | |
84 supports = null; | |
85 } | |
86 if (filePicker !is null) { | |
87 filePicker.dispose (); | |
88 filePicker = null; | |
89 } | |
90 } | |
91 | |
92 int /*long*/ getAddress () { | |
93 return filePicker.getAddress (); | |
94 } | |
95 | |
96 int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) { | |
97 if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE; | |
98 nsID guid = new nsID (); | |
99 XPCOM.memmove (guid, riid, nsID.sizeof); | |
100 | |
101 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) { | |
102 XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF); | |
103 AddRef (); | |
104 return XPCOM.NS_OK; | |
105 } | |
106 if (guid.Equals (nsIFilePicker.NS_IFILEPICKER_IID)) { | |
107 XPCOM.memmove(ppvObject, new int /*long*/[] {filePicker.getAddress ()}, C.PTR_SIZEOF); | |
108 AddRef (); | |
109 return XPCOM.NS_OK; | |
110 } | |
111 if (guid.Equals (nsIFilePicker_1_8.NS_IFILEPICKER_IID)) { | |
112 XPCOM.memmove(ppvObject, new int /*long*/[] {filePicker.getAddress ()}, C.PTR_SIZEOF); | |
113 AddRef (); | |
114 return XPCOM.NS_OK; | |
115 } | |
116 | |
117 XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF); | |
118 return XPCOM.NS_ERROR_NO_INTERFACE; | |
119 } | |
120 | |
121 int Release () { | |
122 refCount--; | |
123 if (refCount is 0) disposeCOMInterfaces (); | |
124 return refCount; | |
125 } | |
126 | |
127 /* | |
128 * As of Mozilla 1.8 some of nsIFilePicker's string arguments changed type. This method | |
129 * answers a java string based on the type of string that is appropriate for the Mozilla | |
130 * version being used. | |
131 */ | |
132 String parseAString (int /*long*/ string) { | |
133 return null; | |
134 } | |
135 | |
136 /* nsIFilePicker */ | |
137 | |
138 int Init (int /*long*/ parent, int /*long*/ title, short mode) { | |
139 parentHandle = parent; | |
140 this.mode = mode; | |
141 this.title = parseAString (title); | |
142 return XPCOM.NS_OK; | |
143 } | |
144 | |
145 int Show (int /*long*/ _retval) { | |
146 if (mode is nsIFilePicker.modeGetFolder) { | |
147 /* picking a directory */ | |
148 int result = showDirectoryPicker (); | |
149 XPCOM.memmove (_retval, new short[] {(short)result}, 2); /* PRInt16 */ | |
150 return XPCOM.NS_OK; | |
151 } | |
152 | |
153 /* picking a file */ | |
154 int style = mode is nsIFilePicker.modeSave ? DWT.SAVE : DWT.OPEN; | |
155 if (mode is nsIFilePicker.modeOpenMultiple) style |= DWT.MULTI; | |
156 Display display = Display.getCurrent (); | |
157 Shell parent = null; // TODO compute parent | |
158 if (parent is null) { | |
159 parent = new Shell (display); | |
160 } | |
161 FileDialog dialog = new FileDialog (parent, style); | |
162 if (title !is null) dialog.setText (title); | |
163 if (directory !is null) dialog.setFilterPath (directory); | |
164 if (masks !is null) dialog.setFilterExtensions (masks); | |
165 if (defaultFilename !is null) dialog.setFileName (defaultFilename); | |
166 String filename = dialog.open (); | |
167 files = dialog.getFileNames (); | |
168 directory = dialog.getFilterPath (); | |
169 title = defaultFilename = null; | |
170 masks = null; | |
171 int result = filename is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK; | |
172 XPCOM.memmove (_retval, new short[] {(short)result}, 2); /* PRInt16 */ | |
173 return XPCOM.NS_OK; | |
174 } | |
175 | |
176 int showDirectoryPicker () { | |
177 Display display = Display.getCurrent (); | |
178 Shell parent = null; // TODO compute parent | |
179 if (parent is null) { | |
180 parent = new Shell (display); | |
181 } | |
182 DirectoryDialog dialog = new DirectoryDialog (parent, DWT.NONE); | |
183 if (title !is null) dialog.setText (title); | |
184 if (directory !is null) dialog.setFilterPath (directory); | |
185 directory = dialog.open (); | |
186 title = defaultFilename = null; | |
187 files = masks = null; | |
188 return directory is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK; | |
189 } | |
190 | |
191 int GetFiles (int /*long*/ aFiles) { | |
192 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
193 } | |
194 | |
195 int GetFileURL (int /*long*/ aFileURL) { | |
196 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
197 } | |
198 | |
199 int GetFile (int /*long*/ aFile) { | |
200 String filename = ""; //$NON-NLS-1$ | |
201 if (directory !is null) filename += directory + SEPARATOR; | |
202 if (files !is null && files.length > 0) filename += files[0]; | |
203 nsEmbedString path = new nsEmbedString (filename); | |
204 int /*long*/[] file = new int /*long*/[1]; | |
205 int rc = XPCOM.NS_NewLocalFile (path.getAddress (), 1, file); | |
206 path.dispose (); | |
207 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
208 if (file[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER); | |
209 XPCOM.memmove (aFile, file, C.PTR_SIZEOF); | |
210 return XPCOM.NS_OK; | |
211 } | |
212 | |
213 int SetDisplayDirectory (int /*long*/ aDisplayDirectory) { | |
214 if (aDisplayDirectory is 0) return XPCOM.NS_OK; | |
215 nsILocalFile file = new nsILocalFile (aDisplayDirectory); | |
216 int /*long*/ pathname = XPCOM.nsEmbedCString_new (); | |
217 file.GetNativePath (pathname); | |
218 int length = XPCOM.nsEmbedCString_Length (pathname); | |
219 int /*long*/ buffer = XPCOM.nsEmbedCString_get (pathname); | |
220 byte[] bytes = new byte[length]; | |
221 XPCOM.memmove (bytes, buffer, length); | |
222 XPCOM.nsEmbedCString_delete (pathname); | |
223 char[] chars = MozillaDelegate.mbcsToWcs (null, bytes); | |
224 directory = new String (chars); | |
225 return XPCOM.NS_OK; | |
226 } | |
227 | |
228 int GetDisplayDirectory (int /*long*/ aDisplayDirectory) { | |
229 String directoryName = directory !is null ? directory : ""; //$NON-NLS-1$ | |
230 nsEmbedString path = new nsEmbedString (directoryName); | |
231 int /*long*/[] file = new int /*long*/[1]; | |
232 int rc = XPCOM.NS_NewLocalFile (path.getAddress (), 1, file); | |
233 path.dispose (); | |
234 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
235 if (file[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER); | |
236 XPCOM.memmove (aDisplayDirectory, file, C.PTR_SIZEOF); | |
237 return XPCOM.NS_OK; | |
238 } | |
239 | |
240 int SetFilterIndex (int aFilterIndex) { | |
241 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
242 } | |
243 | |
244 int GetFilterIndex (int /*long*/ aFilterIndex) { | |
245 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
246 } | |
247 | |
248 int SetDefaultExtension (int /*long*/ aDefaultExtension) { | |
249 /* note that the type of argument 1 changed as of Mozilla 1.8 */ | |
250 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
251 } | |
252 | |
253 int GetDefaultExtension (int /*long*/ aDefaultExtension) { | |
254 /* note that the type of argument 1 changed as of Mozilla 1.8 */ | |
255 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
256 } | |
257 | |
258 int SetDefaultString (int /*long*/ aDefaultString) { | |
259 defaultFilename = parseAString (aDefaultString); | |
260 return XPCOM.NS_OK; | |
261 } | |
262 | |
263 int GetDefaultString (int /*long*/ aDefaultString) { | |
264 /* note that the type of argument 1 changed as of Mozilla 1.8 */ | |
265 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
266 } | |
267 | |
268 int AppendFilter (int /*long*/ title, int /*long*/ filter) { | |
269 /* note that the type of arguments 1 and 2 changed as of Mozilla 1.8 */ | |
270 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
271 } | |
272 | |
273 int AppendFilters (int filterMask) { | |
274 String[] addFilters = null; | |
275 switch (filterMask) { | |
276 case nsIFilePicker.filterAll: | |
277 case nsIFilePicker.filterApps: | |
278 masks = null; /* this is equivalent to no filter */ | |
279 break; | |
280 case nsIFilePicker.filterHTML: | |
281 addFilters = new String[] {"*.htm;*.html"}; //$NON-NLS-1$ | |
282 break; | |
283 case nsIFilePicker.filterImages: | |
284 addFilters = new String[] {"*.gif;*.jpeg;*.jpg;*.png"}; //$NON-NLS-1$ | |
285 break; | |
286 case nsIFilePicker.filterText: | |
287 addFilters = new String[] {"*.txt"}; //$NON-NLS-1$ | |
288 break; | |
289 case nsIFilePicker.filterXML: | |
290 addFilters = new String[] {"*.xml"}; //$NON-NLS-1$ | |
291 break; | |
292 case nsIFilePicker.filterXUL: | |
293 addFilters = new String[] {"*.xul"}; //$NON-NLS-1$ | |
294 break; | |
295 } | |
296 if (masks is null) { | |
297 masks = addFilters; | |
298 } else { | |
299 if (addFilters !is null) { | |
300 String[] newFilters = new String[masks.length + addFilters.length]; | |
301 System.arraycopy (masks, 0, newFilters, 0, masks.length); | |
302 System.arraycopy (addFilters, 0, newFilters, masks.length, addFilters.length); | |
303 masks = newFilters; | |
304 } | |
305 } | |
306 return XPCOM.NS_OK; | |
307 } | |
308 } |