NativeWindowFromNode.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25  */
26 
32  // Abuse the accessibility interfaces to extract a native window handle from a DOM node
33 
34 #include "NativeWindowFromNode.h"
35 #include "nsIInterfaceRequestorUtils.h"
36 #include "nsIDOMNode.h"
37 
38 #include <nsCOMPtr.h>
39 #include "nsIServiceManager.h"
40 #include "nsIDOMDocument.h"
41 #include "nsIDOMDocumentView.h"
42 #include "nsIDOMAbstractView.h"
43 #include "nsIWebNavigation.h"
44 #include "nsIDocShellTreeItem.h"
45 #include "nsIDocShellTreeOwner.h"
46 #include "nsIBaseWindow.h"
47 #include "nsIWidget.h"
48 
49 //-----------------------------------------------------------------------------
51 {
52  NATIVEWINDOW wnd;
53  nsIWidget *widget = NativeWindowFromNode::getWidget(window);
54  if (!widget) return NULL;
55 
56 #ifdef XP_WIN
57  wnd = reinterpret_cast<NATIVEWINDOW>(widget->GetNativeData(NS_NATIVE_WIDGET));
58 #elif defined(XP_MACOSX)
59  wnd = reinterpret_cast<NATIVEWINDOW>(widget->GetNativeData(NS_NATIVE_WINDOW));
60 #elif defined(XP_UNIX)
61  wnd = reinterpret_cast<NATIVEWINDOW>(widget->GetNativeData(NS_NATIVE_WIDGET));
62 #endif
63 
64  return wnd;
65 } // NativeWindowFromNode::get
66 
68 {
69  nsCOMPtr<nsIDOMAbstractView> domAbstractView;
70  nsCOMPtr<nsIDOMDocumentView> domDocumentView(do_QueryInterface(window));
71 
72  //The <window> xul element does not implement nsIDOMDocumentView,
73  //so check for AbstractView if there is no DocumentView.
74  if (!domDocumentView) {
75  domAbstractView = do_QueryInterface(window);
76  if (!domAbstractView) return NULL;
77  }
78  else {
79  domDocumentView->GetDefaultView(getter_AddRefs(domAbstractView));
80  if (!domAbstractView) return NULL;
81  }
82 
83  nsCOMPtr<nsIWebNavigation> webNavigation(do_GetInterface(domAbstractView));
84  nsCOMPtr<nsIDocShellTreeItem> docShellTreeItem(do_QueryInterface(webNavigation));
85  if (!docShellTreeItem) return NULL;
86 
87  nsCOMPtr<nsIDocShellTreeOwner> docShellTreeOwner;
88  docShellTreeItem->GetTreeOwner(getter_AddRefs(docShellTreeOwner));
89  if (!docShellTreeOwner) return NULL;
90 
91  nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(docShellTreeOwner);
92  if (!baseWindow) return NULL;
93 
94  nsCOMPtr<nsIWidget> widget;
95  baseWindow->GetMainWidget(getter_AddRefs(widget));
96 
97  return widget;
98 } // NativeWindowFromNode::get
99 
static nsIWidget * getWidget(nsISupports *window)
Finds the Native window handle associated with a DOM node - Prototypes.
let window
static void * get(nsISupports *window)
#define NATIVEWINDOW