Cleanup: Get this rid of "this->" in fpdfsdk/
[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         return bRet;
340 }
341
342 void CFFL_IFormFiller::OnButtonUp(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bReset, FX_BOOL& bExit,FX_UINT nFlag)
343 {
344         ASSERT(pWidget != NULL);
345         
346         if (!m_bNotifying)
347         {
348                 if (pWidget->GetAAction(CPDF_AAction::ButtonUp))
349                 {
350                         m_bNotifying = TRUE;
351                         int nAge = pWidget->GetAppearanceAge();
352                         int nValueAge = pWidget->GetValueAge();
353                         
354                         ASSERT(pPageView != NULL);
355 //                      CReader_DocView* pDocView = pPageView->GetDocView();
356 //                      ASSERT(pDocView != NULL);
357                         
358                         
359                         
360                         PDFSDK_FieldAction fa;
361                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
362                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
363
364                         pWidget->OnAAction(CPDF_AAction::ButtonUp, fa, pPageView);
365                         m_bNotifying = FALSE;
366                         
367                         if (!IsValidAnnot(pPageView, pWidget))
368                         {
369                                 bExit = TRUE;
370                                 return;
371                         }
372                         
373                         if (nAge != pWidget->GetAppearanceAge())
374                         {
375                                 if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
376                                 {
377                                         pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
378                                 }
379                                 
380                                 bReset = TRUE;
381                         }
382                 }
383         }
384 }
385
386 FX_BOOL CFFL_IFormFiller::OnLButtonDblClk(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
387 {
388         ASSERT(pAnnot != NULL);
389         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
390
391         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
392         {
393                 return pFormFiller->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
394         }
395
396         return FALSE;
397 }
398
399 FX_BOOL CFFL_IFormFiller::OnMouseMove(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
400 {
401         ASSERT(pAnnot != NULL);
402         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
403
404         //change cursor
405         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, TRUE))
406         {
407                 return pFormFiller->OnMouseMove(pPageView, pAnnot, nFlags, point);
408         }
409
410         return FALSE;
411 }
412
413 FX_BOOL CFFL_IFormFiller::OnMouseWheel(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, short zDelta, const CPDF_Point& point)
414 {
415         ASSERT(pAnnot != NULL);
416         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
417
418         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
419         {
420                 return pFormFiller->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta, point);
421         }
422
423         return FALSE;
424 }
425
426 FX_BOOL CFFL_IFormFiller::OnRButtonDown(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
427 {
428         ASSERT(pAnnot != NULL);
429         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
430
431         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
432         {
433                 return pFormFiller->OnRButtonDown(pPageView, pAnnot, nFlags, point);
434         }
435
436         return FALSE;
437 }
438
439 FX_BOOL CFFL_IFormFiller::OnRButtonUp(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
440 {
441         ASSERT(pAnnot != NULL);
442         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
443
444         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
445         {
446                 return pFormFiller->OnRButtonUp(pPageView, pAnnot, nFlags, point);
447         }
448
449         return FALSE;
450 }
451
452 FX_BOOL CFFL_IFormFiller::OnRButtonDblClk(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
453 {
454         ASSERT(pAnnot != NULL);
455         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
456
457         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
458         {
459                 return pFormFiller->OnRButtonDblClk(pPageView, pAnnot, nFlags, point);
460         }
461
462         return FALSE;
463 }
464
465 FX_BOOL CFFL_IFormFiller::OnKeyDown(CPDFSDK_Annot* pAnnot, FX_UINT nKeyCode, FX_UINT nFlags)
466 {
467         ASSERT(pAnnot != NULL);
468         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
469
470         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
471         {
472                 return pFormFiller->OnKeyDown(pAnnot, nKeyCode, nFlags);        
473         }
474
475         return FALSE;
476 }
477
478 FX_BOOL CFFL_IFormFiller::OnChar(CPDFSDK_Annot* pAnnot, FX_UINT nChar, FX_UINT nFlags)
479 {
480         ASSERT(pAnnot != NULL);
481         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
482
483         if (nChar == FWL_VKEY_Tab) return TRUE;
484
485         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
486         {
487                 return pFormFiller->OnChar(pAnnot, nChar, nFlags);
488         }
489
490         return FALSE;
491 }
492
493 void CFFL_IFormFiller::OnDeSelected(CPDFSDK_Annot* pAnnot)
494 {
495         ASSERT(pAnnot != NULL);
496         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
497
498         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
499         {
500                 pFormFiller->OnDeSelected(pAnnot);
501         }
502 }
503
504 void CFFL_IFormFiller::OnSelected(CPDFSDK_Annot* pAnnot)
505 {
506         ASSERT(pAnnot != NULL);
507         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
508
509         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
510         {
511                 pFormFiller->OnSelected(pAnnot);
512         }
513 }
514
515 FX_BOOL CFFL_IFormFiller::OnSetFocus(CPDFSDK_Annot* pAnnot,FX_UINT nFlag)
516 {
517         if(!pAnnot) return FALSE;
518         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
519
520         if (!m_bNotifying)
521         {
522                 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
523                 if (pWidget->GetAAction(CPDF_AAction::GetFocus))
524                 {
525                         m_bNotifying = TRUE;
526                         pWidget->GetAppearanceAge();
527                         int nValueAge = pWidget->GetValueAge();
528                         pWidget->ClearAppModified();
529  
530  
531                         CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
532                         ASSERT(pPageView != NULL);
533                         
534                         PDFSDK_FieldAction fa;
535                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
536                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
537
538  
539                         CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, TRUE);
540                         if(!pFormFiller) return FALSE;
541                         pFormFiller->GetActionData(pPageView, CPDF_AAction::GetFocus, fa);
542  
543                         pWidget->OnAAction(CPDF_AAction::GetFocus, fa, pPageView);
544                         m_bNotifying = FALSE;
545                         
546  //                     if (!IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pAnnot)) return FALSE;
547  
548                         if (pWidget->IsAppModified())
549                         {
550                                 if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
551                                 {
552                                         pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
553                                 }
554                         }
555                 }
556         }
557         
558         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, TRUE))
559         {
560                 if (pFormFiller->OnSetFocus(pAnnot, nFlag))
561                 {
562                         return TRUE;
563                 }
564                 else
565                         return FALSE;
566         }
567
568         return TRUE;
569 }
570
571 FX_BOOL CFFL_IFormFiller::OnKillFocus(CPDFSDK_Annot* pAnnot,FX_UINT nFlag)
572 {
573         if(!pAnnot) return FALSE;
574         ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
575
576         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
577         {
578                 if (pFormFiller->OnKillFocus(pAnnot, nFlag))
579                 {
580                         if (!m_bNotifying)
581                         {
582                                 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
583                                 if (pWidget->GetAAction(CPDF_AAction::LoseFocus))
584                                 {
585                                         m_bNotifying = TRUE;
586                                         pWidget->ClearAppModified();
587  
588                                         CPDFSDK_PageView* pPageView = pWidget->GetPageView();
589                                         ASSERT(pPageView != NULL);
590  
591                                         PDFSDK_FieldAction fa;
592                                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
593                                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
594  
595                                         pFormFiller->GetActionData(pPageView, CPDF_AAction::LoseFocus, fa);
596  
597                                         pWidget->OnAAction(CPDF_AAction::LoseFocus, fa, pPageView);
598                                         m_bNotifying = FALSE;
599  
600                                 }
601                         }
602                 }
603                 else
604                         return FALSE;
605         }
606
607         return TRUE;
608 }
609
610 FX_BOOL CFFL_IFormFiller::IsVisible(CPDFSDK_Widget* pWidget)
611 {
612         return pWidget->IsVisible();
613 }
614
615 FX_BOOL CFFL_IFormFiller::IsReadOnly(CPDFSDK_Widget* pWidget)
616 {
617         ASSERT(pWidget != NULL);
618
619         int nFieldFlags = pWidget->GetFieldFlags();
620
621         return (nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY;
622 }
623
624 FX_BOOL CFFL_IFormFiller::IsFillingAllowed(CPDFSDK_Widget* pWidget)
625 {
626         ASSERT(pWidget != NULL);
627
628         if (pWidget->GetFieldType() == FIELDTYPE_PUSHBUTTON)
629                 return TRUE;
630         else
631         {
632                 CPDF_Page* pPage = pWidget->GetPDFPage();
633                 ASSERT(pPage != NULL);
634  
635                 CPDF_Document* pDocument = pPage->m_pDocument;
636                 ASSERT(pDocument != NULL);
637  
638                 FX_DWORD dwPermissions = pDocument->GetUserPermissions();
639                 return (dwPermissions&FPDFPERM_FILL_FORM) || 
640                                 (dwPermissions&FPDFPERM_ANNOT_FORM) || 
641                         (dwPermissions&FPDFPERM_MODIFY);
642         }
643         return TRUE;    
644 }
645
646 CFFL_FormFiller* CFFL_IFormFiller::GetFormFiller(CPDFSDK_Annot* pAnnot, FX_BOOL bRegister)
647 {
648 //      ASSERT(pAnnot != NULL);
649 //      ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
650
651         CFFL_FormFiller * pFormFiller = NULL;
652         m_Maps.Lookup(pAnnot, pFormFiller);
653
654         if (pFormFiller)
655                 return pFormFiller;
656
657         if (bRegister)
658         {
659                 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;              
660
661                 int nFieldType = pWidget->GetFieldType();
662                 switch(nFieldType)
663                 {
664                 case FIELDTYPE_PUSHBUTTON:
665                         pFormFiller = new CFFL_PushButton(m_pApp, pWidget);
666                         break;
667                 case FIELDTYPE_CHECKBOX:
668                         pFormFiller = new CFFL_CheckBox(m_pApp, pWidget);
669                         break;
670                 case FIELDTYPE_RADIOBUTTON:
671                         pFormFiller = new CFFL_RadioButton(m_pApp, pWidget);
672                         break;
673                 case FIELDTYPE_TEXTFIELD:
674                         pFormFiller = new CFFL_TextField(m_pApp, pWidget);
675                         break;
676                 case FIELDTYPE_LISTBOX:
677                         pFormFiller = new CFFL_ListBox(m_pApp, pWidget);
678                         break;
679                 case FIELDTYPE_COMBOBOX:
680                         pFormFiller = new CFFL_ComboBox(m_pApp, pWidget);
681                         break;
682                 case FIELDTYPE_UNKNOWN:
683                 default:
684                         pFormFiller = NULL;
685                         break;
686                 }
687
688                 if (pFormFiller)
689                 {
690                         m_Maps.SetAt(pAnnot, pFormFiller);
691                 }
692         }
693
694         return pFormFiller;
695 }
696
697 void CFFL_IFormFiller::RemoveFormFiller(CPDFSDK_Annot* pAnnot)
698 {
699         if ( pAnnot != NULL )
700         {
701                 UnRegisterFormFiller( pAnnot );
702         }
703 }
704
705 void CFFL_IFormFiller::UnRegisterFormFiller(CPDFSDK_Annot* pAnnot)
706 {
707         CFFL_FormFiller * pFormFiller = NULL;
708
709         if (m_Maps.Lookup(pAnnot,pFormFiller))
710         {
711                 if (pFormFiller)
712                         delete pFormFiller;
713                 m_Maps.RemoveKey(pAnnot);
714         }
715 }
716
717 void CFFL_IFormFiller::SetFocusAnnotTab(CPDFSDK_Annot* pWidget, FX_BOOL bSameField, FX_BOOL bNext)
718 {
719
720 }
721
722 void CFFL_IFormFiller::QueryWherePopup(void* pPrivateData, FX_FLOAT fPopupMin,FX_FLOAT fPopupMax, int32_t & nRet, FX_FLOAT & fPopupRet)
723 {
724         ASSERT(pPrivateData != NULL);
725
726         CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
727
728         
729
730
731         CPDF_Rect rcPageView(0,0,0,0);
732         rcPageView.right = pData->pWidget->GetPDFPage()->GetPageWidth();
733         rcPageView.bottom = pData->pWidget->GetPDFPage()->GetPageHeight();
734         rcPageView.Normalize();
735
736
737         ASSERT(pData->pWidget != NULL);
738         CPDF_Rect rcAnnot = pData->pWidget->GetRect();
739
740         FX_FLOAT fTop = 0.0f;
741         FX_FLOAT fBottom = 0.0f;
742
743         CPDFSDK_Widget * pWidget = (CPDFSDK_Widget*)pData->pWidget;
744         switch (pWidget->GetRotate() / 90)
745         {
746         default:
747         case 0:
748                 fTop = rcPageView.top - rcAnnot.top;
749                 fBottom = rcAnnot.bottom - rcPageView.bottom;
750                 break;
751         case 1:
752                 fTop = rcAnnot.left - rcPageView.left;
753                 fBottom = rcPageView.right - rcAnnot.right;
754                 break;
755         case 2:
756                 fTop = rcAnnot.bottom - rcPageView.bottom;
757                 fBottom = rcPageView.top - rcAnnot.top;
758                 break;
759         case 3:
760                 fTop = rcPageView.right - rcAnnot.right;
761                 fBottom = rcAnnot.left - rcPageView.left;
762                 break;
763         }
764
765         FX_FLOAT fFactHeight = 0;
766         FX_BOOL bBottom = TRUE;
767         FX_FLOAT fMaxListBoxHeight = 0;
768         if (fPopupMax > FFL_MAXLISTBOXHEIGHT)
769         {
770                 if (fPopupMin > FFL_MAXLISTBOXHEIGHT)
771                 {
772                         fMaxListBoxHeight = fPopupMin;
773                 }
774                 else
775                 {
776                         fMaxListBoxHeight = FFL_MAXLISTBOXHEIGHT;
777                 }
778         }
779         else
780                 fMaxListBoxHeight = fPopupMax;
781
782         if (fBottom > fMaxListBoxHeight)
783         {
784                 fFactHeight = fMaxListBoxHeight;
785                 bBottom = TRUE;
786         }
787         else
788         {
789                 if (fTop > fMaxListBoxHeight)
790                 {
791                         fFactHeight = fMaxListBoxHeight;
792                         bBottom = FALSE;
793                 }
794                 else
795                 {
796                         if (fTop > fBottom)
797                         {
798                                 fFactHeight = fTop;
799                                 bBottom = FALSE;
800                         }
801                         else
802                         {
803                                 fFactHeight = fBottom;
804                                 bBottom = TRUE;
805                         }
806                 }
807         }
808
809         nRet = bBottom ? 0 : 1;
810         fPopupRet = fFactHeight;
811 }
812
813 void CFFL_IFormFiller::OnSetWindowRect(void* pPrivateData, const CPDF_Rect & rcWindow)
814 {
815         ASSERT(pPrivateData != NULL);
816
817         CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
818
819         if (CFFL_FormFiller* pFormFiller = GetFormFiller(pData->pWidget, TRUE))
820         {
821
822                 CPDF_Rect rcOld = pFormFiller->PWLtoFFL(pFormFiller->GetWindowRect(pData->pPageView));
823                 CPDF_Rect rcNew = pFormFiller->PWLtoFFL(rcWindow);
824                 pFormFiller->SetWindowRect(pData->pPageView, rcWindow);
825
826                 CPDF_Rect unRect = rcOld;
827                 unRect.Union(rcNew);
828                 //FX_RECT rcRect = unRect.GetOutterRect();
829                 unRect.left = (FX_FLOAT)(unRect.left - 0.5);
830                 unRect.right = (FX_FLOAT)(unRect.right + 0.5);
831                 unRect.top = (FX_FLOAT)(unRect.top + 0.5);
832                 unRect.bottom = (FX_FLOAT)(unRect.bottom -0.5);
833                 m_pApp->FFI_Invalidate(pData->pWidget->GetPDFPage(), unRect.left, unRect.top, unRect.right, unRect.bottom);
834         }
835 }
836
837 void CFFL_IFormFiller::OnKeyStroke(FX_BOOL bEditOrList, void* pPrivateData, int32_t nKeyCode, CFX_WideString& strChange, 
838                                                                    const CFX_WideString& strChangeEx, FX_BOOL bKeyDown, 
839                                                                    FX_BOOL & bRC, FX_BOOL & bExit)
840 {
841         ASSERT(pPrivateData != NULL);
842         CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
843         ASSERT(pData->pWidget != NULL);
844
845         CFFL_FormFiller* pFormFiller = GetFormFiller(pData->pWidget, FALSE);
846         ASSERT(pFormFiller != NULL);
847
848         pFormFiller->OnKeyStroke(bKeyDown);
849 }
850
851 void CFFL_IFormFiller::OnKeyStrokeCommit(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bRC, FX_BOOL& bExit, FX_DWORD nFlag)
852 {
853         if (!m_bNotifying)
854         {
855                 ASSERT(pWidget != NULL);
856                 if (pWidget->GetAAction(CPDF_AAction::KeyStroke))
857                 {
858                         m_bNotifying = TRUE;
859                         pWidget->ClearAppModified();
860
861                         ASSERT(pPageView != NULL);
862 //                      CReader_DocView* pDocView = pPageView->GetDocView();
863 //                      ASSERT(pDocView != NULL);
864                         
865                 
866
867                         PDFSDK_FieldAction fa;
868                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
869                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
870                         fa.bWillCommit = TRUE;
871                         fa.bKeyDown = TRUE;
872                         fa.bRC = TRUE;
873
874                         CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE);
875                         ASSERT(pFormFiller != NULL);
876
877                         pFormFiller->GetActionData(pPageView, CPDF_AAction::KeyStroke, fa);
878                         pFormFiller->SaveState(pPageView);
879
880                         PDFSDK_FieldAction faOld = fa;
881                         pWidget->OnAAction(CPDF_AAction::KeyStroke, fa, pPageView);
882
883                         bRC = fa.bRC;
884 //                      bExit = !IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pWidget);
885
886                         m_bNotifying = FALSE;
887                 }
888         }
889 }
890
891 void CFFL_IFormFiller::OnValidate(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bRC, FX_BOOL& bExit, FX_DWORD nFlag)
892 {
893         if (!m_bNotifying)
894         {
895                 ASSERT(pWidget != NULL);
896                 if (pWidget->GetAAction(CPDF_AAction::Validate))
897                 {
898                         m_bNotifying = TRUE;
899                         pWidget->ClearAppModified();
900
901                         ASSERT(pPageView != NULL);
902 //                      CReader_DocView* pDocView = pPageView->GetDocView();
903 //                      ASSERT(pDocView != NULL);
904                         
905                         
906
907                         PDFSDK_FieldAction fa;
908                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
909                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
910                         fa.bKeyDown = TRUE;
911                         fa.bRC = TRUE;
912
913                         CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE);
914                         ASSERT(pFormFiller != NULL);
915
916                         pFormFiller->GetActionData(pPageView, CPDF_AAction::Validate, fa);
917                         pFormFiller->SaveState(pPageView);
918
919                         PDFSDK_FieldAction faOld = fa;
920                         pWidget->OnAAction(CPDF_AAction::Validate, fa, pPageView);
921
922                         bRC = fa.bRC;
923 //                      bExit = !IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pWidget);
924
925                         m_bNotifying = FALSE;
926                 }
927         }
928 }
929
930 void CFFL_IFormFiller::OnCalculate(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bExit, FX_DWORD nFlag)
931 {
932         if (!m_bNotifying)
933         {
934                 ASSERT(pWidget != NULL);
935                 ASSERT(pPageView != NULL);
936 //              CReader_DocView* pDocView = pPageView->GetDocView();
937 //              ASSERT(pDocView != NULL);
938                 CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
939                 ASSERT(pDocument != NULL);
940
941                 CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
942                 ASSERT(pInterForm != NULL);
943
944                 pInterForm->OnCalculate(pWidget->GetFormField());
945
946 //              bExit = !IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pWidget);
947
948                 m_bNotifying = FALSE;
949         }
950 }
951
952 void CFFL_IFormFiller::OnFormat(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bExit, FX_DWORD nFlag)
953 {
954         if (!m_bNotifying)
955         {
956                 ASSERT(pWidget != NULL);
957                 ASSERT(pPageView != NULL);
958 //              CReader_DocView* pDocView = pPageView->GetDocView();
959 //              ASSERT(pDocView != NULL);
960                 CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
961                 ASSERT(pDocument != NULL);
962
963                 CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
964                 ASSERT(pInterForm != NULL);
965
966                 FX_BOOL bFormated = FALSE;
967                 CFX_WideString sValue = pInterForm->OnFormat(pWidget->GetFormField(), bFormated);
968
969 //              bExit = !IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pWidget);
970
971                 if (bExit) return;
972
973                 if (bFormated)
974                 {
975                         pInterForm->ResetFieldAppearance(pWidget->GetFormField(), sValue.c_str(), TRUE);
976                         pInterForm->UpdateField(pWidget->GetFormField());
977                 }
978
979                 m_bNotifying = FALSE;
980         }
981 }
982
983 FX_BOOL CFFL_IFormFiller::IsValidAnnot(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot)
984 {
985
986         ASSERT(pPageView != NULL);
987         ASSERT(pAnnot != NULL);
988
989         if(pPageView)
990                 return pPageView->IsValidAnnot(pAnnot->GetPDFAnnot());
991         else
992                 return FALSE;
993 }
994
995 void CFFL_IFormFiller::BeforeUndo(CPDFSDK_Document* pDocument)
996 {
997
998 }
999
1000 void CFFL_IFormFiller::BeforeRedo(CPDFSDK_Document* pDocument)
1001 {
1002         BeforeUndo(pDocument);
1003 }
1004
1005 void CFFL_IFormFiller::AfterUndo(CPDFSDK_Document* pDocument)
1006 {
1007 }
1008
1009 void CFFL_IFormFiller::AfterRedo(CPDFSDK_Document* pDocument)
1010 {
1011 }
1012
1013 FX_BOOL CFFL_IFormFiller::CanCopy(CPDFSDK_Document* pDocument)
1014 {
1015
1016         return FALSE;
1017 }
1018
1019 FX_BOOL CFFL_IFormFiller::CanCut(CPDFSDK_Document* pDocument)
1020 {
1021
1022         return FALSE;
1023 }
1024
1025 FX_BOOL CFFL_IFormFiller::CanPaste(CPDFSDK_Document* pDocument)
1026 {
1027
1028         return FALSE;
1029 }
1030
1031 void CFFL_IFormFiller::DoCopy(CPDFSDK_Document* pDocument)
1032 {
1033 }
1034
1035 void CFFL_IFormFiller::DoCut(CPDFSDK_Document* pDocument)
1036 {
1037 }
1038
1039 void CFFL_IFormFiller::DoPaste(CPDFSDK_Document* pDocument)
1040 {
1041
1042 }
1043 void CFFL_IFormFiller::OnBeforeKeyStroke(FX_BOOL bEditOrList, void* pPrivateData, int32_t nKeyCode,
1044                                                                                           CFX_WideString & strChange, const CFX_WideString& strChangeEx, 
1045                                                                                           int nSelStart, int nSelEnd,
1046                                                                                 FX_BOOL bKeyDown, FX_BOOL & bRC, FX_BOOL & bExit, FX_DWORD nFlag)
1047 {
1048         ASSERT(pPrivateData != NULL);
1049         CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
1050         ASSERT(pData->pWidget != NULL);
1051         
1052         CFFL_FormFiller* pFormFiller = GetFormFiller(pData->pWidget, FALSE);
1053         ASSERT(pFormFiller != NULL);
1054         
1055         if (!m_bNotifying)
1056         {
1057                 if (pData->pWidget->GetAAction(CPDF_AAction::KeyStroke))
1058                 {
1059                         m_bNotifying = TRUE;
1060                         int nAge = pData->pWidget->GetAppearanceAge();
1061                         int nValueAge = pData->pWidget->GetValueAge();
1062
1063                         ASSERT(pData->pPageView != NULL);
1064                         CPDFSDK_Document* pDocument  = pData->pPageView->GetSDKDocument();
1065                         
1066                         PDFSDK_FieldAction fa;
1067                         fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
1068                         fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
1069                         fa.sChange = strChange;
1070                         fa.sChangeEx = strChangeEx;
1071                         fa.bKeyDown = bKeyDown;
1072                         fa.bWillCommit = FALSE;
1073                         fa.bRC = TRUE;
1074                         fa.nSelStart = nSelStart;
1075                         fa.nSelEnd = nSelEnd;
1076
1077
1078                         pFormFiller->GetActionData(pData->pPageView, CPDF_AAction::KeyStroke, fa);
1079                         pFormFiller->SaveState(pData->pPageView);
1080                         
1081                         if (pData->pWidget->OnAAction(CPDF_AAction::KeyStroke, fa, pData->pPageView))
1082                         {
1083                                 if (!IsValidAnnot(pData->pPageView, pData->pWidget))
1084                                 {
1085                                         bExit = TRUE;
1086                                         m_bNotifying = FALSE;
1087                                         return;
1088                                 }
1089                                 
1090                                 if (nAge != pData->pWidget->GetAppearanceAge())
1091                                 {
1092                                         CPWL_Wnd* pWnd = pFormFiller->ResetPDFWindow(pData->pPageView, nValueAge == pData->pWidget->GetValueAge());
1093                                         pData = (CFFL_PrivateData*)pWnd->GetAttachedData();
1094                                         bExit = TRUE;
1095                                 }
1096                                 
1097                                 if (fa.bRC)
1098                                 {
1099                                         pFormFiller->SetActionData(pData->pPageView, CPDF_AAction::KeyStroke, fa);
1100                                         bRC = FALSE;
1101                                 }
1102                                 else
1103                                 {
1104                                         pFormFiller->RestoreState(pData->pPageView);
1105                                         bRC = FALSE;
1106                                 }
1107                                 
1108                                 if (pDocument->GetFocusAnnot() != pData->pWidget)
1109                                 {
1110                                         pFormFiller->CommitData(pData->pPageView,nFlag);
1111                                         bExit = TRUE;
1112                                 }
1113                         }
1114                         else
1115                         {                       
1116                                 if (!IsValidAnnot(pData->pPageView, pData->pWidget))
1117                                 {
1118                                         bExit = TRUE;
1119                                         m_bNotifying = FALSE;
1120                                         return;
1121                                 }
1122                         }
1123                         
1124                         m_bNotifying = FALSE;
1125                 }
1126         }
1127 }
1128
1129 void    CFFL_IFormFiller::OnAfterKeyStroke(FX_BOOL bEditOrList, void* pPrivateData, FX_BOOL & bExit,FX_DWORD nFlag) 
1130 {
1131         ASSERT(pPrivateData != NULL);
1132         CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
1133         ASSERT(pData->pWidget != NULL);
1134         
1135         CFFL_FormFiller* pFormFiller = GetFormFiller(pData->pWidget, FALSE);
1136         ASSERT(pFormFiller != NULL);
1137         
1138         if (!bEditOrList)
1139                 pFormFiller->OnKeyStroke(bExit);
1140 }