OLD | NEW |
(Empty) | |
| 1 // 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 |
| 7 #include "fpdfsdk/include/cpdfsdk_annothandlermgr.h" |
| 8 |
| 9 #include "core/fpdfdoc/include/cpdf_annot.h" |
| 10 #include "fpdfsdk/include/cpdfsdk_annot.h" |
| 11 #include "fpdfsdk/include/cpdfsdk_baannot.h" |
| 12 #include "fpdfsdk/include/cpdfsdk_bfannothandler.h" |
| 13 #include "fpdfsdk/include/cpdfsdk_datetime.h" |
| 14 #include "fpdfsdk/include/fsdk_mgr.h" |
| 15 |
| 16 #ifdef PDF_ENABLE_XFA |
| 17 #include "fpdfsdk/include/cpdfsdk_xfaannothandler.h" |
| 18 #include "fpdfsdk/fpdfxfa/include/fpdfxfa_page.h" |
| 19 #include "xfa/fxfa/include/xfa_ffpageview.h" |
| 20 #include "xfa/fxfa/include/xfa_ffwidget.h" |
| 21 #endif // PDF_ENABLE_XFA |
| 22 |
| 23 CPDFSDK_AnnotHandlerMgr::CPDFSDK_AnnotHandlerMgr(CPDFDoc_Environment* pApp) { |
| 24 m_pApp = pApp; |
| 25 |
| 26 CPDFSDK_BFAnnotHandler* pHandler = new CPDFSDK_BFAnnotHandler(m_pApp); |
| 27 pHandler->SetFormFiller(m_pApp->GetIFormFiller()); |
| 28 RegisterAnnotHandler(pHandler); |
| 29 #ifdef PDF_ENABLE_XFA |
| 30 CPDFSDK_XFAAnnotHandler* pXFAAnnotHandler = |
| 31 new CPDFSDK_XFAAnnotHandler(m_pApp); |
| 32 RegisterAnnotHandler(pXFAAnnotHandler); |
| 33 #endif // PDF_ENABLE_XFA |
| 34 } |
| 35 |
| 36 CPDFSDK_AnnotHandlerMgr::~CPDFSDK_AnnotHandlerMgr() {} |
| 37 |
| 38 void CPDFSDK_AnnotHandlerMgr::RegisterAnnotHandler( |
| 39 IPDFSDK_AnnotHandler* pAnnotHandler) { |
| 40 ASSERT(!GetAnnotHandler(pAnnotHandler->GetType())); |
| 41 |
| 42 m_mapType2Handler[pAnnotHandler->GetType()].reset(pAnnotHandler); |
| 43 } |
| 44 |
| 45 void CPDFSDK_AnnotHandlerMgr::UnRegisterAnnotHandler( |
| 46 IPDFSDK_AnnotHandler* pAnnotHandler) { |
| 47 m_mapType2Handler.erase(pAnnotHandler->GetType()); |
| 48 } |
| 49 |
| 50 CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(CPDF_Annot* pAnnot, |
| 51 CPDFSDK_PageView* pPageView) { |
| 52 ASSERT(pPageView); |
| 53 |
| 54 if (IPDFSDK_AnnotHandler* pAnnotHandler = |
| 55 GetAnnotHandler(pAnnot->GetSubType())) { |
| 56 return pAnnotHandler->NewAnnot(pAnnot, pPageView); |
| 57 } |
| 58 |
| 59 return new CPDFSDK_BAAnnot(pAnnot, pPageView); |
| 60 } |
| 61 |
| 62 #ifdef PDF_ENABLE_XFA |
| 63 CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(CXFA_FFWidget* pAnnot, |
| 64 CPDFSDK_PageView* pPageView) { |
| 65 ASSERT(pAnnot); |
| 66 ASSERT(pPageView); |
| 67 |
| 68 if (IPDFSDK_AnnotHandler* pAnnotHandler = |
| 69 GetAnnotHandler(FSDK_XFAWIDGET_TYPENAME)) { |
| 70 return pAnnotHandler->NewAnnot(pAnnot, pPageView); |
| 71 } |
| 72 |
| 73 return nullptr; |
| 74 } |
| 75 #endif // PDF_ENABLE_XFA |
| 76 |
| 77 void CPDFSDK_AnnotHandlerMgr::ReleaseAnnot(CPDFSDK_Annot* pAnnot) { |
| 78 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) { |
| 79 pAnnotHandler->OnRelease(pAnnot); |
| 80 pAnnotHandler->ReleaseAnnot(pAnnot); |
| 81 } else { |
| 82 delete pAnnot; |
| 83 } |
| 84 } |
| 85 |
| 86 void CPDFSDK_AnnotHandlerMgr::Annot_OnCreate(CPDFSDK_Annot* pAnnot) { |
| 87 CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot(); |
| 88 |
| 89 CPDFSDK_DateTime curTime; |
| 90 pPDFAnnot->GetAnnotDict()->SetAtString("M", curTime.ToPDFDateTimeString()); |
| 91 pPDFAnnot->GetAnnotDict()->SetAtNumber("F", 0); |
| 92 |
| 93 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 94 pAnnotHandler->OnCreate(pAnnot); |
| 95 } |
| 96 |
| 97 void CPDFSDK_AnnotHandlerMgr::Annot_OnLoad(CPDFSDK_Annot* pAnnot) { |
| 98 ASSERT(pAnnot); |
| 99 |
| 100 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 101 pAnnotHandler->OnLoad(pAnnot); |
| 102 } |
| 103 |
| 104 IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler( |
| 105 CPDFSDK_Annot* pAnnot) const { |
| 106 CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot(); |
| 107 if (pPDFAnnot) |
| 108 return GetAnnotHandler(pPDFAnnot->GetSubType()); |
| 109 #ifdef PDF_ENABLE_XFA |
| 110 if (pAnnot->GetXFAWidget()) |
| 111 return GetAnnotHandler(FSDK_XFAWIDGET_TYPENAME); |
| 112 #endif // PDF_ENABLE_XFA |
| 113 return nullptr; |
| 114 } |
| 115 |
| 116 IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler( |
| 117 const CFX_ByteString& sType) const { |
| 118 auto it = m_mapType2Handler.find(sType); |
| 119 return it != m_mapType2Handler.end() ? it->second.get() : nullptr; |
| 120 } |
| 121 |
| 122 void CPDFSDK_AnnotHandlerMgr::Annot_OnDraw(CPDFSDK_PageView* pPageView, |
| 123 CPDFSDK_Annot* pAnnot, |
| 124 CFX_RenderDevice* pDevice, |
| 125 CFX_Matrix* pUser2Device, |
| 126 uint32_t dwFlags) { |
| 127 ASSERT(pAnnot); |
| 128 |
| 129 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) { |
| 130 pAnnotHandler->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags); |
| 131 } else { |
| 132 #ifdef PDF_ENABLE_XFA |
| 133 if (pAnnot->IsXFAField()) |
| 134 return; |
| 135 #endif // PDF_ENABLE_XFA |
| 136 static_cast<CPDFSDK_BAAnnot*>(pAnnot)->DrawAppearance( |
| 137 pDevice, pUser2Device, CPDF_Annot::Normal, nullptr); |
| 138 } |
| 139 } |
| 140 |
| 141 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDown( |
| 142 CPDFSDK_PageView* pPageView, |
| 143 CPDFSDK_Annot* pAnnot, |
| 144 uint32_t nFlags, |
| 145 const CFX_FloatPoint& point) { |
| 146 ASSERT(pAnnot); |
| 147 |
| 148 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 149 return pAnnotHandler->OnLButtonDown(pPageView, pAnnot, nFlags, point); |
| 150 |
| 151 return FALSE; |
| 152 } |
| 153 |
| 154 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonUp( |
| 155 CPDFSDK_PageView* pPageView, |
| 156 CPDFSDK_Annot* pAnnot, |
| 157 uint32_t nFlags, |
| 158 const CFX_FloatPoint& point) { |
| 159 ASSERT(pAnnot); |
| 160 |
| 161 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 162 return pAnnotHandler->OnLButtonUp(pPageView, pAnnot, nFlags, point); |
| 163 |
| 164 return FALSE; |
| 165 } |
| 166 |
| 167 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDblClk( |
| 168 CPDFSDK_PageView* pPageView, |
| 169 CPDFSDK_Annot* pAnnot, |
| 170 uint32_t nFlags, |
| 171 const CFX_FloatPoint& point) { |
| 172 ASSERT(pAnnot); |
| 173 |
| 174 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 175 return pAnnotHandler->OnLButtonDblClk(pPageView, pAnnot, nFlags, point); |
| 176 |
| 177 return FALSE; |
| 178 } |
| 179 |
| 180 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseMove( |
| 181 CPDFSDK_PageView* pPageView, |
| 182 CPDFSDK_Annot* pAnnot, |
| 183 uint32_t nFlags, |
| 184 const CFX_FloatPoint& point) { |
| 185 ASSERT(pAnnot); |
| 186 |
| 187 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 188 return pAnnotHandler->OnMouseMove(pPageView, pAnnot, nFlags, point); |
| 189 |
| 190 return FALSE; |
| 191 } |
| 192 |
| 193 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseWheel( |
| 194 CPDFSDK_PageView* pPageView, |
| 195 CPDFSDK_Annot* pAnnot, |
| 196 uint32_t nFlags, |
| 197 short zDelta, |
| 198 const CFX_FloatPoint& point) { |
| 199 ASSERT(pAnnot); |
| 200 |
| 201 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) { |
| 202 return pAnnotHandler->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta, |
| 203 point); |
| 204 } |
| 205 return FALSE; |
| 206 } |
| 207 |
| 208 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonDown( |
| 209 CPDFSDK_PageView* pPageView, |
| 210 CPDFSDK_Annot* pAnnot, |
| 211 uint32_t nFlags, |
| 212 const CFX_FloatPoint& point) { |
| 213 ASSERT(pAnnot); |
| 214 |
| 215 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 216 return pAnnotHandler->OnRButtonDown(pPageView, pAnnot, nFlags, point); |
| 217 |
| 218 return FALSE; |
| 219 } |
| 220 |
| 221 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonUp( |
| 222 CPDFSDK_PageView* pPageView, |
| 223 CPDFSDK_Annot* pAnnot, |
| 224 uint32_t nFlags, |
| 225 const CFX_FloatPoint& point) { |
| 226 ASSERT(pAnnot); |
| 227 |
| 228 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 229 return pAnnotHandler->OnRButtonUp(pPageView, pAnnot, nFlags, point); |
| 230 |
| 231 return FALSE; |
| 232 } |
| 233 |
| 234 void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseEnter(CPDFSDK_PageView* pPageView, |
| 235 CPDFSDK_Annot* pAnnot, |
| 236 uint32_t nFlag) { |
| 237 ASSERT(pAnnot); |
| 238 |
| 239 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 240 pAnnotHandler->OnMouseEnter(pPageView, pAnnot, nFlag); |
| 241 } |
| 242 |
| 243 void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseExit(CPDFSDK_PageView* pPageView, |
| 244 CPDFSDK_Annot* pAnnot, |
| 245 uint32_t nFlag) { |
| 246 ASSERT(pAnnot); |
| 247 |
| 248 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 249 pAnnotHandler->OnMouseExit(pPageView, pAnnot, nFlag); |
| 250 } |
| 251 |
| 252 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChar(CPDFSDK_Annot* pAnnot, |
| 253 uint32_t nChar, |
| 254 uint32_t nFlags) { |
| 255 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 256 return pAnnotHandler->OnChar(pAnnot, nChar, nFlags); |
| 257 |
| 258 return FALSE; |
| 259 } |
| 260 |
| 261 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyDown(CPDFSDK_Annot* pAnnot, |
| 262 int nKeyCode, |
| 263 int nFlag) { |
| 264 if (!m_pApp->FFI_IsCTRLKeyDown(nFlag) && !m_pApp->FFI_IsALTKeyDown(nFlag)) { |
| 265 CPDFSDK_PageView* pPage = pAnnot->GetPageView(); |
| 266 CPDFSDK_Annot* pFocusAnnot = pPage->GetFocusAnnot(); |
| 267 if (pFocusAnnot && (nKeyCode == FWL_VKEY_Tab)) { |
| 268 CPDFSDK_Annot* pNext = |
| 269 GetNextAnnot(pFocusAnnot, !m_pApp->FFI_IsSHIFTKeyDown(nFlag)); |
| 270 |
| 271 if (pNext && pNext != pFocusAnnot) { |
| 272 CPDFSDK_Document* pDocument = pPage->GetSDKDocument(); |
| 273 pDocument->SetFocusAnnot(pNext); |
| 274 return TRUE; |
| 275 } |
| 276 } |
| 277 } |
| 278 |
| 279 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 280 return pAnnotHandler->OnKeyDown(pAnnot, nKeyCode, nFlag); |
| 281 |
| 282 return FALSE; |
| 283 } |
| 284 |
| 285 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyUp(CPDFSDK_Annot* pAnnot, |
| 286 int nKeyCode, |
| 287 int nFlag) { |
| 288 return FALSE; |
| 289 } |
| 290 |
| 291 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnSetFocus(CPDFSDK_Annot* pAnnot, |
| 292 uint32_t nFlag) { |
| 293 ASSERT(pAnnot); |
| 294 |
| 295 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) { |
| 296 if (pAnnotHandler->OnSetFocus(pAnnot, nFlag)) { |
| 297 CPDFSDK_PageView* pPage = pAnnot->GetPageView(); |
| 298 pPage->GetSDKDocument(); |
| 299 return TRUE; |
| 300 } |
| 301 } |
| 302 return FALSE; |
| 303 } |
| 304 |
| 305 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKillFocus(CPDFSDK_Annot* pAnnot, |
| 306 uint32_t nFlag) { |
| 307 ASSERT(pAnnot); |
| 308 |
| 309 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 310 return pAnnotHandler->OnKillFocus(pAnnot, nFlag); |
| 311 |
| 312 return FALSE; |
| 313 } |
| 314 |
| 315 #ifdef PDF_ENABLE_XFA |
| 316 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChangeFocus( |
| 317 CPDFSDK_Annot* pSetAnnot, |
| 318 CPDFSDK_Annot* pKillAnnot) { |
| 319 FX_BOOL bXFA = (pSetAnnot && pSetAnnot->GetXFAWidget()) || |
| 320 (pKillAnnot && pKillAnnot->GetXFAWidget()); |
| 321 |
| 322 if (bXFA) { |
| 323 if (IPDFSDK_AnnotHandler* pXFAAnnotHandler = |
| 324 GetAnnotHandler(FSDK_XFAWIDGET_TYPENAME)) |
| 325 return pXFAAnnotHandler->OnXFAChangedFocus(pKillAnnot, pSetAnnot); |
| 326 } |
| 327 |
| 328 return TRUE; |
| 329 } |
| 330 #endif // PDF_ENABLE_XFA |
| 331 |
| 332 CFX_FloatRect CPDFSDK_AnnotHandlerMgr::Annot_OnGetViewBBox( |
| 333 CPDFSDK_PageView* pPageView, |
| 334 CPDFSDK_Annot* pAnnot) { |
| 335 ASSERT(pAnnot); |
| 336 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) |
| 337 return pAnnotHandler->GetViewBBox(pPageView, pAnnot); |
| 338 |
| 339 return pAnnot->GetRect(); |
| 340 } |
| 341 |
| 342 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnHitTest(CPDFSDK_PageView* pPageView, |
| 343 CPDFSDK_Annot* pAnnot, |
| 344 const CFX_FloatPoint& point) { |
| 345 ASSERT(pAnnot); |
| 346 if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) { |
| 347 if (pAnnotHandler->CanAnswer(pAnnot)) |
| 348 return pAnnotHandler->HitTest(pPageView, pAnnot, point); |
| 349 } |
| 350 return FALSE; |
| 351 } |
| 352 |
| 353 CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::GetNextAnnot(CPDFSDK_Annot* pSDKAnnot, |
| 354 FX_BOOL bNext) { |
| 355 #ifdef PDF_ENABLE_XFA |
| 356 CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView(); |
| 357 CPDFXFA_Page* pPage = pPageView->GetPDFXFAPage(); |
| 358 if (!pPage) |
| 359 return nullptr; |
| 360 if (pPage->GetPDFPage()) { // for pdf annots. |
| 361 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(), pSDKAnnot->GetType(), ""); |
| 362 CPDFSDK_Annot* pNext = |
| 363 bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot); |
| 364 return pNext; |
| 365 } |
| 366 // for xfa annots |
| 367 std::unique_ptr<IXFA_WidgetIterator> pWidgetIterator( |
| 368 pPage->GetXFAPageView()->CreateWidgetIterator( |
| 369 XFA_TRAVERSEWAY_Tranvalse, XFA_WidgetStatus_Visible | |
| 370 XFA_WidgetStatus_Viewable | |
| 371 XFA_WidgetStatus_Focused)); |
| 372 if (!pWidgetIterator) |
| 373 return nullptr; |
| 374 if (pWidgetIterator->GetCurrentWidget() != pSDKAnnot->GetXFAWidget()) |
| 375 pWidgetIterator->SetCurrentWidget(pSDKAnnot->GetXFAWidget()); |
| 376 CXFA_FFWidget* hNextFocus = |
| 377 bNext ? pWidgetIterator->MoveToNext() : pWidgetIterator->MoveToPrevious(); |
| 378 if (!hNextFocus && pSDKAnnot) |
| 379 hNextFocus = pWidgetIterator->MoveToFirst(); |
| 380 |
| 381 return pPageView->GetAnnotByXFAWidget(hNextFocus); |
| 382 #else // PDF_ENABLE_XFA |
| 383 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(), "Widget", ""); |
| 384 return bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot); |
| 385 #endif // PDF_ENABLE_XFA |
| 386 } |
OLD | NEW |