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