blob: b7bb760295894813fec5d778e0f37fd994d5d5b2 [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);
jaepark35512aa2016-08-29 17:15:08 -070059 pAnnotHandler->ReleaseAnnot(pAnnot);
jaepark98e10192016-08-15 10:51:11 -070060}
61
62void CPDFSDK_AnnotHandlerMgr::Annot_OnCreate(CPDFSDK_Annot* pAnnot) {
63 CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
64
65 CPDFSDK_DateTime curTime;
dsinclair38fd8442016-09-15 10:15:32 -070066 pPDFAnnot->GetAnnotDict()->SetStringFor("M", curTime.ToPDFDateTimeString());
67 pPDFAnnot->GetAnnotDict()->SetNumberFor("F", 0);
jaepark98e10192016-08-15 10:51:11 -070068}
69
70void CPDFSDK_AnnotHandlerMgr::Annot_OnLoad(CPDFSDK_Annot* pAnnot) {
71 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -070072 GetAnnotHandler(pAnnot)->OnLoad(pAnnot);
jaepark98e10192016-08-15 10:51:11 -070073}
74
75IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
76 CPDFSDK_Annot* pAnnot) const {
jaepark35512aa2016-08-29 17:15:08 -070077 return GetAnnotHandler(pAnnot->GetAnnotSubtype());
jaepark98e10192016-08-15 10:51:11 -070078}
79
80IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
jaepark956553e2016-08-31 06:49:27 -070081 CPDF_Annot::Subtype nAnnotSubtype) const {
82 if (nAnnotSubtype == CPDF_Annot::Subtype::WIDGET)
jaepark8c541822016-08-30 13:43:05 -070083 return m_pWidgetHandler.get();
jaepark35512aa2016-08-29 17:15:08 -070084
85#ifdef PDF_ENABLE_XFA
jaepark956553e2016-08-31 06:49:27 -070086 if (nAnnotSubtype == CPDF_Annot::Subtype::XFAWIDGET)
jaepark8c541822016-08-30 13:43:05 -070087 return m_pXFAWidgetHandler.get();
jaepark35512aa2016-08-29 17:15:08 -070088#endif // PDF_ENABLE_XFA
89
90 return m_pBAAnnotHandler.get();
jaepark98e10192016-08-15 10:51:11 -070091}
92
93void CPDFSDK_AnnotHandlerMgr::Annot_OnDraw(CPDFSDK_PageView* pPageView,
94 CPDFSDK_Annot* pAnnot,
95 CFX_RenderDevice* pDevice,
jaepark75f84a52016-09-09 15:39:09 -070096 CFX_Matrix* pUser2Device,
97 bool bDrawAnnots) {
jaepark98e10192016-08-15 10:51:11 -070098 ASSERT(pAnnot);
jaepark75f84a52016-09-09 15:39:09 -070099 GetAnnotHandler(pAnnot)->OnDraw(pPageView, pAnnot, pDevice, pUser2Device,
100 bDrawAnnots);
jaepark98e10192016-08-15 10:51:11 -0700101}
102
103FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDown(
104 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700105 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700106 uint32_t nFlags,
107 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700108 ASSERT(*pAnnot);
109 return GetAnnotHandler(pAnnot->Get())
110 ->OnLButtonDown(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700111}
112
113FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonUp(
114 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700115 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700116 uint32_t nFlags,
117 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700118 ASSERT(*pAnnot);
119 return GetAnnotHandler(pAnnot->Get())
120 ->OnLButtonUp(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700121}
122
123FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDblClk(
124 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700125 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700126 uint32_t nFlags,
127 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700128 ASSERT(*pAnnot);
129 return GetAnnotHandler(pAnnot->Get())
130 ->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700131}
132
133FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseMove(
134 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700135 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700136 uint32_t nFlags,
137 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700138 ASSERT(*pAnnot);
139 return GetAnnotHandler(pAnnot->Get())
140 ->OnMouseMove(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700141}
142
143FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseWheel(
144 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700145 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700146 uint32_t nFlags,
147 short zDelta,
148 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700149 ASSERT(*pAnnot);
150 return GetAnnotHandler(pAnnot->Get())
151 ->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta, point);
jaepark98e10192016-08-15 10:51:11 -0700152}
153
154FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonDown(
155 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700156 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700157 uint32_t nFlags,
158 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700159 ASSERT(*pAnnot);
160 return GetAnnotHandler(pAnnot->Get())
161 ->OnRButtonDown(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700162}
163
164FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonUp(
165 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700166 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700167 uint32_t nFlags,
168 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700169 ASSERT(*pAnnot);
170 return GetAnnotHandler(pAnnot->Get())
171 ->OnRButtonUp(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700172}
173
tsepezf8074ce2016-09-27 14:29:57 -0700174void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseEnter(
175 CPDFSDK_PageView* pPageView,
176 CPDFSDK_Annot::ObservedPtr* pAnnot,
177 uint32_t nFlag) {
178 ASSERT(*pAnnot);
179 GetAnnotHandler(pAnnot->Get())->OnMouseEnter(pPageView, pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700180}
181
tsepezf8074ce2016-09-27 14:29:57 -0700182void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseExit(
183 CPDFSDK_PageView* pPageView,
184 CPDFSDK_Annot::ObservedPtr* pAnnot,
185 uint32_t nFlag) {
186 ASSERT(*pAnnot);
187 GetAnnotHandler(pAnnot->Get())->OnMouseExit(pPageView, pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700188}
189
190FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChar(CPDFSDK_Annot* pAnnot,
191 uint32_t nChar,
192 uint32_t nFlags) {
jaepark35512aa2016-08-29 17:15:08 -0700193 return GetAnnotHandler(pAnnot)->OnChar(pAnnot, nChar, nFlags);
jaepark98e10192016-08-15 10:51:11 -0700194}
195
196FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyDown(CPDFSDK_Annot* pAnnot,
197 int nKeyCode,
198 int nFlag) {
dsinclair8e0638b2016-09-22 11:06:02 -0700199 if (m_pEnv->IsCTRLKeyDown(nFlag) || m_pEnv->IsALTKeyDown(nFlag))
jaepark35512aa2016-08-29 17:15:08 -0700200 return GetAnnotHandler(pAnnot)->OnKeyDown(pAnnot, nKeyCode, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700201
jaepark35512aa2016-08-29 17:15:08 -0700202 CPDFSDK_PageView* pPage = pAnnot->GetPageView();
203 CPDFSDK_Annot* pFocusAnnot = pPage->GetFocusAnnot();
204 if (pFocusAnnot && (nKeyCode == FWL_VKEY_Tab)) {
tsepezf8074ce2016-09-27 14:29:57 -0700205 CPDFSDK_Annot::ObservedPtr pNext(
206 GetNextAnnot(pFocusAnnot, !m_pEnv->IsSHIFTKeyDown(nFlag)));
207 if (pNext && pNext.Get() != pFocusAnnot) {
dsinclairb402b172016-10-11 09:26:32 -0700208 pPage->GetFormFillEnv()->GetSDKDocument()->SetFocusAnnot(&pNext);
jaepark35512aa2016-08-29 17:15:08 -0700209 return TRUE;
jaepark98e10192016-08-15 10:51:11 -0700210 }
211 }
212
jaepark98e10192016-08-15 10:51:11 -0700213 return FALSE;
214}
215
216FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyUp(CPDFSDK_Annot* pAnnot,
217 int nKeyCode,
218 int nFlag) {
219 return FALSE;
220}
221
tsepezf8074ce2016-09-27 14:29:57 -0700222FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnSetFocus(
223 CPDFSDK_Annot::ObservedPtr* pAnnot,
224 uint32_t nFlag) {
225 ASSERT(*pAnnot);
dsinclairb402b172016-10-11 09:26:32 -0700226 return GetAnnotHandler(pAnnot->Get())->OnSetFocus(pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700227}
228
tsepezf8074ce2016-09-27 14:29:57 -0700229FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKillFocus(
230 CPDFSDK_Annot::ObservedPtr* pAnnot,
231 uint32_t nFlag) {
232 ASSERT(*pAnnot);
233 return GetAnnotHandler(pAnnot->Get())->OnKillFocus(pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700234}
235
236#ifdef PDF_ENABLE_XFA
237FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChangeFocus(
tsepezf8074ce2016-09-27 14:29:57 -0700238 CPDFSDK_Annot::ObservedPtr* pSetAnnot,
239 CPDFSDK_Annot::ObservedPtr* pKillAnnot) {
240 FX_BOOL bXFA = (*pSetAnnot && (*pSetAnnot)->GetXFAWidget()) ||
241 (*pKillAnnot && (*pKillAnnot)->GetXFAWidget());
jaepark98e10192016-08-15 10:51:11 -0700242
243 if (bXFA) {
244 if (IPDFSDK_AnnotHandler* pXFAAnnotHandler =
jaepark956553e2016-08-31 06:49:27 -0700245 GetAnnotHandler(CPDF_Annot::Subtype::XFAWIDGET))
jaepark98e10192016-08-15 10:51:11 -0700246 return pXFAAnnotHandler->OnXFAChangedFocus(pKillAnnot, pSetAnnot);
247 }
248
249 return TRUE;
250}
251#endif // PDF_ENABLE_XFA
252
253CFX_FloatRect CPDFSDK_AnnotHandlerMgr::Annot_OnGetViewBBox(
254 CPDFSDK_PageView* pPageView,
255 CPDFSDK_Annot* pAnnot) {
256 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -0700257 return GetAnnotHandler(pAnnot)->GetViewBBox(pPageView, pAnnot);
jaepark98e10192016-08-15 10:51:11 -0700258}
259
260FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnHitTest(CPDFSDK_PageView* pPageView,
261 CPDFSDK_Annot* pAnnot,
262 const CFX_FloatPoint& point) {
263 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -0700264 IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot);
265 if (pAnnotHandler->CanAnswer(pAnnot))
266 return pAnnotHandler->HitTest(pPageView, pAnnot, point);
267
jaepark98e10192016-08-15 10:51:11 -0700268 return FALSE;
269}
270
271CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::GetNextAnnot(CPDFSDK_Annot* pSDKAnnot,
272 FX_BOOL bNext) {
273#ifdef PDF_ENABLE_XFA
274 CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView();
275 CPDFXFA_Page* pPage = pPageView->GetPDFXFAPage();
276 if (!pPage)
277 return nullptr;
278 if (pPage->GetPDFPage()) { // for pdf annots.
jaepark9ed91372016-08-26 16:16:10 -0700279 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(),
280 pSDKAnnot->GetAnnotSubtype());
jaepark98e10192016-08-15 10:51:11 -0700281 CPDFSDK_Annot* pNext =
282 bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
283 return pNext;
284 }
285 // for xfa annots
286 std::unique_ptr<IXFA_WidgetIterator> pWidgetIterator(
287 pPage->GetXFAPageView()->CreateWidgetIterator(
288 XFA_TRAVERSEWAY_Tranvalse, XFA_WidgetStatus_Visible |
289 XFA_WidgetStatus_Viewable |
290 XFA_WidgetStatus_Focused));
291 if (!pWidgetIterator)
292 return nullptr;
293 if (pWidgetIterator->GetCurrentWidget() != pSDKAnnot->GetXFAWidget())
294 pWidgetIterator->SetCurrentWidget(pSDKAnnot->GetXFAWidget());
295 CXFA_FFWidget* hNextFocus =
296 bNext ? pWidgetIterator->MoveToNext() : pWidgetIterator->MoveToPrevious();
297 if (!hNextFocus && pSDKAnnot)
298 hNextFocus = pWidgetIterator->MoveToFirst();
299
300 return pPageView->GetAnnotByXFAWidget(hNextFocus);
301#else // PDF_ENABLE_XFA
jaepark956553e2016-08-31 06:49:27 -0700302 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(), CPDF_Annot::Subtype::WIDGET);
jaepark98e10192016-08-15 10:51:11 -0700303 return bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
304#endif // PDF_ENABLE_XFA
305}