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