Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / fpdfsdk / src / formfiller / FFL_IFormFiller.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/formfiller/FFL_FormFiller.h"
8 #include "../../include/formfiller/FFL_IFormFiller.h"
9 #include "../../include/formfiller/FFL_CheckBox.h"
10 #include "../../include/formfiller/FFL_ComboBox.h"
11 #include "../../include/formfiller/FFL_ListBox.h"
12 #include "../../include/formfiller/FFL_PushButton.h"
13 #include "../../include/formfiller/FFL_RadioButton.h"
14 #include "../../include/formfiller/FFL_TextField.h"
15
16 #define FFL_MAXLISTBOXHEIGHT            140.0f
17
18 // HHOOK CFFL_IFormFiller::m_hookSheet = NULL;
19 // MSG CFFL_IFormFiller::g_Msg;
20
21 /* ----------------------------- CFFL_IFormFiller ----------------------------- */
22
23 CFFL_IFormFiller::CFFL_IFormFiller(CPDFDoc_Environment* pApp) : 
24         m_pApp(pApp),
25         m_bNotifying(FALSE)
26 {
27 }
28
29 CFFL_IFormFiller::~CFFL_IFormFiller()
30 {
31         FX_POSITION pos = m_Maps.GetStartPosition();
32         while (pos)
33         {
34                 CPDFSDK_Annot * pAnnot = NULL;
35                 CFFL_FormFiller * pFormFiller = NULL;
36                 m_Maps.GetNextAssoc(pos,pAnnot,pFormFiller);
37                 delete pFormFiller;
38         }
39         m_Maps.RemoveAll();
40 }
41
42 FX_BOOL CFFL_IFormFiller::Annot_HitTest(CPDFSDK_PageView* pPageView,CPDFSDK_Annot* pAnnot, CPDF_Point point)
43 {
44         CPDF_Rect rc = pAnnot->GetRect();
45         if(rc.Contains(point.x, point.y))
46                 return TRUE;
47         return FALSE;
48 }
49
50 FX_RECT CFFL_IFormFiller::GetViewBBox(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot)
51 {
52         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
53         {
54                 return pFormFiller->GetViewBBox(pPageView, pAnnot);
55         }
56         else
57         {
58                 ASSERT(pPageView != NULL);
59                 ASSERT(pAnnot != NULL);
60
61                 CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
62                 ASSERT(pPDFAnnot != NULL);
63
64                 CPDF_Rect rcAnnot;
65                 pPDFAnnot->GetRect(rcAnnot);
66
67 //              CRect rcWin;
68 //              pPageView->DocToWindow(rcAnnot, rcWin);
69                 CPDF_Rect rcWin = CPWL_Utils::InflateRect(rcAnnot,1);
70 //              rcWin.InflateRect(1, 1);
71
72                 return rcWin.GetOutterRect();
73         }
74 }
75
76 void CFFL_IFormFiller::OnDraw(CPDFSDK_PageView* pPageView, /*HDC hDC,*/ CPDFSDK_Annot* pAnnot, 
77                                                 CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,
78                                                 /*const CRect& rcWindow,*/ FX_DWORD dwFlags)
79 {
80         ASSERT(pPageView != NULL);
81         CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
82
83         if (IsVisible(pWidget))
84         {
85                 if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
86                 {
87                         if (pFormFiller->IsValid())
88                         {
89                                 pFormFiller->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
90                                 
91                                 pAnnot->GetPDFPage();
92                                 
93
94                                 CPDFSDK_Document* pDocument = m_pApp->GetCurrentDoc();
95                                 ASSERT(pDocument != NULL);
96
97                                 if (pDocument->GetFocusAnnot() == pAnnot)
98                                 {
99                                         CPDF_Rect rcFocus = pFormFiller->GetFocusBox(pPageView);
100                                         if (!rcFocus.IsEmpty())
101                                         {
102                                                 CFX_PathData path;
103                                                 
104                                                 path.SetPointCount(5);
105                                                 path.SetPoint(0, rcFocus.left,  rcFocus.top, FXPT_MOVETO);
106                                                 path.SetPoint(1, rcFocus.left,  rcFocus.bottom, FXPT_LINETO);
107                                                 path.SetPoint(2, rcFocus.right,  rcFocus.bottom, FXPT_LINETO);
108                                                 path.SetPoint(3, rcFocus.right,  rcFocus.top, FXPT_LINETO);
109                                                 path.SetPoint(4, rcFocus.left,  rcFocus.top, FXPT_LINETO);
110                                                 
111                                                 CFX_GraphStateData gsd;
112                                                 gsd.SetDashCount(1);                            
113                                                 gsd.m_DashArray[0] = 1.0f;
114                                                 gsd.m_DashPhase = 0;    
115                                                 
116                                                 gsd.m_LineWidth = 1.0f;
117                                                 pDevice->DrawPath(&path, pUser2Device, &gsd, 0, ArgbEncode(255,0,0,0), FXFILL_ALTERNATE);
118
119                                         //      ::DrawFocusRect(hDC, &rcFocus); 
120                                         }
121                                 }
122
123                                 return;
124                         }
125                 }
126
127                 if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
128                         pFormFiller->OnDrawDeactive(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
129                 else
130                         pWidget->DrawAppearance(pDevice, pUser2Device, CPDF_Annot::Normal, NULL);
131
132                 if (!IsReadOnly(pWidget) && IsFillingAllowed(pWidget))
133                 {
134                         pWidget->DrawShadow(pDevice, pPageView);
135                 }
136         
137         }
138 }
139
140 void CFFL_IFormFiller::OnCreate(CPDFSDK_Annot* pAnnot)
141 {
142         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
143         {
144                 pFormFiller->OnCreate(pAnnot);
145         }
146 }
147
148 void CFFL_IFormFiller::OnLoad(CPDFSDK_Annot* pAnnot)
149 {
150         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
151         {
152                 pFormFiller->OnLoad(pAnnot);
153         }
154 }
155
156 void CFFL_IFormFiller::OnDelete(CPDFSDK_Annot* pAnnot)
157 {
158         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
159         {
160                 pFormFiller->OnDelete(pAnnot);
161         }
162
163         UnRegisterFormFiller(pAnnot);
164 }
165
166 void CFFL_IFormFiller::OnMouseEnter(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlag)
167 {
168         ASSERT(pAnnot != NULL);
169         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
170         
171         if (!m_bNotifying)
172         {
173                 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
174                 if (pWidget->GetAAction(CPDF_AAction::CursorEnter))
175                 {
176                         m_bNotifying = TRUE;
177                         
178                         int nValueAge = pWidget->GetValueAge();
179
180                         pWidget->ClearAppModified();
181                         
182                         ASSERT(pPageView != NULL);
183                         
184                         
185                         
186                         PDFSDK_FieldAction fa;
187                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
188                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
189                         pWidget->OnAAction(CPDF_AAction::CursorEnter, fa, pPageView );
190                         m_bNotifying = FALSE;
191                         
192                         //if ( !IsValidAnnot(pPageView, pAnnot) ) return;
193                         
194                         if (pWidget->IsAppModified())
195                         {
196                                 if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
197                                 {
198                                         pFormFiller->ResetPDFWindow(pPageView, pWidget->GetValueAge() == nValueAge);
199                                 }
200                         }
201                 }
202         }
203         
204         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, TRUE))
205         {
206                 pFormFiller->OnMouseEnter(pPageView, pAnnot);
207         }
208 }
209
210 void CFFL_IFormFiller::OnMouseExit(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlag)
211 {
212         ASSERT(pAnnot != NULL);
213         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
214         
215         if (!m_bNotifying)
216         {
217                 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
218                 if (pWidget->GetAAction(CPDF_AAction::CursorExit))
219                 {
220                         m_bNotifying = TRUE;
221                         pWidget->GetAppearanceAge();
222                         int nValueAge = pWidget->GetValueAge();
223                         pWidget->ClearAppModified();
224                         
225                         ASSERT(pPageView != NULL);
226                         
227                         
228                         
229                         PDFSDK_FieldAction fa;
230                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
231                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
232                         
233                         pWidget->OnAAction(CPDF_AAction::CursorExit, fa, pPageView);
234                         m_bNotifying = FALSE;
235                         
236                         //if (!IsValidAnnot(pPageView, pAnnot)) return;
237                         
238                         if (pWidget->IsAppModified())
239                         {
240                                 if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
241                                 {
242                                         pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
243                                 }
244                         }
245                 }
246         }
247         
248         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
249         {
250                 pFormFiller->OnMouseExit(pPageView, pAnnot);
251         }
252 }
253
254 FX_BOOL CFFL_IFormFiller::OnLButtonDown(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
255 {
256         ASSERT(pAnnot != NULL);
257         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
258         
259         if (!m_bNotifying)
260         {
261                 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
262                 if (Annot_HitTest(pPageView, pAnnot, point) && pWidget->GetAAction(CPDF_AAction::ButtonDown))
263                 {
264                         m_bNotifying = TRUE;
265                         pWidget->GetAppearanceAge();
266                         int nValueAge = pWidget->GetValueAge();
267                         pWidget->ClearAppModified();
268                         
269                         ASSERT(pPageView != NULL);
270                         
271                         
272                         
273                         PDFSDK_FieldAction fa;
274                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlags);
275                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlags);
276                         pWidget->OnAAction(CPDF_AAction::ButtonDown, fa, pPageView);
277                         m_bNotifying = FALSE;
278                         
279                         if (!IsValidAnnot(pPageView, pAnnot)) return TRUE;
280                         
281                         if (pWidget->IsAppModified())
282                         {
283                                 if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
284                                 {
285                                         pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
286                                 }
287                         }
288                 }
289         }
290         
291         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
292         {
293                 return pFormFiller->OnLButtonDown(pPageView, pAnnot, nFlags, point);
294         }
295         
296         return FALSE;
297 }
298
299 FX_BOOL CFFL_IFormFiller::OnLButtonUp(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
300 {
301         ASSERT(pAnnot != NULL);
302         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
303         
304         CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
305         //      CReader_Page* pPage = pAnnot->GetPage();
306         //      ASSERT(pPage != NULL);
307         CPDFSDK_Document* pDocument = m_pApp->GetCurrentDoc();
308         ASSERT(pDocument != NULL);              
309         
310         switch (pWidget->GetFieldType())
311         {
312         case FIELDTYPE_PUSHBUTTON:
313         case FIELDTYPE_CHECKBOX:
314         case FIELDTYPE_RADIOBUTTON:
315                 if (GetViewBBox(pPageView, pAnnot).Contains((int)point.x, (int)point.y))
316                 {
317                         pDocument->SetFocusAnnot(pAnnot);
318                 }
319                 break;
320         default:
321                 pDocument->SetFocusAnnot(pAnnot);
322                 break;
323         }
324         
325         FX_BOOL bRet = FALSE;
326         
327         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
328         {
329                 bRet = pFormFiller->OnLButtonUp(pPageView, pAnnot, nFlags, point);
330         }
331
332         if (pDocument->GetFocusAnnot() == pAnnot)
333         {
334                 FX_BOOL bExit = FALSE;
335                 FX_BOOL bReset = FALSE;
336                 OnButtonUp(pWidget, pPageView, bReset, bExit,nFlags);
337                 if (bExit) return TRUE;
338
339                 OnClick(pWidget, pPageView, bReset, bExit, nFlags);
340                 if (bExit) return TRUE;
341         }
342         return bRet;
343 }
344
345 void CFFL_IFormFiller::OnButtonUp(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bReset, FX_BOOL& bExit,FX_UINT nFlag)
346 {
347         ASSERT(pWidget != NULL);
348         
349         if (!m_bNotifying)
350         {
351                 if (pWidget->GetAAction(CPDF_AAction::ButtonUp))
352                 {
353                         m_bNotifying = TRUE;
354                         int nAge = pWidget->GetAppearanceAge();
355                         int nValueAge = pWidget->GetValueAge();
356                         
357                         ASSERT(pPageView != NULL);
358 //                      CReader_DocView* pDocView = pPageView->GetDocView();
359 //                      ASSERT(pDocView != NULL);
360                         
361                         
362                         
363                         PDFSDK_FieldAction fa;
364                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
365                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
366
367                         pWidget->OnAAction(CPDF_AAction::ButtonUp, fa, pPageView);
368                         m_bNotifying = FALSE;
369                         
370                         if (!IsValidAnnot(pPageView, pWidget))
371                         {
372                                 bExit = TRUE;
373                                 return;
374                         }
375                         
376                         if (nAge != pWidget->GetAppearanceAge())
377                         {
378                                 if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
379                                 {
380                                         pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
381                                 }
382                                 
383                                 bReset = TRUE;
384                         }
385                 }
386         }
387 }
388
389 FX_BOOL CFFL_IFormFiller::OnLButtonDblClk(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
390 {
391         ASSERT(pAnnot != NULL);
392         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
393
394         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
395         {
396                 return pFormFiller->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
397         }
398
399         return FALSE;
400 }
401
402 FX_BOOL CFFL_IFormFiller::OnMouseMove(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
403 {
404         ASSERT(pAnnot != NULL);
405         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
406
407         //change cursor
408         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, TRUE))
409         {
410                 return pFormFiller->OnMouseMove(pPageView, pAnnot, nFlags, point);
411         }
412
413         return FALSE;
414 }
415
416 FX_BOOL CFFL_IFormFiller::OnMouseWheel(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, short zDelta, const CPDF_Point& point)
417 {
418         ASSERT(pAnnot != NULL);
419         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
420
421         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
422         {
423                 return pFormFiller->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta, point);
424         }
425
426         return FALSE;
427 }
428
429 FX_BOOL CFFL_IFormFiller::OnRButtonDown(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
430 {
431         ASSERT(pAnnot != NULL);
432         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
433
434         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
435         {
436                 return pFormFiller->OnRButtonDown(pPageView, pAnnot, nFlags, point);
437         }
438
439         return FALSE;
440 }
441
442 FX_BOOL CFFL_IFormFiller::OnRButtonUp(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
443 {
444         ASSERT(pAnnot != NULL);
445         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
446
447         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
448         {
449                 return pFormFiller->OnRButtonUp(pPageView, pAnnot, nFlags, point);
450         }
451
452         return FALSE;
453 }
454
455 FX_BOOL CFFL_IFormFiller::OnRButtonDblClk(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
456 {
457         ASSERT(pAnnot != NULL);
458         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
459
460         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
461         {
462                 return pFormFiller->OnRButtonDblClk(pPageView, pAnnot, nFlags, point);
463         }
464
465         return FALSE;
466 }
467
468 FX_BOOL CFFL_IFormFiller::OnKeyDown(CPDFSDK_Annot* pAnnot, FX_UINT nKeyCode, FX_UINT nFlags)
469 {
470         ASSERT(pAnnot != NULL);
471         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
472
473         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
474         {
475                 return pFormFiller->OnKeyDown(pAnnot, nKeyCode, nFlags);        
476         }
477
478         return FALSE;
479 }
480
481 FX_BOOL CFFL_IFormFiller::OnChar(CPDFSDK_Annot* pAnnot, FX_UINT nChar, FX_UINT nFlags)
482 {
483         ASSERT(pAnnot != NULL);
484         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
485
486         if (nChar == FWL_VKEY_Tab) return TRUE;
487
488         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
489         {
490                 return pFormFiller->OnChar(pAnnot, nChar, nFlags);
491         }
492
493         return FALSE;
494 }
495
496 void CFFL_IFormFiller::OnDeSelected(CPDFSDK_Annot* pAnnot)
497 {
498         ASSERT(pAnnot != NULL);
499         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
500
501         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
502         {
503                 pFormFiller->OnDeSelected(pAnnot);
504         }
505 }
506
507 void CFFL_IFormFiller::OnSelected(CPDFSDK_Annot* pAnnot)
508 {
509         ASSERT(pAnnot != NULL);
510         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
511
512         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
513         {
514                 pFormFiller->OnSelected(pAnnot);
515         }
516 }
517
518 FX_BOOL CFFL_IFormFiller::OnSetFocus(CPDFSDK_Annot* pAnnot,FX_UINT nFlag)
519 {
520         if(!pAnnot) return FALSE;
521         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
522
523         if (!m_bNotifying)
524         {
525                 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
526                 if (pWidget->GetAAction(CPDF_AAction::GetFocus))
527                 {
528                         m_bNotifying = TRUE;
529                         pWidget->GetAppearanceAge();
530                         int nValueAge = pWidget->GetValueAge();
531                         pWidget->ClearAppModified();
532  
533  
534                         CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
535                         ASSERT(pPageView != NULL);
536                         
537                         PDFSDK_FieldAction fa;
538                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
539                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
540
541  
542                         CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, TRUE);
543                         if(!pFormFiller) return FALSE;
544                         pFormFiller->GetActionData(pPageView, CPDF_AAction::GetFocus, fa);
545  
546                         pWidget->OnAAction(CPDF_AAction::GetFocus, fa, pPageView);
547                         m_bNotifying = FALSE;
548                         
549  //                     if (!IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pAnnot)) return FALSE;
550  
551                         if (pWidget->IsAppModified())
552                         {
553                                 if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
554                                 {
555                                         pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
556                                 }
557                         }
558                 }
559         }
560         
561         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, TRUE))
562         {
563                 if (pFormFiller->OnSetFocus(pAnnot, nFlag))
564                 {
565                         return TRUE;
566                 }
567                 else
568                         return FALSE;
569         }
570
571         return TRUE;
572 }
573
574 FX_BOOL CFFL_IFormFiller::OnKillFocus(CPDFSDK_Annot* pAnnot,FX_UINT nFlag)
575 {
576         if(!pAnnot) return FALSE;
577         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
578
579         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
580         {
581                 if (pFormFiller->OnKillFocus(pAnnot, nFlag))
582                 {
583                         if (!m_bNotifying)
584                         {
585                                 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
586                                 if (pWidget->GetAAction(CPDF_AAction::LoseFocus))
587                                 {
588                                         m_bNotifying = TRUE;
589                                         pWidget->ClearAppModified();
590  
591                                         CPDFSDK_PageView* pPageView = pWidget->GetPageView();
592                                         ASSERT(pPageView != NULL);
593  
594                                         PDFSDK_FieldAction fa;
595                                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
596                                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
597  
598                                         pFormFiller->GetActionData(pPageView, CPDF_AAction::LoseFocus, fa);
599  
600                                         pWidget->OnAAction(CPDF_AAction::LoseFocus, fa, pPageView);
601                                         m_bNotifying = FALSE;
602  
603                                 }
604                         }
605                 }
606                 else
607                         return FALSE;
608         }
609
610         return TRUE;
611 }
612
613 FX_BOOL CFFL_IFormFiller::IsVisible(CPDFSDK_Widget* pWidget)
614 {
615         return pWidget->IsVisible();
616 }
617
618 FX_BOOL CFFL_IFormFiller::IsReadOnly(CPDFSDK_Widget* pWidget)
619 {
620         ASSERT(pWidget != NULL);
621
622         int nFieldFlags = pWidget->GetFieldFlags();
623
624         return (nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY;
625 }
626
627 FX_BOOL CFFL_IFormFiller::IsFillingAllowed(CPDFSDK_Widget* pWidget)
628 {
629         ASSERT(pWidget != NULL);
630
631         if (pWidget->GetFieldType() == FIELDTYPE_PUSHBUTTON)
632                 return TRUE;
633         else
634         {
635                 CPDF_Page* pPage = pWidget->GetPDFPage();
636                 ASSERT(pPage != NULL);
637  
638                 CPDF_Document* pDocument = pPage->m_pDocument;
639                 ASSERT(pDocument != NULL);
640  
641                 FX_DWORD dwPermissions = pDocument->GetUserPermissions();
642                 return (dwPermissions&FPDFPERM_FILL_FORM) || 
643                                 (dwPermissions&FPDFPERM_ANNOT_FORM) || 
644                         (dwPermissions&FPDFPERM_MODIFY);
645         }
646         return TRUE;    
647 }
648
649 CFFL_FormFiller* CFFL_IFormFiller::GetFormFiller(CPDFSDK_Annot* pAnnot, FX_BOOL bRegister)
650 {
651 //      ASSERT(pAnnot != NULL);
652 //      ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
653
654         CFFL_FormFiller * pFormFiller = NULL;
655         m_Maps.Lookup(pAnnot, pFormFiller);
656
657         if (pFormFiller)
658                 return pFormFiller;
659
660         if (bRegister)
661         {
662                 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;              
663
664                 int nFieldType = pWidget->GetFieldType();
665                 switch(nFieldType)
666                 {
667                 case FIELDTYPE_PUSHBUTTON:
668                         pFormFiller = new CFFL_PushButton(m_pApp, pWidget);
669                         break;
670                 case FIELDTYPE_CHECKBOX:
671                         pFormFiller = new CFFL_CheckBox(m_pApp, pWidget);
672                         break;
673                 case FIELDTYPE_RADIOBUTTON:
674                         pFormFiller = new CFFL_RadioButton(m_pApp, pWidget);
675                         break;
676                 case FIELDTYPE_TEXTFIELD:
677                         pFormFiller = new CFFL_TextField(m_pApp, pWidget);
678                         break;
679                 case FIELDTYPE_LISTBOX:
680                         pFormFiller = new CFFL_ListBox(m_pApp, pWidget);
681                         break;
682                 case FIELDTYPE_COMBOBOX:
683                         pFormFiller = new CFFL_ComboBox(m_pApp, pWidget);
684                         break;
685                 case FIELDTYPE_UNKNOWN:
686                 default:
687                         pFormFiller = NULL;
688                         break;
689                 }
690
691                 if (pFormFiller)
692                 {
693                         m_Maps.SetAt(pAnnot, pFormFiller);
694                 }
695         }
696
697         return pFormFiller;
698 }
699
700 void CFFL_IFormFiller::RemoveFormFiller(CPDFSDK_Annot* pAnnot)
701 {
702         if ( pAnnot != NULL )
703         {
704                 UnRegisterFormFiller( pAnnot );
705         }
706 }
707
708 void CFFL_IFormFiller::UnRegisterFormFiller(CPDFSDK_Annot* pAnnot)
709 {
710         CFFL_FormFiller * pFormFiller = NULL;
711
712         if (m_Maps.Lookup(pAnnot,pFormFiller))
713         {
714                 if (pFormFiller)
715                         delete pFormFiller;
716                 m_Maps.RemoveKey(pAnnot);
717         }
718 }
719
720 void CFFL_IFormFiller::SetFocusAnnotTab(CPDFSDK_Annot* pWidget, FX_BOOL bSameField, FX_BOOL bNext)
721 {
722
723 }
724
725 void CFFL_IFormFiller::QueryWherePopup(void* pPrivateData, FX_FLOAT fPopupMin,FX_FLOAT fPopupMax, FX_INT32 & nRet, FX_FLOAT & fPopupRet)
726 {
727         ASSERT(pPrivateData != NULL);
728
729         CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
730
731         
732
733
734         CPDF_Rect rcPageView(0,0,0,0);
735         rcPageView.right = pData->pWidget->GetPDFPage()->GetPageWidth();
736         rcPageView.bottom = pData->pWidget->GetPDFPage()->GetPageHeight();
737         rcPageView.Normalize();
738
739
740         ASSERT(pData->pWidget != NULL);
741         CPDF_Rect rcAnnot = pData->pWidget->GetRect();
742
743         FX_FLOAT fTop = 0.0f;
744         FX_FLOAT fBottom = 0.0f;
745
746         CPDFSDK_Widget * pWidget = (CPDFSDK_Widget*)pData->pWidget;
747         switch (pWidget->GetRotate() / 90)
748         {
749         default:
750         case 0:
751                 fTop = rcPageView.top - rcAnnot.top;
752                 fBottom = rcAnnot.bottom - rcPageView.bottom;
753                 break;
754         case 1:
755                 fTop = rcAnnot.left - rcPageView.left;
756                 fBottom = rcPageView.right - rcAnnot.right;
757                 break;
758         case 2:
759                 fTop = rcAnnot.bottom - rcPageView.bottom;
760                 fBottom = rcPageView.top - rcAnnot.top;
761                 break;
762         case 3:
763                 fTop = rcPageView.right - rcAnnot.right;
764                 fBottom = rcAnnot.left - rcPageView.left;
765                 break;
766         }
767
768         FX_FLOAT fFactHeight = 0;
769         FX_BOOL bBottom = TRUE;
770         FX_FLOAT fMaxListBoxHeight = 0;
771         if (fPopupMax > FFL_MAXLISTBOXHEIGHT)
772         {
773                 if (fPopupMin > FFL_MAXLISTBOXHEIGHT)
774                 {
775                         fMaxListBoxHeight = fPopupMin;
776                 }
777                 else
778                 {
779                         fMaxListBoxHeight = FFL_MAXLISTBOXHEIGHT;
780                 }
781         }
782         else
783                 fMaxListBoxHeight = fPopupMax;
784
785         if (fBottom > fMaxListBoxHeight)
786         {
787                 fFactHeight = fMaxListBoxHeight;
788                 bBottom = TRUE;
789         }
790         else
791         {
792                 if (fTop > fMaxListBoxHeight)
793                 {
794                         fFactHeight = fMaxListBoxHeight;
795                         bBottom = FALSE;
796                 }
797                 else
798                 {
799                         if (fTop > fBottom)
800                         {
801                                 fFactHeight = fTop;
802                                 bBottom = FALSE;
803                         }
804                         else
805                         {
806                                 fFactHeight = fBottom;
807                                 bBottom = TRUE;
808                         }
809                 }
810         }
811
812         nRet = bBottom ? 0 : 1;
813         fPopupRet = fFactHeight;
814 }
815
816 void CFFL_IFormFiller::OnSetWindowRect(void* pPrivateData, const CPDF_Rect & rcWindow)
817 {
818         ASSERT(pPrivateData != NULL);
819
820         CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
821
822         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pData->pWidget, TRUE))
823         {
824
825                 CPDF_Rect rcOld = pFormFiller->PWLtoFFL(pFormFiller->GetWindowRect(pData->pPageView));
826                 CPDF_Rect rcNew = pFormFiller->PWLtoFFL(rcWindow);
827                 pFormFiller->SetWindowRect(pData->pPageView, rcWindow);
828
829                 CPDF_Rect unRect = rcOld;
830                 unRect.Union(rcNew);
831                 //FX_RECT rcRect = unRect.GetOutterRect();
832                 unRect.left = (FX_FLOAT)(unRect.left - 0.5);
833                 unRect.right = (FX_FLOAT)(unRect.right + 0.5);
834                 unRect.top = (FX_FLOAT)(unRect.top + 0.5);
835                 unRect.bottom = (FX_FLOAT)(unRect.bottom -0.5);
836                 m_pApp->FFI_Invalidate(pData->pWidget->GetPDFXFAPage(), unRect.left, unRect.top, unRect.right, unRect.bottom);
837         }
838 }
839
840 void CFFL_IFormFiller::OnKeyStroke(FX_BOOL bEditOrList, void* pPrivateData, FX_INT32 nKeyCode, CFX_WideString& strChange, 
841                                                                    const CFX_WideString& strChangeEx, FX_BOOL bKeyDown, 
842                                                                    FX_BOOL & bRC, FX_BOOL & bExit)
843 {
844         ASSERT(pPrivateData != NULL);
845         CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
846         ASSERT(pData->pWidget != NULL);
847
848         CFFL_FormFiller* pFormFiller = GetFormFiller(pData->pWidget, FALSE);
849         ASSERT(pFormFiller != NULL);
850
851         pFormFiller->OnKeyStroke(bKeyDown);
852 }
853
854 void CFFL_IFormFiller::OnKeyStrokeCommit(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bRC, FX_BOOL& bExit, FX_DWORD nFlag)
855 {
856         if (!m_bNotifying)
857         {
858                 ASSERT(pWidget != NULL);
859                 if (pWidget->GetAAction(CPDF_AAction::KeyStroke))
860                 {
861                         m_bNotifying = TRUE;
862                         pWidget->ClearAppModified();
863
864                         ASSERT(pPageView != NULL);
865 //                      CReader_DocView* pDocView = pPageView->GetDocView();
866 //                      ASSERT(pDocView != NULL);
867                         
868                 
869
870                         PDFSDK_FieldAction fa;
871                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
872                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
873                         fa.bWillCommit = TRUE;
874                         fa.nCommitKey = GetCommitKey();
875                         fa.bKeyDown = GetKeyDown();
876                         fa.bRC = TRUE;
877
878                         CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE);
879                         ASSERT(pFormFiller != NULL);
880
881                         pFormFiller->GetActionData(pPageView, CPDF_AAction::KeyStroke, fa);
882                         pFormFiller->SaveState(pPageView);
883
884                         PDFSDK_FieldAction faOld = fa;
885                         pWidget->OnAAction(CPDF_AAction::KeyStroke, fa, pPageView);
886
887                         bRC = fa.bRC;
888 //                      bExit = !IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pWidget);
889
890                         m_bNotifying = FALSE;
891                 }
892         }
893 }
894
895 void CFFL_IFormFiller::OnValidate(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bRC, FX_BOOL& bExit, FX_DWORD nFlag)
896 {
897         if (!m_bNotifying)
898         {
899                 ASSERT(pWidget != NULL);
900                 if (pWidget->GetAAction(CPDF_AAction::Validate))
901                 {
902                         m_bNotifying = TRUE;
903                         pWidget->ClearAppModified();
904
905                         ASSERT(pPageView != NULL);
906 //                      CReader_DocView* pDocView = pPageView->GetDocView();
907 //                      ASSERT(pDocView != NULL);
908                         
909                         
910
911                         PDFSDK_FieldAction fa;
912                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
913                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
914                         fa.bKeyDown = GetKeyDown();
915                         fa.bRC = TRUE;
916
917                         CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE);
918                         ASSERT(pFormFiller != NULL);
919
920                         pFormFiller->GetActionData(pPageView, CPDF_AAction::Validate, fa);
921                         pFormFiller->SaveState(pPageView);
922
923                         PDFSDK_FieldAction faOld = fa;
924                         pWidget->OnAAction(CPDF_AAction::Validate, fa, pPageView);
925
926                         bRC = fa.bRC;
927 //                      bExit = !IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pWidget);
928
929                         m_bNotifying = FALSE;
930                 }
931         }
932 }
933
934 void CFFL_IFormFiller::OnCalculate(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bExit, FX_DWORD nFlag)
935 {
936         if (!m_bNotifying)
937         {
938                 ASSERT(pWidget != NULL);
939                 ASSERT(pPageView != NULL);
940 //              CReader_DocView* pDocView = pPageView->GetDocView();
941 //              ASSERT(pDocView != NULL);
942                 CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
943                 ASSERT(pDocument != NULL);
944
945                 CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
946                 ASSERT(pInterForm != NULL);
947
948                 pInterForm->OnCalculate(pWidget->GetFormField());
949
950 //              bExit = !IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pWidget);
951
952                 m_bNotifying = FALSE;
953         }
954 }
955
956 void CFFL_IFormFiller::OnFormat(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bExit, FX_DWORD nFlag)
957 {
958         if (!m_bNotifying)
959         {
960                 ASSERT(pWidget != NULL);
961                 ASSERT(pPageView != NULL);
962 //              CReader_DocView* pDocView = pPageView->GetDocView();
963 //              ASSERT(pDocView != NULL);
964                 CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
965                 ASSERT(pDocument != NULL);
966
967                 CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
968                 ASSERT(pInterForm != NULL);
969
970                 FX_BOOL bFormated = FALSE;
971                 CFX_WideString sValue = pInterForm->OnFormat(pWidget->GetFormField(), GetCommitKey(), bFormated);
972
973 //              bExit = !IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pWidget);
974
975                 if (bExit) return;
976
977                 if (bFormated)
978                 {
979                         pInterForm->ResetFieldAppearance(pWidget->GetFormField(), sValue, TRUE);
980                         pInterForm->UpdateField(pWidget->GetFormField());
981                 }
982
983                 m_bNotifying = FALSE;
984         }
985 }
986
987 // LRESULT CALLBACK CFFL_IFormFiller::FFL_WndProc(
988 //                                                                        int code,       // hook code
989 //                                                                        WPARAM wParam,  // virtual-key code
990 //                                                                        LPARAM lParam   // keystroke-message information
991 //                                                                              )
992 // {
993 //      if (code != HC_ACTION)
994 //      {
995 //              return CallNextHookEx (m_hookSheet, code, wParam, lParam);
996 //      }
997 // 
998 //      FXSYS_memcpy(&g_Msg, (void*)lParam, sizeof(MSG));       
999 // 
1000 //      return 0;
1001 // }
1002
1003 // MSG  CFFL_IFormFiller::GetLastMessage()
1004 // {
1005 //      return g_Msg;
1006 // }
1007
1008 int CFFL_IFormFiller::GetCommitKey()
1009 {
1010 //      MSG msg = CFFL_IFormFiller::GetLastMessage();
1011
1012         int nCommitKey = 0;
1013 //      switch (msg.message)
1014 //      {
1015 //      case WM_LBUTTONDOWN:
1016 //      case WM_LBUTTONUP:
1017 //              nCommitKey = 1;
1018 //              break;
1019 //      case WM_KEYDOWN:
1020 //              switch (msg.wParam)
1021 //              {
1022 //              case VK_RETURN:
1023 //                      nCommitKey = 2;
1024 //                      break;
1025 //              case VK_TAB:
1026 //                      nCommitKey = 3;
1027 //                      break;
1028 //              }
1029 //              break;
1030 //      }
1031
1032         return nCommitKey;
1033 }
1034
1035 FX_BOOL CFFL_IFormFiller::GetKeyDown()
1036 {
1037         return TRUE;
1038 //      MSG msg = CFFL_IFormFiller::GetLastMessage();
1039 // 
1040 //      return msg.message == WM_KEYDOWN || msg.message == WM_CHAR;
1041 }
1042
1043 void CFFL_IFormFiller::OnClick(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bReset, FX_BOOL& bExit, FX_UINT nFlag)
1044 {
1045         ASSERT(pWidget != NULL);
1046
1047         if (!m_bNotifying)
1048         {
1049                 if (pWidget->HasXFAAAction(PDFSDK_XFA_Click))
1050                 {
1051                         m_bNotifying = TRUE;
1052                         int nAge = pWidget->GetAppearanceAge();
1053                         int nValueAge = pWidget->GetValueAge();
1054
1055                         PDFSDK_FieldAction fa;
1056                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
1057                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
1058
1059                         pWidget->OnXFAAAction(PDFSDK_XFA_Click, fa, pPageView);
1060                         m_bNotifying = FALSE;
1061
1062                         if (!IsValidAnnot(pPageView, pWidget))
1063                         {
1064                                 bExit = TRUE;
1065                                 return;
1066                         }
1067
1068                         if (nAge != pWidget->GetAppearanceAge())
1069                         {
1070                                 if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
1071                                 {
1072                                         pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
1073                                 }
1074
1075                                 bReset = TRUE;
1076                         }
1077                 }
1078         }
1079 }
1080
1081 void CFFL_IFormFiller::OnFull(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bReset, FX_BOOL& bExit, FX_UINT nFlag)
1082 {
1083         ASSERT(pWidget != NULL);
1084
1085         if (!m_bNotifying)
1086         {
1087                 if (pWidget->HasXFAAAction(PDFSDK_XFA_Full))
1088                 {
1089                         m_bNotifying = TRUE;
1090                         int nAge = pWidget->GetAppearanceAge();
1091                         int nValueAge = pWidget->GetValueAge();
1092
1093                         PDFSDK_FieldAction fa;
1094                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
1095                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
1096
1097                         pWidget->OnXFAAAction(PDFSDK_XFA_Full, fa, pPageView);
1098                         m_bNotifying = FALSE;
1099
1100                         if (!IsValidAnnot(pPageView, pWidget))
1101                         {
1102                                 bExit = TRUE;
1103                                 return;
1104                         }
1105
1106                         if (nAge != pWidget->GetAppearanceAge())
1107                         {
1108                                 if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
1109                                 {
1110                                         pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
1111                                 }
1112
1113                                 bReset = TRUE;
1114                         }
1115                 }
1116         }
1117 }
1118
1119 void CFFL_IFormFiller::OnPopupPreOpen(void* pPrivateData, FX_BOOL& bExit, FX_DWORD nFlag)
1120 {
1121         ASSERT(pPrivateData != NULL);
1122         CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
1123         ASSERT(pData->pWidget != NULL);
1124
1125         FX_BOOL bTempReset = FALSE;
1126         FX_BOOL bTempExit = FALSE;
1127         this->OnPreOpen(pData->pWidget, pData->pPageView, bTempReset, bTempExit, nFlag);
1128
1129         if (bTempReset || bTempExit)
1130         {
1131                 bExit = TRUE;
1132         }
1133 }
1134
1135 void CFFL_IFormFiller::OnPopupPostOpen(void* pPrivateData, FX_BOOL& bExit, FX_DWORD nFlag)
1136 {
1137         ASSERT(pPrivateData != NULL);
1138         CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
1139         ASSERT(pData->pWidget != NULL);
1140
1141         FX_BOOL bTempReset = FALSE;
1142         FX_BOOL bTempExit = FALSE;
1143         this->OnPostOpen(pData->pWidget, pData->pPageView, bTempReset, bTempExit, nFlag);
1144
1145         if (bTempReset || bTempExit)
1146         {
1147                 bExit = TRUE;
1148         }
1149 }
1150
1151 void CFFL_IFormFiller::OnPreOpen(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bReset, FX_BOOL& bExit, FX_UINT nFlag)
1152 {
1153         ASSERT(pWidget != NULL);
1154
1155         if (!m_bNotifying)
1156         {
1157                 if (pWidget->HasXFAAAction(PDFSDK_XFA_PreOpen))
1158                 {
1159                         m_bNotifying = TRUE;
1160                         int nAge = pWidget->GetAppearanceAge();
1161                         int nValueAge = pWidget->GetValueAge();
1162
1163                         PDFSDK_FieldAction fa;
1164                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
1165                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
1166
1167                         pWidget->OnXFAAAction(PDFSDK_XFA_PreOpen, fa, pPageView);
1168                         m_bNotifying = FALSE;
1169
1170                         if (!IsValidAnnot(pPageView, pWidget))
1171                         {
1172                                 bExit = TRUE;
1173                                 return;
1174                         }
1175
1176                         if (nAge != pWidget->GetAppearanceAge())
1177                         {
1178                                 if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
1179                                 {
1180                                         pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
1181                                 }
1182
1183                                 bReset = TRUE;
1184                         }
1185                 }
1186         }
1187 }
1188
1189 void CFFL_IFormFiller::OnPostOpen(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bReset, FX_BOOL& bExit, FX_UINT nFlag)
1190 {
1191         ASSERT(pWidget != NULL);
1192
1193         if (!m_bNotifying)
1194         {
1195                 if (pWidget->HasXFAAAction(PDFSDK_XFA_PostOpen))
1196                 {
1197                         m_bNotifying = TRUE;
1198                         int nAge = pWidget->GetAppearanceAge();
1199                         int nValueAge = pWidget->GetValueAge();
1200
1201                         PDFSDK_FieldAction fa;
1202                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
1203                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
1204
1205                         pWidget->OnXFAAAction(PDFSDK_XFA_PostOpen, fa, pPageView);
1206                         m_bNotifying = FALSE;
1207
1208                         if (!IsValidAnnot(pPageView, pWidget))
1209                         {
1210                                 bExit = TRUE;
1211                                 return;
1212                         }
1213
1214                         if (nAge != pWidget->GetAppearanceAge())
1215                         {
1216                                 if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
1217                                 {
1218                                         pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
1219                                 }
1220
1221                                 bReset = TRUE;
1222                         }
1223                 }
1224         }
1225 }
1226
1227 FX_BOOL CFFL_IFormFiller::IsValidAnnot(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot)
1228 {
1229
1230         ASSERT(pPageView != NULL);
1231         ASSERT(pAnnot != NULL);
1232
1233         if(pPageView)
1234                 return pPageView->IsValidAnnot(pAnnot->GetPDFAnnot());
1235         else
1236                 return FALSE;
1237 }
1238
1239 void CFFL_IFormFiller::BeforeUndo(CPDFSDK_Document* pDocument)
1240 {
1241
1242 }
1243
1244 void CFFL_IFormFiller::BeforeRedo(CPDFSDK_Document* pDocument)
1245 {
1246         BeforeUndo(pDocument);
1247 }
1248
1249 void CFFL_IFormFiller::AfterUndo(CPDFSDK_Document* pDocument)
1250 {
1251 }
1252
1253 void CFFL_IFormFiller::AfterRedo(CPDFSDK_Document* pDocument)
1254 {
1255 }
1256
1257 FX_BOOL CFFL_IFormFiller::CanCopy(CPDFSDK_Document* pDocument)
1258 {
1259
1260         return FALSE;
1261 }
1262
1263 FX_BOOL CFFL_IFormFiller::CanCut(CPDFSDK_Document* pDocument)
1264 {
1265
1266         return FALSE;
1267 }
1268
1269 FX_BOOL CFFL_IFormFiller::CanPaste(CPDFSDK_Document* pDocument)
1270 {
1271
1272         return FALSE;
1273 }
1274
1275 void CFFL_IFormFiller::DoCopy(CPDFSDK_Document* pDocument)
1276 {
1277 }
1278
1279 void CFFL_IFormFiller::DoCut(CPDFSDK_Document* pDocument)
1280 {
1281 }
1282
1283 void CFFL_IFormFiller::DoPaste(CPDFSDK_Document* pDocument)
1284 {
1285
1286 }
1287 void CFFL_IFormFiller::OnBeforeKeyStroke(FX_BOOL bEditOrList, void* pPrivateData, FX_INT32 nKeyCode,
1288                                                                                           CFX_WideString & strChange, const CFX_WideString& strChangeEx, 
1289                                                                                           int nSelStart, int nSelEnd,
1290                                                                                 FX_BOOL bKeyDown, FX_BOOL & bRC, FX_BOOL & bExit, FX_DWORD nFlag)
1291 {
1292         ASSERT(pPrivateData != NULL);
1293         CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
1294         ASSERT(pData->pWidget != NULL);
1295         
1296         CFFL_FormFiller* pFormFiller = GetFormFiller(pData->pWidget, FALSE);
1297         ASSERT(pFormFiller != NULL);
1298         
1299         if (pFormFiller->IsFieldFull(pData->pPageView))
1300         {
1301                 FX_BOOL bFullExit = FALSE;
1302                 FX_BOOL bFullReset = FALSE;
1303                 OnFull(pData->pWidget, pData->pPageView, bFullReset, bFullExit, nFlag);
1304
1305                 if (bFullReset || bFullExit)
1306                 {
1307                         bExit = TRUE;
1308                         return;
1309                 }
1310         }
1311         
1312         if (!m_bNotifying)
1313         {
1314                 if (pData->pWidget->GetAAction(CPDF_AAction::KeyStroke))
1315                 {
1316                         m_bNotifying = TRUE;
1317                         int nAge = pData->pWidget->GetAppearanceAge();
1318                         int nValueAge = pData->pWidget->GetValueAge();
1319
1320                         ASSERT(pData->pPageView != NULL);
1321                         CPDFSDK_Document* pDocument  = pData->pPageView->GetSDKDocument();
1322                         
1323                         PDFSDK_FieldAction fa;
1324                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
1325                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
1326                         fa.sChange = strChange;
1327                         fa.sChangeEx = strChangeEx;
1328                         fa.bKeyDown = bKeyDown;
1329                         fa.bWillCommit = FALSE;
1330                         fa.bRC = TRUE;
1331                         fa.nSelStart = nSelStart;
1332                         fa.nSelEnd = nSelEnd;
1333
1334
1335                         pFormFiller->GetActionData(pData->pPageView, CPDF_AAction::KeyStroke, fa);
1336                         pFormFiller->SaveState(pData->pPageView);
1337                         
1338                         if (pData->pWidget->OnAAction(CPDF_AAction::KeyStroke, fa, pData->pPageView))
1339                         {
1340                                 if (!IsValidAnnot(pData->pPageView, pData->pWidget))
1341                                 {
1342                                         bExit = TRUE;
1343                                         m_bNotifying = FALSE;
1344                                         return;
1345                                 }
1346                                 
1347                                 if (nAge != pData->pWidget->GetAppearanceAge())
1348                                 {
1349                                         CPWL_Wnd* pWnd = pFormFiller->ResetPDFWindow(pData->pPageView, nValueAge == pData->pWidget->GetValueAge());
1350                                         pData = (CFFL_PrivateData*)pWnd->GetAttachedData();
1351                                         bExit = TRUE;
1352                                 }
1353                                 
1354                                 if (fa.bRC)
1355                                 {
1356                                         pFormFiller->SetActionData(pData->pPageView, CPDF_AAction::KeyStroke, fa);
1357                                         bRC = FALSE;
1358                                 }
1359                                 else
1360                                 {
1361                                         pFormFiller->RestoreState(pData->pPageView);
1362                                         bRC = FALSE;
1363                                 }
1364                                 
1365                                 if (pDocument->GetFocusAnnot() != pData->pWidget)
1366                                 {
1367                                         pFormFiller->CommitData(pData->pPageView,nFlag);
1368                                         bExit = TRUE;
1369                                 }
1370                         }
1371                         else
1372                         {                       
1373                                 if (!IsValidAnnot(pData->pPageView, pData->pWidget))
1374                                 {
1375                                         bExit = TRUE;
1376                                         m_bNotifying = FALSE;
1377                                         return;
1378                                 }
1379                         }
1380                         
1381                         m_bNotifying = FALSE;
1382                 }
1383         }
1384 }
1385
1386 void    CFFL_IFormFiller::OnAfterKeyStroke(FX_BOOL bEditOrList, void* pPrivateData, FX_BOOL & bExit,FX_DWORD nFlag) 
1387 {
1388         ASSERT(pPrivateData != NULL);
1389         CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
1390         ASSERT(pData->pWidget != NULL);
1391         
1392         CFFL_FormFiller* pFormFiller = GetFormFiller(pData->pWidget, FALSE);
1393         ASSERT(pFormFiller != NULL);
1394         
1395         if (!bEditOrList)
1396                 pFormFiller->OnKeyStroke(bExit);
1397 }