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