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