Back to index

lightning-sunbird  0.9+nobinonly
nsDocShellTreeOwner.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications, Inc.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Travis Bogard <travis@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * 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 
00040 #ifndef nsDocShellTreeOwner_h__
00041 #define nsDocShellTreeOwner_h__
00042 
00043 // Helper Classes
00044 #include "nsCOMPtr.h"
00045 #include "nsString.h"
00046 
00047 // Interfaces Needed
00048 #include "nsIBaseWindow.h"
00049 #include "nsIDocShellTreeOwner.h"
00050 #include "nsIInterfaceRequestor.h"
00051 #include "nsIInterfaceRequestorUtils.h"
00052 #include "nsIWebBrowserChrome.h"
00053 #include "nsIDOMMouseListener.h"
00054 #include "nsIDOMDocument.h"
00055 #include "nsIChromeEventHandler.h"
00056 #include "nsIDOMEventReceiver.h"
00057 #include "nsIEmbeddingSiteWindow.h"
00058 #include "nsIWebProgressListener.h"
00059 #include "nsWeakReference.h"
00060 #include "nsIDOMKeyListener.h"
00061 #include "nsIDOMMouseMotionListener.h"
00062 #include "nsIDOMContextMenuListener.h"
00063 #include "nsITimer.h"
00064 #include "nsIPrompt.h"
00065 #include "nsIAuthPrompt.h"
00066 #include "nsITooltipListener.h"
00067 #include "nsITooltipTextProvider.h"
00068 #include "nsCTooltipTextProvider.h"
00069 #include "nsIDragDropHandler.h"
00070 
00071 #include "nsCommandHandler.h"
00072 
00073 class nsWebBrowser;
00074 class ChromeTooltipListener;
00075 class ChromeContextMenuListener;
00076 
00077 // {6D10C180-6888-11d4-952B-0020183BF181}
00078 #define NS_ICDOCSHELLTREEOWNER_IID \
00079 { 0x6d10c180, 0x6888, 0x11d4, { 0x95, 0x2b, 0x0, 0x20, 0x18, 0x3b, 0xf1, 0x81 } }
00080 
00081 /*
00082  * This is a fake 'hidden' interface that nsDocShellTreeOwner implements.
00083  * Classes such as nsCommandHandler can QI for this interface to be
00084  * sure that they're dealing with a valid nsDocShellTreeOwner and not some
00085  * other object that implements nsIDocShellTreeOwner.
00086  */
00087 class nsICDocShellTreeOwner : public nsISupports
00088 {
00089 public:
00090     NS_DEFINE_STATIC_IID_ACCESSOR(NS_ICDOCSHELLTREEOWNER_IID)
00091 };
00092 
00093 
00094 class nsDocShellTreeOwner : public nsIDocShellTreeOwner,
00095                             public nsIBaseWindow,
00096                             public nsIInterfaceRequestor,
00097                             public nsIWebProgressListener,
00098                             public nsICDocShellTreeOwner,
00099                             public nsSupportsWeakReference
00100 {
00101 friend class nsWebBrowser;
00102 friend class nsCommandHandler;
00103 
00104 public:
00105     NS_DECL_ISUPPORTS
00106 
00107     NS_DECL_NSIBASEWINDOW
00108     NS_DECL_NSIDOCSHELLTREEOWNER
00109     NS_DECL_NSIINTERFACEREQUESTOR
00110     NS_DECL_NSIWEBPROGRESSLISTENER
00111 
00112 protected:
00113     nsDocShellTreeOwner();
00114     virtual ~nsDocShellTreeOwner();
00115 
00116     void WebBrowser(nsWebBrowser* aWebBrowser);
00117     
00118     nsWebBrowser* WebBrowser();
00119     NS_IMETHOD SetTreeOwner(nsIDocShellTreeOwner* aTreeOwner);
00120     NS_IMETHOD SetWebBrowserChrome(nsIWebBrowserChrome* aWebBrowserChrome);
00121 
00122     NS_IMETHOD AddChromeListeners();
00123     NS_IMETHOD RemoveChromeListeners();
00124 
00125     nsresult   FindChildWithName(const PRUnichar *aName, 
00126                  PRBool aRecurse, nsIDocShellTreeItem* aRequestor,
00127                  nsIDocShellTreeItem* aOriginalRequestor,
00128                  nsIDocShellTreeItem **aFoundItem);
00129     nsresult   FindItemWithNameAcrossWindows(const PRUnichar* aName,
00130                  nsIDocShellTreeItem* aRequestor,
00131                  nsIDocShellTreeItem* aOriginalRequestor,
00132                  nsIDocShellTreeItem **aFoundItem);
00133 
00134     void       EnsurePrompter();
00135     void       EnsureAuthPrompter();
00136 
00137     void AddToWatcher();
00138     void RemoveFromWatcher();
00139 
00140     // These helper functions return the correct instances of the requested
00141     // interfaces.  If the object passed to SetWebBrowserChrome() implements
00142     // nsISupportsWeakReference, then these functions call QueryReferent on
00143     // that object.  Otherwise, they return an addrefed pointer.  If the
00144     // WebBrowserChrome object doesn't exist, they return nsnull.
00145     already_AddRefed<nsIWebBrowserChrome>     GetWebBrowserChrome();
00146     already_AddRefed<nsIEmbeddingSiteWindow>  GetOwnerWin();
00147     already_AddRefed<nsIInterfaceRequestor>   GetOwnerRequestor();
00148 
00149 protected:
00150 
00151    // Weak References
00152    nsWebBrowser*           mWebBrowser;
00153    nsIDocShellTreeOwner*   mTreeOwner;
00154    nsIDocShellTreeItem*    mPrimaryContentShell; 
00155 
00156    nsIWebBrowserChrome*    mWebBrowserChrome;
00157    nsIEmbeddingSiteWindow* mOwnerWin;
00158    nsIInterfaceRequestor*  mOwnerRequestor;
00159 
00160    nsWeakPtr               mWebBrowserChromeWeak;   // nsIWebBrowserChrome
00161 
00162     // the objects that listen for chrome events like context menus and tooltips. 
00163     // They are separate objects to avoid circular references between |this|
00164     // and the DOM. These are strong, owning refs.
00165    ChromeTooltipListener*         mChromeTooltipListener;
00166    ChromeContextMenuListener*     mChromeContextMenuListener;
00167    nsCOMPtr<nsIDragDropHandler>   mChromeDragHandler;
00168 
00169    nsCOMPtr<nsIPrompt>     mPrompter;
00170    nsCOMPtr<nsIAuthPrompt> mAuthPrompter;
00171 };
00172 
00173 
00174 //
00175 // class ChromeTooltipListener
00176 //
00177 // The class that listens to the chrome events and tells the embedding
00178 // chrome to show tooltips, as appropriate. Handles registering itself
00179 // with the DOM with AddChromeListeners() and removing itself with
00180 // RemoveChromeListeners().
00181 //
00182 class ChromeTooltipListener : public nsIDOMMouseListener,
00183                                 public nsIDOMKeyListener,
00184                                 public nsIDOMMouseMotionListener
00185 {
00186 public:
00187   NS_DECL_ISUPPORTS
00188   
00189   ChromeTooltipListener ( nsWebBrowser* inBrowser, nsIWebBrowserChrome* inChrome ) ;
00190   virtual ~ChromeTooltipListener ( ) ;
00191 
00192     // nsIDOMMouseListener
00193   NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent) {  return NS_OK; }
00194   NS_IMETHOD MouseDown(nsIDOMEvent* aMouseEvent);
00195   NS_IMETHOD MouseUp(nsIDOMEvent* aMouseEvent);
00196   NS_IMETHOD MouseClick(nsIDOMEvent* aMouseEvent);
00197   NS_IMETHOD MouseDblClick(nsIDOMEvent* aMouseEvent);
00198   NS_IMETHOD MouseOver(nsIDOMEvent* aMouseEvent);
00199   NS_IMETHOD MouseOut(nsIDOMEvent* aMouseEvent);
00200 
00201     // nsIDOMMouseMotionListener
00202   NS_IMETHOD MouseMove(nsIDOMEvent* aMouseEvent);
00203   NS_IMETHOD DragMove(nsIDOMEvent* aMouseEvent) { return NS_OK; };
00204 
00205     // nsIDOMKeyListener
00206   NS_IMETHOD KeyDown(nsIDOMEvent* aKeyEvent) ;
00207   NS_IMETHOD KeyUp(nsIDOMEvent* aKeyEvent) ;
00208   NS_IMETHOD KeyPress(nsIDOMEvent* aKeyEvent) ;
00209 
00210     // Add/remove the relevant listeners, based on what interfaces
00211     // the embedding chrome implements.
00212   NS_IMETHOD AddChromeListeners();
00213   NS_IMETHOD RemoveChromeListeners();
00214 
00215 private:
00216 
00217     // various delays for tooltips
00218   enum {
00219     kTooltipAutoHideTime = 5000,       // 5000ms = 5 seconds
00220     kTooltipShowTime = 500             // 500ms = 0.5 seconds
00221   };
00222 
00223   NS_IMETHOD AddTooltipListener();
00224   NS_IMETHOD RemoveTooltipListener();
00225 
00226   NS_IMETHOD ShowTooltip ( PRInt32 inXCoords, PRInt32 inYCoords, const nsAString & inTipText ) ;
00227   NS_IMETHOD HideTooltip ( ) ;
00228 
00229   nsWebBrowser* mWebBrowser;
00230   nsCOMPtr<nsIDOMEventReceiver> mEventReceiver;
00231   nsCOMPtr<nsITooltipTextProvider> mTooltipTextProvider;
00232   
00233     // This must be a strong ref in order to make sure we can hide the tooltip
00234     // if the window goes away while we're displaying one. If we don't hold
00235     // a strong ref, the chrome might have been disposed of before we get a chance
00236     // to tell it, and no one would ever tell us of that fact.
00237   nsCOMPtr<nsIWebBrowserChrome> mWebBrowserChrome;
00238 
00239   PRPackedBool mTooltipListenerInstalled;
00240 
00241   nsCOMPtr<nsITimer> mTooltipTimer;
00242   static void sTooltipCallback ( nsITimer* aTimer, void* aListener ) ;
00243   PRInt32 mMouseClientX, mMouseClientY;       // mouse coordinates for last mousemove event we saw
00244   PRInt32 mMouseScreenX, mMouseScreenY;       // mouse coordinates for tooltip event
00245   PRBool mShowingTooltip;
00246 
00247     // a timer for auto-hiding the tooltip after a certain delay
00248   nsCOMPtr<nsITimer> mAutoHideTimer;
00249   static void sAutoHideCallback ( nsITimer* aTimer, void* aListener ) ;
00250   void CreateAutoHideTimer ( ) ;
00251 
00252     // The node hovered over that fired the timer. This may turn into the node that
00253     // triggered the tooltip, but only if the timer ever gets around to firing.
00254     // This is a strong reference, because the tooltip content can be destroyed while we're
00255     // waiting for the tooltip to pup up, and we need to detect that.
00256     // It's set only when the tooltip timer is created and launched. The timer must
00257     // either fire or be cancelled (or possibly released?), and we release this
00258     // reference in each of those cases. So we don't leak.
00259   nsCOMPtr<nsIDOMNode> mPossibleTooltipNode;
00260 
00261 }; // ChromeTooltipListener
00262 
00263 
00264 //
00265 // class ChromeContextMenuListener
00266 //
00267 // The class that listens to the chrome events and tells the embedding
00268 // chrome to show context menus, as appropriate. Handles registering itself
00269 // with the DOM with AddChromeListeners() and removing itself with
00270 // RemoveChromeListeners().
00271 //
00272 class ChromeContextMenuListener : public nsIDOMContextMenuListener
00273 {
00274 public:
00275   NS_DECL_ISUPPORTS
00276   
00277   ChromeContextMenuListener ( nsWebBrowser* inBrowser, nsIWebBrowserChrome* inChrome ) ;
00278   virtual ~ChromeContextMenuListener ( ) ;
00279 
00280   // nsIDOMContextMenuListener
00281   NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent) {  return NS_OK; }
00282   NS_IMETHOD ContextMenu ( nsIDOMEvent* aEvent );
00283 
00284   // Add/remove the relevant listeners, based on what interfaces
00285   // the embedding chrome implements.
00286   NS_IMETHOD AddChromeListeners();
00287   NS_IMETHOD RemoveChromeListeners();
00288 
00289 private:
00290 
00291   NS_IMETHOD AddContextMenuListener();
00292   NS_IMETHOD RemoveContextMenuListener();
00293 
00294   PRPackedBool mContextMenuListenerInstalled;
00295 
00296   nsWebBrowser* mWebBrowser;
00297   nsCOMPtr<nsIDOMEventReceiver> mEventReceiver;
00298   nsCOMPtr<nsIWebBrowserChrome> mWebBrowserChrome;
00299 
00300 }; // class ChromeContextMenuListener
00301 
00302 
00303 
00304 #endif /* nsDocShellTreeOwner_h__ */
00305 
00306 
00307 
00308 
00309 
00310 
00311 
00312 
00313 
00314 
00315 
00316