dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 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 | |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 7 | #include "fpdfsdk/cpdfsdk_pageview.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 8 | |
thestig | 7c292e0 | 2016-09-28 14:14:26 -0700 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <vector> |
| 11 | |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 12 | #include "core/fpdfapi/parser/cpdf_document.h" |
dsinclair | 69d9c68 | 2016-10-04 12:18:35 -0700 | [diff] [blame] | 13 | #include "core/fpdfapi/render/cpdf_renderoptions.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 14 | #include "core/fpdfdoc/cpdf_annotlist.h" |
| 15 | #include "core/fpdfdoc/cpdf_interform.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 16 | #include "fpdfsdk/cpdfsdk_annot.h" |
| 17 | #include "fpdfsdk/cpdfsdk_annothandlermgr.h" |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [diff] [blame] | 18 | #include "fpdfsdk/cpdfsdk_annotiteration.h" |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 19 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 20 | #include "fpdfsdk/cpdfsdk_interform.h" |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 21 | #include "third_party/base/ptr_util.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 22 | |
| 23 | #ifdef PDF_ENABLE_XFA |
dsinclair | 4d29e78 | 2016-10-04 14:02:47 -0700 | [diff] [blame] | 24 | #include "fpdfsdk/fpdfxfa/cpdfxfa_page.h" |
dsinclair | 5b49309 | 2016-09-29 20:20:24 -0700 | [diff] [blame] | 25 | #include "xfa/fxfa/xfa_ffdocview.h" |
| 26 | #include "xfa/fxfa/xfa_ffpageview.h" |
| 27 | #include "xfa/fxfa/xfa_ffwidgethandler.h" |
| 28 | #include "xfa/fxfa/xfa_rendercontext.h" |
dsinclair | 202ad72 | 2016-09-29 17:41:42 -0700 | [diff] [blame] | 29 | #include "xfa/fxgraphics/cfx_graphics.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 30 | #endif // PDF_ENABLE_XFA |
| 31 | |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 32 | CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_FormFillEnvironment* pFormFillEnv, |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 33 | UnderlyingPageType* page) |
| 34 | : m_page(page), |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 35 | m_pFormFillEnv(pFormFillEnv), |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 36 | #ifndef PDF_ENABLE_XFA |
| 37 | m_bOwnsPage(false), |
| 38 | #endif // PDF_ENABLE_XFA |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 39 | m_bEnterWidget(false), |
| 40 | m_bExitWidget(false), |
| 41 | m_bOnWidget(false), |
| 42 | m_bValid(false), |
| 43 | m_bLocked(false), |
dsinclair | bcf4623 | 2016-10-03 13:02:27 -0700 | [diff] [blame] | 44 | m_bBeingDestroyed(false) { |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 45 | CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 46 | if (pInterForm) { |
| 47 | CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm(); |
| 48 | #ifdef PDF_ENABLE_XFA |
| 49 | if (page->GetPDFPage()) |
| 50 | pPDFInterForm->FixPageFields(page->GetPDFPage()); |
| 51 | #else // PDF_ENABLE_XFA |
| 52 | pPDFInterForm->FixPageFields(page); |
| 53 | #endif // PDF_ENABLE_XFA |
| 54 | } |
| 55 | #ifndef PDF_ENABLE_XFA |
| 56 | m_page->SetView(this); |
| 57 | #endif // PDF_ENABLE_XFA |
| 58 | } |
| 59 | |
| 60 | CPDFSDK_PageView::~CPDFSDK_PageView() { |
| 61 | #ifndef PDF_ENABLE_XFA |
| 62 | // The call to |ReleaseAnnot| can cause the page pointed to by |m_page| to |
| 63 | // be freed, which will cause issues if we try to cleanup the pageview pointer |
| 64 | // in |m_page|. So, reset the pageview pointer before doing anything else. |
| 65 | m_page->SetView(nullptr); |
| 66 | #endif // PDF_ENABLE_XFA |
| 67 | |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 68 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = |
| 69 | m_pFormFillEnv->GetAnnotHandlerMgr(); |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 70 | for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 71 | pAnnotHandlerMgr->ReleaseAnnot(pAnnot); |
| 72 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 73 | m_SDKAnnotArray.clear(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 74 | m_pAnnotList.reset(); |
| 75 | |
| 76 | #ifndef PDF_ENABLE_XFA |
| 77 | if (m_bOwnsPage) |
| 78 | delete m_page; |
| 79 | #endif // PDF_ENABLE_XFA |
| 80 | } |
| 81 | |
| 82 | void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice, |
| 83 | CFX_Matrix* pUser2Device, |
| 84 | #ifdef PDF_ENABLE_XFA |
| 85 | CPDF_RenderOptions* pOptions, |
| 86 | const FX_RECT& pClip) { |
| 87 | #else |
| 88 | CPDF_RenderOptions* pOptions) { |
| 89 | #endif // PDF_ENABLE_XFA |
| 90 | m_curMatrix = *pUser2Device; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 91 | |
| 92 | #ifdef PDF_ENABLE_XFA |
| 93 | CPDFXFA_Page* pPage = GetPDFXFAPage(); |
| 94 | if (!pPage) |
| 95 | return; |
| 96 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 97 | if (pPage->GetContext()->GetDocType() == DOCTYPE_DYNAMIC_XFA) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 98 | CFX_Graphics gs; |
| 99 | gs.Create(pDevice); |
Dan Sinclair | bba2a7c | 2017-02-07 16:36:39 -0500 | [diff] [blame^] | 100 | CFX_RectF rectClip(static_cast<FX_FLOAT>(pClip.left), |
| 101 | static_cast<FX_FLOAT>(pClip.top), |
| 102 | static_cast<FX_FLOAT>(pClip.Width()), |
| 103 | static_cast<FX_FLOAT>(pClip.Height())); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 104 | gs.SetClipRect(rectClip); |
| 105 | std::unique_ptr<CXFA_RenderContext> pRenderContext(new CXFA_RenderContext); |
| 106 | CXFA_RenderOptions renderOptions; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 107 | renderOptions.m_bHighlight = true; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 108 | CXFA_FFPageView* xfaView = pPage->GetXFAPageView(); |
| 109 | pRenderContext->StartRender(xfaView, &gs, *pUser2Device, renderOptions); |
| 110 | pRenderContext->DoRender(); |
| 111 | pRenderContext->StopRender(); |
| 112 | CXFA_FFDocView* docView = xfaView->GetDocView(); |
| 113 | if (!docView) |
| 114 | return; |
| 115 | CPDFSDK_Annot* annot = GetFocusAnnot(); |
| 116 | if (!annot) |
| 117 | return; |
| 118 | // Render the focus widget |
| 119 | docView->GetWidgetHandler()->RenderWidget(annot->GetXFAWidget(), &gs, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 120 | pUser2Device, false); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | #endif // PDF_ENABLE_XFA |
| 124 | |
| 125 | // for pdf/static xfa. |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [diff] [blame] | 126 | CPDFSDK_AnnotIteration annotIteration(this, true); |
| 127 | for (const auto& pSDKAnnot : annotIteration) { |
| 128 | m_pFormFillEnv->GetAnnotHandlerMgr()->Annot_OnDraw( |
| 129 | this, pSDKAnnot.Get(), pDevice, pUser2Device, pOptions->m_bDrawAnnots); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 133 | CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX, |
| 134 | FX_FLOAT pageY) { |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 135 | CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr(); |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [diff] [blame] | 136 | CPDFSDK_AnnotIteration annotIteration(this, false); |
| 137 | for (const auto& pSDKAnnot : annotIteration) { |
| 138 | CFX_FloatRect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get()); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 139 | if (pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::POPUP) |
| 140 | continue; |
| 141 | if (rc.Contains(pageX, pageY)) |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [diff] [blame] | 142 | return pSDKAnnot.Get(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 143 | } |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 144 | return nullptr; |
| 145 | } |
| 146 | |
| 147 | CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX, |
| 148 | FX_FLOAT pageY) { |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 149 | CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr(); |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [diff] [blame] | 150 | CPDFSDK_AnnotIteration annotIteration(this, false); |
| 151 | for (const auto& pSDKAnnot : annotIteration) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 152 | bool bHitTest = pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET; |
| 153 | #ifdef PDF_ENABLE_XFA |
| 154 | bHitTest = bHitTest || |
| 155 | pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::XFAWIDGET; |
| 156 | #endif // PDF_ENABLE_XFA |
| 157 | if (bHitTest) { |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [diff] [blame] | 158 | pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get()); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 159 | CFX_FloatPoint point(pageX, pageY); |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [diff] [blame] | 160 | if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot.Get(), point)) |
| 161 | return pSDKAnnot.Get(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 164 | return nullptr; |
| 165 | } |
| 166 | |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 167 | #ifdef PDF_ENABLE_XFA |
| 168 | CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CXFA_FFWidget* pPDFAnnot) { |
| 169 | if (!pPDFAnnot) |
| 170 | return nullptr; |
| 171 | |
| 172 | CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot); |
| 173 | if (pSDKAnnot) |
| 174 | return pSDKAnnot; |
| 175 | |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 176 | CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 177 | pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this); |
| 178 | if (!pSDKAnnot) |
| 179 | return nullptr; |
| 180 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 181 | m_SDKAnnotArray.push_back(pSDKAnnot); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 182 | return pSDKAnnot; |
| 183 | } |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 184 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 185 | bool CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 186 | if (!pAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 187 | return false; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 188 | CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage(); |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 189 | if (!pPage || (pPage->GetContext()->GetDocType() != DOCTYPE_STATIC_XFA && |
| 190 | pPage->GetContext()->GetDocType() != DOCTYPE_DYNAMIC_XFA)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 191 | return false; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 192 | |
| 193 | if (GetFocusAnnot() == pAnnot) |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 194 | m_pFormFillEnv->KillFocusAnnot(0); |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 195 | CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 196 | if (pAnnotHandler) |
| 197 | pAnnotHandler->ReleaseAnnot(pAnnot); |
| 198 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 199 | auto it = std::find(m_SDKAnnotArray.begin(), m_SDKAnnotArray.end(), pAnnot); |
| 200 | if (it != m_SDKAnnotArray.end()) |
| 201 | m_SDKAnnotArray.erase(it); |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 202 | if (m_pCaptureWidget.Get() == pAnnot) |
| 203 | m_pCaptureWidget.Reset(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 204 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 205 | return true; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 206 | } |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 207 | #endif // PDF_ENABLE_XFA |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 208 | |
| 209 | CPDF_Document* CPDFSDK_PageView::GetPDFDocument() { |
| 210 | if (m_page) { |
| 211 | #ifdef PDF_ENABLE_XFA |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 212 | return m_page->GetContext()->GetPDFDoc(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 213 | #else // PDF_ENABLE_XFA |
| 214 | return m_page->m_pDocument; |
| 215 | #endif // PDF_ENABLE_XFA |
| 216 | } |
| 217 | return nullptr; |
| 218 | } |
| 219 | |
| 220 | CPDF_Page* CPDFSDK_PageView::GetPDFPage() const { |
| 221 | #ifdef PDF_ENABLE_XFA |
| 222 | return m_page ? m_page->GetPDFPage() : nullptr; |
| 223 | #else // PDF_ENABLE_XFA |
| 224 | return m_page; |
| 225 | #endif // PDF_ENABLE_XFA |
| 226 | } |
| 227 | |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 228 | CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) { |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 229 | for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 230 | if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict) |
| 231 | return pAnnot; |
| 232 | } |
| 233 | return nullptr; |
| 234 | } |
| 235 | |
| 236 | #ifdef PDF_ENABLE_XFA |
| 237 | CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(CXFA_FFWidget* hWidget) { |
| 238 | if (!hWidget) |
| 239 | return nullptr; |
| 240 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 241 | for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 242 | if (pAnnot->GetXFAWidget() == hWidget) |
| 243 | return pAnnot; |
| 244 | } |
| 245 | return nullptr; |
| 246 | } |
| 247 | #endif // PDF_ENABLE_XFA |
| 248 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 249 | bool CPDFSDK_PageView::OnLButtonDown(const CFX_FloatPoint& point, |
| 250 | uint32_t nFlag) { |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 251 | CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point.x, point.y)); |
| 252 | if (!pAnnot) { |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 253 | m_pFormFillEnv->KillFocusAnnot(nFlag); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 254 | return false; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 255 | } |
| 256 | |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 257 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = |
| 258 | m_pFormFillEnv->GetAnnotHandlerMgr(); |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 259 | if (!pAnnotHandlerMgr->Annot_OnLButtonDown(this, &pAnnot, nFlag, point)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 260 | return false; |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 261 | |
| 262 | if (!pAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 263 | return false; |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 264 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 265 | m_pFormFillEnv->SetFocusAnnot(&pAnnot); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 266 | return true; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | #ifdef PDF_ENABLE_XFA |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 270 | bool CPDFSDK_PageView::OnRButtonDown(const CFX_FloatPoint& point, |
| 271 | uint32_t nFlag) { |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 272 | CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point.x, point.y)); |
| 273 | if (!pAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 274 | return false; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 275 | |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 276 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = |
| 277 | m_pFormFillEnv->GetAnnotHandlerMgr(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 278 | bool ok = pAnnotHandlerMgr->Annot_OnRButtonDown(this, &pAnnot, nFlag, point); |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 279 | if (!pAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 280 | return false; |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 281 | |
| 282 | if (ok) |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 283 | m_pFormFillEnv->SetFocusAnnot(&pAnnot); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 284 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 285 | return true; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 286 | } |
| 287 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 288 | bool CPDFSDK_PageView::OnRButtonUp(const CFX_FloatPoint& point, |
| 289 | uint32_t nFlag) { |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 290 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = |
| 291 | m_pFormFillEnv->GetAnnotHandlerMgr(); |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 292 | CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point.x, point.y)); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 293 | if (!pFXAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 294 | return false; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 295 | |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 296 | if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point)) |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 297 | m_pFormFillEnv->SetFocusAnnot(&pFXAnnot); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 298 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 299 | return true; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 300 | } |
| 301 | #endif // PDF_ENABLE_XFA |
| 302 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 303 | bool CPDFSDK_PageView::OnLButtonUp(const CFX_FloatPoint& point, |
| 304 | uint32_t nFlag) { |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 305 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = |
| 306 | m_pFormFillEnv->GetAnnotHandlerMgr(); |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 307 | CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point.x, point.y)); |
| 308 | CPDFSDK_Annot::ObservedPtr pFocusAnnot(GetFocusAnnot()); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 309 | if (pFocusAnnot && pFocusAnnot != pFXAnnot) { |
| 310 | // Last focus Annot gets a chance to handle the event. |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 311 | if (pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFocusAnnot, nFlag, point)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 312 | return true; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 313 | } |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 314 | return pFXAnnot && |
| 315 | pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFXAnnot, nFlag, point); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 316 | } |
| 317 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 318 | bool CPDFSDK_PageView::OnMouseMove(const CFX_FloatPoint& point, int nFlag) { |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 319 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = |
| 320 | m_pFormFillEnv->GetAnnotHandlerMgr(); |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 321 | CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXAnnotAtPoint(point.x, point.y)); |
| 322 | if (pFXAnnot) { |
| 323 | if (m_pCaptureWidget && m_pCaptureWidget != pFXAnnot) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 324 | m_bExitWidget = true; |
| 325 | m_bEnterWidget = false; |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 326 | pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 327 | } |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 328 | m_pCaptureWidget.Reset(pFXAnnot.Get()); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 329 | m_bOnWidget = true; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 330 | if (!m_bEnterWidget) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 331 | m_bEnterWidget = true; |
| 332 | m_bExitWidget = false; |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 333 | pAnnotHandlerMgr->Annot_OnMouseEnter(this, &pFXAnnot, nFlag); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 334 | } |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 335 | pAnnotHandlerMgr->Annot_OnMouseMove(this, &pFXAnnot, nFlag, point); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 336 | return true; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 337 | } |
| 338 | if (m_bOnWidget) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 339 | m_bOnWidget = false; |
| 340 | m_bExitWidget = true; |
| 341 | m_bEnterWidget = false; |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 342 | if (m_pCaptureWidget) { |
| 343 | pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag); |
| 344 | m_pCaptureWidget.Reset(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 347 | return false; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 348 | } |
| 349 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 350 | bool CPDFSDK_PageView::OnMouseWheel(double deltaX, |
| 351 | double deltaY, |
| 352 | const CFX_FloatPoint& point, |
| 353 | int nFlag) { |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 354 | CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point.x, point.y)); |
| 355 | if (!pAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 356 | return false; |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 357 | |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 358 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = |
| 359 | m_pFormFillEnv->GetAnnotHandlerMgr(); |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 360 | return pAnnotHandlerMgr->Annot_OnMouseWheel(this, &pAnnot, nFlag, (int)deltaY, |
| 361 | point); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 362 | } |
| 363 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 364 | bool CPDFSDK_PageView::OnChar(int nChar, uint32_t nFlag) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 365 | if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) { |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 366 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = |
| 367 | m_pFormFillEnv->GetAnnotHandlerMgr(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 368 | return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag); |
| 369 | } |
| 370 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 371 | return false; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 372 | } |
| 373 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 374 | bool CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 375 | if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) { |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 376 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = |
| 377 | m_pFormFillEnv->GetAnnotHandlerMgr(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 378 | return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag); |
| 379 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 380 | return false; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 381 | } |
| 382 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 383 | bool CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) { |
| 384 | return false; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void CPDFSDK_PageView::LoadFXAnnots() { |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 388 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = |
| 389 | m_pFormFillEnv->GetAnnotHandlerMgr(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 390 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 391 | SetLock(true); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 392 | |
| 393 | #ifdef PDF_ENABLE_XFA |
| 394 | CFX_RetainPtr<CPDFXFA_Page> protector(m_page); |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 395 | if (m_pFormFillEnv->GetXFAContext()->GetDocType() == DOCTYPE_DYNAMIC_XFA) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 396 | CXFA_FFPageView* pageView = m_page->GetXFAPageView(); |
| 397 | std::unique_ptr<IXFA_WidgetIterator> pWidgetHander( |
| 398 | pageView->CreateWidgetIterator( |
| 399 | XFA_TRAVERSEWAY_Form, |
| 400 | XFA_WidgetStatus_Visible | XFA_WidgetStatus_Viewable)); |
| 401 | if (!pWidgetHander) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 402 | SetLock(false); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 403 | return; |
| 404 | } |
| 405 | |
| 406 | while (CXFA_FFWidget* pXFAAnnot = pWidgetHander->MoveToNext()) { |
| 407 | CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this); |
| 408 | if (!pAnnot) |
| 409 | continue; |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 410 | m_SDKAnnotArray.push_back(pAnnot); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 411 | pAnnotHandlerMgr->Annot_OnLoad(pAnnot); |
| 412 | } |
| 413 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 414 | SetLock(false); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | #endif // PDF_ENABLE_XFA |
| 418 | |
| 419 | CPDF_Page* pPage = GetPDFPage(); |
| 420 | ASSERT(pPage); |
thestig | 7c292e0 | 2016-09-28 14:14:26 -0700 | [diff] [blame] | 421 | bool bUpdateAP = CPDF_InterForm::IsUpdateAPEnabled(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 422 | // Disable the default AP construction. |
thestig | 7c292e0 | 2016-09-28 14:14:26 -0700 | [diff] [blame] | 423 | CPDF_InterForm::SetUpdateAP(false); |
tsepez | 36eb4bd | 2016-10-03 15:24:27 -0700 | [diff] [blame] | 424 | m_pAnnotList = pdfium::MakeUnique<CPDF_AnnotList>(pPage); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 425 | CPDF_InterForm::SetUpdateAP(bUpdateAP); |
| 426 | |
| 427 | const size_t nCount = m_pAnnotList->Count(); |
| 428 | for (size_t i = 0; i < nCount; ++i) { |
| 429 | CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i); |
| 430 | CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot); |
| 431 | CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this); |
| 432 | if (!pAnnot) |
| 433 | continue; |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 434 | m_SDKAnnotArray.push_back(pAnnot); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 435 | pAnnotHandlerMgr->Annot_OnLoad(pAnnot); |
| 436 | } |
| 437 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 438 | SetLock(false); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 439 | } |
| 440 | |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 441 | void CPDFSDK_PageView::UpdateRects(const std::vector<CFX_FloatRect>& rects) { |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 442 | for (const auto& rc : rects) |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 443 | m_pFormFillEnv->Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) { |
| 447 | CFX_FloatRect rcWindow = pAnnot->GetRect(); |
dsinclair | b402b17 | 2016-10-11 09:26:32 -0700 | [diff] [blame] | 448 | m_pFormFillEnv->Invalidate(m_page, rcWindow.left, rcWindow.top, |
| 449 | rcWindow.right, rcWindow.bottom); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | int CPDFSDK_PageView::GetPageIndex() const { |
| 453 | if (!m_page) |
| 454 | return -1; |
| 455 | |
| 456 | #ifdef PDF_ENABLE_XFA |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 457 | int nDocType = m_page->GetContext()->GetDocType(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 458 | switch (nDocType) { |
| 459 | case DOCTYPE_DYNAMIC_XFA: { |
| 460 | CXFA_FFPageView* pPageView = m_page->GetXFAPageView(); |
| 461 | return pPageView ? pPageView->GetPageIndex() : -1; |
| 462 | } |
| 463 | case DOCTYPE_STATIC_XFA: |
| 464 | case DOCTYPE_PDF: |
| 465 | return GetPageIndexForStaticPDF(); |
| 466 | default: |
| 467 | return -1; |
| 468 | } |
| 469 | #else // PDF_ENABLE_XFA |
| 470 | return GetPageIndexForStaticPDF(); |
| 471 | #endif // PDF_ENABLE_XFA |
| 472 | } |
| 473 | |
| 474 | bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const { |
| 475 | if (!p) |
| 476 | return false; |
| 477 | |
| 478 | const auto& annots = m_pAnnotList->All(); |
| 479 | auto it = std::find_if(annots.begin(), annots.end(), |
| 480 | [p](const std::unique_ptr<CPDF_Annot>& annot) { |
| 481 | return annot.get() == p; |
| 482 | }); |
| 483 | return it != annots.end(); |
| 484 | } |
| 485 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 486 | bool CPDFSDK_PageView::IsValidSDKAnnot(const CPDFSDK_Annot* p) const { |
| 487 | if (!p) |
| 488 | return false; |
| 489 | return pdfium::ContainsValue(m_SDKAnnotArray, p); |
| 490 | } |
| 491 | |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 492 | CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() { |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 493 | CPDFSDK_Annot* pFocusAnnot = m_pFormFillEnv->GetFocusAnnot(); |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 494 | return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr; |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | int CPDFSDK_PageView::GetPageIndexForStaticPDF() const { |
| 498 | CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict; |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 499 | CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument(); |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 500 | return (pDoc && pDict) ? pDoc->GetPageIndex(pDict->GetObjNum()) : -1; |
| 501 | } |