From 4e29727dfcafe240e2f4c6c02870c32deb8305e2 Mon Sep 17 00:00:00 2001 From: cltbld Date: Tue, 19 Oct 2004 21:52:38 +0000 Subject: [PATCH] Backout of GTK2 filepicker checkin. git-svn-id: svn://10.0.0.236/branches/AVIARY_1_0_20040515_BRANCH@164053 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/widget/src/gtk2/Makefile.in | 1 - mozilla/widget/src/gtk2/nsWidgetFactory.cpp | 94 ------ mozilla/widget/src/xpwidgets/Makefile.in | 2 +- .../widget/src/xpwidgets/nsBaseFilePicker.h | 2 + .../components/filepicker/src/Makefile.in | 2 - .../components/filepicker/src/nsFilePicker.js | 315 ++++++++++++++++++ .../filepicker/src/nsFilePicker.js.in | 4 - 7 files changed, 318 insertions(+), 102 deletions(-) create mode 100644 mozilla/xpfe/components/filepicker/src/nsFilePicker.js diff --git a/mozilla/widget/src/gtk2/Makefile.in b/mozilla/widget/src/gtk2/Makefile.in index 91175d8442b..fe9b05e06b1 100644 --- a/mozilla/widget/src/gtk2/Makefile.in +++ b/mozilla/widget/src/gtk2/Makefile.in @@ -62,7 +62,6 @@ CPPSRCS = \ nsGtkMozRemoteHelper.cpp \ nsClipboard.cpp \ nsDragService.cpp \ - nsFilePicker.cpp \ nsSound.cpp \ nsNativeKeyBindings.cpp \ $(NULL) diff --git a/mozilla/widget/src/gtk2/nsWidgetFactory.cpp b/mozilla/widget/src/gtk2/nsWidgetFactory.cpp index 5ff93b6972e..a8d07df4e9f 100644 --- a/mozilla/widget/src/gtk2/nsWidgetFactory.cpp +++ b/mozilla/widget/src/gtk2/nsWidgetFactory.cpp @@ -48,23 +48,10 @@ #include "nsHTMLFormatConverter.h" #include "nsClipboard.h" #include "nsDragService.h" -#include "nsFilePicker.h" #include "nsSound.h" #include "nsBidiKeyboard.h" #include "nsNativeKeyBindings.h" -#include "nsIComponentRegistrar.h" -#include "nsComponentManagerUtils.h" -#include "nsAutoPtr.h" -#include - -/* from nsFilePicker.js */ -#define XULFILEPICKER_CID \ - { 0x54ae32f8, 0x1dd2, 0x11b2, \ - { 0xa2, 0x09, 0xdf, 0x7c, 0x50, 0x53, 0x70, 0xf8} } -static NS_DEFINE_CID(kXULFilePickerCID, XULFILEPICKER_CID); -static NS_DEFINE_CID(kNativeFilePickerCID, NS_FILEPICKER_CID); - NS_GENERIC_FACTORY_CONSTRUCTOR(nsWindow) NS_GENERIC_FACTORY_CONSTRUCTOR(nsChildWindow) NS_GENERIC_FACTORY_CONSTRUCTOR(nsAppShell) @@ -122,81 +109,6 @@ nsNativeKeyBindingsTextAreaConstructor(nsISupports *aOuter, REFNSIID aIID, eKeyBindings_TextArea); } -static NS_IMETHODIMP -nsFilePickerConstructor(nsISupports *aOuter, REFNSIID aIID, - void **aResult) -{ - *aResult = nsnull; - if (aOuter != nsnull) { - return NS_ERROR_NO_AGGREGATION; - } - - nsCOMPtr picker; - PRBool enabled = PR_FALSE; - - /* need pref fu */ - - if (enabled && gtk_check_version(2,4,0) == NULL) { - picker = new nsFilePicker; - } else { - picker = do_CreateInstance(kXULFilePickerCID); - } - - if (!picker) { - return NS_ERROR_OUT_OF_MEMORY; - } - - return picker->QueryInterface(aIID, aResult); -} - -static NS_IMETHODIMP -nsFilePickerRegisterSelf(nsIComponentManager *aCompMgr, - nsIFile *aPath, - const char *aLoaderStr, - const char *aType, - const nsModuleComponentInfo *aInfo) -{ - static PRBool been_here; - if (!been_here) { - been_here = PR_TRUE; - - // Make sure we re-register this so it gets picked up after the XUL picker. - return NS_ERROR_FACTORY_REGISTER_AGAIN; - } - - nsresult rv; - nsCOMPtr compReg = do_QueryInterface(aCompMgr, &rv); - - if (NS_SUCCEEDED(rv) && compReg) { - rv = compReg->RegisterFactoryLocation(kNativeFilePickerCID, - "Gtk2 File Picker", - "@mozilla.org/filepicker;1", - aPath, aLoaderStr, aType); - } - - return rv; -} - -static NS_IMETHODIMP -nsFilePickerUnregisterSelf(nsIComponentManager *aCompMgr, - nsIFile *aPath, - const char *aLoaderStr, - const nsModuleComponentInfo *aInfo) -{ - nsFilePicker::Shutdown(); - - nsresult rv; - nsCOMPtr compReg = do_QueryInterface(aCompMgr, &rv); - - if (NS_SUCCEEDED(rv) && compReg) { - rv = compReg->UnregisterFactoryLocation(kNativeFilePickerCID, - aPath); - } - - return rv; -} - - static const nsModuleComponentInfo components[] = { { "Gtk2 Window", @@ -215,12 +127,6 @@ static const nsModuleComponentInfo components[] = NS_LOOKANDFEEL_CID, "@mozilla.org/widget/lookandfeel;1", nsLookAndFeelConstructor }, - { "Gtk2 File Picker", - NS_FILEPICKER_CID, - "@mozilla.org/filepicker;1", - nsFilePickerConstructor, - nsFilePickerRegisterSelf, - nsFilePickerUnregisterSelf }, { "Gtk2 Sound", NS_SOUND_CID, "@mozilla.org/sound;1", diff --git a/mozilla/widget/src/xpwidgets/Makefile.in b/mozilla/widget/src/xpwidgets/Makefile.in index 179fdb446ab..716711e8012 100644 --- a/mozilla/widget/src/xpwidgets/Makefile.in +++ b/mozilla/widget/src/xpwidgets/Makefile.in @@ -62,7 +62,7 @@ ifneq (,$(filter beos os2 mac cocoa windows,$(MOZ_WIDGET_TOOLKIT))) CPPSRCS += nsBaseClipboard.cpp endif -ifneq (,$(filter beos gtk2 os2 mac cocoa photon windows,$(MOZ_WIDGET_TOOLKIT))) +ifneq (,$(filter beos os2 mac cocoa photon windows,$(MOZ_WIDGET_TOOLKIT))) CPPSRCS += nsBaseFilePicker.cpp REQUIRES += docshell view intl endif diff --git a/mozilla/widget/src/xpwidgets/nsBaseFilePicker.h b/mozilla/widget/src/xpwidgets/nsBaseFilePicker.h index 6b48842fbd7..a8d55225cc8 100644 --- a/mozilla/widget/src/xpwidgets/nsBaseFilePicker.h +++ b/mozilla/widget/src/xpwidgets/nsBaseFilePicker.h @@ -49,6 +49,8 @@ protected: virtual void InitNative(nsIWidget *aParent, const nsAString& aTitle, PRInt16 aMode) = 0; +private: + nsIWidget *DOMWindowToWidget(nsIDOMWindow *dw); }; diff --git a/mozilla/xpfe/components/filepicker/src/Makefile.in b/mozilla/xpfe/components/filepicker/src/Makefile.in index 24396667ade..9257cb3e254 100644 --- a/mozilla/xpfe/components/filepicker/src/Makefile.in +++ b/mozilla/xpfe/components/filepicker/src/Makefile.in @@ -56,5 +56,3 @@ include $(topsrcdir)/config/rules.mk EXTRA_DSO_LDOPTS += $(MOZ_COMPONENT_LIBS) -nsFilePicker.js: nsFilePicker.js.in - $(PERL) $(MOZILLA_DIR)/config/preprocessor.pl $(DEFINES) $(ACDEFINES) $^ > $@ diff --git a/mozilla/xpfe/components/filepicker/src/nsFilePicker.js b/mozilla/xpfe/components/filepicker/src/nsFilePicker.js new file mode 100644 index 00000000000..5e283106d1f --- /dev/null +++ b/mozilla/xpfe/components/filepicker/src/nsFilePicker.js @@ -0,0 +1,315 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 2000 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): Stuart Parmenter + */ + +/* + * No magic constructor behaviour, as is de rigeur for XPCOM. + * If you must perform some initialization, and it could possibly fail (even + * due to an out-of-memory condition), you should use an Init method, which + * can convey failure appropriately (thrown exception in JS, + * NS_FAILED(nsresult) return in C++). + * + * In JS, you can actually cheat, because a thrown exception will cause the + * CreateInstance call to fail in turn, but not all languages are so lucky. + * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code + * for portability reasons -- and even when you're building completely + * platform-specific code, you can't throw across an XPCOM method boundary.) + */ + + +const DEBUG = false; /* set to true to enable debug messages */ + +const FILEPICKER_CONTRACTID = "@mozilla.org/filepicker;1"; +const FILEPICKER_CID = Components.ID("{54ae32f8-1dd2-11b2-a209-df7c505370f8}"); +const LOCAL_FILE_CONTRACTID = "@mozilla.org/file/local;1"; +const APPSHELL_SERV_CONTRACTID = "@mozilla.org/appshell/appShellService;1"; +const STRBUNDLE_SERV_CONTRACTID = "@mozilla.org/intl/stringbundle;1"; + +const nsIAppShellService = Components.interfaces.nsIAppShellService; +const nsILocalFile = Components.interfaces.nsILocalFile; +const nsIFileURL = Components.interfaces.nsIFileURL; +const nsISupports = Components.interfaces.nsISupports; +const nsIFactory = Components.interfaces.nsIFactory; +const nsIFilePicker = Components.interfaces.nsIFilePicker; +const nsIInterfaceRequestor = Components.interfaces.nsIInterfaceRequestor +const nsIDOMWindow = Components.interfaces.nsIDOMWindow; +const nsIStringBundleService = Components.interfaces.nsIStringBundleService; +const nsIWebNavigation = Components.interfaces.nsIWebNavigation; +const nsIDocShellTreeItem = Components.interfaces.nsIDocShellTreeItem; +const nsIBaseWindow = Components.interfaces.nsIBaseWindow; + +var bundle = null; +var lastDirectory = null; + +function nsFilePicker() +{ + if (!bundle) + bundle = srGetStrBundle("chrome://global/locale/filepicker.properties"); + + /* attributes */ + this.mDefaultString = ""; + this.mFilterIndex = 0; + if (lastDirectory) { + this.mDisplayDirectory = Components.classes[LOCAL_FILE_CONTRACTID].createInstance(nsILocalFile); + this.mDisplayDirectory.initWithPath(lastDirectory); + } else { + this.mDisplayDirectory = null; + } + this.mFilterTitles = new Array(); + this.mFilters = new Array(); +} + +nsFilePicker.prototype = { + + /* attribute nsILocalFile displayDirectory; */ + set displayDirectory(a) { this.mDisplayDirectory = a; }, + get displayDirectory() { return this.mDisplayDirectory; }, + + /* readonly attribute nsILocalFile file; */ + set file(a) { throw "readonly property"; }, + get file() { return this.mFilesEnumerator.mFiles[0]; }, + + /* readonly attribute nsISimpleEnumerator files; */ + set files(a) { throw "readonly property"; }, + get files() { return this.mFilesEnumerator; }, + + /* readonly attribute nsIFileURL fileURL; */ + set fileURL(a) { throw "readonly property"; }, + get fileURL() { + if (this.mFilesEnumerator) { + var ioService = Components.classes["@mozilla.org/network/io-service;1"] + .getService(Components.interfaces.nsIIOService); + var url = ioService.newFileURI(this.file); + return url; + } + return null; + }, + + /* attribute wstring defaultString; */ + set defaultString(a) { this.mDefaultString = a; }, + get defaultString() { return this.mDefaultString; }, + + /* attribute wstring defaultExtension */ + set defaultExtension(ext) { }, + get defaultExtension() { return ""; }, + + /* attribute long filterIndex; */ + set filterIndex(a) { this.mFilterIndex = a; }, + get filterIndex() { return this.mFilterIndex; }, + + /* members */ + mFilesEnumerator: undefined, + mParentWindow: null, + + /* methods */ + init: function(parent, title, mode) { + this.mParentWindow = parent; + this.mTitle = title; + this.mMode = mode; + }, + + appendFilters: function(filterMask) { + if (filterMask & nsIFilePicker.filterHTML) { + this.appendFilter(bundle.GetStringFromName("htmlTitle"), + bundle.GetStringFromName("htmlFilter")); + } + if (filterMask & nsIFilePicker.filterText) { + this.appendFilter(bundle.GetStringFromName("textTitle"), + bundle.GetStringFromName("textFilter")); + } + if (filterMask & nsIFilePicker.filterImages) { + this.appendFilter(bundle.GetStringFromName("imageTitle"), + bundle.GetStringFromName("imageFilter")); + } + if (filterMask & nsIFilePicker.filterXML) { + this.appendFilter(bundle.GetStringFromName("xmlTitle"), + bundle.GetStringFromName("xmlFilter")); + } + if (filterMask & nsIFilePicker.filterXUL) { + this.appendFilter(bundle.GetStringFromName("xulTitle"), + bundle.GetStringFromName("xulFilter")); + } + if (filterMask & nsIFilePicker.filterApps) { + // We use "..apps" as a special filter for executable files + this.appendFilter(bundle.GetStringFromName("appsTitle"), + "..apps"); + } + if (filterMask & nsIFilePicker.filterAll) { + this.appendFilter(bundle.GetStringFromName("allTitle"), + bundle.GetStringFromName("allFilter")); + } + }, + + appendFilter: function(title, extensions) { + this.mFilterTitles.push(title); + this.mFilters.push(extensions); + }, + + QueryInterface: function(iid) { + if (!iid.equals(nsIFilePicker) && + !iid.equals(nsISupports)) + throw Components.results.NS_ERROR_NO_INTERFACE; + return this; + }, + + show: function() { + var o = new Object(); + o.title = this.mTitle; + o.mode = this.mMode; + o.displayDirectory = this.mDisplayDirectory; + o.defaultString = this.mDefaultString; + o.filterIndex = this.mFilterIndex; + o.filters = new Object(); + o.filters.titles = this.mFilterTitles; + o.filters.types = this.mFilters; + o.retvals = new Object(); + + var parent; + if (this.mParentWindow) { + parent = this.mParentWindow; + } else if (typeof(window) == "object" && window != null) { + parent = window; + } else { + try { + var appShellService = Components.classes[APPSHELL_SERV_CONTRACTID].getService(nsIAppShellService); + parent = appShellService.hiddenDOMWindow; + } catch(ex) { + debug("Can't get parent. xpconnect hates me so we can't get one from the appShellService.\n"); + debug(ex + "\n"); + } + } + + var parentWin = null; + try { + parentWin = parent.QueryInterface(nsIInterfaceRequestor) + .getInterface(nsIWebNavigation) + .QueryInterface(nsIDocShellTreeItem) + .treeOwner + .QueryInterface(nsIInterfaceRequestor) + .getInterface(nsIBaseWindow); + } catch(ex) { + dump("file picker couldn't get base window\n"+ex+"\n"); + } + try { + if (parentWin) + parentWin.blurSuppression = true; + parent.openDialog("chrome://global/content/filepicker.xul", + "", + "chrome,modal,titlebar,resizable=yes,dependent=yes", + o); + if (parentWin) + parentWin.blurSuppression = false; + + this.mFilterIndex = o.retvals.filterIndex; + this.mFilesEnumerator = o.retvals.files; + lastDirectory = o.retvals.directory; + return o.retvals.buttonStatus; + } catch(ex) { dump("unable to open file picker\n" + ex + "\n"); } + + return null; + } +} + +if (DEBUG) + debug = function (s) { dump("-*- filepicker: " + s + "\n"); } +else + debug = function (s) {} + +/* module foo */ + +var filePickerModule = new Object(); + +filePickerModule.registerSelf = +function (compMgr, fileSpec, location, type) +{ + debug("registering (all right -- a JavaScript module!)"); + compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar); + + compMgr.registerFactoryLocation(FILEPICKER_CID, + "FilePicker JS Component", + FILEPICKER_CONTRACTID, + fileSpec, + location, + type); +} + +filePickerModule.getClassObject = +function (compMgr, cid, iid) { + if (!cid.equals(FILEPICKER_CID)) + throw Components.results.NS_ERROR_NO_INTERFACE; + + if (!iid.equals(Components.interfaces.nsIFactory)) + throw Components.results.NS_ERROR_NOT_IMPLEMENTED; + + return filePickerFactory; +} + +filePickerModule.canUnload = +function(compMgr) +{ + debug("Unloading component."); + return true; +} + +/* factory object */ +var filePickerFactory = new Object(); + +filePickerFactory.createInstance = +function (outer, iid) { + debug("CI: " + iid); + debug("IID:" + nsIFilePicker); + if (outer != null) + throw Components.results.NS_ERROR_NO_AGGREGATION; + + return (new nsFilePicker()).QueryInterface(iid); +} + +/* entrypoint */ +function NSGetModule(compMgr, fileSpec) { + return filePickerModule; +} + + + +/* crap from strres.js that I want to use for string bundles since I can't include another .js file.... */ + +var strBundleService = null; + +function srGetStrBundle(path) +{ + var strBundle = null; + + if (!strBundleService) { + try { + strBundleService = Components.classes[STRBUNDLE_SERV_CONTRACTID].getService(nsIStringBundleService); + } catch (ex) { + dump("\n--** strBundleService createInstance failed **--\n"); + return null; + } + } + + strBundle = strBundleService.createBundle(path); + if (!strBundle) { + dump("\n--** strBundle createInstance failed **--\n"); + } + return strBundle; +} + diff --git a/mozilla/xpfe/components/filepicker/src/nsFilePicker.js.in b/mozilla/xpfe/components/filepicker/src/nsFilePicker.js.in index 352e79a0fdf..5e283106d1f 100644 --- a/mozilla/xpfe/components/filepicker/src/nsFilePicker.js.in +++ b/mozilla/xpfe/components/filepicker/src/nsFilePicker.js.in @@ -245,11 +245,7 @@ function (compMgr, fileSpec, location, type) compMgr.registerFactoryLocation(FILEPICKER_CID, "FilePicker JS Component", -#ifndef MOZ_WIDGET_GTK2 FILEPICKER_CONTRACTID, -#else - "", -#endif fileSpec, location, type);