Mercurial > projects > dwt-linux
diff dwt/browser.old/FilePicker.d @ 288:4ee8c4237614
old branches... commit by mistake
author | John Reimer<terminal.node@gmail.com> |
---|---|
date | Tue, 05 Aug 2008 18:00:50 -0700 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwt/browser.old/FilePicker.d Tue Aug 05 18:00:50 2008 -0700 @@ -0,0 +1,453 @@ +/******************************************************************************* + * Copyright (c) 2003, 2007 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Port to the D programming language: + * John Reimer <terminal.node@gmail.com> + *******************************************************************************/ + +module dwt.browser.FilePicker; + +import tango.io.model.IFile; +import Utf = tango.text.convert.Utf; + +import dwt.dwthelper.utils; + +import dwt.DWT; + +import XPCOM = dwt.internal.mozilla.nsXPCOM; + +import dwt.browser.Mozilla; +import dwt.browser.MozillaDelegate; + +import dwt.internal.mozilla.nsEmbedString; +import dwt.internal.mozilla.nsIDOMWindow; +import dwt.internal.mozilla.nsID; +import dwt.internal.mozilla.nsIFilePicker; +import dwt.internal.mozilla.nsILocalFile; +import dwt.internal.mozilla.nsISupports; +import dwt.internal.mozilla.nsSimpleEnumerator; +import dwt.internal.mozilla.nsStringAPI; +import dwt.internal.mozilla.nsError; + +import dwt.widgets.DirectoryDialog; +import dwt.widgets.Display; +import dwt.widgets.FileDialog; +import dwt.widgets.Shell; + + +class FilePicker : nsISupports, nsIFilePicker +{ + /************************************************************************** + + **************************************************************************/ + + private + { + int _refCount = 0; + short _mode; + + nsIDOMWindow _parent; + + String[] _files; + String _masks; + String _defaultFilename, + _directory, + _title; + } + /************************************************************************** + + **************************************************************************/ + + static final String SEPARATOR = FileConst.FileSeparatorChar; + + /************************************************************************** + + **************************************************************************/ + + this () + { + } + + /************************************************************************** + + **************************************************************************/ + + nsrefcnt AddRef () + { + _refCount++; + return _refCount; + } + + /************************************************************************** + + **************************************************************************/ + + nsresult QueryInterface ( inout nsID riid, void** ppvObject) + { + if (riid is null || ppvObject is null) + return NS_ERROR_NO_INTERFACE; + + if (riid == nsISupports.IID) + { + *ppvObject = cast(void*)cast(nsISupports) this; + AddRef (); + return NS_OK; + } + + if (riid == nsIFilePicker.IID)) + { + *ppvObject = cast(void*)cast(nsIFilePicker) this; + AddRef (); + return NS_OK; + } + + if (riid == nsIFilePicker_1_8.IID) + { + *ppvObject = cast(void*)cast(nsIFilePicker_1_8) this; + AddRef (); + return NS_OK; + } + + *ppvObject = null; + return NS_ERROR_NO_INTERFACE; + } + + /************************************************************************** + + **************************************************************************/ + + nsrefcnt Release () + { + _refCount--; + + if (_refCount is 0) + // No big deal here; just allow the GC to reap this object + // once all references are expired. -JJR + // delete this; + return 0; + + return _refCount; + } + + /************************************************************************** + + **************************************************************************/ + + /* SWT Comment: + * + * As of Mozilla 1.8 some of nsIFilePicker's string arguments changed type. This method + * answers a java string based on the type of string that is appropriate for the Mozilla + * version being used. + */ + + override String parseAString (nsEmbedString str) + { + return null; + } + + /************************************************************************** + + **************************************************************************/ + + /* nsIFilePicker */ + + nsresult Init (nsIDOMWindow parent, nsAString* title, PRInt16 mode) + { + _parent = parent; + _mode = mode; + _title = parseAString (title); + return XPCOM; + } + + /************************************************************************** + + **************************************************************************/ + + nsresult Show (PRUint32* retval) + { + if (_mode is nsIFilePicker.modeGetFolder) + { + /* picking a directory */ + int result = this.showDirectoryPicker (); + *retval = result; + return NS_OK; + } + + /* picking a file */ + int style = _mode is nsIFilePicker.modeSave ? DWT.SAVE : DWT.OPEN; + + if (_mode is nsIFilePicker.modeOpenMultiple) + style |= DWT.MULTI; + + Display display = Display.getCurrent (); + Shell parent = null; // TODO compute parent + + if (parent is null) + { + parent = new Shell (display); + } + + FileDialog dialog = new FileDialog (parent, style); + + if (_title !is null) + dialog.setText (_title); + + if (_directory !is null) + dialog.setFilterPath (_directory); + + if (_masks !is null) { + String[] str ~= _masks; + dialog.setFilterExtensions ( str ); + } + + if (_defaultFilename !is null) + dialog.setFileName (_defaultFilename); + + String filename = dialog.open (); + files = dialog.getFileNames (); + _directory = dialog.getFilterPath (); + _title = _defaultFilename = null; + _masks = null; + int result = filename is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK; + *retval = result; + return NS_OK; + } + + /************************************************************************** + + **************************************************************************/ + + int showDirectoryPicker () + { + Display display = Display.getCurrent (); + Shell parent = null; // TODO compute parent + + if (parent is null) + { + parent = new Shell (display); + } + + DirectoryDialog dialog = new DirectoryDialog (parent, DWT.NONE); + + if (_title !is null) + dialog.setText (_title); + if (_directory !is null) + dialog.setFilterPath (_directory); + _directory = dialog.open (); + _title = _defaultFilename = null; + _files = _masks = null; + return _directory is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK; + } + + /************************************************************************** + + **************************************************************************/ + + nsresult GetFiles ( nsISimpleEnumerator* aFiles) + { + return NS_ERROR_NOT_IMPLEMENTED; + } + + /************************************************************************** + + **************************************************************************/ + + nsresult GetFileURL (nsIFileURL aFileURL) + { + return NS_ERROR_NOT_IMPLEMENTED; + } + + /************************************************************************** + + **************************************************************************/ + + nsresult GetFile (nsILocalFile* aFile) + { + String filename = ""; //$NON-NLS-1$ + + if (_directory !is null) + filename ~= _directory ~ SEPARATOR; + if (_files !is null && _files.length > 0) + filename ~= _files[0]; + + // Create a nsEmbedString which will be automatically disposed + // at the end of scope. + scope auto path = new nsEmbedString( filename ); + + nsresult rc = XPCOM.NS_NewLocalFile ( cast(nsAString*)path, 1, aFile); + + if (rc !is NS_OK) + Mozilla.error (rc); + if (aFile is null) + Mozilla.error (NS_ERROR_NULL_POINTER); + + return NS_OK; + } + + /************************************************************************** + + **************************************************************************/ + + nsresult SetDisplayDirectory (nsILocalFile aDisplayDirectory) + { + if (aDisplayDirectory is null) + return NS_OK; + + scope auto pathname = new nsEmbedCString; + aDisplayDirectory.GetNativePath ( cast(nsACString*)pathname ); + // TODO: CHECK IF THIS DOES CORRECT STRING CONVERSION -JJR + char[] buffer = pathname.toString(); + char[] chars = MozillaDelegate.mbcsToWcs (null, buffer); + // "buffer" contains a dup'ed string so we can safely reuse + // it's memory for the _directory member. -JJR + _directory = buffer; + return NS_OK; + } + + /************************************************************************** + + **************************************************************************/ + + nsresult GetDisplayDirectory (nsILocalFile* aDisplayDirectory) + { + String directoryName = _directory !is null ? _directory : ""; //$NON-NLS-1$ + scope auto path = new nsEmbedString (directoryName); + nsresult rc = XPCOM.NS_NewLocalFile ( cast(nsAString*)path, 1, aDisplayDirectory); + + if (rc !is NS_OK) + Mozilla.error (rc); + if (aDisplayDirectory is null) + Mozilla.error (NS_ERROR_NULL_POINTER); + return NS_OK; + } + + /************************************************************************** + + **************************************************************************/ + + nsresult SetFilterIndex (PRInt32 aFilterIndex) + { + return NS_ERROR_NOT_IMPLEMENTED; + } + + /************************************************************************** + + **************************************************************************/ + + nresult GetFilterIndex (PRInt32* aFilterIndex) + { + return NS_ERROR_NOT_IMPLEMENTED; + } + + /************************************************************************** + + **************************************************************************/ + + int SetDefaultExtension (nsAString* aDefaultExtension) + { + /* note that the type of argument 1 changed as of Mozilla 1.8 */ + return NS_ERROR_NOT_IMPLEMENTED; + } + + /************************************************************************** + + **************************************************************************/ + + int GetDefaultExtension (nsAString* aDefaultExtension) + { + /* note that the type of argument 1 changed as of Mozilla 1.8 */ + return NS_ERROR_NOT_IMPLEMENTED; + } + + /************************************************************************** + + **************************************************************************/ + + nresult SetDefaultString (nsAString* aDefaultString) + { + defaultFilename = parseAString (aDefaultString); + return NS_OK; + } + + /************************************************************************** + + **************************************************************************/ + + nresult GetDefaultString (nsAString* aDefaultString) + { + /* note that the type of argument 1 changed as of Mozilla 1.8 */ + return NS_ERROR_NOT_IMPLEMENTED; + } + + /************************************************************************** + + **************************************************************************/ + + nresult AppendFilter (nsAString* title, nsAString* filter) + { + /* note that the type of arguments 1 and 2 changed as of Mozilla 1.8 */ + return NS_ERROR_NOT_IMPLEMENTED; + } + + /************************************************************************** + + **************************************************************************/ + + nresult AppendFilters (PRInt32 filterMask) + { + String addFilters; + + switch (filterMask) + { + case nsIFilePicker.filterAll: + case nsIFilePicker.filterApps: + _masks = null; /* this is equivalent to no filter */ + break; + case nsIFilePicker.filterHTML: + addFilters = "*.htm;*.html" ; + break; + case nsIFilePicker.filterImages: + addFilters = "*.gif;*.jpeg;*.jpg;*.png"; + break; + case nsIFilePicker.filterText: + addFilters = "*.txt"; + break; + case nsIFilePicker.filterXML: + addFilters = "*.xml"; + break; + case nsIFilePicker.filterXUL: + addFilters = "*.xul"; + break; + } + + if (_masks is null) + { + _masks = addFilters; + } else { + if (addFilters !is null) + _masks ~= addFilters; + } + return NS_OK; + } + +/****************************************************************************** + + FilePicker for Mozilla Version 1.8 + +******************************************************************************/ + +class FilePicker_1_8 : FilePicker +{ + String parseAString (nsEmbedString str) + { + if (str is null) + return null; + return Utf.toString( str.toString16() ); + } +} \ No newline at end of file