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