blob: 898b9ccc0fdec3243ce77b2d10246e633d4286ee [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
tsepez0e606b52016-11-18 16:22:41 -08009#include "core/fpdfapi/parser/cpdf_number.h"
10#include "core/fpdfapi/parser/cpdf_string.h"
dsinclair1727aee2016-09-29 13:12:56 -070011#include "core/fpdfdoc/cpdf_annot.h"
dsinclair114e46a2016-09-29 17:18:21 -070012#include "fpdfsdk/cba_annotiterator.h"
13#include "fpdfsdk/cpdfsdk_annot.h"
14#include "fpdfsdk/cpdfsdk_baannot.h"
15#include "fpdfsdk/cpdfsdk_baannothandler.h"
16#include "fpdfsdk/cpdfsdk_datetime.h"
dsinclair735606d2016-10-05 15:47:02 -070017#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070018#include "fpdfsdk/cpdfsdk_pageview.h"
19#include "fpdfsdk/cpdfsdk_widgethandler.h"
jaepark98e10192016-08-15 10:51:11 -070020
21#ifdef PDF_ENABLE_XFA
dsinclair114e46a2016-09-29 17:18:21 -070022#include "fpdfsdk/cpdfsdk_xfawidgethandler.h"
dsinclair4d29e782016-10-04 14:02:47 -070023#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
dsinclair5b493092016-09-29 20:20:24 -070024#include "xfa/fxfa/xfa_ffpageview.h"
25#include "xfa/fxfa/xfa_ffwidget.h"
jaepark98e10192016-08-15 10:51:11 -070026#endif // PDF_ENABLE_XFA
27
dsinclair735606d2016-10-05 15:47:02 -070028CPDFSDK_AnnotHandlerMgr::CPDFSDK_AnnotHandlerMgr(
dsinclair8779fa82016-10-12 12:05:44 -070029 CPDFSDK_FormFillEnvironment* pFormFillEnv)
jaepark35512aa2016-08-29 17:15:08 -070030 : m_pBAAnnotHandler(new CPDFSDK_BAAnnotHandler()),
dsinclair8779fa82016-10-12 12:05:44 -070031 m_pWidgetHandler(new CPDFSDK_WidgetHandler(pFormFillEnv)),
jaepark98e10192016-08-15 10:51:11 -070032#ifdef PDF_ENABLE_XFA
dsinclair8779fa82016-10-12 12:05:44 -070033 m_pXFAWidgetHandler(new CPDFSDK_XFAWidgetHandler(pFormFillEnv)),
jaepark98e10192016-08-15 10:51:11 -070034#endif // PDF_ENABLE_XFA
dsinclair8779fa82016-10-12 12:05:44 -070035 m_pFormFillEnv(pFormFillEnv) {
36 m_pWidgetHandler->SetFormFiller(m_pFormFillEnv->GetInteractiveFormFiller());
jaepark98e10192016-08-15 10:51:11 -070037}
38
39CPDFSDK_AnnotHandlerMgr::~CPDFSDK_AnnotHandlerMgr() {}
40
jaepark98e10192016-08-15 10:51:11 -070041CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(CPDF_Annot* pAnnot,
42 CPDFSDK_PageView* pPageView) {
43 ASSERT(pPageView);
jaepark35512aa2016-08-29 17:15:08 -070044 return GetAnnotHandler(pAnnot->GetSubtype())->NewAnnot(pAnnot, pPageView);
jaepark98e10192016-08-15 10:51:11 -070045}
46
47#ifdef PDF_ENABLE_XFA
48CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(CXFA_FFWidget* pAnnot,
49 CPDFSDK_PageView* pPageView) {
50 ASSERT(pAnnot);
51 ASSERT(pPageView);
52
jaepark956553e2016-08-31 06:49:27 -070053 return GetAnnotHandler(CPDF_Annot::Subtype::XFAWIDGET)
54 ->NewAnnot(pAnnot, pPageView);
jaepark98e10192016-08-15 10:51:11 -070055}
56#endif // PDF_ENABLE_XFA
57
58void CPDFSDK_AnnotHandlerMgr::ReleaseAnnot(CPDFSDK_Annot* pAnnot) {
jaepark35512aa2016-08-29 17:15:08 -070059 IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -070060 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;
tsepez0e606b52016-11-18 16:22:41 -080067 pPDFAnnot->GetAnnotDict()->SetNewFor<CPDF_String>(
68 "M", curTime.ToPDFDateTimeString(), false);
69 pPDFAnnot->GetAnnotDict()->SetNewFor<CPDF_Number>("F", 0);
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
tsepez4cf55152016-11-02 14:37:54 -0700105bool CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDown(
jaepark98e10192016-08-15 10:51:11 -0700106 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
tsepez4cf55152016-11-02 14:37:54 -0700115bool CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonUp(
jaepark98e10192016-08-15 10:51:11 -0700116 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
tsepez4cf55152016-11-02 14:37:54 -0700125bool CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDblClk(
jaepark98e10192016-08-15 10:51:11 -0700126 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
tsepez4cf55152016-11-02 14:37:54 -0700135bool CPDFSDK_AnnotHandlerMgr::Annot_OnMouseMove(
jaepark98e10192016-08-15 10:51:11 -0700136 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
tsepez4cf55152016-11-02 14:37:54 -0700145bool CPDFSDK_AnnotHandlerMgr::Annot_OnMouseWheel(
jaepark98e10192016-08-15 10:51:11 -0700146 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
tsepez4cf55152016-11-02 14:37:54 -0700156bool CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonDown(
jaepark98e10192016-08-15 10:51:11 -0700157 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
tsepez4cf55152016-11-02 14:37:54 -0700166bool CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonUp(
jaepark98e10192016-08-15 10:51:11 -0700167 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
tsepez4cf55152016-11-02 14:37:54 -0700192bool 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
tsepez4cf55152016-11-02 14:37:54 -0700198bool CPDFSDK_AnnotHandlerMgr::Annot_OnKeyDown(CPDFSDK_Annot* pAnnot,
199 int nKeyCode,
200 int nFlag) {
dsinclair8779fa82016-10-12 12:05:44 -0700201 if (m_pFormFillEnv->IsCTRLKeyDown(nFlag) ||
thestig7b3252f2016-11-08 21:30:11 -0800202 m_pFormFillEnv->IsALTKeyDown(nFlag)) {
jaepark35512aa2016-08-29 17:15:08 -0700203 return GetAnnotHandler(pAnnot)->OnKeyDown(pAnnot, nKeyCode, nFlag);
thestig7b3252f2016-11-08 21:30:11 -0800204 }
jaepark98e10192016-08-15 10:51:11 -0700205
jaepark35512aa2016-08-29 17:15:08 -0700206 CPDFSDK_PageView* pPage = pAnnot->GetPageView();
207 CPDFSDK_Annot* pFocusAnnot = pPage->GetFocusAnnot();
208 if (pFocusAnnot && (nKeyCode == FWL_VKEY_Tab)) {
tsepezf8074ce2016-09-27 14:29:57 -0700209 CPDFSDK_Annot::ObservedPtr pNext(
dsinclair8779fa82016-10-12 12:05:44 -0700210 GetNextAnnot(pFocusAnnot, !m_pFormFillEnv->IsSHIFTKeyDown(nFlag)));
tsepezf8074ce2016-09-27 14:29:57 -0700211 if (pNext && pNext.Get() != pFocusAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700212 pPage->GetFormFillEnv()->SetFocusAnnot(&pNext);
tsepez4cf55152016-11-02 14:37:54 -0700213 return true;
jaepark98e10192016-08-15 10:51:11 -0700214 }
215 }
216
thestig7b3252f2016-11-08 21:30:11 -0800217 return GetAnnotHandler(pAnnot)->OnKeyDown(pAnnot, nKeyCode, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700218}
219
tsepez4cf55152016-11-02 14:37:54 -0700220bool CPDFSDK_AnnotHandlerMgr::Annot_OnKeyUp(CPDFSDK_Annot* pAnnot,
221 int nKeyCode,
222 int nFlag) {
223 return false;
jaepark98e10192016-08-15 10:51:11 -0700224}
225
tsepez4cf55152016-11-02 14:37:54 -0700226bool CPDFSDK_AnnotHandlerMgr::Annot_OnSetFocus(
tsepezf8074ce2016-09-27 14:29:57 -0700227 CPDFSDK_Annot::ObservedPtr* pAnnot,
228 uint32_t nFlag) {
229 ASSERT(*pAnnot);
dsinclairb402b172016-10-11 09:26:32 -0700230 return GetAnnotHandler(pAnnot->Get())->OnSetFocus(pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700231}
232
tsepez4cf55152016-11-02 14:37:54 -0700233bool CPDFSDK_AnnotHandlerMgr::Annot_OnKillFocus(
tsepezf8074ce2016-09-27 14:29:57 -0700234 CPDFSDK_Annot::ObservedPtr* pAnnot,
235 uint32_t nFlag) {
236 ASSERT(*pAnnot);
237 return GetAnnotHandler(pAnnot->Get())->OnKillFocus(pAnnot, nFlag);
jaepark98e10192016-08-15 10:51:11 -0700238}
239
240#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700241bool CPDFSDK_AnnotHandlerMgr::Annot_OnChangeFocus(
tsepezf8074ce2016-09-27 14:29:57 -0700242 CPDFSDK_Annot::ObservedPtr* pSetAnnot,
243 CPDFSDK_Annot::ObservedPtr* pKillAnnot) {
tsepez4cf55152016-11-02 14:37:54 -0700244 bool bXFA = (*pSetAnnot && (*pSetAnnot)->GetXFAWidget()) ||
245 (*pKillAnnot && (*pKillAnnot)->GetXFAWidget());
jaepark98e10192016-08-15 10:51:11 -0700246
247 if (bXFA) {
248 if (IPDFSDK_AnnotHandler* pXFAAnnotHandler =
jaepark956553e2016-08-31 06:49:27 -0700249 GetAnnotHandler(CPDF_Annot::Subtype::XFAWIDGET))
jaepark98e10192016-08-15 10:51:11 -0700250 return pXFAAnnotHandler->OnXFAChangedFocus(pKillAnnot, pSetAnnot);
251 }
252
tsepez4cf55152016-11-02 14:37:54 -0700253 return true;
jaepark98e10192016-08-15 10:51:11 -0700254}
255#endif // PDF_ENABLE_XFA
256
257CFX_FloatRect CPDFSDK_AnnotHandlerMgr::Annot_OnGetViewBBox(
258 CPDFSDK_PageView* pPageView,
259 CPDFSDK_Annot* pAnnot) {
260 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -0700261 return GetAnnotHandler(pAnnot)->GetViewBBox(pPageView, pAnnot);
jaepark98e10192016-08-15 10:51:11 -0700262}
263
tsepez4cf55152016-11-02 14:37:54 -0700264bool CPDFSDK_AnnotHandlerMgr::Annot_OnHitTest(CPDFSDK_PageView* pPageView,
265 CPDFSDK_Annot* pAnnot,
266 const CFX_FloatPoint& point) {
jaepark98e10192016-08-15 10:51:11 -0700267 ASSERT(pAnnot);
jaepark35512aa2016-08-29 17:15:08 -0700268 IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot);
269 if (pAnnotHandler->CanAnswer(pAnnot))
270 return pAnnotHandler->HitTest(pPageView, pAnnot, point);
271
tsepez4cf55152016-11-02 14:37:54 -0700272 return false;
jaepark98e10192016-08-15 10:51:11 -0700273}
274
275CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::GetNextAnnot(CPDFSDK_Annot* pSDKAnnot,
tsepez4cf55152016-11-02 14:37:54 -0700276 bool bNext) {
jaepark98e10192016-08-15 10:51:11 -0700277#ifdef PDF_ENABLE_XFA
278 CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView();
279 CPDFXFA_Page* pPage = pPageView->GetPDFXFAPage();
280 if (!pPage)
281 return nullptr;
282 if (pPage->GetPDFPage()) { // for pdf annots.
jaepark9ed91372016-08-26 16:16:10 -0700283 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(),
284 pSDKAnnot->GetAnnotSubtype());
jaepark98e10192016-08-15 10:51:11 -0700285 CPDFSDK_Annot* pNext =
286 bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
287 return pNext;
288 }
289 // for xfa annots
290 std::unique_ptr<IXFA_WidgetIterator> pWidgetIterator(
291 pPage->GetXFAPageView()->CreateWidgetIterator(
292 XFA_TRAVERSEWAY_Tranvalse, XFA_WidgetStatus_Visible |
293 XFA_WidgetStatus_Viewable |
294 XFA_WidgetStatus_Focused));
295 if (!pWidgetIterator)
296 return nullptr;
297 if (pWidgetIterator->GetCurrentWidget() != pSDKAnnot->GetXFAWidget())
298 pWidgetIterator->SetCurrentWidget(pSDKAnnot->GetXFAWidget());
299 CXFA_FFWidget* hNextFocus =
300 bNext ? pWidgetIterator->MoveToNext() : pWidgetIterator->MoveToPrevious();
301 if (!hNextFocus && pSDKAnnot)
302 hNextFocus = pWidgetIterator->MoveToFirst();
303
304 return pPageView->GetAnnotByXFAWidget(hNextFocus);
305#else // PDF_ENABLE_XFA
jaepark956553e2016-08-31 06:49:27 -0700306 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(), CPDF_Annot::Subtype::WIDGET);
jaepark98e10192016-08-15 10:51:11 -0700307 return bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
308#endif // PDF_ENABLE_XFA
309}