Back to index

lightning-sunbird  0.9+nobinonly
nsRootBoxFrame.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 #include "nsHTMLParts.h"
00038 #include "nsContainerFrame.h"
00039 #include "nsCSSRendering.h"
00040 #include "nsIDocument.h"
00041 #include "nsPresContext.h"
00042 #include "nsViewsCID.h"
00043 #include "nsIView.h"
00044 #include "nsIViewManager.h"
00045 #include "nsIWidget.h"
00046 #include "nsPageFrame.h"
00047 #include "nsIRenderingContext.h"
00048 #include "nsGUIEvent.h"
00049 #include "nsIDOMEvent.h"
00050 #include "nsStyleConsts.h"
00051 #include "nsIViewManager.h"
00052 #include "nsHTMLAtoms.h"
00053 #include "nsIEventStateManager.h"
00054 #include "nsIDeviceContext.h"
00055 #include "nsIScrollableView.h"
00056 #include "nsLayoutAtoms.h"
00057 #include "nsIPresShell.h"
00058 #include "nsBoxFrame.h"
00059 #include "nsStackLayout.h"
00060 #include "nsIRootBox.h"
00061 #include "nsIContent.h"
00062 #include "nsXULTooltipListener.h"
00063 
00064 // Interface IDs
00065 
00066 //#define DEBUG_REFLOW
00067 
00068 class nsRootBoxFrame : public nsBoxFrame, public nsIRootBox {
00069 public:
00070 
00071   friend nsresult NS_NewBoxFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame);
00072 
00073   nsRootBoxFrame(nsIPresShell* aShell);
00074 
00075   NS_DECL_ISUPPORTS_INHERITED
00076 
00077   NS_IMETHOD GetPopupSetFrame(nsIFrame** aResult);
00078   NS_IMETHOD SetPopupSetFrame(nsIFrame* aPopupSet);
00079   NS_IMETHOD GetDefaultTooltip(nsIContent** aResult);
00080   NS_IMETHOD SetDefaultTooltip(nsIContent* aTooltip);
00081   NS_IMETHOD AddTooltipSupport(nsIContent* aNode);
00082   NS_IMETHOD RemoveTooltipSupport(nsIContent* aNode);
00083 
00084   NS_IMETHOD AppendFrames(nsIAtom*        aListName,
00085                           nsIFrame*       aFrameList);
00086   NS_IMETHOD InsertFrames(nsIAtom*        aListName,
00087                           nsIFrame*       aPrevFrame,
00088                           nsIFrame*       aFrameList);
00089   NS_IMETHOD RemoveFrame(nsIAtom*        aListName,
00090                          nsIFrame*       aOldFrame);
00091 
00092   NS_IMETHOD Reflow(nsPresContext*          aPresContext,
00093                     nsHTMLReflowMetrics&     aDesiredSize,
00094                     const nsHTMLReflowState& aReflowState,
00095                     nsReflowStatus&          aStatus);
00096   NS_IMETHOD HandleEvent(nsPresContext* aPresContext, 
00097                          nsGUIEvent*     aEvent,
00098                          nsEventStatus*  aEventStatus);
00099   NS_IMETHOD GetFrameForPoint(const nsPoint& aPoint, 
00100                               nsFramePaintLayer aWhichLayer,
00101                               nsIFrame**     aFrame);
00102 
00108   virtual nsIAtom* GetType() const;
00109   
00110 #ifdef DEBUG
00111   NS_IMETHOD GetFrameName(nsString& aResult) const;
00112 #endif
00113 
00114   nsIFrame* mPopupSetFrame;
00115 
00116 protected:
00117   nsIContent* mDefaultTooltip;
00118 };
00119 
00120 //----------------------------------------------------------------------
00121 
00122 nsresult
00123 NS_NewRootBoxFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame)
00124 {
00125   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00126   if (nsnull == aNewFrame) {
00127     return NS_ERROR_NULL_POINTER;
00128   }
00129   nsRootBoxFrame* it = new (aPresShell) nsRootBoxFrame (aPresShell);
00130   if (nsnull == it) {
00131     return NS_ERROR_OUT_OF_MEMORY;
00132   }
00133 
00134   *aNewFrame = it;
00135 
00136   return NS_OK;
00137 }
00138 
00139 nsRootBoxFrame::nsRootBoxFrame(nsIPresShell* aShell):nsBoxFrame(aShell, PR_TRUE)
00140 {
00141   mPopupSetFrame = nsnull;
00142 
00143   nsCOMPtr<nsIBoxLayout> layout;
00144   NS_NewStackLayout(aShell, layout);
00145   SetLayoutManager(layout);
00146 }
00147 
00148 NS_IMETHODIMP
00149 nsRootBoxFrame::AppendFrames(nsIAtom*        aListName,
00150                              nsIFrame*       aFrameList)
00151 {
00152   nsresult  rv;
00153 
00154   NS_ASSERTION(!aListName, "unexpected child list name");
00155   NS_PRECONDITION(mFrames.IsEmpty(), "already have a child frame");
00156   if (aListName) {
00157     // We only support unnamed principal child list
00158     rv = NS_ERROR_INVALID_ARG;
00159 
00160   } else if (!mFrames.IsEmpty()) {
00161     // We only allow a single child frame
00162     rv = NS_ERROR_FAILURE;
00163 
00164   } else {
00165     rv = nsBoxFrame::AppendFrames(aListName, aFrameList);
00166   }
00167 
00168   return rv;
00169 }
00170 
00171 NS_IMETHODIMP
00172 nsRootBoxFrame::InsertFrames(nsIAtom*        aListName,
00173                              nsIFrame*       aPrevFrame,
00174                              nsIFrame*       aFrameList)
00175 {
00176   nsresult  rv;
00177 
00178   // Because we only support a single child frame inserting is the same
00179   // as appending
00180   NS_PRECONDITION(!aPrevFrame, "unexpected previous sibling frame");
00181   if (aPrevFrame) {
00182     rv = NS_ERROR_UNEXPECTED;
00183   } else {
00184     rv = AppendFrames(aListName, aFrameList);
00185   }
00186 
00187   return rv;
00188 }
00189 
00190 NS_IMETHODIMP
00191 nsRootBoxFrame::RemoveFrame(nsIAtom*        aListName,
00192                             nsIFrame*       aOldFrame)
00193 {
00194   nsresult  rv;
00195 
00196   NS_ASSERTION(!aListName, "unexpected child list name");
00197   if (aListName) {
00198     // We only support the unnamed principal child list
00199     rv = NS_ERROR_INVALID_ARG;
00200   
00201   } else if (aOldFrame == mFrames.FirstChild()) {
00202      rv = nsBoxFrame::RemoveFrame(aListName, aOldFrame);
00203   } else {
00204     rv = NS_ERROR_FAILURE;
00205   }
00206 
00207   return rv;
00208 }
00209 
00210 #ifdef DEBUG_REFLOW
00211 PRInt32 gReflows = 0;
00212 #endif
00213 
00214 NS_IMETHODIMP
00215 nsRootBoxFrame::Reflow(nsPresContext*          aPresContext,
00216                        nsHTMLReflowMetrics&     aDesiredSize,
00217                        const nsHTMLReflowState& aReflowState,
00218                        nsReflowStatus&          aStatus)
00219 {
00220   DO_GLOBAL_REFLOW_COUNT("nsRootBoxFrame", aReflowState.reason);
00221 
00222 #ifdef DEBUG_REFLOW
00223   gReflows++;
00224   printf("----Reflow %d----\n", gReflows);
00225 #endif
00226   return nsBoxFrame::Reflow(aPresContext, aDesiredSize, aReflowState, aStatus);
00227 }
00228 
00229 NS_IMETHODIMP
00230 nsRootBoxFrame::HandleEvent(nsPresContext* aPresContext, 
00231                        nsGUIEvent* aEvent,
00232                        nsEventStatus* aEventStatus)
00233 {
00234   NS_ENSURE_ARG_POINTER(aEventStatus);
00235   if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
00236     return NS_OK;
00237   }
00238 
00239   if (aEvent->message == NS_MOUSE_LEFT_BUTTON_UP ||
00240       aEvent->message == NS_MOUSE_MIDDLE_BUTTON_UP ||
00241       aEvent->message == NS_MOUSE_RIGHT_BUTTON_UP) {
00242     nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
00243   }
00244 
00245   return NS_OK;
00246 }
00247 
00248 NS_IMETHODIMP
00249 nsRootBoxFrame::GetFrameForPoint(const nsPoint& aPoint, 
00250                                    nsFramePaintLayer aWhichLayer,
00251                                    nsIFrame**     aFrame)
00252 {
00253   // this should act like a block, so we need to override
00254   return nsBoxFrame::GetFrameForPoint(aPoint, aWhichLayer, aFrame);
00255 }
00256 
00257 nsIAtom*
00258 nsRootBoxFrame::GetType() const
00259 {
00260   return nsLayoutAtoms::rootFrame;
00261 }
00262 
00263 NS_IMETHODIMP
00264 nsRootBoxFrame::GetPopupSetFrame(nsIFrame** aResult)
00265 {
00266   *aResult = mPopupSetFrame;
00267   return NS_OK;
00268 }
00269 
00270 NS_IMETHODIMP
00271 nsRootBoxFrame::SetPopupSetFrame(nsIFrame* aPopupSet)
00272 {
00273   // Under normal conditions this should only be called once.  However,
00274   // if something triggers ReconstructDocElementHierarchy, we will
00275   // destroy this frame's child (the nsDocElementBoxFrame), but not this
00276   // frame.  This will cause the popupset to remove itself by calling
00277   // |SetPopupSetFrame(nsnull)|, and then we'll be able to accept a new
00278   // popupset.  Since the anonymous content is associated with the
00279   // nsDocElementBoxFrame, we'll get a new popupset when the new doc
00280   // element box frame is created.
00281   if (!mPopupSetFrame || !aPopupSet) {
00282     mPopupSetFrame = aPopupSet;
00283   } else {
00284     NS_NOTREACHED("Popup set is already defined! Only 1 allowed.");
00285   }
00286   return NS_OK;
00287 }
00288 
00289 NS_IMETHODIMP
00290 nsRootBoxFrame::GetDefaultTooltip(nsIContent** aTooltip)
00291 {
00292   *aTooltip = mDefaultTooltip;
00293   return NS_OK;
00294 }
00295 
00296 NS_IMETHODIMP
00297 nsRootBoxFrame::SetDefaultTooltip(nsIContent* aTooltip)
00298 {
00299   mDefaultTooltip = aTooltip;
00300   return NS_OK;
00301 }
00302 
00303 NS_IMETHODIMP
00304 nsRootBoxFrame::AddTooltipSupport(nsIContent* aNode)
00305 {
00306   // listener will be refcounted by dom event targets that
00307   // it will add itself to, and destroyed when those targets
00308   // are destroyed
00309   nsXULTooltipListener* listener = new nsXULTooltipListener();
00310   if (!listener)
00311     return NS_ERROR_OUT_OF_MEMORY;
00312 
00313   listener->Init(aNode, this);
00314   return NS_OK;
00315 }
00316 
00317 NS_IMETHODIMP
00318 nsRootBoxFrame::RemoveTooltipSupport(nsIContent* aNode)
00319 {
00320   // XXjh yuck, I'll have to implement a way to get at
00321   // the tooltip listener for a given node to make 
00322   // this work.  Not crucial, we aren't removing 
00323   // tooltips from any nodes in the app just yet.
00324   return NS_ERROR_NOT_IMPLEMENTED;
00325 }
00326 
00327 NS_IMETHODIMP_(nsrefcnt) 
00328 nsRootBoxFrame::AddRef(void)
00329 {
00330   return NS_OK;
00331 }
00332 
00333 NS_IMETHODIMP_(nsrefcnt)
00334 nsRootBoxFrame::Release(void)
00335 {
00336   return NS_OK;
00337 }
00338 
00339 NS_INTERFACE_MAP_BEGIN(nsRootBoxFrame)
00340   NS_INTERFACE_MAP_ENTRY(nsIRootBox)
00341 NS_INTERFACE_MAP_END_INHERITING(nsBoxFrame)
00342 
00343 #ifdef DEBUG
00344 NS_IMETHODIMP
00345 nsRootBoxFrame::GetFrameName(nsString& aResult) const
00346 {
00347   return MakeFrameName(NS_LITERAL_STRING("RootBox"), aResult);
00348 }
00349 #endif