|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "inLayoutUtils.h" |
|
7 |
|
8 #include "nsIDocument.h" |
|
9 #include "nsIDOMDocument.h" |
|
10 #include "nsIContent.h" |
|
11 #include "nsIContentViewer.h" |
|
12 #include "nsPIDOMWindow.h" |
|
13 #include "nsIDocShell.h" |
|
14 #include "nsIPresShell.h" |
|
15 #include "nsPresContext.h" |
|
16 #include "mozilla/EventStateManager.h" |
|
17 #include "mozilla/dom/Element.h" |
|
18 |
|
19 using namespace mozilla; |
|
20 |
|
21 /////////////////////////////////////////////////////////////////////////////// |
|
22 |
|
23 nsIDOMWindow* |
|
24 inLayoutUtils::GetWindowFor(nsIDOMNode* aNode) |
|
25 { |
|
26 nsCOMPtr<nsIDOMDocument> doc1; |
|
27 aNode->GetOwnerDocument(getter_AddRefs(doc1)); |
|
28 return GetWindowFor(doc1.get()); |
|
29 } |
|
30 |
|
31 nsIDOMWindow* |
|
32 inLayoutUtils::GetWindowFor(nsIDOMDocument* aDoc) |
|
33 { |
|
34 nsCOMPtr<nsIDOMWindow> window; |
|
35 aDoc->GetDefaultView(getter_AddRefs(window)); |
|
36 return window; |
|
37 } |
|
38 |
|
39 nsIPresShell* |
|
40 inLayoutUtils::GetPresShellFor(nsISupports* aThing) |
|
41 { |
|
42 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aThing); |
|
43 |
|
44 return window->GetDocShell()->GetPresShell(); |
|
45 } |
|
46 |
|
47 /*static*/ |
|
48 nsIFrame* |
|
49 inLayoutUtils::GetFrameFor(nsIDOMElement* aElement) |
|
50 { |
|
51 nsCOMPtr<nsIContent> content = do_QueryInterface(aElement); |
|
52 return content->GetPrimaryFrame(); |
|
53 } |
|
54 |
|
55 EventStateManager* |
|
56 inLayoutUtils::GetEventStateManagerFor(nsIDOMElement *aElement) |
|
57 { |
|
58 NS_PRECONDITION(aElement, "Passing in a null element is bad"); |
|
59 |
|
60 nsCOMPtr<nsIDOMDocument> domDoc; |
|
61 aElement->GetOwnerDocument(getter_AddRefs(domDoc)); |
|
62 nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc); |
|
63 |
|
64 if (!doc) { |
|
65 NS_WARNING("Could not get an nsIDocument!"); |
|
66 return nullptr; |
|
67 } |
|
68 |
|
69 nsIPresShell *shell = doc->GetShell(); |
|
70 if (!shell) |
|
71 return nullptr; |
|
72 |
|
73 return shell->GetPresContext()->EventStateManager(); |
|
74 } |
|
75 |
|
76 nsIDOMDocument* |
|
77 inLayoutUtils::GetSubDocumentFor(nsIDOMNode* aNode) |
|
78 { |
|
79 nsCOMPtr<nsIContent> content = do_QueryInterface(aNode); |
|
80 if (content) { |
|
81 nsCOMPtr<nsIDocument> doc = content->GetDocument(); |
|
82 if (doc) { |
|
83 nsCOMPtr<nsIDOMDocument> domdoc(do_QueryInterface(doc->GetSubDocumentFor(content))); |
|
84 |
|
85 return domdoc; |
|
86 } |
|
87 } |
|
88 |
|
89 return nullptr; |
|
90 } |
|
91 |
|
92 nsIDOMNode* |
|
93 inLayoutUtils::GetContainerFor(const nsIDocument& aDoc) |
|
94 { |
|
95 nsPIDOMWindow* pwin = aDoc.GetWindow(); |
|
96 if (!pwin) { |
|
97 return nullptr; |
|
98 } |
|
99 |
|
100 nsCOMPtr<nsIDOMNode> node = do_QueryInterface(pwin->GetFrameElementInternal()); |
|
101 return node; |
|
102 } |
|
103 |