blob: 077c7faf54cda009537ae021cb9534134c1bf792 [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"
15#include "fpdfsdk/cpdfsdk_document.h"
dsinclair735606d2016-10-05 15:47:02 -070016#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070017#include "fpdfsdk/cpdfsdk_pageview.h"
18#include "fpdfsdk/cpdfsdk_widgethandler.h"
jaepark98e10192016-08-15 10:51:11 -070019
20#ifdef PDF_ENABLE_XFA
dsinclair114e46a2016-09-29 17:18:21 -070021#include "fpdfsdk/cpdfsdk_xfawidgethandler.h"
dsinclair4d29e782016-10-04 14:02:47 -070022#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
dsinclair5b493092016-09-29 20:20:24 -070023#include "xfa/fxfa/xfa_ffpageview.h"
24#include "xfa/fxfa/xfa_ffwidget.h"
jaepark98e10192016-08-15 10:51:11 -070025#endif // PDF_ENABLE_XFA
26
dsinclair735606d2016-10-05 15:47:02 -070027CPDFSDK_AnnotHandlerMgr::CPDFSDK_AnnotHandlerMgr(
28 CPDFSDK_FormFillEnvironment* pEnv)
jaepark35512aa2016-08-29 17:15:08 -070029 : m_pBAAnnotHandler(new CPDFSDK_BAAnnotHandler()),
dsinclair8e0638b2016-09-22 11:06:02 -070030 m_pWidgetHandler(new CPDFSDK_WidgetHandler(pEnv)),
jaepark98e10192016-08-15 10:51:11 -070031#ifdef PDF_ENABLE_XFA
dsinclair8e0638b2016-09-22 11:06:02 -070032 m_pXFAWidgetHandler(new CPDFSDK_XFAWidgetHandler(pEnv)),
jaepark98e10192016-08-15 10:51:11 -070033#endif // PDF_ENABLE_XFA
dsinclair8e0638b2016-09-22 11:06:02 -070034 m_pEnv(pEnv) {
35 m_pWidgetHandler->SetFormFiller(m_pEnv->GetInteractiveFormFiller());
jaepark98e10192016-08-15 10:51:11 -070036}
37
38CPDFSDK_AnnotHandlerMgr::~CPDFSDK_AnnotHandlerMgr() {}
39
jaepark98e10192016-08-15 10:51:11 -070040CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(CPDF_Annot* pAnnot,
41 CPDFSDK_PageView* pPageView) {
42 ASSERT(pPageView);
jaepark35512aa2016-08-29 17:15:08 -070043 return GetAnnotHandler(pAnnot->GetSubtype())->NewAnnot(pAnnot, pPageView);
jaepark98e10192016-08-15 10:51:11 -070044}
45
46#ifdef PDF_ENABLE_XFA
47CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(CXFA_FFWidget* pAnnot,
48 CPDFSDK_PageView* pPageView) {
49 ASSERT(pAnnot);
50 ASSERT(pPageView);
51
jaepark956553e2016-08-31 06:49:27 -070052 return GetAnnotHandler(CPDF_Annot::Subtype::XFAWIDGET)
53 ->NewAnnot(pAnnot, pPageView);
jaepark98e10192016-08-15 10:51:11 -070054}
55#endif // PDF_ENABLE_XFA
56
57void CPDFSDK_AnnotHandlerMgr::ReleaseAnnot(CPDFSDK_Annot* pAnnot) {
jaepark35512aa2016-08-29 17:15:08 -070058 IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot);
59 pAnnotHandler->OnRelease(pAnnot);
60 pAnnotHandler->ReleaseAnnot(pAnnot);
jaepark98e10192016-08-15 10:51:11 -070061}
62
63void CPDFSDK_AnnotHandlerMgr::Annot_OnCreate(CPDFSDK_Annot* pAnnot) {
64 CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
65
66 CPDFSDK_DateTime curTime;
dsinclair38fd8442016-09-15 10:15:32 -070067 pPDFAnnot->GetAnnotDict()->SetStringFor("M", curTime.ToPDFDateTimeString());
68 pPDFAnnot->GetAnnotDict()->SetNumberFor("F", 0);
jaepark98e10192016-08-15 10:51:11 -070069
jaepark35512aa2016-08-29 17:15:08 -070070 GetAnnotHandler(pAnnot)->OnCreate(pAnnot);
jaepark98e10192016-08-15 10:51:11 -070071}
72
73void CPDFSDK_AnnotHandlerMgr::Annot_OnLoad(CPDFSDK_Annot* pAnnot) {
74 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -070075 GetAnnotHandler(pAnnot)->OnLoad(pAnnot);
jaepark98e10192016-08-15 10:51:11 -070076}
77
78IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
79 CPDFSDK_Annot* pAnnot) const {
jaepark35512aa2016-08-29 17:15:08 -070080 return GetAnnotHandler(pAnnot->GetAnnotSubtype());
jaepark98e10192016-08-15 10:51:11 -070081}
82
83IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
jaepark956553e2016-08-31 06:49:27 -070084 CPDF_Annot::Subtype nAnnotSubtype) const {
85 if (nAnnotSubtype == CPDF_Annot::Subtype::WIDGET)
jaepark8c541822016-08-30 13:43:05 -070086 return m_pWidgetHandler.get();
jaepark35512aa2016-08-29 17:15:08 -070087
88#ifdef PDF_ENABLE_XFA
jaepark956553e2016-08-31 06:49:27 -070089 if (nAnnotSubtype == CPDF_Annot::Subtype::XFAWIDGET)
jaepark8c541822016-08-30 13:43:05 -070090 return m_pXFAWidgetHandler.get();
jaepark35512aa2016-08-29 17:15:08 -070091#endif // PDF_ENABLE_XFA
92
93 return m_pBAAnnotHandler.get();
jaepark98e10192016-08-15 10:51:11 -070094}
95
96void CPDFSDK_AnnotHandlerMgr::Annot_OnDraw(CPDFSDK_PageView* pPageView,
97 CPDFSDK_Annot* pAnnot,
98 CFX_RenderDevice* pDevice,
jaepark75f84a52016-09-09 15:39:09 -070099 CFX_Matrix* pUser2Device,
100 bool bDrawAnnots) {
jaepark98e10192016-08-15 10:51:11 -0700101 ASSERT(pAnnot);
jaepark75f84a52016-09-09 15:39:09 -0700102 GetAnnotHandler(pAnnot)->OnDraw(pPageView, pAnnot, pDevice, pUser2Device,
103 bDrawAnnots);
jaepark98e10192016-08-15 10:51:11 -0700104}
105
106FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDown(
107 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700108 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700109 uint32_t nFlags,
110 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700111 ASSERT(*pAnnot);
112 return GetAnnotHandler(pAnnot->Get())
113 ->OnLButtonDown(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700114}
115
116FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonUp(
117 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700118 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700119 uint32_t nFlags,
120 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700121 ASSERT(*pAnnot);
122 return GetAnnotHandler(pAnnot->Get())
123 ->OnLButtonUp(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700124}
125
126FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDblClk(
127 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700128 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700129 uint32_t nFlags,
130 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700131 ASSERT(*pAnnot);
132 return GetAnnotHandler(pAnnot->Get())
133 ->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700134}
135
136FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseMove(
137 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700138 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700139 uint32_t nFlags,
140 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700141 ASSERT(*pAnnot);
142 return GetAnnotHandler(pAnnot->Get())
143 ->OnMouseMove(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700144}
145
146FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseWheel(
147 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700148 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700149 uint32_t nFlags,
150 short zDelta,
151 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700152 ASSERT(*pAnnot);
153 return GetAnnotHandler(pAnnot->Get())
154 ->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta, point);
jaepark98e10192016-08-15 10:51:11 -0700155}
156
157FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonDown(
158 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700159 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700160 uint32_t nFlags,
161 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700162 ASSERT(*pAnnot);
163 return GetAnnotHandler(pAnnot->Get())
164 ->OnRButtonDown(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700165}
166
167FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonUp(
168 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700169 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700170 uint32_t nFlags,
171 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700172 ASSERT(*pAnnot);
173 return GetAnnotHandler(pAnnot->Get())
174 ->OnRButtonUp(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700175}
176
tsepezf8074ce2016-09-27 14:29:57 -0700177void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseEnter(
178 CPDFSDK_PageView* pPageView,
179 CPDFSDK_Annot::ObservedPtr* pAnnot,
180 uint32_t nFlag) {
181 ASSERT(*pAnnot);
182 GetAnnotHandler(pAnnot->Get())->OnMouseEnter(pPageView, pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700183}
184
tsepezf8074ce2016-09-27 14:29:57 -0700185void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseExit(
186 CPDFSDK_PageView* pPageView,
187 CPDFSDK_Annot::ObservedPtr* pAnnot,
188 uint32_t nFlag) {
189 ASSERT(*pAnnot);
190 GetAnnotHandler(pAnnot->Get())->OnMouseExit(pPageView, pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700191}
192
193FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChar(CPDFSDK_Annot* pAnnot,
194 uint32_t nChar,
195 uint32_t nFlags) {
jaepark35512aa2016-08-29 17:15:08 -0700196 return GetAnnotHandler(pAnnot)->OnChar(pAnnot, nChar, nFlags);
jaepark98e10192016-08-15 10:51:11 -0700197}
198
199FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyDown(CPDFSDK_Annot* pAnnot,
200 int nKeyCode,
201 int nFlag) {
dsinclair8e0638b2016-09-22 11:06:02 -0700202 if (m_pEnv->IsCTRLKeyDown(nFlag) || m_pEnv->IsALTKeyDown(nFlag))
jaepark35512aa2016-08-29 17:15:08 -0700203 return GetAnnotHandler(pAnnot)->OnKeyDown(pAnnot, nKeyCode, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700204
jaepark35512aa2016-08-29 17:15:08 -0700205 CPDFSDK_PageView* pPage = pAnnot->GetPageView();
206 CPDFSDK_Annot* pFocusAnnot = pPage->GetFocusAnnot();
207 if (pFocusAnnot && (nKeyCode == FWL_VKEY_Tab)) {
tsepezf8074ce2016-09-27 14:29:57 -0700208 CPDFSDK_Annot::ObservedPtr pNext(
209 GetNextAnnot(pFocusAnnot, !m_pEnv->IsSHIFTKeyDown(nFlag)));
210 if (pNext && pNext.Get() != pFocusAnnot) {
dsinclairb402b172016-10-11 09:26:32 -0700211 pPage->GetFormFillEnv()->GetSDKDocument()->SetFocusAnnot(&pNext);
jaepark35512aa2016-08-29 17:15:08 -0700212 return TRUE;
jaepark98e10192016-08-15 10:51:11 -0700213 }
214 }
215
jaepark98e10192016-08-15 10:51:11 -0700216 return FALSE;
217}
218
219FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyUp(CPDFSDK_Annot* pAnnot,
220 int nKeyCode,
221 int nFlag) {
222 return FALSE;
223}
224
tsepezf8074ce2016-09-27 14:29:57 -0700225FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnSetFocus(
226 CPDFSDK_Annot::ObservedPtr* pAnnot,
227 uint32_t nFlag) {
228 ASSERT(*pAnnot);
dsinclairb402b172016-10-11 09:26:32 -0700229 return GetAnnotHandler(pAnnot->Get())->OnSetFocus(pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700230}
231
tsepezf8074ce2016-09-27 14:29:57 -0700232FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKillFocus(
233 CPDFSDK_Annot::ObservedPtr* pAnnot,
234 uint32_t nFlag) {
235 ASSERT(*pAnnot);
236 return GetAnnotHandler(pAnnot->Get())->OnKillFocus(pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700237}
238
239#ifdef PDF_ENABLE_XFA
240FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChangeFocus(
tsepezf8074ce2016-09-27 14:29:57 -0700241 CPDFSDK_Annot::ObservedPtr* pSetAnnot,
242 CPDFSDK_Annot::ObservedPtr* pKillAnnot) {
243 FX_BOOL bXFA = (*pSetAnnot && (*pSetAnnot)->GetXFAWidget()) ||
244 (*pKillAnnot && (*pKillAnnot)->GetXFAWidget());
jaepark98e10192016-08-15 10:51:11 -0700245
246 if (bXFA) {
247 if (IPDFSDK_AnnotHandler* pXFAAnnotHandler =
jaepark956553e2016-08-31 06:49:27 -0700248 GetAnnotHandler(CPDF_Annot::Subtype::XFAWIDGET))
jaepark98e10192016-08-15 10:51:11 -0700249 return pXFAAnnotHandler->OnXFAChangedFocus(pKillAnnot, pSetAnnot);
250 }
251
252 return TRUE;
253}
254#endif // PDF_ENABLE_XFA
255
256CFX_FloatRect CPDFSDK_AnnotHandlerMgr::Annot_OnGetViewBBox(
257 CPDFSDK_PageView* pPageView,
258 CPDFSDK_Annot* pAnnot) {
259 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -0700260 return GetAnnotHandler(pAnnot)->GetViewBBox(pPageView, pAnnot);
jaepark98e10192016-08-15 10:51:11 -0700261}
262
263FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnHitTest(CPDFSDK_PageView* pPageView,
264 CPDFSDK_Annot* pAnnot,
265 const CFX_FloatPoint& point) {
266 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -0700267 IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot);
268 if (pAnnotHandler->CanAnswer(pAnnot))
269 return pAnnotHandler->HitTest(pPageView, pAnnot, point);
270
jaepark98e10192016-08-15 10:51:11 -0700271 return FALSE;
272}
273
274CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::GetNextAnnot(CPDFSDK_Annot* pSDKAnnot,
275 FX_BOOL bNext) {
276#ifdef PDF_ENABLE_XFA
277 CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView();
278 CPDFXFA_Page* pPage = pPageView->GetPDFXFAPage();
279 if (!pPage)
280 return nullptr;
281 if (pPage->GetPDFPage()) { // for pdf annots.
jaepark9ed91372016-08-26 16:16:10 -0700282 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(),
283 pSDKAnnot->GetAnnotSubtype());
jaepark98e10192016-08-15 10:51:11 -0700284 CPDFSDK_Annot* pNext =
285 bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
286 return pNext;
287 }
288 // for xfa annots
289 std::unique_ptr<IXFA_WidgetIterator> pWidgetIterator(
290 pPage->GetXFAPageView()->CreateWidgetIterator(
291 XFA_TRAVERSEWAY_Tranvalse, XFA_WidgetStatus_Visible |
292 XFA_WidgetStatus_Viewable |
293 XFA_WidgetStatus_Focused));
294 if (!pWidgetIterator)
295 return nullptr;
296 if (pWidgetIterator->GetCurrentWidget() != pSDKAnnot->GetXFAWidget())
297 pWidgetIterator->SetCurrentWidget(pSDKAnnot->GetXFAWidget());
298 CXFA_FFWidget* hNextFocus =
299 bNext ? pWidgetIterator->MoveToNext() : pWidgetIterator->MoveToPrevious();
300 if (!hNextFocus && pSDKAnnot)
301 hNextFocus = pWidgetIterator->MoveToFirst();
302
303 return pPageView->GetAnnotByXFAWidget(hNextFocus);
304#else // PDF_ENABLE_XFA
jaepark956553e2016-08-31 06:49:27 -0700305 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(), CPDF_Annot::Subtype::WIDGET);
jaepark98e10192016-08-15 10:51:11 -0700306 return bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
307#endif // PDF_ENABLE_XFA
308}