blob: 32ff8d635e8134e73c8df053bd9389c064a3fad5 [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"
16#include "fpdfsdk/cpdfsdk_environment.h"
17#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
dsinclair8e0638b2016-09-22 11:06:02 -070027CPDFSDK_AnnotHandlerMgr::CPDFSDK_AnnotHandlerMgr(CPDFSDK_Environment* pEnv)
jaepark35512aa2016-08-29 17:15:08 -070028 : m_pBAAnnotHandler(new CPDFSDK_BAAnnotHandler()),
dsinclair8e0638b2016-09-22 11:06:02 -070029 m_pWidgetHandler(new CPDFSDK_WidgetHandler(pEnv)),
jaepark98e10192016-08-15 10:51:11 -070030#ifdef PDF_ENABLE_XFA
dsinclair8e0638b2016-09-22 11:06:02 -070031 m_pXFAWidgetHandler(new CPDFSDK_XFAWidgetHandler(pEnv)),
jaepark98e10192016-08-15 10:51:11 -070032#endif // PDF_ENABLE_XFA
dsinclair8e0638b2016-09-22 11:06:02 -070033 m_pEnv(pEnv) {
34 m_pWidgetHandler->SetFormFiller(m_pEnv->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);
58 pAnnotHandler->OnRelease(pAnnot);
59 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
jaepark35512aa2016-08-29 17:15:08 -070069 GetAnnotHandler(pAnnot)->OnCreate(pAnnot);
jaepark98e10192016-08-15 10:51:11 -070070}
71
72void CPDFSDK_AnnotHandlerMgr::Annot_OnLoad(CPDFSDK_Annot* pAnnot) {
73 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -070074 GetAnnotHandler(pAnnot)->OnLoad(pAnnot);
jaepark98e10192016-08-15 10:51:11 -070075}
76
77IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
78 CPDFSDK_Annot* pAnnot) const {
jaepark35512aa2016-08-29 17:15:08 -070079 return GetAnnotHandler(pAnnot->GetAnnotSubtype());
jaepark98e10192016-08-15 10:51:11 -070080}
81
82IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
jaepark956553e2016-08-31 06:49:27 -070083 CPDF_Annot::Subtype nAnnotSubtype) const {
84 if (nAnnotSubtype == CPDF_Annot::Subtype::WIDGET)
jaepark8c541822016-08-30 13:43:05 -070085 return m_pWidgetHandler.get();
jaepark35512aa2016-08-29 17:15:08 -070086
87#ifdef PDF_ENABLE_XFA
jaepark956553e2016-08-31 06:49:27 -070088 if (nAnnotSubtype == CPDF_Annot::Subtype::XFAWIDGET)
jaepark8c541822016-08-30 13:43:05 -070089 return m_pXFAWidgetHandler.get();
jaepark35512aa2016-08-29 17:15:08 -070090#endif // PDF_ENABLE_XFA
91
92 return m_pBAAnnotHandler.get();
jaepark98e10192016-08-15 10:51:11 -070093}
94
95void CPDFSDK_AnnotHandlerMgr::Annot_OnDraw(CPDFSDK_PageView* pPageView,
96 CPDFSDK_Annot* pAnnot,
97 CFX_RenderDevice* pDevice,
jaepark75f84a52016-09-09 15:39:09 -070098 CFX_Matrix* pUser2Device,
99 bool bDrawAnnots) {
jaepark98e10192016-08-15 10:51:11 -0700100 ASSERT(pAnnot);
jaepark75f84a52016-09-09 15:39:09 -0700101 GetAnnotHandler(pAnnot)->OnDraw(pPageView, pAnnot, pDevice, pUser2Device,
102 bDrawAnnots);
jaepark98e10192016-08-15 10:51:11 -0700103}
104
105FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDown(
106 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700107 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700108 uint32_t nFlags,
109 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700110 ASSERT(*pAnnot);
111 return GetAnnotHandler(pAnnot->Get())
112 ->OnLButtonDown(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700113}
114
115FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonUp(
116 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700117 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700118 uint32_t nFlags,
119 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700120 ASSERT(*pAnnot);
121 return GetAnnotHandler(pAnnot->Get())
122 ->OnLButtonUp(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700123}
124
125FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDblClk(
126 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700127 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700128 uint32_t nFlags,
129 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700130 ASSERT(*pAnnot);
131 return GetAnnotHandler(pAnnot->Get())
132 ->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700133}
134
135FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseMove(
136 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700137 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700138 uint32_t nFlags,
139 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700140 ASSERT(*pAnnot);
141 return GetAnnotHandler(pAnnot->Get())
142 ->OnMouseMove(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700143}
144
145FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseWheel(
146 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700147 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700148 uint32_t nFlags,
149 short zDelta,
150 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700151 ASSERT(*pAnnot);
152 return GetAnnotHandler(pAnnot->Get())
153 ->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta, point);
jaepark98e10192016-08-15 10:51:11 -0700154}
155
156FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonDown(
157 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700158 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700159 uint32_t nFlags,
160 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700161 ASSERT(*pAnnot);
162 return GetAnnotHandler(pAnnot->Get())
163 ->OnRButtonDown(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700164}
165
166FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonUp(
167 CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700168 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark98e10192016-08-15 10:51:11 -0700169 uint32_t nFlags,
170 const CFX_FloatPoint& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700171 ASSERT(*pAnnot);
172 return GetAnnotHandler(pAnnot->Get())
173 ->OnRButtonUp(pPageView, pAnnot, nFlags, point);
jaepark98e10192016-08-15 10:51:11 -0700174}
175
tsepezf8074ce2016-09-27 14:29:57 -0700176void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseEnter(
177 CPDFSDK_PageView* pPageView,
178 CPDFSDK_Annot::ObservedPtr* pAnnot,
179 uint32_t nFlag) {
180 ASSERT(*pAnnot);
181 GetAnnotHandler(pAnnot->Get())->OnMouseEnter(pPageView, pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700182}
183
tsepezf8074ce2016-09-27 14:29:57 -0700184void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseExit(
185 CPDFSDK_PageView* pPageView,
186 CPDFSDK_Annot::ObservedPtr* pAnnot,
187 uint32_t nFlag) {
188 ASSERT(*pAnnot);
189 GetAnnotHandler(pAnnot->Get())->OnMouseExit(pPageView, pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700190}
191
192FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChar(CPDFSDK_Annot* pAnnot,
193 uint32_t nChar,
194 uint32_t nFlags) {
jaepark35512aa2016-08-29 17:15:08 -0700195 return GetAnnotHandler(pAnnot)->OnChar(pAnnot, nChar, nFlags);
jaepark98e10192016-08-15 10:51:11 -0700196}
197
198FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyDown(CPDFSDK_Annot* pAnnot,
199 int nKeyCode,
200 int nFlag) {
dsinclair8e0638b2016-09-22 11:06:02 -0700201 if (m_pEnv->IsCTRLKeyDown(nFlag) || m_pEnv->IsALTKeyDown(nFlag))
jaepark35512aa2016-08-29 17:15:08 -0700202 return GetAnnotHandler(pAnnot)->OnKeyDown(pAnnot, nKeyCode, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700203
jaepark35512aa2016-08-29 17:15:08 -0700204 CPDFSDK_PageView* pPage = pAnnot->GetPageView();
205 CPDFSDK_Annot* pFocusAnnot = pPage->GetFocusAnnot();
206 if (pFocusAnnot && (nKeyCode == FWL_VKEY_Tab)) {
tsepezf8074ce2016-09-27 14:29:57 -0700207 CPDFSDK_Annot::ObservedPtr pNext(
208 GetNextAnnot(pFocusAnnot, !m_pEnv->IsSHIFTKeyDown(nFlag)));
209 if (pNext && pNext.Get() != pFocusAnnot) {
210 pPage->GetSDKDocument()->SetFocusAnnot(&pNext);
jaepark35512aa2016-08-29 17:15:08 -0700211 return TRUE;
jaepark98e10192016-08-15 10:51:11 -0700212 }
213 }
214
jaepark98e10192016-08-15 10:51:11 -0700215 return FALSE;
216}
217
218FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyUp(CPDFSDK_Annot* pAnnot,
219 int nKeyCode,
220 int nFlag) {
221 return FALSE;
222}
223
tsepezf8074ce2016-09-27 14:29:57 -0700224FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnSetFocus(
225 CPDFSDK_Annot::ObservedPtr* pAnnot,
226 uint32_t nFlag) {
227 ASSERT(*pAnnot);
228 if (!GetAnnotHandler(pAnnot->Get())->OnSetFocus(pAnnot, nFlag))
jaepark35512aa2016-08-29 17:15:08 -0700229 return FALSE;
230
tsepezf8074ce2016-09-27 14:29:57 -0700231 (*pAnnot)->GetPageView()->GetSDKDocument();
jaepark35512aa2016-08-29 17:15:08 -0700232 return TRUE;
jaepark98e10192016-08-15 10:51:11 -0700233}
234
tsepezf8074ce2016-09-27 14:29:57 -0700235FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKillFocus(
236 CPDFSDK_Annot::ObservedPtr* pAnnot,
237 uint32_t nFlag) {
238 ASSERT(*pAnnot);
239 return GetAnnotHandler(pAnnot->Get())->OnKillFocus(pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700240}
241
242#ifdef PDF_ENABLE_XFA
243FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChangeFocus(
tsepezf8074ce2016-09-27 14:29:57 -0700244 CPDFSDK_Annot::ObservedPtr* pSetAnnot,
245 CPDFSDK_Annot::ObservedPtr* pKillAnnot) {
246 FX_BOOL bXFA = (*pSetAnnot && (*pSetAnnot)->GetXFAWidget()) ||
247 (*pKillAnnot && (*pKillAnnot)->GetXFAWidget());
jaepark98e10192016-08-15 10:51:11 -0700248
249 if (bXFA) {
250 if (IPDFSDK_AnnotHandler* pXFAAnnotHandler =
jaepark956553e2016-08-31 06:49:27 -0700251 GetAnnotHandler(CPDF_Annot::Subtype::XFAWIDGET))
jaepark98e10192016-08-15 10:51:11 -0700252 return pXFAAnnotHandler->OnXFAChangedFocus(pKillAnnot, pSetAnnot);
253 }
254
255 return TRUE;
256}
257#endif // PDF_ENABLE_XFA
258
259CFX_FloatRect CPDFSDK_AnnotHandlerMgr::Annot_OnGetViewBBox(
260 CPDFSDK_PageView* pPageView,
261 CPDFSDK_Annot* pAnnot) {
262 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -0700263 return GetAnnotHandler(pAnnot)->GetViewBBox(pPageView, pAnnot);
jaepark98e10192016-08-15 10:51:11 -0700264}
265
266FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnHitTest(CPDFSDK_PageView* pPageView,
267 CPDFSDK_Annot* pAnnot,
268 const CFX_FloatPoint& point) {
269 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -0700270 IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot);
271 if (pAnnotHandler->CanAnswer(pAnnot))
272 return pAnnotHandler->HitTest(pPageView, pAnnot, point);
273
jaepark98e10192016-08-15 10:51:11 -0700274 return FALSE;
275}
276
277CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::GetNextAnnot(CPDFSDK_Annot* pSDKAnnot,
278 FX_BOOL bNext) {
279#ifdef PDF_ENABLE_XFA
280 CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView();
281 CPDFXFA_Page* pPage = pPageView->GetPDFXFAPage();
282 if (!pPage)
283 return nullptr;
284 if (pPage->GetPDFPage()) { // for pdf annots.
jaepark9ed91372016-08-26 16:16:10 -0700285 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(),
286 pSDKAnnot->GetAnnotSubtype());
jaepark98e10192016-08-15 10:51:11 -0700287 CPDFSDK_Annot* pNext =
288 bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
289 return pNext;
290 }
291 // for xfa annots
292 std::unique_ptr<IXFA_WidgetIterator> pWidgetIterator(
293 pPage->GetXFAPageView()->CreateWidgetIterator(
294 XFA_TRAVERSEWAY_Tranvalse, XFA_WidgetStatus_Visible |
295 XFA_WidgetStatus_Viewable |
296 XFA_WidgetStatus_Focused));
297 if (!pWidgetIterator)
298 return nullptr;
299 if (pWidgetIterator->GetCurrentWidget() != pSDKAnnot->GetXFAWidget())
300 pWidgetIterator->SetCurrentWidget(pSDKAnnot->GetXFAWidget());
301 CXFA_FFWidget* hNextFocus =
302 bNext ? pWidgetIterator->MoveToNext() : pWidgetIterator->MoveToPrevious();
303 if (!hNextFocus && pSDKAnnot)
304 hNextFocus = pWidgetIterator->MoveToFirst();
305
306 return pPageView->GetAnnotByXFAWidget(hNextFocus);
307#else // PDF_ENABLE_XFA
jaepark956553e2016-08-31 06:49:27 -0700308 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(), CPDF_Annot::Subtype::WIDGET);
jaepark98e10192016-08-15 10:51:11 -0700309 return bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
310#endif // PDF_ENABLE_XFA
311}