Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / fpdfsdk / src / fpdfxfa / fpdfxfa_doc.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4  \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #include "../../include/fsdk_define.h"\r
8 #include "../../include/fpdfxfa/fpdfxfa_doc.h"\r
9 #include "../../include/fpdfformfill.h"\r
10 #include "../../include/fsdk_mgr.h"\r
11 #include "../../include/fpdfxfa/fpdfxfa_app.h"\r
12 #include "../../include/fpdfxfa/fpdfxfa_util.h"\r
13 #include "../../include/fpdfxfa/fpdfxfa_page.h"\r
14 #include "../../include/javascript/IJavaScript.h"\r
15 \r
16 \r
17 \r
18 #define IDS_XFA_StringMonth_April "April"\r
19 #define IDS_XFA_StringMonth_May "May"\r
20 #define IDS_XFA_StringMonth_June "June"\r
21 #define IDS_XFA_StringMonth_July "July"\r
22 #define IDS_XFA_StringMonth_Aug "August"\r
23 #define IDS_XFA_StringMonth_Sept "September"\r
24 #define IDS_XFA_StringMonth_Oct "October"\r
25 #define IDS_XFA_StringMonth_Nov "November"\r
26 #define IDS_XFA_StringMonth_Dec "December"\r
27 #define IDS_XFA_String_Today    "Today"\r
28 #define IDS_XFA_ValidateLimit   "Message limit exceeded. Remaining %d validation errors not reported."\r
29 #define IDS_XFA_Validate_Input  "At least one required field was empty. Please fill in the required fields\r\n(highlighted) before continuing."\r
30 \r
31 // submit\r
32 #define FXFA_CONFIG                             0x00000001\r
33 #define FXFA_TEMPLATE                   0x00000010\r
34 #define FXFA_LOCALESET                  0x00000100\r
35 #define FXFA_DATASETS                   0x00001000\r
36 #define FXFA_XMPMETA                    0x00010000\r
37 #define FXFA_XFDF                               0x00100000\r
38 #define FXFA_FORM                               0x01000000\r
39 #define FXFA_PDF                                0x10000000\r
40 \r
41 #ifndef _WIN32\r
42 extern void SetLastError(int err);\r
43 \r
44 extern int GetLastError();\r
45 #endif\r
46 \r
47 CPDFXFA_Document::CPDFXFA_Document(CPDF_Document* pPDFDoc, CPDFXFA_App* pProvider) :\r
48         m_pPDFDoc(pPDFDoc),\r
49         m_pApp(pProvider),\r
50         m_pXFADoc(NULL),\r
51         m_pXFADocView(NULL),\r
52         m_iDocType(DOCTYPE_PDF),\r
53         m_pJSContext(NULL),\r
54         m_pSDKDoc(NULL)\r
55 {\r
56         m_XFAPageList.RemoveAll();\r
57 }\r
58 \r
59 CPDFXFA_Document::~CPDFXFA_Document()\r
60 {\r
61         if (m_pPDFDoc)\r
62         {\r
63                 CPDF_Parser* pParser = (CPDF_Parser*)m_pPDFDoc->GetParser();\r
64                 if (pParser == NULL) \r
65                 {\r
66                         delete m_pPDFDoc;\r
67                 }else \r
68                 {\r
69                         delete pParser;\r
70                 }\r
71                 m_pPDFDoc = NULL;\r
72         }\r
73         if (m_pXFADoc)\r
74         {\r
75                 IXFA_App* pApp = m_pApp->GetXFAApp();\r
76                 if (pApp) \r
77                 {\r
78                         IXFA_DocHandler* pDocHandler = pApp->GetDocHandler();\r
79                         if (pDocHandler)\r
80                         {\r
81                                 pDocHandler->CloseDoc(m_pXFADoc);\r
82                                 pDocHandler->ReleaseDoc(m_pXFADoc);\r
83                                 m_pXFADoc = NULL;\r
84                         }       \r
85                 }\r
86         }\r
87 \r
88         if (m_pJSContext)\r
89         {\r
90                 if (m_pSDKDoc && m_pSDKDoc->GetEnv())\r
91                 {\r
92                         m_pSDKDoc->GetEnv()->GetJSRuntime()->ReleaseContext(m_pJSContext);\r
93                         m_pJSContext = NULL;\r
94                 }\r
95         }\r
96 \r
97         \r
98         if (m_pSDKDoc)\r
99                 delete m_pSDKDoc;\r
100         m_pSDKDoc = NULL;\r
101 }\r
102 \r
103 FX_BOOL CPDFXFA_Document::LoadXFADoc()\r
104 {\r
105         if (!m_pPDFDoc) \r
106                 return FALSE;\r
107 \r
108         m_XFAPageList.RemoveAll();\r
109 \r
110         int iDocType = DOCTYPE_PDF;\r
111         FX_BOOL hasXFAField = FPDF_HasXFAField(m_pPDFDoc, iDocType);\r
112 \r
113         if (hasXFAField)\r
114         {\r
115                 IXFA_App* pApp = m_pApp->GetXFAApp();\r
116                 if (pApp)\r
117                 {\r
118                         m_pXFADoc = pApp->CreateDoc(this, m_pPDFDoc);\r
119                         if (!m_pXFADoc)\r
120                         {\r
121                                 SetLastError(FPDF_ERR_XFALOAD);\r
122                                 return FALSE;\r
123                         }\r
124 \r
125                         IXFA_DocHandler* pDocHandler = pApp->GetDocHandler();\r
126                         if (pDocHandler)\r
127                         {\r
128                                 int iStatus = pDocHandler->StartLoad(m_pXFADoc);\r
129                                 iStatus = pDocHandler->DoLoad(m_pXFADoc, NULL);\r
130                                 if (iStatus != 100) \r
131                                 {\r
132                                         pDocHandler->CloseDoc(m_pXFADoc);\r
133                                         pDocHandler->ReleaseDoc(m_pXFADoc);\r
134                                         m_pXFADoc = NULL;\r
135 \r
136                                         SetLastError(FPDF_ERR_XFALOAD);\r
137                                         return FALSE;\r
138                                 }\r
139                                 pDocHandler->StopLoad(m_pXFADoc);\r
140                                 pDocHandler->SetJSERuntime(m_pXFADoc, m_pApp->GetJSERuntime());\r
141 \r
142                                 if (pDocHandler->GetDocType(m_pXFADoc) == XFA_DOCTYPE_Dynamic)\r
143                                         m_iDocType = DOCTYPE_DYNIMIC_XFA;\r
144                                 else\r
145                                         m_iDocType = DOCTYPE_STATIC_XFA;\r
146 \r
147                                 m_pXFADocView = pDocHandler->CreateDocView(m_pXFADoc, XFA_DOCVIEW_View);\r
148                                 FXSYS_assert(m_pXFADocView);\r
149 \r
150                                 if (m_pXFADocView->StartLayout() < 0) \r
151                                 {\r
152                                         pDocHandler->CloseDoc(m_pXFADoc);\r
153                                         pDocHandler->ReleaseDoc(m_pXFADoc);\r
154                                         m_pXFADoc = NULL;\r
155 \r
156                                         SetLastError(FPDF_ERR_XFALAYOUT);\r
157                                         return FALSE;\r
158                                 }\r
159                                 else\r
160                                 {\r
161                                         m_pXFADocView->DoLayout(NULL);\r
162                                         m_pXFADocView->StopLayout();\r
163 \r
164                                         return TRUE;\r
165                                 }                               \r
166                         }\r
167 \r
168                         return FALSE;\r
169                 }\r
170 \r
171                 return FALSE;\r
172         }\r
173         \r
174         return TRUE;\r
175 }\r
176 \r
177 int CPDFXFA_Document::GetPageCount()\r
178 {\r
179         if (!m_pPDFDoc && !m_pXFADoc)\r
180                 return 0;\r
181 \r
182         switch (m_iDocType)\r
183         {\r
184         case DOCTYPE_PDF:\r
185         case DOCTYPE_STATIC_XFA:\r
186                 if (m_pPDFDoc)\r
187                         return m_pPDFDoc->GetPageCount();\r
188         case DOCTYPE_DYNIMIC_XFA:\r
189                 if (m_pXFADoc)\r
190                         return m_pXFADocView->CountPageViews();\r
191         default:\r
192                 return 0;\r
193         }\r
194 \r
195         return 0;\r
196 }\r
197 \r
198 CPDFXFA_Page* CPDFXFA_Document::GetPage(int page_index)\r
199 {\r
200         if (!m_pPDFDoc && !m_pXFADoc)\r
201                 return NULL;\r
202 \r
203         CPDFXFA_Page* pPage = NULL;\r
204         if (m_XFAPageList.GetSize())\r
205         {\r
206                 pPage = m_XFAPageList.GetAt(page_index);\r
207                 if (pPage)\r
208                         pPage->AddRef();\r
209         }\r
210         else \r
211         {\r
212                 m_XFAPageList.SetSize(GetPageCount());\r
213         }\r
214 \r
215         if (!pPage) \r
216         {\r
217                 pPage = FX_NEW CPDFXFA_Page(this, page_index);\r
218                 FX_BOOL bRet = pPage->LoadPage();\r
219                 if (!bRet) {\r
220                         delete pPage;\r
221                         return NULL;\r
222                 }\r
223 \r
224                 m_XFAPageList.SetAt(page_index, pPage);\r
225         }\r
226 \r
227         return pPage;\r
228 }\r
229 \r
230 CPDFXFA_Page* CPDFXFA_Document::GetPage(IXFA_PageView* pPage)\r
231 {\r
232         if (!pPage)\r
233                 return NULL;\r
234 \r
235         if (!m_pXFADoc)\r
236                 return NULL;\r
237 \r
238         if (m_iDocType != DOCTYPE_DYNIMIC_XFA)\r
239                 return NULL;\r
240 \r
241         int nSize = m_XFAPageList.GetSize();\r
242         for (int i=0; i<nSize; i++)\r
243         {\r
244                 CPDFXFA_Page* pTempPage = m_XFAPageList.GetAt(i);\r
245                 if (!pTempPage) continue;\r
246                 if (pTempPage->GetXFAPageView() && pTempPage->GetXFAPageView() == pPage)\r
247                         return pTempPage;\r
248         }\r
249 \r
250         return NULL;\r
251 }\r
252 \r
253 void CPDFXFA_Document::RemovePage(CPDFXFA_Page* page)\r
254 {\r
255         m_XFAPageList.SetAt(page->GetPageIndex(), NULL);\r
256 }\r
257 \r
258 CPDFSDK_Document* CPDFXFA_Document::GetSDKDocument(CPDFDoc_Environment* pFormFillEnv)\r
259 {\r
260         if (!pFormFillEnv)\r
261                 return m_pSDKDoc;\r
262 \r
263         if (m_pSDKDoc)\r
264                 return m_pSDKDoc;\r
265 \r
266         m_pSDKDoc = new CPDFSDK_Document(this, pFormFillEnv);\r
267         if (!m_pSDKDoc)\r
268                 return NULL;\r
269 \r
270         return m_pSDKDoc;\r
271 }\r
272 \r
273 void CPDFXFA_Document::ReleaseSDKDoc()\r
274 {\r
275         if (m_pSDKDoc)\r
276                 delete m_pSDKDoc;\r
277 \r
278         m_pSDKDoc = NULL;\r
279 }\r
280 \r
281 void CPDFXFA_Document::FXRect2PDFRect(const CFX_RectF& fxRectF, CPDF_Rect& pdfRect)\r
282 {\r
283         pdfRect.left = fxRectF.left;\r
284         pdfRect.top = fxRectF.bottom();\r
285         pdfRect.right = fxRectF.right();\r
286         pdfRect.bottom = fxRectF.top;\r
287 }\r
288 \r
289 //////////////////////////////////////////////////////////////////////////\r
290 void CPDFXFA_Document::SetChangeMark(XFA_HDOC hDoc)\r
291 {\r
292         if (hDoc == m_pXFADoc && m_pSDKDoc)\r
293         {\r
294                 m_pSDKDoc->SetChangeMark();\r
295         }\r
296 }\r
297 \r
298 FX_BOOL CPDFXFA_Document::GetChangeMark(XFA_HDOC hDoc)\r
299 {\r
300         if (hDoc == m_pXFADoc && m_pSDKDoc)\r
301                 return m_pSDKDoc->GetChangeMark();\r
302         return FALSE;\r
303 }\r
304 \r
305 void CPDFXFA_Document::InvalidateRect(IXFA_PageView* pPageView, const CFX_RectF& rt, FX_DWORD dwFlags /* = 0 */)\r
306 {\r
307         if (!m_pXFADoc || !m_pSDKDoc)\r
308                 return;\r
309 \r
310         if (m_iDocType != DOCTYPE_DYNIMIC_XFA)\r
311                 return;\r
312 \r
313         CPDF_Rect rcPage;\r
314         FXRect2PDFRect(rt, rcPage);\r
315 \r
316         CPDFXFA_Page* pPage = GetPage(pPageView);\r
317 \r
318         if (pPage == NULL)\r
319                 return;\r
320 \r
321         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
322         if (!pEnv)\r
323                 return;\r
324 \r
325         pEnv->FFI_Invalidate((FPDF_PAGE)pPage, rcPage.left, rcPage.bottom, rcPage.right, rcPage.top);\r
326 }\r
327 \r
328 void CPDFXFA_Document::InvalidateRect(XFA_HWIDGET hWidget, FX_DWORD dwFlags /* = 0 */)\r
329 {\r
330         if (!hWidget)\r
331                 return;\r
332 \r
333         if (!m_pXFADoc || !m_pSDKDoc || !m_pXFADocView)\r
334                 return;\r
335         \r
336         if (m_iDocType != DOCTYPE_DYNIMIC_XFA)\r
337                 return;\r
338 \r
339         IXFA_WidgetHandler* pWidgetHandler = m_pXFADocView->GetWidgetHandler();\r
340         if (!pWidgetHandler)\r
341                 return;\r
342 \r
343         IXFA_PageView* pPageView = pWidgetHandler->GetPageView(hWidget);\r
344         if (!pPageView)\r
345                 return;\r
346 \r
347         CFX_RectF rect;\r
348         pWidgetHandler->GetRect(hWidget, rect);\r
349         InvalidateRect(pPageView, rect, dwFlags);\r
350 }\r
351 \r
352 void CPDFXFA_Document::DisplayCaret(XFA_HWIDGET hWidget, FX_BOOL bVisible, const CFX_RectF* pRtAnchor)\r
353 {\r
354         if (!hWidget || pRtAnchor == NULL) \r
355                 return;\r
356 \r
357         if (!m_pXFADoc || !m_pSDKDoc || !m_pXFADocView)\r
358                 return;\r
359 \r
360         if (m_iDocType != DOCTYPE_DYNIMIC_XFA)\r
361                 return;\r
362 \r
363         IXFA_WidgetHandler* pWidgetHandler = m_pXFADocView->GetWidgetHandler();\r
364         if (!pWidgetHandler)\r
365                 return;\r
366 \r
367         IXFA_PageView* pPageView = pWidgetHandler->GetPageView(hWidget);\r
368         if (!pPageView)\r
369                 return;\r
370 \r
371         CPDFXFA_Page* pPage = GetPage(pPageView);\r
372 \r
373         if (pPage == NULL)\r
374                 return;\r
375 \r
376         CPDF_Rect rcCaret;\r
377         FXRect2PDFRect(*pRtAnchor, rcCaret);\r
378 \r
379         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
380         if (!pEnv)\r
381                 return;\r
382 \r
383         pEnv->FFI_DisplayCaret((FPDF_PAGE)pPage, bVisible, rcCaret.left, rcCaret.top, rcCaret.right, rcCaret.bottom);\r
384 \r
385 }\r
386 \r
387 FX_BOOL CPDFXFA_Document::GetPopupPos(XFA_HWIDGET hWidget, FX_FLOAT fMinPopup, FX_FLOAT fMaxPopup, const CFX_RectF &rtAnchor, CFX_RectF &rtPopup)\r
388 {\r
389         if (NULL == hWidget)\r
390         {\r
391                 return FALSE;\r
392         }\r
393         IXFA_PageView* pXFAPageView = m_pXFADocView->GetWidgetHandler()->GetPageView(hWidget);\r
394         if (NULL == pXFAPageView)\r
395         {\r
396                 return FALSE;\r
397         }\r
398         CPDFXFA_Page* pPage = GetPage(pXFAPageView);\r
399         if (pPage == NULL)\r
400                 return FALSE;\r
401 \r
402         CXFA_WidgetAcc* pWidgetAcc = m_pXFADocView->GetWidgetHandler()->GetDataAcc(hWidget);\r
403 \r
404         int nRotate = pWidgetAcc->GetRotate();\r
405         \r
406         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
407         if (pEnv == NULL)\r
408                 return FALSE;\r
409         FS_RECTF pageViewRect;\r
410         pEnv->FFI_GetPageViewRect(pPage, pageViewRect);\r
411 \r
412         CPDF_Rect rcAnchor;\r
413 \r
414         rcAnchor.left = rtAnchor.left;\r
415         rcAnchor.top = rtAnchor.bottom();\r
416         rcAnchor.right = rtAnchor.right();\r
417         rcAnchor.bottom = rtAnchor.top;\r
418         \r
419         int t1,t2,t;\r
420         FX_DWORD dwPos; FX_FLOAT fPoupHeight;\r
421         switch(nRotate)\r
422         {\r
423                 \r
424         case 90:\r
425                 {\r
426                         t1 = (int)(pageViewRect.right - rcAnchor.right);\r
427                         t2 = (int)(rcAnchor.left  - pageViewRect.left);\r
428                         if (rcAnchor.bottom < pageViewRect.bottom) \r
429                         {\r
430                                 rtPopup.left += rcAnchor.bottom - pageViewRect.bottom;\r
431                         }\r
432                  \r
433                         break;\r
434                 }\r
435                 \r
436         case 180:\r
437                 {\r
438                         t2 = (int)(pageViewRect.top - rcAnchor.top);\r
439                         t1 = (int)(rcAnchor.bottom - pageViewRect.bottom); \r
440                         if (rcAnchor.left < pageViewRect.left) \r
441                         {\r
442                                 rtPopup.left += rcAnchor.left - pageViewRect.left; \r
443                         }\r
444                         break;\r
445                 }\r
446         case 270:\r
447                 {\r
448                         t1 = (int)(rcAnchor.left  - pageViewRect.left);\r
449                         t2 = (int)(pageViewRect.right - rcAnchor.right);\r
450                 \r
451                         if (rcAnchor.top > pageViewRect.top) \r
452                         {\r
453                                 rtPopup.left -= rcAnchor.top - pageViewRect.top; \r
454                         }\r
455                         break;\r
456                 }\r
457         case 0:\r
458         default:\r
459                 {\r
460                         t1 = (int)(pageViewRect.top - rcAnchor.top);\r
461                         t2 = (int)(rcAnchor.bottom - pageViewRect.bottom); \r
462                         if (rcAnchor.right > pageViewRect.right) \r
463                         {\r
464                                 rtPopup.left -= rcAnchor.right - pageViewRect.right; \r
465                         }\r
466                         break;\r
467                 }\r
468                 \r
469         }\r
470 \r
471         if (t1 <= 0 && t2 <= 0)\r
472         {\r
473                 return FALSE;\r
474         }\r
475         if (t1 <= 0)\r
476         {\r
477                 t = t2;\r
478                 dwPos = 1;\r
479         }\r
480         else if(t2 <= 0)\r
481         {\r
482                 t = t1;\r
483                 dwPos = 0;\r
484         }\r
485         else if (t1 > t2 )\r
486         {\r
487                 t = t1;\r
488                 dwPos = 0;\r
489         }\r
490         else \r
491         {\r
492                 t = t2;\r
493                 dwPos = 1;\r
494         }\r
495         if (t<fMinPopup)\r
496         {\r
497                 fPoupHeight = fMinPopup;\r
498         }\r
499         else if (t > fMaxPopup)\r
500         {\r
501                 fPoupHeight = fMaxPopup;\r
502         }\r
503         else\r
504         {\r
505                 fPoupHeight = (FX_FLOAT)t;\r
506         }\r
507 \r
508         switch(nRotate)\r
509         {\r
510                 \r
511         case 0:\r
512         case 180:\r
513                 {\r
514                         if (dwPos == 0)\r
515                         {\r
516                                 rtPopup.top = rtAnchor.height;\r
517                                 rtPopup.height = fPoupHeight;\r
518                         }\r
519                         else\r
520                         {\r
521                                 rtPopup.top = - fPoupHeight;\r
522                                 rtPopup.height = fPoupHeight;\r
523                         }\r
524                         break;\r
525                 }\r
526         case  90:\r
527         case  270:\r
528                 {\r
529                         if (dwPos == 0)\r
530                         {\r
531                                 rtPopup.top = rtAnchor.width;\r
532                                 rtPopup.height = fPoupHeight;\r
533                         }\r
534                         else\r
535                         {\r
536                                 rtPopup.top = - fPoupHeight;\r
537                                 rtPopup.height = fPoupHeight;\r
538                         }\r
539                         break;\r
540                 }\r
541         default:\r
542                         break;\r
543         }\r
544         \r
545         return TRUE;\r
546 }\r
547 \r
548 FX_BOOL CPDFXFA_Document::PopupMenu(XFA_HWIDGET hWidget, CFX_PointF ptPopup, const CFX_RectF* pRectExclude)\r
549 {\r
550         if (NULL == hWidget)\r
551         {\r
552                 return FALSE;\r
553         }\r
554         IXFA_PageView* pXFAPageView = m_pXFADocView->GetWidgetHandler()->GetPageView(hWidget);\r
555         if (pXFAPageView == NULL)\r
556                 return FALSE;\r
557         CPDFXFA_Page* pPage = GetPage(pXFAPageView);\r
558 \r
559         if (pPage == NULL)\r
560                 return FALSE;\r
561 \r
562         int menuFlag = 0;\r
563 \r
564         IXFA_MenuHandler* pXFAMenuHander = m_pApp->GetXFAApp()->GetMenuHandler();\r
565         if (pXFAMenuHander->CanUndo(hWidget))\r
566                 menuFlag |= FXFA_MEMU_UNDO;\r
567         if (pXFAMenuHander->CanRedo(hWidget))\r
568                 menuFlag |= FXFA_MEMU_REDO;\r
569         if (pXFAMenuHander->CanPaste(hWidget))\r
570                 menuFlag |= FXFA_MEMU_PASTE;\r
571         if (pXFAMenuHander->CanCopy(hWidget))\r
572                 menuFlag |= FXFA_MEMU_COPY;\r
573         if (pXFAMenuHander->CanCut(hWidget))\r
574                 menuFlag |= FXFA_MEMU_CUT;\r
575         if (pXFAMenuHander->CanSelectAll(hWidget))\r
576                 menuFlag |= FXFA_MEMU_SELECTALL;\r
577 \r
578 \r
579         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
580         if (pEnv == NULL)\r
581                 return FALSE;\r
582 \r
583         return pEnv->FFI_PopupMenu(pPage, hWidget, menuFlag, ptPopup, NULL);\r
584 }\r
585 \r
586 void CPDFXFA_Document::PageViewEvent(IXFA_PageView* pPageView, FX_DWORD dwFlags)\r
587 {\r
588         if (m_iDocType != DOCTYPE_DYNIMIC_XFA)\r
589                 return;\r
590 \r
591         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
592         if (pEnv == NULL)\r
593                 return;\r
594 \r
595         CPDFXFA_Page* pPage = GetPage(pPageView);\r
596         if (pPage == NULL)\r
597                 return;\r
598 \r
599         if (dwFlags == FXFA_PAGEVIEWEVENT_POSTADDED)\r
600         {\r
601                 //pEnv->FFI_PageEvent(pPage, FXFA_PAGEVIEWEVENT_POSTADDED);\r
602         }\r
603         else if (dwFlags == FXFA_PAGEVIEWEVENT_POSTREMOVED)\r
604         {\r
605                 //pEnv->FFI_PageEvent(pPage, FXFA_PAGEVIEWEVENT_POSTREMOVED);\r
606                 //RemovePage(pPage);\r
607                 //delete pPage;\r
608         }\r
609 }\r
610 \r
611 void CPDFXFA_Document::WidgetEvent(XFA_HWIDGET hWidget, CXFA_WidgetAcc* pWidgetData, FX_DWORD dwEvent, FX_LPVOID pParam, FX_LPVOID pAdditional)\r
612 {\r
613         if (m_iDocType != DOCTYPE_DYNIMIC_XFA || NULL == hWidget)\r
614                 return;\r
615         \r
616         int pageViewCount = m_pSDKDoc->GetPageViewCount();\r
617 \r
618         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
619         if (pEnv == NULL)\r
620                 return;\r
621 \r
622         if (NULL == hWidget) return;\r
623 \r
624         IXFA_PageView* pPageView = m_pXFADocView->GetWidgetHandler()->GetPageView(hWidget);\r
625 \r
626         if (pPageView == NULL)\r
627                 return;\r
628         CPDFXFA_Page* pXFAPage = GetPage(pPageView);\r
629         if (pXFAPage == NULL)\r
630                 return;\r
631 \r
632         CPDFSDK_PageView* pSdkPageView = m_pSDKDoc->GetPageView(pXFAPage);\r
633 \r
634         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();\r
635 \r
636 \r
637         if (dwEvent == XFA_WIDGETEVENT_PostAdded)\r
638         {\r
639 //                      CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(hWidget, pSdkPageView);\r
640 //                      pAnnotHandlerMgr->Annot_OnLoad(pAnnot);\r
641 \r
642                         //pEnv->FFI_WidgetEvent(hWidget, XFA_WIDGETEVENT_PostAdded);\r
643 //              IXFA_PageView* pOldPageView = (IXFA_PageView*)pAdditional;\r
644 //              if (pOldPageView)\r
645 //              {\r
646 //                      CPDFXFA_Page* pDestPage = m_pSDKDoc->GetPageView((IXFA_PageView*)pOldPageView);\r
647 //                      ASSERT(pDestPage);\r
648 //                      CPDFSDK_Annot* pAnnot = pDestPage->GetAnnotByXFAWidget(hWidget);\r
649 //                      if (pAnnot)\r
650 //                      {\r
651 //                              if (m_pSDKDoc->GetFocusAnnot() == pAnnot)\r
652 //                              {\r
653 //                                      m_pSDKDoc->SetFocusAnnot(NULL);\r
654 //                              }\r
655 //                              pDestPage->DeleteAnnot(pAnnot);\r
656 //                      }\r
657 //              }\r
658                 pSdkPageView->AddAnnot(hWidget);\r
659 \r
660         }\r
661         else if (dwEvent == XFA_WIDGETEVENT_PreRemoved)\r
662         {       \r
663                 CPDFSDK_Annot* pAnnot = pSdkPageView->GetAnnotByXFAWidget(hWidget);\r
664                 if (pAnnot) {\r
665                         pSdkPageView->DeleteAnnot(pAnnot);\r
666                         //pEnv->FFI_WidgetEvent(hWidget, XFA_WIDGETEVENT_PreRemoved);\r
667                 }\r
668         }\r
669 }\r
670 \r
671 FX_INT32 CPDFXFA_Document::CountPages(XFA_HDOC hDoc)\r
672 {\r
673         if (hDoc == m_pXFADoc && m_pSDKDoc)\r
674         {\r
675                 return GetPageCount();\r
676         }\r
677         return 0;\r
678 }\r
679 FX_INT32 CPDFXFA_Document::GetCurrentPage(XFA_HDOC hDoc)\r
680 {\r
681         if (hDoc != m_pXFADoc || !m_pSDKDoc)\r
682                 return -1;\r
683         if (m_iDocType != DOCTYPE_DYNIMIC_XFA)\r
684                 return -1;\r
685 \r
686         \r
687         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
688         if (pEnv == NULL)\r
689                 return -1;\r
690 \r
691         return pEnv->FFI_GetCurrentPageIndex(this);\r
692 }\r
693 void CPDFXFA_Document::SetCurrentPage(XFA_HDOC hDoc, FX_INT32 iCurPage)\r
694 {\r
695         if (hDoc != m_pXFADoc || !m_pSDKDoc)\r
696                 return;\r
697         if (m_iDocType != DOCTYPE_DYNIMIC_XFA)\r
698                 return;\r
699                 \r
700         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
701         if (pEnv == NULL)\r
702                 return;\r
703 \r
704         pEnv->FFI_SetCurrentPage(this, iCurPage);\r
705 }\r
706 FX_BOOL CPDFXFA_Document::IsCalculationsEnabled(XFA_HDOC hDoc)\r
707 {\r
708         if (hDoc != m_pXFADoc || !m_pSDKDoc)\r
709                 return FALSE;\r
710         if (m_pSDKDoc->GetInterForm())\r
711                 return m_pSDKDoc->GetInterForm()->IsXfaCalculateEnabled();\r
712 \r
713         return FALSE;\r
714 \r
715 }\r
716 void CPDFXFA_Document::SetCalculationsEnabled(XFA_HDOC hDoc, FX_BOOL bEnabled)\r
717 {\r
718         if (hDoc != m_pXFADoc || !m_pSDKDoc)\r
719                 return;\r
720         if (m_pSDKDoc->GetInterForm())\r
721                 m_pSDKDoc->GetInterForm()->XfaEnableCalculate(bEnabled);\r
722 }\r
723 \r
724 void CPDFXFA_Document::GetTitle(XFA_HDOC hDoc, CFX_WideString &wsTitle)\r
725 {\r
726         if (hDoc != m_pXFADoc)\r
727                 return;\r
728         if (m_pPDFDoc == NULL)\r
729                 return;\r
730         CPDF_Dictionary* pInfoDict = m_pPDFDoc->GetInfo();\r
731 \r
732         if (pInfoDict == NULL)\r
733                 return;\r
734         \r
735         CFX_ByteString csTitle = pInfoDict->GetString("Title");\r
736     wsTitle = wsTitle.FromLocal(csTitle.GetBuffer(csTitle.GetLength()));\r
737         csTitle.ReleaseBuffer(csTitle.GetLength());\r
738 }\r
739 void CPDFXFA_Document::SetTitle(XFA_HDOC hDoc, FX_WSTR wsTitle)\r
740 {\r
741         if (hDoc != m_pXFADoc)\r
742                 return;\r
743         if (m_pPDFDoc == NULL)\r
744                 return;\r
745         CPDF_Dictionary* pInfoDict = m_pPDFDoc->GetInfo();\r
746 \r
747         if (pInfoDict == NULL)\r
748                 return;\r
749         pInfoDict->SetAt("Title", FX_NEW CPDF_String(wsTitle));\r
750 }\r
751 void CPDFXFA_Document::ExportData(XFA_HDOC hDoc, FX_WSTR wsFilePath, FX_BOOL bXDP)\r
752 {\r
753         if (hDoc != m_pXFADoc)\r
754                 return;\r
755         if (m_iDocType != DOCTYPE_DYNIMIC_XFA && m_iDocType != DOCTYPE_STATIC_XFA)\r
756                 return;\r
757         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
758         if (pEnv == NULL)\r
759                 return;\r
760         int fileType = bXDP?FXFA_SAVEAS_XDP:FXFA_SAVEAS_XML;\r
761         CFX_ByteString bs = CFX_WideString(wsFilePath).UTF16LE_Encode();\r
762 \r
763         if (wsFilePath.IsEmpty()) {\r
764                 if (!pEnv->GetFormFillInfo() || pEnv->GetFormFillInfo()->m_pJsPlatform == NULL)\r
765                         return;\r
766                 CFX_WideString filepath = pEnv->JS_fieldBrowse();\r
767                 bs = filepath.UTF16LE_Encode();\r
768         }\r
769         int len = bs.GetLength()/sizeof(unsigned short);\r
770         FPDF_FILEHANDLER* pFileHandler = pEnv->FFI_OpenFile(bXDP?FXFA_SAVEAS_XDP:FXFA_SAVEAS_XML, (FPDF_WIDESTRING)bs.GetBuffer(len*sizeof(unsigned short)), "wb");\r
771         bs.ReleaseBuffer(len*sizeof(unsigned short));\r
772 \r
773         if (pFileHandler == NULL)\r
774                 return;\r
775 \r
776         CFPDF_FileStream fileWrite(pFileHandler);\r
777 \r
778         IXFA_DocHandler *pXFADocHander = m_pApp->GetXFAApp()->GetDocHandler();\r
779         CFX_ByteString content;\r
780         if (fileType == FXFA_SAVEAS_XML)\r
781         {\r
782                 content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";\r
783                 fileWrite.WriteBlock((FX_LPCSTR)content, fileWrite.GetSize(), content.GetLength());\r
784                 CFX_WideStringC data(L"data");\r
785                 if( pXFADocHander->SavePackage(m_pXFADocView->GetDoc(),data, &fileWrite))\r
786                 {\r
787                         NULL;\r
788                 }\r
789         }\r
790         /*else if (fileType == FXFA_FILE_STATIC_XDP)\r
791         {\r
792                 content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";\r
793                 fileWrite.WriteBlock((FX_LPCSTR)content, fileWrite.GetSize(), content.GetLength());\r
794                 CFX_WideStringC data(L"data");\r
795                 if( pXFADocHander->SavePackage(m_pXFADocView->GetDoc(), data, &fileWrite))\r
796                 {\r
797                         NULL;\r
798                 }\r
799                 CFX_WideString wPath = pEnv->FFI_GetFilePath(pFileHandler);\r
800 //              CFX_WideString wPath;\r
801 //              wPath.FromUTF16LE(filePath);\r
802                 CFX_ByteString bPath = wPath.UTF8Encode();\r
803                 CFX_ByteString szFormat = "\n<pdf href=\"%s\" xmlns=\"http://ns.adobe.com/xdp/pdf/\"/>";\r
804                 content.Format(szFormat,(char*)(FX_LPCSTR)bPath);\r
805                 fileWrite.WriteBlock((FX_LPCSTR)content,fileWrite.GetSize(), content.GetLength());\r
806         }\r
807         */\r
808         else if (fileType == FXFA_SAVEAS_XDP)\r
809         {       \r
810                 if (m_pPDFDoc == NULL)\r
811                         return;\r
812                 CPDF_Dictionary* pRoot = m_pPDFDoc->GetRoot();\r
813                 if (pRoot == NULL)\r
814                         return;\r
815                 CPDF_Dictionary* pAcroForm = pRoot->GetDict("AcroForm");\r
816                 if (NULL == pAcroForm)\r
817                         return;\r
818                 CPDF_Object* pXFA = pAcroForm->GetElement("XFA");\r
819                 if (pXFA == NULL)\r
820                         return;\r
821                 if (pXFA->GetType() != PDFOBJ_ARRAY)\r
822                         return;\r
823                 CPDF_Array* pArray = pXFA->GetArray();\r
824                 if (NULL == pArray)\r
825                         return;\r
826                 int size = pArray->GetCount();\r
827                 int iFormIndex = -1;\r
828                 int iDataSetsIndex = -1;\r
829                 for (int i=1; i<size;i+=2)\r
830                 {\r
831                         CPDF_Object* pPDFObj = pArray->GetElement(i);\r
832                         CPDF_Object* pPrePDFObj = pArray->GetElement(i-1);\r
833                         if(pPrePDFObj->GetType() != PDFOBJ_STRING)\r
834                                 continue;\r
835                         if (pPDFObj->GetType() != PDFOBJ_REFERENCE)\r
836                                 continue;\r
837                         CPDF_Object* pDirectObj = pPDFObj->GetDirect();\r
838                         if(pDirectObj->GetType() != PDFOBJ_STREAM)\r
839                                 continue;\r
840                         if (pPrePDFObj->GetString()=="form")\r
841                         {\r
842                                 CFX_WideStringC form(L"form");\r
843                                 pXFADocHander->SavePackage(m_pXFADocView->GetDoc(), form, &fileWrite);\r
844                         }\r
845                         else if (pPrePDFObj->GetString()=="datasets")\r
846                         {\r
847                                 CFX_WideStringC datasets(L"datasets");\r
848                                 pXFADocHander->SavePackage(m_pXFADocView->GetDoc(), datasets, &fileWrite);\r
849                         }\r
850                         else \r
851                         {       \r
852                                 if (i == size-1)\r
853                                 {\r
854                                         //CFX_WideString wPath = pEnv->FFI_GetFilePath(pFileHandler);\r
855                                         CFX_WideString wPath = CFX_WideString::FromUTF16LE((unsigned short*)(FX_LPCSTR)bs, bs.GetLength()/sizeof(unsigned short));\r
856                                         CFX_ByteString bPath = wPath.UTF8Encode();\r
857                                         CFX_ByteString szFormat = "\n<pdf href=\"%s\" xmlns=\"http://ns.adobe.com/xdp/pdf/\"/>";\r
858                                         content.Format(szFormat,(char*)(FX_LPCSTR)bPath);\r
859                                         fileWrite.WriteBlock((FX_LPCSTR)content,fileWrite.GetSize(), content.GetLength());\r
860                                 }\r
861 \r
862                                 CPDF_Stream* pStream = (CPDF_Stream*)pDirectObj;\r
863                                 CPDF_StreamAcc* pAcc = FX_NEW CPDF_StreamAcc;\r
864                                 pAcc->LoadAllData(pStream);\r
865                                 fileWrite.WriteBlock(pAcc->GetData(), fileWrite.GetSize(), pAcc->GetSize());\r
866                                 delete pAcc;\r
867                         }\r
868                 }\r
869         }\r
870         FX_BOOL bError= fileWrite.Flush();\r
871 }\r
872 void CPDFXFA_Document::ImportData(XFA_HDOC hDoc, FX_WSTR wsFilePath)\r
873 {\r
874         //TODO...\r
875 }\r
876 \r
877 void CPDFXFA_Document::GotoURL(XFA_HDOC hDoc, FX_WSTR bsURL, FX_BOOL bAppend)\r
878 {\r
879         if (hDoc != m_pXFADoc)\r
880                 return;\r
881 \r
882         if (m_iDocType != DOCTYPE_DYNIMIC_XFA)\r
883                 return;\r
884                 \r
885         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
886         if (pEnv == NULL)\r
887                 return;\r
888         \r
889         CFX_WideStringC str(bsURL.GetPtr());\r
890         \r
891         pEnv->FFI_GotoURL(this, str, bAppend);\r
892 \r
893 }\r
894 \r
895 FX_BOOL CPDFXFA_Document::IsValidationsEnabled(XFA_HDOC hDoc)\r
896 {\r
897         if (hDoc != m_pXFADoc || !m_pSDKDoc)\r
898                 return FALSE;\r
899         if (m_pSDKDoc->GetInterForm())\r
900                 return m_pSDKDoc->GetInterForm()->IsXfaValidationsEnabled();\r
901 \r
902         return TRUE;\r
903 }\r
904 void CPDFXFA_Document::SetValidationsEnabled(XFA_HDOC hDoc, FX_BOOL bEnabled)\r
905 {\r
906         if (hDoc != m_pXFADoc || !m_pSDKDoc)\r
907                 return;\r
908         if (m_pSDKDoc->GetInterForm())\r
909                 m_pSDKDoc->GetInterForm()->XfaSetValidationsEnabled(bEnabled);\r
910 }\r
911 void  CPDFXFA_Document::SetFocusWidget(XFA_HDOC hDoc, XFA_HWIDGET hWidget)\r
912 {\r
913         if (hDoc != m_pXFADoc)\r
914                 return;\r
915 \r
916         if (NULL == hWidget) {\r
917                 m_pSDKDoc->SetFocusAnnot(NULL);\r
918                 return;\r
919         }\r
920 \r
921         int pageViewCount = m_pSDKDoc->GetPageViewCount();\r
922         for (int i = 0; i < pageViewCount; i++)\r
923         {\r
924                 CPDFSDK_PageView* pPageView = m_pSDKDoc->GetPageView(i);\r
925                 if (pPageView == NULL)\r
926                         continue;\r
927                 CPDFSDK_Annot* pAnnot = pPageView->GetAnnotByXFAWidget(hWidget);\r
928                 if (pAnnot) {\r
929                         m_pSDKDoc->SetFocusAnnot(pAnnot);\r
930                         break;\r
931                 }\r
932         }\r
933 }\r
934 void CPDFXFA_Document::Print(XFA_HDOC hDoc, FX_INT32 nStartPage, FX_INT32 nEndPage, FX_DWORD dwOptions)\r
935 {\r
936         if (hDoc != m_pXFADoc)\r
937                 return;\r
938                 \r
939         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
940         if (pEnv == NULL)\r
941                 return;\r
942 \r
943         if (!pEnv->GetFormFillInfo() || pEnv->GetFormFillInfo()->m_pJsPlatform == NULL)\r
944                 return;\r
945         if (pEnv->GetFormFillInfo()->m_pJsPlatform->Doc_print == NULL)\r
946                 return;\r
947         pEnv->GetFormFillInfo()->m_pJsPlatform->Doc_print(pEnv->GetFormFillInfo()->m_pJsPlatform,dwOptions&XFA_PRINTOPT_ShowDialog,\r
948                 nStartPage,nEndPage,dwOptions&XFA_PRINTOPT_CanCancel,dwOptions&XFA_PRINTOPT_ShrinkPage,dwOptions&XFA_PRINTOPT_AsImage,\r
949                 dwOptions&XFA_PRINTOPT_ReverseOrder,dwOptions&XFA_PRINTOPT_PrintAnnot);\r
950 }\r
951 \r
952 void CPDFXFA_Document::GetURL(XFA_HDOC hDoc, CFX_WideString &wsDocURL)\r
953 {\r
954         if (hDoc != m_pXFADoc)\r
955                 return;\r
956 \r
957         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
958         if (pEnv == NULL)\r
959                 return;\r
960 \r
961         pEnv->FFI_GetURL(this, wsDocURL);\r
962 }\r
963 \r
964 FX_ARGB CPDFXFA_Document::GetHighlightColor(XFA_HDOC hDoc)\r
965 {\r
966         if (hDoc != m_pXFADoc)\r
967                 return 0;\r
968         if (m_pSDKDoc)\r
969         {\r
970                 if(CPDFSDK_InterForm* pInterForm = m_pSDKDoc->GetInterForm())\r
971                 {\r
972                         FX_COLORREF color = pInterForm->GetHighlightColor(FPDF_FORMFIELD_XFA);\r
973                         FX_BYTE alpha = pInterForm->GetHighlightAlpha();\r
974                         FX_ARGB argb = ArgbEncode((int)alpha, color);\r
975                         return argb;\r
976                 }       \r
977         }\r
978         return 0;\r
979 }\r
980 \r
981 void CPDFXFA_Document::AddDoRecord(XFA_HWIDGET hWidget)\r
982 {\r
983         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
984         if (pEnv == NULL)\r
985                 return;\r
986         return;\r
987         //pEnv->FFI_AddDoRecord(this, hWidget);\r
988 }\r
989 \r
990 FX_BOOL CPDFXFA_Document::_NotifySubmit(FX_BOOL bPrevOrPost)\r
991 {\r
992         if (bPrevOrPost)\r
993                 return _OnBeforeNotifySumbit();\r
994         else\r
995                 _OnAfterNotifySumbit();\r
996         return TRUE;\r
997 }\r
998 \r
999 FX_BOOL CPDFXFA_Document::_OnBeforeNotifySumbit()\r
1000 {\r
1001         if (m_iDocType != DOCTYPE_DYNIMIC_XFA && m_iDocType != DOCTYPE_STATIC_XFA)\r
1002                 return TRUE;\r
1003         if (m_pXFADocView == NULL)\r
1004                 return TRUE;\r
1005         IXFA_WidgetHandler* pWidgetHandler = m_pXFADocView->GetWidgetHandler();\r
1006         if (pWidgetHandler == NULL)\r
1007                 return TRUE;\r
1008         IXFA_WidgetAccIterator* pWidgetAccIterator = m_pXFADocView->CreateWidgetAccIterator();\r
1009         if (pWidgetAccIterator)\r
1010         {\r
1011                 CXFA_EventParam Param;\r
1012                 Param.m_eType = XFA_EVENT_PreSubmit;\r
1013                 CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext();\r
1014                 while (pWidgetAcc)\r
1015                 {\r
1016                         pWidgetHandler->ProcessEvent(pWidgetAcc, &Param);\r
1017                         pWidgetAcc = pWidgetAccIterator->MoveToNext();\r
1018                 }\r
1019                 pWidgetAccIterator->Release();\r
1020         }\r
1021         pWidgetAccIterator = m_pXFADocView->CreateWidgetAccIterator();\r
1022         if (pWidgetAccIterator)\r
1023         {\r
1024                 CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext();\r
1025                 pWidgetAcc = pWidgetAccIterator->MoveToNext();\r
1026                 while (pWidgetAcc)\r
1027                 {\r
1028                         int fRet = pWidgetAcc->ProcessValidate(-1);\r
1029                         if (fRet == XFA_EVENTERROR_Error)\r
1030                         {\r
1031                                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
1032                                 if (pEnv == NULL)\r
1033                                         return FALSE;\r
1034                                 CFX_WideString ws;\r
1035                                 ws.FromLocal(IDS_XFA_Validate_Input);\r
1036                                 CFX_ByteString bs = ws.UTF16LE_Encode();\r
1037                                 int len = bs.GetLength()/sizeof(unsigned short);\r
1038                                 pEnv->FFI_Alert((FPDF_WIDESTRING)bs.GetBuffer(len*sizeof(unsigned short)), (FPDF_WIDESTRING)L"", 0, 1);\r
1039                                 bs.ReleaseBuffer(len*sizeof(unsigned short));\r
1040                                 pWidgetAccIterator->Release();\r
1041                                 return FALSE;\r
1042                         }\r
1043                         pWidgetAcc = pWidgetAccIterator->MoveToNext();\r
1044                 }\r
1045                 pWidgetAccIterator->Release();\r
1046                 m_pXFADocView->UpdateDocView();\r
1047         }\r
1048         \r
1049         return TRUE;\r
1050 \r
1051 }\r
1052 void CPDFXFA_Document::_OnAfterNotifySumbit()\r
1053 {\r
1054         if (m_iDocType != DOCTYPE_DYNIMIC_XFA && m_iDocType != DOCTYPE_STATIC_XFA)\r
1055                 return;\r
1056         if (m_pXFADocView == NULL)\r
1057                 return;\r
1058         IXFA_WidgetHandler* pWidgetHandler = m_pXFADocView->GetWidgetHandler();\r
1059         if (pWidgetHandler == NULL)\r
1060                 return;\r
1061         IXFA_WidgetAccIterator* pWidgetAccIterator = m_pXFADocView->CreateWidgetAccIterator();\r
1062         if (pWidgetAccIterator == NULL)\r
1063                 return;\r
1064         CXFA_EventParam Param; \r
1065         Param.m_eType =  XFA_EVENT_PostSubmit;\r
1066 \r
1067         CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext();\r
1068         while (pWidgetAcc)\r
1069         {\r
1070                 pWidgetHandler->ProcessEvent(pWidgetAcc, &Param);\r
1071                 pWidgetAcc = pWidgetAccIterator->MoveToNext();\r
1072         }\r
1073         pWidgetAccIterator->Release();\r
1074         m_pXFADocView->UpdateDocView();\r
1075 }\r
1076 \r
1077 FX_BOOL CPDFXFA_Document::SubmitData(XFA_HDOC hDoc, CXFA_Submit submit)\r
1078 {\r
1079         if (!_NotifySubmit(TRUE))\r
1080                 return FALSE;\r
1081         if (NULL == m_pXFADocView)\r
1082                 return FALSE;\r
1083         m_pXFADocView->UpdateDocView();\r
1084 \r
1085         FX_BOOL ret = _SubmitData(hDoc, submit);\r
1086         _NotifySubmit(FALSE);\r
1087         return ret;\r
1088 }\r
1089 \r
1090 IFX_FileRead* CPDFXFA_Document::OpenLinkedFile(XFA_HDOC hDoc, const CFX_WideString& wsLink)\r
1091 {\r
1092         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
1093         if (pEnv == NULL)\r
1094                 return FALSE;\r
1095         CFX_ByteString bs = wsLink.UTF16LE_Encode();\r
1096         int len = bs.GetLength()/sizeof(unsigned short);\r
1097         FPDF_FILEHANDLER* pFileHandler = pEnv->FFI_OpenFile(0, (FPDF_WIDESTRING)bs.GetBuffer(len*sizeof(unsigned short)), "rb");\r
1098         bs.ReleaseBuffer(len*sizeof(unsigned short));\r
1099 \r
1100         if (pFileHandler == NULL)\r
1101                 return NULL;\r
1102         CFPDF_FileStream* pFileRead = FX_NEW CFPDF_FileStream(pFileHandler);\r
1103         return pFileRead;\r
1104 }\r
1105 FX_BOOL CPDFXFA_Document::_ExportSubmitFile(FPDF_FILEHANDLER* pFileHandler, int fileType, FPDF_DWORD encodeType, FPDF_DWORD flag)\r
1106 {\r
1107         if (NULL == m_pXFADocView)\r
1108                 return FALSE;\r
1109         IXFA_DocHandler* pDocHandler = m_pApp->GetXFAApp()->GetDocHandler();\r
1110         CFX_ByteString content;\r
1111 \r
1112         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
1113         if (pEnv == NULL)\r
1114                 return FALSE;\r
1115         \r
1116         CFPDF_FileStream fileStream(pFileHandler);\r
1117 \r
1118         if (fileType == FXFA_SAVEAS_XML)\r
1119         {\r
1120                 CFX_WideString ws;\r
1121                 ws.FromLocal("data");\r
1122                 CFX_ByteString content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";\r
1123                 fileStream.WriteBlock((FX_LPCSTR)content,0,content.GetLength());\r
1124                 pDocHandler->SavePackage(m_pXFADoc, ws, &fileStream);\r
1125         }\r
1126         else if (fileType == FXFA_SAVEAS_XDP)\r
1127         {       \r
1128                 if (flag == 0)\r
1129                         flag = FXFA_CONFIG|FXFA_TEMPLATE|FXFA_LOCALESET|FXFA_DATASETS|FXFA_XMPMETA|FXFA_XFDF|FXFA_FORM;\r
1130                 if (m_pPDFDoc == NULL) \r
1131                 {\r
1132                         fileStream.Flush();\r
1133                         return FALSE;\r
1134                 }       \r
1135                 CPDF_Dictionary* pRoot = m_pPDFDoc->GetRoot();\r
1136                 if (pRoot == NULL) \r
1137                 {\r
1138                         fileStream.Flush();\r
1139                         return FALSE;\r
1140                 }\r
1141                 CPDF_Dictionary* pAcroForm = pRoot->GetDict("AcroForm");\r
1142                 if (NULL == pAcroForm)\r
1143                 {\r
1144                         fileStream.Flush();\r
1145                         return FALSE;\r
1146                 }\r
1147                 CPDF_Object* pXFA = pAcroForm->GetElement("XFA");\r
1148                 if (pXFA == NULL) \r
1149                 {\r
1150                         fileStream.Flush();\r
1151                         return FALSE;\r
1152                 }\r
1153                 if (pXFA->GetType() != PDFOBJ_ARRAY)\r
1154                 {\r
1155                         fileStream.Flush();\r
1156                         return FALSE;\r
1157                 }\r
1158                 CPDF_Array* pArray = pXFA->GetArray();\r
1159                 if (NULL == pArray)\r
1160                 {\r
1161                         fileStream.Flush();\r
1162                         return FALSE;\r
1163                 }\r
1164                 int size = pArray->GetCount();\r
1165                 int iFormIndex = -1;\r
1166                 int iDataSetsIndex = -1;\r
1167                 for (int i=1; i<size;i+=2)\r
1168                 {\r
1169                         CPDF_Object* pPDFObj = pArray->GetElement(i);\r
1170                         CPDF_Object* pPrePDFObj = pArray->GetElement(i-1);\r
1171                         if(pPrePDFObj->GetType() != PDFOBJ_STRING)\r
1172                                 continue;\r
1173                         if (pPDFObj->GetType() != PDFOBJ_REFERENCE)\r
1174                                 continue;\r
1175                         CPDF_Object* pDirectObj = pPDFObj->GetDirect();\r
1176                         if (pDirectObj->GetType() != PDFOBJ_STREAM)\r
1177                                 continue;\r
1178                         if (pPrePDFObj->GetString()=="config" && !(flag & FXFA_CONFIG))\r
1179                                 continue;\r
1180                         if (pPrePDFObj->GetString()=="template" && !(flag & FXFA_TEMPLATE))\r
1181                                 continue;\r
1182                         if (pPrePDFObj->GetString()=="localeSet" && !(flag & FXFA_LOCALESET))\r
1183                                 continue;\r
1184                         if (pPrePDFObj->GetString()=="datasets" && !(flag & FXFA_DATASETS))\r
1185                                 continue;\r
1186                         if (pPrePDFObj->GetString()=="xmpmeta" && !(flag & FXFA_XMPMETA))\r
1187                                 continue;\r
1188                         if (pPrePDFObj->GetString()=="xfdf" && !(flag & FXFA_XFDF))\r
1189                                 continue;\r
1190                         if (pPrePDFObj->GetString()=="form" && !(flag & FXFA_FORM))\r
1191                                 continue;\r
1192                         if (pPrePDFObj->GetString()=="form")\r
1193                         {\r
1194                                 CFX_WideString ws;\r
1195                                 ws.FromLocal("form");\r
1196                                 pDocHandler->SavePackage(m_pXFADoc, ws, &fileStream);\r
1197                         }\r
1198                         else if (pPrePDFObj->GetString()=="datasets")\r
1199                         {\r
1200                                 CFX_WideString ws;\r
1201                                 ws.FromLocal("datasets");\r
1202                                 pDocHandler->SavePackage(m_pXFADoc, ws, &fileStream);\r
1203                         }\r
1204                         else \r
1205                         {       \r
1206                                 //PDF,creator.\r
1207                                 //TODO:\r
1208                         }\r
1209                 }\r
1210         }\r
1211         return TRUE;\r
1212 }\r
1213 \r
1214 void CPDFXFA_Document::_ClearChangeMark()\r
1215 {\r
1216         if (m_pSDKDoc)\r
1217                 m_pSDKDoc->ClearChangeMark();\r
1218 }\r
1219 \r
1220 void CPDFXFA_Document::_ToXFAContentFlags(CFX_WideString csSrcContent, FPDF_DWORD& flag)\r
1221 {\r
1222         if (csSrcContent.Find(L" config ", 0) != -1)\r
1223                 flag |= FXFA_CONFIG;\r
1224         if (csSrcContent.Find(L" template ", 0) != -1)\r
1225                 flag  |= FXFA_TEMPLATE;\r
1226         if (csSrcContent.Find(L" localeSet ", 0) != -1)\r
1227                 flag  |= FXFA_LOCALESET;\r
1228         if (csSrcContent.Find(L" datasets ", 0) != -1)\r
1229                 flag  |= FXFA_DATASETS;\r
1230         if (csSrcContent.Find(L" xmpmeta ", 0) != -1)\r
1231                 flag  |= FXFA_XMPMETA;\r
1232         if (csSrcContent.Find(L" xfdf ", 0) != -1)\r
1233                 flag  |= FXFA_XFDF;\r
1234         if (csSrcContent.Find(L" form ", 0) != -1)\r
1235                 flag  |= FXFA_FORM;\r
1236         if (flag == 0)\r
1237                 flag = FXFA_CONFIG|FXFA_TEMPLATE|FXFA_LOCALESET|FXFA_DATASETS|FXFA_XMPMETA|FXFA_XFDF|FXFA_FORM;\r
1238 }\r
1239 FX_BOOL CPDFXFA_Document::_MailToInfo(CFX_WideString& csURL, CFX_WideString& csToAddress, CFX_WideString& csCCAddress, CFX_WideString& csBCCAddress, CFX_WideString& csSubject, CFX_WideString& csMsg)\r
1240 {\r
1241         CFX_WideString srcURL = csURL;\r
1242         srcURL.TrimLeft();\r
1243         if (0 != srcURL.Left(7).CompareNoCase(L"mailto:"))\r
1244                 return FALSE;\r
1245         int pos = srcURL.Find(L'?',0);\r
1246         CFX_WideString tmp;\r
1247         if (pos == -1) {\r
1248                 pos = srcURL.Find(L'@',0);\r
1249                 if (pos == -1)\r
1250                         return FALSE;\r
1251                 else\r
1252                 {\r
1253                         tmp = srcURL.Right(csURL.GetLength()-7);\r
1254                         tmp.TrimLeft();\r
1255                         tmp.TrimRight();\r
1256                 }\r
1257         } else {\r
1258                 tmp = srcURL.Left(pos);\r
1259                 tmp = tmp.Right(tmp.GetLength()-7);\r
1260                 tmp.TrimLeft();\r
1261                 tmp.TrimRight();\r
1262         }\r
1263         \r
1264         csToAddress = tmp;\r
1265         \r
1266         srcURL = srcURL.Right(srcURL.GetLength()-(pos+1));\r
1267         while ( !srcURL.IsEmpty() )\r
1268         {\r
1269                 srcURL.TrimLeft();\r
1270                 srcURL.TrimRight();\r
1271                 pos = srcURL.Find(L'&',0);\r
1272                 if (pos == -1)\r
1273                         tmp = srcURL;\r
1274                 else\r
1275                         tmp = srcURL.Left(pos);\r
1276                 \r
1277                 tmp.TrimLeft();\r
1278                 tmp.TrimRight();\r
1279                 if (tmp.GetLength() >= 3 && 0 == tmp.Left(3).CompareNoCase(L"cc=") )\r
1280                 {\r
1281                         tmp = tmp.Right(tmp.GetLength()-3);\r
1282                         if (!csCCAddress.IsEmpty())\r
1283                                 csCCAddress += L';';\r
1284                         csCCAddress += tmp;\r
1285                         \r
1286                 } \r
1287                 else if (tmp.GetLength() >= 4 && 0 == tmp.Left(4).CompareNoCase(L"bcc="))\r
1288                 {\r
1289                         tmp = tmp.Right(tmp.GetLength() - 4);\r
1290                         if (!csBCCAddress.IsEmpty())\r
1291                                 csBCCAddress += L';';\r
1292                         csBCCAddress += tmp;\r
1293                 }\r
1294                 else if (tmp.GetLength() >= 8 && 0 ==tmp.Left(8).CompareNoCase(L"subject=") )\r
1295                 {\r
1296                         tmp=tmp.Right(tmp.GetLength()-8);\r
1297                         csSubject += tmp;\r
1298                 }\r
1299                 else if(tmp.GetLength() >= 5 && 0 ==tmp.Left(5).CompareNoCase(L"body=") )\r
1300                 {\r
1301                         tmp=tmp.Right(tmp.GetLength() - 5);\r
1302                         csMsg += tmp;\r
1303                 }\r
1304                 if (pos == -1)\r
1305                         srcURL = L"";\r
1306                 else\r
1307                         srcURL = srcURL.Right(csURL.GetLength()-(pos+1));\r
1308         }\r
1309         csToAddress.Replace((FX_LPCWSTR)L",", (FX_LPCWSTR)L";");\r
1310         csCCAddress.Replace((FX_LPCWSTR)L",", (FX_LPCWSTR)L";");\r
1311         csBCCAddress.Replace((FX_LPCWSTR)L",", (FX_LPCWSTR)L";");\r
1312         return TRUE;\r
1313 }\r
1314 \r
1315 FX_BOOL CPDFXFA_Document::_SubmitData(XFA_HDOC hDoc, CXFA_Submit submit)\r
1316 {\r
1317         CFX_WideStringC csURLC;\r
1318         submit.GetSubmitTarget(csURLC);\r
1319         CFX_WideString csURL = csURLC;\r
1320         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();\r
1321         if (pEnv == NULL)\r
1322                 return FALSE;\r
1323         if (csURL.IsEmpty()){    \r
1324                 CFX_WideString ws;\r
1325                 ws.FromLocal("Submit cancelled.");\r
1326                 CFX_ByteString bs = ws.UTF16LE_Encode();\r
1327                 int len = bs.GetLength()/sizeof(unsigned short);\r
1328                 pEnv->FFI_Alert((FPDF_WIDESTRING)bs.GetBuffer(len*sizeof(unsigned short)), (FPDF_WIDESTRING)L"", 0, 4);\r
1329                 bs.ReleaseBuffer(len*sizeof(unsigned short));\r
1330                 return FALSE;\r
1331         }\r
1332 \r
1333         FPDF_BOOL bRet = TRUE;\r
1334         FPDF_FILEHANDLER* pFileHandler = NULL;\r
1335         int fileFlag = -1;\r
1336 \r
1337         if (submit.GetSubmitFormat() == XFA_ATTRIBUTEENUM_Xdp)\r
1338         {\r
1339                 CFX_WideStringC csContentC;\r
1340                 submit.GetSubmitXDPContent(csContentC);\r
1341                 CFX_WideString csContent;\r
1342                 csContent = csContentC.GetPtr();\r
1343                 csContent.TrimLeft();\r
1344                 csContent.TrimRight();\r
1345                 CFX_WideString space;\r
1346                 space.FromLocal(" ");\r
1347                 csContent = space + csContent + space;\r
1348                 FPDF_DWORD flag = 0;\r
1349                 if (submit.IsSubmitEmbedPDF())\r
1350                         flag |= FXFA_PDF;\r
1351                 _ToXFAContentFlags(csContent, flag);\r
1352                 pFileHandler = pEnv->FFI_OpenFile(FXFA_SAVEAS_XDP, NULL, "wb");\r
1353                 fileFlag = FXFA_SAVEAS_XDP;\r
1354                 _ExportSubmitFile(pFileHandler, FXFA_SAVEAS_XDP, 0, flag);\r
1355         }\r
1356         else if (submit.GetSubmitFormat() == XFA_ATTRIBUTEENUM_Xml )\r
1357         {\r
1358                 pFileHandler = pEnv->FFI_OpenFile(FXFA_SAVEAS_XML, NULL, "wb");\r
1359                 fileFlag = FXFA_SAVEAS_XML;\r
1360                 _ExportSubmitFile(pFileHandler, FXFA_SAVEAS_XML, 0);\r
1361         }\r
1362         else if (submit.GetSubmitFormat() == XFA_ATTRIBUTEENUM_Pdf )\r
1363         {\r
1364                         //csfilename = csDocName;\r
1365         }\r
1366         else if (submit.GetSubmitFormat() == XFA_ATTRIBUTEENUM_Formdata )\r
1367         {\r
1368                 return FALSE;\r
1369         }\r
1370         else if (submit.GetSubmitFormat() == XFA_ATTRIBUTEENUM_Urlencoded )\r
1371         {\r
1372                 pFileHandler = pEnv->FFI_OpenFile(FXFA_SAVEAS_XML, NULL, "wb");\r
1373                 fileFlag = FXFA_SAVEAS_XML;\r
1374                 _ExportSubmitFile(pFileHandler, FXFA_SAVEAS_XML, 0);\r
1375         }\r
1376         else if (submit.GetSubmitFormat() == XFA_ATTRIBUTEENUM_Xfd )\r
1377         {\r
1378                 return FALSE;\r
1379         }\r
1380         else\r
1381         {\r
1382                 return FALSE;\r
1383         }\r
1384         if (pFileHandler == NULL)\r
1385                 return FALSE;\r
1386         if (0 == csURL.Left(7).CompareNoCase(L"mailto:"))\r
1387         {\r
1388                 CFX_WideString csToAddress; \r
1389                 CFX_WideString csCCAddress;\r
1390                 CFX_WideString csBCCAddress;\r
1391                 CFX_WideString csSubject;\r
1392                 CFX_WideString csMsg;\r
1393 \r
1394                 bRet = _MailToInfo(csURL, csToAddress, csCCAddress, csBCCAddress, csSubject, csMsg);\r
1395                 if (FALSE == bRet)\r
1396                         return FALSE;\r
1397 \r
1398                 CFX_ByteString bsTo = CFX_WideString(csToAddress).UTF16LE_Encode();\r
1399                 CFX_ByteString bsCC = CFX_WideString(csCCAddress).UTF16LE_Encode();\r
1400                 CFX_ByteString bsBcc = CFX_WideString(csBCCAddress).UTF16LE_Encode();\r
1401                 CFX_ByteString bsSubject = CFX_WideString(csSubject).UTF16LE_Encode();\r
1402                 CFX_ByteString bsMsg = CFX_WideString(csMsg).UTF16LE_Encode();\r
1403 \r
1404                 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());\r
1405                 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());\r
1406                 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());\r
1407                 FPDF_WIDESTRING pSubject = (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());\r
1408                 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());\r
1409 \r
1410                 pEnv->FFI_EmailTo(pFileHandler, pTo, pSubject, pCC, pBcc, pMsg);\r
1411                 bsTo.ReleaseBuffer();\r
1412                 bsCC.ReleaseBuffer();\r
1413                 bsBcc.ReleaseBuffer();\r
1414                 bsSubject.ReleaseBuffer();\r
1415                 bsMsg.ReleaseBuffer();\r
1416         }\r
1417         else\r
1418         {\r
1419                 //http¡¢ftp\r
1420                 CFX_WideString ws;\r
1421                 CFX_ByteString bs = csURL.UTF16LE_Encode();\r
1422                 int len = bs.GetLength()/sizeof(unsigned short);\r
1423                 pEnv->FFI_UploadTo(pFileHandler, fileFlag, (FPDF_WIDESTRING)bs.GetBuffer(len*sizeof(unsigned short)));\r
1424                 bs.ReleaseBuffer(len*sizeof(unsigned short));\r
1425         }\r
1426 \r
1427         return bRet;\r
1428 }\r
1429 \r
1430 FX_BOOL CPDFXFA_Document::SetGlobalProperty(XFA_HDOC hDoc, FX_BSTR szPropName, FXJSE_HVALUE hValue)\r
1431 {\r
1432         if (hDoc != m_pXFADoc)\r
1433                 return FALSE;\r
1434 \r
1435         if (m_pSDKDoc && m_pSDKDoc->GetEnv()->GetJSRuntime()) \r
1436                 return m_pSDKDoc->GetEnv()->GetJSRuntime()->SetHValueByName(szPropName, hValue);\r
1437         return FALSE;\r
1438 }\r
1439 FX_BOOL CPDFXFA_Document::GetPDFScriptObject(XFA_HDOC hDoc, FX_BSTR utf8Name, FXJSE_HVALUE hValue)\r
1440 {\r
1441         if (hDoc != m_pXFADoc)\r
1442                 return FALSE;\r
1443 \r
1444         if (!m_pSDKDoc || !m_pSDKDoc->GetEnv()->GetJSRuntime())\r
1445                 return FALSE;\r
1446 \r
1447         if (!m_pJSContext)\r
1448         {\r
1449                 m_pSDKDoc->GetEnv()->GetJSRuntime()->SetReaderDocument(m_pSDKDoc);\r
1450                 m_pJSContext =m_pSDKDoc->GetEnv()->GetJSRuntime()->NewContext();\r
1451         }\r
1452 \r
1453         return _GetHValueByName(utf8Name, hValue, m_pSDKDoc->GetEnv()->GetJSRuntime());\r
1454 \r
1455 }\r
1456 FX_BOOL CPDFXFA_Document::GetGlobalProperty(XFA_HDOC hDoc, FX_BSTR szPropName, FXJSE_HVALUE hValue)\r
1457 {\r
1458         if (hDoc != m_pXFADoc)\r
1459                 return FALSE;\r
1460         if (!m_pSDKDoc || !m_pSDKDoc->GetEnv()->GetJSRuntime())\r
1461                 return FALSE;\r
1462 \r
1463         if (!m_pJSContext)\r
1464         {\r
1465                 m_pSDKDoc->GetEnv()->GetJSRuntime()->SetReaderDocument(m_pSDKDoc);\r
1466                 m_pJSContext = m_pSDKDoc->GetEnv()->GetJSRuntime()->NewContext();\r
1467         }\r
1468 \r
1469         return _GetHValueByName(szPropName, hValue, m_pSDKDoc->GetEnv()->GetJSRuntime());\r
1470 \r
1471 }\r
1472 FX_BOOL CPDFXFA_Document::_GetHValueByName(FX_BSTR utf8Name, FXJSE_HVALUE hValue, IFXJS_Runtime* runTime)\r
1473 {\r
1474         return runTime->GetHValueByName(utf8Name, hValue);\r
1475 }\r
1476 \r