clean up to use generic factory constructors. _so_ much cleaner this way. r=pavlov,shaver

This commit is contained in:
blizzard%redhat.com 2000-03-24 00:00:22 +00:00
Родитель 252fbc969d
Коммит c0ddb79044
1 изменённых файлов: 129 добавлений и 184 удалений

Просмотреть файл

@ -12,209 +12,154 @@
*
* 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) 1998 Netscape Communications Corporation. All
* Rights Reserved.
* The Initial Developer of the Original Code is Christopher
* Blizzard. Portions created by Christopher Blizzard are
* Copyright (C) 2000 Christopher Blizzard. All Rights Reserved.
*
* Contributor(s):
* Christopher Blizzzard <blizzard@mozilla.org>
*/
#include "nscore.h"
#include "nsIFactory.h"
#include "nsISupports.h"
#include "nsIGenericFactory.h"
#include "nsIModule.h"
#include "nsCOMPtr.h"
#include "nsGfxCIID.h"
#include "nsFontMetricsGTK.h"
#include "nsRenderingContextGTK.h"
#include "nsImageGTK.h"
#include "nsDeviceContextGTK.h"
#include "nsRegionGTK.h"
#include "nsScriptableRegion.h"
#include "nsBlender.h"
#include "nsRenderingContextGTK.h"
// aka nsDeviceContextSpecGTK.h
#include "nsDeviceContextSpecG.h"
#include "nsDeviceContextSpecFactoryG.h"
#include "nsIDeviceContextSpecPS.h"
#include "nsIImageManager.h"
// aka nsDeviceContextSpecFactoryGTK.h
#include "nsDeviceContextSpecFactoryG.h"
#include "nsScreenManagerGtk.h"
#include <gtk/gtk.h>
#include "nsBlender.h"
#include "nsScriptableRegion.h"
#include "nsIImageManager.h"
static NS_DEFINE_IID(kCFontMetrics, NS_FONT_METRICS_CID);
static NS_DEFINE_IID(kCFontEnumerator, NS_FONT_ENUMERATOR_CID);
static NS_DEFINE_IID(kCRenderingContext, NS_RENDERING_CONTEXT_CID);
static NS_DEFINE_IID(kCImage, NS_IMAGE_CID);
static NS_DEFINE_IID(kCDeviceContext, NS_DEVICE_CONTEXT_CID);
static NS_DEFINE_IID(kCRegion, NS_REGION_CID);
static NS_DEFINE_IID(kCScriptableRegion, NS_SCRIPTABLE_REGION_CID);
// objects that just require generic constructors
static NS_DEFINE_IID(kCBlender, NS_BLENDER_CID);
NS_GENERIC_FACTORY_CONSTRUCTOR(nsFontMetricsGTK)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsDeviceContextGTK)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsRenderingContextGTK)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsImageGTK)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsBlender)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsRegionGTK)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsDeviceContextSpecGTK)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsDeviceContextSpecFactoryGTK)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsFontEnumeratorGTK)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsScreenManagerGtk)
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
// our custom constructors
static NS_DEFINE_IID(kCDeviceContextSpec, NS_DEVICE_CONTEXT_SPEC_CID);
static NS_DEFINE_IID(kCDeviceContextSpecFactory, NS_DEVICE_CONTEXT_SPEC_FACTORY_CID);
static NS_DEFINE_IID(kImageManagerImpl, NS_IMAGEMANAGER_CID);
static NS_DEFINE_IID(kCScreenManager, NS_SCREENMANAGER_CID);
static nsresult nsScriptableRegionConstructor(nsISupports *aOuter, REFNSIID aIID, void **aResult)
{
nsresult rv;
nsIScriptableRegion *inst;
class nsGfxFactoryGTK : public nsIFactory
{
public:
// nsISupports methods
NS_DECL_ISUPPORTS
NS_DECL_NSIFACTORY
nsGfxFactoryGTK(const nsCID &aClass);
virtual ~nsGfxFactoryGTK();
private:
nsCID mClassID;
};
nsGfxFactoryGTK::nsGfxFactoryGTK(const nsCID &aClass)
{
NS_INIT_ISUPPORTS();
mClassID = aClass;
}
nsGfxFactoryGTK::~nsGfxFactoryGTK()
{
}
nsresult nsGfxFactoryGTK::QueryInterface(const nsIID &aIID,
void **aResult)
{
if (aResult == NULL) {
return NS_ERROR_NULL_POINTER;
}
// Always NULL result, in case of failure
*aResult = NULL;
if (aIID.Equals(kISupportsIID)) {
*aResult = (void *)(nsISupports*)this;
} else if (aIID.Equals(kIFactoryIID)) {
*aResult = (void *)(nsIFactory*)this;
}
if (*aResult == NULL) {
return NS_NOINTERFACE;
}
AddRef(); // Increase reference count for caller
return NS_OK;
}
NS_IMPL_ADDREF(nsGfxFactoryGTK);
NS_IMPL_RELEASE(nsGfxFactoryGTK);
nsresult nsGfxFactoryGTK::CreateInstance(nsISupports *aOuter,
const nsIID &aIID,
void **aResult)
{
nsresult res;
if (aResult == NULL) {
return NS_ERROR_NULL_POINTER;
}
*aResult = NULL;
nsISupports *inst = nsnull;
PRBool already_addreffed = PR_FALSE;
if (mClassID.Equals(kCFontMetrics)) {
inst = (nsISupports *)new nsFontMetricsGTK();
if ( NULL == aResult )
{
rv = NS_ERROR_NULL_POINTER;
return rv;
}
else if (mClassID.Equals(kCDeviceContext)) {
inst = (nsISupports *)new nsDeviceContextGTK();
*aResult = NULL;
if (NULL != aOuter)
{
rv = NS_ERROR_NO_AGGREGATION;
return rv;
}
else if (mClassID.Equals(kCRenderingContext)) {
inst = (nsISupports *)new nsRenderingContextGTK();
// create an nsRegionGtk and get the scriptable region from it
nsCOMPtr <nsIRegion> rgn;
NS_NEWXPCOM(rgn, nsRegionGTK);
if (rgn != nsnull)
{
nsCOMPtr<nsIScriptableRegion> scriptableRgn = new nsScriptableRegion(rgn);
inst = scriptableRgn;
}
else if (mClassID.Equals(kCImage)) {
inst = (nsISupports *)new nsImageGTK();
if (NULL == inst)
{
rv = NS_ERROR_OUT_OF_MEMORY;
return rv;
}
else if (mClassID.Equals(kCRegion)) {
nsRegionGTK* dcs;
NS_NEWXPCOM(dcs, nsRegionGTK);
inst = (nsISupports *)dcs;
}
else if (mClassID.Equals(kCScriptableRegion)) {
nsCOMPtr<nsIRegion> rgn;
NS_NEWXPCOM(rgn, nsRegionGTK);
if (rgn != nsnull) {
nsCOMPtr<nsIScriptableRegion> scriptableRgn = new nsScriptableRegion(rgn);
inst = scriptableRgn;
}
}
else if (mClassID.Equals(kCBlender)) {
inst = (nsISupports *)new nsBlender;
}
else if (mClassID.Equals(kCDeviceContextSpec)) {
nsDeviceContextSpecGTK* dcs;
NS_NEWXPCOM(dcs, nsDeviceContextSpecGTK);
inst = (nsISupports *)((nsIDeviceContextSpecPS *)dcs);
}
else if (mClassID.Equals(kCDeviceContextSpecFactory)) {
nsDeviceContextSpecFactoryGTK* dcs;
NS_NEWXPCOM(dcs, nsDeviceContextSpecFactoryGTK);
inst = (nsISupports *)dcs;
}
else if (mClassID.Equals(kImageManagerImpl)) {
nsCOMPtr<nsIImageManager> iManager;
res = NS_NewImageManager(getter_AddRefs(iManager));
already_addreffed = PR_TRUE;
if (NS_SUCCEEDED(res))
{
res = iManager->QueryInterface(NS_GET_IID(nsISupports), (void**)&inst);
}
}
else if (mClassID.Equals(kCFontEnumerator)) {
nsFontEnumeratorGTK* fe;
NS_NEWXPCOM(fe, nsFontEnumeratorGTK);
inst = (nsISupports *)fe;
}
else if (mClassID.Equals(kCScreenManager)) {
NS_NEWXPCOM(inst, nsScreenManagerGtk);
}
if (inst == NULL) {
return NS_ERROR_OUT_OF_MEMORY;
}
if (already_addreffed == PR_FALSE)
NS_ADDREF(inst);
res = inst->QueryInterface(aIID, aResult);
NS_ADDREF(inst);
rv = inst->QueryInterface(aIID, aResult);
NS_RELEASE(inst);
return res;
}
nsresult nsGfxFactoryGTK::LockFactory(PRBool aLock)
{
// Not implemented in simplest case.
return NS_OK;
}
// return the proper factory to the caller
extern "C" NS_GFXNONXP nsresult NSGetFactory(nsISupports* servMgr,
const nsCID &aClass,
const char *aClassName,
const char *aProgID,
nsIFactory **aFactory)
{
if (nsnull == aFactory) {
return NS_ERROR_NULL_POINTER;
}
*aFactory = new nsGfxFactoryGTK(aClass);
if (nsnull == aFactory) {
return NS_ERROR_OUT_OF_MEMORY;
}
return (*aFactory)->QueryInterface(kIFactoryIID, (void**)aFactory);
return rv;
}
static nsresult nsImageManagerConstructor(nsISupports *aOuter, REFNSIID aIID, void **aResult)
{
nsresult rv;
if ( NULL == aResult )
{
rv = NS_ERROR_NULL_POINTER;
return rv;
}
*aResult = NULL;
if (NULL != aOuter)
{
rv = NS_ERROR_NO_AGGREGATION;
return rv;
}
// this will return an image manager with a count of 1
rv = NS_NewImageManager((nsIImageManager **)aResult);
return rv;
}
static nsModuleComponentInfo components[] =
{
{ "Gtk Font Metrics",
NS_FONT_METRICS_CID,
"mozilla.gfx.font_metrics.gtk.1",
nsFontMetricsGTKConstructor },
{ "Gtk Device Context",
NS_DEVICE_CONTEXT_CID,
"mozilla.gfx.device_context.gtk.1",
nsDeviceContextGTKConstructor },
{ "Gtk Rendering Context",
NS_RENDERING_CONTEXT_CID,
"mozilla.gfx.rendering_context.gtk.1",
nsRenderingContextGTKConstructor },
{ "Gtk Image",
NS_IMAGE_CID,
"mozilla.gfx.image.gtk.1",
nsImageGTKConstructor },
{ "Gtk Region",
NS_REGION_CID,
"mozilla.gfx.region.gtk.1",
nsRegionGTKConstructor },
{ "Scriptable Region",
NS_SCRIPTABLE_REGION_CID,
"mozilla.gfx.scriptable_region.1",
nsScriptableRegionConstructor },
{ "Blender",
NS_BLENDER_CID,
"mozilla.gfx.blender.1",
nsBlenderConstructor },
{ "Gtk Device Context Spec",
NS_DEVICE_CONTEXT_SPEC_CID,
"mozilla.gfx.device_context_spec.gtk.1",
nsDeviceContextSpecGTKConstructor },
{ "Gtk Device Context Spec Factory",
NS_DEVICE_CONTEXT_SPEC_FACTORY_CID,
"mozilla.gfx.device_context_spec_factory.gtk.1",
nsDeviceContextSpecFactoryGTKConstructor },
{ "Image Manager",
NS_IMAGEMANAGER_CID,
"mozilla.gfx.image_manager.1",
nsImageManagerConstructor },
{ "GTK Font Enumerator",
NS_FONT_ENUMERATOR_CID,
"mozilla.gfx.font_enumerator.gtk.1",
nsFontEnumeratorGTKConstructor },
{ "Gtk Screen Manager",
NS_SCREENMANAGER_CID,
"mozilla.gfx.screenmanager.gtk.1",
nsScreenManagerGtkConstructor }
};
NS_IMPL_NSGETMODULE("nsGfxGTKModule", components)