Merge to XFA: 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/fpdfxfa/fpdfxfa_doc.h"
9 #include "../include/fpdfxfa/fpdfxfa_util.h"
10 #include "../include/fsdk_mgr.h"
11 #include "../include/formfiller/FFL_FormFiller.h"
12 #include "../include/fsdk_annothandler.h"
13
14
15 CPDFSDK_AnnotHandlerMgr::CPDFSDK_AnnotHandlerMgr(CPDFDoc_Environment* pApp)
16 {
17         m_pApp = pApp;
18
19         CPDFSDK_BFAnnotHandler* pHandler = new CPDFSDK_BFAnnotHandler(m_pApp);
20         pHandler->SetFormFiller(m_pApp->GetIFormFiller());
21         RegisterAnnotHandler(pHandler);
22
23         CPDFSDK_XFAAnnotHandler* pXFAAnnotHandler = new CPDFSDK_XFAAnnotHandler(m_pApp);
24         RegisterAnnotHandler(pXFAAnnotHandler);
25 }
26
27 CPDFSDK_AnnotHandlerMgr::~CPDFSDK_AnnotHandlerMgr()
28 {
29         for(int i=0; i<m_Handlers.GetSize(); i++)
30         {
31                 IPDFSDK_AnnotHandler* pHandler = m_Handlers.GetAt(i);
32                 delete pHandler;
33         }
34         m_Handlers.RemoveAll();
35         m_mapType2Handler.RemoveAll();
36 }
37
38 void    CPDFSDK_AnnotHandlerMgr::RegisterAnnotHandler(IPDFSDK_AnnotHandler* pAnnotHandler)
39 {
40         ASSERT(pAnnotHandler != NULL);
41         
42         ASSERT(GetAnnotHandler(pAnnotHandler->GetType()) == NULL);
43         
44         m_Handlers.Add(pAnnotHandler);
45         m_mapType2Handler.SetAt(pAnnotHandler->GetType(), (void*)pAnnotHandler);
46 }
47
48 void CPDFSDK_AnnotHandlerMgr::UnRegisterAnnotHandler(IPDFSDK_AnnotHandler* pAnnotHandler)
49 {
50         ASSERT(pAnnotHandler != NULL);
51         
52         m_mapType2Handler.RemoveKey(pAnnotHandler->GetType());
53         
54         for (int i=0, sz=m_Handlers.GetSize(); i<sz; i++)
55         {
56                 if (m_Handlers.GetAt(i) == pAnnotHandler)
57                 {
58                         m_Handlers.RemoveAt(i);
59                         break;
60                 }
61         }
62 }
63
64 CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(CPDF_Annot * pAnnot, CPDFSDK_PageView *pPageView)
65 {
66         ASSERT(pAnnot != NULL);
67         ASSERT(pPageView != NULL);
68         
69         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot->GetSubType()))
70         {
71                 return pAnnotHandler->NewAnnot(pAnnot, pPageView);
72         }
73         
74         return new CPDFSDK_BAAnnot(pAnnot, pPageView);
75 }
76
77 CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(IXFA_Widget* pAnnot, CPDFSDK_PageView* pPageView)
78 {
79         ASSERT(pAnnot != NULL);
80         ASSERT(pPageView != NULL);
81
82         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(FSDK_XFAWIDGET_TYPENAME))
83         {
84                 return pAnnotHandler->NewAnnot(pAnnot, pPageView);
85         }
86
87         return NULL;
88 }
89
90 void CPDFSDK_AnnotHandlerMgr::ReleaseAnnot(CPDFSDK_Annot* pAnnot)
91 {
92         ASSERT(pAnnot != NULL);
93         
94         pAnnot->GetPDFPage();
95         
96         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
97         {
98                 pAnnotHandler->OnRelease(pAnnot);
99                 pAnnotHandler->ReleaseAnnot(pAnnot);
100         }
101         else
102         {
103                 delete (CPDFSDK_Annot*)pAnnot;
104         }
105 }
106
107 void CPDFSDK_AnnotHandlerMgr::Annot_OnCreate(CPDFSDK_Annot* pAnnot)
108 {
109         ASSERT(pAnnot != NULL);
110         
111         CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
112         
113         CPDFSDK_DateTime curTime;
114         pPDFAnnot->GetAnnotDict()->SetAtString("M", curTime.ToPDFDateTimeString());
115         pPDFAnnot->GetAnnotDict()->SetAtNumber("F", 0);
116         
117         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
118         {
119                 pAnnotHandler->OnCreate(pAnnot);
120         }
121 }
122
123 void CPDFSDK_AnnotHandlerMgr::Annot_OnLoad(CPDFSDK_Annot* pAnnot)
124 {
125         ASSERT(pAnnot != NULL);
126         
127         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
128         {
129                 pAnnotHandler->OnLoad(pAnnot);
130         }
131 }
132
133 IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(CPDFSDK_Annot* pAnnot) const
134 {
135         ASSERT(pAnnot != NULL);
136         
137         CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
138         if (pPDFAnnot)
139                 return GetAnnotHandler(pPDFAnnot->GetSubType());
140         else if (pAnnot->GetXFAWidget())
141                 return GetAnnotHandler(FSDK_XFAWIDGET_TYPENAME);
142         return NULL;
143 }
144
145 IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(const CFX_ByteString& sType) const
146 {
147         void* pRet = NULL;
148         m_mapType2Handler.Lookup(sType, pRet);  
149         return (IPDFSDK_AnnotHandler*)pRet;
150 }
151
152 void CPDFSDK_AnnotHandlerMgr::Annot_OnDraw(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,FX_DWORD dwFlags)
153 {
154         ASSERT(pAnnot != NULL);
155         
156         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
157         {
158                 pAnnotHandler->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
159         }
160         else
161         {
162                 if (!pAnnot->IsXFAField())
163                         ((CPDFSDK_BAAnnot*)pAnnot)->DrawAppearance(pDevice, pUser2Device, CPDF_Annot::Normal, NULL);
164         }
165 }
166
167
168 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDown(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
169 {
170         ASSERT(pAnnot != NULL);
171         
172         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
173         {
174                 return pAnnotHandler->OnLButtonDown(pPageView, pAnnot, nFlags, point);
175         }
176         return FALSE;
177 }
178 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonUp(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
179  {
180         ASSERT(pAnnot != NULL);
181
182         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
183         {
184                 return pAnnotHandler->OnLButtonUp(pPageView, pAnnot, nFlags, point);
185         }
186         return FALSE;
187  }
188 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDblClk(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
189 {
190         ASSERT(pAnnot != NULL);
191         
192         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
193         {
194                 return pAnnotHandler->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
195         }
196         return FALSE;
197 }
198 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseMove(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
199 {
200         ASSERT(pAnnot != NULL);
201         
202         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
203         {
204                 return pAnnotHandler->OnMouseMove(pPageView, pAnnot, nFlags, point);
205         }
206         return FALSE;
207 }
208 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseWheel(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, short zDelta, const CPDF_Point& point)
209 {
210         ASSERT(pAnnot != NULL);
211         
212         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
213         {
214                 return pAnnotHandler->OnMouseWheel(pPageView, pAnnot,nFlags,zDelta, point);
215         }
216         return FALSE;
217 }
218 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonDown(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
219 {
220         ASSERT(pAnnot != NULL);
221         
222         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
223         {
224                 return pAnnotHandler->OnRButtonDown(pPageView, pAnnot, nFlags, point);
225         }
226         return FALSE;
227 }
228 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonUp(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
229 {
230         ASSERT(pAnnot != NULL);
231         
232         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
233         {
234                 return pAnnotHandler->OnRButtonUp(pPageView, pAnnot, nFlags, point);
235         }
236         return FALSE;
237 }
238
239 void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseEnter(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
240 {
241         ASSERT(pAnnot != NULL);
242         
243         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
244         {
245                 pAnnotHandler->OnMouseEnter(pPageView, pAnnot, nFlag);
246         }
247         return ;
248 }
249
250 void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseExit(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
251 {
252         ASSERT(pAnnot != NULL);
253         
254         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
255         {
256                 pAnnotHandler->OnMouseExit(pPageView, pAnnot, nFlag);
257         }
258         return;
259 }
260
261 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChar(CPDFSDK_Annot* pAnnot, FX_DWORD nChar, FX_DWORD nFlags)
262 {
263
264         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
265         {
266                 return pAnnotHandler->OnChar(pAnnot,nChar, nFlags);
267         }
268         return FALSE;
269
270 }
271
272 FX_BOOL                 CPDFSDK_AnnotHandlerMgr::Annot_OnKeyDown(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
273 {
274
275         if (!m_pApp->FFI_IsCTRLKeyDown(nFlag) && !m_pApp->FFI_IsALTKeyDown(nFlag))
276         {
277                 CPDFSDK_PageView* pPage = pAnnot->GetPageView();
278                 CPDFSDK_Annot* pFocusAnnot = pPage->GetFocusAnnot();
279                 if (pFocusAnnot && (nKeyCode == FWL_VKEY_Tab))
280                 {
281                         CPDFSDK_Annot* pNext = GetNextAnnot(pFocusAnnot, !m_pApp->FFI_IsSHIFTKeyDown(nFlag));
282
283                         if(pNext && pNext != pFocusAnnot)
284                         {
285                                 CPDFSDK_Document* pDocument = pPage->GetSDKDocument();
286                                 pDocument->SetFocusAnnot(pNext);
287                                 return TRUE;
288                         }
289                 }
290         }
291
292         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
293         {
294                 return pAnnotHandler->OnKeyDown(pAnnot,nKeyCode, nFlag);
295         }
296         return FALSE;
297 }
298 FX_BOOL                 CPDFSDK_AnnotHandlerMgr::Annot_OnKeyUp(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
299 {
300         return FALSE;
301 }
302
303 FX_BOOL                 CPDFSDK_AnnotHandlerMgr::Annot_OnSetFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
304 {
305         ASSERT(pAnnot != NULL);
306
307         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
308         {
309                 if (pAnnotHandler->OnSetFocus(pAnnot, nFlag))
310                 {
311                         CPDFSDK_PageView* pPage = pAnnot->GetPageView();
312                         ASSERT(pPage != NULL);
313
314                         pPage->GetSDKDocument();
315         //              pDocument->SetTopmostAnnot(pAnnot);
316
317                         return TRUE;
318                 }
319                 else
320                 {
321                         return FALSE;
322                 }
323         }
324         
325         return FALSE;
326 }
327
328 FX_BOOL                 CPDFSDK_AnnotHandlerMgr::Annot_OnKillFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
329 {
330         ASSERT(pAnnot != NULL);
331         
332         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
333         {
334                 if (pAnnotHandler->OnKillFocus(pAnnot, nFlag))
335                 {       
336                         return TRUE;
337                 }
338                 else
339                         return FALSE;
340         }
341         
342         return FALSE;
343 }
344
345 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChangeFocus(CPDFSDK_Annot* pSetAnnot,CPDFSDK_Annot* pKillAnnot)
346 {
347         FX_BOOL bXFA = (pSetAnnot && pSetAnnot->GetXFAWidget()) ||
348                 (pKillAnnot && pKillAnnot->GetXFAWidget());
349
350         if (bXFA)
351         {
352                 if (IPDFSDK_AnnotHandler* pXFAAnnotHandler = GetAnnotHandler(FSDK_XFAWIDGET_TYPENAME))
353                         return pXFAAnnotHandler->OnXFAChangedFocus(pKillAnnot, pSetAnnot);
354         }
355
356         return TRUE;
357 }
358
359 CPDF_Rect       CPDFSDK_AnnotHandlerMgr::Annot_OnGetViewBBox(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot)
360 {
361         ASSERT(pAnnot);
362         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
363         {
364                 return pAnnotHandler->GetViewBBox(pPageView, pAnnot);
365         }
366         return pAnnot->GetRect();
367 }
368
369 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnHitTest(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, const CPDF_Point& point)
370 {
371         ASSERT(pAnnot);
372         if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
373         {
374                 if(pAnnotHandler->CanAnswer(pAnnot))
375                         return pAnnotHandler->HitTest(pPageView, pAnnot, point);
376         }
377         return FALSE;
378 }
379
380 CPDFSDK_Annot*  CPDFSDK_AnnotHandlerMgr::GetNextAnnot(CPDFSDK_Annot* pSDKAnnot,FX_BOOL bNext)
381 {
382         CPDFSDK_PageView* pPageView =  pSDKAnnot->GetPageView();
383         CPDFXFA_Page* pPage = pPageView->GetPDFXFAPage();
384         if (pPage == NULL) 
385                 return NULL;
386         if (pPage->GetPDFPage()) { // for pdf annots. 
387                 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(), pSDKAnnot->GetType(), "");
388                 CPDFSDK_Annot* pNext = bNext ? 
389                 ai.GetNextAnnot(pSDKAnnot) : 
390                 ai.GetPrevAnnot(pSDKAnnot);
391                 return pNext;
392         }
393         // for xfa annots
394         IXFA_WidgetIterator* pWidgetIterator = pPage->GetXFAPageView()->CreateWidgetIterator(XFA_TRAVERSEWAY_Tranvalse, XFA_WIDGETFILTER_Visible | XFA_WIDGETFILTER_Viewable | XFA_WIDGETFILTER_Field);
395         if (pWidgetIterator == NULL)
396                 return  NULL;
397         if (pWidgetIterator->GetCurrentWidget() != pSDKAnnot->GetXFAWidget())   
398                 pWidgetIterator->SetCurrentWidget(pSDKAnnot->GetXFAWidget());
399         IXFA_Widget* hNextFocus = NULL;
400         hNextFocus = bNext?pWidgetIterator->MoveToNext():pWidgetIterator->MoveToPrevious();
401         if (hNextFocus == NULL && pSDKAnnot != NULL)
402                 hNextFocus = pWidgetIterator->MoveToFirst();
403
404         pWidgetIterator->Release();
405         return pPageView->GetAnnotByXFAWidget(hNextFocus);
406 }
407
408 FX_BOOL CPDFSDK_BFAnnotHandler::CanAnswer(CPDFSDK_Annot* pAnnot)
409 {
410         ASSERT(pAnnot);
411         ASSERT(pAnnot->GetType() == "Widget");
412         CFX_ByteString sSubType = pAnnot->GetSubType();
413         
414         if (sSubType == BFFT_SIGNATURE)
415         {
416         }
417         else
418         {
419                 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
420                 if (!pWidget->IsVisible()) return FALSE;
421
422                 int nFieldFlags = pWidget->GetFieldFlags();
423                 if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY) return FALSE;
424                 if (pWidget->GetFieldType() == FIELDTYPE_PUSHBUTTON)
425                         return TRUE;
426                 else
427                 {
428                         CPDF_Page* pPage = pWidget->GetPDFPage();
429                         ASSERT(pPage != NULL);
430                         
431                         CPDF_Document* pDocument = pPage->m_pDocument;
432                         ASSERT(pDocument != NULL);
433                         
434                         FX_DWORD dwPermissions = pDocument->GetUserPermissions();
435                         return (dwPermissions&FPDFPERM_FILL_FORM) || 
436                                 (dwPermissions&FPDFPERM_ANNOT_FORM) || 
437                         (dwPermissions&FPDFPERM_ANNOT_FORM);
438                 }
439         }
440
441         return FALSE;
442 }
443
444 CPDFSDK_Annot*          CPDFSDK_BFAnnotHandler::NewAnnot(CPDF_Annot* pAnnot, CPDFSDK_PageView* pPage)
445 {
446         ASSERT(pPage != NULL);
447         pPage->GetPDFDocument();
448         
449         CPDFSDK_Document* pSDKDoc  = m_pApp->GetCurrentDoc();
450         ASSERT(pSDKDoc);
451         CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pSDKDoc->GetInterForm();
452         ASSERT(pInterForm != NULL);
453         
454         CPDFSDK_Widget* pWidget = NULL;
455         if (CPDF_FormControl* pCtrl = CPDFSDK_Widget::GetFormControl(pInterForm->GetInterForm(), pAnnot->GetAnnotDict()))
456         {
457                 pWidget = new CPDFSDK_Widget(pAnnot, pPage, pInterForm);
458                 pInterForm->AddMap(pCtrl, pWidget);
459                 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
460                 if(pPDFInterForm && pPDFInterForm->NeedConstructAP())
461                         pWidget->ResetAppearance(NULL,FALSE);
462         }
463         
464         return pWidget;
465 }
466
467 CPDFSDK_Annot*          CPDFSDK_BFAnnotHandler::NewAnnot(IXFA_Widget* hWidget, CPDFSDK_PageView* pPage)
468 {
469         return NULL;
470 }
471
472 void CPDFSDK_BFAnnotHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot)
473 {
474         ASSERT(pAnnot != NULL);
475
476         if (m_pFormFiller)
477                 m_pFormFiller->OnDelete(pAnnot);
478         
479         CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
480         CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm();
481         ASSERT(pInterForm != NULL);
482         
483         CPDF_FormControl* pCtrol = pWidget->GetFormControl();
484         pInterForm->RemoveMap(pCtrol);
485         
486
487         delete pWidget;
488 }
489
490
491 void CPDFSDK_BFAnnotHandler::OnDraw(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,  FX_DWORD dwFlags)
492 {
493         ASSERT(pAnnot != NULL);
494         CFX_ByteString sSubType = pAnnot->GetSubType();
495         
496         if (sSubType == BFFT_SIGNATURE)
497         {
498                 ((CPDFSDK_BAAnnot*)pAnnot)->DrawAppearance(pDevice, pUser2Device, CPDF_Annot::Normal, NULL);
499         }
500         else
501         {
502                 if (m_pFormFiller)
503                 {
504                         m_pFormFiller->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
505                 }
506         }
507 }
508
509 void CPDFSDK_BFAnnotHandler::OnMouseEnter(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag) 
510 {
511         ASSERT(pAnnot != NULL);
512         CFX_ByteString sSubType = pAnnot->GetSubType();
513         
514         if (sSubType == BFFT_SIGNATURE)
515         {
516         }
517         else
518         {
519                 if (m_pFormFiller)
520                          m_pFormFiller->OnMouseEnter(pPageView, pAnnot, nFlag);
521         }
522         
523
524 }
525 void CPDFSDK_BFAnnotHandler::OnMouseExit(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag) 
526 {
527         ASSERT(pAnnot != NULL);
528         CFX_ByteString sSubType = pAnnot->GetSubType();
529         
530         if (sSubType == BFFT_SIGNATURE)
531         {
532         }
533         else
534         {
535                 if (m_pFormFiller)
536                          m_pFormFiller->OnMouseExit(pPageView, pAnnot, nFlag);
537         }
538         
539 }
540 FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonDown(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
541 {
542         ASSERT(pAnnot != NULL);
543         CFX_ByteString sSubType = pAnnot->GetSubType();
544         
545         if (sSubType == BFFT_SIGNATURE)
546         {
547         }
548         else
549         {
550                 if (m_pFormFiller)
551                         return m_pFormFiller->OnLButtonDown(pPageView, pAnnot, nFlags, point);
552         }
553         
554         return FALSE;
555 }
556
557 FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonUp(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
558 {
559         ASSERT(pAnnot != NULL);
560         CFX_ByteString sSubType = pAnnot->GetSubType();
561         
562         if (sSubType == BFFT_SIGNATURE)
563         {
564         }
565         else
566         {
567                 if (m_pFormFiller)
568                         return m_pFormFiller->OnLButtonUp(pPageView, pAnnot, nFlags, point);
569         }
570         
571         return FALSE;
572 }
573
574 FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonDblClk(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
575 {
576         ASSERT(pAnnot != NULL);
577         CFX_ByteString sSubType = pAnnot->GetSubType();
578         
579         if (sSubType == BFFT_SIGNATURE)
580         {
581         }
582         else
583         {
584                 if (m_pFormFiller)
585                         return m_pFormFiller->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
586         }
587         
588         return FALSE;
589 }
590
591 FX_BOOL CPDFSDK_BFAnnotHandler::OnMouseMove(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
592 {       
593         ASSERT(pAnnot != NULL);
594         CFX_ByteString sSubType = pAnnot->GetSubType();
595
596         if (sSubType == BFFT_SIGNATURE)
597         {
598         }
599         else
600         {
601                 if (m_pFormFiller)
602                         return m_pFormFiller->OnMouseMove(pPageView, pAnnot, nFlags, point);
603         }
604
605         return FALSE;
606
607 }
608
609
610 FX_BOOL CPDFSDK_BFAnnotHandler::OnMouseWheel(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, short zDelta, const CPDF_Point& point)
611 {
612         ASSERT(pAnnot != NULL);
613         CFX_ByteString sSubType = pAnnot->GetSubType();
614         
615         if (sSubType == BFFT_SIGNATURE)
616         {
617         }
618         else
619         {
620                 if (m_pFormFiller)
621                         return m_pFormFiller->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta,point);
622         }
623         
624         return FALSE;
625 }
626
627 FX_BOOL CPDFSDK_BFAnnotHandler::OnRButtonDown(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
628 {
629         ASSERT(pAnnot != NULL);
630         CFX_ByteString sSubType = pAnnot->GetSubType();
631         
632         if (sSubType == BFFT_SIGNATURE)
633         {
634         }
635         else
636         {
637                 if (m_pFormFiller)
638                         return m_pFormFiller->OnRButtonDown(pPageView, pAnnot, nFlags, point);
639         }
640         
641         return FALSE;
642 }
643 FX_BOOL CPDFSDK_BFAnnotHandler::OnRButtonUp(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
644 {
645         ASSERT(pAnnot != NULL);
646         CFX_ByteString sSubType = pAnnot->GetSubType();
647         
648         if (sSubType == BFFT_SIGNATURE)
649         {
650         }
651         else
652         {
653                 if (m_pFormFiller)
654                         return m_pFormFiller->OnRButtonUp(pPageView, pAnnot, nFlags, point);
655         }
656         
657         return FALSE;
658 }
659
660 FX_BOOL CPDFSDK_BFAnnotHandler::OnChar(CPDFSDK_Annot* pAnnot, FX_DWORD nChar, FX_DWORD nFlags)
661 {
662         ASSERT(pAnnot != NULL);
663         CFX_ByteString sSubType = pAnnot->GetSubType();
664         
665         if (sSubType == BFFT_SIGNATURE)
666         {
667         }
668         else
669         {
670                 if (m_pFormFiller)
671                         return m_pFormFiller->OnChar(pAnnot,nChar, nFlags);
672         }
673         
674         return FALSE;
675 }
676
677 FX_BOOL CPDFSDK_BFAnnotHandler::OnKeyDown(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
678 {
679         ASSERT(pAnnot != NULL);
680         CFX_ByteString sSubType = pAnnot->GetSubType();
681         
682         if (sSubType == BFFT_SIGNATURE)
683         {
684         }
685         else
686         {
687                 if (m_pFormFiller)
688                         return m_pFormFiller->OnKeyDown(pAnnot,nKeyCode, nFlag);
689         }
690         
691         return FALSE;
692 }
693
694 FX_BOOL CPDFSDK_BFAnnotHandler::OnKeyUp(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
695 {
696
697         return FALSE;
698 }
699 void    CPDFSDK_BFAnnotHandler::OnCreate(CPDFSDK_Annot* pAnnot) 
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                         m_pFormFiller->OnCreate(pAnnot);
711         }
712 }
713
714 void CPDFSDK_BFAnnotHandler::OnLoad(CPDFSDK_Annot* pAnnot)
715 {
716         ASSERT(pAnnot != NULL);
717         
718         CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
719         ASSERT(pPageView != NULL);
720
721         CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
722         ASSERT(pSDKDoc != NULL);
723
724         CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
725         ASSERT(pDoc != NULL);
726         
727         CFX_ByteString sSubType = pAnnot->GetSubType();
728         
729         if (sSubType == BFFT_SIGNATURE)
730         {
731         }
732         else
733         {
734                 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
735                 if (!pWidget->IsAppearanceValid())
736                         pWidget->ResetAppearance(NULL, FALSE);
737
738                 int nFieldType = pWidget->GetFieldType();
739                 if (nFieldType == FIELDTYPE_TEXTFIELD || nFieldType == FIELDTYPE_COMBOBOX)
740                 {
741                         FX_BOOL bFormated = FALSE;
742                         CFX_WideString sValue = pWidget->OnFormat(0, bFormated);
743                         if (bFormated && nFieldType == FIELDTYPE_COMBOBOX)
744                         {
745                                 pWidget->ResetAppearance(sValue.c_str(), FALSE);
746                         }
747                 }
748                 
749                 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) 
750                 {
751                         if (!pWidget->IsAppearanceValid() && !pWidget->GetValue().IsEmpty())
752                                 pWidget->ResetAppearance(FALSE);
753                 }
754                 
755
756                 if (m_pFormFiller)
757                         m_pFormFiller->OnLoad(pAnnot);
758         }
759 }
760
761 FX_BOOL CPDFSDK_BFAnnotHandler::OnSetFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
762 {
763         ASSERT(pAnnot != NULL);
764         CFX_ByteString sSubType = pAnnot->GetSubType();
765         
766         if (sSubType == BFFT_SIGNATURE)
767         {
768         }
769         else
770         {
771                 if (m_pFormFiller)
772                         return m_pFormFiller->OnSetFocus(pAnnot,nFlag);
773         }
774         
775         return TRUE;
776 }
777 FX_BOOL CPDFSDK_BFAnnotHandler::OnKillFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
778 {
779         ASSERT(pAnnot != NULL);
780         CFX_ByteString sSubType = pAnnot->GetSubType();
781         
782         if (sSubType == BFFT_SIGNATURE)
783         {
784         }
785         else
786         {
787                 if (m_pFormFiller)
788                         return m_pFormFiller->OnKillFocus(pAnnot,nFlag);
789         }
790         
791         return TRUE;
792 }
793
794 CPDF_Rect CPDFSDK_BFAnnotHandler::GetViewBBox(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot)
795 {
796         ASSERT(pAnnot != NULL);
797         CFX_ByteString sSubType = pAnnot->GetSubType();
798         
799         if (sSubType == BFFT_SIGNATURE)
800         {
801         }
802         else
803         {
804                 if (m_pFormFiller)
805                         return m_pFormFiller->GetViewBBox(pPageView, pAnnot);
806
807         }
808         
809         return CPDF_Rect(0,0,0,0);
810 }
811
812 FX_BOOL CPDFSDK_BFAnnotHandler::HitTest(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, const CPDF_Point& point)
813 {
814         ASSERT(pPageView);
815         ASSERT(pAnnot);
816
817         CPDF_Rect rect = GetViewBBox(pPageView, pAnnot);
818         return rect.Contains(point.x, point.y);
819 }
820
821 //CPDFSDK_XFAAnnotHandler
822
823 #define FWL_WGTHITTEST_Unknown                  0
824 #define FWL_WGTHITTEST_Client                   1 //arrow
825 #define FWL_WGTHITTEST_Titlebar                 11 //caption
826 #define FWL_WGTHITTEST_HScrollBar               15
827 #define FWL_WGTHITTEST_VScrollBar               16
828 #define FWL_WGTHITTEST_Border                   17
829 #define FWL_WGTHITTEST_Edit                             19
830 #define FWL_WGTHITTEST_HyperLink        20
831
832 CPDFSDK_XFAAnnotHandler::CPDFSDK_XFAAnnotHandler(CPDFDoc_Environment* pApp) :
833         m_pApp(pApp)
834 {
835
836 }
837
838 CPDFSDK_Annot* CPDFSDK_XFAAnnotHandler::NewAnnot(IXFA_Widget* pAnnot, CPDFSDK_PageView* pPage)
839 {
840         ASSERT(pPage != NULL);
841         ASSERT(pAnnot != NULL);
842
843         CPDFSDK_Document* pSDKDoc  = m_pApp->GetCurrentDoc();
844         ASSERT(pSDKDoc);
845         CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pSDKDoc->GetInterForm();
846         ASSERT(pInterForm != NULL);
847
848         CPDFSDK_XFAWidget* pWidget = NULL;
849         pWidget = new CPDFSDK_XFAWidget(pAnnot, pPage, pInterForm);
850         ASSERT(pWidget != NULL);
851         pInterForm->AddXFAMap(pAnnot, pWidget);
852
853         return pWidget;
854 }
855
856 FX_BOOL CPDFSDK_XFAAnnotHandler::CanAnswer(CPDFSDK_Annot* pAnnot)
857 {
858         ASSERT(pAnnot != NULL);
859
860         return pAnnot->GetXFAWidget() != NULL;
861 }
862
863 void CPDFSDK_XFAAnnotHandler::OnDraw(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device, FX_DWORD dwFlags)
864 {
865         ASSERT(pPageView != NULL);
866         ASSERT(pAnnot != NULL);
867
868         CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
869         ASSERT(pSDKDoc != NULL);
870
871         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
872         ASSERT(pWidgetHandler != NULL);
873
874         CFX_Graphics gs;
875         gs.Create(pDevice);
876
877         CFX_Matrix mt;
878         mt = *(CFX_Matrix*)pUser2Device;
879
880         IXFA_Widget* hWidget = pAnnot->GetXFAWidget();
881         ASSERT(hWidget != NULL);
882
883         FX_BOOL bIsHighlight = FALSE;
884         if (pSDKDoc->GetFocusAnnot() != pAnnot)
885                 bIsHighlight = TRUE;
886
887         pWidgetHandler->RenderWidget(pAnnot->GetXFAWidget(), &gs, &mt, bIsHighlight);
888
889         // to do highlight and shadow
890
891 }
892
893 void CPDFSDK_XFAAnnotHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot)
894 {
895         ASSERT(pAnnot != NULL);
896
897         CPDFSDK_XFAWidget* pWidget = (CPDFSDK_XFAWidget*)pAnnot;
898         CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm();
899         ASSERT(pInterForm != NULL);
900
901         pInterForm->RemoveXFAMap(pWidget->GetXFAWidget());
902
903         delete pWidget;
904 }
905
906 CPDF_Rect CPDFSDK_XFAAnnotHandler::GetViewBBox(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot)
907 {
908         ASSERT(pAnnot != NULL);
909
910         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
911         ASSERT(pWidgetHandler != NULL);
912
913         XFA_ELEMENT eType = pWidgetHandler->GetDataAcc(pAnnot->GetXFAWidget())->GetUIType();
914         CFX_RectF rcBBox;
915         if (eType == XFA_ELEMENT_Signature)
916                 pWidgetHandler->GetBBox (pAnnot->GetXFAWidget(), rcBBox, XFA_WIDGETSTATUS_Visible, TRUE);
917         else 
918                 pWidgetHandler->GetBBox (pAnnot->GetXFAWidget(), rcBBox, 0);
919
920         CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width, rcBBox.top+rcBBox.height);
921         rcWidget.left -= 1.0f;
922         rcWidget.right += 1.0f;
923         rcWidget.bottom -= 1.0f;
924         rcWidget.top += 1.0f;
925
926         return rcWidget;
927 }
928
929 FX_BOOL CPDFSDK_XFAAnnotHandler::HitTest(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, const CPDF_Point& point)
930 {
931         if (!pPageView || !pAnnot)
932                 return FALSE;
933
934         CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
935         if (!pSDKDoc)
936                 return FALSE;
937
938         CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
939         if (!pDoc)
940                 return FALSE;
941
942         IXFA_DocView* pDocView = pDoc->GetXFADocView();
943         if (!pDocView)
944                 return FALSE;
945
946         IXFA_WidgetHandler* pWidgetHandler = pDocView->GetWidgetHandler();
947         if (!pWidgetHandler)
948                 return FALSE;
949
950         FX_DWORD dwHitTest = pWidgetHandler->OnHitTest(pAnnot->GetXFAWidget(), point.x, point.y);
951         return (dwHitTest != FWL_WGTHITTEST_Unknown);
952 }
953
954 void CPDFSDK_XFAAnnotHandler::OnMouseEnter(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
955 {
956         if (!pPageView || !pAnnot) 
957                 return;
958         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
959         ASSERT(pWidgetHandler != NULL);
960
961         pWidgetHandler->OnMouseEnter(pAnnot->GetXFAWidget());
962 }
963
964 void CPDFSDK_XFAAnnotHandler::OnMouseExit(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
965 {
966         if (!pPageView || !pAnnot)
967                 return;
968
969         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
970         ASSERT(pWidgetHandler != NULL);
971
972         pWidgetHandler->OnMouseExit(pAnnot->GetXFAWidget());
973 }
974
975 FX_BOOL CPDFSDK_XFAAnnotHandler::OnLButtonDown(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
976 {
977         if (!pPageView || !pAnnot)
978                 return FALSE;
979
980         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
981         ASSERT(pWidgetHandler != NULL);
982
983         FX_BOOL bRet = FALSE;
984         bRet = pWidgetHandler->OnLButtonDown(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
985
986         return bRet;
987 }
988
989 FX_BOOL CPDFSDK_XFAAnnotHandler::OnLButtonUp(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
990 {
991         if (!pPageView || !pAnnot)
992                 return FALSE;
993
994         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
995         ASSERT(pWidgetHandler != NULL);
996
997         FX_BOOL bRet = FALSE;
998         bRet = pWidgetHandler->OnLButtonUp(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
999
1000         return bRet;
1001 }
1002
1003 FX_BOOL CPDFSDK_XFAAnnotHandler::OnLButtonDblClk(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
1004 {
1005         if (!pPageView || !pAnnot)
1006                 return FALSE;
1007
1008         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1009         ASSERT(pWidgetHandler != NULL);
1010
1011         FX_BOOL bRet = FALSE;
1012         bRet = pWidgetHandler->OnLButtonDblClk(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
1013
1014         return bRet;
1015 }
1016
1017 FX_BOOL CPDFSDK_XFAAnnotHandler::OnMouseMove(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
1018 {
1019         if (!pPageView || !pAnnot)
1020                 return FALSE;
1021
1022         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1023         ASSERT(pWidgetHandler != NULL);
1024
1025         FX_BOOL bRet = FALSE;
1026         bRet = pWidgetHandler->OnMouseMove(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
1027
1028         return bRet;
1029 }
1030
1031 FX_BOOL CPDFSDK_XFAAnnotHandler::OnMouseWheel(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, short zDelta, const CPDF_Point& point)
1032 {
1033         if (!pPageView || !pAnnot)
1034                 return FALSE;
1035
1036         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1037         ASSERT(pWidgetHandler != NULL);
1038
1039         FX_BOOL bRet = FALSE;
1040         bRet = pWidgetHandler->OnMouseWheel(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), zDelta, point.x, point.y);
1041
1042         return bRet;
1043 }
1044
1045 FX_BOOL CPDFSDK_XFAAnnotHandler::OnRButtonDown(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
1046 {
1047         if (!pPageView || !pAnnot)
1048                 return FALSE;
1049
1050         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1051         ASSERT(pWidgetHandler != NULL);
1052
1053         FX_BOOL bRet = FALSE;
1054         bRet = pWidgetHandler->OnRButtonDown(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
1055
1056         return bRet;
1057 }
1058
1059 FX_BOOL CPDFSDK_XFAAnnotHandler::OnRButtonUp(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
1060 {
1061         if (!pPageView || !pAnnot)
1062                 return FALSE;
1063
1064         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1065         ASSERT(pWidgetHandler != NULL);
1066
1067         FX_BOOL bRet = FALSE;
1068         bRet = pWidgetHandler->OnRButtonUp(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
1069
1070         return bRet;
1071 }
1072
1073 FX_BOOL CPDFSDK_XFAAnnotHandler::OnRButtonDblClk(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
1074 {
1075         if (!pPageView || !pAnnot)
1076                 return FALSE;
1077
1078         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1079         ASSERT(pWidgetHandler != NULL);
1080
1081         FX_BOOL bRet = FALSE;
1082         bRet = pWidgetHandler->OnRButtonDblClk(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
1083
1084         return bRet;
1085 }
1086
1087 FX_BOOL CPDFSDK_XFAAnnotHandler::OnChar(CPDFSDK_Annot* pAnnot, FX_DWORD nChar, FX_DWORD nFlags)
1088 {
1089         if (!pAnnot)
1090                 return FALSE;
1091
1092         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1093         ASSERT(pWidgetHandler != NULL);
1094
1095         FX_BOOL bRet = FALSE;
1096         bRet = pWidgetHandler->OnChar(pAnnot->GetXFAWidget(), nChar, GetFWLFlags(nFlags));
1097
1098         return bRet;
1099 }
1100
1101 FX_BOOL CPDFSDK_XFAAnnotHandler::OnKeyDown(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
1102 {
1103         if (!pAnnot)
1104                 return FALSE;
1105
1106         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1107         ASSERT(pWidgetHandler != NULL);
1108
1109         FX_BOOL bRet = FALSE;
1110         bRet = pWidgetHandler->OnKeyDown(pAnnot->GetXFAWidget(), nKeyCode, GetFWLFlags(nFlag));
1111
1112         return bRet;
1113 }
1114
1115 FX_BOOL CPDFSDK_XFAAnnotHandler::OnKeyUp(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
1116 {
1117         if (!pAnnot)
1118                 return FALSE;
1119
1120         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1121         ASSERT(pWidgetHandler != NULL);
1122
1123         FX_BOOL bRet = FALSE;
1124         bRet = pWidgetHandler->OnKeyUp(pAnnot->GetXFAWidget(), nKeyCode, GetFWLFlags(nFlag));
1125
1126         return bRet;
1127 }
1128
1129 FX_BOOL CPDFSDK_XFAAnnotHandler::OnSetFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
1130 {
1131         return TRUE;
1132 }
1133
1134 FX_BOOL CPDFSDK_XFAAnnotHandler::OnKillFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
1135 {
1136         return TRUE;
1137 }
1138
1139 FX_BOOL CPDFSDK_XFAAnnotHandler::OnXFAChangedFocus(CPDFSDK_Annot* pOldAnnot, CPDFSDK_Annot* pNewAnnot)
1140 {
1141         IXFA_WidgetHandler* pWidgetHandler = NULL;
1142
1143         if (pOldAnnot)
1144                 pWidgetHandler = GetXFAWidgetHandler(pOldAnnot);
1145         else if (pNewAnnot)
1146                 pWidgetHandler = GetXFAWidgetHandler(pNewAnnot);
1147
1148         if (pWidgetHandler)
1149         {
1150                 FX_BOOL bRet = TRUE;
1151                 IXFA_Widget* hWidget = pNewAnnot ? pNewAnnot->GetXFAWidget() : NULL;
1152                 if (hWidget)
1153                 {
1154                         IXFA_PageView* pXFAPageView = pWidgetHandler->GetPageView(hWidget);
1155                         if (pXFAPageView)
1156                         {
1157                                 bRet = pXFAPageView->GetDocView()->SetFocus(hWidget);
1158                                 if (pXFAPageView->GetDocView()->GetFocusWidget() == hWidget)
1159                                         bRet = TRUE;
1160                         }
1161                 }
1162                 return bRet;
1163         }
1164
1165         return TRUE;
1166 }
1167
1168 IXFA_WidgetHandler* CPDFSDK_XFAAnnotHandler::GetXFAWidgetHandler(CPDFSDK_Annot* pAnnot)
1169 {
1170         if (!pAnnot)
1171                 return NULL;
1172
1173         CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
1174         if (!pPageView)
1175                 return NULL;
1176
1177         CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
1178         if (!pSDKDoc)
1179                 return NULL;
1180
1181         CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
1182         if (!pDoc)
1183                 return NULL;
1184
1185         IXFA_DocView* pDocView = pDoc->GetXFADocView();
1186         if (!pDocView)
1187                 return NULL;
1188
1189         return pDocView->GetWidgetHandler();
1190 }
1191
1192 #define FWL_KEYFLAG_Ctrl                                        (1 << 0)
1193 #define FWL_KEYFLAG_Alt                                         (1 << 1)
1194 #define FWL_KEYFLAG_Shift                                       (1 << 2)
1195 #define FWL_KEYFLAG_LButton                                     (1 << 3)
1196 #define FWL_KEYFLAG_RButton                                     (1 << 4)
1197 #define FWL_KEYFLAG_MButton                                     (1 << 5)
1198
1199 FX_DWORD CPDFSDK_XFAAnnotHandler::GetFWLFlags(FX_DWORD dwFlag)
1200 {
1201         FX_DWORD dwFWLFlag = 0;
1202
1203         if (dwFlag & FWL_EVENTFLAG_ControlKey)
1204                 dwFWLFlag |= FWL_KEYFLAG_Ctrl;
1205         if (dwFlag & FWL_EVENTFLAG_LeftButtonDown)
1206                 dwFWLFlag |= FWL_KEYFLAG_LButton;
1207         if (dwFlag & FWL_EVENTFLAG_MiddleButtonDown)
1208                 dwFWLFlag |= FWL_KEYFLAG_MButton;
1209         if (dwFlag & FWL_EVENTFLAG_RightButtonDown)
1210                 dwFWLFlag |= FWL_KEYFLAG_RButton;
1211         if (dwFlag & FWL_EVENTFLAG_ShiftKey)
1212                 dwFWLFlag |= FWL_KEYFLAG_Shift;
1213         if (dwFlag & FWL_EVENTFLAG_AltKey)
1214                 dwFWLFlag |= FWL_KEYFLAG_Alt;
1215
1216         return dwFWLFlag;
1217 }
1218
1219 //CReader_AnnotIteratorEx
1220
1221 CPDFSDK_AnnotIterator::CPDFSDK_AnnotIterator(CPDFSDK_PageView * pPageView,FX_BOOL bReverse,
1222                                                                                                  FX_BOOL bIgnoreTopmost/*=FALSE*/,
1223                                                                                                  FX_BOOL bCircle/*=FALSE*/,
1224                                                                                                  CFX_PtrArray *pList/*=NULL*/)
1225 {
1226         ASSERT(pPageView);
1227         m_bReverse=bReverse;
1228         m_bIgnoreTopmost= bIgnoreTopmost;
1229         m_bCircle=bCircle;
1230         m_pIteratorAnnotList.RemoveAll();
1231         InitIteratorAnnotList(pPageView,pList);
1232 }
1233
1234 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::NextAnnot (const CPDFSDK_Annot* pCurrent) 
1235 {
1236         
1237         int index=-1;
1238         int nCount=this->m_pIteratorAnnotList.GetSize();
1239         if(pCurrent){
1240                 for(int i=0;i<nCount;i++){
1241                         CPDFSDK_Annot * pReaderAnnot= (CPDFSDK_Annot *)m_pIteratorAnnotList.GetAt(i);
1242                         if(pReaderAnnot ==pCurrent){                    
1243                                 index=i;
1244                                 break;
1245                         }                       
1246                 }
1247         }       
1248         return NextAnnot(index);
1249 }
1250 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::PrevAnnot (const CPDFSDK_Annot*pCurrent)
1251 {
1252         
1253         int index=-1;
1254         int nCount=this->m_pIteratorAnnotList.GetSize();
1255         if(pCurrent){
1256                 for(int i=0;i<nCount;i++){
1257                         CPDFSDK_Annot * pReaderAnnot= (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
1258                         if(pReaderAnnot ==pCurrent){                    
1259                                 index=i;
1260                                 break;
1261                         }                       
1262                 }       
1263         }
1264         return PrevAnnot(index);        
1265 }
1266 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::NextAnnot (int& index) 
1267 {       
1268         
1269         int nCount=m_pIteratorAnnotList.GetSize();
1270     if(nCount<=0) index=-1;
1271     else{
1272                 if(index<0){
1273                         index=0;                
1274                 }
1275                 else{           
1276                         if(m_bCircle){                  
1277                                 index=( index <nCount-1) ? (index+1) :0;                
1278                         }
1279                         else{
1280                                 index=( index <nCount-1) ? (index+1) :-1;               
1281                         }
1282                         
1283                 }       
1284         }
1285         return (index <0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index);           
1286 }
1287
1288
1289 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::PrevAnnot (int& index)
1290 {
1291         
1292         int nCount=m_pIteratorAnnotList.GetSize();
1293     if(nCount<=0) index=-1;
1294         else{   
1295                 if(index<0){
1296                         index=nCount-1;          
1297                 }
1298                 else{   
1299                         if(m_bCircle){                  
1300                                 index = ( index >0) ? (index-1) :nCount-1;              
1301                         }
1302                         else{
1303                                 index = ( index >0) ? (index-1) :-1;    
1304                         }                               
1305                 }
1306         }
1307         return (index <0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index);           
1308 }
1309
1310
1311 CPDFSDK_Annot*CPDFSDK_AnnotIterator::Next(const CPDFSDK_Annot* pCurrent) 
1312 {
1313
1314         return (m_bReverse) ? PrevAnnot(pCurrent):NextAnnot(pCurrent);           
1315
1316 }
1317
1318 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::Prev(const CPDFSDK_Annot* pCurrent) 
1319 {
1320
1321         return (m_bReverse) ? NextAnnot(pCurrent):PrevAnnot(pCurrent);           
1322 }
1323
1324 CPDFSDK_Annot*CPDFSDK_AnnotIterator::Next(int& index )
1325 {
1326         
1327         return (m_bReverse) ? PrevAnnot(index):NextAnnot(index);                 
1328         
1329 }
1330
1331 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::Prev(int& index )
1332 {
1333         
1334         return (m_bReverse) ? NextAnnot(index):PrevAnnot(index);                 
1335 }
1336
1337
1338 void CPDFSDK_AnnotIterator::InsertSort(CFX_PtrArray &arrayList, AI_COMPARE pCompare)
1339 {
1340         for (int i = 1; i < arrayList.GetSize(); i++)
1341         {
1342                 if (pCompare((CPDFSDK_Annot*)(arrayList[i]) , (CPDFSDK_Annot*)(arrayList[i-1])) < 0)
1343                 {
1344                         int j = i-1;
1345                         CPDFSDK_Annot* pTemp = (CPDFSDK_Annot*)arrayList[i];
1346                         
1347                         do
1348                         {
1349                                 arrayList[j + 1] = arrayList[j];
1350                         } while (--j >= 0 && pCompare(pTemp, (CPDFSDK_Annot*)arrayList[j]) < 0);
1351
1352                         arrayList[j+1] = pTemp;
1353                 }
1354         }
1355 }
1356
1357 int LyOrderCompare(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2)
1358 {
1359         if(p1->GetLayoutOrder() < p2->GetLayoutOrder())
1360                 return -1;
1361         else if (p1->GetLayoutOrder() == p2->GetLayoutOrder())
1362                 return 0;
1363         else
1364                 return 1;
1365 }
1366
1367 FX_BOOL CPDFSDK_AnnotIterator::InitIteratorAnnotList(CPDFSDK_PageView* pPageView,CFX_PtrArray * pAnnotList)
1368 {
1369         ASSERT(pPageView);
1370         
1371         
1372
1373         if(pAnnotList==NULL){   
1374                 pAnnotList=pPageView->GetAnnotList();
1375         }
1376
1377         this->m_pIteratorAnnotList.RemoveAll();
1378         if(!pAnnotList) return FALSE;
1379
1380         CPDFSDK_Annot * pTopMostAnnot= (m_bIgnoreTopmost) ? NULL : pPageView->GetFocusAnnot();
1381
1382
1383         int nCount =pAnnotList->GetSize();
1384
1385         for(int i = nCount- 1 ;i >= 0;i--)
1386         {
1387                 CPDFSDK_Annot * pReaderAnnot= (CPDFSDK_Annot*)pAnnotList->GetAt(i);
1388                 m_pIteratorAnnotList.Add(pReaderAnnot); 
1389         }
1390
1391         InsertSort(m_pIteratorAnnotList,&LyOrderCompare);
1392
1393         if(pTopMostAnnot)
1394         {
1395                 for(int i=0 ;i<nCount;i++)
1396                 {
1397                         CPDFSDK_Annot * pReaderAnnot = (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
1398                         if(pReaderAnnot == pTopMostAnnot)
1399                         {
1400                                 m_pIteratorAnnotList.RemoveAt(i);
1401                                 m_pIteratorAnnotList.InsertAt(0, pReaderAnnot);
1402                                 break;
1403                         }       
1404                 }
1405         }
1406
1407         return TRUE;
1408 }
1409