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