Back to index

lightning-sunbird  0.9+nobinonly
nsView.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 mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsView.h"
00039 #include "nsIWidget.h"
00040 #include "nsViewManager.h"
00041 #include "nsIWidget.h"
00042 #include "nsIButton.h"
00043 #include "nsGUIEvent.h"
00044 #include "nsIDeviceContext.h"
00045 #include "nsIComponentManager.h"
00046 #include "nsIRenderingContext.h"
00047 #include "nsTransform2D.h"
00048 #include "nsIScrollableView.h"
00049 #include "nsVoidArray.h"
00050 #include "nsGfxCIID.h"
00051 #include "nsIRegion.h"
00052 #include "nsIInterfaceRequestor.h"
00053 
00054 
00055 //mmptemp
00056 
00057 static nsEventStatus PR_CALLBACK HandleEvent(nsGUIEvent *aEvent);
00058 
00059 
00060 //#define SHOW_VIEW_BORDERS
00061 //#define HIDE_ALL_WIDGETS
00062 
00063 // {34297A07-A8FD-d811-87C6-000244212BCB}
00064 #define VIEW_WRAPPER_IID \
00065 { 0x34297a07, 0xa8fd, 0xd811, { 0x87, 0xc6, 0x0, 0x2, 0x44, 0x21, 0x2b, 0xcb } };
00066 
00067 
00071 class ViewWrapper : public nsIInterfaceRequestor
00072 {
00073   public:
00074     NS_DEFINE_STATIC_IID_ACCESSOR(VIEW_WRAPPER_IID)
00075     NS_DECL_ISUPPORTS
00076     NS_DECL_NSIINTERFACEREQUESTOR
00077 
00078     ViewWrapper(nsView* aView) : mView(aView) {}
00079 
00080     nsView* GetView() { return mView; }
00081   private:
00082     nsView* mView;
00083 };
00084 
00085 NS_IMPL_ADDREF(ViewWrapper)
00086 NS_IMPL_RELEASE(ViewWrapper)
00087 #ifndef DEBUG
00088 NS_IMPL_QUERY_INTERFACE2(ViewWrapper, ViewWrapper, nsIInterfaceRequestor)
00089 
00090 #else
00091 NS_IMETHODIMP ViewWrapper::QueryInterface(REFNSIID aIID, void** aInstancePtr)
00092 {
00093   NS_ENSURE_ARG_POINTER(aInstancePtr);
00094 
00095   NS_ASSERTION(!aIID.Equals(NS_GET_IID(nsIView)) &&
00096                !aIID.Equals(NS_GET_IID(nsIScrollableView)),
00097                "Someone expects a viewwrapper to be a view!");
00098   
00099   *aInstancePtr = nsnull;
00100   
00101   if (aIID.Equals(NS_GET_IID(nsISupports))) {
00102     *aInstancePtr = NS_STATIC_CAST(nsISupports*, this);
00103   }
00104   else if (aIID.Equals(NS_GET_IID(ViewWrapper))) {
00105     *aInstancePtr = this;
00106   }
00107   else if (aIID.Equals(NS_GET_IID(nsIInterfaceRequestor))) {
00108     *aInstancePtr = this;
00109   }
00110 
00111 
00112   if (*aInstancePtr) {
00113     AddRef();
00114     return NS_OK;
00115   }
00116 
00117   return NS_NOINTERFACE;
00118 }
00119 #endif
00120 
00121 NS_IMETHODIMP ViewWrapper::GetInterface(REFNSIID aIID, void** aInstancePtr)
00122 {
00123   if (aIID.Equals(NS_GET_IID(nsIScrollableView))) {
00124     *aInstancePtr = mView->ToScrollableView();
00125     return NS_OK;
00126   }
00127   if (aIID.Equals(NS_GET_IID(nsIView))) {
00128     *aInstancePtr = mView;
00129     return NS_OK;
00130   }
00131   return QueryInterface(aIID, aInstancePtr);
00132 }
00133 
00138 static ViewWrapper* GetWrapperFor(nsIWidget* aWidget)
00139 {
00140   // The widget's client data points back to the owning view
00141   if (aWidget) {
00142     void* clientData;
00143     aWidget->GetClientData(clientData);
00144     nsISupports* data = (nsISupports*)clientData;
00145     
00146     if (data) {
00147       ViewWrapper* wrapper;
00148       CallQueryInterface(data, &wrapper);
00149       // Give a weak reference to the caller. There will still be at least one
00150       // reference left, since the wrapper was addrefed when set on the widget.
00151       if (wrapper)
00152         wrapper->Release();
00153       return wrapper;
00154     }
00155   }
00156   return nsnull;
00157 }
00158 
00159 //
00160 // Main events handler
00161 //
00162 nsEventStatus PR_CALLBACK HandleEvent(nsGUIEvent *aEvent)
00163 { 
00164 //printf(" %d %d %d (%d,%d) \n", aEvent->widget, aEvent->widgetSupports, 
00165 //       aEvent->message, aEvent->point.x, aEvent->point.y);
00166   nsEventStatus result = nsEventStatus_eIgnore;
00167   nsView       *view = nsView::GetViewFor(aEvent->widget);
00168 
00169   if (view)
00170   {
00171     view->GetViewManager()->DispatchEvent(aEvent, &result);
00172   }
00173 
00174   return result;
00175 }
00176 
00177 MOZ_DECL_CTOR_COUNTER(nsView)
00178 
00179 nsView::nsView(nsViewManager* aViewManager, nsViewVisibility aVisibility)
00180 {
00181   MOZ_COUNT_CTOR(nsView);
00182 
00183   mVis = aVisibility;
00184   // Views should be transparent by default. Not being transparent is
00185   // a promise that the view will paint all its pixels opaquely. Views
00186   // should make this promise explicitly by calling
00187   // SetViewContentTransparency.
00188   mVFlags = NS_VIEW_FLAG_TRANSPARENT;
00189   mOpacity = 1.0f;
00190   mViewManager = aViewManager;
00191   mChildRemoved = PR_FALSE;
00192   mDirtyRegion = nsnull;
00193   mDeletionObserver = nsnull;
00194 }
00195 
00196 void nsView::DropMouseGrabbing() {
00197   // check to see if we are grabbing events
00198   if (mViewManager->GetMouseEventGrabber() == this) {
00199     // we are grabbing events. Move the grab to the parent if we can.
00200     PRBool boolResult; //not used
00201     // if GetParent() returns null, then we release the grab, which is the best we can do
00202     mViewManager->GrabMouseEvents(GetParent(), boolResult);
00203   }
00204 }
00205 
00206 nsView::~nsView()
00207 {
00208   MOZ_COUNT_DTOR(nsView);
00209 
00210   while (GetFirstChild())
00211   {
00212     nsView* child = GetFirstChild();
00213     if (child->GetViewManager() == mViewManager) {
00214       child->Destroy();
00215     } else {
00216       // just unhook it. Someone else will want to destroy this.
00217       RemoveChild(child);
00218     }
00219   }
00220 
00221   if (mViewManager)
00222   {
00223     DropMouseGrabbing();
00224   
00225     nsView *rootView = mViewManager->GetRootView();
00226     
00227     if (rootView)
00228     {
00229       // Root views can have parents!
00230       if (mParent)
00231       {
00232         mViewManager->RemoveChild(this);
00233       }
00234 
00235       if (rootView == this)
00236       {
00237         // Inform the view manager that the root view has gone away...
00238         mViewManager->SetRootView(nsnull);
00239       }
00240     }
00241     else if (mParent)
00242     {
00243       mParent->RemoveChild(this);
00244     }
00245     
00246     mViewManager = nsnull;
00247   }
00248   else if (mParent)
00249   {
00250     mParent->RemoveChild(this);
00251   }
00252 
00253   if (mZParent)
00254   {
00255     mZParent->RemoveReparentedView();
00256     mZParent->Destroy();
00257   }
00258 
00259   // Destroy and release the widget
00260   if (mWindow)
00261   {
00262     // Release memory for the view wrapper
00263     ViewWrapper* wrapper = GetWrapperFor(mWindow);
00264     NS_IF_RELEASE(wrapper);
00265 
00266     mWindow->SetClientData(nsnull);
00267     mWindow->Destroy();
00268     NS_RELEASE(mWindow);
00269   }
00270   delete mDirtyRegion;
00271   delete mClipRect;
00272 
00273   if (mDeletionObserver) {
00274     mDeletionObserver->Clear();
00275   }
00276 }
00277 
00278 nsresult nsView::QueryInterface(const nsIID& aIID, void** aInstancePtr)
00279 {
00280   if (nsnull == aInstancePtr) {
00281     return NS_ERROR_NULL_POINTER;
00282   }
00283 
00284   NS_ASSERTION(!aIID.Equals(NS_GET_IID(nsISupports)),
00285                "Someone expects views to be ISupports-derived!");
00286   
00287   *aInstancePtr = nsnull;
00288   
00289   if (aIID.Equals(NS_GET_IID(nsIView))) {
00290     *aInstancePtr = (void*)(nsIView*)this;
00291     return NS_OK;
00292   }
00293 
00294   return NS_NOINTERFACE;
00295 }
00296 
00297 nsIView* nsIView::GetViewFor(nsIWidget* aWidget)
00298 {           
00299   NS_PRECONDITION(nsnull != aWidget, "null widget ptr");
00300 
00301   ViewWrapper* wrapper = GetWrapperFor(aWidget);
00302   if (wrapper)
00303     return wrapper->GetView();  
00304   return nsnull;
00305 }
00306 
00307 void nsIView::Destroy()
00308 {
00309   delete this;
00310 }
00311 
00312 NS_IMETHODIMP nsView::Paint(nsIRenderingContext& rc, const nsRect& rect,
00313                               PRUint32 aPaintFlags, PRBool &aResult)
00314 {
00315     // Just paint, assume compositor knows what it's doing.
00316     if (nsnull != mClientData) {
00317       nsCOMPtr<nsIViewObserver> observer;
00318       if (NS_OK == mViewManager->GetViewObserver(*getter_AddRefs(observer))) {
00319         observer->Paint((nsIView *)this, rc, rect);
00320       }
00321     }
00322         return NS_OK;
00323 }
00324 
00325 NS_IMETHODIMP nsView::Paint(nsIRenderingContext& rc, const nsIRegion& region,
00326                               PRUint32 aPaintFlags, PRBool &aResult)
00327 {
00328   // XXX apply region to rc
00329   // XXX get bounding rect from region
00330   //if (nsnull != mClientData)
00331   //{
00332   //  nsIViewObserver *obs;
00333   //
00334   //  if (NS_OK == mViewManager->GetViewObserver(obs))
00335   //  {
00336   //    obs->Paint((nsIView *)this, rc, rect, aPaintFlags);
00337   //    NS_RELEASE(obs);
00338   //  }
00339   //}
00340   aResult = PR_FALSE;
00341   return NS_ERROR_NOT_IMPLEMENTED;
00342 }
00343 
00344 void nsView::SetPosition(nscoord aX, nscoord aY)
00345 {
00346   mDimBounds.x += aX - mPosX;
00347   mDimBounds.y += aY - mPosY;
00348   mPosX = aX;
00349   mPosY = aY;
00350 
00351   NS_ASSERTION(GetParent() || (aX == 0 && aY == 0),
00352                "Don't try to move the root widget to something non-zero");
00353 
00354   ResetWidgetBounds(PR_TRUE, PR_TRUE, PR_FALSE);
00355 }
00356 
00357 void nsView::SetPositionIgnoringChildWidgets(nscoord aX, nscoord aY)
00358 {
00359   mDimBounds.x += aX - mPosX;
00360   mDimBounds.y += aY - mPosY;
00361   mPosX = aX;
00362   mPosY = aY;
00363 
00364   ResetWidgetBounds(PR_FALSE, PR_TRUE, PR_FALSE);
00365 }
00366 
00367 void nsView::ResetWidgetBounds(PRBool aRecurse, PRBool aMoveOnly,
00368                                PRBool aInvalidateChangedSize) {
00369   if (mWindow) {
00370     // If our view manager has refresh disabled, then do nothing; the view
00371     // manager will set our position when refresh is reenabled.  Just let it
00372     // know that it has pending updates.
00373     if (!mViewManager->IsRefreshEnabled()) {
00374       mViewManager->PostPendingUpdate();
00375       return;
00376     }
00377 
00378     DoResetWidgetBounds(aMoveOnly, aInvalidateChangedSize);
00379   } else if (aRecurse) {
00380     // reposition any widgets under this view
00381     for (nsView* v = GetFirstChild(); v; v = v->GetNextSibling()) {
00382       v->ResetWidgetBounds(PR_TRUE, aMoveOnly, aInvalidateChangedSize);
00383     }
00384   }
00385 }
00386 
00387 void nsView::DoResetWidgetBounds(PRBool aMoveOnly,
00388                                  PRBool aInvalidateChangedSize) {
00389   // The geometry of a root view's widget is controlled externally,
00390   // NOT by sizing or positioning the view
00391   if (mViewManager->GetRootView() == this) {
00392     return;
00393   }
00394 
00395   NS_PRECONDITION(mWindow, "Why was this called??");
00396   nsIDeviceContext  *dx;
00397   float             t2p, p2t;
00398   
00399   mViewManager->GetDeviceContext(dx);
00400   t2p = dx->AppUnitsToDevUnits();
00401   p2t = dx->DevUnitsToAppUnits();
00402   NS_RELEASE(dx);
00403 
00404   nsPoint offset(0, 0);
00405   if (GetParent()) {
00406     nsIWidget* parentWidget = GetParent()->GetNearestWidget(&offset);
00407 
00408     nsWindowType type;
00409     mWindow->GetWindowType(type);
00410     if (type == eWindowType_popup) {
00411       // put offset into screen coordinates
00412       nsRect screenRect(0,0,1,1);
00413       parentWidget->WidgetToScreen(screenRect, screenRect);
00414       offset += nsPoint(NSIntPixelsToTwips(screenRect.x, p2t),
00415                         NSIntPixelsToTwips(screenRect.y, p2t));
00416     }
00417   }
00418 
00419   nsRect newBounds(NSTwipsToIntPixels((mDimBounds.x + offset.x), t2p),
00420                    NSTwipsToIntPixels((mDimBounds.y + offset.y), t2p),
00421                    NSTwipsToIntPixels(mDimBounds.width, t2p),
00422                    NSTwipsToIntPixels(mDimBounds.height, t2p));
00423     
00424   PRBool changedPos = PR_TRUE;
00425   PRBool changedSize = PR_TRUE;
00426   if (!(mVFlags & NS_VIEW_FLAG_HAS_POSITIONED_WIDGET)) {
00427     mVFlags |= NS_VIEW_FLAG_HAS_POSITIONED_WIDGET;
00428   } else {
00429     nsRect curBounds;
00430     mWindow->GetBounds(curBounds);
00431     changedPos = curBounds.TopLeft() != newBounds.TopLeft();
00432     changedSize = curBounds.Size() != newBounds.Size();
00433   }
00434 
00435   if (changedPos) {
00436     if (changedSize && !aMoveOnly) {
00437       mWindow->Resize(newBounds.x, newBounds.y, newBounds.width, newBounds.height,
00438                       aInvalidateChangedSize);
00439     } else {
00440       mWindow->Move(newBounds.x, newBounds.y);
00441     }
00442   } else {
00443     if (changedSize && !aMoveOnly) {
00444       mWindow->Resize(newBounds.width, newBounds.height, aInvalidateChangedSize);
00445     } // else do nothing!
00446   }
00447 }
00448 
00449 void nsView::SetDimensions(const nsRect& aRect, PRBool aPaint, PRBool aResizeWidget)
00450 {
00451   nsRect dims = aRect;
00452   dims.MoveBy(mPosX, mPosY);
00453 
00454   // Don't use nsRect's operator== here, since it returns true when
00455   // both rects are empty even if they have different widths and we
00456   // have cases where that sort of thing matters to us.
00457   if (mDimBounds.TopLeft() == dims.TopLeft() &&
00458       mDimBounds.Size() == dims.Size()) {
00459     return;
00460   }
00461 
00462   mDimBounds = dims;
00463 
00464   if (aResizeWidget) {
00465     ResetWidgetBounds(PR_FALSE, PR_FALSE, aPaint);
00466   }
00467 }
00468 
00469 NS_IMETHODIMP nsView::SetVisibility(nsViewVisibility aVisibility)
00470 {
00471 
00472   mVis = aVisibility;
00473 
00474   if (aVisibility == nsViewVisibility_kHide)
00475   {
00476     DropMouseGrabbing();
00477   }
00478 
00479   if (nsnull != mWindow)
00480   {
00481 #ifndef HIDE_ALL_WIDGETS
00482     if (mVis == nsViewVisibility_kShow)
00483     {
00484       DoResetWidgetBounds(PR_FALSE, PR_TRUE);
00485       mWindow->Show(PR_TRUE);
00486     }
00487     else
00488 #endif
00489       mWindow->Show(PR_FALSE);
00490   }
00491 
00492   return NS_OK;
00493 }
00494 
00495 NS_IMETHODIMP nsView::SetFloating(PRBool aFloatingView)
00496 {
00497        if (aFloatingView)
00498               mVFlags |= NS_VIEW_FLAG_FLOATING;
00499        else
00500               mVFlags &= ~NS_VIEW_FLAG_FLOATING;
00501 
00502 #if 0
00503        // recursively make all sub-views "floating" grr.
00504        for (nsView* child = mFirstChild; chlid; child = child->GetNextSibling()) {
00505               child->SetFloating(aFloatingView);
00506        }
00507 #endif
00508 
00509        return NS_OK;
00510 }
00511 
00512 void nsView::InvalidateHierarchy(nsViewManager *aViewManagerParent)
00513 {
00514   if (aViewManagerParent) {
00515     // We're removed from the view hierarchy of aRemovalPoint, so make sure
00516     // we're not still grabbing mouse events.
00517     if (aViewManagerParent->GetMouseEventGrabber() == this) {
00518       PRBool res;
00519       aViewManagerParent->GrabMouseEvents(nsnull, res);
00520     }
00521   }
00522 
00523   if (mViewManager->GetRootView() == this)
00524     mViewManager->InvalidateHierarchy();
00525 
00526   for (nsView *child = mFirstChild; child; child = child->GetNextSibling())
00527     child->InvalidateHierarchy(aViewManagerParent);
00528 }
00529 
00530 void nsView::InsertChild(nsView *aChild, nsView *aSibling)
00531 {
00532   NS_PRECONDITION(nsnull != aChild, "null ptr");
00533 
00534   if (nsnull != aChild)
00535   {
00536     if (nsnull != aSibling)
00537     {
00538 #ifdef NS_DEBUG
00539       NS_ASSERTION(aSibling->GetParent() == this, "tried to insert view with invalid sibling");
00540 #endif
00541       //insert after sibling
00542       aChild->SetNextSibling(aSibling->GetNextSibling());
00543       aSibling->SetNextSibling(aChild);
00544     }
00545     else
00546     {
00547       aChild->SetNextSibling(mFirstChild);
00548       mFirstChild = aChild;
00549     }
00550     aChild->SetParent(this);
00551 
00552     // If we just inserted a root view, then update the RootViewManager
00553     // on all view managers in the new subtree.
00554 
00555     nsViewManager *vm = aChild->GetViewManager();
00556     if (vm->GetRootView() == aChild)
00557     {
00558       aChild->InvalidateHierarchy(nsnull); // don't care about releasing grabs
00559     }
00560   }
00561 }
00562 
00563 void nsView::RemoveChild(nsView *child)
00564 {
00565   NS_PRECONDITION(nsnull != child, "null ptr");
00566 
00567   if (nsnull != child)
00568   {
00569     nsView* prevKid = nsnull;
00570     nsView* kid = mFirstChild;
00571     PRBool found = PR_FALSE;
00572     while (nsnull != kid) {
00573       if (kid == child) {
00574         if (nsnull != prevKid) {
00575           prevKid->SetNextSibling(kid->GetNextSibling());
00576         } else {
00577           mFirstChild = kid->GetNextSibling();
00578         }
00579         child->SetParent(nsnull);
00580         found = PR_TRUE;
00581         break;
00582       }
00583       prevKid = kid;
00584       mChildRemoved = PR_TRUE;
00585            kid = kid->GetNextSibling();
00586     }
00587     NS_ASSERTION(found, "tried to remove non child");
00588 
00589     // If we just removed a root view, then update the RootViewManager
00590     // on all view managers in the removed subtree.
00591 
00592     nsViewManager *vm = child->GetViewManager();
00593     if (vm->GetRootView() == child)
00594     {
00595       child->InvalidateHierarchy(GetViewManager());
00596     }
00597   }
00598 }
00599 
00600 NS_IMETHODIMP nsView::SetOpacity(float opacity)
00601 {
00602   mOpacity = opacity;
00603   return NS_OK;
00604 }
00605 
00606 NS_IMETHODIMP nsView::SetContentTransparency(PRBool aTransparent)
00607 {
00608   if (aTransparent == PR_TRUE)
00609     mVFlags |= NS_VIEW_FLAG_TRANSPARENT;
00610   else
00611     mVFlags &= ~NS_VIEW_FLAG_TRANSPARENT;
00612 
00613   return NS_OK;
00614 }
00615 
00616 // Native widgets ultimately just can't deal with the awesome power of
00617 // CSS2 z-index. However, we set the z-index on the widget anyway
00618 // because in many simple common cases the widgets do end up in the
00619 // right order. We set each widget's z-index to the z-index of the
00620 // nearest ancestor that has non-auto z-index.
00621 static void UpdateNativeWidgetZIndexes(nsView* aView, PRInt32 aZIndex)
00622 {
00623   if (aView->HasWidget()) {
00624     nsIWidget* widget = aView->GetWidget();
00625     PRInt32 curZ;
00626     widget->GetZIndex(&curZ);
00627     if (curZ != aZIndex) {
00628       widget->SetZIndex(aZIndex);
00629     }
00630   } else {
00631     for (nsView* v = aView->GetFirstChild(); v; v = v->GetNextSibling()) {
00632       if (v->GetZIndexIsAuto()) {
00633         UpdateNativeWidgetZIndexes(v, aZIndex);
00634       }
00635     }
00636   }
00637 }
00638 
00639 static PRInt32 FindNonAutoZIndex(nsView* aView)
00640 {
00641   while (aView) {
00642     if (!aView->GetZIndexIsAuto()) {
00643       return aView->GetZIndex();
00644     }
00645     aView = aView->GetParent();
00646   }
00647   return 0;
00648 }
00649 
00650 nsresult nsIView::CreateWidget(const nsIID &aWindowIID,
00651                                nsWidgetInitData *aWidgetInitData,
00652                                nsNativeWidget aNative,
00653                                PRBool aEnableDragDrop,
00654                                PRBool aResetVisibility,
00655                                nsContentType aContentType)
00656 {
00657   nsIDeviceContext  *dx;
00658   nsRect            trect = mDimBounds;
00659 
00660   if (NS_UNLIKELY(!!mWindow)) {
00661     NS_ERROR("We already have a window for this view? BAD");
00662     ViewWrapper* wrapper = GetWrapperFor(mWindow);
00663     NS_IF_RELEASE(wrapper);
00664     mWindow->SetClientData(nsnull);
00665     NS_RELEASE(mWindow);
00666   }
00667 
00668   mViewManager->GetDeviceContext(dx);
00669   float scale = dx->AppUnitsToDevUnits();
00670 
00671   trect *= scale;
00672 
00673   nsView* v = NS_STATIC_CAST(nsView*, this);
00674   if (NS_OK == v->LoadWidget(aWindowIID))
00675   {
00676     PRBool usewidgets;
00677 
00678     dx->SupportsNativeWidgets(usewidgets);
00679 
00680     if (PR_TRUE == usewidgets)
00681     {
00682       PRBool initDataPassedIn = PR_TRUE;
00683       nsWidgetInitData initData;
00684       if (!aWidgetInitData) {
00685         // No initData, we're a child window
00686         // Create initData to pass in params
00687         initDataPassedIn = PR_FALSE;
00688         initData.clipChildren = PR_TRUE; // Clip child window's children
00689         initData.clipSiblings = PR_TRUE; // Clip child window's siblings
00690         aWidgetInitData = &initData;
00691       }
00692       aWidgetInitData->mContentType = aContentType;
00693 
00694       if (aNative)
00695         mWindow->Create(aNative, trect, ::HandleEvent, dx, nsnull, nsnull, aWidgetInitData);
00696       else
00697       {
00698         if (!initDataPassedIn && GetParent() && 
00699           GetParent()->GetViewManager() != mViewManager)
00700           initData.mListenForResizes = PR_TRUE;
00701 
00702         nsPoint offset(0, 0);
00703         nsIWidget* parentWidget = GetParent() ? GetParent()->GetNearestWidget(&offset)
00704           : nsnull;
00705         trect += offset;
00706         if (aWidgetInitData->mWindowType == eWindowType_popup) {
00707           mWindow->Create(parentWidget->GetNativeData(NS_NATIVE_WIDGET), trect,
00708                           ::HandleEvent, dx, nsnull, nsnull, aWidgetInitData);
00709         } else {
00710           mWindow->Create(parentWidget, trect,
00711                           ::HandleEvent, dx, nsnull, nsnull, aWidgetInitData);
00712         }
00713       }
00714       if (aEnableDragDrop) {
00715         mWindow->EnableDragDrop(PR_TRUE);
00716       }
00717       
00718       // propagate the z-index to the widget.
00719       UpdateNativeWidgetZIndexes(v, FindNonAutoZIndex(v));
00720     }
00721   }
00722 
00723   //make sure visibility state is accurate
00724   
00725   if (aResetVisibility) {
00726     v->SetVisibility(GetVisibility());
00727   }
00728 
00729   NS_RELEASE(dx);
00730 
00731   return NS_OK;
00732 }
00733 
00734 void nsView::SetZIndex(PRBool aAuto, PRInt32 aZIndex, PRBool aTopMost)
00735 {
00736   PRBool oldIsAuto = GetZIndexIsAuto();
00737   mVFlags = (mVFlags & ~NS_VIEW_FLAG_AUTO_ZINDEX) | (aAuto ? NS_VIEW_FLAG_AUTO_ZINDEX : 0);
00738   mZIndex = aZIndex;
00739   SetTopMost(aTopMost);
00740   
00741   if (HasWidget() || !oldIsAuto || !aAuto) {
00742     UpdateNativeWidgetZIndexes(this, FindNonAutoZIndex(this));
00743   }
00744 }
00745 
00746 NS_IMETHODIMP nsView::SetWidget(nsIWidget *aWidget)
00747 {
00748   ViewWrapper* wrapper = new ViewWrapper(this);
00749   if (!wrapper)
00750     return NS_ERROR_OUT_OF_MEMORY;
00751   NS_ADDREF(wrapper); // Will be released in ~nsView or upon setting a new widget
00752 
00753   // Destroy any old wrappers if there are any
00754   ViewWrapper* oldWrapper = GetWrapperFor(aWidget);
00755   NS_IF_RELEASE(oldWrapper);
00756   NS_IF_RELEASE(mWindow);
00757 
00758   mWindow = aWidget;
00759 
00760   if (nsnull != mWindow)
00761   {
00762     NS_ADDREF(mWindow);
00763     mWindow->SetClientData(wrapper);
00764   }
00765 
00766   mVFlags &= ~NS_VIEW_FLAG_HAS_POSITIONED_WIDGET;
00767 
00768   UpdateNativeWidgetZIndexes(this, FindNonAutoZIndex(this));
00769 
00770   return NS_OK;
00771 }
00772 
00773 //
00774 // internal window creation functions
00775 //
00776 nsresult nsView::LoadWidget(const nsCID &aClassIID)
00777 {
00778   ViewWrapper* wrapper = new ViewWrapper(this);
00779   if (!wrapper)
00780     return NS_ERROR_OUT_OF_MEMORY;
00781   NS_ADDREF(wrapper); // Will be released in ~nsView
00782 
00783   nsresult rv = CallCreateInstance(aClassIID, &mWindow);
00784 
00785   if (NS_SUCCEEDED(rv)) {
00786     // Set the widget's client data
00787     mWindow->SetClientData(wrapper);
00788   } else {
00789     delete wrapper;
00790   }
00791 
00792   mVFlags &= ~NS_VIEW_FLAG_HAS_POSITIONED_WIDGET;
00793   return rv;
00794 }
00795 
00796 #ifdef DEBUG
00797 void nsIView::List(FILE* out, PRInt32 aIndent) const
00798 {
00799   PRInt32 i;
00800   for (i = aIndent; --i >= 0; ) fputs("  ", out);
00801   fprintf(out, "%p ", (void*)this);
00802   if (nsnull != mWindow) {
00803     nsRect windowBounds;
00804     nsRect nonclientBounds;
00805     float p2t;
00806     nsIDeviceContext *dx;
00807     mViewManager->GetDeviceContext(dx);
00808     p2t = dx->DevUnitsToAppUnits();
00809     NS_RELEASE(dx);
00810     mWindow->GetClientBounds(windowBounds);
00811     windowBounds *= p2t;
00812     mWindow->GetBounds(nonclientBounds);
00813     nonclientBounds *= p2t;
00814     nsrefcnt widgetRefCnt = mWindow->AddRef() - 1;
00815     mWindow->Release();
00816     PRInt32 Z;
00817     mWindow->GetZIndex(&Z);
00818     fprintf(out, "(widget=%p[%d] z=%d pos={%d,%d,%d,%d}) ",
00819             (void*)mWindow, widgetRefCnt, Z,
00820             nonclientBounds.x, nonclientBounds.y,
00821             windowBounds.width, windowBounds.height);
00822   }
00823   nsRect brect = GetBounds();
00824   fprintf(out, "{%d,%d,%d,%d}",
00825           brect.x, brect.y, brect.width, brect.height);
00826   const nsView* v = NS_STATIC_CAST(const nsView*, this);
00827   if (v->IsZPlaceholderView()) {
00828     fprintf(out, " z-placeholder(%p)",
00829             (void*)NS_STATIC_CAST(const nsZPlaceholderView*, this)->GetReparentedView());
00830   }
00831   if (v->GetZParent()) {
00832     fprintf(out, " zparent=%p", (void*)v->GetZParent());
00833   }
00834   fprintf(out, " z=%d vis=%d opc=%1.3f tran=%d clientData=%p <\n",
00835           mZIndex, mVis, mOpacity, IsTransparent(), mClientData);
00836   for (nsView* kid = mFirstChild; kid; kid = kid->GetNextSibling()) {
00837     NS_ASSERTION(kid->GetParent() == this, "incorrect parent");
00838     kid->List(out, aIndent + 1);
00839   }
00840   for (i = aIndent; --i >= 0; ) fputs("  ", out);
00841   fputs(">\n", out);
00842 }
00843 #endif // DEBUG
00844 
00845 nsPoint nsIView::GetOffsetTo(const nsIView* aOther) const
00846 {
00847   nsPoint offset(0, 0);
00848   const nsIView* v;
00849   for (v = this; v != aOther && v; v = v->GetParent()) {
00850     offset += v->GetPosition();
00851   }
00852 
00853   if (v != aOther) {
00854     // Looks like aOther wasn't an ancestor of |this|.  So now we have
00855     // the root-VM-relative position of |this| in |offset|.  Convert back
00856     // to the coordinates of aOther
00857     while (aOther) {
00858       offset -= aOther->GetPosition();
00859       aOther = aOther->GetParent();
00860     }
00861   }
00862 
00863   return offset;
00864 }
00865 
00866 nsIntPoint nsIView::GetScreenPosition() const
00867 {
00868   nsIntRect screenRect(0,0,0,0);  
00869   nsPoint toWidgetOffset(0,0);
00870   nsIWidget* widget = GetNearestWidget(&toWidgetOffset);
00871   if (widget) {
00872     nsCOMPtr<nsIDeviceContext> dx;
00873     mViewManager->GetDeviceContext(*getter_AddRefs(dx));
00874     float t2p = dx->AppUnitsToDevUnits();
00875     nsIntRect ourRect(NSTwipsToIntPixels(toWidgetOffset.x, t2p),
00876                       NSTwipsToIntPixels(toWidgetOffset.y, t2p),
00877                       0,
00878                       0);
00879     widget->WidgetToScreen(ourRect, screenRect);
00880   }
00881   
00882   return nsIntPoint(screenRect.x, screenRect.y);
00883 }
00884 
00885 nsIWidget* nsIView::GetNearestWidget(nsPoint* aOffset) const
00886 {
00887   nsPoint pt(0, 0);
00888   const nsView* v;
00889   for (v = NS_STATIC_CAST(const nsView*, this);
00890        v && !v->HasWidget(); v = v->GetParent()) {
00891     pt += v->GetPosition();
00892   }
00893   if (!v) {
00894     if (aOffset) {
00895       *aOffset = pt;
00896     }
00897     return NS_STATIC_CAST(const nsView*, this)->GetViewManager()->GetWidget();
00898   }
00899 
00900   // pt is now the offset from v's origin to this's origin
00901   // The widget's origin is the top left corner of v's bounds, which may
00902   // not coincide with v's origin
00903   if (aOffset) {
00904     nsRect vBounds = v->GetBounds();
00905     *aOffset = pt + v->GetPosition() -  nsPoint(vBounds.x, vBounds.y);
00906   }
00907   return v->GetWidget();
00908 }
00909 
00910 PRBool nsIView::IsRoot() const
00911 {
00912   NS_ASSERTION(mViewManager != nsnull," View manager is null in nsView::IsRoot()");
00913   return mViewManager->GetRootView() == this;
00914 }
00915 
00916 PRBool nsIView::ExternalIsRoot() const
00917 {
00918   return nsIView::IsRoot();
00919 }
00920 
00921 void
00922 nsIView_MOZILLA_1_8_BRANCH::SetDeletionObserver(nsWeakView* aDeletionObserver)
00923 {
00924   if (mDeletionObserver && aDeletionObserver) {
00925     aDeletionObserver->SetPrevious(mDeletionObserver);
00926   }
00927   mDeletionObserver = aDeletionObserver;
00928 }
00929 
00935 static PRBool ApplyClipRect(const nsView* aView, nsRect* aRect, PRBool aFollowPlaceholders,
00936                             nsIView* aStopAtView)
00937 {
00938   // this records the offset from the origin of the current aView
00939   // to the origin of the initial aView
00940   nsPoint offset(0, 0);
00941   PRBool lastViewIsFloating = aView->GetFloating();
00942   PRBool foundPlaceholders = PR_FALSE;
00943   while (NS_STATIC_CAST(const nsIView*, aView) != aStopAtView) {
00944     const nsView* parentView = aView->GetParent();
00945     nsPoint offsetFromParent = aView->GetPosition();
00946 
00947     const nsView* zParent = aView->GetZParent();
00948     if (zParent) {
00949       foundPlaceholders = PR_TRUE;
00950       if (aFollowPlaceholders) {
00951         // correct offsetFromParent to account for the fact that we're
00952         // switching parentView to ZParent
00953         // Note that the common case is that parentView is an ancestor of
00954         // ZParent.
00955         const nsView* zParentChain;
00956         for (zParentChain = zParent;
00957              zParentChain != parentView && zParentChain;
00958              zParentChain = zParentChain->GetParent()) {
00959           offsetFromParent -= zParentChain->GetPosition();
00960         }
00961         if (!zParentChain) {
00962           // uh oh. As we walked up the tree, we missed zParent. This can
00963           // happen because of odd (but legal) containing block hierarchies.
00964           // Just compute the required offset from scratch; this is slow,
00965           // but hopefully rarely executed.
00966           offsetFromParent = nsViewManager::ComputeViewOffset(aView)
00967             - nsViewManager::ComputeViewOffset(zParent);
00968         }
00969         parentView = zParent;
00970       }
00971     }
00972 
00973     if (!parentView) {
00974       break;
00975     }
00976 
00977     PRBool parentIsFloating = parentView->GetFloating();
00978     if (lastViewIsFloating && !parentIsFloating) {
00979       break;
00980     }
00981 
00982     // now make offset be the offset from parentView's origin to the initial
00983     // aView's origin
00984     offset += offsetFromParent;
00985     if (parentView->GetClipChildrenToBounds(aFollowPlaceholders)) {
00986       // Get the parent's clip rect (which is just the dimensions in this
00987       // case) into the initial aView's coordinates
00988       nsRect clipRect = parentView->GetDimensions();
00989       clipRect -= offset;
00990       PRBool overlap = aRect->IntersectRect(clipRect, *aRect);
00991       if (!overlap) {
00992         break;
00993       }
00994     }
00995     // The child-clipping rect is applied to all *content* children.
00996     // So we apply it if we're in the placeholder-following pass, or
00997     // if we're in the first pass and we haven't detected any
00998     // placeholders yet, in which case this geometric ancestor is also
00999     // a content ancestor.
01000     const nsRect* r = parentView->GetClipChildrenToRect();
01001     if (r && (!foundPlaceholders || aFollowPlaceholders)) {
01002       // Get the parent's clip rect into the initial aView's coordinates
01003       nsRect clipRect = *r;
01004       clipRect -= offset;
01005       PRBool overlap = aRect->IntersectRect(clipRect, *aRect);
01006       if (!overlap) {
01007         break;
01008       }
01009     }
01010 
01011     aView = parentView;
01012     lastViewIsFloating = parentIsFloating;
01013   }
01014 
01015   return foundPlaceholders;
01016 }
01017 
01018 nsRect nsView::GetClippedRect(nsIView* aStopAtView)
01019 {
01020   nsRect clip = GetDimensions();
01021   PRBool foundPlaceholders = ApplyClipRect(this, &clip, PR_FALSE, aStopAtView);
01022   if (foundPlaceholders && !clip.IsEmpty()) {
01023     ApplyClipRect(this, &clip, PR_TRUE, aStopAtView);
01024   }
01025   return clip;
01026 }