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