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