Mercurial > projects > dwt-linux
annotate dwt/browser/FilePicker.d @ 345:5abc6f7f7a95
Fixups
author | John Reimer <terminal.node@gmail.com> |
---|---|
date | Tue, 28 Oct 2008 22:07:01 -0700 |
parents | 8198e6052012 |
children | 9a4d7706df52 |
rev | line source |
---|---|
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
1 /******************************************************************************* |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2003, 2007 IBM Corporation and others. |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
3 * All rights reserved. This program and the accompanying materials |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
4 * are made available under the terms of the Eclipse Public License v1.0 |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
5 * which accompanies this distribution, and is available at |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
6 * http://www.eclipse.org/legal/epl-v10.html |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
7 * |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
8 * Contributors: |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
9 * IBM Corporation - initial API and implementation |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
10 * Port to the D programming language: |
286 | 11 * John Reimer <terminal.node@gmail.com> |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
12 *******************************************************************************/ |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
13 module dwt.browser.FilePicker; |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
14 |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
15 import dwt.dwthelper.utils; |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
16 |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
17 import dwt.DWT; |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
18 |
341
942da4b6558a
Ongoing fixup for compile
John Reimer <terminal.node@gmail.com>
parents:
286
diff
changeset
|
19 import XPCOM = dwt.internal.mozilla.XPCOM; |
942da4b6558a
Ongoing fixup for compile
John Reimer <terminal.node@gmail.com>
parents:
286
diff
changeset
|
20 |
344 | 21 import dwt.internal.mozilla.Common; |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
22 import dwt.internal.mozilla.nsEmbedString; |
344 | 23 import dwt.internal.mozilla.nsISupports; |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
24 import dwt.internal.mozilla.nsID; |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
25 import dwt.internal.mozilla.nsIFilePicker; |
286 | 26 import dwt.internal.mozilla.nsIFilePicker_1_8; |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
27 import dwt.internal.mozilla.nsILocalFile; |
344 | 28 import dwt.internal.mozilla.nsIFileURL; |
29 import dwt.internal.mozilla.nsIDOMWindow; | |
30 import dwt.internal.mozilla.nsISimpleEnumerator; | |
31 import dwt.internal.mozilla.nsStringAPI; | |
32 | |
345 | 33 import dwt.browser.Mozilla; |
34 | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
35 import dwt.widgets.DirectoryDialog; |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
36 import dwt.widgets.Display; |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
37 import dwt.widgets.FileDialog; |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
38 import dwt.widgets.Shell; |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
39 |
286 | 40 class FilePicker : nsIFilePicker { |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
41 |
286 | 42 int refCount = 0; |
43 short mode; | |
44 nsIDOMWindow parentHandle; | |
45 String[] files, masks; | |
46 String defaultFilename, directory, title; | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
47 |
286 | 48 static final String SEPARATOR = System.getProperty ("file.separator"); //$NON-NLS-1$ |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
49 |
286 | 50 this () { |
51 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
52 |
286 | 53 nsrefcnt AddRef () { |
54 refCount++; | |
55 return refCount; | |
56 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
57 |
286 | 58 nsresult QueryInterface (nsID* riid, void** ppvObject) { |
345 | 59 if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE; |
286 | 60 |
345 | 61 if (*riid == nsISupports.IID) { |
286 | 62 *ppvObject = cast(void*)cast(nsISupports) this; |
63 AddRef (); | |
345 | 64 return XPCOM.NS_OK; |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
65 } |
345 | 66 if (*riid == nsIFilePicker.IID) { |
286 | 67 *ppvObject = cast(void*)cast(nsIFilePicker) this; |
68 AddRef (); | |
345 | 69 return XPCOM.NS_OK; |
286 | 70 } |
345 | 71 if (*riid == nsIFilePicker_1_8.IID) { |
286 | 72 *ppvObject = cast(void*)cast(nsIFilePicker_1_8) this; |
73 AddRef (); | |
345 | 74 return XPCOM.NS_OK; |
286 | 75 } |
76 *ppvObject = null; | |
345 | 77 return XPCOM.NS_ERROR_NO_INTERFACE; |
286 | 78 } |
79 | |
80 nsrefcnt Release () { | |
81 refCount--; | |
82 if (refCount is 0) return 0; | |
83 return refCount; | |
84 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
85 |
286 | 86 /* |
87 * As of Mozilla 1.8 some of nsIFilePicker's string arguments changed type. This method | |
88 * answers a java string based on the type of string that is appropriate for the Mozilla | |
89 * version being used. | |
90 */ | |
344 | 91 String parseAString (nsAString* string) { |
286 | 92 return null; |
93 } | |
94 | |
95 /* nsIFilePicker */ | |
96 | |
97 nsresult Init (nsIDOMWindow parent, nsAString* title, PRInt16 mode) { | |
98 parentHandle = parent; | |
99 this.mode = mode; | |
100 this.title = parseAString (title); | |
101 return XPCOM.NS_OK; | |
102 } | |
103 | |
345 | 104 nsresult Show (PRInt16* _retval) { |
286 | 105 if (mode is nsIFilePicker.modeGetFolder) { |
106 /* picking a directory */ | |
107 int result = showDirectoryPicker (); | |
345 | 108 *_retval = cast(int)cast(PRInt16)result; /* PRInt16 */ |
286 | 109 return XPCOM.NS_OK; |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
110 } |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
111 |
286 | 112 /* picking a file */ |
113 int style = mode is nsIFilePicker.modeSave ? DWT.SAVE : DWT.OPEN; | |
114 if (mode is nsIFilePicker.modeOpenMultiple) style |= DWT.MULTI; | |
115 Display display = Display.getCurrent (); | |
116 Shell parent = null; // TODO compute parent | |
117 if (parent is null) { | |
118 parent = new Shell (display); | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
119 } |
286 | 120 FileDialog dialog = new FileDialog (parent, style); |
121 if (title !is null) dialog.setText (title); | |
122 if (directory !is null) dialog.setFilterPath (directory); | |
123 if (masks !is null) dialog.setFilterExtensions (masks); | |
124 if (defaultFilename !is null) dialog.setFileName (defaultFilename); | |
125 String filename = dialog.open (); | |
126 files = dialog.getFileNames (); | |
127 directory = dialog.getFilterPath (); | |
128 title = defaultFilename = null; | |
129 masks = null; | |
130 int result = filename is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK; | |
131 *_retval = cast(int)cast(short)result;; /* PRInt16 */ | |
132 return XPCOM.NS_OK; | |
133 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
134 |
286 | 135 int showDirectoryPicker () { |
136 Display display = Display.getCurrent (); | |
137 Shell parent = null; // TODO compute parent | |
138 if (parent is null) { | |
139 parent = new Shell (display); | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
140 } |
286 | 141 DirectoryDialog dialog = new DirectoryDialog (parent, DWT.NONE); |
142 if (title !is null) dialog.setText (title); | |
143 if (directory !is null) dialog.setFilterPath (directory); | |
144 directory = dialog.open (); | |
145 title = defaultFilename = null; | |
146 files = masks = null; | |
147 return directory is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK; | |
148 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
149 |
286 | 150 nsresult GetFiles (nsISimpleEnumerator* aFiles) { |
151 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
152 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
153 |
345 | 154 nsresult GetFileURL ( nsIFileURL* aFileURL ) { |
286 | 155 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; |
156 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
157 |
286 | 158 nsresult GetFile (nsILocalFile* aFile) { |
159 String filename = ""; //$NON-NLS-1$ | |
160 if (directory !is null) filename ~= directory ~ SEPARATOR; | |
161 if (files !is null && files.length > 0) filename ~= files[0]; | |
345 | 162 scope auto path = new nsEmbedString (toString16(filename)); |
286 | 163 int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)path, 1, aFile); |
164 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
165 if (aFile is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER); | |
166 return XPCOM.NS_OK; | |
167 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
168 |
286 | 169 nsresult SetDisplayDirectory (nsILocalFile aDisplayDirectory) { |
170 if (aDisplayDirectory is null) return XPCOM.NS_OK; | |
171 scope auto pathname = new nsEmbedCString(); | |
172 aDisplayDirectory.GetNativePath (cast(nsACString*)pathname); | |
173 // wchar[] chars = MozillaDelegate.mbcsToWcs (null, bytes); | |
174 directory = pathname.toString; | |
175 return XPCOM.NS_OK; | |
176 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
177 |
286 | 178 nsresult GetDisplayDirectory (nsILocalFile* aDisplayDirectory) { |
179 String directoryName = directory !is null ? directory : ""; //$NON-NLS-1$ | |
180 scope auto path = new nsEmbedString (Utf.toString16(directoryName)); | |
181 int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)path, 1, aDisplayDirectory); | |
182 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
183 if (aDisplayDirectory is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER); | |
184 return XPCOM.NS_OK; | |
185 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
186 |
286 | 187 nsresult SetFilterIndex (PRInt32 aFilterIndex) { |
188 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
189 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
190 |
286 | 191 nsresult GetFilterIndex (PRInt32* aFilterIndex) { |
192 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
193 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
194 |
286 | 195 nsresult SetDefaultExtension (nsAString* aDefaultExtension) { |
196 /* note that the type of argument 1 changed as of Mozilla 1.8 */ | |
197 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
198 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
199 |
286 | 200 nsresult GetDefaultExtension (nsAString* aDefaultExtension) { |
201 /* note that the type of argument 1 changed as of Mozilla 1.8 */ | |
202 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
203 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
204 |
286 | 205 nsresult SetDefaultString (nsAString* aDefaultString) { |
206 defaultFilename = parseAString (aDefaultString); | |
207 return XPCOM.NS_OK; | |
208 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
209 |
286 | 210 nsresult GetDefaultString (nsAString* aDefaultString) { |
211 /* note that the type of argument 1 changed as of Mozilla 1.8 */ | |
212 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
213 } | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
214 |
286 | 215 nsresult AppendFilter (nsAString* title, nsAString* filter) { |
216 /* note that the type of arguments 1 and 2 changed as of Mozilla 1.8 */ | |
217 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
218 } | |
219 | |
220 nsresult AppendFilters (PRInt32 filterMask) { | |
221 String[] addFilters = null; | |
222 switch (filterMask) { | |
223 case nsIFilePicker.filterAll: | |
224 case nsIFilePicker.filterApps: | |
225 masks = null; /* this is equivalent to no filter */ | |
226 break; | |
227 case nsIFilePicker.filterHTML: | |
228 addFilters[0] = "*.htm;*.html"; //$NON-NLS-1$ | |
229 break; | |
230 case nsIFilePicker.filterImages: | |
231 addFilters[0] = "*.gif;*.jpeg;*.jpg;*.png"; //$NON-NLS-1$ | |
232 break; | |
233 case nsIFilePicker.filterText: | |
234 addFilters[0] = "*.txt"; //$NON-NLS-1$ | |
235 break; | |
236 case nsIFilePicker.filterXML: | |
237 addFilters[0] = "*.xml"; //$NON-NLS-1$ | |
238 break; | |
239 case nsIFilePicker.filterXUL: | |
240 addFilters[0] = "*.xul"; //$NON-NLS-1$ | |
241 break; | |
242 } | |
243 if (masks is null) { | |
244 masks = addFilters; | |
245 } else { | |
246 if (addFilters !is null) { | |
247 masks ~= addFilters; | |
278
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
248 } |
93409d9838c5
Commit more browser/xpcom updates, including still uncoverted source.
John Reimer<terminal.node@gmail.com>
parents:
diff
changeset
|
249 } |
286 | 250 return XPCOM.NS_OK; |
251 } | |
252 } |