blob: 51ce88e21fc7fbc24ee2bbaebacbbf15dddd5259 [file] [log] [blame]
jaepark98e10192016-08-15 10:51:11 -07001// 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
dsinclair114e46a2016-09-29 17:18:21 -07007#include "fpdfsdk/cpdfsdk_annothandlermgr.h"
jaepark98e10192016-08-15 10:51:11 -07008
dsinclair1727aee2016-09-29 13:12:56 -07009#include "core/fpdfdoc/cpdf_annot.h"
dsinclair114e46a2016-09-29 17:18:21 -070010#include "fpdfsdk/cba_annotiterator.h"
11#include "fpdfsdk/cpdfsdk_annot.h"
12#include "fpdfsdk/cpdfsdk_baannot.h"
13#include "fpdfsdk/cpdfsdk_baannothandler.h"
14#include "fpdfsdk/cpdfsdk_datetime.h"
dsinclair735606d2016-10-05 15:47:02 -070015#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070016#include "fpdfsdk/cpdfsdk_pageview.h"
17#include "fpdfsdk/cpdfsdk_widgethandler.h"
jaepark98e10192016-08-15 10:51:11 -070018
19#ifdef PDF_ENABLE_XFA
dsinclair114e46a2016-09-29 17:18:21 -070020#include "fpdfsdk/cpdfsdk_xfawidgethandler.h"
dsinclair4d29e782016-10-04 14:02:47 -070021#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
dsinclair5b493092016-09-29 20:20:24 -070022#include "xfa/fxfa/xfa_ffpageview.h"
23#include "xfa/fxfa/xfa_ffwidget.h"
jaepark98e10192016-08-15 10:51:11 -070024#endif // PDF_ENABLE_XFA
25
dsinclair735606d2016-10-05 15:47:02 -070026CPDFSDK_AnnotHandlerMgr::CPDFSDK_AnnotHandlerMgr(
dsinclair8779fa82016-10-12 12:05:44 -070027 CPDFSDK_FormFillEnvironment* pFormFillEnv)
jaepark35512aa2016-08-29 17:15:08 -070028 : m_pBAAnnotHandler(new CPDFSDK_BAAnnotHandler()),
dsinclair8779fa82016-10-12 12:05:44 -070029 m_pWidgetHandler(new CPDFSDK_WidgetHandler(pFormFillEnv)),
jaepark98e10192016-08-15 10:51:11 -070030#ifdef PDF_ENABLE_XFA
dsinclair8779fa82016-10-12 12:05:44 -070031 m_pXFAWidgetHandler(new CPDFSDK_XFAWidgetHandler(pFormFillEnv)),
jaepark98e10192016-08-15 10:51:11 -070032#endif // PDF_ENABLE_XFA
dsinclair8779fa82016-10-12 12:05:44 -070033 m_pFormFillEnv(pFormFillEnv) {
34 m_pWidgetHandler->SetFormFiller(m_pFormFillEnv->GetInteractiveFormFiller());
jaepark98e10192016-08-15 10:51:11 -070035}
36
37CPDFSDK_AnnotHandlerMgr::~CPDFSDK_AnnotHandlerMgr() {}
38
jaepark98e10192016-08-15 10:51:11 -070039CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(CPDF_Annot* pAnnot,
40 CPDFSDK_PageView* pPageView) {
41 ASSERT(pPageView);
jaepark35512aa2016-08-29 17:15:08 -070042 return GetAnnotHandler(pAnnot->GetSubtype())->NewAnnot(pAnnot, pPageView);
jaepark98e10192016-08-15 10:51:11 -070043}
44
45#ifdef PDF_ENABLE_XFA
46CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(CXFA_FFWidget* pAnnot,
47 CPDFSDK_PageView* pPageView) {
48 ASSERT(pAnnot);
49 ASSERT(pPageView);
50
jaepark956553e2016-08-31 06:49:27 -070051 return GetAnnotHandler(CPDF_Annot::Subtype::XFAWIDGET)
52 ->NewAnnot(pAnnot, pPageView);
jaepark98e10192016-08-15 10:51:11 -070053}
54#endif // PDF_ENABLE_XFA
55
56void CPDFSDK_AnnotHandlerMgr::ReleaseAnnot(CPDFSDK_Annot* pAnnot) {
jaepark35512aa2016-08-29 17:15:08 -070057 IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -070058 pAnnotHandler->ReleaseAnnot(pAnnot);
jaepark98e10192016-08-15 10:51:11 -070059}
60
61void CPDFSDK_AnnotHandlerMgr::Annot_OnCreate(CPDFSDK_Annot* pAnnot) {
62 CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
63
64 CPDFSDK_DateTime curTime;
dsinclair38fd8442016-09-15 10:15:32 -070065 pPDFAnnot->GetAnnotDict()->SetStringFor("M", curTime.ToPDFDateTimeString());
66 pPDFAnnot->GetAnnotDict()->SetNumberFor("F", 0);
jaepark98e10192016-08-15 10:51:11 -070067}
68
69void CPDFSDK_AnnotHandlerMgr::Annot_OnLoad(CPDFSDK_Annot* pAnnot) {
70 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -070071 GetAnnotHandler(pAnnot)->OnLoad(pAnnot);
jaepark98e10192016-08-15 10:51:11 -070072}
73
74IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
75 CPDFSDK_Annot* pAnnot) const {
jaepark35512aa2016-08-29 17:15:08 -070076 return GetAnnotHandler(pAnnot->GetAnnotSubtype());
jaepark98e10192016-08-15 10:51:11 -070077}
78
79IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
jaepark956553e2016-08-31 06:49:27 -070080 CPDF_Annot::Subtype nAnnotSubtype) const {
81 if (nAnnotSubtype == CPDF_Annot::Subtype::WIDGET)
jaepark8c541822016-08-30 13:43:05 -070082 return m_pWidgetHandler.get();
jaepark35512aa2016-08-29 17:15:08 -070083
84#ifdef PDF_ENABLE_XFA
jaepark956553e2016-08-31 06:49:27 -070085 if (nAnnotSubtype == CPDF_Annot::Subtype::XFAWIDGET)
jaepark8c541822016-08-30 13:43:05 -070086 return m_pXFAWidgetHandler.get();
jaepark35512aa2016-08-29 17:15:08 -070087#endif // PDF_ENABLE_XFA
88
89 return m_pBAAnnotHandler.get();
jaepark98e10192016-08-15 10:51:11 -070090}
91
92void CPDFSDK_AnnotHandlerMgr::Annot_OnDraw(CPDFSDK_PageView* pPageView,
93 CPDFSDK_Annot* pAnnot,
94 CFX_RenderDevice* pDevice,
jaepark75f84a52016-09-09 15:39:09 -070095 CFX_Matrix* pUser2Device,
96 bool bDrawAnnots) {
jaepark98e10192016-08-15 10:51:11 -070097 ASSERT(pAnnot);
jaepark75f84a52016-09-09 15:39:09 -070098 GetAnnotHandler(pAnnot)->OnDraw(pPageView, pAnnot, pDevice, pUser2Device,
99 bDrawAnnots);
jaepark98e10192016-08-15 10:51:11 -0700100}
101
tsepez4cf55152016-11-02 14:37:54 -0700102bool CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDown(
jaepark98e10192016-08-15 10:51:11 -0700103 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700104 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700105 uint32_t nFlags,
106 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700107 ASSERT(*pAnnot);
108 return GetAnnotHandler(pAnnot->Get())
109 ->OnLButtonDown(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700110}
111
tsepez4cf55152016-11-02 14:37:54 -0700112bool CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonUp(
jaepark98e10192016-08-15 10:51:11 -0700113 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700114 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700115 uint32_t nFlags,
116 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700117 ASSERT(*pAnnot);
118 return GetAnnotHandler(pAnnot->Get())
119 ->OnLButtonUp(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700120}
121
tsepez4cf55152016-11-02 14:37:54 -0700122bool CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDblClk(
jaepark98e10192016-08-15 10:51:11 -0700123 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700124 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700125 uint32_t nFlags,
126 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700127 ASSERT(*pAnnot);
128 return GetAnnotHandler(pAnnot->Get())
129 ->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700130}
131
tsepez4cf55152016-11-02 14:37:54 -0700132bool CPDFSDK_AnnotHandlerMgr::Annot_OnMouseMove(
jaepark98e10192016-08-15 10:51:11 -0700133 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700134 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700135 uint32_t nFlags,
136 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700137 ASSERT(*pAnnot);
138 return GetAnnotHandler(pAnnot->Get())
139 ->OnMouseMove(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700140}
141
tsepez4cf55152016-11-02 14:37:54 -0700142bool CPDFSDK_AnnotHandlerMgr::Annot_OnMouseWheel(
jaepark98e10192016-08-15 10:51:11 -0700143 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700144 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700145 uint32_t nFlags,
146 short zDelta,
147 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700148 ASSERT(*pAnnot);
149 return GetAnnotHandler(pAnnot->Get())
150 ->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta, point);
jaepark98e10192016-08-15 10:51:11 -0700151}
152
tsepez4cf55152016-11-02 14:37:54 -0700153bool CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonDown(
jaepark98e10192016-08-15 10:51:11 -0700154 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700155 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700156 uint32_t nFlags,
157 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700158 ASSERT(*pAnnot);
159 return GetAnnotHandler(pAnnot->Get())
160 ->OnRButtonDown(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700161}
162
tsepez4cf55152016-11-02 14:37:54 -0700163bool CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonUp(
jaepark98e10192016-08-15 10:51:11 -0700164 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700165 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700166 uint32_t nFlags,
167 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700168 ASSERT(*pAnnot);
169 return GetAnnotHandler(pAnnot->Get())
170 ->OnRButtonUp(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700171}
172
tsepezf8074ce2016-09-27 14:29:57 -0700173void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseEnter(
174 CPDFSDK_PageView* pPageView,
175 CPDFSDK_Annot::ObservedPtr* pAnnot,
176 uint32_t nFlag) {
177 ASSERT(*pAnnot);
178 GetAnnotHandler(pAnnot->Get())->OnMouseEnter(pPageView, pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700179}
180
tsepezf8074ce2016-09-27 14:29:57 -0700181void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseExit(
182 CPDFSDK_PageView* pPageView,
183 CPDFSDK_Annot::ObservedPtr* pAnnot,
184 uint32_t nFlag) {
185 ASSERT(*pAnnot);
186 GetAnnotHandler(pAnnot->Get())->OnMouseExit(pPageView, pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700187}
188
tsepez4cf55152016-11-02 14:37:54 -0700189bool CPDFSDK_AnnotHandlerMgr::Annot_OnChar(CPDFSDK_Annot* pAnnot,
190 uint32_t nChar,
191 uint32_t nFlags) {
jaepark35512aa2016-08-29 17:15:08 -0700192 return GetAnnotHandler(pAnnot)->OnChar(pAnnot, nChar, nFlags);
jaepark98e10192016-08-15 10:51:11 -0700193}
194
tsepez4cf55152016-11-02 14:37:54 -0700195bool CPDFSDK_AnnotHandlerMgr::Annot_OnKeyDown(CPDFSDK_Annot* pAnnot,
196 int nKeyCode,
197 int nFlag) {
dsinclair8779fa82016-10-12 12:05:44 -0700198 if (m_pFormFillEnv->IsCTRLKeyDown(nFlag) ||
thestig7b3252f2016-11-08 21:30:11 -0800199 m_pFormFillEnv->IsALTKeyDown(nFlag)) {
jaepark35512aa2016-08-29 17:15:08 -0700200 return GetAnnotHandler(pAnnot)->OnKeyDown(pAnnot, nKeyCode, nFlag);
thestig7b3252f2016-11-08 21:30:11 -0800201 }
jaepark98e10192016-08-15 10:51:11 -0700202
jaepark35512aa2016-08-29 17:15:08 -0700203 CPDFSDK_PageView* pPage = pAnnot->GetPageView();
204 CPDFSDK_Annot* pFocusAnnot = pPage->GetFocusAnnot();
205 if (pFocusAnnot && (nKeyCode == FWL_VKEY_Tab)) {
tsepezf8074ce2016-09-27 14:29:57 -0700206 CPDFSDK_Annot::ObservedPtr pNext(
dsinclair8779fa82016-10-12 12:05:44 -0700207 GetNextAnnot(pFocusAnnot, !m_pFormFillEnv->IsSHIFTKeyDown(nFlag)));
tsepezf8074ce2016-09-27 14:29:57 -0700208 if (pNext && pNext.Get() != pFocusAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700209 pPage->GetFormFillEnv()->SetFocusAnnot(&pNext);
tsepez4cf55152016-11-02 14:37:54 -0700210 return true;
jaepark98e10192016-08-15 10:51:11 -0700211 }
212 }
213
thestig7b3252f2016-11-08 21:30:11 -0800214 return GetAnnotHandler(pAnnot)->OnKeyDown(pAnnot, nKeyCode, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700215}
216
tsepez4cf55152016-11-02 14:37:54 -0700217bool CPDFSDK_AnnotHandlerMgr::Annot_OnKeyUp(CPDFSDK_Annot* pAnnot,
218 int nKeyCode,
219 int nFlag) {
220 return false;
jaepark98e10192016-08-15 10:51:11 -0700221}
222
tsepez4cf55152016-11-02 14:37:54 -0700223bool CPDFSDK_AnnotHandlerMgr::Annot_OnSetFocus(
tsepezf8074ce2016-09-27 14:29:57 -0700224 CPDFSDK_Annot::ObservedPtr* pAnnot,
225 uint32_t nFlag) {
226 ASSERT(*pAnnot);
dsinclairb402b172016-10-11 09:26:32 -0700227 return GetAnnotHandler(pAnnot->Get())->OnSetFocus(pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700228}
229
tsepez4cf55152016-11-02 14:37:54 -0700230bool CPDFSDK_AnnotHandlerMgr::Annot_OnKillFocus(
tsepezf8074ce2016-09-27 14:29:57 -0700231 CPDFSDK_Annot::ObservedPtr* pAnnot,
232 uint32_t nFlag) {
233 ASSERT(*pAnnot);
234 return GetAnnotHandler(pAnnot->Get())->OnKillFocus(pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700235}
236
237#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700238bool CPDFSDK_AnnotHandlerMgr::Annot_OnChangeFocus(
tsepezf8074ce2016-09-27 14:29:57 -0700239 CPDFSDK_Annot::ObservedPtr* pSetAnnot,
240 CPDFSDK_Annot::ObservedPtr* pKillAnnot) {
tsepez4cf55152016-11-02 14:37:54 -0700241 bool bXFA = (*pSetAnnot && (*pSetAnnot)->GetXFAWidget()) ||
242 (*pKillAnnot && (*pKillAnnot)->GetXFAWidget());
jaepark98e10192016-08-15 10:51:11 -0700243
244 if (bXFA) {
245 if (IPDFSDK_AnnotHandler* pXFAAnnotHandler =
jaepark956553e2016-08-31 06:49:27 -0700246 GetAnnotHandler(CPDF_Annot::Subtype::XFAWIDGET))
jaepark98e10192016-08-15 10:51:11 -0700247 return pXFAAnnotHandler->OnXFAChangedFocus(pKillAnnot, pSetAnnot);
248 }
249
tsepez4cf55152016-11-02 14:37:54 -0700250 return true;
jaepark98e10192016-08-15 10:51:11 -0700251}
252#endif // PDF_ENABLE_XFA
253
254CFX_FloatRect CPDFSDK_AnnotHandlerMgr::Annot_OnGetViewBBox(
255 CPDFSDK_PageView* pPageView,
256 CPDFSDK_Annot* pAnnot) {
257 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -0700258 return GetAnnotHandler(pAnnot)->GetViewBBox(pPageView, pAnnot);
jaepark98e10192016-08-15 10:51:11 -0700259}
260
tsepez4cf55152016-11-02 14:37:54 -0700261bool CPDFSDK_AnnotHandlerMgr::Annot_OnHitTest(CPDFSDK_PageView* pPageView,
262 CPDFSDK_Annot* pAnnot,
263 const CFX_FloatPoint& point) {
jaepark98e10192016-08-15 10:51:11 -0700264 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -0700265 IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot);
266 if (pAnnotHandler->CanAnswer(pAnnot))
267 return pAnnotHandler->HitTest(pPageView, pAnnot, point);
268
tsepez4cf55152016-11-02 14:37:54 -0700269 return false;
jaepark98e10192016-08-15 10:51:11 -0700270}
271
272CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::GetNextAnnot(CPDFSDK_Annot* pSDKAnnot,
tsepez4cf55152016-11-02 14:37:54 -0700273 bool bNext) {
jaepark98e10192016-08-15 10:51:11 -0700274#ifdef PDF_ENABLE_XFA
275 CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView();
276 CPDFXFA_Page* pPage = pPageView->GetPDFXFAPage();
277 if (!pPage)
278 return nullptr;
279 if (pPage->GetPDFPage()) { // for pdf annots.
jaepark9ed91372016-08-26 16:16:10 -0700280 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(),
281 pSDKAnnot->GetAnnotSubtype());
jaepark98e10192016-08-15 10:51:11 -0700282 CPDFSDK_Annot* pNext =
283 bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
284 return pNext;
285 }
286 // for xfa annots
287 std::unique_ptr<IXFA_WidgetIterator> pWidgetIterator(
288 pPage->GetXFAPageView()->CreateWidgetIterator(
289 XFA_TRAVERSEWAY_Tranvalse, XFA_WidgetStatus_Visible |
290 XFA_WidgetStatus_Viewable |
291 XFA_WidgetStatus_Focused));
292 if (!pWidgetIterator)
293 return nullptr;
294 if (pWidgetIterator->GetCurrentWidget() != pSDKAnnot->GetXFAWidget())
295 pWidgetIterator->SetCurrentWidget(pSDKAnnot->GetXFAWidget());
296 CXFA_FFWidget* hNextFocus =
297 bNext ? pWidgetIterator->MoveToNext() : pWidgetIterator->MoveToPrevious();
298 if (!hNextFocus && pSDKAnnot)
299 hNextFocus = pWidgetIterator->MoveToFirst();
300
301 return pPageView->GetAnnotByXFAWidget(hNextFocus);
302#else // PDF_ENABLE_XFA
jaepark956553e2016-08-31 06:49:27 -0700303 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(), CPDF_Annot::Subtype::WIDGET);
jaepark98e10192016-08-15 10:51:11 -0700304 return bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
305#endif // PDF_ENABLE_XFA
306}