Back to index

lightning-sunbird  0.9+nobinonly
nsXTFXULVisualWrapper.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Mozilla XTF project.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Alex Fritze.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alex Fritze <alex@croczilla.com> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCOMPtr.h"
00040 #include "nsXTFVisualWrapper.h"
00041 #include "nsIXTFXULVisual.h"
00042 #include "nsXTFWeakTearoff.h"
00043 #include "nsIXTFXULVisualWrapper.h"
00044 
00045 typedef nsXTFVisualWrapper nsXTFXULVisualWrapperBase;
00046 
00048 // nsXTFXULVisualWrapper class
00049 class nsXTFXULVisualWrapper : public nsXTFXULVisualWrapperBase,
00050                               public nsIXTFXULVisualWrapper
00051 {
00052 protected:
00053   friend nsresult
00054   NS_NewXTFXULVisualWrapper(nsIXTFXULVisual* xtfElement,
00055                             nsINodeInfo* ni,
00056                             nsIContent** aResult);
00057 
00058   nsXTFXULVisualWrapper(nsINodeInfo* ni, nsIXTFXULVisual* xtfElement);
00059   virtual ~nsXTFXULVisualWrapper();
00060   nsresult Init();
00061   
00062 public:
00063   // nsISupports interface
00064   NS_DECL_ISUPPORTS_INHERITED
00065 
00066   // nsIXTFElementWrapperPrivate interface
00067   virtual PRUint32 GetElementType() { return nsIXTFElement::ELEMENT_TYPE_XUL_VISUAL; }
00068   
00069   // nsIXTFXULVisualWrapper interface
00070   NS_DECL_NSIXTFXULVISUALWRAPPER
00071 
00072   // nsIXTFStyledElementWrapper
00073   NS_FORWARD_NSIXTFSTYLEDELEMENTWRAPPER(nsXTFStyledElementWrapper::)
00074 
00075   // nsIXTFElementWrapper interface
00076   NS_FORWARD_NSIXTFELEMENTWRAPPER(nsXTFXULVisualWrapperBase::)
00077   
00078 private:
00079   virtual nsIXTFElement *GetXTFElement() const { return mXTFElement; }
00080   virtual nsIXTFVisual *GetXTFVisual() const { return mXTFElement; }
00081     
00082   nsCOMPtr<nsIXTFXULVisual> mXTFElement;
00083 };
00084 
00085 //----------------------------------------------------------------------
00086 // implementation:
00087 
00088 nsXTFXULVisualWrapper::nsXTFXULVisualWrapper(nsINodeInfo* aNodeInfo,
00089                                              nsIXTFXULVisual* xtfElement)
00090     : nsXTFXULVisualWrapperBase(aNodeInfo), mXTFElement(xtfElement)
00091 {
00092 #ifdef DEBUG
00093 //  printf("nsXTFXULVisualWrapper CTOR\n");
00094 #endif
00095   NS_ASSERTION(mXTFElement, "xtfElement is null");  
00096 }
00097 
00098 nsresult
00099 nsXTFXULVisualWrapper::Init()
00100 {
00101   nsXTFXULVisualWrapperBase::Init();
00102   
00103   // pass a weak wrapper (non base object ref-counted), so that
00104   // our mXTFElement can safely addref/release.
00105   nsISupports *weakWrapper=nsnull;
00106   NS_NewXTFWeakTearoff(NS_GET_IID(nsIXTFXULVisualWrapper),
00107                        (nsIXTFXULVisualWrapper*)this,
00108                        &weakWrapper);
00109   if (!weakWrapper) {
00110     NS_ERROR("could not construct weak wrapper");
00111     return NS_ERROR_FAILURE;
00112   }
00113 
00114   mXTFElement->OnCreated((nsIXTFXULVisualWrapper*)weakWrapper);
00115   weakWrapper->Release();
00116   
00117   return NS_OK;
00118 }
00119 
00120 nsXTFXULVisualWrapper::~nsXTFXULVisualWrapper()
00121 {
00122   mXTFElement->OnDestroyed();
00123   mXTFElement = nsnull;
00124   
00125 #ifdef DEBUG
00126 //  printf("nsXTFXULVisualWrapper DTOR\n");
00127 #endif
00128 }
00129 
00130 nsresult
00131 NS_NewXTFXULVisualWrapper(nsIXTFXULVisual* xtfElement,
00132                           nsINodeInfo* ni,
00133                           nsIContent** aResult)
00134 {
00135   *aResult = nsnull;
00136 
00137   if (!xtfElement) {
00138     NS_ERROR("can't construct an xtf wrapper without an xtf element");
00139     return NS_ERROR_FAILURE;
00140   }
00141   
00142   nsXTFXULVisualWrapper* result = new nsXTFXULVisualWrapper(ni, xtfElement);
00143   if (!result)
00144     return NS_ERROR_OUT_OF_MEMORY;
00145 
00146   NS_ADDREF(result);
00147 
00148   nsresult rv = result->Init();
00149 
00150   if (NS_FAILED(rv)) {
00151     NS_RELEASE(result);
00152     return rv;
00153   }
00154   
00155   *aResult = result;
00156   return NS_OK;
00157 }
00158 
00159 //----------------------------------------------------------------------
00160 // nsISupports implementation
00161 
00162 
00163 NS_IMPL_ADDREF_INHERITED(nsXTFXULVisualWrapper,nsXTFXULVisualWrapperBase)
00164 NS_IMPL_RELEASE_INHERITED(nsXTFXULVisualWrapper,nsXTFXULVisualWrapperBase)
00165 
00166 NS_INTERFACE_MAP_BEGIN(nsXTFXULVisualWrapper)
00167   NS_INTERFACE_MAP_ENTRY(nsIXTFXULVisualWrapper)
00168   NS_INTERFACE_MAP_ENTRY(nsIXTFStyledElementWrapper)
00169 NS_INTERFACE_MAP_END_INHERITING(nsXTFXULVisualWrapperBase)
00170 
00171 //----------------------------------------------------------------------
00172 // nsIXTFXULVisualWrapper implementation:
00173 
00174 // XXX nothing yet