Replace FX_NEW with new, remove tests from fpdfsdk
[pdfium.git] / fpdfsdk / src / fsdk_baseform.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4  
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "../include/fsdk_define.h"
8 #include "../include/fsdk_mgr.h"
9 #include "../include/fsdk_baseannot.h"
10 #include "../include/fsdk_baseform.h"
11 #include "../include/formfiller/FFL_FormFiller.h"
12 #include "../include/fsdk_actionhandler.h"
13
14 #include "../include/javascript/IJavaScript.h"
15
16 //------------------------------------------------------------------------------------
17 //*                                                                             CPDFSDK_Widget 
18 //------------------------------------------------------------------------------------
19
20 #define IsFloatZero(f)                                          ((f) < 0.01 && (f) > -0.01)
21 #define IsFloatBigger(fa,fb)                            ((fa) > (fb) && !IsFloatZero((fa) - (fb)))
22 #define IsFloatSmaller(fa,fb)                           ((fa) < (fb) && !IsFloatZero((fa) - (fb)))
23 #define IsFloatEqual(fa,fb)                                     IsFloatZero((fa)-(fb))
24
25 CPDFSDK_Widget::CPDFSDK_Widget(CPDF_Annot* pAnnot, CPDFSDK_PageView* pPageView, CPDFSDK_InterForm* pInterForm) :
26                                         CPDFSDK_Annot(pAnnot, pPageView),
27                                         m_pInterForm(pInterForm),
28                                         m_nAppAge(0),
29                                         m_nValueAge(0)
30 {
31         ASSERT(m_pInterForm != NULL);
32 }
33
34 CPDFSDK_Widget::~CPDFSDK_Widget()
35 {
36
37 }
38
39 FX_BOOL         CPDFSDK_Widget::IsWidgetAppearanceValid(CPDF_Annot::AppearanceMode mode)
40 {
41         ASSERT(m_pAnnot != NULL);
42         ASSERT(m_pAnnot->m_pAnnotDict != NULL);
43         
44         CPDF_Dictionary* pAP = m_pAnnot->m_pAnnotDict->GetDict("AP");
45         if (pAP == NULL) return FALSE;
46         
47         // Choose the right sub-ap
48         const FX_CHAR* ap_entry = "N";
49         if (mode == CPDF_Annot::Down)
50                 ap_entry = "D";
51         else if (mode == CPDF_Annot::Rollover)
52                 ap_entry = "R";
53         if (!pAP->KeyExist(ap_entry))
54                 ap_entry = "N";
55         
56         // Get the AP stream or subdirectory
57         CPDF_Object* psub = pAP->GetElementValue(ap_entry);
58         if (psub == NULL) return FALSE;
59         
60         int nFieldType = GetFieldType();
61         switch (nFieldType)
62         {
63         case FIELDTYPE_PUSHBUTTON:
64         case FIELDTYPE_COMBOBOX:
65         case FIELDTYPE_LISTBOX:
66         case FIELDTYPE_TEXTFIELD:
67         case FIELDTYPE_SIGNATURE:
68                 return psub->GetType() == PDFOBJ_STREAM;
69         case FIELDTYPE_CHECKBOX:
70         case FIELDTYPE_RADIOBUTTON:
71                 if (psub->GetType() == PDFOBJ_DICTIONARY) 
72                 {
73                         CPDF_Dictionary* pSubDict = (CPDF_Dictionary*)psub;
74                         
75                         return pSubDict->GetStream(this->GetAppState()) != NULL;
76                 }
77                 else
78                         return FALSE;
79                 break;
80         }
81         
82         return TRUE;
83 }
84
85 int     CPDFSDK_Widget::GetFieldType() const
86 {
87         CPDF_FormField* pField = GetFormField();
88         ASSERT(pField != NULL);
89         
90         return pField->GetFieldType();
91 }
92
93 int CPDFSDK_Widget::GetFieldFlags() const
94 {
95         CPDF_InterForm* pPDFInterForm = m_pInterForm->GetInterForm();
96         ASSERT(pPDFInterForm != NULL);
97
98         CPDF_FormControl* pFormControl = pPDFInterForm->GetControlByDict(m_pAnnot->m_pAnnotDict);
99         CPDF_FormField* pFormField = pFormControl->GetField();
100         return pFormField->GetFieldFlags();
101 }
102
103 CFX_ByteString CPDFSDK_Widget::GetSubType() const
104 {
105         int nType = GetFieldType();
106         
107         if (nType == FIELDTYPE_SIGNATURE)
108                 return BFFT_SIGNATURE;
109         return CPDFSDK_Annot::GetSubType();
110 }
111
112 CPDF_FormField* CPDFSDK_Widget::GetFormField() const
113 {
114         ASSERT(m_pInterForm != NULL);
115         
116         CPDF_FormControl* pCtrl = GetFormControl();     
117         ASSERT(pCtrl != NULL);
118         
119         return pCtrl->GetField();
120 }
121
122 CPDF_FormControl* CPDFSDK_Widget::GetFormControl() const
123 {
124         ASSERT(m_pInterForm != NULL);
125         
126         CPDF_InterForm* pPDFInterForm = m_pInterForm->GetInterForm();
127         ASSERT(pPDFInterForm != NULL);
128         
129         return pPDFInterForm->GetControlByDict(GetAnnotDict());
130 }
131 static CPDF_Dictionary* BF_GetField(CPDF_Dictionary* pFieldDict, const FX_CHAR* name)
132 {
133         if (pFieldDict == NULL) return NULL;
134         // First check the dictionary itself
135         CPDF_Object* pAttr = pFieldDict->GetElementValue(name);
136         if (pAttr) return pFieldDict;
137         
138         // Now we need to search from parents
139         CPDF_Dictionary* pParent = pFieldDict->GetDict("Parent");
140         if (pParent == NULL) return NULL;
141         
142         return BF_GetField(pParent, name);
143 }
144
145 CPDF_FormControl* CPDFSDK_Widget::GetFormControl(CPDF_InterForm* pInterForm, CPDF_Dictionary* pAnnotDict)
146 {
147         ASSERT(pInterForm != NULL);
148         ASSERT(pAnnotDict != NULL);
149         
150         CPDF_FormControl* pControl = pInterForm->GetControlByDict(pAnnotDict);
151         
152         return pControl;
153 }
154
155 int CPDFSDK_Widget::GetRotate() const
156 {
157         CPDF_FormControl* pCtrl = this->GetFormControl();
158         ASSERT(pCtrl != NULL);
159         
160         return pCtrl->GetRotation() % 360;
161 }
162
163 FX_BOOL CPDFSDK_Widget::GetFillColor(FX_COLORREF& color) const
164 {
165         CPDF_FormControl* pFormCtrl = GetFormControl();
166         ASSERT(pFormCtrl != NULL);
167         
168         int iColorType = 0;     
169         color = FX_ARGBTOCOLORREF(pFormCtrl->GetBackgroundColor(iColorType));
170         
171         return iColorType != COLORTYPE_TRANSPARENT;
172 }
173
174 FX_BOOL CPDFSDK_Widget::GetBorderColor(FX_COLORREF& color) const
175 {
176         CPDF_FormControl* pFormCtrl = GetFormControl();
177         ASSERT(pFormCtrl != NULL);
178         
179         int iColorType = 0;     
180         color = FX_ARGBTOCOLORREF(pFormCtrl->GetBorderColor(iColorType));
181         
182         return iColorType != COLORTYPE_TRANSPARENT;
183 }
184
185 FX_BOOL CPDFSDK_Widget::GetTextColor(FX_COLORREF& color) const
186 {
187         CPDF_FormControl* pFormCtrl = GetFormControl();
188         ASSERT(pFormCtrl != NULL);
189         
190         CPDF_DefaultAppearance da = pFormCtrl->GetDefaultAppearance();
191         if (da.HasColor())
192         {
193                 FX_ARGB argb;
194                 int iColorType = COLORTYPE_TRANSPARENT; 
195                 da.GetColor(argb, iColorType);
196                 color = FX_ARGBTOCOLORREF(argb);
197                 
198                 return iColorType != COLORTYPE_TRANSPARENT;
199         }
200         
201         return FALSE;
202 }
203
204 FX_FLOAT CPDFSDK_Widget::GetFontSize() const
205 {
206         CPDF_FormControl* pFormCtrl = GetFormControl();
207         ASSERT(pFormCtrl != NULL);
208         
209         CPDF_DefaultAppearance pDa = pFormCtrl->GetDefaultAppearance();
210         CFX_ByteString csFont = "";
211         FX_FLOAT fFontSize = 0.0f;
212         pDa.GetFont(csFont, fFontSize);
213         
214         return fFontSize;
215 }
216
217 int     CPDFSDK_Widget::GetSelectedIndex(int nIndex) const
218 {
219         CPDF_FormField* pFormField = GetFormField();
220         ASSERT(pFormField != NULL);
221         
222         return pFormField->GetSelectedIndex(nIndex);
223 }
224
225 CFX_WideString CPDFSDK_Widget::GetValue() const
226 {
227         CPDF_FormField* pFormField = GetFormField();
228         ASSERT(pFormField != NULL);
229         
230         return pFormField->GetValue();
231 }
232
233 CFX_WideString CPDFSDK_Widget::GetDefaultValue() const
234 {
235         CPDF_FormField* pFormField = GetFormField();
236         ASSERT(pFormField != NULL);
237         
238         return pFormField->GetDefaultValue();
239 }
240
241 CFX_WideString CPDFSDK_Widget::GetOptionLabel(int nIndex) const
242 {
243         CPDF_FormField* pFormField = GetFormField();
244         ASSERT(pFormField != NULL);
245         
246         return pFormField->GetOptionLabel(nIndex);
247 }
248
249 int     CPDFSDK_Widget::CountOptions() const
250 {
251         CPDF_FormField* pFormField = GetFormField();
252         ASSERT(pFormField != NULL);
253         
254         return pFormField->CountOptions();
255 }
256
257 FX_BOOL CPDFSDK_Widget::IsOptionSelected(int nIndex) const
258 {
259         CPDF_FormField* pFormField = GetFormField();
260         ASSERT(pFormField != NULL);
261         
262         return pFormField->IsItemSelected(nIndex);
263 }
264
265 int     CPDFSDK_Widget::GetTopVisibleIndex() const
266 {
267         CPDF_FormField* pFormField = GetFormField();
268         ASSERT(pFormField != NULL);
269         
270         return pFormField->GetTopVisibleIndex();
271 }
272
273 FX_BOOL CPDFSDK_Widget::IsChecked() const
274 {
275         CPDF_FormControl* pFormCtrl = GetFormControl();
276         ASSERT(pFormCtrl != NULL);
277         
278         return pFormCtrl->IsChecked();
279 }
280
281 int     CPDFSDK_Widget::GetAlignment() const
282 {
283         CPDF_FormControl* pFormCtrl = GetFormControl();
284         ASSERT(pFormCtrl != NULL);
285         
286         return pFormCtrl->GetControlAlignment();
287 }
288
289 int     CPDFSDK_Widget::GetMaxLen() const
290 {
291         CPDF_FormField* pFormField = GetFormField();
292         ASSERT(pFormField != NULL);
293         
294         return pFormField->GetMaxLen();
295 }
296
297 void CPDFSDK_Widget::SetCheck(FX_BOOL bChecked, FX_BOOL bNotify)
298 {
299         CPDF_FormControl* pFormCtrl = GetFormControl();
300         ASSERT(pFormCtrl != NULL);
301         
302         CPDF_FormField* pFormField = pFormCtrl->GetField();
303         ASSERT(pFormField != NULL);
304         
305         pFormField->CheckControl(pFormField->GetControlIndex(pFormCtrl), bChecked, bNotify);
306 }
307
308 void CPDFSDK_Widget::SetValue(const CFX_WideString& sValue, FX_BOOL bNotify)
309 {
310         CPDF_FormField* pFormField = GetFormField();
311         ASSERT(pFormField != NULL);
312         
313         pFormField->SetValue(sValue, bNotify);
314 }
315
316 void CPDFSDK_Widget::SetDefaultValue(const CFX_WideString& sValue)
317 {
318 }
319 void CPDFSDK_Widget::SetOptionSelection(int index, FX_BOOL bSelected, FX_BOOL bNotify)
320 {
321         CPDF_FormField* pFormField = GetFormField();
322         ASSERT(pFormField != NULL);
323         
324         pFormField->SetItemSelection(index, bSelected, bNotify);
325 }
326
327 void CPDFSDK_Widget::ClearSelection(FX_BOOL bNotify)
328 {
329         CPDF_FormField* pFormField = GetFormField();
330         ASSERT(pFormField != NULL);
331         
332         pFormField->ClearSelection(bNotify);
333 }
334
335 void CPDFSDK_Widget::SetTopVisibleIndex(int index)
336 {
337 }
338
339 void CPDFSDK_Widget::SetAppModified()
340 {
341         m_bAppModified = TRUE;
342 }
343
344 void CPDFSDK_Widget::ClearAppModified()
345 {
346         m_bAppModified = FALSE;
347 }
348
349 FX_BOOL CPDFSDK_Widget::IsAppModified() const
350 {
351         return m_bAppModified;
352 }
353
354 void CPDFSDK_Widget::ResetAppearance(FX_LPCWSTR sValue, FX_BOOL bValueChanged)
355 {
356         SetAppModified();
357
358         m_nAppAge++;
359         if (m_nAppAge > 999999)
360                 m_nAppAge = 0;
361         if (bValueChanged)
362                 m_nValueAge++;
363
364         int nFieldType = GetFieldType();
365         
366         switch (nFieldType)
367         {
368         case FIELDTYPE_PUSHBUTTON:
369                 ResetAppearance_PushButton();
370                 break;
371         case FIELDTYPE_CHECKBOX:
372                 ResetAppearance_CheckBox();
373                 break;
374         case FIELDTYPE_RADIOBUTTON:
375                 ResetAppearance_RadioButton();
376                 break;
377         case FIELDTYPE_COMBOBOX:
378                 ResetAppearance_ComboBox(sValue);
379                 break;
380         case FIELDTYPE_LISTBOX:
381                 ResetAppearance_ListBox();
382                 break;
383         case FIELDTYPE_TEXTFIELD:
384                 ResetAppearance_TextField(sValue);
385                 break;
386         }
387         
388         ASSERT(m_pAnnot != NULL);
389         m_pAnnot->ClearCachedAP();
390 }
391
392 CFX_WideString CPDFSDK_Widget::OnFormat(int nCommitKey, FX_BOOL& bFormated)
393 {
394         CPDF_FormField* pFormField = GetFormField();
395         ASSERT(pFormField != NULL);
396         
397         ASSERT(m_pInterForm != NULL);
398         
399         return m_pInterForm->OnFormat(pFormField, nCommitKey, bFormated);
400
401 }
402
403 void CPDFSDK_Widget::ResetFieldAppearance(FX_BOOL bValueChanged)
404 {
405         CPDF_FormField* pFormField = GetFormField();
406         ASSERT(pFormField != NULL);
407         
408         ASSERT(m_pInterForm != NULL);
409
410         m_pInterForm->ResetFieldAppearance(pFormField, NULL, bValueChanged);
411 }
412
413 void    CPDFSDK_Widget::DrawAppearance(CFX_RenderDevice* pDevice, const CPDF_Matrix* pUser2Device,
414                 CPDF_Annot::AppearanceMode mode, const CPDF_RenderOptions* pOptions)
415 {
416         int nFieldType = GetFieldType();
417         
418         if ((nFieldType == FIELDTYPE_CHECKBOX || nFieldType == FIELDTYPE_RADIOBUTTON) &&
419                 mode == CPDF_Annot::Normal && 
420                 !this->IsWidgetAppearanceValid(CPDF_Annot::Normal))
421         {
422                 CFX_PathData pathData;
423                 
424                 CPDF_Rect rcAnnot = this->GetRect();
425                 
426                 pathData.AppendRect(rcAnnot.left, rcAnnot.bottom,
427                         rcAnnot.right, rcAnnot.top);
428                 
429                 CFX_GraphStateData gsd;
430                 gsd.m_LineWidth = 0.0f;
431                 
432                 pDevice->DrawPath(&pathData, pUser2Device, &gsd, 0, 0xFFAAAAAA, FXFILL_ALTERNATE);
433         }
434         else
435         {
436                 CPDFSDK_Annot::DrawAppearance(pDevice, pUser2Device, mode, pOptions);
437         }
438 }
439
440 void CPDFSDK_Widget::UpdateField()
441 {
442         CPDF_FormField* pFormField = GetFormField();
443         ASSERT(pFormField != NULL);
444         
445         ASSERT(m_pInterForm != NULL);
446         m_pInterForm->UpdateField(pFormField);
447 }
448
449 void CPDFSDK_Widget::DrawShadow(CFX_RenderDevice* pDevice, CPDFSDK_PageView* pPageView)
450 {
451         ASSERT(m_pInterForm != NULL);
452  
453         int nFieldType = GetFieldType();
454         if (m_pInterForm->IsNeedHighLight(nFieldType))
455         {
456  
457 //              if (nFieldType != FIELDTYPE_PUSHBUTTON)
458 //              {
459                         CPDF_Rect rc  = GetRect();
460                         FX_COLORREF color = m_pInterForm->GetHighlightColor(nFieldType);
461                         FX_BYTE alpha = m_pInterForm->GetHighlightAlpha();
462
463                         CFX_FloatRect rcDevice;
464                         ASSERT(m_pInterForm->GetDocument());
465                         CPDFDoc_Environment* pEnv = m_pInterForm->GetDocument()->GetEnv();
466                         if(!pEnv)
467                                 return;
468                         CFX_AffineMatrix page2device;
469                         pPageView->GetCurrentMatrix(page2device);
470                         page2device.Transform(((FX_FLOAT)rc.left), ((FX_FLOAT)rc.bottom), rcDevice.left, rcDevice.bottom);
471 //                      pEnv->FFI_PageToDevice(m_pPageView->GetPDFPage(), rc.left, rc.bottom, &rcDevice.left, &rcDevice.bottom);
472 //                      pEnv->FFI_PageToDevice(m_pPageView->GetPDFPage(), rc.right, rc.top, &rcDevice.right, &rcDevice.top);
473                         page2device.Transform(((FX_FLOAT)rc.right), ((FX_FLOAT)rc.top), rcDevice.right, rcDevice.top);
474
475                         rcDevice.Normalize();
476
477                         FX_ARGB argb = ArgbEncode((int)alpha, color);
478                         FX_RECT rcDev((int)rcDevice.left,(int)rcDevice.top,(int)rcDevice.right,(int)rcDevice.bottom);
479                         pDevice->FillRect(&rcDev, argb);        
480                         /*              }*/
481         }
482 }
483
484 void CPDFSDK_Widget::ResetAppearance_PushButton()
485 {
486         CPDF_FormControl* pControl = GetFormControl();
487         ASSERT(pControl != NULL);
488
489
490         
491         CPDF_Rect rcWindow = GetRotatedRect();  
492
493         FX_INT32 nLayout = 0;
494
495         switch (pControl->GetTextPosition())
496         {
497         case TEXTPOS_ICON:
498                 nLayout = PPBL_ICON;
499                 break;
500         case TEXTPOS_BELOW:
501                 nLayout = PPBL_ICONTOPLABELBOTTOM;
502                 break;
503         case TEXTPOS_ABOVE:
504                 nLayout = PPBL_LABELTOPICONBOTTOM;
505                 break;
506         case TEXTPOS_RIGHT:
507                 nLayout = PPBL_ICONLEFTLABELRIGHT;
508                 break;
509         case TEXTPOS_LEFT:
510                 nLayout = PPBL_LABELLEFTICONRIGHT;
511                 break;
512         case TEXTPOS_OVERLAID:
513                 nLayout = PPBL_LABELOVERICON;
514                 break;
515         default:
516                 nLayout = PPBL_LABEL;
517                 break;
518         }
519
520         CPWL_Color crBackground, crBorder;
521
522         int iColorType;
523         FX_FLOAT fc[4];
524
525         pControl->GetOriginalBackgroundColor(iColorType, fc);
526         if (iColorType > 0)
527                 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
528
529         pControl->GetOriginalBorderColor(iColorType, fc);
530         if (iColorType > 0)
531                 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
532
533         FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
534         FX_INT32 nBorderStyle = 0;
535         CPWL_Dash dsBorder(3,0,0);
536         CPWL_Color crLeftTop,crRightBottom;
537
538         switch (GetBorderStyle())
539         {
540         case BBS_DASH:
541                 nBorderStyle = PBS_DASH;
542                 dsBorder = CPWL_Dash(3, 3, 0);
543                 break;
544         case BBS_BEVELED:
545                 nBorderStyle = PBS_BEVELED;
546                 fBorderWidth *= 2;
547                 crLeftTop = CPWL_Color(COLORTYPE_GRAY,1);
548                 crRightBottom = CPWL_Utils::DevideColor(crBackground,2);
549                 break;
550         case BBS_INSET:
551                 nBorderStyle = PBS_INSET;
552                 fBorderWidth *= 2;
553                 crLeftTop = CPWL_Color(COLORTYPE_GRAY,0.5);
554                 crRightBottom = CPWL_Color(COLORTYPE_GRAY,0.75);
555                 break;
556         case BBS_UNDERLINE:
557                 nBorderStyle = PBS_UNDERLINED;
558                 break;
559         default: 
560                 nBorderStyle = PBS_SOLID;
561                 break;
562         }
563
564         CPDF_Rect rcClient = CPWL_Utils::DeflateRect(rcWindow,fBorderWidth);    
565
566         CPWL_Color crText(COLORTYPE_GRAY,0);
567
568         FX_FLOAT fFontSize = 12.0f;
569         CFX_ByteString csNameTag;
570
571         CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
572         if (da.HasColor())
573         {
574                 da.GetColor(iColorType, fc);
575                 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
576         }
577
578         if (da.HasFont()) 
579                 da.GetFont(csNameTag, fFontSize);
580
581         CFX_WideString csWCaption;
582         CFX_WideString csNormalCaption, csRolloverCaption, csDownCaption;
583
584         if (pControl->HasMKEntry("CA"))
585         {
586                 csNormalCaption = pControl->GetNormalCaption();
587         }
588         if (pControl->HasMKEntry("RC"))
589         {
590                 csRolloverCaption = pControl->GetRolloverCaption();
591         }
592         if (pControl->HasMKEntry("AC"))
593         {
594                 csDownCaption = pControl->GetDownCaption();
595         }
596
597         CPDF_Stream* pNormalIcon = NULL;
598         CPDF_Stream* pRolloverIcon = NULL;
599         CPDF_Stream* pDownIcon = NULL;
600
601         if (pControl->HasMKEntry("I"))
602         {
603                 pNormalIcon = pControl->GetNormalIcon();
604         }
605         if (pControl->HasMKEntry("RI"))
606         {
607                 pRolloverIcon = pControl->GetRolloverIcon();
608         }
609         if (pControl->HasMKEntry("IX"))
610         {
611                 pDownIcon = pControl->GetDownIcon();
612         }
613
614         if (pNormalIcon)
615         {
616                 if (CPDF_Dictionary* pImageDict = pNormalIcon->GetDict())
617                 {
618                         if (pImageDict->GetString("Name").IsEmpty())
619                                 pImageDict->SetAtString("Name", "ImgA");
620                 }
621         }
622
623         if (pRolloverIcon)
624         {
625                 if (CPDF_Dictionary* pImageDict = pRolloverIcon->GetDict())
626                 {
627                         if (pImageDict->GetString("Name").IsEmpty())
628                                 pImageDict->SetAtString("Name", "ImgB");
629                 }
630         }
631
632         if (pDownIcon)
633         {
634                 if (CPDF_Dictionary* pImageDict = pDownIcon->GetDict())
635                 {
636                         if (pImageDict->GetString("Name").IsEmpty())
637                                 pImageDict->SetAtString("Name", "ImgC");
638                 }
639         }
640
641         CPDF_IconFit iconFit = pControl->GetIconFit();
642
643 //      ASSERT(this->m_pBaseForm != NULL);
644         ASSERT(this->m_pInterForm != NULL);
645         CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
646         ASSERT(pDoc != NULL);
647         CPDFDoc_Environment* pEnv = pDoc->GetEnv();
648
649         CBA_FontMap FontMap(this,pEnv->GetSysHandler());//, ISystemHandle::GetSystemHandler(m_pBaseForm->GetEnv()));
650         FontMap.Initial();
651
652         FontMap.SetAPType("N");
653
654         CFX_ByteString csAP = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) + 
655                 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder, crLeftTop, crRightBottom, nBorderStyle, dsBorder) +
656                 CPWL_Utils::GetPushButtonAppStream(iconFit.GetFittingBounds() ? rcWindow : rcClient, &FontMap, pNormalIcon, iconFit, csNormalCaption, crText, fFontSize, nLayout);
657
658         WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP);
659         if (pNormalIcon)
660                 AddImageToAppearance("N", pNormalIcon);
661
662         CPDF_FormControl::HighlightingMode eHLM = pControl->GetHighlightingMode();
663         if (eHLM == CPDF_FormControl::Push || eHLM == CPDF_FormControl::Toggle)
664         {
665                 if (csRolloverCaption.IsEmpty() && !pRolloverIcon)                      
666                 {
667                         csRolloverCaption = csNormalCaption;
668                         pRolloverIcon = pNormalIcon;
669                 }
670
671                 FontMap.SetAPType("R");
672
673                 csAP = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) + 
674                                 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder, crLeftTop, crRightBottom, nBorderStyle, dsBorder) +
675                                 CPWL_Utils::GetPushButtonAppStream(iconFit.GetFittingBounds() ? rcWindow : rcClient, &FontMap, pRolloverIcon, iconFit, csRolloverCaption, crText, fFontSize, nLayout);
676
677                 WriteAppearance("R", GetRotatedRect(), GetMatrix(), csAP);
678                 if (pRolloverIcon)
679                         AddImageToAppearance("R", pRolloverIcon);
680
681                 if (csDownCaption.IsEmpty() && !pDownIcon)
682                 {
683                         csDownCaption = csNormalCaption;
684                         pDownIcon = pNormalIcon;
685                 }
686
687                 switch (nBorderStyle)
688                 {
689                 case PBS_BEVELED:
690                         {
691                                 CPWL_Color crTemp = crLeftTop;
692                                 crLeftTop = crRightBottom;
693                                 crRightBottom = crTemp;
694                         }
695                         break;
696                 case PBS_INSET:
697                         crLeftTop = CPWL_Color(COLORTYPE_GRAY,0);
698                         crRightBottom = CPWL_Color(COLORTYPE_GRAY,1);
699                         break;
700                 }
701                 
702                 FontMap.SetAPType("D");
703
704                 csAP = CPWL_Utils::GetRectFillAppStream(rcWindow, CPWL_Utils::SubstractColor(crBackground,0.25f)) + 
705                         CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder, crLeftTop, crRightBottom, nBorderStyle, dsBorder) + 
706                         CPWL_Utils::GetPushButtonAppStream(iconFit.GetFittingBounds() ? rcWindow : rcClient, &FontMap, pDownIcon, iconFit, csDownCaption, crText, fFontSize, nLayout);
707
708                 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP);
709                 if (pDownIcon)
710                         AddImageToAppearance("D", pDownIcon);
711         }
712         else
713         {
714                 RemoveAppearance("D");
715                 RemoveAppearance("R");
716         }
717 }
718
719 void CPDFSDK_Widget::ResetAppearance_CheckBox()
720 {
721         CPDF_FormControl* pControl = GetFormControl();
722         ASSERT(pControl != NULL);
723
724
725
726         CPWL_Color crBackground, crBorder, crText;
727         
728         int iColorType;
729         FX_FLOAT fc[4];
730
731         pControl->GetOriginalBackgroundColor(iColorType, fc);
732         if (iColorType > 0)
733                 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
734
735         pControl->GetOriginalBorderColor(iColorType, fc);
736         if (iColorType > 0)
737                 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
738
739         FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
740         FX_INT32 nBorderStyle = 0;
741         CPWL_Dash dsBorder(3,0,0);
742         CPWL_Color crLeftTop,crRightBottom;
743
744         switch (GetBorderStyle())
745         {
746         case BBS_DASH:
747                 nBorderStyle = PBS_DASH;
748                 dsBorder = CPWL_Dash(3, 3, 0);
749                 break;
750         case BBS_BEVELED:
751                 nBorderStyle = PBS_BEVELED;
752                 fBorderWidth *= 2;
753                 crLeftTop = CPWL_Color(COLORTYPE_GRAY,1);
754                 crRightBottom = CPWL_Utils::DevideColor(crBackground,2);
755                 break;
756         case BBS_INSET:
757                 nBorderStyle = PBS_INSET;
758                 fBorderWidth *= 2;
759                 crLeftTop = CPWL_Color(COLORTYPE_GRAY,0.5);
760                 crRightBottom = CPWL_Color(COLORTYPE_GRAY,0.75);
761                 break;
762         case BBS_UNDERLINE:
763                 nBorderStyle = PBS_UNDERLINED;
764                 break;
765         default: 
766                 nBorderStyle = PBS_SOLID;
767                 break;
768         }
769
770         CPDF_Rect rcWindow = GetRotatedRect();
771         CPDF_Rect rcClient = CPWL_Utils::DeflateRect(rcWindow,fBorderWidth);
772
773         CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
774         if (da.HasColor())
775         {
776                 da.GetColor(iColorType, fc);
777                 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
778         }
779
780         FX_INT32 nStyle = 0;
781
782         CFX_WideString csWCaption = pControl->GetNormalCaption();
783         if (csWCaption.GetLength() > 0)
784         {
785                 switch (csWCaption[0])
786                 {
787                 case L'l':
788                         nStyle = PCS_CIRCLE;                    
789                         break;
790                 case L'8':
791                         nStyle = PCS_CROSS;
792                         break;
793                 case L'u':
794                         nStyle = PCS_DIAMOND;
795                         break;
796                 case L'n':
797                         nStyle = PCS_SQUARE;
798                         break;
799                 case L'H':
800                         nStyle = PCS_STAR;
801                         break;
802                 default: //L'4'
803                         nStyle = PCS_CHECK;
804                         break;
805                 }
806         }
807         else
808         {
809                 nStyle = PCS_CHECK;
810         }
811
812         CFX_ByteString csAP_N_ON = CPWL_Utils::GetRectFillAppStream(rcWindow,crBackground) +
813                 CPWL_Utils::GetBorderAppStream(rcWindow,fBorderWidth,crBorder,crLeftTop,crRightBottom,nBorderStyle,dsBorder);
814
815         CFX_ByteString csAP_N_OFF = csAP_N_ON;
816
817         switch (nBorderStyle)
818         {
819         case PBS_BEVELED:
820                 {
821                         CPWL_Color crTemp = crLeftTop;
822                         crLeftTop = crRightBottom;
823                         crRightBottom = crTemp;
824                 }
825                 break;
826         case PBS_INSET:
827                 crLeftTop = CPWL_Color(COLORTYPE_GRAY,0);
828                 crRightBottom = CPWL_Color(COLORTYPE_GRAY,1);
829                 break;
830         }
831
832         CFX_ByteString csAP_D_ON = CPWL_Utils::GetRectFillAppStream(rcWindow,CPWL_Utils::SubstractColor(crBackground,0.25f)) + 
833                 CPWL_Utils::GetBorderAppStream(rcWindow,fBorderWidth,crBorder,crLeftTop,crRightBottom,nBorderStyle,dsBorder);
834
835         CFX_ByteString csAP_D_OFF = csAP_D_ON;
836
837         csAP_N_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient,nStyle,crText);
838         csAP_D_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient,nStyle,crText);
839
840         WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON, pControl->GetCheckedAPState());
841         WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
842
843         WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON, pControl->GetCheckedAPState());
844         WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
845
846         CFX_ByteString csAS = GetAppState();
847         if (csAS.IsEmpty())
848                 SetAppState("Off");
849 }
850
851 void CPDFSDK_Widget::ResetAppearance_RadioButton()
852 {
853         CPDF_FormControl* pControl = GetFormControl();
854         ASSERT(pControl != NULL);
855         
856
857
858         CPWL_Color crBackground, crBorder, crText;
859         
860         int iColorType;
861         FX_FLOAT fc[4];
862
863         pControl->GetOriginalBackgroundColor(iColorType, fc);
864         if (iColorType > 0)
865                 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
866
867         pControl->GetOriginalBorderColor(iColorType, fc);
868         if (iColorType > 0)
869                 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
870
871         FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
872         FX_INT32 nBorderStyle = 0;
873         CPWL_Dash dsBorder(3,0,0);
874         CPWL_Color crLeftTop,crRightBottom;
875
876         switch (GetBorderStyle())
877         {
878         case BBS_DASH:
879                 nBorderStyle = PBS_DASH;
880                 dsBorder = CPWL_Dash(3, 3, 0);
881                 break;
882         case BBS_BEVELED:
883                 nBorderStyle = PBS_BEVELED;
884                 fBorderWidth *= 2;
885                 crLeftTop = CPWL_Color(COLORTYPE_GRAY,1);
886                 crRightBottom = CPWL_Utils::DevideColor(crBackground,2);
887                 break;
888         case BBS_INSET:
889                 nBorderStyle = PBS_INSET;
890                 fBorderWidth *= 2;
891                 crLeftTop = CPWL_Color(COLORTYPE_GRAY,0.5);
892                 crRightBottom = CPWL_Color(COLORTYPE_GRAY,0.75);
893                 break;
894         case BBS_UNDERLINE:
895                 nBorderStyle = PBS_UNDERLINED;
896                 break;
897         default: 
898                 nBorderStyle = PBS_SOLID;
899                 break;
900         }
901
902         CPDF_Rect rcWindow = GetRotatedRect();
903         CPDF_Rect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
904
905         CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
906         if (da.HasColor())
907         {
908                 da.GetColor(iColorType, fc);
909                 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
910         }
911
912         FX_INT32 nStyle = 0;
913
914         CFX_WideString csWCaption = pControl->GetNormalCaption();
915         if (csWCaption.GetLength() > 0)
916         {
917                 switch (csWCaption[0])
918                 {
919                 default: //L'l':
920                         nStyle = PCS_CIRCLE;                    
921                         break;
922                 case L'8':
923                         nStyle = PCS_CROSS;
924                         break;
925                 case L'u':
926                         nStyle = PCS_DIAMOND;
927                         break;
928                 case L'n':
929                         nStyle = PCS_SQUARE;
930                         break;
931                 case L'H':
932                         nStyle = PCS_STAR;
933                         break;
934                 case L'4':
935                         nStyle = PCS_CHECK;
936                         break;
937                 }
938         }
939         else
940         {
941                 nStyle = PCS_CIRCLE;
942         }
943
944         CFX_ByteString csAP_N_ON;
945
946         CPDF_Rect rcCenter = CPWL_Utils::DeflateRect(CPWL_Utils::GetCenterSquare(rcWindow), 1.0f);
947         
948         if (nStyle == PCS_CIRCLE)
949         {
950                 if (nBorderStyle == PBS_BEVELED)
951                 {
952                         crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
953                         crRightBottom = CPWL_Utils::SubstractColor(crBackground,0.25f);
954                 }
955                 else if (nBorderStyle == PBS_INSET)
956                 {
957                         crLeftTop = CPWL_Color(COLORTYPE_GRAY,0.5f);
958                         crRightBottom = CPWL_Color(COLORTYPE_GRAY,0.75f);
959                 }
960
961                 csAP_N_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter,crBackground) + 
962                         CPWL_Utils::GetCircleBorderAppStream(rcCenter,fBorderWidth,crBorder,crLeftTop,crRightBottom,nBorderStyle,dsBorder);
963         }
964         else
965         {
966                 csAP_N_ON = CPWL_Utils::GetRectFillAppStream(rcWindow,crBackground) + 
967                         CPWL_Utils::GetBorderAppStream(rcWindow,fBorderWidth,crBorder,crLeftTop,crRightBottom,nBorderStyle,dsBorder);
968         }
969
970         CFX_ByteString csAP_N_OFF = csAP_N_ON;
971
972         switch (nBorderStyle)
973         {
974         case PBS_BEVELED:
975                 {
976                         CPWL_Color crTemp = crLeftTop;
977                         crLeftTop = crRightBottom;
978                         crRightBottom = crTemp;
979                 }
980                 break;
981         case PBS_INSET:
982                 crLeftTop = CPWL_Color(COLORTYPE_GRAY,0);
983                 crRightBottom = CPWL_Color(COLORTYPE_GRAY,1);
984                 break;
985         }
986
987         CFX_ByteString csAP_D_ON;
988
989         if (nStyle == PCS_CIRCLE)
990         {
991                 CPWL_Color crBK = CPWL_Utils::SubstractColor(crBackground,0.25f);
992                 if (nBorderStyle == PBS_BEVELED)
993                 {
994                         crLeftTop = CPWL_Utils::SubstractColor(crBackground,0.25f);
995                         crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
996                         crBK = crBackground;
997                 }
998                 else if (nBorderStyle == PBS_INSET)
999                 {
1000                         crLeftTop = CPWL_Color(COLORTYPE_GRAY,0);
1001                         crRightBottom = CPWL_Color(COLORTYPE_GRAY,1);
1002                 }
1003
1004                 csAP_D_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter,crBK)
1005                         + CPWL_Utils::GetCircleBorderAppStream(rcCenter,fBorderWidth,crBorder,crLeftTop,crRightBottom,nBorderStyle,dsBorder);
1006         }
1007         else
1008         {
1009                 csAP_D_ON = CPWL_Utils::GetRectFillAppStream(rcWindow,CPWL_Utils::SubstractColor(crBackground,0.25f)) + 
1010                         CPWL_Utils::GetBorderAppStream(rcWindow,fBorderWidth,crBorder,crLeftTop,crRightBottom,nBorderStyle,dsBorder);           
1011         }
1012
1013         CFX_ByteString csAP_D_OFF = csAP_D_ON;
1014
1015         csAP_N_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient,nStyle,crText);
1016         csAP_D_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient,nStyle,crText);
1017
1018         WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON, pControl->GetCheckedAPState());
1019         WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
1020
1021         WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON, pControl->GetCheckedAPState());
1022         WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
1023
1024         CFX_ByteString csAS = GetAppState();
1025         if (csAS.IsEmpty())
1026                 SetAppState("Off");
1027 }
1028
1029 void CPDFSDK_Widget::ResetAppearance_ComboBox(FX_LPCWSTR sValue)
1030 {
1031         CPDF_FormControl* pControl = GetFormControl();
1032         ASSERT(pControl != NULL);
1033         CPDF_FormField* pField = pControl->GetField();
1034         ASSERT(pField != NULL);
1035
1036         CFX_ByteTextBuf sBody, sLines;
1037
1038         CPDF_Rect rcClient = GetClientRect();
1039         CPDF_Rect rcButton = rcClient;
1040         rcButton.left = rcButton.right - 13;
1041         rcButton.Normalize();
1042
1043         if (IFX_Edit * pEdit = IFX_Edit::NewEdit())
1044         {
1045                 pEdit->EnableRefresh(FALSE);
1046
1047                 ASSERT(this->m_pInterForm != NULL);
1048                 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1049                 ASSERT(pDoc != NULL);
1050                 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
1051                 CBA_FontMap FontMap(this,pEnv->GetSysHandler());
1052                 FontMap.Initial();
1053                 pEdit->SetFontMap(&FontMap);
1054
1055                 CPDF_Rect rcEdit = rcClient;
1056                 rcEdit.right = rcButton.left;
1057                 rcEdit.Normalize();
1058                 
1059                 pEdit->SetPlateRect(rcEdit);
1060                 pEdit->SetAlignmentV(1);
1061
1062                 FX_FLOAT fFontSize = this->GetFontSize();
1063                 if (IsFloatZero(fFontSize))
1064                         pEdit->SetAutoFontSize(TRUE);
1065                 else
1066                         pEdit->SetFontSize(fFontSize);
1067                 
1068                 pEdit->Initialize();
1069                 
1070                 if (sValue)
1071                         pEdit->SetText(sValue);
1072                 else
1073                 {
1074                         FX_INT32 nCurSel = pField->GetSelectedIndex(0);
1075
1076                         if (nCurSel < 0)
1077                                 pEdit->SetText(pField->GetValue().c_str());
1078                         else
1079                                 pEdit->SetText(pField->GetOptionLabel(nCurSel).c_str());
1080                 }
1081
1082                 CPDF_Rect rcContent = pEdit->GetContentRect();
1083
1084                 CFX_ByteString sEdit = CPWL_Utils::GetEditAppStream(pEdit,CPDF_Point(0.0f,0.0f));
1085                 if (sEdit.GetLength() > 0)
1086                 {
1087                         sBody << "/Tx BMC\n" << "q\n";
1088                         if (rcContent.Width() > rcEdit.Width() ||
1089                                 rcContent.Height() > rcEdit.Height())
1090                         {
1091                                 sBody << rcEdit.left << " " << rcEdit.bottom << " " 
1092                                         << rcEdit.Width() << " " << rcEdit.Height() << " re\nW\nn\n";
1093                         }
1094
1095                         CPWL_Color crText = GetTextPWLColor();  
1096                         sBody << "BT\n" << CPWL_Utils::GetColorAppStream(crText) << sEdit << "ET\n" << "Q\nEMC\n";
1097                 }
1098
1099                 IFX_Edit::DelEdit(pEdit);
1100         }
1101
1102         sBody << CPWL_Utils::GetDropButtonAppStream(rcButton);
1103
1104         CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() + sLines.GetByteString() + sBody.GetByteString();
1105
1106         WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
1107 }
1108
1109 void CPDFSDK_Widget::ResetAppearance_ListBox()
1110 {
1111         CPDF_FormControl* pControl = GetFormControl();
1112         ASSERT(pControl != NULL);
1113         CPDF_FormField* pField = pControl->GetField();
1114         ASSERT(pField != NULL);
1115
1116         CPDF_Rect rcClient = GetClientRect();
1117
1118         CFX_ByteTextBuf sBody, sLines;
1119
1120         if (IFX_Edit * pEdit = IFX_Edit::NewEdit())
1121         {
1122                 pEdit->EnableRefresh(FALSE);
1123
1124 //              ASSERT(this->m_pBaseForm != NULL);
1125                 ASSERT(this->m_pInterForm != NULL);
1126                 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1127                 ASSERT(pDoc != NULL);
1128                 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
1129
1130                 CBA_FontMap FontMap(this,pEnv->GetSysHandler());
1131                 FontMap.Initial();
1132                 pEdit->SetFontMap(&FontMap);
1133
1134                 pEdit->SetPlateRect(CPDF_Rect(rcClient.left,0.0f,rcClient.right,0.0f)); 
1135                 
1136                 FX_FLOAT fFontSize = GetFontSize();
1137
1138                 if (IsFloatZero(fFontSize))
1139                         pEdit->SetFontSize(12.0f);
1140                 else
1141                         pEdit->SetFontSize(fFontSize);
1142                 
1143                 pEdit->Initialize();
1144
1145                 CFX_ByteTextBuf sList;
1146                 FX_FLOAT fy = rcClient.top;
1147
1148                 FX_INT32 nTop = pField->GetTopVisibleIndex();
1149                 FX_INT32 nCount = pField->CountOptions();
1150                 FX_INT32 nSelCount = pField->CountSelectedItems();
1151
1152                 for (FX_INT32 i=nTop; i<nCount; i++)
1153                 {
1154                         FX_BOOL bSelected = FALSE;                              
1155                         for (FX_INT32 j=0; j<nSelCount; j++)
1156                         {
1157                                 if (pField->GetSelectedIndex(j) == i)
1158                                 {
1159                                         bSelected = TRUE;
1160                                         break;
1161                                 }
1162                         }
1163
1164                         pEdit->SetText(pField->GetOptionLabel(i).c_str());
1165
1166                         CPDF_Rect rcContent = pEdit->GetContentRect();
1167                         FX_FLOAT fItemHeight = rcContent.Height();
1168
1169                         if (bSelected)
1170                         {
1171                                 CPDF_Rect rcItem = CPDF_Rect(rcClient.left,fy-fItemHeight,rcClient.right,fy);
1172                                 sList << "q\n" << CPWL_Utils::GetColorAppStream(CPWL_Color(COLORTYPE_RGB,0,51.0f/255.0f,113.0f/255.0f),TRUE)
1173                                         << rcItem.left << " " << rcItem.bottom << " " << rcItem.Width() << " " << rcItem.Height() << " re f\n" << "Q\n";
1174
1175                                 sList << "BT\n" << CPWL_Utils::GetColorAppStream(CPWL_Color(COLORTYPE_GRAY,1),TRUE) << 
1176                                         CPWL_Utils::GetEditAppStream(pEdit,CPDF_Point(0.0f,fy)) << "ET\n";
1177                         }
1178                         else
1179                         {
1180                                 CPWL_Color crText = GetTextPWLColor();
1181                                 sList << "BT\n" << CPWL_Utils::GetColorAppStream(crText,TRUE) << 
1182                                 CPWL_Utils::GetEditAppStream(pEdit,CPDF_Point(0.0f,fy)) << "ET\n";
1183                         }
1184
1185                         fy -= fItemHeight;
1186                 }
1187                                         
1188                 if (sList.GetSize() > 0)
1189                 {
1190                         sBody << "/Tx BMC\n" << "q\n" << rcClient.left << " " << rcClient.bottom << " " 
1191                                         << rcClient.Width() << " " << rcClient.Height() << " re\nW\nn\n";
1192                         sBody << sList << "Q\nEMC\n";
1193                 }
1194
1195                 IFX_Edit::DelEdit(pEdit);
1196         }
1197
1198         CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() + sLines.GetByteString() + sBody.GetByteString();
1199
1200         WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
1201 }
1202
1203 void CPDFSDK_Widget::ResetAppearance_TextField(FX_LPCWSTR sValue)
1204 {
1205         CPDF_FormControl* pControl = GetFormControl();
1206         ASSERT(pControl != NULL);
1207         CPDF_FormField* pField = pControl->GetField();
1208         ASSERT(pField != NULL);
1209
1210         CFX_ByteTextBuf sBody, sLines;
1211         
1212         if (IFX_Edit * pEdit = IFX_Edit::NewEdit())
1213         {
1214                 pEdit->EnableRefresh(FALSE);
1215
1216 //              ASSERT(this->m_pBaseForm != NULL);
1217                 ASSERT(this->m_pInterForm != NULL);
1218                 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1219                 ASSERT(pDoc != NULL);
1220                 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
1221
1222                 CBA_FontMap FontMap(this,pEnv->GetSysHandler());//, ISystemHandle::GetSystemHandler(m_pBaseForm->GetEnv()));
1223                 FontMap.Initial();
1224                 pEdit->SetFontMap(&FontMap);
1225
1226                 CPDF_Rect rcClient = GetClientRect();
1227                 pEdit->SetPlateRect(rcClient);
1228                 pEdit->SetAlignmentH(pControl->GetControlAlignment());
1229                 
1230                 FX_DWORD dwFieldFlags = pField->GetFieldFlags();
1231                 FX_BOOL bMultiLine = (dwFieldFlags >> 12) & 1;
1232
1233                 if (bMultiLine)
1234                 {
1235                         pEdit->SetMultiLine(TRUE);
1236                         pEdit->SetAutoReturn(TRUE);
1237                 }
1238                 else
1239                 {
1240                         pEdit->SetAlignmentV(1);
1241                 }
1242
1243                 FX_WORD subWord = 0;
1244                 if ((dwFieldFlags >> 13) & 1)
1245                 {
1246                         subWord = '*';
1247                         pEdit->SetPasswordChar(subWord);
1248                 }
1249
1250                 int nMaxLen = pField->GetMaxLen();
1251                 FX_BOOL bCharArray = (dwFieldFlags >> 24) & 1;
1252                 FX_FLOAT fFontSize = GetFontSize();     
1253
1254                 if (nMaxLen > 0)
1255                 {
1256                         if (bCharArray)
1257                         {
1258                                 pEdit->SetCharArray(nMaxLen);
1259
1260                                 if (IsFloatZero(fFontSize))
1261                                 {
1262                                         fFontSize = CPWL_Edit::GetCharArrayAutoFontSize(FontMap.GetPDFFont(0),rcClient,nMaxLen);
1263                                 }
1264                         }
1265                         else
1266                         {
1267                                 if (sValue)
1268                                         nMaxLen = wcslen((const wchar_t*)sValue); 
1269                                 pEdit->SetLimitChar(nMaxLen);
1270                         }
1271                 }
1272
1273                 if (IsFloatZero(fFontSize))
1274                         pEdit->SetAutoFontSize(TRUE);
1275                 else
1276                         pEdit->SetFontSize(fFontSize);
1277
1278                 pEdit->Initialize();
1279                 
1280                 if (sValue)
1281                         pEdit->SetText(sValue);
1282                 else
1283                         pEdit->SetText(pField->GetValue().c_str());
1284
1285                 CPDF_Rect rcContent = pEdit->GetContentRect();
1286
1287                 CFX_ByteString sEdit = CPWL_Utils::GetEditAppStream(pEdit,CPDF_Point(0.0f,0.0f),
1288                                                                                                                                         NULL,!bCharArray,subWord);
1289
1290                 if (sEdit.GetLength() > 0)
1291                 {
1292                         sBody << "/Tx BMC\n" << "q\n";
1293                         if (rcContent.Width() > rcClient.Width() ||
1294                                 rcContent.Height() > rcClient.Height())
1295                         {
1296                                 sBody << rcClient.left << " " << rcClient.bottom << " " 
1297                                         << rcClient.Width() << " " << rcClient.Height() << " re\nW\nn\n";
1298                         }
1299                         CPWL_Color crText = GetTextPWLColor();  
1300                         sBody << "BT\n" << CPWL_Utils::GetColorAppStream(crText) << sEdit << "ET\n" << "Q\nEMC\n";
1301                 }
1302
1303                 if (bCharArray)
1304                 {
1305                         switch (GetBorderStyle())
1306                         {
1307                         case BBS_SOLID:
1308                                 {
1309                                         CFX_ByteString sColor = CPWL_Utils::GetColorAppStream(GetBorderPWLColor(),FALSE);
1310                                         if (sColor.GetLength() > 0)
1311                                         {
1312                                                 sLines << "q\n" << GetBorderWidth() << " w\n" 
1313                                                         << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(),FALSE) << " 2 J 0 j\n";                                    
1314
1315                                                 for (FX_INT32 i=1;i<nMaxLen;i++)
1316                                                 {
1317                                                         sLines << rcClient.left + ((rcClient.right - rcClient.left)/nMaxLen)*i << " "
1318                                                                 << rcClient.bottom << " m\n"
1319                                                                 << rcClient.left + ((rcClient.right - rcClient.left)/nMaxLen)*i << " "
1320                                                                 << rcClient.top << " l S\n";                                            
1321                                                 }
1322
1323                                                 sLines << "Q\n";                
1324                                         }
1325                                 }
1326                                 break;
1327                         case BBS_DASH:
1328                                 {
1329                                         CFX_ByteString sColor = CPWL_Utils::GetColorAppStream(GetBorderPWLColor(),FALSE);
1330                                         if (sColor.GetLength() > 0)
1331                                         {
1332                                                 CPWL_Dash dsBorder = CPWL_Dash(3, 3, 0);
1333
1334                                                 sLines << "q\n" << GetBorderWidth() << " w\n" 
1335                                                         << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(),FALSE)
1336                                                         << "[" << dsBorder.nDash << " " 
1337                                                         << dsBorder.nGap << "] " 
1338                                                         << dsBorder.nPhase << " d\n";
1339
1340                                                 for (FX_INT32 i=1;i<nMaxLen;i++)                                        
1341                                                 {
1342                                                         sLines << rcClient.left + ((rcClient.right - rcClient.left)/nMaxLen)*i << " "
1343                                                                 << rcClient.bottom << " m\n"
1344                                                                 << rcClient.left + ((rcClient.right - rcClient.left)/nMaxLen)*i << " "
1345                                                                 << rcClient.top << " l S\n";    
1346                                                 }
1347
1348                                                 sLines << "Q\n";
1349                                         }
1350                                 }
1351                                 break;
1352                         }
1353                 }
1354
1355                 IFX_Edit::DelEdit(pEdit);
1356         }
1357
1358         CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() + sLines.GetByteString() + sBody.GetByteString();
1359         WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
1360 }
1361
1362 CPDF_Rect CPDFSDK_Widget::GetClientRect() const
1363 {
1364         CPDF_Rect rcWindow = GetRotatedRect();
1365         FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1366         switch (GetBorderStyle())
1367         {
1368         case BBS_BEVELED:
1369         case BBS_INSET:
1370                 fBorderWidth *= 2.0f;
1371                 break;
1372         }
1373
1374         return CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
1375 }
1376
1377 CPDF_Rect CPDFSDK_Widget::GetRotatedRect() const
1378 {
1379         CPDF_Rect rectAnnot = GetRect();
1380         FX_FLOAT fWidth = rectAnnot.right - rectAnnot.left;
1381         FX_FLOAT fHeight = rectAnnot.top - rectAnnot.bottom;
1382
1383         CPDF_FormControl* pControl = GetFormControl();
1384         ASSERT(pControl != NULL);
1385
1386         CPDF_Rect rcPDFWindow;
1387         switch(abs(pControl->GetRotation() % 360))
1388         {
1389                 case 0:
1390                 case 180:
1391                 default:
1392                         rcPDFWindow = CPDF_Rect(0, 0, fWidth, fHeight); 
1393                         break;
1394                 case 90:
1395                 case 270:
1396                         rcPDFWindow = CPDF_Rect(0, 0, fHeight, fWidth);
1397                         break;
1398         }
1399
1400         return rcPDFWindow;
1401 }
1402
1403 CFX_ByteString CPDFSDK_Widget::GetBackgroundAppStream() const
1404 {
1405         CPWL_Color crBackground = GetFillPWLColor();
1406         if (crBackground.nColorType != COLORTYPE_TRANSPARENT)
1407                 return CPWL_Utils::GetRectFillAppStream(GetRotatedRect(), crBackground);
1408         else
1409                 return "";
1410 }
1411
1412 CFX_ByteString CPDFSDK_Widget::GetBorderAppStream() const
1413 {
1414         CPDF_Rect rcWindow = GetRotatedRect();
1415         CPWL_Color crBorder = GetBorderPWLColor();
1416         CPWL_Color crBackground = GetFillPWLColor();
1417         CPWL_Color crLeftTop, crRightBottom;
1418
1419         FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1420         FX_INT32 nBorderStyle = 0;
1421         CPWL_Dash dsBorder(3,0,0);
1422
1423         switch (GetBorderStyle())
1424         {
1425         case BBS_DASH:
1426                 nBorderStyle = PBS_DASH;
1427                 dsBorder = CPWL_Dash(3, 3, 0);
1428                 break;
1429         case BBS_BEVELED:
1430                 nBorderStyle = PBS_BEVELED;
1431                 fBorderWidth *= 2;
1432                 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1433                 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1434                 break;
1435         case BBS_INSET:
1436                 nBorderStyle = PBS_INSET;
1437                 fBorderWidth *= 2;
1438                 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1439                 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1440                 break;
1441         case BBS_UNDERLINE:
1442                 nBorderStyle = PBS_UNDERLINED;
1443                 break;
1444         default: 
1445                 nBorderStyle = PBS_SOLID;
1446                 break;
1447         }
1448
1449         return CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder, crLeftTop, 
1450                 crRightBottom, nBorderStyle, dsBorder);
1451 }
1452
1453 CPDF_Matrix CPDFSDK_Widget::GetMatrix() const
1454 {
1455         CPDF_Matrix mt;
1456         CPDF_FormControl* pControl = GetFormControl();
1457         ASSERT(pControl != NULL);
1458
1459         CPDF_Rect rcAnnot = GetRect();
1460         FX_FLOAT fWidth = rcAnnot.right - rcAnnot.left;
1461         FX_FLOAT fHeight = rcAnnot.top - rcAnnot.bottom;
1462         
1463
1464
1465         switch (abs(pControl->GetRotation() % 360))
1466         {
1467                 case 0:
1468                 default:
1469                         mt = CPDF_Matrix(1, 0, 0, 1, 0, 0);
1470                         break;
1471                 case 90:
1472                         mt = CPDF_Matrix(0, 1, -1, 0, fWidth, 0);
1473                         break;
1474                 case 180:
1475                         mt = CPDF_Matrix(-1, 0, 0, -1, fWidth, fHeight);
1476                         break;
1477                 case 270:
1478                         mt = CPDF_Matrix(0, -1, 1, 0, 0, fHeight);
1479                         break;
1480         }
1481
1482         return mt;
1483 }
1484
1485 CPWL_Color CPDFSDK_Widget::GetTextPWLColor() const
1486 {
1487         CPWL_Color crText = CPWL_Color(COLORTYPE_GRAY, 0);
1488
1489         CPDF_FormControl* pFormCtrl = GetFormControl();
1490         ASSERT(pFormCtrl != NULL);
1491
1492         CPDF_DefaultAppearance da = pFormCtrl->GetDefaultAppearance();
1493         if (da.HasColor())
1494         {
1495                 FX_INT32 iColorType;
1496                 FX_FLOAT fc[4];
1497                 da.GetColor(iColorType, fc);
1498                 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1499         }
1500
1501         return crText;
1502 }
1503
1504 CPWL_Color CPDFSDK_Widget::GetBorderPWLColor() const
1505 {
1506         CPWL_Color crBorder;
1507
1508         CPDF_FormControl* pFormCtrl = GetFormControl();
1509         ASSERT(pFormCtrl != NULL);
1510
1511         FX_INT32 iColorType;
1512         FX_FLOAT fc[4];
1513         pFormCtrl->GetOriginalBorderColor(iColorType, fc);
1514         if (iColorType > 0)
1515                 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1516
1517         return crBorder;
1518 }
1519
1520 CPWL_Color CPDFSDK_Widget::GetFillPWLColor() const
1521 {
1522         CPWL_Color crFill;
1523
1524         CPDF_FormControl* pFormCtrl = GetFormControl();
1525         ASSERT(pFormCtrl != NULL);
1526
1527         FX_INT32 iColorType;
1528         FX_FLOAT fc[4];
1529         pFormCtrl->GetOriginalBackgroundColor(iColorType, fc);
1530         if (iColorType > 0)
1531                 crFill = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1532
1533         return crFill;
1534 }
1535
1536 void CPDFSDK_Widget::AddImageToAppearance(const CFX_ByteString& sAPType, CPDF_Stream* pImage)
1537 {
1538         ASSERT(pImage != NULL);
1539
1540         ASSERT(m_pAnnot != NULL);
1541         ASSERT(m_pAnnot->m_pAnnotDict != NULL);
1542
1543         CPDF_Document* pDoc = m_pPageView->GetPDFDocument();//pDocument->GetDocument();
1544         ASSERT(pDoc != NULL);
1545
1546         CPDF_Dictionary* pAPDict = m_pAnnot->m_pAnnotDict->GetDict("AP");
1547         ASSERT(pAPDict != NULL);
1548
1549         CPDF_Stream* pStream = pAPDict->GetStream(sAPType);
1550         ASSERT(pStream != NULL);
1551
1552         CPDF_Dictionary* pStreamDict = pStream->GetDict();
1553         ASSERT(pStreamDict != NULL);
1554
1555         CFX_ByteString sImageAlias = "IMG";
1556
1557         if (CPDF_Dictionary* pImageDict = pImage->GetDict())
1558         {
1559                 sImageAlias = pImageDict->GetString("Name");
1560                 if (sImageAlias.IsEmpty())
1561                         sImageAlias = "IMG";
1562         }       
1563
1564         CPDF_Dictionary* pStreamResList = pStreamDict->GetDict("Resources");
1565         if (!pStreamResList)
1566         {
1567                 pStreamResList = new CPDF_Dictionary();
1568                 pStreamDict->SetAt("Resources", pStreamResList);
1569         }
1570
1571         if (pStreamResList)
1572         {
1573                 CPDF_Dictionary* pXObject = new CPDF_Dictionary;
1574                 pXObject->SetAtReference(sImageAlias, pDoc, pImage);
1575                 pStreamResList->SetAt("XObject", pXObject);
1576         }
1577 }
1578
1579 void CPDFSDK_Widget::RemoveAppearance(const CFX_ByteString& sAPType)
1580 {
1581         ASSERT(m_pAnnot != NULL);
1582         ASSERT(m_pAnnot->m_pAnnotDict != NULL);
1583
1584         if (CPDF_Dictionary* pAPDict = m_pAnnot->m_pAnnotDict->GetDict("AP"))
1585         {
1586                 pAPDict->RemoveAt(sAPType);
1587         }
1588 }
1589
1590 FX_BOOL CPDFSDK_Widget::OnAAction(CPDF_AAction::AActionType type, PDFSDK_FieldAction& data, CPDFSDK_PageView* pPageView)
1591 {
1592         CPDF_Action action = GetAAction(type);
1593
1594         if (action && action.GetType() != CPDF_Action::Unknown)
1595         {
1596                 CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
1597                 ASSERT(pDocument != NULL);
1598  
1599                 CPDFDoc_Environment* pEnv = pDocument->GetEnv();
1600                 ASSERT(pEnv != NULL);
1601
1602                 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();/*(CPDFSDK_ActionHandler*)pApp->GetActionHandler();*/
1603                 ASSERT(pActionHandler != NULL);
1604  
1605                 return pActionHandler->DoAction_Field(action, type, pDocument, GetFormField(), data);
1606         }
1607
1608         return FALSE;
1609 }
1610
1611 CPDF_Action     CPDFSDK_Widget::GetAAction(CPDF_AAction::AActionType eAAT)
1612 {
1613         switch (eAAT)
1614         {
1615         case CPDF_AAction::CursorEnter:
1616         case CPDF_AAction::CursorExit:
1617         case CPDF_AAction::ButtonDown:
1618         case CPDF_AAction::ButtonUp:
1619         case CPDF_AAction::GetFocus:
1620         case CPDF_AAction::LoseFocus:
1621         case CPDF_AAction::PageOpen:
1622         case CPDF_AAction::PageClose:
1623         case CPDF_AAction::PageVisible:
1624         case CPDF_AAction::PageInvisible:
1625                 return CPDFSDK_Annot::GetAAction(eAAT);
1626
1627         case CPDF_AAction::KeyStroke:
1628         case CPDF_AAction::Format:
1629         case CPDF_AAction::Validate:
1630         case CPDF_AAction::Calculate:
1631                 {
1632                         CPDF_FormField* pField = this->GetFormField();
1633                         if (CPDF_AAction aa = pField->GetAdditionalAction())
1634                                 return aa.GetAction(eAAT);
1635
1636                         return CPDFSDK_Annot::GetAAction(eAAT);
1637                 }
1638         default:
1639                 break;
1640         }
1641
1642         return CPDF_Action();
1643 }
1644
1645
1646 CFX_WideString CPDFSDK_Widget::GetAlternateName() const
1647 {
1648         CPDF_FormField* pFormField = GetFormField();
1649         ASSERT(pFormField != NULL);
1650
1651         return pFormField->GetAlternateName();
1652 }
1653
1654 FX_INT32        CPDFSDK_Widget::GetAppearanceAge() const
1655 {
1656         return m_nAppAge;
1657 }
1658
1659 FX_INT32 CPDFSDK_Widget::GetValueAge() const
1660 {
1661         return m_nValueAge;
1662 }
1663
1664
1665 FX_BOOL CPDFSDK_Widget::HitTest(FX_FLOAT pageX, FX_FLOAT pageY)
1666 {
1667         CPDF_Annot* pAnnot = GetPDFAnnot();
1668         CFX_FloatRect annotRect;
1669         pAnnot->GetRect(annotRect);
1670         if(annotRect.Contains(pageX, pageY))
1671         {
1672                 if (!IsVisible()) return FALSE;
1673                 
1674                 int nFieldFlags = GetFieldFlags();
1675                 if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY) 
1676                         return FALSE;
1677                 
1678                 return TRUE;
1679         }
1680         return FALSE;
1681 }
1682
1683 CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_Document* pDocument)
1684         :m_pDocument(pDocument),
1685         m_pInterForm(NULL),
1686         m_bCalculate(TRUE),
1687         m_bBusy(FALSE)
1688 {
1689         ASSERT(m_pDocument != NULL);
1690         m_pInterForm = new CPDF_InterForm(m_pDocument->GetDocument(), FALSE);
1691         ASSERT(m_pInterForm != NULL);
1692         m_pInterForm->SetFormNotify(this);
1693
1694         for(int i=0; i<6; i++)
1695                 m_bNeedHightlight[i] = FALSE;
1696         m_iHighlightAlpha = 0;
1697 }
1698
1699 CPDFSDK_InterForm::~CPDFSDK_InterForm()
1700 {
1701         ASSERT(m_pInterForm != NULL);
1702         delete m_pInterForm;
1703         m_pInterForm = NULL;
1704
1705         m_Map.RemoveAll();
1706 }
1707
1708 void CPDFSDK_InterForm::Destroy()
1709 {
1710         delete this;
1711 }
1712
1713 CPDF_InterForm* CPDFSDK_InterForm::GetInterForm()
1714 {
1715         return m_pInterForm;
1716 }
1717
1718 CPDFSDK_Document* CPDFSDK_InterForm::GetDocument()
1719 {
1720         return m_pDocument;
1721 }
1722
1723 FX_BOOL CPDFSDK_InterForm::HighlightWidgets()
1724 {
1725         return FALSE;
1726 }
1727
1728 CPDFSDK_Widget* CPDFSDK_InterForm::GetSibling(CPDFSDK_Widget* pWidget, FX_BOOL bNext) const
1729 {
1730         ASSERT(pWidget != NULL);
1731
1732         CBA_AnnotIterator* pIterator = new CBA_AnnotIterator(pWidget->GetPageView(), "Widget", "");
1733         ASSERT(pIterator != NULL);
1734
1735         CPDFSDK_Widget* pRet = NULL;
1736
1737         if (bNext)
1738                 pRet = (CPDFSDK_Widget*)pIterator->GetNextAnnot(pWidget);
1739         else
1740                 pRet = (CPDFSDK_Widget*)pIterator->GetPrevAnnot(pWidget);
1741
1742         pIterator->Release();
1743         
1744         return pRet;
1745
1746 }
1747
1748 CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl) const
1749 {
1750         if(!pControl || !m_pInterForm) return NULL;
1751         
1752         CPDFSDK_Widget* pWidget = NULL;
1753         m_Map.Lookup(pControl, pWidget);
1754
1755         if (pWidget) return pWidget;
1756
1757         CPDF_Dictionary* pControlDict = pControl->GetWidget();
1758         ASSERT(pControlDict != NULL);
1759
1760         ASSERT(m_pDocument != NULL);
1761         CPDF_Document* pDocument = m_pDocument->GetDocument();
1762
1763         CPDFSDK_PageView* pPage = NULL;
1764
1765         if (CPDF_Dictionary* pPageDict = pControlDict->GetDict("P"))
1766         {
1767                 int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum());
1768                 if (nPageIndex >= 0)
1769                 {
1770                         pPage = m_pDocument->GetPageView(nPageIndex);
1771                 }
1772         }
1773
1774         if (!pPage) 
1775         {
1776                 int nPageIndex = GetPageIndexByAnnotDict(pDocument, pControlDict);
1777                 if (nPageIndex >= 0)
1778                 {
1779                         pPage = m_pDocument->GetPageView(nPageIndex);
1780                 }
1781         }
1782
1783         if (pPage)
1784                 return (CPDFSDK_Widget*)pPage->GetAnnotByDict(pControlDict);
1785
1786         return NULL;
1787 }
1788
1789 void CPDFSDK_InterForm::GetWidgets(const CFX_WideString& sFieldName, CFX_PtrArray& widgets)
1790 {
1791         ASSERT(m_pInterForm != NULL);
1792
1793         for (int i=0,sz=m_pInterForm->CountFields(sFieldName); i<sz; i++)
1794         {
1795                 CPDF_FormField* pFormField = m_pInterForm->GetField(i, sFieldName);
1796                 ASSERT(pFormField != NULL);
1797
1798                 GetWidgets(pFormField, widgets);        
1799         }
1800 }
1801
1802 void CPDFSDK_InterForm::GetWidgets(CPDF_FormField* pField, CFX_PtrArray& widgets)
1803 {
1804         ASSERT(pField != NULL);
1805
1806         for (int i=0,isz=pField->CountControls(); i<isz; i++)
1807         {
1808                 CPDF_FormControl* pFormCtrl = pField->GetControl(i);
1809                 ASSERT(pFormCtrl != NULL);
1810
1811                 CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl);
1812
1813                 if (pWidget)
1814                         widgets.Add(pWidget);
1815         }
1816 }
1817
1818 int CPDFSDK_InterForm::GetPageIndexByAnnotDict(CPDF_Document* pDocument, CPDF_Dictionary* pAnnotDict) const
1819 {
1820         ASSERT(pDocument != NULL);
1821         ASSERT(pAnnotDict != NULL);
1822
1823         for (int i=0,sz=pDocument->GetPageCount(); i<sz; i++)
1824         {
1825                 if (CPDF_Dictionary* pPageDict = pDocument->GetPage(i))
1826                 {                       
1827                         if (CPDF_Array* pAnnots = pPageDict->GetArray("Annots"))
1828                         {
1829                                 for (int j=0,jsz=pAnnots->GetCount(); j<jsz; j++)
1830                                 {
1831                                         CPDF_Object* pDict = pAnnots->GetElementValue(j);
1832                                         if (pAnnotDict == pDict)
1833                                         {
1834                                                 return i;
1835                                         }
1836                                 }
1837                         }
1838                 }
1839         }
1840
1841         return -1;
1842 }
1843
1844 void CPDFSDK_InterForm::AddMap(CPDF_FormControl* pControl, CPDFSDK_Widget* pWidget)
1845 {
1846         m_Map.SetAt(pControl, pWidget);
1847 }
1848
1849 void CPDFSDK_InterForm::RemoveMap(CPDF_FormControl* pControl)
1850 {
1851         m_Map.RemoveKey(pControl);
1852 }
1853
1854 void CPDFSDK_InterForm::EnableCalculate(FX_BOOL bEnabled)
1855 {
1856         m_bCalculate = bEnabled;
1857 }
1858
1859 FX_BOOL CPDFSDK_InterForm::IsCalculateEnabled() const
1860 {
1861         return m_bCalculate;
1862 }
1863
1864 #ifdef _WIN32
1865 CPDF_Stream* CPDFSDK_InterForm::LoadImageFromFile(const CFX_WideString& sFile)
1866 {
1867         ASSERT(m_pDocument != NULL);
1868         CPDF_Document* pDocument = m_pDocument->GetDocument();
1869         ASSERT(pDocument != NULL);
1870
1871         CPDF_Stream* pRetStream = NULL;
1872
1873         if (CFX_DIBitmap* pBmp = CFX_WindowsDIB::LoadFromFile(sFile))
1874         {
1875                 int nWidth = pBmp->GetWidth();
1876                 int nHeight = pBmp->GetHeight();
1877
1878                 CPDF_Image Image(pDocument);
1879                 Image.SetImage(pBmp, FALSE);
1880                 CPDF_Stream* pImageStream = Image.GetStream();
1881                 if (pImageStream)
1882                 {
1883                         if (pImageStream->GetObjNum() == 0)
1884                                 pDocument->AddIndirectObject(pImageStream);
1885
1886                         CPDF_Dictionary* pStreamDict = new CPDF_Dictionary();
1887                         pStreamDict->SetAtName("Subtype", "Form");
1888                         pStreamDict->SetAtName("Name", "IMG");
1889                         CPDF_Array* pMatrix = new CPDF_Array();
1890                         pStreamDict->SetAt("Matrix", pMatrix);
1891                         pMatrix->AddInteger(1);
1892                         pMatrix->AddInteger(0);
1893                         pMatrix->AddInteger(0);
1894                         pMatrix->AddInteger(1);
1895                         pMatrix->AddInteger(-nWidth / 2);
1896                         pMatrix->AddInteger(-nHeight / 2);
1897                         CPDF_Dictionary* pResource = new CPDF_Dictionary();
1898                         pStreamDict->SetAt("Resources", pResource);
1899                         CPDF_Dictionary* pXObject = new CPDF_Dictionary();
1900                         pResource->SetAt("XObject", pXObject);
1901                         pXObject->SetAtReference("Img", pDocument, pImageStream);
1902                         CPDF_Array* pProcSet = new CPDF_Array();
1903                         pResource->SetAt("ProcSet", pProcSet);
1904                         pProcSet->AddName("PDF");
1905                         pProcSet->AddName("ImageC");
1906                         pStreamDict->SetAtName("Type", "XObject");
1907                         CPDF_Array* pBBox = new CPDF_Array();
1908                         pStreamDict->SetAt("BBox", pBBox);
1909                         pBBox->AddInteger(0);
1910                         pBBox->AddInteger(0);
1911                         pBBox->AddInteger(nWidth);
1912                         pBBox->AddInteger(nHeight);
1913                         pStreamDict->SetAtInteger("FormType", 1);
1914
1915                         pRetStream = new CPDF_Stream(NULL, 0, NULL);
1916                         CFX_ByteString csStream;
1917                         csStream.Format("q\n%d 0 0 %d 0 0 cm\n/Img Do\nQ", nWidth, nHeight);
1918                         pRetStream->InitStream((FX_BYTE*)csStream.c_str(), csStream.GetLength(), pStreamDict);
1919                         pDocument->AddIndirectObject(pRetStream);
1920                 }
1921
1922                 delete pBmp;
1923         }
1924
1925         return pRetStream;
1926 }
1927 #endif
1928
1929 void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField)
1930 {
1931         ASSERT(m_pDocument != NULL);
1932         CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
1933         ASSERT(pEnv);
1934         if(!pEnv->IsJSInitiated())
1935                 return;
1936
1937         if (m_bBusy) return;
1938
1939         m_bBusy = TRUE;
1940
1941         if (this->IsCalculateEnabled())
1942         {
1943                 IFXJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
1944                 ASSERT(pRuntime != NULL);
1945
1946                 pRuntime->SetReaderDocument(m_pDocument);
1947
1948                 int nSize = m_pInterForm->CountFieldsInCalculationOrder();
1949                 for (int i=0; i<nSize; i++)
1950                 {
1951                         if(CPDF_FormField* pField = m_pInterForm->GetFieldInCalculationOrder(i))
1952                         {
1953 //                      ASSERT(pField != NULL);
1954                                 int nType = pField->GetFieldType();
1955                                 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD)
1956                                 {
1957                                         CPDF_AAction aAction = pField->GetAdditionalAction();
1958                                         if (aAction && aAction.ActionExist(CPDF_AAction::Calculate))
1959                                         {
1960                                                 CPDF_Action action = aAction.GetAction(CPDF_AAction::Calculate);
1961                                                 if (action)
1962                                                 {
1963                                                         CFX_WideString csJS = action.GetJavaScript();
1964                                                         if (!csJS.IsEmpty())
1965                                                         {
1966                                                                 IFXJS_Context* pContext = pRuntime->NewContext();
1967                                                                 ASSERT(pContext != NULL);
1968                                                                 
1969                                                                 CFX_WideString sOldValue = pField->GetValue();
1970                                                                 CFX_WideString sValue = sOldValue;
1971                                                                 FX_BOOL bRC = TRUE;
1972                                                                 pContext->OnField_Calculate(pFormField, pField, sValue, bRC);
1973                                                                 
1974                                                                 CFX_WideString sInfo;
1975                                                                 FX_BOOL bRet = pContext->RunScript(csJS, sInfo);
1976                                                                 pRuntime->ReleaseContext(pContext);
1977                                                                 
1978                                                                 if (bRet)
1979                                                                 {
1980                                                                         if (bRC)
1981                                                                         {
1982                                                                                 if (sValue.Compare(sOldValue) != 0)
1983                                                                                         pField->SetValue(sValue, TRUE);
1984                                                                         }
1985                                                                 }
1986                                                         }
1987                                                 }
1988                                         }
1989                                 }
1990                         }
1991                 }
1992
1993                 
1994         }
1995
1996         m_bBusy = FALSE;
1997 }
1998
1999 CFX_WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField, int nCommitKey, FX_BOOL& bFormated)
2000 {
2001         ASSERT(m_pDocument != NULL);
2002         ASSERT(pFormField != NULL);
2003
2004         CFX_WideString sValue = pFormField->GetValue();
2005         CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2006         ASSERT(pEnv);
2007         if(!pEnv->IsJSInitiated())
2008         {
2009                 bFormated = FALSE;
2010                 return sValue;
2011         } 
2012
2013         IFXJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
2014         ASSERT(pRuntime != NULL);
2015         
2016         pRuntime->SetReaderDocument(m_pDocument);
2017
2018         if (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX)
2019         {
2020                 if (pFormField->CountSelectedItems() > 0)
2021                 {
2022                         int index = pFormField->GetSelectedIndex(0);
2023                         if (index >= 0)
2024                                 sValue = pFormField->GetOptionLabel(index);
2025                 }
2026         }
2027
2028         bFormated = FALSE;
2029
2030         CPDF_AAction aAction = pFormField->GetAdditionalAction();
2031         if (aAction != NULL && aAction.ActionExist(CPDF_AAction::Format)) 
2032         {
2033                 CPDF_Action action = aAction.GetAction(CPDF_AAction::Format);
2034                 if (action)
2035                 {                       
2036                         CFX_WideString script = action.GetJavaScript();
2037                         if (!script.IsEmpty())
2038                         {
2039                                 CFX_WideString Value = sValue;
2040
2041                                 IFXJS_Context* pContext = pRuntime->NewContext();
2042                                 ASSERT(pContext != NULL);
2043
2044                                 pContext->OnField_Format(nCommitKey, pFormField, Value, TRUE);
2045                         
2046                                 CFX_WideString sInfo;
2047                                 FX_BOOL bRet = pContext->RunScript(script, sInfo);
2048                                 pRuntime->ReleaseContext(pContext);
2049
2050                                 if (bRet)
2051                                 {
2052                                         sValue = Value;
2053                                         bFormated = TRUE;
2054                                 }
2055                         }
2056                 }
2057         }
2058
2059         return sValue;
2060 }
2061
2062 void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField, FX_LPCWSTR sValue, FX_BOOL bValueChanged)
2063 {
2064         ASSERT(pFormField != NULL);
2065
2066         for (int i=0,sz=pFormField->CountControls(); i<sz; i++)
2067         {
2068                 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
2069                 ASSERT(pFormCtrl != NULL);
2070
2071                 ASSERT(m_pInterForm != NULL);
2072                 if(CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl))
2073                         pWidget->ResetAppearance(sValue, bValueChanged);
2074         }
2075 }
2076
2077 void CPDFSDK_InterForm::UpdateField(CPDF_FormField* pFormField)
2078 {
2079         ASSERT(pFormField != NULL);
2080
2081         for (int i=0,sz=pFormField->CountControls(); i<sz; i++)
2082         {
2083                 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
2084                 ASSERT(pFormCtrl != NULL);
2085
2086                 if(CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl))
2087                 {
2088                         CPDFDoc_Environment * pEnv = m_pDocument->GetEnv();
2089                         CFFL_IFormFiller* pIFormFiller = pEnv->GetIFormFiller();
2090                         
2091                         CPDF_Page * pPage = pWidget->GetPDFPage();
2092                         CPDFSDK_PageView * pPageView = m_pDocument->GetPageView(pPage,FALSE);
2093
2094                         FX_RECT rcBBox = pIFormFiller->GetViewBBox(pPageView, pWidget);
2095
2096                         pEnv->FFI_Invalidate(pPage,rcBBox.left, rcBBox.top, rcBBox.right, rcBBox.bottom);
2097                 }
2098         }
2099 }
2100
2101 void CPDFSDK_InterForm::OnKeyStrokeCommit(CPDF_FormField* pFormField, CFX_WideString& csValue, FX_BOOL& bRC)
2102 {
2103         ASSERT(pFormField != NULL);
2104
2105         CPDF_AAction aAction = pFormField->GetAdditionalAction();
2106         if (aAction != NULL && aAction.ActionExist(CPDF_AAction::KeyStroke)) 
2107         {
2108                 CPDF_Action action = aAction.GetAction(CPDF_AAction::KeyStroke);
2109                 if (action)
2110                 {                        
2111                         ASSERT(m_pDocument != NULL);
2112                         CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2113                         ASSERT(pEnv != NULL);
2114
2115                         CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
2116                         ASSERT(pActionHandler != NULL);
2117         
2118                         PDFSDK_FieldAction fa;
2119                         fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
2120                         fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
2121                         fa.sValue = csValue;
2122
2123                         pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::KeyStroke, 
2124                                 m_pDocument, pFormField, fa);
2125                         bRC = fa.bRC;
2126                 }
2127         }
2128 }
2129
2130 void CPDFSDK_InterForm::OnValidate(CPDF_FormField* pFormField, CFX_WideString& csValue, FX_BOOL& bRC)
2131 {
2132         ASSERT(pFormField != NULL);
2133
2134         CPDF_AAction aAction = pFormField->GetAdditionalAction();
2135         if (aAction != NULL && aAction.ActionExist(CPDF_AAction::Validate)) 
2136         {
2137                 CPDF_Action action = aAction.GetAction(CPDF_AAction::Validate);
2138                 if (action)
2139                 {               
2140                         ASSERT(m_pDocument != NULL);
2141                         CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2142                         ASSERT(pEnv != NULL);
2143                         
2144                         CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
2145                         ASSERT(pActionHandler != NULL);
2146
2147                         PDFSDK_FieldAction fa;
2148                         fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
2149                         fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
2150                         fa.sValue = csValue;
2151
2152                         pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::Validate, m_pDocument, pFormField, fa);
2153                         bRC = fa.bRC;
2154          
2155                 }
2156         }
2157 }
2158
2159 /* ----------------------------- action ----------------------------- */
2160
2161 FX_BOOL CPDFSDK_InterForm::DoAction_Hide(const CPDF_Action& action)
2162 {
2163         ASSERT(action);
2164
2165         CPDF_ActionFields af = action.GetWidgets();
2166         CFX_PtrArray fieldObjects;
2167         af.GetAllFields(fieldObjects);
2168         CFX_PtrArray widgetArray;
2169         CFX_PtrArray fields;
2170         GetFieldFromObjects(fieldObjects, fields);
2171
2172         FX_BOOL bHide = action.GetHideStatus();
2173
2174         FX_BOOL bChanged = FALSE;
2175         
2176         for (int i=0, sz=fields.GetSize(); i<sz; i++)
2177         {
2178                 CPDF_FormField* pField = (CPDF_FormField*)fields[i];
2179                 ASSERT(pField != NULL);
2180
2181         
2182                 for (int j=0,jsz=pField->CountControls(); j<jsz; j++)
2183                 {
2184                         CPDF_FormControl* pControl = pField->GetControl(j);
2185                         ASSERT(pControl != NULL);
2186
2187                         if (CPDFSDK_Widget* pWidget = GetWidget(pControl))
2188                         {
2189                                 int nFlags = pWidget->GetFlags();
2190                                 if (bHide)
2191                                 {
2192                                         nFlags &= (~ANNOTFLAG_INVISIBLE);
2193                                         nFlags &= (~ANNOTFLAG_NOVIEW);
2194                                         nFlags |= (ANNOTFLAG_HIDDEN);
2195                                 }
2196                                 else
2197                                 {
2198                                         nFlags &= (~ANNOTFLAG_INVISIBLE);
2199                                         nFlags &= (~ANNOTFLAG_HIDDEN);
2200                                         nFlags &= (~ANNOTFLAG_NOVIEW);
2201                                 }
2202                                 pWidget->SetFlags(nFlags);
2203
2204                                 CPDFSDK_PageView* pPageView = pWidget->GetPageView();
2205                                 ASSERT(pPageView != NULL);
2206  
2207                                 pPageView->UpdateView(pWidget);
2208
2209                                 bChanged = TRUE;
2210                         }
2211                 }
2212         }
2213
2214         return bChanged;
2215 }
2216
2217 FX_BOOL CPDFSDK_InterForm::DoAction_SubmitForm(const CPDF_Action& action)
2218 {
2219         ASSERT(action);
2220         ASSERT(m_pInterForm != NULL);
2221
2222         CFX_WideString sDestination = action.GetFilePath();
2223         if (sDestination.IsEmpty()) return FALSE;
2224
2225         CPDF_Dictionary* pActionDict = action.GetDict();
2226         if (pActionDict->KeyExist("Fields"))
2227         {
2228                 CPDF_ActionFields af = action.GetWidgets();
2229                 FX_DWORD dwFlags = action.GetFlags();
2230                 
2231                 CFX_PtrArray fieldObjects;
2232                 af.GetAllFields(fieldObjects);
2233                 CFX_PtrArray fields;
2234                 GetFieldFromObjects(fieldObjects, fields);
2235                 
2236                 if (fields.GetSize() != 0)
2237                 {
2238                         FX_BOOL bIncludeOrExclude = !(dwFlags & 0x01);
2239                         if (m_pInterForm->CheckRequiredFields(&fields, bIncludeOrExclude))
2240                         {
2241                                 return FALSE;
2242                         }
2243                         return SubmitFields(sDestination, fields, bIncludeOrExclude, FALSE);
2244                 }
2245                 else
2246                 {
2247                         if ( m_pInterForm->CheckRequiredFields())
2248                         {
2249                                 return FALSE;
2250                         }
2251
2252                         return SubmitForm(sDestination, FALSE);
2253                 }
2254         }
2255         else
2256         {
2257                 if ( m_pInterForm->CheckRequiredFields())
2258                 {
2259                         return FALSE;
2260                 }
2261
2262                 return SubmitForm(sDestination, FALSE);
2263         }
2264 }
2265
2266 FX_BOOL CPDFSDK_InterForm::SubmitFields(const CFX_WideString& csDestination, const CFX_PtrArray& fields,
2267                                                                         FX_BOOL bIncludeOrExclude, FX_BOOL bUrlEncoded)
2268 {
2269         CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2270         ASSERT(pEnv != NULL);
2271
2272         CFX_ByteTextBuf textBuf;
2273         ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude, textBuf);
2274
2275         FX_LPBYTE pBuffer = textBuf.GetBuffer();
2276         FX_STRSIZE nBufSize = textBuf.GetLength();
2277         
2278         if (bUrlEncoded)
2279         {
2280                 if(!FDFToURLEncodedData(pBuffer, nBufSize))
2281                         return FALSE;
2282         }
2283
2284         pEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str());
2285         
2286         return TRUE;
2287 }
2288
2289 void CPDFSDK_InterForm::DoFDFBuffer(CFX_ByteString sBuffer)
2290 {
2291         ASSERT(m_pDocument != NULL);
2292
2293         if (CFDF_Document *pFDFDocument = CFDF_Document::ParseMemory((const unsigned char *)sBuffer.GetBuffer(sBuffer.GetLength()), sBuffer.GetLength()))
2294         {                                               
2295                 CPDF_Dictionary* pRootDic = pFDFDocument->GetRoot();
2296                 if(pRootDic)
2297                 {
2298                         CPDF_Dictionary * pFDFDict=pRootDic->GetDict("FDF");
2299                         if(pFDFDict)
2300                         {               
2301                                 CPDF_Dictionary * pJSDict = pFDFDict->GetDict("JavaScript");
2302                                 if(pJSDict)
2303                                 {
2304                                         CFX_WideString csJS;
2305                                 
2306                                         CPDF_Object* pJS = pJSDict->GetElementValue("Before");
2307                                         if (pJS != NULL)
2308                                         {
2309                                                 int iType = pJS->GetType();
2310                                                 if (iType == PDFOBJ_STRING)
2311                                                         csJS = pJSDict->GetUnicodeText("Before");
2312                                                 else if (iType == PDFOBJ_STREAM)
2313                                                         csJS = pJS->GetUnicodeText();
2314                                         }
2315                                         
2316                                 }
2317                         }
2318                 }
2319                 delete pFDFDocument;
2320         }
2321
2322         sBuffer.ReleaseBuffer();
2323 }
2324
2325 FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(CFX_WideString csFDFFile, CFX_WideString csTxtFile)
2326 {
2327         return TRUE;
2328 }
2329
2330 FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(FX_LPBYTE& pBuf, FX_STRSIZE& nBufSize)
2331 {
2332         CFDF_Document* pFDF = CFDF_Document::ParseMemory(pBuf, nBufSize);
2333         if (pFDF)
2334         {
2335                 CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDict("FDF");
2336                 if (pMainDict == NULL) return FALSE;
2337                 
2338                 // Get fields
2339                 CPDF_Array* pFields = pMainDict->GetArray("Fields");
2340                 if (pFields == NULL) return FALSE;
2341                 
2342                 CFX_ByteTextBuf fdfEncodedData;
2343
2344                 for (FX_DWORD i = 0; i < pFields->GetCount(); i ++) 
2345                 {
2346                         CPDF_Dictionary* pField = pFields->GetDict(i);
2347                         if (pField == NULL) continue;
2348                         CFX_WideString name;
2349                         name = pField->GetUnicodeText("T");
2350                         CFX_ByteString name_b = CFX_ByteString::FromUnicode(name);
2351                         CFX_ByteString csBValue = pField->GetString("V");
2352                         CFX_WideString csWValue = PDF_DecodeText(csBValue);
2353                         CFX_ByteString csValue_b = CFX_ByteString::FromUnicode(csWValue);
2354
2355                         fdfEncodedData = fdfEncodedData<<name_b.GetBuffer(name_b.GetLength());
2356                         name_b.ReleaseBuffer();
2357                         fdfEncodedData = fdfEncodedData<<"=";
2358                         fdfEncodedData = fdfEncodedData<<csValue_b.GetBuffer(csValue_b.GetLength());
2359                         csValue_b.ReleaseBuffer();
2360                         if(i != pFields->GetCount()-1)
2361                                 fdfEncodedData = fdfEncodedData<<"&";
2362                 }
2363                 
2364                 nBufSize = fdfEncodedData.GetLength();
2365                 pBuf = FX_Alloc(FX_BYTE, nBufSize);
2366                 if(!pBuf)
2367                         return FALSE;
2368                 FXSYS_memcpy(pBuf, fdfEncodedData.GetBuffer(), nBufSize);
2369                 
2370         }
2371         return TRUE;
2372 }
2373
2374 FX_BOOL CPDFSDK_InterForm::ExportFieldsToFDFTextBuf(const CFX_PtrArray& fields,FX_BOOL bIncludeOrExclude, CFX_ByteTextBuf& textBuf)
2375 {
2376         ASSERT(m_pDocument != NULL);
2377         ASSERT(m_pInterForm != NULL);
2378         
2379         CFDF_Document* pFDF = m_pInterForm->ExportToFDF(m_pDocument->GetPath(),(CFX_PtrArray&)fields, bIncludeOrExclude);
2380         if (!pFDF) return FALSE;
2381         FX_BOOL bRet = pFDF->WriteBuf(textBuf); // = FALSE;//
2382         delete pFDF;
2383         
2384         return bRet;
2385 }
2386
2387 CFX_WideString CPDFSDK_InterForm::GetTemporaryFileName(const CFX_WideString& sFileExt)
2388 {
2389         CFX_WideString sFileName;
2390         return L"";
2391 }
2392
2393 FX_BOOL CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination, FX_BOOL bUrlEncoded)
2394 {
2395         if (sDestination.IsEmpty()) return FALSE;
2396
2397         CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2398         ASSERT(pEnv != NULL);
2399
2400         if(NULL == m_pDocument) return FALSE;
2401         CFX_WideString wsPDFFilePath = m_pDocument->GetPath();
2402         
2403         if(NULL == m_pInterForm) return FALSE;
2404         CFDF_Document* pFDFDoc = m_pInterForm->ExportToFDF(wsPDFFilePath);
2405         if (NULL == pFDFDoc) return FALSE;
2406
2407         CFX_ByteTextBuf FdfBuffer;
2408         FX_BOOL bRet = pFDFDoc->WriteBuf(FdfBuffer);
2409         delete pFDFDoc;
2410         if (!bRet) return FALSE;
2411
2412         FX_LPBYTE pBuffer = FdfBuffer.GetBuffer();
2413         FX_STRSIZE nBufSize = FdfBuffer.GetLength();
2414         
2415         if (bUrlEncoded)
2416         {
2417                 if(!FDFToURLEncodedData(pBuffer, nBufSize))
2418                         return FALSE;
2419         }
2420
2421         pEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str());
2422         
2423         if (bUrlEncoded && pBuffer)
2424         {
2425                 FX_Free(pBuffer);
2426                 pBuffer = NULL; 
2427         }
2428
2429         return TRUE;
2430 }
2431
2432 FX_BOOL CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf)
2433 {
2434
2435         ASSERT(m_pInterForm != NULL);
2436         ASSERT(m_pDocument != NULL);
2437         
2438         CFDF_Document* pFDF = m_pInterForm->ExportToFDF(m_pDocument->GetPath());
2439         if (!pFDF) return FALSE;
2440         
2441         FX_BOOL bRet = pFDF->WriteBuf(textBuf);
2442         delete pFDF;
2443         
2444         return bRet;
2445 }
2446
2447
2448 FX_BOOL CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action)
2449 {
2450         ASSERT(action);
2451
2452         CPDF_Dictionary* pActionDict = action.GetDict();
2453         if (pActionDict->KeyExist("Fields"))
2454         {
2455                 CPDF_ActionFields af = action.GetWidgets();
2456                 FX_DWORD dwFlags = action.GetFlags();
2457
2458                 CFX_PtrArray fieldObjects;
2459                 af.GetAllFields(fieldObjects);
2460                 CFX_PtrArray fields;
2461                 GetFieldFromObjects(fieldObjects, fields);
2462                 return m_pInterForm->ResetForm(fields, !(dwFlags & 0x01), TRUE);
2463         }
2464
2465         return m_pInterForm->ResetForm(TRUE);
2466 }
2467
2468 FX_BOOL CPDFSDK_InterForm::DoAction_ImportData(const CPDF_Action& action)
2469 {
2470         return FALSE;
2471 }
2472
2473 void CPDFSDK_InterForm::GetFieldFromObjects(const CFX_PtrArray& objects, CFX_PtrArray& fields)
2474 {
2475         ASSERT(m_pInterForm != NULL);
2476
2477         int iCount = objects.GetSize();
2478         for (int i = 0; i < iCount; i ++)
2479         {
2480                 CPDF_Object* pObject = (CPDF_Object*)objects[i];
2481                 if (pObject == NULL) continue;
2482                 
2483                 int iType = pObject->GetType();
2484                 if (iType == PDFOBJ_STRING)
2485                 {
2486                         CFX_WideString csName = pObject->GetUnicodeText();
2487                         CPDF_FormField* pField = m_pInterForm->GetField(0, csName);
2488                         if (pField != NULL)
2489                                 fields.Add(pField);
2490                 }
2491                 else if (iType == PDFOBJ_DICTIONARY)
2492                 {
2493                         if (m_pInterForm->IsValidFormField(pObject))
2494                                 fields.Add(pObject);
2495                 }
2496         }
2497 }
2498
2499 /* ----------------------------- CPDF_FormNotify ----------------------------- */
2500
2501 int     CPDFSDK_InterForm::BeforeValueChange(const CPDF_FormField* pField, CFX_WideString& csValue)
2502 {
2503         ASSERT(pField != NULL);
2504
2505         CPDF_FormField* pFormField = (CPDF_FormField*)pField;
2506
2507         int nType = pFormField->GetFieldType();
2508         if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD)
2509         {
2510                 FX_BOOL bRC = TRUE;
2511                 OnKeyStrokeCommit(pFormField, csValue, bRC);
2512                 if (bRC) 
2513                 {
2514                         OnValidate(pFormField, csValue, bRC);
2515                         if (bRC)
2516                                 return 1;
2517                         else
2518                                 return -1;
2519                 }
2520                 else
2521                         return -1;
2522         }
2523         else
2524                 return 0;
2525 }
2526
2527 int     CPDFSDK_InterForm::AfterValueChange(const CPDF_FormField* pField)
2528 {
2529         ASSERT(pField != NULL);
2530
2531         CPDF_FormField* pFormField = (CPDF_FormField*)pField;
2532         int nType = pFormField->GetFieldType();
2533
2534         if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD)
2535         {
2536                 this->OnCalculate(pFormField);
2537                 FX_BOOL bFormated = FALSE;
2538                 CFX_WideString sValue = this->OnFormat(pFormField, 0, bFormated);
2539                 if (bFormated)
2540                         this->ResetFieldAppearance(pFormField, sValue, TRUE);
2541                 else
2542                         this->ResetFieldAppearance(pFormField, NULL, TRUE);
2543                 this->UpdateField(pFormField);
2544         }
2545
2546         return 0;
2547 }
2548
2549 int     CPDFSDK_InterForm::BeforeSelectionChange(const CPDF_FormField* pField, CFX_WideString& csValue)
2550 {
2551         ASSERT(pField != NULL);
2552
2553         CPDF_FormField* pFormField = (CPDF_FormField*)pField;
2554
2555         int nType = pFormField->GetFieldType();
2556         if (nType == FIELDTYPE_LISTBOX)
2557         {
2558                 FX_BOOL bRC = TRUE;
2559                 OnKeyStrokeCommit(pFormField, csValue, bRC);
2560                 if (bRC) 
2561                 {
2562                         OnValidate(pFormField, csValue, bRC);
2563                         if (bRC)
2564                                 return 1;
2565                         else
2566                                 return -1;
2567                 }
2568                 else
2569                         return -1;
2570         }
2571         else
2572                 return 0;
2573 }
2574
2575 int     CPDFSDK_InterForm::AfterSelectionChange(const CPDF_FormField* pField)
2576 {
2577         ASSERT(pField != NULL);
2578
2579         CPDF_FormField* pFormField = (CPDF_FormField*)pField;
2580         int nType = pFormField->GetFieldType();
2581
2582         if (nType == FIELDTYPE_LISTBOX)
2583         {
2584                 this->OnCalculate(pFormField);
2585                 this->ResetFieldAppearance(pFormField, NULL, TRUE);
2586                 this->UpdateField(pFormField);
2587         }
2588
2589         return 0;
2590 }
2591
2592 int     CPDFSDK_InterForm::AfterCheckedStatusChange(const CPDF_FormField* pField, const CFX_ByteArray& statusArray)
2593 {
2594         ASSERT(pField != NULL);
2595
2596         CPDF_FormField* pFormField = (CPDF_FormField*)pField;
2597         int nType = pFormField->GetFieldType();
2598
2599         if (nType == FIELDTYPE_CHECKBOX || nType == FIELDTYPE_RADIOBUTTON)
2600         {
2601                 this->OnCalculate(pFormField);
2602                 //this->ResetFieldAppearance(pFormField, NULL);
2603                 this->UpdateField(pFormField);
2604         }
2605
2606         return 0;
2607 }
2608
2609 int     CPDFSDK_InterForm::BeforeFormReset(const CPDF_InterForm* pForm)
2610 {
2611         return 0;
2612 }
2613
2614 int     CPDFSDK_InterForm::AfterFormReset(const CPDF_InterForm* pForm)
2615 {
2616         this->OnCalculate(NULL);
2617
2618         return 0;
2619 }
2620
2621 int     CPDFSDK_InterForm::BeforeFormImportData(const CPDF_InterForm* pForm)
2622 {
2623         return 0;
2624 }
2625
2626 int     CPDFSDK_InterForm::AfterFormImportData(const CPDF_InterForm* pForm)
2627 {
2628         this->OnCalculate(NULL);
2629
2630         return 0;
2631 }
2632
2633 FX_BOOL CPDFSDK_InterForm::IsNeedHighLight(int nFieldType)
2634 {
2635         if(nFieldType <1 || nFieldType > 6)
2636                 return FALSE;
2637         return m_bNeedHightlight[nFieldType-1];
2638 }
2639
2640 void CPDFSDK_InterForm::RemoveAllHighLight()
2641 {
2642         memset((void*)m_bNeedHightlight, 0, 6*sizeof(FX_BOOL));
2643 }
2644 void   CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, int nFieldType)
2645 {
2646         if(nFieldType <0 || nFieldType > 6) return;
2647         switch(nFieldType)
2648         {
2649         case 0:
2650                 {
2651                         for(int i=0; i<6; i++)
2652                         {
2653                                 m_aHighlightColor[i] = clr;
2654                                 m_bNeedHightlight[i] = TRUE;
2655                         }
2656                         break;
2657                 }
2658         default:
2659                 {
2660                         m_aHighlightColor[nFieldType-1] = clr;
2661                         m_bNeedHightlight[nFieldType-1] = TRUE;
2662                         break;
2663                 }
2664         }
2665         
2666 }
2667
2668 FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(int nFieldType)
2669 {
2670         if(nFieldType <0 || nFieldType >6) return FXSYS_RGB(255,255,255);
2671         if(nFieldType == 0)
2672                 return m_aHighlightColor[0];
2673         else
2674                 return m_aHighlightColor[nFieldType-1];
2675 }
2676
2677 /* ------------------------- CBA_AnnotIterator ------------------------- */
2678
2679 CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView, const CFX_ByteString& sType, const CFX_ByteString& sSubType)
2680         :m_pPageView(pPageView),
2681         m_sType(sType),
2682         m_sSubType(sSubType),
2683         m_nTabs(BAI_STRUCTURE)
2684 {
2685         ASSERT(m_pPageView != NULL);
2686
2687         CPDF_Page* pPDFPage = m_pPageView->GetPDFPage();
2688         ASSERT(pPDFPage != NULL);
2689         ASSERT(pPDFPage->m_pFormDict != NULL);
2690
2691         CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetString("Tabs");
2692
2693         if (sTabs == "R")
2694         {
2695                 m_nTabs = BAI_ROW;
2696         }
2697         else if (sTabs == "C")
2698         {
2699                 m_nTabs = BAI_COLUMN;
2700         }
2701         else
2702         {
2703                 m_nTabs = BAI_STRUCTURE;
2704         }
2705
2706         GenerateResults();
2707 }
2708
2709 CBA_AnnotIterator::~CBA_AnnotIterator()
2710 {
2711         m_Annots.RemoveAll();
2712 }
2713
2714 CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot()
2715 {
2716         if (m_Annots.GetSize() > 0)
2717                 return m_Annots[0];
2718         
2719         return NULL;
2720 }
2721
2722 CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot()
2723 {
2724         if (m_Annots.GetSize() > 0)
2725                 return m_Annots[m_Annots.GetSize() - 1];
2726
2727         return NULL;
2728 }
2729
2730 CPDFSDK_Annot* CBA_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot)
2731 {
2732         for (int i=0,sz=m_Annots.GetSize(); i<sz; i++)
2733         {
2734                 if (m_Annots[i] == pAnnot)
2735                 {
2736                         if (i+1 < sz)
2737                                 return m_Annots[i+1];
2738                         else
2739                                 return m_Annots[0];
2740                 }
2741         }
2742
2743         return NULL;
2744 }
2745
2746 CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot)
2747 {
2748         for (int i=0,sz=m_Annots.GetSize(); i<sz; i++)
2749         {
2750                 if (m_Annots[i] == pAnnot)
2751                 {
2752                         if (i-1 >= 0)
2753                                 return m_Annots[i-1];
2754                         else
2755                                 return m_Annots[sz-1];
2756                 }
2757         }
2758
2759         return NULL;
2760 }
2761
2762 int CBA_AnnotIterator::CompareByLeft(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2)
2763 {
2764         ASSERT(p1 != NULL);
2765         ASSERT(p2 != NULL);
2766
2767         CPDF_Rect rcAnnot1 = GetAnnotRect(p1);
2768         CPDF_Rect rcAnnot2 = GetAnnotRect(p2);
2769
2770         if (rcAnnot1.left < rcAnnot2.left)
2771                 return -1;
2772         if (rcAnnot1.left > rcAnnot2.left)
2773                 return 1;
2774         return 0;
2775 }
2776
2777
2778 int CBA_AnnotIterator::CompareByTop(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2)
2779 {
2780         ASSERT(p1 != NULL);
2781         ASSERT(p2 != NULL);
2782
2783         CPDF_Rect rcAnnot1 = GetAnnotRect(p1);
2784         CPDF_Rect rcAnnot2 = GetAnnotRect(p2);
2785
2786         if (rcAnnot1.top < rcAnnot2.top)
2787                 return -1;
2788         if (rcAnnot1.top > rcAnnot2.top)
2789                 return 1;
2790         return 0;
2791 }
2792
2793 void CBA_AnnotIterator::GenerateResults()
2794 {
2795         ASSERT(m_pPageView != NULL);
2796
2797         switch (m_nTabs)
2798         {
2799         case BAI_STRUCTURE:
2800                 {
2801                         for (int i=0,sz=m_pPageView->CountAnnots(); i<sz; i++)
2802                         {
2803                                 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2804                                 ASSERT(pAnnot != NULL);
2805
2806                                 if (pAnnot->GetType() == m_sType 
2807                                         && pAnnot->GetSubType() == m_sSubType)
2808                                         m_Annots.Add(pAnnot);
2809                         }
2810                 }
2811                 break;
2812         case BAI_ROW:
2813                 {
2814                         CPDFSDK_SortAnnots sa;
2815
2816                         {
2817                                 
2818                                 for (int i=0,sz=m_pPageView->CountAnnots(); i<sz; i++)
2819                                 {
2820                                         CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2821                                         ASSERT(pAnnot != NULL);
2822
2823                                         if (pAnnot->GetType() == m_sType 
2824                                                 && pAnnot->GetSubType() == m_sSubType)
2825                                                 sa.Add(pAnnot);
2826                                 }
2827                         }
2828
2829                         if (sa.GetSize() > 0)
2830                         {
2831                                 sa.Sort(CBA_AnnotIterator::CompareByLeft);
2832                         }
2833
2834                         while (sa.GetSize() > 0)
2835                         {
2836                                 int nLeftTopIndex = -1;
2837
2838                                 {
2839                                         FX_FLOAT fTop = 0.0f;
2840
2841                                         for (int i=sa.GetSize()-1; i>=0; i--)
2842                                         {
2843                                                 CPDFSDK_Annot* pAnnot = sa.GetAt(i);
2844                                                 ASSERT(pAnnot != NULL);
2845
2846                                                 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot);
2847
2848                                                 if (rcAnnot.top > fTop)
2849                                                 {
2850                                                         nLeftTopIndex = i;
2851                                                         fTop = rcAnnot.top;
2852                                                 }
2853                                         }
2854                                 }
2855
2856                                 if (nLeftTopIndex >= 0)
2857                                 {
2858                                         CPDFSDK_Annot* pLeftTopAnnot = sa.GetAt(nLeftTopIndex);
2859                                         ASSERT(pLeftTopAnnot != NULL);
2860
2861                                         CPDF_Rect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
2862                                         
2863                                         m_Annots.Add(pLeftTopAnnot);
2864                                         sa.RemoveAt(nLeftTopIndex);
2865
2866                                         CFX_ArrayTemplate<int> aSelect;
2867
2868                                         {
2869                                                 for (int i=0,sz=sa.GetSize(); i<sz; i++)
2870                                                 {
2871                                                         CPDFSDK_Annot* pAnnot = sa.GetAt(i);
2872                                                         ASSERT(pAnnot != NULL);
2873
2874                                                         CPDF_Rect rcAnnot = GetAnnotRect(pAnnot);
2875
2876                                                         FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f;
2877
2878                                                         if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top)
2879                                                                 aSelect.Add(i);
2880                                                 }
2881                                         }
2882
2883                                         {
2884                                                 for (int i=0,sz=aSelect.GetSize(); i<sz; i++)
2885                                                 {
2886                                                         m_Annots.Add(sa[aSelect[i]]);
2887                                                 }
2888                                         }
2889
2890                                         {
2891                                                 for (int i=aSelect.GetSize()-1; i>=0; i--)
2892                                                 {
2893                                                         sa.RemoveAt(aSelect[i]);
2894                                                 }
2895                                         }
2896
2897                                         aSelect.RemoveAll();
2898                                 }
2899                         }
2900                         sa.RemoveAll();
2901                 }
2902                 break;
2903         case BAI_COLUMN:
2904                 {
2905                         CPDFSDK_SortAnnots sa;
2906
2907                         {
2908                                 for (int i=0,sz=m_pPageView->CountAnnots(); i<sz; i++)
2909                                 {
2910                                         CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2911                                         ASSERT(pAnnot != NULL);
2912
2913                                         if (pAnnot->GetType() == m_sType 
2914                                                 && pAnnot->GetSubType() == m_sSubType)
2915                                                 sa.Add(pAnnot);
2916                                 }
2917                         }
2918
2919                         if (sa.GetSize() > 0)
2920                         {
2921                                 sa.Sort(CBA_AnnotIterator::CompareByTop, FALSE);
2922                         }
2923
2924                         while (sa.GetSize() > 0)
2925                         {
2926                                 int nLeftTopIndex = -1;
2927
2928                                 {
2929                                         FX_FLOAT fLeft = -1.0f;
2930
2931                                         for (int i=sa.GetSize()-1; i>=0; i--)
2932                                         {
2933                                                 CPDFSDK_Annot* pAnnot = sa.GetAt(i);
2934                                                 ASSERT(pAnnot != NULL);
2935
2936                                                 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot);
2937
2938                                                 if (fLeft < 0)
2939                                                 {
2940                                                         nLeftTopIndex = 0;
2941                                                         fLeft = rcAnnot.left;
2942                                                 }
2943                                                 else if (rcAnnot.left < fLeft)
2944                                                 {
2945                                                         nLeftTopIndex = i;
2946                                                         fLeft = rcAnnot.left;
2947                                                 }
2948                                         }
2949                                 }
2950
2951                                 if (nLeftTopIndex >= 0)
2952                                 {
2953                                         CPDFSDK_Annot* pLeftTopAnnot = sa.GetAt(nLeftTopIndex);
2954                                         ASSERT(pLeftTopAnnot != NULL);
2955
2956                                         CPDF_Rect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
2957                                         
2958                                         m_Annots.Add(pLeftTopAnnot);
2959                                         sa.RemoveAt(nLeftTopIndex);
2960
2961                                         CFX_ArrayTemplate<int> aSelect;
2962
2963                                         {
2964                                                 for (int i=0,sz=sa.GetSize(); i<sz; i++)
2965                                                 {
2966                                                         CPDFSDK_Annot* pAnnot = sa.GetAt(i);
2967                                                         ASSERT(pAnnot != NULL);
2968
2969                                                         CPDF_Rect rcAnnot = GetAnnotRect(pAnnot);
2970
2971                                                         FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f;
2972
2973                                                         if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right)
2974                                                                 aSelect.Add(i);
2975                                                 }
2976                                         }
2977
2978                                         {
2979                                                 for (int i=0,sz=aSelect.GetSize(); i<sz; i++)
2980                                                 {
2981                                                         m_Annots.Add(sa[aSelect[i]]);
2982                                                 }
2983                                         }
2984
2985                                         {
2986                                                 for (int i=aSelect.GetSize()-1; i>=0; i--)
2987                                                 {
2988                                                         sa.RemoveAt(aSelect[i]);
2989                                                 }
2990                                         }
2991
2992                                         aSelect.RemoveAll();
2993                                 }
2994                         }
2995                         sa.RemoveAll();
2996                 }
2997                 break;
2998         }
2999 }
3000
3001 CPDF_Rect CBA_AnnotIterator::GetAnnotRect(CPDFSDK_Annot* pAnnot)
3002 {
3003         ASSERT(pAnnot != NULL);
3004
3005         CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
3006         ASSERT(pPDFAnnot != NULL);
3007
3008         CPDF_Rect rcAnnot;
3009         pPDFAnnot->GetRect(rcAnnot);
3010
3011         return rcAnnot;
3012 }
3013