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