michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "inLayoutUtils.h" michael@0: michael@0: #include "nsIDocument.h" michael@0: #include "nsIDOMDocument.h" michael@0: #include "nsIContent.h" michael@0: #include "nsIContentViewer.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsIDocShell.h" michael@0: #include "nsIPresShell.h" michael@0: #include "nsPresContext.h" michael@0: #include "mozilla/EventStateManager.h" michael@0: #include "mozilla/dom/Element.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: nsIDOMWindow* michael@0: inLayoutUtils::GetWindowFor(nsIDOMNode* aNode) michael@0: { michael@0: nsCOMPtr doc1; michael@0: aNode->GetOwnerDocument(getter_AddRefs(doc1)); michael@0: return GetWindowFor(doc1.get()); michael@0: } michael@0: michael@0: nsIDOMWindow* michael@0: inLayoutUtils::GetWindowFor(nsIDOMDocument* aDoc) michael@0: { michael@0: nsCOMPtr window; michael@0: aDoc->GetDefaultView(getter_AddRefs(window)); michael@0: return window; michael@0: } michael@0: michael@0: nsIPresShell* michael@0: inLayoutUtils::GetPresShellFor(nsISupports* aThing) michael@0: { michael@0: nsCOMPtr window = do_QueryInterface(aThing); michael@0: michael@0: return window->GetDocShell()->GetPresShell(); michael@0: } michael@0: michael@0: /*static*/ michael@0: nsIFrame* michael@0: inLayoutUtils::GetFrameFor(nsIDOMElement* aElement) michael@0: { michael@0: nsCOMPtr content = do_QueryInterface(aElement); michael@0: return content->GetPrimaryFrame(); michael@0: } michael@0: michael@0: EventStateManager* michael@0: inLayoutUtils::GetEventStateManagerFor(nsIDOMElement *aElement) michael@0: { michael@0: NS_PRECONDITION(aElement, "Passing in a null element is bad"); michael@0: michael@0: nsCOMPtr domDoc; michael@0: aElement->GetOwnerDocument(getter_AddRefs(domDoc)); michael@0: nsCOMPtr doc = do_QueryInterface(domDoc); michael@0: michael@0: if (!doc) { michael@0: NS_WARNING("Could not get an nsIDocument!"); michael@0: return nullptr; michael@0: } michael@0: michael@0: nsIPresShell *shell = doc->GetShell(); michael@0: if (!shell) michael@0: return nullptr; michael@0: michael@0: return shell->GetPresContext()->EventStateManager(); michael@0: } michael@0: michael@0: nsIDOMDocument* michael@0: inLayoutUtils::GetSubDocumentFor(nsIDOMNode* aNode) michael@0: { michael@0: nsCOMPtr content = do_QueryInterface(aNode); michael@0: if (content) { michael@0: nsCOMPtr doc = content->GetDocument(); michael@0: if (doc) { michael@0: nsCOMPtr domdoc(do_QueryInterface(doc->GetSubDocumentFor(content))); michael@0: michael@0: return domdoc; michael@0: } michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: nsIDOMNode* michael@0: inLayoutUtils::GetContainerFor(const nsIDocument& aDoc) michael@0: { michael@0: nsPIDOMWindow* pwin = aDoc.GetWindow(); michael@0: if (!pwin) { michael@0: return nullptr; michael@0: } michael@0: michael@0: nsCOMPtr node = do_QueryInterface(pwin->GetFrameElementInternal()); michael@0: return node; michael@0: } michael@0: