Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa / src / fxfa / src / parser / xfa_layout_appadapter.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4 \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #include "../../../foxitlib.h"\r
8 #include "../common/xfa_utils.h"\r
9 #include "../common/xfa_object.h"\r
10 #include "../common/xfa_document.h"\r
11 #include "../common/xfa_parser.h"\r
12 #include "../common/xfa_script.h"\r
13 #include "../common/xfa_docdata.h"\r
14 #include "../common/xfa_doclayout.h"\r
15 #include "../common/xfa_debug.h"\r
16 #include "../common/xfa_localemgr.h"\r
17 #include "../common/xfa_fm2jsapi.h"\r
18 #include "xfa_debug_parser.h"\r
19 #include "xfa_document_layout_imp.h"\r
20 #include "xfa_layout_itemlayout.h"\r
21 #include "xfa_layout_pagemgr_new.h"\r
22 #include "xfa_layout_appadapter.h"\r
23 IXFA_DocLayout* IXFA_LayoutPage::GetLayout() const\r
24 {\r
25     CXFA_ContainerLayoutItemImpl* pThis = (CXFA_ContainerLayoutItemImpl*)this;\r
26     return pThis->m_pFormNode->GetDocument()->GetLayoutProcessor();\r
27 }\r
28 FX_INT32 IXFA_LayoutPage::GetPageIndex() const\r
29 {\r
30     CXFA_ContainerLayoutItemImpl* pThis = (CXFA_ContainerLayoutItemImpl*)this;\r
31     return pThis->m_pFormNode->GetDocument()->GetLayoutProcessor()->GetLayoutPageMgr()->GetPageIndex((IXFA_LayoutPage*)this);\r
32 }\r
33 void IXFA_LayoutPage::GetPageSize(CFX_SizeF &size)\r
34 {\r
35     CXFA_ContainerLayoutItemImpl* pThis = (CXFA_ContainerLayoutItemImpl*)this;\r
36     size.Set(0, 0);\r
37     CXFA_Node *pMedium = pThis->m_pFormNode->GetFirstChildByClass(XFA_ELEMENT_Medium);\r
38     if (pMedium) {\r
39         size.x = pMedium->GetMeasure(XFA_ATTRIBUTE_Short).ToUnit(XFA_UNIT_Pt);\r
40         size.y = pMedium->GetMeasure(XFA_ATTRIBUTE_Long).ToUnit(XFA_UNIT_Pt);\r
41         if (pMedium->GetEnum(XFA_ATTRIBUTE_Orientation) == XFA_ATTRIBUTEENUM_Landscape) {\r
42             size.Set(size.y, size.x);\r
43         }\r
44     }\r
45 }\r
46 CXFA_Node* IXFA_LayoutPage::GetMasterPage() const\r
47 {\r
48     CXFA_ContainerLayoutItemImpl* pThis = (CXFA_ContainerLayoutItemImpl*)this;\r
49     return pThis->m_pFormNode;\r
50 }\r
51 IXFA_LayoutPage*        CXFA_LayoutItem::GetPage() const\r
52 {\r
53     CXFA_ContainerLayoutItemImpl* pThis = (CXFA_ContainerLayoutItemImpl*)this;\r
54     for(CXFA_LayoutItemImpl* pCurNode = pThis; pCurNode; pCurNode = pCurNode->m_pParent) {\r
55         if(pCurNode->m_pFormNode->GetClassID() == XFA_ELEMENT_PageArea) {\r
56             return (IXFA_LayoutPage*)pCurNode;\r
57         }\r
58     }\r
59     return NULL;\r
60 }\r
61 CXFA_Node* CXFA_LayoutItem::GetFormNode() const\r
62 {\r
63     CXFA_ContentLayoutItemImpl* pThis = (CXFA_ContentLayoutItemImpl*)this;\r
64     return pThis->m_pFormNode;\r
65 }\r
66 void CXFA_LayoutItem::GetRect(CFX_RectF &rtLayout, FX_BOOL bRelative) const\r
67 {\r
68     CXFA_ContentLayoutItemImpl* pThis = (CXFA_ContentLayoutItemImpl*)this;\r
69     CFX_PointF sPos  = pThis->m_sPos;\r
70     CFX_SizeF  sSize = pThis->m_sSize;\r
71     if (!bRelative) {\r
72         for(CXFA_LayoutItemImpl* pLayoutItem = pThis->m_pParent; pLayoutItem; pLayoutItem = pLayoutItem->m_pParent) {\r
73             if(pLayoutItem->IsContentLayoutItem()) {\r
74                 sPos += ((CXFA_ContentLayoutItemImpl*)pLayoutItem)->m_sPos;\r
75                 if(CXFA_Node* pMarginNode = pLayoutItem->m_pFormNode->GetFirstChildByClass(XFA_ELEMENT_Margin)) {\r
76                     sPos.Add(pMarginNode->GetMeasure(XFA_ATTRIBUTE_LeftInset).ToUnit(XFA_UNIT_Pt), pMarginNode->GetMeasure(XFA_ATTRIBUTE_TopInset).ToUnit(XFA_UNIT_Pt));\r
77                 }\r
78             } else {\r
79                 if(pLayoutItem->m_pFormNode->GetClassID() == XFA_ELEMENT_ContentArea) {\r
80                     sPos.Add(pLayoutItem->m_pFormNode->GetMeasure(XFA_ATTRIBUTE_X).ToUnit(XFA_UNIT_Pt), pLayoutItem->m_pFormNode->GetMeasure(XFA_ATTRIBUTE_Y).ToUnit(XFA_UNIT_Pt));\r
81                     break;\r
82                 } else if(pLayoutItem->m_pFormNode->GetClassID() == XFA_ELEMENT_PageArea) {\r
83                     break;\r
84                 }\r
85             }\r
86         }\r
87     }\r
88     rtLayout.Set(sPos.x, sPos.y, sSize.x, sSize.y);\r
89 }\r
90 CXFA_LayoutItem* CXFA_LayoutItem::GetParent() const\r
91 {\r
92     CXFA_LayoutItemImpl* pThis = (CXFA_LayoutItemImpl*)this;\r
93     return (CXFA_LayoutItem*)pThis->m_pParent;\r
94 }\r
95 CXFA_LayoutItem* CXFA_LayoutItem::GetFirst() const\r
96 {\r
97     CXFA_ContentLayoutItemImpl* pThis = (CXFA_ContentLayoutItemImpl*)this;\r
98     CXFA_ContentLayoutItemImpl* pCurNode = pThis;\r
99     while(pCurNode->m_pPrev) {\r
100         pCurNode = pCurNode->m_pPrev;\r
101     }\r
102     return (CXFA_LayoutItem*)pCurNode;\r
103 }\r
104 CXFA_LayoutItem* CXFA_LayoutItem::GetPrev() const\r
105 {\r
106     CXFA_ContentLayoutItemImpl* pThis = (CXFA_ContentLayoutItemImpl*)this;\r
107     return (CXFA_LayoutItem*)pThis->m_pPrev;\r
108 }\r
109 CXFA_LayoutItem* CXFA_LayoutItem::GetNext() const\r
110 {\r
111     CXFA_ContentLayoutItemImpl* pThis = (CXFA_ContentLayoutItemImpl*)this;\r
112     return (CXFA_LayoutItem*)pThis->m_pNext;\r
113 }\r
114 CXFA_LayoutItem* CXFA_LayoutItem::GetLast() const\r
115 {\r
116     CXFA_ContentLayoutItemImpl* pThis = (CXFA_ContentLayoutItemImpl*)this;\r
117     CXFA_ContentLayoutItemImpl* pCurNode = pThis;\r
118     while(pCurNode->m_pNext) {\r
119         pCurNode = pCurNode->m_pNext;\r
120     }\r
121     return (CXFA_LayoutItem*)pCurNode;\r
122 }\r
123 FX_INT32 CXFA_LayoutItem::GetIndex() const\r
124 {\r
125     CXFA_ContentLayoutItemImpl* pThis = (CXFA_ContentLayoutItemImpl*)this;\r
126     FX_INT32 iIndex = 0;\r
127     CXFA_ContentLayoutItemImpl* pCurNode = pThis;\r
128     while(pCurNode->m_pPrev) {\r
129         pCurNode = pCurNode->m_pPrev;\r
130         iIndex++;\r
131     }\r
132     return iIndex;\r
133 }\r
134 FX_INT32 CXFA_LayoutItem::GetCount() const\r
135 {\r
136     CXFA_ContentLayoutItemImpl* pThis = (CXFA_ContentLayoutItemImpl*)this;\r
137     FX_INT32 iCount = 1;\r
138     CXFA_ContentLayoutItemImpl* pCurNode = NULL;\r
139     pCurNode = pThis;\r
140     while(pCurNode->m_pPrev) {\r
141         pCurNode = pCurNode->m_pPrev;\r
142         iCount++;\r
143     }\r
144     pCurNode = pThis;\r
145     while(pCurNode->m_pNext) {\r
146         pCurNode = pCurNode->m_pNext;\r
147         iCount++;\r
148     }\r
149     return iCount;\r
150 }\r
151 FX_DWORD XFA_GetRelevant(CXFA_Node* pFormItem, FX_DWORD dwParentRelvant)\r
152 {\r
153     FX_DWORD dwRelevant = XFA_LAYOUTSTATUS_Viewable | XFA_LAYOUTSTATUS_Printable;\r
154     CFX_WideStringC wsRelevant;\r
155     if (pFormItem->TryCData(XFA_ATTRIBUTE_Relevant, wsRelevant)) {\r
156         if (wsRelevant == FX_WSTRC(L"+print") || wsRelevant == FX_WSTRC(L"print")) {\r
157             dwRelevant &= ~XFA_LAYOUTSTATUS_Viewable;\r
158         } else if (wsRelevant == FX_WSTRC(L"-print")) {\r
159             dwRelevant  &= ~XFA_LAYOUTSTATUS_Printable;\r
160         }\r
161     }\r
162     if (!(dwParentRelvant & XFA_LAYOUTSTATUS_Viewable) && (dwRelevant != XFA_LAYOUTSTATUS_Viewable)) {\r
163         dwRelevant &= ~XFA_LAYOUTSTATUS_Viewable;\r
164     }\r
165     if (!(dwParentRelvant & XFA_LAYOUTSTATUS_Printable) && (dwRelevant != XFA_LAYOUTSTATUS_Printable)) {\r
166         dwRelevant &= ~XFA_LAYOUTSTATUS_Printable;\r
167     }\r
168     return dwRelevant;\r
169 }\r
170 void XFA_ReleaseLayoutItem(CXFA_LayoutItemImpl *pLayoutItem)\r
171 {\r
172     CXFA_LayoutItemImpl *pNext, *pNode = pLayoutItem->m_pFirstChild;\r
173     while (pNode) {\r
174         pNext = pNode->m_pNextSibling;\r
175         pNode->m_pParent = NULL;\r
176         XFA_ReleaseLayoutItem(pNode);\r
177         pNode = pNext;\r
178     }\r
179     delete pLayoutItem;\r
180 }\r