jaepark | 98e1019 | 2016-08-15 10:51:11 -0700 | [diff] [blame] | 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_xfaannothandler.h" |
| 8 | |
| 9 | #ifdef PDF_ENABLE_XFA |
| 10 | |
| 11 | #include "core/fpdfdoc/include/cpdf_interform.h" |
| 12 | #include "fpdfsdk/fpdfxfa/include/fpdfxfa_doc.h" |
| 13 | #include "fpdfsdk/include/cpdfsdk_annot.h" |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame^] | 14 | #include "fpdfsdk/include/cpdfsdk_interform.h" |
| 15 | #include "fpdfsdk/include/cpdfsdk_xfawidget.h" |
jaepark | 98e1019 | 2016-08-15 10:51:11 -0700 | [diff] [blame] | 16 | #include "fpdfsdk/include/fsdk_mgr.h" |
| 17 | #include "xfa/fxfa/include/fxfa_basic.h" |
| 18 | #include "xfa/fxfa/include/xfa_ffdocview.h" |
| 19 | #include "xfa/fxfa/include/xfa_ffpageview.h" |
| 20 | #include "xfa/fxfa/include/xfa_ffwidget.h" |
| 21 | #include "xfa/fxfa/include/xfa_ffwidgethandler.h" |
| 22 | #include "xfa/fxgraphics/include/cfx_graphics.h" |
| 23 | #include "xfa/fwl/core/include/fwl_widgethit.h" |
| 24 | |
| 25 | CPDFSDK_XFAAnnotHandler::CPDFSDK_XFAAnnotHandler(CPDFDoc_Environment* pApp) |
| 26 | : m_pApp(pApp) {} |
| 27 | |
| 28 | CPDFSDK_XFAAnnotHandler::~CPDFSDK_XFAAnnotHandler() {} |
| 29 | |
| 30 | CFX_ByteString CPDFSDK_XFAAnnotHandler::GetType() { |
| 31 | return FSDK_XFAWIDGET_TYPENAME; |
| 32 | } |
| 33 | |
| 34 | CFX_ByteString CPDFSDK_XFAAnnotHandler::GetName() { |
| 35 | return "XFAWidgetHandler"; |
| 36 | } |
| 37 | |
| 38 | FX_BOOL CPDFSDK_XFAAnnotHandler::CanAnswer(CPDFSDK_Annot* pAnnot) { |
| 39 | return !!pAnnot->GetXFAWidget(); |
| 40 | } |
| 41 | |
| 42 | CPDFSDK_Annot* CPDFSDK_XFAAnnotHandler::NewAnnot(CPDF_Annot* pAnnot, |
| 43 | CPDFSDK_PageView* pPage) { |
| 44 | return nullptr; |
| 45 | } |
| 46 | |
| 47 | CPDFSDK_Annot* CPDFSDK_XFAAnnotHandler::NewAnnot(CXFA_FFWidget* pAnnot, |
| 48 | CPDFSDK_PageView* pPage) { |
| 49 | CPDFSDK_Document* pSDKDoc = m_pApp->GetSDKDocument(); |
| 50 | CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm(); |
| 51 | CPDFSDK_XFAWidget* pWidget = new CPDFSDK_XFAWidget(pAnnot, pPage, pInterForm); |
| 52 | pInterForm->AddXFAMap(pAnnot, pWidget); |
| 53 | return pWidget; |
| 54 | } |
| 55 | |
| 56 | void CPDFSDK_XFAAnnotHandler::OnDraw(CPDFSDK_PageView* pPageView, |
| 57 | CPDFSDK_Annot* pAnnot, |
| 58 | CFX_RenderDevice* pDevice, |
| 59 | CFX_Matrix* pUser2Device, |
| 60 | uint32_t dwFlags) { |
| 61 | ASSERT(pPageView); |
| 62 | ASSERT(pAnnot); |
| 63 | |
| 64 | CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument(); |
| 65 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 66 | |
| 67 | CFX_Graphics gs; |
| 68 | gs.Create(pDevice); |
| 69 | |
| 70 | CFX_Matrix mt; |
| 71 | mt = *pUser2Device; |
| 72 | |
| 73 | FX_BOOL bIsHighlight = FALSE; |
| 74 | if (pSDKDoc->GetFocusAnnot() != pAnnot) |
| 75 | bIsHighlight = TRUE; |
| 76 | |
| 77 | pWidgetHandler->RenderWidget(pAnnot->GetXFAWidget(), &gs, &mt, bIsHighlight); |
| 78 | |
| 79 | // to do highlight and shadow |
| 80 | } |
| 81 | |
| 82 | void CPDFSDK_XFAAnnotHandler::OnDrawSleep(CPDFSDK_PageView* pPageView, |
| 83 | CPDFSDK_Annot* pAnnot, |
| 84 | CFX_RenderDevice* pDevice, |
| 85 | CFX_Matrix* pUser2Device, |
| 86 | const CFX_FloatRect& rcWindow, |
| 87 | uint32_t dwFlags) {} |
| 88 | |
| 89 | void CPDFSDK_XFAAnnotHandler::OnCreate(CPDFSDK_Annot* pAnnot) {} |
| 90 | |
| 91 | void CPDFSDK_XFAAnnotHandler::OnLoad(CPDFSDK_Annot* pAnnot) {} |
| 92 | |
| 93 | void CPDFSDK_XFAAnnotHandler::OnDelete(CPDFSDK_Annot* pAnnot) {} |
| 94 | |
| 95 | void CPDFSDK_XFAAnnotHandler::OnRelease(CPDFSDK_Annot* pAnnot) {} |
| 96 | |
| 97 | void CPDFSDK_XFAAnnotHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot) { |
| 98 | CPDFSDK_XFAWidget* pWidget = reinterpret_cast<CPDFSDK_XFAWidget*>(pAnnot); |
| 99 | CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm(); |
| 100 | pInterForm->RemoveXFAMap(pWidget->GetXFAWidget()); |
| 101 | |
| 102 | delete pWidget; |
| 103 | } |
| 104 | |
| 105 | void CPDFSDK_XFAAnnotHandler::DeleteAnnot(CPDFSDK_Annot* pAnnot) {} |
| 106 | |
| 107 | CFX_FloatRect CPDFSDK_XFAAnnotHandler::GetViewBBox(CPDFSDK_PageView* pPageView, |
| 108 | CPDFSDK_Annot* pAnnot) { |
| 109 | ASSERT(pAnnot); |
| 110 | |
| 111 | CFX_RectF rcBBox; |
| 112 | XFA_Element eType = pAnnot->GetXFAWidget()->GetDataAcc()->GetUIType(); |
| 113 | if (eType == XFA_Element::Signature) |
| 114 | pAnnot->GetXFAWidget()->GetBBox(rcBBox, XFA_WidgetStatus_Visible, TRUE); |
| 115 | else |
| 116 | pAnnot->GetXFAWidget()->GetBBox(rcBBox, XFA_WidgetStatus_None); |
| 117 | |
| 118 | CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width, |
| 119 | rcBBox.top + rcBBox.height); |
| 120 | rcWidget.left -= 1.0f; |
| 121 | rcWidget.right += 1.0f; |
| 122 | rcWidget.bottom -= 1.0f; |
| 123 | rcWidget.top += 1.0f; |
| 124 | |
| 125 | return rcWidget; |
| 126 | } |
| 127 | |
| 128 | FX_BOOL CPDFSDK_XFAAnnotHandler::HitTest(CPDFSDK_PageView* pPageView, |
| 129 | CPDFSDK_Annot* pAnnot, |
| 130 | const CFX_FloatPoint& point) { |
| 131 | if (!pPageView || !pAnnot) |
| 132 | return FALSE; |
| 133 | |
| 134 | CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument(); |
| 135 | if (!pSDKDoc) |
| 136 | return FALSE; |
| 137 | |
| 138 | CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument(); |
| 139 | if (!pDoc) |
| 140 | return FALSE; |
| 141 | |
| 142 | CXFA_FFDocView* pDocView = pDoc->GetXFADocView(); |
| 143 | if (!pDocView) |
| 144 | return FALSE; |
| 145 | |
| 146 | CXFA_FFWidgetHandler* pWidgetHandler = pDocView->GetWidgetHandler(); |
| 147 | if (!pWidgetHandler) |
| 148 | return FALSE; |
| 149 | |
| 150 | FWL_WidgetHit dwHitTest = |
| 151 | pWidgetHandler->OnHitTest(pAnnot->GetXFAWidget(), point.x, point.y); |
| 152 | return dwHitTest != FWL_WidgetHit::Unknown; |
| 153 | } |
| 154 | |
| 155 | void CPDFSDK_XFAAnnotHandler::OnMouseEnter(CPDFSDK_PageView* pPageView, |
| 156 | CPDFSDK_Annot* pAnnot, |
| 157 | uint32_t nFlag) { |
| 158 | if (!pPageView || !pAnnot) |
| 159 | return; |
| 160 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 161 | pWidgetHandler->OnMouseEnter(pAnnot->GetXFAWidget()); |
| 162 | } |
| 163 | |
| 164 | void CPDFSDK_XFAAnnotHandler::OnMouseExit(CPDFSDK_PageView* pPageView, |
| 165 | CPDFSDK_Annot* pAnnot, |
| 166 | uint32_t nFlag) { |
| 167 | if (!pPageView || !pAnnot) |
| 168 | return; |
| 169 | |
| 170 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 171 | pWidgetHandler->OnMouseExit(pAnnot->GetXFAWidget()); |
| 172 | } |
| 173 | |
| 174 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnLButtonDown(CPDFSDK_PageView* pPageView, |
| 175 | CPDFSDK_Annot* pAnnot, |
| 176 | uint32_t nFlags, |
| 177 | const CFX_FloatPoint& point) { |
| 178 | if (!pPageView || !pAnnot) |
| 179 | return FALSE; |
| 180 | |
| 181 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 182 | return pWidgetHandler->OnLButtonDown(pAnnot->GetXFAWidget(), |
| 183 | GetFWLFlags(nFlags), point.x, point.y); |
| 184 | } |
| 185 | |
| 186 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnLButtonUp(CPDFSDK_PageView* pPageView, |
| 187 | CPDFSDK_Annot* pAnnot, |
| 188 | uint32_t nFlags, |
| 189 | const CFX_FloatPoint& point) { |
| 190 | if (!pPageView || !pAnnot) |
| 191 | return FALSE; |
| 192 | |
| 193 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 194 | return pWidgetHandler->OnLButtonUp(pAnnot->GetXFAWidget(), |
| 195 | GetFWLFlags(nFlags), point.x, point.y); |
| 196 | } |
| 197 | |
| 198 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnLButtonDblClk(CPDFSDK_PageView* pPageView, |
| 199 | CPDFSDK_Annot* pAnnot, |
| 200 | uint32_t nFlags, |
| 201 | const CFX_FloatPoint& point) { |
| 202 | if (!pPageView || !pAnnot) |
| 203 | return FALSE; |
| 204 | |
| 205 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 206 | return pWidgetHandler->OnLButtonDblClk(pAnnot->GetXFAWidget(), |
| 207 | GetFWLFlags(nFlags), point.x, point.y); |
| 208 | } |
| 209 | |
| 210 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnMouseMove(CPDFSDK_PageView* pPageView, |
| 211 | CPDFSDK_Annot* pAnnot, |
| 212 | uint32_t nFlags, |
| 213 | const CFX_FloatPoint& point) { |
| 214 | if (!pPageView || !pAnnot) |
| 215 | return FALSE; |
| 216 | |
| 217 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 218 | return pWidgetHandler->OnMouseMove(pAnnot->GetXFAWidget(), |
| 219 | GetFWLFlags(nFlags), point.x, point.y); |
| 220 | } |
| 221 | |
| 222 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnMouseWheel(CPDFSDK_PageView* pPageView, |
| 223 | CPDFSDK_Annot* pAnnot, |
| 224 | uint32_t nFlags, |
| 225 | short zDelta, |
| 226 | const CFX_FloatPoint& point) { |
| 227 | if (!pPageView || !pAnnot) |
| 228 | return FALSE; |
| 229 | |
| 230 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 231 | return pWidgetHandler->OnMouseWheel( |
| 232 | pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), zDelta, point.x, point.y); |
| 233 | } |
| 234 | |
| 235 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnRButtonDown(CPDFSDK_PageView* pPageView, |
| 236 | CPDFSDK_Annot* pAnnot, |
| 237 | uint32_t nFlags, |
| 238 | const CFX_FloatPoint& point) { |
| 239 | if (!pPageView || !pAnnot) |
| 240 | return FALSE; |
| 241 | |
| 242 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 243 | return pWidgetHandler->OnRButtonDown(pAnnot->GetXFAWidget(), |
| 244 | GetFWLFlags(nFlags), point.x, point.y); |
| 245 | } |
| 246 | |
| 247 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnRButtonUp(CPDFSDK_PageView* pPageView, |
| 248 | CPDFSDK_Annot* pAnnot, |
| 249 | uint32_t nFlags, |
| 250 | const CFX_FloatPoint& point) { |
| 251 | if (!pPageView || !pAnnot) |
| 252 | return FALSE; |
| 253 | |
| 254 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 255 | return pWidgetHandler->OnRButtonUp(pAnnot->GetXFAWidget(), |
| 256 | GetFWLFlags(nFlags), point.x, point.y); |
| 257 | } |
| 258 | |
| 259 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnRButtonDblClk(CPDFSDK_PageView* pPageView, |
| 260 | CPDFSDK_Annot* pAnnot, |
| 261 | uint32_t nFlags, |
| 262 | const CFX_FloatPoint& point) { |
| 263 | if (!pPageView || !pAnnot) |
| 264 | return FALSE; |
| 265 | |
| 266 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 267 | return pWidgetHandler->OnRButtonDblClk(pAnnot->GetXFAWidget(), |
| 268 | GetFWLFlags(nFlags), point.x, point.y); |
| 269 | } |
| 270 | |
| 271 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnChar(CPDFSDK_Annot* pAnnot, |
| 272 | uint32_t nChar, |
| 273 | uint32_t nFlags) { |
| 274 | if (!pAnnot) |
| 275 | return FALSE; |
| 276 | |
| 277 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 278 | return pWidgetHandler->OnChar(pAnnot->GetXFAWidget(), nChar, |
| 279 | GetFWLFlags(nFlags)); |
| 280 | } |
| 281 | |
| 282 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnKeyDown(CPDFSDK_Annot* pAnnot, |
| 283 | int nKeyCode, |
| 284 | int nFlag) { |
| 285 | if (!pAnnot) |
| 286 | return FALSE; |
| 287 | |
| 288 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 289 | return pWidgetHandler->OnKeyDown(pAnnot->GetXFAWidget(), nKeyCode, |
| 290 | GetFWLFlags(nFlag)); |
| 291 | } |
| 292 | |
| 293 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnKeyUp(CPDFSDK_Annot* pAnnot, |
| 294 | int nKeyCode, |
| 295 | int nFlag) { |
| 296 | if (!pAnnot) |
| 297 | return FALSE; |
| 298 | |
| 299 | CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot); |
| 300 | return pWidgetHandler->OnKeyUp(pAnnot->GetXFAWidget(), nKeyCode, |
| 301 | GetFWLFlags(nFlag)); |
| 302 | } |
| 303 | |
| 304 | void CPDFSDK_XFAAnnotHandler::OnDeSelected(CPDFSDK_Annot* pAnnot) {} |
| 305 | |
| 306 | void CPDFSDK_XFAAnnotHandler::OnSelected(CPDFSDK_Annot* pAnnot) {} |
| 307 | |
| 308 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnSetFocus(CPDFSDK_Annot* pAnnot, |
| 309 | uint32_t nFlag) { |
| 310 | return TRUE; |
| 311 | } |
| 312 | |
| 313 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnKillFocus(CPDFSDK_Annot* pAnnot, |
| 314 | uint32_t nFlag) { |
| 315 | return TRUE; |
| 316 | } |
| 317 | |
| 318 | FX_BOOL CPDFSDK_XFAAnnotHandler::OnXFAChangedFocus(CPDFSDK_Annot* pOldAnnot, |
| 319 | CPDFSDK_Annot* pNewAnnot) { |
| 320 | CXFA_FFWidgetHandler* pWidgetHandler = nullptr; |
| 321 | |
| 322 | if (pOldAnnot) |
| 323 | pWidgetHandler = GetXFAWidgetHandler(pOldAnnot); |
| 324 | else if (pNewAnnot) |
| 325 | pWidgetHandler = GetXFAWidgetHandler(pNewAnnot); |
| 326 | |
| 327 | if (pWidgetHandler) { |
| 328 | FX_BOOL bRet = TRUE; |
| 329 | CXFA_FFWidget* hWidget = pNewAnnot ? pNewAnnot->GetXFAWidget() : nullptr; |
| 330 | if (hWidget) { |
| 331 | CXFA_FFPageView* pXFAPageView = hWidget->GetPageView(); |
| 332 | if (pXFAPageView) { |
| 333 | bRet = pXFAPageView->GetDocView()->SetFocus(hWidget); |
| 334 | if (pXFAPageView->GetDocView()->GetFocusWidget() == hWidget) |
| 335 | bRet = TRUE; |
| 336 | } |
| 337 | } |
| 338 | return bRet; |
| 339 | } |
| 340 | |
| 341 | return TRUE; |
| 342 | } |
| 343 | |
| 344 | CXFA_FFWidgetHandler* CPDFSDK_XFAAnnotHandler::GetXFAWidgetHandler( |
| 345 | CPDFSDK_Annot* pAnnot) { |
| 346 | if (!pAnnot) |
| 347 | return nullptr; |
| 348 | |
| 349 | CPDFSDK_PageView* pPageView = pAnnot->GetPageView(); |
| 350 | if (!pPageView) |
| 351 | return nullptr; |
| 352 | |
| 353 | CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument(); |
| 354 | if (!pSDKDoc) |
| 355 | return nullptr; |
| 356 | |
| 357 | CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument(); |
| 358 | if (!pDoc) |
| 359 | return nullptr; |
| 360 | |
| 361 | CXFA_FFDocView* pDocView = pDoc->GetXFADocView(); |
| 362 | if (!pDocView) |
| 363 | return nullptr; |
| 364 | |
| 365 | return pDocView->GetWidgetHandler(); |
| 366 | } |
| 367 | |
| 368 | const uint32_t FWL_KEYFLAG_Ctrl = (1 << 0); |
| 369 | const uint32_t FWL_KEYFLAG_Alt = (1 << 1); |
| 370 | const uint32_t FWL_KEYFLAG_Shift = (1 << 2); |
| 371 | const uint32_t FWL_KEYFLAG_LButton = (1 << 3); |
| 372 | const uint32_t FWL_KEYFLAG_RButton = (1 << 4); |
| 373 | const uint32_t FWL_KEYFLAG_MButton = (1 << 5); |
| 374 | |
| 375 | uint32_t CPDFSDK_XFAAnnotHandler::GetFWLFlags(uint32_t dwFlag) { |
| 376 | uint32_t dwFWLFlag = 0; |
| 377 | |
| 378 | if (dwFlag & FWL_EVENTFLAG_ControlKey) |
| 379 | dwFWLFlag |= FWL_KEYFLAG_Ctrl; |
| 380 | if (dwFlag & FWL_EVENTFLAG_LeftButtonDown) |
| 381 | dwFWLFlag |= FWL_KEYFLAG_LButton; |
| 382 | if (dwFlag & FWL_EVENTFLAG_MiddleButtonDown) |
| 383 | dwFWLFlag |= FWL_KEYFLAG_MButton; |
| 384 | if (dwFlag & FWL_EVENTFLAG_RightButtonDown) |
| 385 | dwFWLFlag |= FWL_KEYFLAG_RButton; |
| 386 | if (dwFlag & FWL_EVENTFLAG_ShiftKey) |
| 387 | dwFWLFlag |= FWL_KEYFLAG_Shift; |
| 388 | if (dwFlag & FWL_EVENTFLAG_AltKey) |
| 389 | dwFWLFlag |= FWL_KEYFLAG_Alt; |
| 390 | |
| 391 | return dwFWLFlag; |
| 392 | } |
| 393 | |
| 394 | #endif // PDF_ENABLE_XFA |