Back to index

lightning-sunbird  0.9+nobinonly
nsIView.h
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 #ifndef nsIView_h___
00039 #define nsIView_h___
00040 
00041 #include "nsCoord.h"
00042 #include "nsRect.h"
00043 #include "nsPoint.h"
00044 #include <stdio.h>
00045 #include "nsIWidget.h"
00046 
00047 class nsIViewManager;
00048 class nsIScrollableView;
00049 class nsViewManager;
00050 class nsView;
00051 class nsWeakView;
00052 struct nsRect;
00053 
00054 // Enumerated type to indicate the visibility of a layer.
00055 // hide - the layer is not shown.
00056 // show - the layer is shown irrespective of the visibility of 
00057 //        the layer's parent.
00058 enum nsViewVisibility {
00059   nsViewVisibility_kHide = 0,
00060   nsViewVisibility_kShow = 1
00061 };
00062 
00063 // IID for the nsIView interface
00064 #define NS_IVIEW_IID    \
00065 { 0x658f72ee, 0x32ef, 0x4e93, \
00066 { 0xb8, 0x4e, 0x5f, 0x0f, 0x8f, 0x77, 0xe4, 0x22 } }
00067 
00068 // Public view flags are defined in this file
00069 #define NS_VIEW_FLAGS_PUBLIC              0x00FF
00070 // Private view flags are private to the view module,
00071 // and are defined in nsView.h
00072 #define NS_VIEW_FLAGS_PRIVATE             0xFF00
00073 
00074 // Public view flags
00075 
00076 // The view is transparent
00077 #define NS_VIEW_FLAG_TRANSPARENT          0x0001
00078 
00079 // The view is always painted onto a background consisting
00080 // of a uniform field of opaque pixels.
00081 #define NS_VIEW_FLAG_UNIFORM_BACKGROUND   0x0002
00082 
00083 // Indicates that the view is using auto z-indexing
00084 #define NS_VIEW_FLAG_AUTO_ZINDEX          0x0004
00085 
00086 // Indicates that the view is a floating view.
00087 #define NS_VIEW_FLAG_FLOATING             0x0008
00088 
00089 // If set it indicates that this view should be
00090 // displayed above z-index:auto views if this view 
00091 // is z-index:auto also
00092 #define NS_VIEW_FLAG_TOPMOST              0x0010
00093 
00094 struct nsViewZIndex {
00095   PRBool mIsAuto;
00096   PRInt32 mZIndex;
00097   PRBool mIsTopmost;
00098   
00099   nsViewZIndex(PRBool aIsAuto, PRInt32 aZIndex, PRBool aIsTopmost)
00100     : mIsAuto(aIsAuto), mZIndex(aZIndex), mIsTopmost(aIsTopmost) {}
00101 };
00102 
00103 //----------------------------------------------------------------------
00104 
00118 class nsIView
00119 {
00120 public:
00121   NS_DEFINE_STATIC_IID_ACCESSOR(NS_IVIEW_IID)
00122 
00123   
00127   virtual nsIScrollableView* ToScrollableView() { return nsnull; }
00128 
00134   static nsIView* GetViewFor(nsIWidget* aWidget);
00135 
00142   nsIViewManager* GetViewManager() const
00143   { return NS_REINTERPRET_CAST(nsIViewManager*, mViewManager); }
00144 
00155   void Destroy();
00156 
00164   nsPoint GetPosition() const {
00165     // Call ExternalIsRoot here so that we can get to it from other
00166     // components
00167     NS_ASSERTION(!ExternalIsRoot() || (mPosX == 0 && mPosY == 0),
00168                  "root views should always have explicit position of (0,0)");
00169     return nsPoint(mPosX, mPosY);
00170   }
00171   
00179   nsRect GetBounds() const { return mDimBounds; }
00180 
00194   nsPoint GetOffsetTo(const nsIView* aOther) const;
00195 
00201   nsIntPoint GetScreenPosition() const;
00202   
00207   nsViewVisibility GetVisibility() const { return mVis; }
00208 
00218   nsViewZIndex GetZIndex() const { return nsViewZIndex((mVFlags & NS_VIEW_FLAG_AUTO_ZINDEX) != 0,
00219                                                        mZIndex,
00220                                                        (mVFlags & NS_VIEW_FLAG_TOPMOST) != 0); }
00221 
00230   PRBool GetFloating() const { return (mVFlags & NS_VIEW_FLAG_FLOATING) != 0; }
00231 
00236   nsIView* GetParent() const { return NS_REINTERPRET_CAST(nsIView*, mParent); }
00237 
00242   nsIView* GetFirstChild() const { return NS_REINTERPRET_CAST(nsIView*, mFirstChild); }
00243 
00248   nsIView* GetNextSibling() const { return NS_REINTERPRET_CAST(nsIView*, mNextSibling); }
00249 
00256   float GetOpacity() const { return mOpacity; }
00257 
00264   PRBool IsTransparent() const { return (mVFlags & NS_VIEW_FLAG_TRANSPARENT) != 0; }
00265 
00275   void SetHasUniformBackground(PRBool aUniform) {
00276     if (aUniform) {
00277       mVFlags |= NS_VIEW_FLAG_UNIFORM_BACKGROUND;
00278     } else {
00279       mVFlags &= ~NS_VIEW_FLAG_UNIFORM_BACKGROUND;
00280     }
00281   }
00282 
00283   PRBool HasUniformBackground() {
00284     return mVFlags & NS_VIEW_FLAG_UNIFORM_BACKGROUND;
00285   }
00286 
00291   void SetClientData(void *aData) { mClientData = aData; }
00292 
00297   void* GetClientData() const { return mClientData; }
00298 
00310   virtual nsIWidget* GetNearestWidget(nsPoint* aOffset) const;
00311 
00327   nsresult CreateWidget(const nsIID &aWindowIID,
00328                         nsWidgetInitData *aWidgetInitData = nsnull,
00329                         nsNativeWidget aNative = nsnull,
00330                         PRBool aEnableDragDrop = PR_TRUE,
00331                         PRBool aResetVisibility = PR_TRUE,
00332                         nsContentType aWindowType = eContentTypeInherit);
00333 
00341   nsIWidget* GetWidget() const { return mWindow; }
00342 
00346   PRBool HasWidget() const { return mWindow != nsnull; }
00347 
00348 #ifdef DEBUG
00349 
00355   virtual void List(FILE* out, PRInt32 aIndent = 0) const;
00356 #endif // DEBUG
00357 
00361   PRBool IsRoot() const;
00362 
00363   virtual PRBool ExternalIsRoot() const;
00364 
00365 protected:
00366   nsViewManager     *mViewManager;
00367   nsView            *mParent;
00368   nsIWidget         *mWindow;
00369   nsView            *mNextSibling;
00370   nsView            *mFirstChild;
00371   void              *mClientData;
00372   PRInt32           mZIndex;
00373   nsViewVisibility  mVis;
00374   nscoord           mPosX, mPosY;
00375   nsRect            mDimBounds; // relative to parent
00376   float             mOpacity;
00377   PRUint32          mVFlags;
00378 
00379   virtual ~nsIView() {}
00380 };
00381 
00382 // This is not a real interface! Used only by nsWeakView.
00383 class nsIView_MOZILLA_1_8_BRANCH : public nsIView
00384 {
00385 public:
00386   void SetDeletionObserver(nsWeakView* aDeletionObserver);
00387 protected:
00388   friend class nsWeakView;
00389   nsWeakView* mDeletionObserver;
00390 };
00391 
00392 // nsWeakViews must *not* be used in heap!
00393 class nsWeakView
00394 {
00395 public:
00396   nsWeakView(nsIView* aView)
00397   : mPrev(nsnull), mView(NS_STATIC_CAST(nsIView_MOZILLA_1_8_BRANCH*, aView))
00398   {
00399     if (mView) {
00400       mView->SetDeletionObserver(this);
00401     }
00402   }
00403 
00404   ~nsWeakView()
00405   {
00406     if (mView) {
00407       NS_ASSERTION(mView->mDeletionObserver == this,
00408                    "nsWeakViews deleted in wrong order!");
00409       // Clear deletion observer temporarily.
00410       mView->SetDeletionObserver(nsnull);
00411       // Put back the previous deletion observer.
00412       mView->SetDeletionObserver(mPrev);
00413     }
00414   }
00415 
00416   PRBool IsAlive() { return !!mView; }
00417 
00418   nsIView* GetView() { return mView; }
00419 
00420   void SetPrevious(nsWeakView* aWeakView) { mPrev = aWeakView; }
00421 
00422   void Clear()
00423   {
00424     if (mPrev) {
00425       mPrev->Clear();
00426     }
00427     mView = nsnull;
00428   }
00429 private:
00430   static void* operator new(size_t) CPP_THROW_NEW { return 0; }
00431   static void operator delete(void*, size_t) {}
00432   nsWeakView*                    mPrev;
00433   nsIView_MOZILLA_1_8_BRANCH*    mView;
00434 };
00435 
00436 #endif