Cleanup: Remove CFX_MapPtrToPtr from fpdfppo.cpp.
[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     return bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
349 }
350
351 FX_BOOL CPDFSDK_BFAnnotHandler::CanAnswer(CPDFSDK_Annot* pAnnot)
352 {
353         ASSERT(pAnnot);
354         ASSERT(pAnnot->GetType() == "Widget");
355         CFX_ByteString sSubType = pAnnot->GetSubType();
356
357         if (sSubType == BFFT_SIGNATURE)
358         {
359         }
360         else
361         {
362                 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
363                 if (!pWidget->IsVisible()) return FALSE;
364
365                 int nFieldFlags = pWidget->GetFieldFlags();
366                 if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY) return FALSE;
367                 if (pWidget->GetFieldType() == FIELDTYPE_PUSHBUTTON)
368                         return TRUE;
369                 else
370                 {
371                         CPDF_Page* pPage = pWidget->GetPDFPage();
372                         ASSERT(pPage != NULL);
373
374                         CPDF_Document* pDocument = pPage->m_pDocument;
375                         ASSERT(pDocument != NULL);
376
377                         FX_DWORD dwPermissions = pDocument->GetUserPermissions();
378                         return (dwPermissions&FPDFPERM_FILL_FORM) ||
379                                 (dwPermissions&FPDFPERM_ANNOT_FORM) ||
380                         (dwPermissions&FPDFPERM_ANNOT_FORM);
381                 }
382         }
383
384         return FALSE;
385 }
386
387 CPDFSDK_Annot*          CPDFSDK_BFAnnotHandler::NewAnnot(CPDF_Annot* pAnnot, CPDFSDK_PageView* pPage)
388 {
389         ASSERT(pPage != NULL);
390         pPage->GetPDFDocument();
391
392         CPDFSDK_Document* pSDKDoc  = m_pApp->GetCurrentDoc();
393         ASSERT(pSDKDoc);
394         CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pSDKDoc->GetInterForm();
395         ASSERT(pInterForm != NULL);
396
397         CPDFSDK_Widget* pWidget = NULL;
398         if (CPDF_FormControl* pCtrl = CPDFSDK_Widget::GetFormControl(pInterForm->GetInterForm(), pAnnot->GetAnnotDict()))
399         {
400                 pWidget = new CPDFSDK_Widget(pAnnot, pPage, pInterForm);
401                 pInterForm->AddMap(pCtrl, pWidget);
402                 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
403                 if(pPDFInterForm && pPDFInterForm->NeedConstructAP())
404                         pWidget->ResetAppearance(NULL,FALSE);
405         }
406
407         return pWidget;
408 }
409
410 void CPDFSDK_BFAnnotHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot)
411 {
412         ASSERT(pAnnot != NULL);
413
414         if (m_pFormFiller)
415                 m_pFormFiller->OnDelete(pAnnot);
416
417         CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
418         CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm();
419         ASSERT(pInterForm != NULL);
420
421         CPDF_FormControl* pCtrol = pWidget->GetFormControl();
422         pInterForm->RemoveMap(pCtrol);
423
424
425         delete pWidget;
426 }
427
428
429 void CPDFSDK_BFAnnotHandler::OnDraw(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,  FX_DWORD dwFlags)
430 {
431         ASSERT(pAnnot != NULL);
432         CFX_ByteString sSubType = pAnnot->GetSubType();
433
434         if (sSubType == BFFT_SIGNATURE)
435         {
436                 pAnnot->DrawAppearance(pDevice, pUser2Device, CPDF_Annot::Normal, NULL);
437         }
438         else
439         {
440                 if (m_pFormFiller)
441                 {
442                         m_pFormFiller->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
443                 }
444         }
445 }
446
447 void CPDFSDK_BFAnnotHandler::OnMouseEnter(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
448 {
449         ASSERT(pAnnot != NULL);
450         CFX_ByteString sSubType = pAnnot->GetSubType();
451
452         if (sSubType == BFFT_SIGNATURE)
453         {
454         }
455         else
456         {
457                 if (m_pFormFiller)
458                          m_pFormFiller->OnMouseEnter(pPageView, pAnnot, nFlag);
459         }
460
461
462 }
463 void CPDFSDK_BFAnnotHandler::OnMouseExit(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
464 {
465         ASSERT(pAnnot != NULL);
466         CFX_ByteString sSubType = pAnnot->GetSubType();
467
468         if (sSubType == BFFT_SIGNATURE)
469         {
470         }
471         else
472         {
473                 if (m_pFormFiller)
474                          m_pFormFiller->OnMouseExit(pPageView, pAnnot, nFlag);
475         }
476
477 }
478 FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonDown(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
479 {
480         ASSERT(pAnnot != NULL);
481         CFX_ByteString sSubType = pAnnot->GetSubType();
482
483         if (sSubType == BFFT_SIGNATURE)
484         {
485         }
486         else
487         {
488                 if (m_pFormFiller)
489                         return m_pFormFiller->OnLButtonDown(pPageView, pAnnot, nFlags, point);
490         }
491
492         return FALSE;
493 }
494
495 FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonUp(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
496 {
497         ASSERT(pAnnot != NULL);
498         CFX_ByteString sSubType = pAnnot->GetSubType();
499
500         if (sSubType == BFFT_SIGNATURE)
501         {
502         }
503         else
504         {
505                 if (m_pFormFiller)
506                         return m_pFormFiller->OnLButtonUp(pPageView, pAnnot, nFlags, point);
507         }
508
509         return FALSE;
510 }
511
512 FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonDblClk(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
513 {
514         ASSERT(pAnnot != NULL);
515         CFX_ByteString sSubType = pAnnot->GetSubType();
516
517         if (sSubType == BFFT_SIGNATURE)
518         {
519         }
520         else
521         {
522                 if (m_pFormFiller)
523                         return m_pFormFiller->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
524         }
525
526         return FALSE;
527 }
528
529 FX_BOOL CPDFSDK_BFAnnotHandler::OnMouseMove(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
530 {
531         ASSERT(pAnnot != NULL);
532         CFX_ByteString sSubType = pAnnot->GetSubType();
533
534         if (sSubType == BFFT_SIGNATURE)
535         {
536         }
537         else
538         {
539                 if (m_pFormFiller)
540                         return m_pFormFiller->OnMouseMove(pPageView, pAnnot, nFlags, point);
541         }
542
543         return FALSE;
544
545 }
546
547
548 FX_BOOL CPDFSDK_BFAnnotHandler::OnMouseWheel(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, short zDelta, const CPDF_Point& point)
549 {
550         ASSERT(pAnnot != NULL);
551         CFX_ByteString sSubType = pAnnot->GetSubType();
552
553         if (sSubType == BFFT_SIGNATURE)
554         {
555         }
556         else
557         {
558                 if (m_pFormFiller)
559                         return m_pFormFiller->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta,point);
560         }
561
562         return FALSE;
563 }
564
565 FX_BOOL CPDFSDK_BFAnnotHandler::OnRButtonDown(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
566 {
567         ASSERT(pAnnot != NULL);
568         CFX_ByteString sSubType = pAnnot->GetSubType();
569
570         if (sSubType == BFFT_SIGNATURE)
571         {
572         }
573         else
574         {
575                 if (m_pFormFiller)
576                         return m_pFormFiller->OnRButtonDown(pPageView, pAnnot, nFlags, point);
577         }
578
579         return FALSE;
580 }
581 FX_BOOL CPDFSDK_BFAnnotHandler::OnRButtonUp(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
582 {
583         ASSERT(pAnnot != NULL);
584         CFX_ByteString sSubType = pAnnot->GetSubType();
585
586         if (sSubType == BFFT_SIGNATURE)
587         {
588         }
589         else
590         {
591                 if (m_pFormFiller)
592                         return m_pFormFiller->OnRButtonUp(pPageView, pAnnot, nFlags, point);
593         }
594
595         return FALSE;
596 }
597
598 FX_BOOL CPDFSDK_BFAnnotHandler::OnChar(CPDFSDK_Annot* pAnnot, FX_DWORD nChar, FX_DWORD nFlags)
599 {
600         ASSERT(pAnnot != NULL);
601         CFX_ByteString sSubType = pAnnot->GetSubType();
602
603         if (sSubType == BFFT_SIGNATURE)
604         {
605         }
606         else
607         {
608                 if (m_pFormFiller)
609                         return m_pFormFiller->OnChar(pAnnot,nChar, nFlags);
610         }
611
612         return FALSE;
613 }
614
615 FX_BOOL CPDFSDK_BFAnnotHandler::OnKeyDown(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
616 {
617         ASSERT(pAnnot != NULL);
618         CFX_ByteString sSubType = pAnnot->GetSubType();
619
620         if (sSubType == BFFT_SIGNATURE)
621         {
622         }
623         else
624         {
625                 if (m_pFormFiller)
626                         return m_pFormFiller->OnKeyDown(pAnnot,nKeyCode, nFlag);
627         }
628
629         return FALSE;
630 }
631
632 FX_BOOL CPDFSDK_BFAnnotHandler::OnKeyUp(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
633 {
634
635         return FALSE;
636 }
637 void    CPDFSDK_BFAnnotHandler::OnCreate(CPDFSDK_Annot* pAnnot)
638 {
639         ASSERT(pAnnot != NULL);
640         CFX_ByteString sSubType = pAnnot->GetSubType();
641
642         if (sSubType == BFFT_SIGNATURE)
643         {
644         }
645         else
646         {
647                 if (m_pFormFiller)
648                         m_pFormFiller->OnCreate(pAnnot);
649         }
650 }
651
652 void CPDFSDK_BFAnnotHandler::OnLoad(CPDFSDK_Annot* pAnnot)
653 {
654         ASSERT(pAnnot != NULL);
655
656         CFX_ByteString sSubType = pAnnot->GetSubType();
657
658         if (sSubType == BFFT_SIGNATURE)
659         {
660         }
661         else
662         {
663                 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
664                 if (!pWidget->IsAppearanceValid())
665                         pWidget->ResetAppearance(NULL, FALSE);
666
667                 int nFieldType = pWidget->GetFieldType();
668                 if (nFieldType == FIELDTYPE_TEXTFIELD || nFieldType == FIELDTYPE_COMBOBOX)
669                 {
670                         FX_BOOL bFormated = FALSE;
671                         CFX_WideString sValue = pWidget->OnFormat(bFormated);
672                         if (bFormated && nFieldType == FIELDTYPE_COMBOBOX)
673                         {
674                                 pWidget->ResetAppearance(sValue.c_str(), FALSE);
675                         }
676                 }
677
678                 if (m_pFormFiller)
679                         m_pFormFiller->OnLoad(pAnnot);
680         }
681 }
682
683 FX_BOOL CPDFSDK_BFAnnotHandler::OnSetFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
684 {
685         ASSERT(pAnnot != NULL);
686         CFX_ByteString sSubType = pAnnot->GetSubType();
687
688         if (sSubType == BFFT_SIGNATURE)
689         {
690         }
691         else
692         {
693                 if (m_pFormFiller)
694                         return m_pFormFiller->OnSetFocus(pAnnot,nFlag);
695         }
696
697         return TRUE;
698 }
699 FX_BOOL CPDFSDK_BFAnnotHandler::OnKillFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
700 {
701         ASSERT(pAnnot != NULL);
702         CFX_ByteString sSubType = pAnnot->GetSubType();
703
704         if (sSubType == BFFT_SIGNATURE)
705         {
706         }
707         else
708         {
709                 if (m_pFormFiller)
710                         return m_pFormFiller->OnKillFocus(pAnnot,nFlag);
711         }
712
713         return TRUE;
714 }
715
716 CPDF_Rect CPDFSDK_BFAnnotHandler::GetViewBBox(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot)
717 {
718         ASSERT(pAnnot != NULL);
719         CFX_ByteString sSubType = pAnnot->GetSubType();
720
721         if (sSubType == BFFT_SIGNATURE)
722         {
723         }
724         else
725         {
726                 if (m_pFormFiller)
727                         return m_pFormFiller->GetViewBBox(pPageView, pAnnot);
728
729         }
730
731         return CPDF_Rect(0,0,0,0);
732 }
733
734 FX_BOOL CPDFSDK_BFAnnotHandler::HitTest(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, const CPDF_Point& point)
735 {
736         ASSERT(pPageView);
737         ASSERT(pAnnot);
738
739         CPDF_Rect rect = GetViewBBox(pPageView, pAnnot);
740         return rect.Contains(point.x, point.y);
741 }
742
743 //CReader_AnnotIteratorEx
744
745 CPDFSDK_AnnotIterator::CPDFSDK_AnnotIterator(CPDFSDK_PageView * pPageView,FX_BOOL bReverse,
746                                                                                                  FX_BOOL bIgnoreTopmost/*=FALSE*/,
747                                                                                                  FX_BOOL bCircle/*=FALSE*/,
748                                                                                                  CFX_PtrArray *pList/*=NULL*/)
749 {
750         ASSERT(pPageView);
751         m_bReverse=bReverse;
752         m_bIgnoreTopmost= bIgnoreTopmost;
753         m_bCircle=bCircle;
754         m_pIteratorAnnotList.RemoveAll();
755         InitIteratorAnnotList(pPageView,pList);
756 }
757
758 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::NextAnnot (const CPDFSDK_Annot* pCurrent)
759 {
760
761         int index = -1;
762         int nCount = m_pIteratorAnnotList.GetSize();
763         if (pCurrent) {
764                 for(int i=0;i<nCount;i++){
765                         CPDFSDK_Annot * pReaderAnnot= (CPDFSDK_Annot *)m_pIteratorAnnotList.GetAt(i);
766                         if(pReaderAnnot ==pCurrent){
767                                 index=i;
768                                 break;
769                         }
770                 }
771         }
772         return NextAnnot(index);
773 }
774 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::PrevAnnot (const CPDFSDK_Annot*pCurrent)
775 {
776         int index = -1;
777         int nCount = m_pIteratorAnnotList.GetSize();
778         if(pCurrent){
779                 for(int i=0;i<nCount;i++){
780                         CPDFSDK_Annot * pReaderAnnot= (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
781                         if(pReaderAnnot ==pCurrent){
782                                 index=i;
783                                 break;
784                         }
785                 }
786         }
787         return PrevAnnot(index);
788 }
789 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::NextAnnot (int& index)
790 {
791
792         int nCount=m_pIteratorAnnotList.GetSize();
793     if(nCount<=0) index=-1;
794     else{
795                 if(index<0){
796                         index=0;
797                 }
798                 else{
799                         if(m_bCircle){
800                                 index=( index <nCount-1) ? (index+1) :0;
801                         }
802                         else{
803                                 index=( index <nCount-1) ? (index+1) :-1;
804                         }
805
806                 }
807         }
808         return (index <0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index);
809 }
810
811
812 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::PrevAnnot (int& index)
813 {
814
815         int nCount=m_pIteratorAnnotList.GetSize();
816     if(nCount<=0) index=-1;
817         else{
818                 if(index<0){
819                         index=nCount-1;
820                 }
821                 else{
822                         if(m_bCircle){
823                                 index = ( index >0) ? (index-1) :nCount-1;
824                         }
825                         else{
826                                 index = ( index >0) ? (index-1) :-1;
827                         }
828                 }
829         }
830         return (index <0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index);
831 }
832
833
834 CPDFSDK_Annot*CPDFSDK_AnnotIterator::Next(const CPDFSDK_Annot* pCurrent)
835 {
836
837         return (m_bReverse) ? PrevAnnot(pCurrent):NextAnnot(pCurrent);
838
839 }
840
841 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::Prev(const CPDFSDK_Annot* pCurrent)
842 {
843
844         return (m_bReverse) ? NextAnnot(pCurrent):PrevAnnot(pCurrent);
845 }
846
847 CPDFSDK_Annot*CPDFSDK_AnnotIterator::Next(int& index )
848 {
849
850         return (m_bReverse) ? PrevAnnot(index):NextAnnot(index);
851
852 }
853
854 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::Prev(int& index )
855 {
856
857         return (m_bReverse) ? NextAnnot(index):PrevAnnot(index);
858 }
859
860
861 void CPDFSDK_AnnotIterator::InsertSort(CFX_PtrArray &arrayList, AI_COMPARE pCompare)
862 {
863         for (int i = 1; i < arrayList.GetSize(); i++)
864         {
865                 if (pCompare((CPDFSDK_Annot*)(arrayList[i]) , (CPDFSDK_Annot*)(arrayList[i-1])) < 0)
866                 {
867                         int j = i-1;
868                         CPDFSDK_Annot* pTemp = (CPDFSDK_Annot*)arrayList[i];
869
870                         do
871                         {
872                                 arrayList[j + 1] = arrayList[j];
873                         } while (--j >= 0 && pCompare(pTemp, (CPDFSDK_Annot*)arrayList[j]) < 0);
874
875                         arrayList[j+1] = pTemp;
876                 }
877         }
878 }
879
880 int LyOrderCompare(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2)
881 {
882         if(p1->GetLayoutOrder() < p2->GetLayoutOrder())
883                 return -1;
884         else if (p1->GetLayoutOrder() == p2->GetLayoutOrder())
885                 return 0;
886         else
887                 return 1;
888 }
889
890 FX_BOOL CPDFSDK_AnnotIterator::InitIteratorAnnotList(CPDFSDK_PageView* pPageView,CFX_PtrArray * pAnnotList)
891 {
892         ASSERT(pPageView);
893
894         if(pAnnotList==NULL){
895                 pAnnotList=pPageView->GetAnnotList();
896         }
897
898         m_pIteratorAnnotList.RemoveAll();
899         if(!pAnnotList) return FALSE;
900
901         CPDFSDK_Annot * pTopMostAnnot= (m_bIgnoreTopmost) ? NULL : pPageView->GetFocusAnnot();
902
903
904         int nCount =pAnnotList->GetSize();
905
906         for(int i = nCount- 1 ;i >= 0;i--)
907         {
908                 CPDFSDK_Annot * pReaderAnnot= (CPDFSDK_Annot*)pAnnotList->GetAt(i);
909                 m_pIteratorAnnotList.Add(pReaderAnnot);
910         }
911
912         InsertSort(m_pIteratorAnnotList,&LyOrderCompare);
913
914         if(pTopMostAnnot)
915         {
916                 for(int i=0 ;i<nCount;i++)
917                 {
918                         CPDFSDK_Annot * pReaderAnnot = (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
919                         if(pReaderAnnot == pTopMostAnnot)
920                         {
921                                 m_pIteratorAnnotList.RemoveAt(i);
922                                 m_pIteratorAnnotList.InsertAt(0, pReaderAnnot);
923                                 break;
924                         }
925                 }
926         }
927
928         return TRUE;
929 }
930