• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "xfa/fxfa/layout/cxfa_layoutitem.h"
8 
9 #include <utility>
10 
11 #include "fxjs/xfa/cjx_object.h"
12 #include "xfa/fxfa/cxfa_ffnotify.h"
13 #include "xfa/fxfa/layout/cxfa_contentlayoutitem.h"
14 #include "xfa/fxfa/layout/cxfa_layoutprocessor.h"
15 #include "xfa/fxfa/layout/cxfa_viewlayoutitem.h"
16 #include "xfa/fxfa/parser/cxfa_margin.h"
17 #include "xfa/fxfa/parser/cxfa_measurement.h"
18 #include "xfa/fxfa/parser/cxfa_node.h"
19 
XFA_ReleaseLayoutItem(const RetainPtr<CXFA_LayoutItem> & pLayoutItem)20 void XFA_ReleaseLayoutItem(const RetainPtr<CXFA_LayoutItem>& pLayoutItem) {
21   RetainPtr<CXFA_LayoutItem> pNode(pLayoutItem->GetFirstChild());
22   while (pNode) {
23     RetainPtr<CXFA_LayoutItem> pNext(pNode->GetNextSibling());
24     XFA_ReleaseLayoutItem(pNode);
25     pNode = std::move(pNext);
26   }
27   CXFA_Document* pDocument = pLayoutItem->GetFormNode()->GetDocument();
28   CXFA_FFNotify* pNotify = pDocument->GetNotify();
29   auto* pDocLayout = CXFA_LayoutProcessor::FromDocument(pDocument);
30   pNotify->OnLayoutItemRemoving(pDocLayout, pLayoutItem.Get());
31   if (pLayoutItem->GetFormNode()->GetElementType() == XFA_Element::PageArea) {
32     pNotify->OnPageEvent(ToViewLayoutItem(pLayoutItem.Get()),
33                          XFA_PAGEVIEWEVENT_PostRemoved);
34   }
35   pLayoutItem->RemoveSelfIfParented();
36 }
37 
CXFA_LayoutItem(CXFA_Node * pNode,ItemType type)38 CXFA_LayoutItem::CXFA_LayoutItem(CXFA_Node* pNode, ItemType type)
39     : m_ItemType(type), m_pFormNode(pNode) {}
40 
~CXFA_LayoutItem()41 CXFA_LayoutItem::~CXFA_LayoutItem() {
42   CHECK(!GetParent());
43   if (m_pFormNode) {
44     auto* pJSObj = m_pFormNode->JSObject();
45     if (pJSObj && pJSObj->GetLayoutItem() == this)
46       pJSObj->SetLayoutItem(nullptr);
47   }
48 }
49 
AsViewLayoutItem()50 CXFA_ViewLayoutItem* CXFA_LayoutItem::AsViewLayoutItem() {
51   return IsViewLayoutItem() ? static_cast<CXFA_ViewLayoutItem*>(this) : nullptr;
52 }
53 
AsViewLayoutItem() const54 const CXFA_ViewLayoutItem* CXFA_LayoutItem::AsViewLayoutItem() const {
55   return IsViewLayoutItem() ? static_cast<const CXFA_ViewLayoutItem*>(this)
56                             : nullptr;
57 }
58 
AsContentLayoutItem()59 CXFA_ContentLayoutItem* CXFA_LayoutItem::AsContentLayoutItem() {
60   return IsContentLayoutItem() ? static_cast<CXFA_ContentLayoutItem*>(this)
61                                : nullptr;
62 }
63 
AsContentLayoutItem() const64 const CXFA_ContentLayoutItem* CXFA_LayoutItem::AsContentLayoutItem() const {
65   return IsContentLayoutItem()
66              ? static_cast<const CXFA_ContentLayoutItem*>(this)
67              : nullptr;
68 }
69 
GetPage() const70 const CXFA_ViewLayoutItem* CXFA_LayoutItem::GetPage() const {
71   for (CXFA_LayoutItem* pCurNode = const_cast<CXFA_LayoutItem*>(this); pCurNode;
72        pCurNode = pCurNode->GetParent()) {
73     if (pCurNode->m_pFormNode->GetElementType() == XFA_Element::PageArea)
74       return pCurNode->AsViewLayoutItem();
75   }
76   return nullptr;
77 }
78