Cleanup parts of CPDFSDK_AnnotIterator and CPDFSDK_PageView.
[pdfium.git] / fpdfsdk / src / fsdk_annothandler.cpp
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 "../include/fsdk_define.h"
10 #include "../include/fsdk_mgr.h"
11 #include "../include/formfiller/FFL_FormFiller.h"
12 #include "../include/fsdk_annothandler.h"
13
14 CPDFSDK_AnnotHandlerMgr::CPDFSDK_AnnotHandlerMgr(CPDFDoc_Environment* pApp) {
15   m_pApp = pApp;
16
17   CPDFSDK_BFAnnotHandler* pHandler = new CPDFSDK_BFAnnotHandler(m_pApp);
18   pHandler->SetFormFiller(m_pApp->GetIFormFiller());
19   RegisterAnnotHandler(pHandler);
20 }
21
22 CPDFSDK_AnnotHandlerMgr::~CPDFSDK_AnnotHandlerMgr() {
23   for (int i = 0; i < m_Handlers.GetSize(); i++) {
24     IPDFSDK_AnnotHandler* pHandler = m_Handlers.GetAt(i);
25     delete pHandler;
26   }
27   m_Handlers.RemoveAll();
28   m_mapType2Handler.clear();
29 }
30
31 void CPDFSDK_AnnotHandlerMgr::RegisterAnnotHandler(
32     IPDFSDK_AnnotHandler* pAnnotHandler) {
33   ASSERT(!GetAnnotHandler(pAnnotHandler->GetType()));
34
35   m_Handlers.Add(pAnnotHandler);
36   m_mapType2Handler[pAnnotHandler->GetType()] = pAnnotHandler;
37 }
38
39 void CPDFSDK_AnnotHandlerMgr::UnRegisterAnnotHandler(
40     IPDFSDK_AnnotHandler* pAnnotHandler) {
41   m_mapType2Handler.erase(pAnnotHandler->GetType());
42   for (int i = 0, sz = m_Handlers.GetSize(); i < sz; i++) {
43     if (m_Handlers.GetAt(i) == pAnnotHandler) {
44       m_Handlers.RemoveAt(i);
45       break;
46     }
47   }
48 }
49
50 CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(CPDF_Annot* pAnnot,
51                                                  CPDFSDK_PageView* pPageView) {
52   ASSERT(pAnnot != NULL);
53   ASSERT(pPageView != NULL);
54
55   if (IPDFSDK_AnnotHandler* pAnnotHandler =
56           GetAnnotHandler(pAnnot->GetSubType())) {
57     return pAnnotHandler->NewAnnot(pAnnot, pPageView);
58   }
59
60   return new CPDFSDK_Annot(pAnnot, pPageView);
61 }
62
63 void CPDFSDK_AnnotHandlerMgr::ReleaseAnnot(CPDFSDK_Annot* pAnnot) {
64   ASSERT(pAnnot != NULL);
65
66   pAnnot->GetPDFPage();
67
68   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
69     pAnnotHandler->OnRelease(pAnnot);
70     pAnnotHandler->ReleaseAnnot(pAnnot);
71   } else {
72     delete (CPDFSDK_Annot*)pAnnot;
73   }
74 }
75
76 void CPDFSDK_AnnotHandlerMgr::Annot_OnCreate(CPDFSDK_Annot* pAnnot) {
77   ASSERT(pAnnot != NULL);
78
79   CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
80
81   CPDFSDK_DateTime curTime;
82   pPDFAnnot->GetAnnotDict()->SetAtString("M", curTime.ToPDFDateTimeString());
83   pPDFAnnot->GetAnnotDict()->SetAtNumber("F", 0);
84
85   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
86     pAnnotHandler->OnCreate(pAnnot);
87   }
88 }
89
90 void CPDFSDK_AnnotHandlerMgr::Annot_OnLoad(CPDFSDK_Annot* pAnnot) {
91   ASSERT(pAnnot != NULL);
92
93   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
94     pAnnotHandler->OnLoad(pAnnot);
95   }
96 }
97
98 IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
99     CPDFSDK_Annot* pAnnot) const {
100   ASSERT(pAnnot != NULL);
101
102   CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
103   ASSERT(pPDFAnnot != NULL);
104
105   return GetAnnotHandler(pPDFAnnot->GetSubType());
106 }
107
108 IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
109     const CFX_ByteString& sType) const {
110   auto it = m_mapType2Handler.find(sType);
111   return it != m_mapType2Handler.end() ? it->second : nullptr;
112 }
113
114 void CPDFSDK_AnnotHandlerMgr::Annot_OnDraw(CPDFSDK_PageView* pPageView,
115                                            CPDFSDK_Annot* pAnnot,
116                                            CFX_RenderDevice* pDevice,
117                                            CPDF_Matrix* pUser2Device,
118                                            FX_DWORD dwFlags) {
119   ASSERT(pAnnot != NULL);
120
121   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
122     pAnnotHandler->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
123   } else {
124     pAnnot->DrawAppearance(pDevice, pUser2Device, CPDF_Annot::Normal, NULL);
125   }
126 }
127
128 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDown(
129     CPDFSDK_PageView* pPageView,
130     CPDFSDK_Annot* pAnnot,
131     FX_DWORD nFlags,
132     const CPDF_Point& point) {
133   ASSERT(pAnnot != NULL);
134
135   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
136     return pAnnotHandler->OnLButtonDown(pPageView, pAnnot, nFlags, point);
137   }
138   return FALSE;
139 }
140 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonUp(CPDFSDK_PageView* pPageView,
141                                                    CPDFSDK_Annot* pAnnot,
142                                                    FX_DWORD nFlags,
143                                                    const CPDF_Point& point) {
144   ASSERT(pAnnot != NULL);
145
146   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
147     return pAnnotHandler->OnLButtonUp(pPageView, pAnnot, nFlags, point);
148   }
149   return FALSE;
150 }
151 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDblClk(
152     CPDFSDK_PageView* pPageView,
153     CPDFSDK_Annot* pAnnot,
154     FX_DWORD nFlags,
155     const CPDF_Point& point) {
156   ASSERT(pAnnot != NULL);
157
158   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
159     return pAnnotHandler->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
160   }
161   return FALSE;
162 }
163 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseMove(CPDFSDK_PageView* pPageView,
164                                                    CPDFSDK_Annot* pAnnot,
165                                                    FX_DWORD nFlags,
166                                                    const CPDF_Point& point) {
167   ASSERT(pAnnot != NULL);
168
169   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
170     return pAnnotHandler->OnMouseMove(pPageView, pAnnot, nFlags, point);
171   }
172   return FALSE;
173 }
174 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseWheel(CPDFSDK_PageView* pPageView,
175                                                     CPDFSDK_Annot* pAnnot,
176                                                     FX_DWORD nFlags,
177                                                     short zDelta,
178                                                     const CPDF_Point& point) {
179   ASSERT(pAnnot != NULL);
180
181   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
182     return pAnnotHandler->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta,
183                                        point);
184   }
185   return FALSE;
186 }
187 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonDown(
188     CPDFSDK_PageView* pPageView,
189     CPDFSDK_Annot* pAnnot,
190     FX_DWORD nFlags,
191     const CPDF_Point& point) {
192   ASSERT(pAnnot != NULL);
193
194   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
195     return pAnnotHandler->OnRButtonDown(pPageView, pAnnot, nFlags, point);
196   }
197   return FALSE;
198 }
199 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonUp(CPDFSDK_PageView* pPageView,
200                                                    CPDFSDK_Annot* pAnnot,
201                                                    FX_DWORD nFlags,
202                                                    const CPDF_Point& point) {
203   ASSERT(pAnnot != NULL);
204
205   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
206     return pAnnotHandler->OnRButtonUp(pPageView, pAnnot, nFlags, point);
207   }
208   return FALSE;
209 }
210
211 void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseEnter(CPDFSDK_PageView* pPageView,
212                                                  CPDFSDK_Annot* pAnnot,
213                                                  FX_DWORD nFlag) {
214   ASSERT(pAnnot != NULL);
215
216   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
217     pAnnotHandler->OnMouseEnter(pPageView, pAnnot, nFlag);
218   }
219   return;
220 }
221
222 void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseExit(CPDFSDK_PageView* pPageView,
223                                                 CPDFSDK_Annot* pAnnot,
224                                                 FX_DWORD nFlag) {
225   ASSERT(pAnnot != NULL);
226
227   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
228     pAnnotHandler->OnMouseExit(pPageView, pAnnot, nFlag);
229   }
230   return;
231 }
232
233 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChar(CPDFSDK_Annot* pAnnot,
234                                               FX_DWORD nChar,
235                                               FX_DWORD nFlags) {
236   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
237     return pAnnotHandler->OnChar(pAnnot, nChar, nFlags);
238   }
239   return FALSE;
240 }
241
242 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyDown(CPDFSDK_Annot* pAnnot,
243                                                  int nKeyCode,
244                                                  int nFlag) {
245   if (!m_pApp->FFI_IsCTRLKeyDown(nFlag) && !m_pApp->FFI_IsALTKeyDown(nFlag)) {
246     CPDFSDK_PageView* pPage = pAnnot->GetPageView();
247     CPDFSDK_Annot* pFocusAnnot = pPage->GetFocusAnnot();
248     if (pFocusAnnot && (nKeyCode == FWL_VKEY_Tab)) {
249       CPDFSDK_Annot* pNext =
250           GetNextAnnot(pFocusAnnot, !m_pApp->FFI_IsSHIFTKeyDown(nFlag));
251
252       if (pNext && pNext != pFocusAnnot) {
253         CPDFSDK_Document* pDocument = pPage->GetSDKDocument();
254         pDocument->SetFocusAnnot(pNext);
255         return TRUE;
256       }
257     }
258   }
259
260   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
261     return pAnnotHandler->OnKeyDown(pAnnot, nKeyCode, nFlag);
262   }
263   return FALSE;
264 }
265 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyUp(CPDFSDK_Annot* pAnnot,
266                                                int nKeyCode,
267                                                int nFlag) {
268   return FALSE;
269 }
270
271 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnSetFocus(CPDFSDK_Annot* pAnnot,
272                                                   FX_DWORD nFlag) {
273   ASSERT(pAnnot != NULL);
274
275   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
276     if (pAnnotHandler->OnSetFocus(pAnnot, nFlag)) {
277       CPDFSDK_PageView* pPage = pAnnot->GetPageView();
278       pPage->GetSDKDocument();
279       return TRUE;
280     }
281   }
282   return FALSE;
283 }
284
285 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKillFocus(CPDFSDK_Annot* pAnnot,
286                                                    FX_DWORD nFlag) {
287   ASSERT(pAnnot);
288   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
289     return pAnnotHandler->OnKillFocus(pAnnot, nFlag);
290
291   return FALSE;
292 }
293
294 CPDF_Rect CPDFSDK_AnnotHandlerMgr::Annot_OnGetViewBBox(
295     CPDFSDK_PageView* pPageView,
296     CPDFSDK_Annot* pAnnot) {
297   ASSERT(pAnnot);
298   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
299     return pAnnotHandler->GetViewBBox(pPageView, pAnnot);
300
301   return pAnnot->GetRect();
302 }
303
304 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnHitTest(CPDFSDK_PageView* pPageView,
305                                                  CPDFSDK_Annot* pAnnot,
306                                                  const CPDF_Point& point) {
307   ASSERT(pAnnot);
308   if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
309     if (pAnnotHandler->CanAnswer(pAnnot))
310       return pAnnotHandler->HitTest(pPageView, pAnnot, point);
311   }
312   return FALSE;
313 }
314
315 CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::GetNextAnnot(CPDFSDK_Annot* pSDKAnnot,
316                                                      FX_BOOL bNext) {
317   CBA_AnnotIterator ai(pSDKAnnot->GetPageView(), "Widget", "");
318   return bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
319 }
320
321 FX_BOOL CPDFSDK_BFAnnotHandler::CanAnswer(CPDFSDK_Annot* pAnnot) {
322   ASSERT(pAnnot->GetType() == "Widget");
323   if (pAnnot->GetSubType() == BFFT_SIGNATURE)
324     return FALSE;
325
326   CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
327   if (!pWidget->IsVisible())
328     return FALSE;
329
330   int nFieldFlags = pWidget->GetFieldFlags();
331   if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY)
332     return FALSE;
333
334   if (pWidget->GetFieldType() == FIELDTYPE_PUSHBUTTON)
335     return TRUE;
336
337   CPDF_Page* pPage = pWidget->GetPDFPage();
338   CPDF_Document* pDocument = pPage->m_pDocument;
339   FX_DWORD dwPermissions = pDocument->GetUserPermissions();
340   return (dwPermissions & FPDFPERM_FILL_FORM) ||
341          (dwPermissions & FPDFPERM_ANNOT_FORM);
342 }
343
344 CPDFSDK_Annot* CPDFSDK_BFAnnotHandler::NewAnnot(CPDF_Annot* pAnnot,
345                                                 CPDFSDK_PageView* pPage) {
346   CPDFSDK_Document* pSDKDoc = m_pApp->GetSDKDocument();
347   CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pSDKDoc->GetInterForm();
348   CPDF_FormControl* pCtrl = CPDFSDK_Widget::GetFormControl(
349       pInterForm->GetInterForm(), pAnnot->GetAnnotDict());
350   if (!pCtrl)
351     return nullptr;
352
353   CPDFSDK_Widget* pWidget = new CPDFSDK_Widget(pAnnot, pPage, pInterForm);
354   pInterForm->AddMap(pCtrl, pWidget);
355   CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
356   if (pPDFInterForm && pPDFInterForm->NeedConstructAP())
357     pWidget->ResetAppearance(nullptr, FALSE);
358
359   return pWidget;
360 }
361
362 void CPDFSDK_BFAnnotHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot) {
363   ASSERT(pAnnot != NULL);
364
365   if (m_pFormFiller)
366     m_pFormFiller->OnDelete(pAnnot);
367
368   CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
369   CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm();
370   ASSERT(pInterForm != NULL);
371
372   CPDF_FormControl* pCtrol = pWidget->GetFormControl();
373   pInterForm->RemoveMap(pCtrol);
374
375   delete pWidget;
376 }
377
378 void CPDFSDK_BFAnnotHandler::OnDraw(CPDFSDK_PageView* pPageView,
379                                     CPDFSDK_Annot* pAnnot,
380                                     CFX_RenderDevice* pDevice,
381                                     CPDF_Matrix* pUser2Device,
382                                     FX_DWORD dwFlags) {
383   ASSERT(pAnnot != NULL);
384   CFX_ByteString sSubType = pAnnot->GetSubType();
385
386   if (sSubType == BFFT_SIGNATURE) {
387     pAnnot->DrawAppearance(pDevice, pUser2Device, CPDF_Annot::Normal, NULL);
388   } else {
389     if (m_pFormFiller) {
390       m_pFormFiller->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
391     }
392   }
393 }
394
395 void CPDFSDK_BFAnnotHandler::OnMouseEnter(CPDFSDK_PageView* pPageView,
396                                           CPDFSDK_Annot* pAnnot,
397                                           FX_DWORD nFlag) {
398   ASSERT(pAnnot != NULL);
399   CFX_ByteString sSubType = pAnnot->GetSubType();
400
401   if (sSubType == BFFT_SIGNATURE) {
402   } else {
403     if (m_pFormFiller)
404       m_pFormFiller->OnMouseEnter(pPageView, pAnnot, nFlag);
405   }
406 }
407 void CPDFSDK_BFAnnotHandler::OnMouseExit(CPDFSDK_PageView* pPageView,
408                                          CPDFSDK_Annot* pAnnot,
409                                          FX_DWORD nFlag) {
410   ASSERT(pAnnot != NULL);
411   CFX_ByteString sSubType = pAnnot->GetSubType();
412
413   if (sSubType == BFFT_SIGNATURE) {
414   } else {
415     if (m_pFormFiller)
416       m_pFormFiller->OnMouseExit(pPageView, pAnnot, nFlag);
417   }
418 }
419 FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonDown(CPDFSDK_PageView* pPageView,
420                                               CPDFSDK_Annot* pAnnot,
421                                               FX_DWORD nFlags,
422                                               const CPDF_Point& point) {
423   ASSERT(pAnnot != NULL);
424   CFX_ByteString sSubType = pAnnot->GetSubType();
425
426   if (sSubType == BFFT_SIGNATURE) {
427   } else {
428     if (m_pFormFiller)
429       return m_pFormFiller->OnLButtonDown(pPageView, pAnnot, nFlags, point);
430   }
431
432   return FALSE;
433 }
434
435 FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonUp(CPDFSDK_PageView* pPageView,
436                                             CPDFSDK_Annot* pAnnot,
437                                             FX_DWORD nFlags,
438                                             const CPDF_Point& point) {
439   ASSERT(pAnnot != NULL);
440   CFX_ByteString sSubType = pAnnot->GetSubType();
441
442   if (sSubType == BFFT_SIGNATURE) {
443   } else {
444     if (m_pFormFiller)
445       return m_pFormFiller->OnLButtonUp(pPageView, pAnnot, nFlags, point);
446   }
447
448   return FALSE;
449 }
450
451 FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonDblClk(CPDFSDK_PageView* pPageView,
452                                                 CPDFSDK_Annot* pAnnot,
453                                                 FX_DWORD nFlags,
454                                                 const CPDF_Point& point) {
455   ASSERT(pAnnot != NULL);
456   CFX_ByteString sSubType = pAnnot->GetSubType();
457
458   if (sSubType == BFFT_SIGNATURE) {
459   } else {
460     if (m_pFormFiller)
461       return m_pFormFiller->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
462   }
463
464   return FALSE;
465 }
466
467 FX_BOOL CPDFSDK_BFAnnotHandler::OnMouseMove(CPDFSDK_PageView* pPageView,
468                                             CPDFSDK_Annot* pAnnot,
469                                             FX_DWORD nFlags,
470                                             const CPDF_Point& point) {
471   ASSERT(pAnnot != NULL);
472   CFX_ByteString sSubType = pAnnot->GetSubType();
473
474   if (sSubType == BFFT_SIGNATURE) {
475   } else {
476     if (m_pFormFiller)
477       return m_pFormFiller->OnMouseMove(pPageView, pAnnot, nFlags, point);
478   }
479
480   return FALSE;
481 }
482
483 FX_BOOL CPDFSDK_BFAnnotHandler::OnMouseWheel(CPDFSDK_PageView* pPageView,
484                                              CPDFSDK_Annot* pAnnot,
485                                              FX_DWORD nFlags,
486                                              short zDelta,
487                                              const CPDF_Point& point) {
488   ASSERT(pAnnot != NULL);
489   CFX_ByteString sSubType = pAnnot->GetSubType();
490
491   if (sSubType == BFFT_SIGNATURE) {
492   } else {
493     if (m_pFormFiller)
494       return m_pFormFiller->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta,
495                                          point);
496   }
497
498   return FALSE;
499 }
500
501 FX_BOOL CPDFSDK_BFAnnotHandler::OnRButtonDown(CPDFSDK_PageView* pPageView,
502                                               CPDFSDK_Annot* pAnnot,
503                                               FX_DWORD nFlags,
504                                               const CPDF_Point& point) {
505   ASSERT(pAnnot != NULL);
506   CFX_ByteString sSubType = pAnnot->GetSubType();
507
508   if (sSubType == BFFT_SIGNATURE) {
509   } else {
510     if (m_pFormFiller)
511       return m_pFormFiller->OnRButtonDown(pPageView, pAnnot, nFlags, point);
512   }
513
514   return FALSE;
515 }
516 FX_BOOL CPDFSDK_BFAnnotHandler::OnRButtonUp(CPDFSDK_PageView* pPageView,
517                                             CPDFSDK_Annot* pAnnot,
518                                             FX_DWORD nFlags,
519                                             const CPDF_Point& point) {
520   ASSERT(pAnnot != NULL);
521   CFX_ByteString sSubType = pAnnot->GetSubType();
522
523   if (sSubType == BFFT_SIGNATURE) {
524   } else {
525     if (m_pFormFiller)
526       return m_pFormFiller->OnRButtonUp(pPageView, pAnnot, nFlags, point);
527   }
528
529   return FALSE;
530 }
531
532 FX_BOOL CPDFSDK_BFAnnotHandler::OnChar(CPDFSDK_Annot* pAnnot,
533                                        FX_DWORD nChar,
534                                        FX_DWORD nFlags) {
535   ASSERT(pAnnot != NULL);
536   CFX_ByteString sSubType = pAnnot->GetSubType();
537
538   if (sSubType == BFFT_SIGNATURE) {
539   } else {
540     if (m_pFormFiller)
541       return m_pFormFiller->OnChar(pAnnot, nChar, nFlags);
542   }
543
544   return FALSE;
545 }
546
547 FX_BOOL CPDFSDK_BFAnnotHandler::OnKeyDown(CPDFSDK_Annot* pAnnot,
548                                           int nKeyCode,
549                                           int nFlag) {
550   ASSERT(pAnnot != NULL);
551   CFX_ByteString sSubType = pAnnot->GetSubType();
552
553   if (sSubType == BFFT_SIGNATURE) {
554   } else {
555     if (m_pFormFiller)
556       return m_pFormFiller->OnKeyDown(pAnnot, nKeyCode, nFlag);
557   }
558
559   return FALSE;
560 }
561
562 FX_BOOL CPDFSDK_BFAnnotHandler::OnKeyUp(CPDFSDK_Annot* pAnnot,
563                                         int nKeyCode,
564                                         int nFlag) {
565   return FALSE;
566 }
567 void CPDFSDK_BFAnnotHandler::OnCreate(CPDFSDK_Annot* pAnnot) {
568   ASSERT(pAnnot != NULL);
569   CFX_ByteString sSubType = pAnnot->GetSubType();
570
571   if (sSubType == BFFT_SIGNATURE) {
572   } else {
573     if (m_pFormFiller)
574       m_pFormFiller->OnCreate(pAnnot);
575   }
576 }
577
578 void CPDFSDK_BFAnnotHandler::OnLoad(CPDFSDK_Annot* pAnnot) {
579   ASSERT(pAnnot != NULL);
580
581   CFX_ByteString sSubType = pAnnot->GetSubType();
582
583   if (sSubType == BFFT_SIGNATURE) {
584   } else {
585     CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
586     if (!pWidget->IsAppearanceValid())
587       pWidget->ResetAppearance(NULL, FALSE);
588
589     int nFieldType = pWidget->GetFieldType();
590     if (nFieldType == FIELDTYPE_TEXTFIELD || nFieldType == FIELDTYPE_COMBOBOX) {
591       FX_BOOL bFormated = FALSE;
592       CFX_WideString sValue = pWidget->OnFormat(bFormated);
593       if (bFormated && nFieldType == FIELDTYPE_COMBOBOX) {
594         pWidget->ResetAppearance(sValue.c_str(), FALSE);
595       }
596     }
597
598     if (m_pFormFiller)
599       m_pFormFiller->OnLoad(pAnnot);
600   }
601 }
602
603 FX_BOOL CPDFSDK_BFAnnotHandler::OnSetFocus(CPDFSDK_Annot* pAnnot,
604                                            FX_DWORD nFlag) {
605   ASSERT(pAnnot != NULL);
606   CFX_ByteString sSubType = pAnnot->GetSubType();
607
608   if (sSubType == BFFT_SIGNATURE) {
609   } else {
610     if (m_pFormFiller)
611       return m_pFormFiller->OnSetFocus(pAnnot, nFlag);
612   }
613
614   return TRUE;
615 }
616 FX_BOOL CPDFSDK_BFAnnotHandler::OnKillFocus(CPDFSDK_Annot* pAnnot,
617                                             FX_DWORD nFlag) {
618   ASSERT(pAnnot != NULL);
619   CFX_ByteString sSubType = pAnnot->GetSubType();
620
621   if (sSubType == BFFT_SIGNATURE) {
622   } else {
623     if (m_pFormFiller)
624       return m_pFormFiller->OnKillFocus(pAnnot, nFlag);
625   }
626
627   return TRUE;
628 }
629
630 CPDF_Rect CPDFSDK_BFAnnotHandler::GetViewBBox(CPDFSDK_PageView* pPageView,
631                                               CPDFSDK_Annot* pAnnot) {
632   ASSERT(pAnnot != NULL);
633   CFX_ByteString sSubType = pAnnot->GetSubType();
634
635   if (sSubType == BFFT_SIGNATURE) {
636   } else {
637     if (m_pFormFiller)
638       return m_pFormFiller->GetViewBBox(pPageView, pAnnot);
639   }
640
641   return CPDF_Rect(0, 0, 0, 0);
642 }
643
644 FX_BOOL CPDFSDK_BFAnnotHandler::HitTest(CPDFSDK_PageView* pPageView,
645                                         CPDFSDK_Annot* pAnnot,
646                                         const CPDF_Point& point) {
647   ASSERT(pPageView);
648   ASSERT(pAnnot);
649
650   CPDF_Rect rect = GetViewBBox(pPageView, pAnnot);
651   return rect.Contains(point.x, point.y);
652 }
653
654 CPDFSDK_AnnotIterator::CPDFSDK_AnnotIterator(CPDFSDK_PageView* pPageView,
655                                              bool bReverse)
656     : m_bReverse(bReverse), m_pos(0) {
657   const std::vector<CPDFSDK_Annot*>& annots = pPageView->GetAnnotList();
658   m_iteratorAnnotList.insert(m_iteratorAnnotList.begin(), annots.rbegin(),
659                              annots.rend());
660   std::stable_sort(m_iteratorAnnotList.begin(), m_iteratorAnnotList.end(),
661                    [](CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) {
662                      return p1->GetLayoutOrder() < p2->GetLayoutOrder();
663                    });
664
665   CPDFSDK_Annot* pTopMostAnnot = pPageView->GetFocusAnnot();
666   if (!pTopMostAnnot)
667     return;
668
669   auto it = std::find(m_iteratorAnnotList.begin(), m_iteratorAnnotList.end(),
670                       pTopMostAnnot);
671   if (it != m_iteratorAnnotList.end()) {
672     CPDFSDK_Annot* pReaderAnnot = *it;
673     m_iteratorAnnotList.erase(it);
674     m_iteratorAnnotList.insert(m_iteratorAnnotList.begin(), pReaderAnnot);
675   }
676 }
677
678 CPDFSDK_AnnotIterator::~CPDFSDK_AnnotIterator() {
679 }
680
681 CPDFSDK_Annot* CPDFSDK_AnnotIterator::NextAnnot() {
682   if (m_pos < m_iteratorAnnotList.size())
683     return m_iteratorAnnotList[m_pos++];
684   return nullptr;
685 }
686
687 CPDFSDK_Annot* CPDFSDK_AnnotIterator::PrevAnnot() {
688   if (m_pos < m_iteratorAnnotList.size())
689     return m_iteratorAnnotList[m_iteratorAnnotList.size() - ++m_pos];
690   return nullptr;
691 }
692
693 CPDFSDK_Annot* CPDFSDK_AnnotIterator::Next() {
694   return m_bReverse ? PrevAnnot() : NextAnnot();
695 }