Back to index

lightning-sunbird  0.9+nobinonly
nsPopupBoxObject.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 Communicator client 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  *   David W. Hyatt <hyatt@netscape.com>
00024  *   Ben Goodger <ben@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 #include "nsCOMPtr.h"
00040 #include "nsIPopupBoxObject.h"
00041 #include "nsIPopupSetFrame.h"
00042 #include "nsIRootBox.h"
00043 #include "nsBoxObject.h"
00044 #include "nsIPresShell.h"
00045 #include "nsFrameManager.h"
00046 #include "nsIContent.h"
00047 #include "nsIDocument.h"
00048 #include "nsIDOMDocument.h"
00049 #include "nsIDOMElement.h"
00050 #include "nsIFrame.h"
00051 #include "nsINameSpaceManager.h"
00052 #include "nsHTMLAtoms.h"
00053 #include "nsXULAtoms.h"
00054 #include "nsMenuPopupFrame.h"
00055 #include "nsIViewManager.h"
00056 #include "nsIWidget.h"
00057 
00058 
00059 class nsPopupBoxObject : public nsIPopupBoxObject, public nsBoxObject
00060 {
00061 public:
00062   NS_DECL_ISUPPORTS
00063   NS_DECL_NSIPOPUPBOXOBJECT
00064 
00065   nsPopupBoxObject();
00066   virtual ~nsPopupBoxObject();
00067 
00068   nsMenuPopupFrame* GetMenuPopupFrame()
00069   {
00070     nsIFrame* frame = GetFrame();
00071     if (frame && frame->GetType() == nsLayoutAtoms::menuPopupFrame)
00072       return NS_STATIC_CAST(nsMenuPopupFrame*, frame);
00073     return nsnull;
00074   }
00075 };
00076 
00077 /* Implementation file */
00078 NS_IMPL_ADDREF(nsPopupBoxObject)
00079 NS_IMPL_RELEASE(nsPopupBoxObject)
00080 
00081 NS_IMETHODIMP 
00082 nsPopupBoxObject::QueryInterface(REFNSIID iid, void** aResult)
00083 {
00084   if (!aResult)
00085     return NS_ERROR_NULL_POINTER;
00086   
00087   if (iid.Equals(NS_GET_IID(nsIPopupBoxObject))) {
00088     *aResult = (nsIPopupBoxObject*)this;
00089     NS_ADDREF(this);
00090     return NS_OK;
00091   }
00092 
00093   return nsBoxObject::QueryInterface(iid, aResult);
00094 }
00095   
00096 nsPopupBoxObject::nsPopupBoxObject()
00097 {
00098 }
00099 
00100 nsPopupBoxObject::~nsPopupBoxObject()
00101 {
00102   /* destructor code */
00103 }
00104 
00105 /* void openPopup (in boolean openFlag); */
00106 
00107 NS_IMETHODIMP
00108 nsPopupBoxObject::HidePopup()
00109 {
00110   nsIFrame* ourFrame = GetFrame();
00111   if (!ourFrame)
00112     return NS_OK;
00113 
00114   nsCOMPtr<nsIPresShell> shell = GetPresShell();
00115   if (!shell) {
00116     return NS_OK;
00117   }
00118   
00119   nsIFrame* rootFrame = shell->FrameManager()->GetRootFrame();
00120   if (!rootFrame)
00121     return NS_OK;
00122 
00123   if (rootFrame) {
00124     rootFrame = rootFrame->GetFirstChild(nsnull);
00125   }
00126 
00127   nsIRootBox* rootBox = nsnull;
00128   CallQueryInterface(rootFrame, &rootBox);
00129   if (!rootBox)
00130     return NS_OK;
00131 
00132   nsIFrame* popupSetFrame;
00133   rootBox->GetPopupSetFrame(&popupSetFrame);
00134   if (!popupSetFrame)
00135     return NS_OK;
00136 
00137   nsIPopupSetFrame* popupSet = nsnull;
00138   CallQueryInterface(popupSetFrame, &popupSet);
00139   if (!popupSet)
00140     return NS_OK;
00141 
00142   nsWeakFrame weakOurFrame(ourFrame);
00143   nsWeakFrame weakPopupSetFrame(popupSetFrame);
00144   popupSet->HidePopup(ourFrame);
00145   if (weakOurFrame.IsAlive() && weakPopupSetFrame.IsAlive()) {
00146     popupSet->DestroyPopup(ourFrame, PR_TRUE);
00147   }
00148 
00149   return NS_OK;
00150 }
00151 
00152 NS_IMETHODIMP
00153 nsPopupBoxObject::ShowPopup(nsIDOMElement* aSrcContent, 
00154                             nsIDOMElement* aPopupContent, 
00155                             PRInt32 aXPos, PRInt32 aYPos, 
00156                             const PRUnichar *aPopupType, const PRUnichar *anAnchorAlignment, 
00157                             const PRUnichar *aPopupAlignment)
00158 {
00159   nsCOMPtr<nsIPresShell> shell = GetPresShell();
00160   if (!shell) {
00161     return NS_OK;
00162   }
00163   
00164   nsIFrame* rootFrame = shell->FrameManager()->GetRootFrame();
00165   if (!rootFrame)
00166     return NS_OK;
00167 
00168   if (rootFrame) {
00169     rootFrame = rootFrame->GetFirstChild(nsnull);
00170   }
00171 
00172   nsIRootBox* rootBox = nsnull;
00173   CallQueryInterface(rootFrame, &rootBox);
00174   if (!rootBox)
00175     return NS_OK;
00176 
00177   nsIFrame* popupSetFrame;
00178   rootBox->GetPopupSetFrame(&popupSetFrame);
00179   if (!popupSetFrame)
00180     return NS_OK;
00181 
00182   nsIPopupSetFrame* popupSet = nsnull;
00183   CallQueryInterface(popupSetFrame, &popupSet);
00184   if (!popupSet)
00185     return NS_OK;
00186 
00187   nsCOMPtr<nsIContent> srcContent(do_QueryInterface(aSrcContent));
00188   nsCOMPtr<nsIContent> popupContent(do_QueryInterface(aPopupContent));
00189 
00190   nsAutoString popupType(aPopupType);
00191   nsAutoString anchorAlign(anAnchorAlignment);
00192   nsAutoString popupAlign(aPopupAlignment);
00193 
00194   // Use |left| and |top| dimension attributes to position the popup if
00195   // present, as they may have been persisted. 
00196   nsAutoString left, top;
00197   popupContent->GetAttr(kNameSpaceID_None, nsXULAtoms::left, left);
00198   popupContent->GetAttr(kNameSpaceID_None, nsXULAtoms::top, top);
00199   
00200   PRInt32 err;
00201   if (!left.IsEmpty()) {
00202     aXPos = left.ToInteger(&err);
00203     if (NS_FAILED(err))
00204       return err;
00205   }
00206   if (!top.IsEmpty()) {
00207     aYPos = top.ToInteger(&err);
00208     if (NS_FAILED(err))
00209       return err;
00210   }
00211 
00212   return popupSet->ShowPopup(srcContent, popupContent, aXPos, aYPos, 
00213                              popupType, anchorAlign, popupAlign);
00214 }
00215 
00216 NS_IMETHODIMP
00217 nsPopupBoxObject::MoveTo(PRInt32 aLeft, PRInt32 aTop)
00218 {
00219   nsMenuPopupFrame* menuPopupFrame = GetMenuPopupFrame();
00220   if (!menuPopupFrame) return NS_OK;
00221 
00222   menuPopupFrame->MoveTo(aLeft, aTop);
00223 
00224   return NS_OK;
00225 }
00226 
00227 NS_IMETHODIMP
00228 nsPopupBoxObject::SizeTo(PRInt32 aWidth, PRInt32 aHeight)
00229 {
00230   nsAutoString width, height;
00231   width.AppendInt(aWidth);
00232   height.AppendInt(aHeight);
00233 
00234   nsCOMPtr<nsIContent> content = mContent;
00235   content->SetAttr(kNameSpaceID_None, nsHTMLAtoms::width, width, PR_FALSE);
00236   content->SetAttr(kNameSpaceID_None, nsHTMLAtoms::height, height, PR_TRUE);
00237 
00238   return NS_OK;
00239 }
00240 
00241 NS_IMETHODIMP
00242 nsPopupBoxObject::GetAutoPosition(PRBool* aShouldAutoPosition)
00243 {
00244   nsMenuPopupFrame* menuPopupFrame = GetMenuPopupFrame();
00245   if (!menuPopupFrame) return NS_OK;
00246 
00247   menuPopupFrame->GetAutoPosition(aShouldAutoPosition);
00248 
00249   return NS_OK;
00250 }
00251 
00252 NS_IMETHODIMP
00253 nsPopupBoxObject::SetAutoPosition(PRBool aShouldAutoPosition)
00254 {
00255   nsMenuPopupFrame* menuPopupFrame = GetMenuPopupFrame();
00256   if (!menuPopupFrame) return NS_OK;
00257 
00258   menuPopupFrame->SetAutoPosition(aShouldAutoPosition);
00259 
00260   return NS_OK;
00261 }
00262 
00263 NS_IMETHODIMP
00264 nsPopupBoxObject::EnableRollup(PRBool aShouldRollup)
00265 {
00266   nsMenuPopupFrame* menuPopupFrame = GetMenuPopupFrame();
00267   if (!menuPopupFrame) return NS_OK;
00268 
00269   menuPopupFrame->EnableRollup(aShouldRollup);
00270 
00271   return NS_OK;
00272 }
00273 
00274 NS_IMETHODIMP
00275 nsPopupBoxObject::EnableKeyboardNavigator(PRBool aEnableKeyboardNavigator)
00276 {
00277   nsMenuPopupFrame* menuPopupFrame = GetMenuPopupFrame();
00278   if (!menuPopupFrame) return NS_OK;
00279 
00280   if (aEnableKeyboardNavigator)
00281     menuPopupFrame->InstallKeyboardNavigator();
00282   else
00283     menuPopupFrame->RemoveKeyboardNavigator();
00284   
00285   return NS_OK;
00286 }
00287 
00288 // Creation Routine ///////////////////////////////////////////////////////////////////////
00289 
00290 nsresult
00291 NS_NewPopupBoxObject(nsIBoxObject** aResult)
00292 {
00293   *aResult = new nsPopupBoxObject;
00294   if (!*aResult)
00295     return NS_ERROR_OUT_OF_MEMORY;
00296   NS_ADDREF(*aResult);
00297   return NS_OK;
00298 }
00299