Replace XFA_HWIDGET with IXFA_Widget*
[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         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         IXFA_Widget* 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(IXFA_Widget* 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                 if (!pWidget->IsAppearanceValid())
738                         pWidget->ResetAppearance(NULL, FALSE);
739
740                 int nFieldType = pWidget->GetFieldType();
741                 if (nFieldType == FIELDTYPE_TEXTFIELD || nFieldType == FIELDTYPE_COMBOBOX)
742                 {
743                         FX_BOOL bFormated = FALSE;
744                         CFX_WideString sValue = pWidget->OnFormat(0, bFormated);
745                         if (bFormated && nFieldType == FIELDTYPE_COMBOBOX)
746                         {
747                                 pWidget->ResetAppearance(sValue.c_str(), FALSE);
748                         }
749                 }
750                 
751                 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) 
752                 {
753                         if (!pWidget->IsAppearanceValid() && !pWidget->GetValue().IsEmpty())
754                                 pWidget->ResetAppearance(FALSE);
755                 }
756                 
757
758                 if (m_pFormFiller)
759                         m_pFormFiller->OnLoad(pAnnot);
760         }
761 }
762
763 FX_BOOL CPDFSDK_BFAnnotHandler::OnSetFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
764 {
765         ASSERT(pAnnot != NULL);
766         CFX_ByteString sSubType = pAnnot->GetSubType();
767         
768         if (sSubType == BFFT_SIGNATURE)
769         {
770         }
771         else
772         {
773                 if (m_pFormFiller)
774                         return m_pFormFiller->OnSetFocus(pAnnot,nFlag);
775         }
776         
777         return TRUE;
778 }
779 FX_BOOL CPDFSDK_BFAnnotHandler::OnKillFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
780 {
781         ASSERT(pAnnot != NULL);
782         CFX_ByteString sSubType = pAnnot->GetSubType();
783         
784         if (sSubType == BFFT_SIGNATURE)
785         {
786         }
787         else
788         {
789                 if (m_pFormFiller)
790                         return m_pFormFiller->OnKillFocus(pAnnot,nFlag);
791         }
792         
793         return TRUE;
794 }
795
796 CPDF_Rect CPDFSDK_BFAnnotHandler::GetViewBBox(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot)
797 {
798         ASSERT(pAnnot != NULL);
799         CFX_ByteString sSubType = pAnnot->GetSubType();
800         
801         if (sSubType == BFFT_SIGNATURE)
802         {
803         }
804         else
805         {
806                 if (m_pFormFiller)
807                         return m_pFormFiller->GetViewBBox(pPageView, pAnnot);
808
809         }
810         
811         return CPDF_Rect(0,0,0,0);
812 }
813
814 FX_BOOL CPDFSDK_BFAnnotHandler::HitTest(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, const CPDF_Point& point)
815 {
816         ASSERT(pPageView);
817         ASSERT(pAnnot);
818
819         CPDF_Rect rect = GetViewBBox(pPageView, pAnnot);
820         return rect.Contains(point.x, point.y);
821 }
822
823 //CPDFSDK_XFAAnnotHandler
824
825 #define FWL_WGTHITTEST_Unknown                  0
826 #define FWL_WGTHITTEST_Client                   1 //arrow
827 #define FWL_WGTHITTEST_Titlebar                 11 //caption
828 #define FWL_WGTHITTEST_HScrollBar               15
829 #define FWL_WGTHITTEST_VScrollBar               16
830 #define FWL_WGTHITTEST_Border                   17
831 #define FWL_WGTHITTEST_Edit                             19
832 #define FWL_WGTHITTEST_HyperLink        20
833
834 CPDFSDK_XFAAnnotHandler::CPDFSDK_XFAAnnotHandler(CPDFDoc_Environment* pApp) :
835         m_pApp(pApp)
836 {
837
838 }
839
840 CPDFSDK_Annot* CPDFSDK_XFAAnnotHandler::NewAnnot(IXFA_Widget* pAnnot, CPDFSDK_PageView* pPage)
841 {
842         ASSERT(pPage != NULL);
843         ASSERT(pAnnot != NULL);
844
845         CPDFSDK_Document* pSDKDoc  = m_pApp->GetCurrentDoc();
846         ASSERT(pSDKDoc);
847         CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pSDKDoc->GetInterForm();
848         ASSERT(pInterForm != NULL);
849
850         CPDFSDK_XFAWidget* pWidget = NULL;
851         pWidget = new CPDFSDK_XFAWidget(pAnnot, pPage, pInterForm);
852         ASSERT(pWidget != NULL);
853         pInterForm->AddXFAMap(pAnnot, pWidget);
854
855         return pWidget;
856 }
857
858 FX_BOOL CPDFSDK_XFAAnnotHandler::CanAnswer(CPDFSDK_Annot* pAnnot)
859 {
860         ASSERT(pAnnot != NULL);
861
862         return pAnnot->GetXFAWidget() != NULL;
863 }
864
865 void CPDFSDK_XFAAnnotHandler::OnDraw(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device, FX_DWORD dwFlags)
866 {
867         ASSERT(pPageView != NULL);
868         ASSERT(pAnnot != NULL);
869
870         CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
871         ASSERT(pSDKDoc != NULL);
872
873         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
874         ASSERT(pWidgetHandler != NULL);
875
876         CFX_Graphics gs;
877         gs.Create(pDevice);
878
879         CFX_Matrix mt;
880         mt = *(CFX_Matrix*)pUser2Device;
881
882         IXFA_Widget* hWidget = pAnnot->GetXFAWidget();
883         ASSERT(hWidget != NULL);
884
885         FX_BOOL bIsHighlight = FALSE;
886         if (pSDKDoc->GetFocusAnnot() != pAnnot)
887                 bIsHighlight = TRUE;
888
889         pWidgetHandler->RenderWidget(pAnnot->GetXFAWidget(), &gs, &mt, bIsHighlight);
890
891         // to do highlight and shadow
892
893 }
894
895 void CPDFSDK_XFAAnnotHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot)
896 {
897         ASSERT(pAnnot != NULL);
898
899         CPDFSDK_XFAWidget* pWidget = (CPDFSDK_XFAWidget*)pAnnot;
900         CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm();
901         ASSERT(pInterForm != NULL);
902
903         pInterForm->RemoveXFAMap(pWidget->GetXFAWidget());
904
905         delete pWidget;
906 }
907
908 CPDF_Rect CPDFSDK_XFAAnnotHandler::GetViewBBox(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot)
909 {
910         ASSERT(pAnnot != NULL);
911
912         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
913         ASSERT(pWidgetHandler != NULL);
914
915         XFA_ELEMENT eType = pWidgetHandler->GetDataAcc(pAnnot->GetXFAWidget())->GetUIType();
916         CFX_RectF rcBBox;
917         if (eType == XFA_ELEMENT_Signature)
918                 pWidgetHandler->GetBBox (pAnnot->GetXFAWidget(), rcBBox, XFA_WIDGETSTATUS_Visible, TRUE);
919         else 
920                 pWidgetHandler->GetBBox (pAnnot->GetXFAWidget(), rcBBox, 0);
921
922         CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width, rcBBox.top+rcBBox.height);
923         rcWidget.left -= 1.0f;
924         rcWidget.right += 1.0f;
925         rcWidget.bottom -= 1.0f;
926         rcWidget.top += 1.0f;
927
928         return rcWidget;
929 }
930
931 FX_BOOL CPDFSDK_XFAAnnotHandler::HitTest(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, const CPDF_Point& point)
932 {
933         if (!pPageView || !pAnnot)
934                 return FALSE;
935
936         CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
937         if (!pSDKDoc)
938                 return FALSE;
939
940         CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
941         if (!pDoc)
942                 return FALSE;
943
944         IXFA_DocView* pDocView = pDoc->GetXFADocView();
945         if (!pDocView)
946                 return FALSE;
947
948         IXFA_WidgetHandler* pWidgetHandler = pDocView->GetWidgetHandler();
949         if (!pWidgetHandler)
950                 return FALSE;
951
952         FX_DWORD dwHitTest = pWidgetHandler->OnHitTest(pAnnot->GetXFAWidget(), point.x, point.y);
953         return (dwHitTest != FWL_WGTHITTEST_Unknown);
954 }
955
956 void CPDFSDK_XFAAnnotHandler::OnMouseEnter(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
957 {
958         if (!pPageView || !pAnnot) 
959                 return;
960         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
961         ASSERT(pWidgetHandler != NULL);
962
963         pWidgetHandler->OnMouseEnter(pAnnot->GetXFAWidget());
964 }
965
966 void CPDFSDK_XFAAnnotHandler::OnMouseExit(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
967 {
968         if (!pPageView || !pAnnot)
969                 return;
970
971         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
972         ASSERT(pWidgetHandler != NULL);
973
974         pWidgetHandler->OnMouseExit(pAnnot->GetXFAWidget());
975 }
976
977 FX_BOOL CPDFSDK_XFAAnnotHandler::OnLButtonDown(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
978 {
979         if (!pPageView || !pAnnot)
980                 return FALSE;
981
982         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
983         ASSERT(pWidgetHandler != NULL);
984
985         FX_BOOL bRet = FALSE;
986         bRet = pWidgetHandler->OnLButtonDown(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
987
988         return bRet;
989 }
990
991 FX_BOOL CPDFSDK_XFAAnnotHandler::OnLButtonUp(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
992 {
993         if (!pPageView || !pAnnot)
994                 return FALSE;
995
996         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
997         ASSERT(pWidgetHandler != NULL);
998
999         FX_BOOL bRet = FALSE;
1000         bRet = pWidgetHandler->OnLButtonUp(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
1001
1002         return bRet;
1003 }
1004
1005 FX_BOOL CPDFSDK_XFAAnnotHandler::OnLButtonDblClk(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
1006 {
1007         if (!pPageView || !pAnnot)
1008                 return FALSE;
1009
1010         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1011         ASSERT(pWidgetHandler != NULL);
1012
1013         FX_BOOL bRet = FALSE;
1014         bRet = pWidgetHandler->OnLButtonDblClk(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
1015
1016         return bRet;
1017 }
1018
1019 FX_BOOL CPDFSDK_XFAAnnotHandler::OnMouseMove(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
1020 {
1021         if (!pPageView || !pAnnot)
1022                 return FALSE;
1023
1024         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1025         ASSERT(pWidgetHandler != NULL);
1026
1027         FX_BOOL bRet = FALSE;
1028         bRet = pWidgetHandler->OnMouseMove(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
1029
1030         return bRet;
1031 }
1032
1033 FX_BOOL CPDFSDK_XFAAnnotHandler::OnMouseWheel(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, short zDelta, const CPDF_Point& point)
1034 {
1035         if (!pPageView || !pAnnot)
1036                 return FALSE;
1037
1038         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1039         ASSERT(pWidgetHandler != NULL);
1040
1041         FX_BOOL bRet = FALSE;
1042         bRet = pWidgetHandler->OnMouseWheel(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), zDelta, point.x, point.y);
1043
1044         return bRet;
1045 }
1046
1047 FX_BOOL CPDFSDK_XFAAnnotHandler::OnRButtonDown(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
1048 {
1049         if (!pPageView || !pAnnot)
1050                 return FALSE;
1051
1052         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1053         ASSERT(pWidgetHandler != NULL);
1054
1055         FX_BOOL bRet = FALSE;
1056         bRet = pWidgetHandler->OnRButtonDown(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
1057
1058         return bRet;
1059 }
1060
1061 FX_BOOL CPDFSDK_XFAAnnotHandler::OnRButtonUp(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
1062 {
1063         if (!pPageView || !pAnnot)
1064                 return FALSE;
1065
1066         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1067         ASSERT(pWidgetHandler != NULL);
1068
1069         FX_BOOL bRet = FALSE;
1070         bRet = pWidgetHandler->OnRButtonUp(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
1071
1072         return bRet;
1073 }
1074
1075 FX_BOOL CPDFSDK_XFAAnnotHandler::OnRButtonDblClk(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
1076 {
1077         if (!pPageView || !pAnnot)
1078                 return FALSE;
1079
1080         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1081         ASSERT(pWidgetHandler != NULL);
1082
1083         FX_BOOL bRet = FALSE;
1084         bRet = pWidgetHandler->OnRButtonDblClk(pAnnot->GetXFAWidget(), GetFWLFlags(nFlags), point.x, point.y);
1085
1086         return bRet;
1087 }
1088
1089 FX_BOOL CPDFSDK_XFAAnnotHandler::OnChar(CPDFSDK_Annot* pAnnot, FX_DWORD nChar, FX_DWORD nFlags)
1090 {
1091         if (!pAnnot)
1092                 return FALSE;
1093
1094         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1095         ASSERT(pWidgetHandler != NULL);
1096
1097         FX_BOOL bRet = FALSE;
1098         bRet = pWidgetHandler->OnChar(pAnnot->GetXFAWidget(), nChar, GetFWLFlags(nFlags));
1099
1100         return bRet;
1101 }
1102
1103 FX_BOOL CPDFSDK_XFAAnnotHandler::OnKeyDown(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
1104 {
1105         if (!pAnnot)
1106                 return FALSE;
1107
1108         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1109         ASSERT(pWidgetHandler != NULL);
1110
1111         FX_BOOL bRet = FALSE;
1112         bRet = pWidgetHandler->OnKeyDown(pAnnot->GetXFAWidget(), nKeyCode, GetFWLFlags(nFlag));
1113
1114         return bRet;
1115 }
1116
1117 FX_BOOL CPDFSDK_XFAAnnotHandler::OnKeyUp(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
1118 {
1119         if (!pAnnot)
1120                 return FALSE;
1121
1122         IXFA_WidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
1123         ASSERT(pWidgetHandler != NULL);
1124
1125         FX_BOOL bRet = FALSE;
1126         bRet = pWidgetHandler->OnKeyUp(pAnnot->GetXFAWidget(), nKeyCode, GetFWLFlags(nFlag));
1127
1128         return bRet;
1129 }
1130
1131 FX_BOOL CPDFSDK_XFAAnnotHandler::OnSetFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
1132 {
1133         return TRUE;
1134 }
1135
1136 FX_BOOL CPDFSDK_XFAAnnotHandler::OnKillFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
1137 {
1138         return TRUE;
1139 }
1140
1141 FX_BOOL CPDFSDK_XFAAnnotHandler::OnXFAChangedFocus(CPDFSDK_Annot* pOldAnnot, CPDFSDK_Annot* pNewAnnot)
1142 {
1143         IXFA_WidgetHandler* pWidgetHandler = NULL;
1144
1145         if (pOldAnnot)
1146                 pWidgetHandler = GetXFAWidgetHandler(pOldAnnot);
1147         else if (pNewAnnot)
1148                 pWidgetHandler = GetXFAWidgetHandler(pNewAnnot);
1149
1150         if (pWidgetHandler)
1151         {
1152                 FX_BOOL bRet = TRUE;
1153                 IXFA_Widget* hWidget = pNewAnnot ? pNewAnnot->GetXFAWidget() : NULL;
1154                 if (hWidget)
1155                 {
1156                         IXFA_PageView* pXFAPageView = pWidgetHandler->GetPageView(hWidget);
1157                         if (pXFAPageView)
1158                         {
1159                                 bRet = pXFAPageView->GetDocView()->SetFocus(hWidget);
1160                                 if (pXFAPageView->GetDocView()->GetFocusWidget() == hWidget)
1161                                         bRet = TRUE;
1162                         }
1163                 }
1164                 return bRet;
1165         }
1166
1167         return TRUE;
1168 }
1169
1170 IXFA_WidgetHandler* CPDFSDK_XFAAnnotHandler::GetXFAWidgetHandler(CPDFSDK_Annot* pAnnot)
1171 {
1172         if (!pAnnot)
1173                 return NULL;
1174
1175         CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
1176         if (!pPageView)
1177                 return NULL;
1178
1179         CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
1180         if (!pSDKDoc)
1181                 return NULL;
1182
1183         CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
1184         if (!pDoc)
1185                 return NULL;
1186
1187         IXFA_DocView* pDocView = pDoc->GetXFADocView();
1188         if (!pDocView)
1189                 return NULL;
1190
1191         return pDocView->GetWidgetHandler();
1192 }
1193
1194 #define FWL_KEYFLAG_Ctrl                                        (1 << 0)
1195 #define FWL_KEYFLAG_Alt                                         (1 << 1)
1196 #define FWL_KEYFLAG_Shift                                       (1 << 2)
1197 #define FWL_KEYFLAG_LButton                                     (1 << 3)
1198 #define FWL_KEYFLAG_RButton                                     (1 << 4)
1199 #define FWL_KEYFLAG_MButton                                     (1 << 5)
1200
1201 FX_DWORD CPDFSDK_XFAAnnotHandler::GetFWLFlags(FX_DWORD dwFlag)
1202 {
1203         FX_DWORD dwFWLFlag = 0;
1204
1205         if (dwFlag & FWL_EVENTFLAG_ControlKey)
1206                 dwFWLFlag |= FWL_KEYFLAG_Ctrl;
1207         if (dwFlag & FWL_EVENTFLAG_LeftButtonDown)
1208                 dwFWLFlag |= FWL_KEYFLAG_LButton;
1209         if (dwFlag & FWL_EVENTFLAG_MiddleButtonDown)
1210                 dwFWLFlag |= FWL_KEYFLAG_MButton;
1211         if (dwFlag & FWL_EVENTFLAG_RightButtonDown)
1212                 dwFWLFlag |= FWL_KEYFLAG_RButton;
1213         if (dwFlag & FWL_EVENTFLAG_ShiftKey)
1214                 dwFWLFlag |= FWL_KEYFLAG_Shift;
1215         if (dwFlag & FWL_EVENTFLAG_AltKey)
1216                 dwFWLFlag |= FWL_KEYFLAG_Alt;
1217
1218         return dwFWLFlag;
1219 }
1220
1221 //CReader_AnnotIteratorEx
1222
1223 CPDFSDK_AnnotIterator::CPDFSDK_AnnotIterator(CPDFSDK_PageView * pPageView,FX_BOOL bReverse,
1224                                                                                                  FX_BOOL bIgnoreTopmost/*=FALSE*/,
1225                                                                                                  FX_BOOL bCircle/*=FALSE*/,
1226                                                                                                  CFX_PtrArray *pList/*=NULL*/)
1227 {
1228         ASSERT(pPageView);
1229         m_bReverse=bReverse;
1230         m_bIgnoreTopmost= bIgnoreTopmost;
1231         m_bCircle=bCircle;
1232         m_pIteratorAnnotList.RemoveAll();
1233         InitIteratorAnnotList(pPageView,pList);
1234 }
1235
1236 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::NextAnnot (const CPDFSDK_Annot* pCurrent) 
1237 {
1238         
1239         int index=-1;
1240         int nCount=this->m_pIteratorAnnotList.GetSize();
1241         if(pCurrent){
1242                 for(int i=0;i<nCount;i++){
1243                         CPDFSDK_Annot * pReaderAnnot= (CPDFSDK_Annot *)m_pIteratorAnnotList.GetAt(i);
1244                         if(pReaderAnnot ==pCurrent){                    
1245                                 index=i;
1246                                 break;
1247                         }                       
1248                 }
1249         }       
1250         return NextAnnot(index);
1251 }
1252 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::PrevAnnot (const CPDFSDK_Annot*pCurrent)
1253 {
1254         
1255         int index=-1;
1256         int nCount=this->m_pIteratorAnnotList.GetSize();
1257         if(pCurrent){
1258                 for(int i=0;i<nCount;i++){
1259                         CPDFSDK_Annot * pReaderAnnot= (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
1260                         if(pReaderAnnot ==pCurrent){                    
1261                                 index=i;
1262                                 break;
1263                         }                       
1264                 }       
1265         }
1266         return PrevAnnot(index);        
1267 }
1268 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::NextAnnot (int& index) 
1269 {       
1270         
1271         int nCount=m_pIteratorAnnotList.GetSize();
1272     if(nCount<=0) index=-1;
1273     else{
1274                 if(index<0){
1275                         index=0;                
1276                 }
1277                 else{           
1278                         if(m_bCircle){                  
1279                                 index=( index <nCount-1) ? (index+1) :0;                
1280                         }
1281                         else{
1282                                 index=( index <nCount-1) ? (index+1) :-1;               
1283                         }
1284                         
1285                 }       
1286         }
1287         return (index <0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index);           
1288 }
1289
1290
1291 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::PrevAnnot (int& index)
1292 {
1293         
1294         int nCount=m_pIteratorAnnotList.GetSize();
1295     if(nCount<=0) index=-1;
1296         else{   
1297                 if(index<0){
1298                         index=nCount-1;          
1299                 }
1300                 else{   
1301                         if(m_bCircle){                  
1302                                 index = ( index >0) ? (index-1) :nCount-1;              
1303                         }
1304                         else{
1305                                 index = ( index >0) ? (index-1) :-1;    
1306                         }                               
1307                 }
1308         }
1309         return (index <0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index);           
1310 }
1311
1312
1313 CPDFSDK_Annot*CPDFSDK_AnnotIterator::Next(const CPDFSDK_Annot* pCurrent) 
1314 {
1315
1316         return (m_bReverse) ? PrevAnnot(pCurrent):NextAnnot(pCurrent);           
1317
1318 }
1319
1320 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::Prev(const CPDFSDK_Annot* pCurrent) 
1321 {
1322
1323         return (m_bReverse) ? NextAnnot(pCurrent):PrevAnnot(pCurrent);           
1324 }
1325
1326 CPDFSDK_Annot*CPDFSDK_AnnotIterator::Next(int& index )
1327 {
1328         
1329         return (m_bReverse) ? PrevAnnot(index):NextAnnot(index);                 
1330         
1331 }
1332
1333 CPDFSDK_Annot*  CPDFSDK_AnnotIterator::Prev(int& index )
1334 {
1335         
1336         return (m_bReverse) ? NextAnnot(index):PrevAnnot(index);                 
1337 }
1338
1339
1340 void CPDFSDK_AnnotIterator::InsertSort(CFX_PtrArray &arrayList, AI_COMPARE pCompare)
1341 {
1342         for (int i = 1; i < arrayList.GetSize(); i++)
1343         {
1344                 if (pCompare((CPDFSDK_Annot*)(arrayList[i]) , (CPDFSDK_Annot*)(arrayList[i-1])) < 0)
1345                 {
1346                         int j = i-1;
1347                         CPDFSDK_Annot* pTemp = (CPDFSDK_Annot*)arrayList[i];
1348                         
1349                         do
1350                         {
1351                                 arrayList[j + 1] = arrayList[j];
1352                         } while (--j >= 0 && pCompare(pTemp, (CPDFSDK_Annot*)arrayList[j]) < 0);
1353
1354                         arrayList[j+1] = pTemp;
1355                 }
1356         }
1357 }
1358
1359 int LyOrderCompare(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2)
1360 {
1361         if(p1->GetLayoutOrder() < p2->GetLayoutOrder())
1362                 return -1;
1363         else if (p1->GetLayoutOrder() == p2->GetLayoutOrder())
1364                 return 0;
1365         else
1366                 return 1;
1367 }
1368
1369 FX_BOOL CPDFSDK_AnnotIterator::InitIteratorAnnotList(CPDFSDK_PageView* pPageView,CFX_PtrArray * pAnnotList)
1370 {
1371         ASSERT(pPageView);
1372         
1373         
1374
1375         if(pAnnotList==NULL){   
1376                 pAnnotList=pPageView->GetAnnotList();
1377         }
1378
1379         this->m_pIteratorAnnotList.RemoveAll();
1380         if(!pAnnotList) return FALSE;
1381
1382         CPDFSDK_Annot * pTopMostAnnot= (m_bIgnoreTopmost) ? NULL : pPageView->GetFocusAnnot();
1383
1384
1385         int nCount =pAnnotList->GetSize();
1386
1387         for(int i = nCount- 1 ;i >= 0;i--)
1388         {
1389                 CPDFSDK_Annot * pReaderAnnot= (CPDFSDK_Annot*)pAnnotList->GetAt(i);
1390                 m_pIteratorAnnotList.Add(pReaderAnnot); 
1391         }
1392
1393         InsertSort(m_pIteratorAnnotList,&LyOrderCompare);
1394
1395         if(pTopMostAnnot)
1396         {
1397                 for(int i=0 ;i<nCount;i++)
1398                 {
1399                         CPDFSDK_Annot * pReaderAnnot = (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
1400                         if(pReaderAnnot == pTopMostAnnot)
1401                         {
1402                                 m_pIteratorAnnotList.RemoveAt(i);
1403                                 m_pIteratorAnnotList.InsertAt(0, pReaderAnnot);
1404                                 break;
1405                         }       
1406                 }
1407         }
1408
1409         return TRUE;
1410 }
1411