1 // Copyright 2014 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 <algorithm>
8 
9 #include "fpdfsdk/include/formfiller/FFL_FormFiller.h"
10 #include "fpdfsdk/include/fsdk_annothandler.h"
11 #include "fpdfsdk/include/fsdk_define.h"
12 #include "fpdfsdk/include/fsdk_mgr.h"
13 
14 #ifdef PDF_ENABLE_XFA
15 #include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h"
16 #include "fpdfsdk/include/fpdfxfa/fpdfxfa_util.h"
17 #endif  // PDF_ENABLE_XFA
18 
CPDFSDK_AnnotHandlerMgr(CPDFDoc_Environment * pApp)19 CPDFSDK_AnnotHandlerMgr::CPDFSDK_AnnotHandlerMgr(CPDFDoc_Environment* pApp) {
20   m_pApp = pApp;
21 
22   CPDFSDK_BFAnnotHandler* pHandler = new CPDFSDK_BFAnnotHandler(m_pApp);
23   pHandler->SetFormFiller(m_pApp->GetIFormFiller());
24   RegisterAnnotHandler(pHandler);
25 #ifdef PDF_ENABLE_XFA
26   CPDFSDK_XFAAnnotHandler* pXFAAnnotHandler =
27       new CPDFSDK_XFAAnnotHandler(m_pApp);
28   RegisterAnnotHandler(pXFAAnnotHandler);
29 #endif  // PDF_ENABLE_XFA
30 }
31 
~CPDFSDK_AnnotHandlerMgr()32 CPDFSDK_AnnotHandlerMgr::~CPDFSDK_AnnotHandlerMgr() {
33   for (int i = 0; i < m_Handlers.GetSize(); i++) {
34     IPDFSDK_AnnotHandler* pHandler = m_Handlers.GetAt(i);
35     delete pHandler;
36   }
37   m_Handlers.RemoveAll();
38   m_mapType2Handler.clear();
39 }
40 
RegisterAnnotHandler(IPDFSDK_AnnotHandler * pAnnotHandler)41 void CPDFSDK_AnnotHandlerMgr::RegisterAnnotHandler(
42     IPDFSDK_AnnotHandler* pAnnotHandler) {
43   ASSERT(!GetAnnotHandler(pAnnotHandler->GetType()));
44 
45   m_Handlers.Add(pAnnotHandler);
46   m_mapType2Handler[pAnnotHandler->GetType()] = pAnnotHandler;
47 }
48 
UnRegisterAnnotHandler(IPDFSDK_AnnotHandler * pAnnotHandler)49 void CPDFSDK_AnnotHandlerMgr::UnRegisterAnnotHandler(
50     IPDFSDK_AnnotHandler* pAnnotHandler) {
51   m_mapType2Handler.erase(pAnnotHandler->GetType());
52   for (int i = 0, sz = m_Handlers.GetSize(); i < sz; i++) {
53     if (m_Handlers.GetAt(i) == pAnnotHandler) {
54       m_Handlers.RemoveAt(i);
55       break;
56     }
57   }
58 }
59 
NewAnnot(CPDF_Annot * pAnnot,CPDFSDK_PageView * pPageView)60 CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(CPDF_Annot* pAnnot,
61                                                  CPDFSDK_PageView* pPageView) {
62   ASSERT(pPageView);
63 
64   if (IPDFSDK_AnnotHandler* pAnnotHandler =
65           GetAnnotHandler(pAnnot->GetSubType())) {
66     return pAnnotHandler->NewAnnot(pAnnot, pPageView);
67   }
68 
69   return new CPDFSDK_BAAnnot(pAnnot, pPageView);
70 }
71 
72 #ifdef PDF_ENABLE_XFA
NewAnnot(IXFA_Widget * pAnnot,CPDFSDK_PageView * pPageView)73 CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(IXFA_Widget* pAnnot,
74                                                  CPDFSDK_PageView* pPageView) {
75   ASSERT(pAnnot != NULL);
76   ASSERT(pPageView != NULL);
77 
78   if (IPDFSDK_AnnotHandler* pAnnotHandler =
79           GetAnnotHandler(FSDK_XFAWIDGET_TYPENAME)) {
80     return pAnnotHandler->NewAnnot(pAnnot, pPageView);
81   }
82 
83   return NULL;
84 }
85 #endif  // PDF_ENABLE_XFA
86 
ReleaseAnnot(CPDFSDK_Annot * pAnnot)87 void CPDFSDK_AnnotHandlerMgr::ReleaseAnnot(CPDFSDK_Annot* pAnnot) {
88   pAnnot->GetPDFPage();
89 
90   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
91     pAnnotHandler->OnRelease(pAnnot);
92     pAnnotHandler->ReleaseAnnot(pAnnot);
93   } else {
94     delete (CPDFSDK_Annot*)pAnnot;
95   }
96 }
97 
Annot_OnCreate(CPDFSDK_Annot * pAnnot)98 void CPDFSDK_AnnotHandlerMgr::Annot_OnCreate(CPDFSDK_Annot* pAnnot) {
99   CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
100 
101   CPDFSDK_DateTime curTime;
102   pPDFAnnot->GetAnnotDict()->SetAtString("M", curTime.ToPDFDateTimeString());
103   pPDFAnnot->GetAnnotDict()->SetAtNumber("F", 0);
104 
105   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
106     pAnnotHandler->OnCreate(pAnnot);
107   }
108 }
109 
Annot_OnLoad(CPDFSDK_Annot * pAnnot)110 void CPDFSDK_AnnotHandlerMgr::Annot_OnLoad(CPDFSDK_Annot* pAnnot) {
111   ASSERT(pAnnot);
112 
113   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
114     pAnnotHandler->OnLoad(pAnnot);
115   }
116 }
117 
GetAnnotHandler(CPDFSDK_Annot * pAnnot) const118 IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
119     CPDFSDK_Annot* pAnnot) const {
120   CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
121   if (pPDFAnnot)
122     return GetAnnotHandler(pPDFAnnot->GetSubType());
123 #ifdef PDF_ENABLE_XFA
124   if (pAnnot->GetXFAWidget())
125     return GetAnnotHandler(FSDK_XFAWIDGET_TYPENAME);
126 #endif  // PDF_ENABLE_XFA
127   return nullptr;
128 }
129 
GetAnnotHandler(const CFX_ByteString & sType) const130 IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
131     const CFX_ByteString& sType) const {
132   auto it = m_mapType2Handler.find(sType);
133   return it != m_mapType2Handler.end() ? it->second : nullptr;
134 }
135 
Annot_OnDraw(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,CFX_RenderDevice * pDevice,CFX_Matrix * pUser2Device,FX_DWORD dwFlags)136 void CPDFSDK_AnnotHandlerMgr::Annot_OnDraw(CPDFSDK_PageView* pPageView,
137                                            CPDFSDK_Annot* pAnnot,
138                                            CFX_RenderDevice* pDevice,
139                                            CFX_Matrix* pUser2Device,
140                                            FX_DWORD dwFlags) {
141   ASSERT(pAnnot);
142 
143   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
144     pAnnotHandler->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
145   } else {
146 #ifdef PDF_ENABLE_XFA
147     if (pAnnot->IsXFAField())
148       return;
149 #endif  // PDF_ENABLE_XFA
150     static_cast<CPDFSDK_BAAnnot*>(pAnnot)
151         ->DrawAppearance(pDevice, pUser2Device, CPDF_Annot::Normal, nullptr);
152   }
153 }
154 
Annot_OnLButtonDown(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)155 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDown(
156     CPDFSDK_PageView* pPageView,
157     CPDFSDK_Annot* pAnnot,
158     FX_DWORD nFlags,
159     const CPDF_Point& point) {
160   ASSERT(pAnnot);
161 
162   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
163     return pAnnotHandler->OnLButtonDown(pPageView, pAnnot, nFlags, point);
164   }
165   return FALSE;
166 }
Annot_OnLButtonUp(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)167 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonUp(CPDFSDK_PageView* pPageView,
168                                                    CPDFSDK_Annot* pAnnot,
169                                                    FX_DWORD nFlags,
170                                                    const CPDF_Point& point) {
171   ASSERT(pAnnot);
172 
173   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
174     return pAnnotHandler->OnLButtonUp(pPageView, pAnnot, nFlags, point);
175   }
176   return FALSE;
177 }
Annot_OnLButtonDblClk(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)178 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDblClk(
179     CPDFSDK_PageView* pPageView,
180     CPDFSDK_Annot* pAnnot,
181     FX_DWORD nFlags,
182     const CPDF_Point& point) {
183   ASSERT(pAnnot);
184 
185   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
186     return pAnnotHandler->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
187   }
188   return FALSE;
189 }
Annot_OnMouseMove(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)190 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseMove(CPDFSDK_PageView* pPageView,
191                                                    CPDFSDK_Annot* pAnnot,
192                                                    FX_DWORD nFlags,
193                                                    const CPDF_Point& point) {
194   ASSERT(pAnnot);
195 
196   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
197     return pAnnotHandler->OnMouseMove(pPageView, pAnnot, nFlags, point);
198   }
199   return FALSE;
200 }
Annot_OnMouseWheel(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,short zDelta,const CPDF_Point & point)201 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseWheel(CPDFSDK_PageView* pPageView,
202                                                     CPDFSDK_Annot* pAnnot,
203                                                     FX_DWORD nFlags,
204                                                     short zDelta,
205                                                     const CPDF_Point& point) {
206   ASSERT(pAnnot);
207 
208   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
209     return pAnnotHandler->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta,
210                                        point);
211   }
212   return FALSE;
213 }
Annot_OnRButtonDown(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)214 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonDown(
215     CPDFSDK_PageView* pPageView,
216     CPDFSDK_Annot* pAnnot,
217     FX_DWORD nFlags,
218     const CPDF_Point& point) {
219   ASSERT(pAnnot);
220 
221   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
222     return pAnnotHandler->OnRButtonDown(pPageView, pAnnot, nFlags, point);
223   }
224   return FALSE;
225 }
Annot_OnRButtonUp(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)226 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonUp(CPDFSDK_PageView* pPageView,
227                                                    CPDFSDK_Annot* pAnnot,
228                                                    FX_DWORD nFlags,
229                                                    const CPDF_Point& point) {
230   ASSERT(pAnnot);
231 
232   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
233     return pAnnotHandler->OnRButtonUp(pPageView, pAnnot, nFlags, point);
234   }
235   return FALSE;
236 }
237 
Annot_OnMouseEnter(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlag)238 void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseEnter(CPDFSDK_PageView* pPageView,
239                                                  CPDFSDK_Annot* pAnnot,
240                                                  FX_DWORD nFlag) {
241   ASSERT(pAnnot);
242 
243   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
244     pAnnotHandler->OnMouseEnter(pPageView, pAnnot, nFlag);
245   }
246   return;
247 }
248 
Annot_OnMouseExit(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlag)249 void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseExit(CPDFSDK_PageView* pPageView,
250                                                 CPDFSDK_Annot* pAnnot,
251                                                 FX_DWORD nFlag) {
252   ASSERT(pAnnot);
253 
254   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
255     pAnnotHandler->OnMouseExit(pPageView, pAnnot, nFlag);
256   }
257   return;
258 }
259 
Annot_OnChar(CPDFSDK_Annot * pAnnot,FX_DWORD nChar,FX_DWORD nFlags)260 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChar(CPDFSDK_Annot* pAnnot,
261                                               FX_DWORD nChar,
262                                               FX_DWORD nFlags) {
263   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
264     return pAnnotHandler->OnChar(pAnnot, nChar, nFlags);
265   }
266   return FALSE;
267 }
268 
Annot_OnKeyDown(CPDFSDK_Annot * pAnnot,int nKeyCode,int nFlag)269 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyDown(CPDFSDK_Annot* pAnnot,
270                                                  int nKeyCode,
271                                                  int nFlag) {
272   if (!m_pApp->FFI_IsCTRLKeyDown(nFlag) && !m_pApp->FFI_IsALTKeyDown(nFlag)) {
273     CPDFSDK_PageView* pPage = pAnnot->GetPageView();
274     CPDFSDK_Annot* pFocusAnnot = pPage->GetFocusAnnot();
275     if (pFocusAnnot && (nKeyCode == FWL_VKEY_Tab)) {
276       CPDFSDK_Annot* pNext =
277           GetNextAnnot(pFocusAnnot, !m_pApp->FFI_IsSHIFTKeyDown(nFlag));
278 
279       if (pNext && pNext != pFocusAnnot) {
280         CPDFSDK_Document* pDocument = pPage->GetSDKDocument();
281         pDocument->SetFocusAnnot(pNext);
282         return TRUE;
283       }
284     }
285   }
286 
287   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
288     return pAnnotHandler->OnKeyDown(pAnnot, nKeyCode, nFlag);
289   }
290   return FALSE;
291 }
Annot_OnKeyUp(CPDFSDK_Annot * pAnnot,int nKeyCode,int nFlag)292 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyUp(CPDFSDK_Annot* pAnnot,
293                                                int nKeyCode,
294                                                int nFlag) {
295   return FALSE;
296 }
297 
Annot_OnSetFocus(CPDFSDK_Annot * pAnnot,FX_DWORD nFlag)298 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnSetFocus(CPDFSDK_Annot* pAnnot,
299                                                   FX_DWORD nFlag) {
300   ASSERT(pAnnot);
301 
302   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
303     if (pAnnotHandler->OnSetFocus(pAnnot, nFlag)) {
304       CPDFSDK_PageView* pPage = pAnnot->GetPageView();
305       pPage->GetSDKDocument();
306       return TRUE;
307     }
308   }
309   return FALSE;
310 }
311 
Annot_OnKillFocus(CPDFSDK_Annot * pAnnot,FX_DWORD nFlag)312 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKillFocus(CPDFSDK_Annot* pAnnot,
313                                                    FX_DWORD nFlag) {
314   ASSERT(pAnnot);
315   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
316     return pAnnotHandler->OnKillFocus(pAnnot, nFlag);
317 
318   return FALSE;
319 }
320 
321 #ifdef PDF_ENABLE_XFA
Annot_OnChangeFocus(CPDFSDK_Annot * pSetAnnot,CPDFSDK_Annot * pKillAnnot)322 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChangeFocus(
323     CPDFSDK_Annot* pSetAnnot,
324     CPDFSDK_Annot* pKillAnnot) {
325   FX_BOOL bXFA = (pSetAnnot && pSetAnnot->GetXFAWidget()) ||
326                  (pKillAnnot && pKillAnnot->GetXFAWidget());
327 
328   if (bXFA) {
329     if (IPDFSDK_AnnotHandler* pXFAAnnotHandler =
330             GetAnnotHandler(FSDK_XFAWIDGET_TYPENAME))
331       return pXFAAnnotHandler->OnXFAChangedFocus(pKillAnnot, pSetAnnot);
332   }
333 
334   return TRUE;
335 }
336 #endif  // PDF_ENABLE_XFA
337 
Annot_OnGetViewBBox(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot)338 CPDF_Rect CPDFSDK_AnnotHandlerMgr::Annot_OnGetViewBBox(
339     CPDFSDK_PageView* pPageView,
340     CPDFSDK_Annot* pAnnot) {
341   ASSERT(pAnnot);
342   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
343     return pAnnotHandler->GetViewBBox(pPageView, pAnnot);
344 
345   return pAnnot->GetRect();
346 }
347 
Annot_OnHitTest(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,const CPDF_Point & point)348 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnHitTest(CPDFSDK_PageView* pPageView,
349                                                  CPDFSDK_Annot* pAnnot,
350                                                  const CPDF_Point& point) {
351   ASSERT(pAnnot);
352   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
353     if (pAnnotHandler->CanAnswer(pAnnot))
354       return pAnnotHandler->HitTest(pPageView, pAnnot, point);
355   }
356   return FALSE;
357 }
358 
GetNextAnnot(CPDFSDK_Annot * pSDKAnnot,FX_BOOL bNext)359 CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::GetNextAnnot(CPDFSDK_Annot* pSDKAnnot,
360                                                      FX_BOOL bNext) {
361 #ifdef PDF_ENABLE_XFA
362   CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView();
363   CPDFXFA_Page* pPage = pPageView->GetPDFXFAPage();
364   if (pPage == NULL)
365     return NULL;
366   if (pPage->GetPDFPage()) {  // for pdf annots.
367     CBA_AnnotIterator ai(pSDKAnnot->GetPageView(), pSDKAnnot->GetType(), "");
368     CPDFSDK_Annot* pNext =
369         bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
370     return pNext;
371   }
372   // for xfa annots
373   IXFA_WidgetIterator* pWidgetIterator =
374       pPage->GetXFAPageView()->CreateWidgetIterator(
375           XFA_TRAVERSEWAY_Tranvalse, XFA_WIDGETFILTER_Visible |
376                                          XFA_WIDGETFILTER_Viewable |
377                                          XFA_WIDGETFILTER_Field);
378   if (pWidgetIterator == NULL)
379     return NULL;
380   if (pWidgetIterator->GetCurrentWidget() != pSDKAnnot->GetXFAWidget())
381     pWidgetIterator->SetCurrentWidget(pSDKAnnot->GetXFAWidget());
382   IXFA_Widget* hNextFocus = NULL;
383   hNextFocus =
384       bNext ? pWidgetIterator->MoveToNext() : pWidgetIterator->MoveToPrevious();
385   if (hNextFocus == NULL && pSDKAnnot != NULL)
386     hNextFocus = pWidgetIterator->MoveToFirst();
387 
388   pWidgetIterator->Release();
389   return pPageView->GetAnnotByXFAWidget(hNextFocus);
390 #else   // PDF_ENABLE_XFA
391   CBA_AnnotIterator ai(pSDKAnnot->GetPageView(), "Widget", "");
392   return bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
393 #endif  // PDF_ENABLE_XFA
394 }
395 
CanAnswer(CPDFSDK_Annot * pAnnot)396 FX_BOOL CPDFSDK_BFAnnotHandler::CanAnswer(CPDFSDK_Annot* pAnnot) {
397   ASSERT(pAnnot->GetType() == "Widget");
398   if (pAnnot->GetSubType() == BFFT_SIGNATURE)
399     return FALSE;
400 
401   CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
402   if (!pWidget->IsVisible())
403     return FALSE;
404 
405   int nFieldFlags = pWidget->GetFieldFlags();
406   if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY)
407     return FALSE;
408 
409   if (pWidget->GetFieldType() == FIELDTYPE_PUSHBUTTON)
410     return TRUE;
411 
412   CPDF_Page* pPage = pWidget->GetPDFPage();
413   CPDF_Document* pDocument = pPage->m_pDocument;
414   FX_DWORD dwPermissions = pDocument->GetUserPermissions();
415   return (dwPermissions & FPDFPERM_FILL_FORM) ||
416          (dwPermissions & FPDFPERM_ANNOT_FORM);
417 }
418 
NewAnnot(CPDF_Annot * pAnnot,CPDFSDK_PageView * pPage)419 CPDFSDK_Annot* CPDFSDK_BFAnnotHandler::NewAnnot(CPDF_Annot* pAnnot,
420                                                 CPDFSDK_PageView* pPage) {
421   CPDFSDK_Document* pSDKDoc = m_pApp->GetSDKDocument();
422   CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pSDKDoc->GetInterForm();
423   CPDF_FormControl* pCtrl = CPDFSDK_Widget::GetFormControl(
424       pInterForm->GetInterForm(), pAnnot->GetAnnotDict());
425   if (!pCtrl)
426     return nullptr;
427 
428   CPDFSDK_Widget* pWidget = new CPDFSDK_Widget(pAnnot, pPage, pInterForm);
429   pInterForm->AddMap(pCtrl, pWidget);
430   CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
431   if (pPDFInterForm && pPDFInterForm->NeedConstructAP())
432     pWidget->ResetAppearance(nullptr, FALSE);
433 
434   return pWidget;
435 }
436 
437 #ifdef PDF_ENABLE_XFA
NewAnnot(IXFA_Widget * hWidget,CPDFSDK_PageView * pPage)438 CPDFSDK_Annot* CPDFSDK_BFAnnotHandler::NewAnnot(IXFA_Widget* hWidget,
439                                                 CPDFSDK_PageView* pPage) {
440   return NULL;
441 }
442 #endif  // PDF_ENABLE_XFA
443 
ReleaseAnnot(CPDFSDK_Annot * pAnnot)444 void CPDFSDK_BFAnnotHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot) {
445   ASSERT(pAnnot);
446 
447   if (m_pFormFiller)
448     m_pFormFiller->OnDelete(pAnnot);
449 
450   CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
451   CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm();
452   CPDF_FormControl* pCtrol = pWidget->GetFormControl();
453   pInterForm->RemoveMap(pCtrol);
454 
455   delete pWidget;
456 }
457 
OnDraw(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,CFX_RenderDevice * pDevice,CFX_Matrix * pUser2Device,FX_DWORD dwFlags)458 void CPDFSDK_BFAnnotHandler::OnDraw(CPDFSDK_PageView* pPageView,
459                                     CPDFSDK_Annot* pAnnot,
460                                     CFX_RenderDevice* pDevice,
461                                     CFX_Matrix* pUser2Device,
462                                     FX_DWORD dwFlags) {
463   CFX_ByteString sSubType = pAnnot->GetSubType();
464 
465   if (sSubType == BFFT_SIGNATURE) {
466     static_cast<CPDFSDK_BAAnnot*>(pAnnot)
467         ->DrawAppearance(pDevice, pUser2Device, CPDF_Annot::Normal, nullptr);
468   } else {
469     if (m_pFormFiller) {
470       m_pFormFiller->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
471     }
472   }
473 }
474 
OnMouseEnter(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlag)475 void CPDFSDK_BFAnnotHandler::OnMouseEnter(CPDFSDK_PageView* pPageView,
476                                           CPDFSDK_Annot* pAnnot,
477                                           FX_DWORD nFlag) {
478   CFX_ByteString sSubType = pAnnot->GetSubType();
479 
480   if (sSubType == BFFT_SIGNATURE) {
481   } else {
482     if (m_pFormFiller)
483       m_pFormFiller->OnMouseEnter(pPageView, pAnnot, nFlag);
484   }
485 }
OnMouseExit(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlag)486 void CPDFSDK_BFAnnotHandler::OnMouseExit(CPDFSDK_PageView* pPageView,
487                                          CPDFSDK_Annot* pAnnot,
488                                          FX_DWORD nFlag) {
489   CFX_ByteString sSubType = pAnnot->GetSubType();
490 
491   if (sSubType == BFFT_SIGNATURE) {
492   } else {
493     if (m_pFormFiller)
494       m_pFormFiller->OnMouseExit(pPageView, pAnnot, nFlag);
495   }
496 }
OnLButtonDown(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)497 FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonDown(CPDFSDK_PageView* pPageView,
498                                               CPDFSDK_Annot* pAnnot,
499                                               FX_DWORD nFlags,
500                                               const CPDF_Point& point) {
501   CFX_ByteString sSubType = pAnnot->GetSubType();
502 
503   if (sSubType == BFFT_SIGNATURE) {
504   } else {
505     if (m_pFormFiller)
506       return m_pFormFiller->OnLButtonDown(pPageView, pAnnot, nFlags, point);
507   }
508 
509   return FALSE;
510 }
511 
OnLButtonUp(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)512 FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonUp(CPDFSDK_PageView* pPageView,
513                                             CPDFSDK_Annot* pAnnot,
514                                             FX_DWORD nFlags,
515                                             const CPDF_Point& point) {
516   CFX_ByteString sSubType = pAnnot->GetSubType();
517 
518   if (sSubType == BFFT_SIGNATURE) {
519   } else {
520     if (m_pFormFiller)
521       return m_pFormFiller->OnLButtonUp(pPageView, pAnnot, nFlags, point);
522   }
523 
524   return FALSE;
525 }
526 
OnLButtonDblClk(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)527 FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonDblClk(CPDFSDK_PageView* pPageView,
528                                                 CPDFSDK_Annot* pAnnot,
529                                                 FX_DWORD nFlags,
530                                                 const CPDF_Point& point) {
531   CFX_ByteString sSubType = pAnnot->GetSubType();
532 
533   if (sSubType == BFFT_SIGNATURE) {
534   } else {
535     if (m_pFormFiller)
536       return m_pFormFiller->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
537   }
538 
539   return FALSE;
540 }
541 
OnMouseMove(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)542 FX_BOOL CPDFSDK_BFAnnotHandler::OnMouseMove(CPDFSDK_PageView* pPageView,
543                                             CPDFSDK_Annot* pAnnot,
544                                             FX_DWORD nFlags,
545                                             const CPDF_Point& point) {
546   CFX_ByteString sSubType = pAnnot->GetSubType();
547 
548   if (sSubType == BFFT_SIGNATURE) {
549   } else {
550     if (m_pFormFiller)
551       return m_pFormFiller->OnMouseMove(pPageView, pAnnot, nFlags, point);
552   }
553 
554   return FALSE;
555 }
556 
OnMouseWheel(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,short zDelta,const CPDF_Point & point)557 FX_BOOL CPDFSDK_BFAnnotHandler::OnMouseWheel(CPDFSDK_PageView* pPageView,
558                                              CPDFSDK_Annot* pAnnot,
559                                              FX_DWORD nFlags,
560                                              short zDelta,
561                                              const CPDF_Point& point) {
562   CFX_ByteString sSubType = pAnnot->GetSubType();
563 
564   if (sSubType == BFFT_SIGNATURE) {
565   } else {
566     if (m_pFormFiller)
567       return m_pFormFiller->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta,
568                                          point);
569   }
570 
571   return FALSE;
572 }
573 
OnRButtonDown(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)574 FX_BOOL CPDFSDK_BFAnnotHandler::OnRButtonDown(CPDFSDK_PageView* pPageView,
575                                               CPDFSDK_Annot* pAnnot,
576                                               FX_DWORD nFlags,
577                                               const CPDF_Point& point) {
578   CFX_ByteString sSubType = pAnnot->GetSubType();
579 
580   if (sSubType == BFFT_SIGNATURE) {
581   } else {
582     if (m_pFormFiller)
583       return m_pFormFiller->OnRButtonDown(pPageView, pAnnot, nFlags, point);
584   }
585 
586   return FALSE;
587 }
OnRButtonUp(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)588 FX_BOOL CPDFSDK_BFAnnotHandler::OnRButtonUp(CPDFSDK_PageView* pPageView,
589                                             CPDFSDK_Annot* pAnnot,
590                                             FX_DWORD nFlags,
591                                             const CPDF_Point& point) {
592   CFX_ByteString sSubType = pAnnot->GetSubType();
593 
594   if (sSubType == BFFT_SIGNATURE) {
595   } else {
596     if (m_pFormFiller)
597       return m_pFormFiller->OnRButtonUp(pPageView, pAnnot, nFlags, point);
598   }
599 
600   return FALSE;
601 }
602 
OnChar(CPDFSDK_Annot * pAnnot,FX_DWORD nChar,FX_DWORD nFlags)603 FX_BOOL CPDFSDK_BFAnnotHandler::OnChar(CPDFSDK_Annot* pAnnot,
604                                        FX_DWORD nChar,
605                                        FX_DWORD nFlags) {
606   CFX_ByteString sSubType = pAnnot->GetSubType();
607 
608   if (sSubType == BFFT_SIGNATURE) {
609   } else {
610     if (m_pFormFiller)
611       return m_pFormFiller->OnChar(pAnnot, nChar, nFlags);
612   }
613 
614   return FALSE;
615 }
616 
OnKeyDown(CPDFSDK_Annot * pAnnot,int nKeyCode,int nFlag)617 FX_BOOL CPDFSDK_BFAnnotHandler::OnKeyDown(CPDFSDK_Annot* pAnnot,
618                                           int nKeyCode,
619                                           int nFlag) {
620   CFX_ByteString sSubType = pAnnot->GetSubType();
621 
622   if (sSubType == BFFT_SIGNATURE) {
623   } else {
624     if (m_pFormFiller)
625       return m_pFormFiller->OnKeyDown(pAnnot, nKeyCode, nFlag);
626   }
627 
628   return FALSE;
629 }
630 
OnKeyUp(CPDFSDK_Annot * pAnnot,int nKeyCode,int nFlag)631 FX_BOOL CPDFSDK_BFAnnotHandler::OnKeyUp(CPDFSDK_Annot* pAnnot,
632                                         int nKeyCode,
633                                         int nFlag) {
634   return FALSE;
635 }
OnCreate(CPDFSDK_Annot * pAnnot)636 void CPDFSDK_BFAnnotHandler::OnCreate(CPDFSDK_Annot* pAnnot) {
637   CFX_ByteString sSubType = pAnnot->GetSubType();
638 
639   if (sSubType == BFFT_SIGNATURE) {
640   } else {
641     if (m_pFormFiller)
642       m_pFormFiller->OnCreate(pAnnot);
643   }
644 }
645 
OnLoad(CPDFSDK_Annot * pAnnot)646 void CPDFSDK_BFAnnotHandler::OnLoad(CPDFSDK_Annot* pAnnot) {
647   if (pAnnot->GetSubType() == BFFT_SIGNATURE)
648     return;
649 
650   CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
651   if (!pWidget->IsAppearanceValid())
652     pWidget->ResetAppearance(NULL, FALSE);
653 
654   int nFieldType = pWidget->GetFieldType();
655   if (nFieldType == FIELDTYPE_TEXTFIELD || nFieldType == FIELDTYPE_COMBOBOX) {
656     FX_BOOL bFormated = FALSE;
657     CFX_WideString sValue = pWidget->OnFormat(bFormated);
658     if (bFormated && nFieldType == FIELDTYPE_COMBOBOX) {
659       pWidget->ResetAppearance(sValue.c_str(), FALSE);
660     }
661   }
662 
663 #ifdef PDF_ENABLE_XFA
664   CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
665   CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
666   CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
667   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
668     if (!pWidget->IsAppearanceValid() && !pWidget->GetValue().IsEmpty())
669       pWidget->ResetAppearance(FALSE);
670   }
671 #endif  // PDF_ENABLE_XFA
672   if (m_pFormFiller)
673     m_pFormFiller->OnLoad(pAnnot);
674 }
675 
OnSetFocus(CPDFSDK_Annot * pAnnot,FX_DWORD nFlag)676 FX_BOOL CPDFSDK_BFAnnotHandler::OnSetFocus(CPDFSDK_Annot* pAnnot,
677                                            FX_DWORD nFlag) {
678   CFX_ByteString sSubType = pAnnot->GetSubType();
679 
680   if (sSubType == BFFT_SIGNATURE) {
681   } else {
682     if (m_pFormFiller)
683       return m_pFormFiller->OnSetFocus(pAnnot, nFlag);
684   }
685 
686   return TRUE;
687 }
OnKillFocus(CPDFSDK_Annot * pAnnot,FX_DWORD nFlag)688 FX_BOOL CPDFSDK_BFAnnotHandler::OnKillFocus(CPDFSDK_Annot* pAnnot,
689                                             FX_DWORD nFlag) {
690   CFX_ByteString sSubType = pAnnot->GetSubType();
691 
692   if (sSubType == BFFT_SIGNATURE) {
693   } else {
694     if (m_pFormFiller)
695       return m_pFormFiller->OnKillFocus(pAnnot, nFlag);
696   }
697 
698   return TRUE;
699 }
700 
GetViewBBox(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot)701 CPDF_Rect CPDFSDK_BFAnnotHandler::GetViewBBox(CPDFSDK_PageView* pPageView,
702                                               CPDFSDK_Annot* pAnnot) {
703   CFX_ByteString sSubType = pAnnot->GetSubType();
704 
705   if (sSubType == BFFT_SIGNATURE) {
706   } else {
707     if (m_pFormFiller)
708       return m_pFormFiller->GetViewBBox(pPageView, pAnnot);
709   }
710 
711   return CPDF_Rect(0, 0, 0, 0);
712 }
713 
HitTest(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,const CPDF_Point & point)714 FX_BOOL CPDFSDK_BFAnnotHandler::HitTest(CPDFSDK_PageView* pPageView,
715                                         CPDFSDK_Annot* pAnnot,
716                                         const CPDF_Point& point) {
717   ASSERT(pPageView);
718   ASSERT(pAnnot);
719 
720   CPDF_Rect rect = GetViewBBox(pPageView, pAnnot);
721   return rect.Contains(point.x, point.y);
722 }
723 
724 #ifdef PDF_ENABLE_XFA
725 #define FWL_WGTHITTEST_Unknown 0
726 #define FWL_WGTHITTEST_Client 1     // arrow
727 #define FWL_WGTHITTEST_Titlebar 11  // caption
728 #define FWL_WGTHITTEST_HScrollBar 15
729 #define FWL_WGTHITTEST_VScrollBar 16
730 #define FWL_WGTHITTEST_Border 17
731 #define FWL_WGTHITTEST_Edit 19
732 #define FWL_WGTHITTEST_HyperLink 20
733 
CPDFSDK_XFAAnnotHandler(CPDFDoc_Environment * pApp)734 CPDFSDK_XFAAnnotHandler::CPDFSDK_XFAAnnotHandler(CPDFDoc_Environment* pApp)
735     : m_pApp(pApp) {}
736 
NewAnnot(IXFA_Widget * pAnnot,CPDFSDK_PageView * pPage)737 CPDFSDK_Annot* CPDFSDK_XFAAnnotHandler::NewAnnot(IXFA_Widget* pAnnot,
738                                                  CPDFSDK_PageView* pPage) {
739   CPDFSDK_Document* pSDKDoc = m_pApp->GetSDKDocument();
740   CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pSDKDoc->GetInterForm();
741   CPDFSDK_XFAWidget* pWidget = new CPDFSDK_XFAWidget(pAnnot, pPage, pInterForm);
742   pInterForm->AddXFAMap(pAnnot, pWidget);
743   return pWidget;
744 }
745 
CanAnswer(CPDFSDK_Annot * pAnnot)746 FX_BOOL CPDFSDK_XFAAnnotHandler::CanAnswer(CPDFSDK_Annot* pAnnot) {
747   return pAnnot->GetXFAWidget() != NULL;
748 }
749 
OnDraw(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,CFX_RenderDevice * pDevice,CFX_Matrix * pUser2Device,FX_DWORD dwFlags)750 void CPDFSDK_XFAAnnotHandler::OnDraw(CPDFSDK_PageView* pPageView,
751                                      CPDFSDK_Annot* pAnnot,
752                                      CFX_RenderDevice* pDevice,
753                                      CFX_Matrix* pUser2Device,
754                                      FX_DWORD dwFlags) {
755   ASSERT(pPageView != NULL);
756   ASSERT(pAnnot != NULL);
757 
758   CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
759   ASSERT(pSDKDoc != NULL);
760 
761   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
762   ASSERT(pWidgetHandler != NULL);
763 
764   CFX_Graphics gs;
765   gs.Create(pDevice);
766 
767   CFX_Matrix mt;
768   mt = *(CFX_Matrix*)pUser2Device;
769 
770   FX_BOOL bIsHighlight = FALSE;
771   if (pSDKDoc->GetFocusAnnot() != pAnnot)
772     bIsHighlight = TRUE;
773 
774   pWidgetHandler->RenderWidget(pAnnot->GetXFAWidget(), &gs, &mt, bIsHighlight);
775 
776   // to do highlight and shadow
777 }
778 
ReleaseAnnot(CPDFSDK_Annot * pAnnot)779 void CPDFSDK_XFAAnnotHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot) {
780   ASSERT(pAnnot != NULL);
781 
782   CPDFSDK_XFAWidget* pWidget = (CPDFSDK_XFAWidget*)pAnnot;
783   CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm();
784   ASSERT(pInterForm != NULL);
785 
786   pInterForm->RemoveXFAMap(pWidget->GetXFAWidget());
787 
788   delete pWidget;
789 }
790 
GetViewBBox(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot)791 CPDF_Rect CPDFSDK_XFAAnnotHandler::GetViewBBox(CPDFSDK_PageView* pPageView,
792                                                CPDFSDK_Annot* pAnnot) {
793   ASSERT(pAnnot != NULL);
794 
795   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
796   ASSERT(pWidgetHandler != NULL);
797 
798   CFX_RectF rcBBox;
799   XFA_ELEMENT eType =
800       pWidgetHandler->GetDataAcc(pAnnot->GetXFAWidget())->GetUIType();
801   if (eType == XFA_ELEMENT_Signature)
802     pWidgetHandler->GetBBox(pAnnot->GetXFAWidget(), rcBBox,
803                             XFA_WIDGETSTATUS_Visible, TRUE);
804   else
805     pWidgetHandler->GetBBox(pAnnot->GetXFAWidget(), rcBBox, 0);
806 
807   CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width,
808                          rcBBox.top + rcBBox.height);
809   rcWidget.left -= 1.0f;
810   rcWidget.right += 1.0f;
811   rcWidget.bottom -= 1.0f;
812   rcWidget.top += 1.0f;
813 
814   return rcWidget;
815 }
816 
HitTest(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,const CPDF_Point & point)817 FX_BOOL CPDFSDK_XFAAnnotHandler::HitTest(CPDFSDK_PageView* pPageView,
818                                          CPDFSDK_Annot* pAnnot,
819                                          const CPDF_Point& point) {
820   if (!pPageView || !pAnnot)
821     return FALSE;
822 
823   CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
824   if (!pSDKDoc)
825     return FALSE;
826 
827   CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
828   if (!pDoc)
829     return FALSE;
830 
831   IXFA_DocView* pDocView = pDoc->GetXFADocView();
832   if (!pDocView)
833     return FALSE;
834 
835   IXFA_WidgetHandler* pWidgetHandler = pDocView->GetWidgetHandler();
836   if (!pWidgetHandler)
837     return FALSE;
838 
839   FX_DWORD dwHitTest =
840       pWidgetHandler->OnHitTest(pAnnot->GetXFAWidget(), point.x, point.y);
841   return (dwHitTest != FWL_WGTHITTEST_Unknown);
842 }
843 
OnMouseEnter(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlag)844 void CPDFSDK_XFAAnnotHandler::OnMouseEnter(CPDFSDK_PageView* pPageView,
845                                            CPDFSDK_Annot* pAnnot,
846                                            FX_DWORD nFlag) {
847   if (!pPageView || !pAnnot)
848     return;
849   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
850   ASSERT(pWidgetHandler != NULL);
851 
852   pWidgetHandler->OnMouseEnter(pAnnot->GetXFAWidget());
853 }
854 
OnMouseExit(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlag)855 void CPDFSDK_XFAAnnotHandler::OnMouseExit(CPDFSDK_PageView* pPageView,
856                                           CPDFSDK_Annot* pAnnot,
857                                           FX_DWORD nFlag) {
858   if (!pPageView || !pAnnot)
859     return;
860 
861   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
862   ASSERT(pWidgetHandler != NULL);
863 
864   pWidgetHandler->OnMouseExit(pAnnot->GetXFAWidget());
865 }
866 
OnLButtonDown(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)867 FX_BOOL CPDFSDK_XFAAnnotHandler::OnLButtonDown(CPDFSDK_PageView* pPageView,
868                                                CPDFSDK_Annot* pAnnot,
869                                                FX_DWORD nFlags,
870                                                const CPDF_Point& point) {
871   if (!pPageView || !pAnnot)
872     return FALSE;
873 
874   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
875   ASSERT(pWidgetHandler != NULL);
876 
877   FX_BOOL bRet = FALSE;
878   bRet = pWidgetHandler->OnLButtonDown(pAnnot->GetXFAWidget(),
879                                        GetFWLFlags(nFlags), point.x, point.y);
880 
881   return bRet;
882 }
883 
OnLButtonUp(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)884 FX_BOOL CPDFSDK_XFAAnnotHandler::OnLButtonUp(CPDFSDK_PageView* pPageView,
885                                              CPDFSDK_Annot* pAnnot,
886                                              FX_DWORD nFlags,
887                                              const CPDF_Point& point) {
888   if (!pPageView || !pAnnot)
889     return FALSE;
890 
891   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
892   ASSERT(pWidgetHandler != NULL);
893 
894   FX_BOOL bRet = FALSE;
895   bRet = pWidgetHandler->OnLButtonUp(pAnnot->GetXFAWidget(),
896                                      GetFWLFlags(nFlags), point.x, point.y);
897 
898   return bRet;
899 }
900 
OnLButtonDblClk(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)901 FX_BOOL CPDFSDK_XFAAnnotHandler::OnLButtonDblClk(CPDFSDK_PageView* pPageView,
902                                                  CPDFSDK_Annot* pAnnot,
903                                                  FX_DWORD nFlags,
904                                                  const CPDF_Point& point) {
905   if (!pPageView || !pAnnot)
906     return FALSE;
907 
908   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
909   ASSERT(pWidgetHandler != NULL);
910 
911   FX_BOOL bRet = FALSE;
912   bRet = pWidgetHandler->OnLButtonDblClk(pAnnot->GetXFAWidget(),
913                                          GetFWLFlags(nFlags), point.x, point.y);
914 
915   return bRet;
916 }
917 
OnMouseMove(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)918 FX_BOOL CPDFSDK_XFAAnnotHandler::OnMouseMove(CPDFSDK_PageView* pPageView,
919                                              CPDFSDK_Annot* pAnnot,
920                                              FX_DWORD nFlags,
921                                              const CPDF_Point& point) {
922   if (!pPageView || !pAnnot)
923     return FALSE;
924 
925   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
926   ASSERT(pWidgetHandler != NULL);
927 
928   FX_BOOL bRet = FALSE;
929   bRet = pWidgetHandler->OnMouseMove(pAnnot->GetXFAWidget(),
930                                      GetFWLFlags(nFlags), point.x, point.y);
931 
932   return bRet;
933 }
934 
OnMouseWheel(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,short zDelta,const CPDF_Point & point)935 FX_BOOL CPDFSDK_XFAAnnotHandler::OnMouseWheel(CPDFSDK_PageView* pPageView,
936                                               CPDFSDK_Annot* pAnnot,
937                                               FX_DWORD nFlags,
938                                               short zDelta,
939                                               const CPDF_Point& point) {
940   if (!pPageView || !pAnnot)
941     return FALSE;
942 
943   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
944   ASSERT(pWidgetHandler != NULL);
945 
946   FX_BOOL bRet = FALSE;
947   bRet = pWidgetHandler->OnMouseWheel(
948       pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), zDelta, point.x, point.y);
949 
950   return bRet;
951 }
952 
OnRButtonDown(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)953 FX_BOOL CPDFSDK_XFAAnnotHandler::OnRButtonDown(CPDFSDK_PageView* pPageView,
954                                                CPDFSDK_Annot* pAnnot,
955                                                FX_DWORD nFlags,
956                                                const CPDF_Point& point) {
957   if (!pPageView || !pAnnot)
958     return FALSE;
959 
960   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
961   ASSERT(pWidgetHandler != NULL);
962 
963   FX_BOOL bRet = FALSE;
964   bRet = pWidgetHandler->OnRButtonDown(pAnnot->GetXFAWidget(),
965                                        GetFWLFlags(nFlags), point.x, point.y);
966 
967   return bRet;
968 }
969 
OnRButtonUp(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)970 FX_BOOL CPDFSDK_XFAAnnotHandler::OnRButtonUp(CPDFSDK_PageView* pPageView,
971                                              CPDFSDK_Annot* pAnnot,
972                                              FX_DWORD nFlags,
973                                              const CPDF_Point& point) {
974   if (!pPageView || !pAnnot)
975     return FALSE;
976 
977   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
978   ASSERT(pWidgetHandler != NULL);
979 
980   FX_BOOL bRet = FALSE;
981   bRet = pWidgetHandler->OnRButtonUp(pAnnot->GetXFAWidget(),
982                                      GetFWLFlags(nFlags), point.x, point.y);
983 
984   return bRet;
985 }
986 
OnRButtonDblClk(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,FX_DWORD nFlags,const CPDF_Point & point)987 FX_BOOL CPDFSDK_XFAAnnotHandler::OnRButtonDblClk(CPDFSDK_PageView* pPageView,
988                                                  CPDFSDK_Annot* pAnnot,
989                                                  FX_DWORD nFlags,
990                                                  const CPDF_Point& point) {
991   if (!pPageView || !pAnnot)
992     return FALSE;
993 
994   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
995   ASSERT(pWidgetHandler != NULL);
996 
997   FX_BOOL bRet = FALSE;
998   bRet = pWidgetHandler->OnRButtonDblClk(pAnnot->GetXFAWidget(),
999                                          GetFWLFlags(nFlags), point.x, point.y);
1000 
1001   return bRet;
1002 }
1003 
OnChar(CPDFSDK_Annot * pAnnot,FX_DWORD nChar,FX_DWORD nFlags)1004 FX_BOOL CPDFSDK_XFAAnnotHandler::OnChar(CPDFSDK_Annot* pAnnot,
1005                                         FX_DWORD nChar,
1006                                         FX_DWORD nFlags) {
1007   if (!pAnnot)
1008     return FALSE;
1009 
1010   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1011   ASSERT(pWidgetHandler != NULL);
1012 
1013   FX_BOOL bRet = FALSE;
1014   bRet = pWidgetHandler->OnChar(pAnnot->GetXFAWidget(), nChar,
1015                                 GetFWLFlags(nFlags));
1016 
1017   return bRet;
1018 }
1019 
OnKeyDown(CPDFSDK_Annot * pAnnot,int nKeyCode,int nFlag)1020 FX_BOOL CPDFSDK_XFAAnnotHandler::OnKeyDown(CPDFSDK_Annot* pAnnot,
1021                                            int nKeyCode,
1022                                            int nFlag) {
1023   if (!pAnnot)
1024     return FALSE;
1025 
1026   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1027   ASSERT(pWidgetHandler != NULL);
1028 
1029   FX_BOOL bRet = FALSE;
1030   bRet = pWidgetHandler->OnKeyDown(pAnnot->GetXFAWidget(), nKeyCode,
1031                                    GetFWLFlags(nFlag));
1032 
1033   return bRet;
1034 }
1035 
OnKeyUp(CPDFSDK_Annot * pAnnot,int nKeyCode,int nFlag)1036 FX_BOOL CPDFSDK_XFAAnnotHandler::OnKeyUp(CPDFSDK_Annot* pAnnot,
1037                                          int nKeyCode,
1038                                          int nFlag) {
1039   if (!pAnnot)
1040     return FALSE;
1041 
1042   IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1043   ASSERT(pWidgetHandler != NULL);
1044 
1045   FX_BOOL bRet = FALSE;
1046   bRet = pWidgetHandler->OnKeyUp(pAnnot->GetXFAWidget(), nKeyCode,
1047                                  GetFWLFlags(nFlag));
1048 
1049   return bRet;
1050 }
1051 
OnSetFocus(CPDFSDK_Annot * pAnnot,FX_DWORD nFlag)1052 FX_BOOL CPDFSDK_XFAAnnotHandler::OnSetFocus(CPDFSDK_Annot* pAnnot,
1053                                             FX_DWORD nFlag) {
1054   return TRUE;
1055 }
1056 
OnKillFocus(CPDFSDK_Annot * pAnnot,FX_DWORD nFlag)1057 FX_BOOL CPDFSDK_XFAAnnotHandler::OnKillFocus(CPDFSDK_Annot* pAnnot,
1058                                              FX_DWORD nFlag) {
1059   return TRUE;
1060 }
1061 
OnXFAChangedFocus(CPDFSDK_Annot * pOldAnnot,CPDFSDK_Annot * pNewAnnot)1062 FX_BOOL CPDFSDK_XFAAnnotHandler::OnXFAChangedFocus(CPDFSDK_Annot* pOldAnnot,
1063                                                    CPDFSDK_Annot* pNewAnnot) {
1064   IXFA_WidgetHandler* pWidgetHandler = NULL;
1065 
1066   if (pOldAnnot)
1067     pWidgetHandler = GetXFAWidgetHandler(pOldAnnot);
1068   else if (pNewAnnot)
1069     pWidgetHandler = GetXFAWidgetHandler(pNewAnnot);
1070 
1071   if (pWidgetHandler) {
1072     FX_BOOL bRet = TRUE;
1073     IXFA_Widget* hWidget = pNewAnnot ? pNewAnnot->GetXFAWidget() : NULL;
1074     if (hWidget) {
1075       IXFA_PageView* pXFAPageView = pWidgetHandler->GetPageView(hWidget);
1076       if (pXFAPageView) {
1077         bRet = pXFAPageView->GetDocView()->SetFocus(hWidget);
1078         if (pXFAPageView->GetDocView()->GetFocusWidget() == hWidget)
1079           bRet = TRUE;
1080       }
1081     }
1082     return bRet;
1083   }
1084 
1085   return TRUE;
1086 }
1087 
GetXFAWidgetHandler(CPDFSDK_Annot * pAnnot)1088 IXFA_WidgetHandler* CPDFSDK_XFAAnnotHandler::GetXFAWidgetHandler(
1089     CPDFSDK_Annot* pAnnot) {
1090   if (!pAnnot)
1091     return NULL;
1092 
1093   CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
1094   if (!pPageView)
1095     return NULL;
1096 
1097   CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
1098   if (!pSDKDoc)
1099     return NULL;
1100 
1101   CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
1102   if (!pDoc)
1103     return NULL;
1104 
1105   IXFA_DocView* pDocView = pDoc->GetXFADocView();
1106   if (!pDocView)
1107     return NULL;
1108 
1109   return pDocView->GetWidgetHandler();
1110 }
1111 
1112 #define FWL_KEYFLAG_Ctrl (1 << 0)
1113 #define FWL_KEYFLAG_Alt (1 << 1)
1114 #define FWL_KEYFLAG_Shift (1 << 2)
1115 #define FWL_KEYFLAG_LButton (1 << 3)
1116 #define FWL_KEYFLAG_RButton (1 << 4)
1117 #define FWL_KEYFLAG_MButton (1 << 5)
1118 
GetFWLFlags(FX_DWORD dwFlag)1119 FX_DWORD CPDFSDK_XFAAnnotHandler::GetFWLFlags(FX_DWORD dwFlag) {
1120   FX_DWORD dwFWLFlag = 0;
1121 
1122   if (dwFlag & FWL_EVENTFLAG_ControlKey)
1123     dwFWLFlag |= FWL_KEYFLAG_Ctrl;
1124   if (dwFlag & FWL_EVENTFLAG_LeftButtonDown)
1125     dwFWLFlag |= FWL_KEYFLAG_LButton;
1126   if (dwFlag & FWL_EVENTFLAG_MiddleButtonDown)
1127     dwFWLFlag |= FWL_KEYFLAG_MButton;
1128   if (dwFlag & FWL_EVENTFLAG_RightButtonDown)
1129     dwFWLFlag |= FWL_KEYFLAG_RButton;
1130   if (dwFlag & FWL_EVENTFLAG_ShiftKey)
1131     dwFWLFlag |= FWL_KEYFLAG_Shift;
1132   if (dwFlag & FWL_EVENTFLAG_AltKey)
1133     dwFWLFlag |= FWL_KEYFLAG_Alt;
1134 
1135   return dwFWLFlag;
1136 }
1137 #endif  // PDF_ENABLE_XFA
1138 
CPDFSDK_AnnotIterator(CPDFSDK_PageView * pPageView,bool bReverse)1139 CPDFSDK_AnnotIterator::CPDFSDK_AnnotIterator(CPDFSDK_PageView* pPageView,
1140                                              bool bReverse)
1141     : m_bReverse(bReverse), m_pos(0) {
1142   const std::vector<CPDFSDK_Annot*>& annots = pPageView->GetAnnotList();
1143   m_iteratorAnnotList.insert(m_iteratorAnnotList.begin(), annots.rbegin(),
1144                              annots.rend());
1145   std::stable_sort(m_iteratorAnnotList.begin(), m_iteratorAnnotList.end(),
1146                    [](CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) {
1147                      return p1->GetLayoutOrder() < p2->GetLayoutOrder();
1148                    });
1149 
1150   CPDFSDK_Annot* pTopMostAnnot = pPageView->GetFocusAnnot();
1151   if (!pTopMostAnnot)
1152     return;
1153 
1154   auto it = std::find(m_iteratorAnnotList.begin(), m_iteratorAnnotList.end(),
1155                       pTopMostAnnot);
1156   if (it != m_iteratorAnnotList.end()) {
1157     CPDFSDK_Annot* pReaderAnnot = *it;
1158     m_iteratorAnnotList.erase(it);
1159     m_iteratorAnnotList.insert(m_iteratorAnnotList.begin(), pReaderAnnot);
1160   }
1161 }
1162 
~CPDFSDK_AnnotIterator()1163 CPDFSDK_AnnotIterator::~CPDFSDK_AnnotIterator() {
1164 }
1165 
NextAnnot()1166 CPDFSDK_Annot* CPDFSDK_AnnotIterator::NextAnnot() {
1167   if (m_pos < m_iteratorAnnotList.size())
1168     return m_iteratorAnnotList[m_pos++];
1169   return nullptr;
1170 }
1171 
PrevAnnot()1172 CPDFSDK_Annot* CPDFSDK_AnnotIterator::PrevAnnot() {
1173   if (m_pos < m_iteratorAnnotList.size())
1174     return m_iteratorAnnotList[m_iteratorAnnotList.size() - ++m_pos];
1175   return nullptr;
1176 }
1177 
Next()1178 CPDFSDK_Annot* CPDFSDK_AnnotIterator::Next() {
1179   return m_bReverse ? PrevAnnot() : NextAnnot();
1180 }
1181