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