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