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