xref: /aosp_15_r20/external/pdfium/xfa/fxfa/layout/cxfa_layoutitem.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2016 The PDFium Authors
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 "fxjs/xfa/cjx_object.h"
10 #include "xfa/fxfa/cxfa_ffnotify.h"
11 #include "xfa/fxfa/layout/cxfa_contentlayoutitem.h"
12 #include "xfa/fxfa/layout/cxfa_layoutprocessor.h"
13 #include "xfa/fxfa/layout/cxfa_viewlayoutitem.h"
14 #include "xfa/fxfa/parser/cxfa_margin.h"
15 #include "xfa/fxfa/parser/cxfa_measurement.h"
16 #include "xfa/fxfa/parser/cxfa_node.h"
17 
XFA_ReleaseLayoutItem(CXFA_LayoutItem * pLayoutItem)18 void XFA_ReleaseLayoutItem(CXFA_LayoutItem* pLayoutItem) {
19   CXFA_LayoutItem* pNode = pLayoutItem->GetFirstChild();
20   while (pNode) {
21     CXFA_LayoutItem* pNext = pNode->GetNextSibling();
22     XFA_ReleaseLayoutItem(pNode);
23     pNode = pNext;
24   }
25   CXFA_Document* pDocument = pLayoutItem->GetFormNode()->GetDocument();
26   CXFA_FFNotify* pNotify = pDocument->GetNotify();
27   auto* pDocLayout = CXFA_LayoutProcessor::FromDocument(pDocument);
28   pNotify->OnLayoutItemRemoving(pDocLayout, pLayoutItem);
29   if (pLayoutItem->GetFormNode()->GetElementType() == XFA_Element::PageArea) {
30     pNotify->OnPageViewEvent(ToViewLayoutItem(pLayoutItem),
31                              CXFA_FFDoc::PageViewEvent::kPostRemoved);
32   }
33   pLayoutItem->RemoveSelfIfParented();
34 }
35 
CXFA_LayoutItem(CXFA_Node * pNode,ItemType type)36 CXFA_LayoutItem::CXFA_LayoutItem(CXFA_Node* pNode, ItemType type)
37     : m_ItemType(type), m_pFormNode(pNode) {}
38 
39 CXFA_LayoutItem::~CXFA_LayoutItem() = default;
40 
PreFinalize()41 void CXFA_LayoutItem::PreFinalize() {
42   if (!m_pFormNode)
43     return;
44 
45   auto* pJSObj = m_pFormNode->JSObject();
46   if (pJSObj && pJSObj->GetLayoutItem() == this)
47     pJSObj->SetLayoutItem(nullptr);
48 }
49 
Trace(cppgc::Visitor * visitor) const50 void CXFA_LayoutItem::Trace(cppgc::Visitor* visitor) const {
51   GCedTreeNode<CXFA_LayoutItem>::Trace(visitor);
52   visitor->Trace(m_pFormNode);
53 }
54 
AsViewLayoutItem()55 CXFA_ViewLayoutItem* CXFA_LayoutItem::AsViewLayoutItem() {
56   return IsViewLayoutItem() ? static_cast<CXFA_ViewLayoutItem*>(this) : nullptr;
57 }
58 
AsViewLayoutItem() const59 const CXFA_ViewLayoutItem* CXFA_LayoutItem::AsViewLayoutItem() const {
60   return IsViewLayoutItem() ? static_cast<const CXFA_ViewLayoutItem*>(this)
61                             : nullptr;
62 }
63 
AsContentLayoutItem()64 CXFA_ContentLayoutItem* CXFA_LayoutItem::AsContentLayoutItem() {
65   return IsContentLayoutItem() ? static_cast<CXFA_ContentLayoutItem*>(this)
66                                : nullptr;
67 }
68 
AsContentLayoutItem() const69 const CXFA_ContentLayoutItem* CXFA_LayoutItem::AsContentLayoutItem() const {
70   return IsContentLayoutItem()
71              ? static_cast<const CXFA_ContentLayoutItem*>(this)
72              : nullptr;
73 }
74 
GetPage() const75 const CXFA_ViewLayoutItem* CXFA_LayoutItem::GetPage() const {
76   for (CXFA_LayoutItem* pCurNode = const_cast<CXFA_LayoutItem*>(this); pCurNode;
77        pCurNode = pCurNode->GetParent()) {
78     if (pCurNode->m_pFormNode->GetElementType() == XFA_Element::PageArea)
79       return pCurNode->AsViewLayoutItem();
80   }
81   return nullptr;
82 }
83 
SetFormNode(CXFA_Node * pNode)84 void CXFA_LayoutItem::SetFormNode(CXFA_Node* pNode) {
85   // Not in header, assignment requires complete type, not just forward decl.
86   m_pFormNode = pNode;
87 }
88