1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../third_party/base/nonstd_unique_ptr.h"
8 #include "../include/fsdk_define.h"
9 #include "../include/fsdk_mgr.h"
10 #include "../include/fsdk_baseannot.h"
11 #include "../include/fsdk_baseform.h"
12 #include "../include/formfiller/FFL_FormFiller.h"
13 #include "../include/fsdk_actionhandler.h"
15 #include "../include/javascript/IJavaScript.h"
17 //------------------------------------------------------------------------------------
19 //------------------------------------------------------------------------------------
21 #define IsFloatZero(f) ((f) < 0.01 && (f) > -0.01)
22 #define IsFloatBigger(fa, fb) ((fa) > (fb) && !IsFloatZero((fa) - (fb)))
23 #define IsFloatSmaller(fa, fb) ((fa) < (fb) && !IsFloatZero((fa) - (fb)))
24 #define IsFloatEqual(fa, fb) IsFloatZero((fa) - (fb))
26 CPDFSDK_Widget::CPDFSDK_Widget(CPDF_Annot* pAnnot,
27 CPDFSDK_PageView* pPageView,
28 CPDFSDK_InterForm* pInterForm)
29 : CPDFSDK_Annot(pAnnot, pPageView),
30 m_pInterForm(pInterForm),
33 ASSERT(m_pInterForm != NULL);
36 CPDFSDK_Widget::~CPDFSDK_Widget() {}
38 FX_BOOL CPDFSDK_Widget::IsWidgetAppearanceValid(
39 CPDF_Annot::AppearanceMode mode) {
40 CPDF_Dictionary* pAP = m_pAnnot->GetAnnotDict()->GetDict("AP");
44 // Choose the right sub-ap
45 const FX_CHAR* ap_entry = "N";
46 if (mode == CPDF_Annot::Down)
48 else if (mode == CPDF_Annot::Rollover)
50 if (!pAP->KeyExist(ap_entry))
53 // Get the AP stream or subdirectory
54 CPDF_Object* psub = pAP->GetElementValue(ap_entry);
58 int nFieldType = GetFieldType();
60 case FIELDTYPE_PUSHBUTTON:
61 case FIELDTYPE_COMBOBOX:
62 case FIELDTYPE_LISTBOX:
63 case FIELDTYPE_TEXTFIELD:
64 case FIELDTYPE_SIGNATURE:
65 return psub->GetType() == PDFOBJ_STREAM;
66 case FIELDTYPE_CHECKBOX:
67 case FIELDTYPE_RADIOBUTTON:
68 if (CPDF_Dictionary* pSubDict = psub->AsDictionary()) {
69 return pSubDict->GetStream(GetAppState()) != NULL;
76 int CPDFSDK_Widget::GetFieldType() const {
77 CPDF_FormField* pField = GetFormField();
78 ASSERT(pField != NULL);
80 return pField->GetFieldType();
83 int CPDFSDK_Widget::GetFieldFlags() const {
84 CPDF_InterForm* pPDFInterForm = m_pInterForm->GetInterForm();
85 ASSERT(pPDFInterForm != NULL);
87 CPDF_FormControl* pFormControl =
88 pPDFInterForm->GetControlByDict(m_pAnnot->GetAnnotDict());
89 CPDF_FormField* pFormField = pFormControl->GetField();
90 return pFormField->GetFieldFlags();
93 CFX_ByteString CPDFSDK_Widget::GetSubType() const {
94 int nType = GetFieldType();
96 if (nType == FIELDTYPE_SIGNATURE)
97 return BFFT_SIGNATURE;
98 return CPDFSDK_Annot::GetSubType();
101 CPDF_FormField* CPDFSDK_Widget::GetFormField() const {
102 ASSERT(m_pInterForm != NULL);
104 CPDF_FormControl* pCtrl = GetFormControl();
105 ASSERT(pCtrl != NULL);
107 return pCtrl->GetField();
110 CPDF_FormControl* CPDFSDK_Widget::GetFormControl() const {
111 ASSERT(m_pInterForm != NULL);
113 CPDF_InterForm* pPDFInterForm = m_pInterForm->GetInterForm();
114 ASSERT(pPDFInterForm != NULL);
116 return pPDFInterForm->GetControlByDict(GetAnnotDict());
119 CPDF_FormControl* CPDFSDK_Widget::GetFormControl(CPDF_InterForm* pInterForm,
120 CPDF_Dictionary* pAnnotDict) {
121 ASSERT(pInterForm != NULL);
122 ASSERT(pAnnotDict != NULL);
124 CPDF_FormControl* pControl = pInterForm->GetControlByDict(pAnnotDict);
129 int CPDFSDK_Widget::GetRotate() const {
130 CPDF_FormControl* pCtrl = GetFormControl();
131 ASSERT(pCtrl != NULL);
133 return pCtrl->GetRotation() % 360;
136 FX_BOOL CPDFSDK_Widget::GetFillColor(FX_COLORREF& color) const {
137 CPDF_FormControl* pFormCtrl = GetFormControl();
138 ASSERT(pFormCtrl != NULL);
141 color = FX_ARGBTOCOLORREF(pFormCtrl->GetBackgroundColor(iColorType));
143 return iColorType != COLORTYPE_TRANSPARENT;
146 FX_BOOL CPDFSDK_Widget::GetBorderColor(FX_COLORREF& color) const {
147 CPDF_FormControl* pFormCtrl = GetFormControl();
148 ASSERT(pFormCtrl != NULL);
151 color = FX_ARGBTOCOLORREF(pFormCtrl->GetBorderColor(iColorType));
153 return iColorType != COLORTYPE_TRANSPARENT;
156 FX_BOOL CPDFSDK_Widget::GetTextColor(FX_COLORREF& color) const {
157 CPDF_FormControl* pFormCtrl = GetFormControl();
158 ASSERT(pFormCtrl != NULL);
160 CPDF_DefaultAppearance da = pFormCtrl->GetDefaultAppearance();
163 int iColorType = COLORTYPE_TRANSPARENT;
164 da.GetColor(argb, iColorType);
165 color = FX_ARGBTOCOLORREF(argb);
167 return iColorType != COLORTYPE_TRANSPARENT;
173 FX_FLOAT CPDFSDK_Widget::GetFontSize() const {
174 CPDF_FormControl* pFormCtrl = GetFormControl();
175 ASSERT(pFormCtrl != NULL);
177 CPDF_DefaultAppearance pDa = pFormCtrl->GetDefaultAppearance();
178 CFX_ByteString csFont = "";
179 FX_FLOAT fFontSize = 0.0f;
180 pDa.GetFont(csFont, fFontSize);
185 int CPDFSDK_Widget::GetSelectedIndex(int nIndex) const {
186 CPDF_FormField* pFormField = GetFormField();
187 return pFormField->GetSelectedIndex(nIndex);
190 CFX_WideString CPDFSDK_Widget::GetValue() const {
191 CPDF_FormField* pFormField = GetFormField();
192 return pFormField->GetValue();
195 CFX_WideString CPDFSDK_Widget::GetDefaultValue() const {
196 CPDF_FormField* pFormField = GetFormField();
197 ASSERT(pFormField != NULL);
199 return pFormField->GetDefaultValue();
202 CFX_WideString CPDFSDK_Widget::GetOptionLabel(int nIndex) const {
203 CPDF_FormField* pFormField = GetFormField();
204 ASSERT(pFormField != NULL);
206 return pFormField->GetOptionLabel(nIndex);
209 int CPDFSDK_Widget::CountOptions() const {
210 CPDF_FormField* pFormField = GetFormField();
211 ASSERT(pFormField != NULL);
213 return pFormField->CountOptions();
216 FX_BOOL CPDFSDK_Widget::IsOptionSelected(int nIndex) const {
217 CPDF_FormField* pFormField = GetFormField();
218 return pFormField->IsItemSelected(nIndex);
221 int CPDFSDK_Widget::GetTopVisibleIndex() const {
222 CPDF_FormField* pFormField = GetFormField();
223 ASSERT(pFormField != NULL);
225 return pFormField->GetTopVisibleIndex();
228 FX_BOOL CPDFSDK_Widget::IsChecked() const {
229 CPDF_FormControl* pFormCtrl = GetFormControl();
230 ASSERT(pFormCtrl != NULL);
232 return pFormCtrl->IsChecked();
235 int CPDFSDK_Widget::GetAlignment() const {
236 CPDF_FormControl* pFormCtrl = GetFormControl();
237 ASSERT(pFormCtrl != NULL);
239 return pFormCtrl->GetControlAlignment();
242 int CPDFSDK_Widget::GetMaxLen() const {
243 CPDF_FormField* pFormField = GetFormField();
244 ASSERT(pFormField != NULL);
246 return pFormField->GetMaxLen();
249 void CPDFSDK_Widget::SetCheck(FX_BOOL bChecked, FX_BOOL bNotify) {
250 CPDF_FormControl* pFormCtrl = GetFormControl();
251 ASSERT(pFormCtrl != NULL);
253 CPDF_FormField* pFormField = pFormCtrl->GetField();
254 ASSERT(pFormField != NULL);
256 pFormField->CheckControl(pFormField->GetControlIndex(pFormCtrl), bChecked,
260 void CPDFSDK_Widget::SetValue(const CFX_WideString& sValue, FX_BOOL bNotify) {
261 CPDF_FormField* pFormField = GetFormField();
262 ASSERT(pFormField != NULL);
264 pFormField->SetValue(sValue, bNotify);
267 void CPDFSDK_Widget::SetDefaultValue(const CFX_WideString& sValue) {}
268 void CPDFSDK_Widget::SetOptionSelection(int index,
271 CPDF_FormField* pFormField = GetFormField();
272 ASSERT(pFormField != NULL);
274 pFormField->SetItemSelection(index, bSelected, bNotify);
277 void CPDFSDK_Widget::ClearSelection(FX_BOOL bNotify) {
278 CPDF_FormField* pFormField = GetFormField();
279 ASSERT(pFormField != NULL);
281 pFormField->ClearSelection(bNotify);
284 void CPDFSDK_Widget::SetTopVisibleIndex(int index) {}
286 void CPDFSDK_Widget::SetAppModified() {
287 m_bAppModified = TRUE;
290 void CPDFSDK_Widget::ClearAppModified() {
291 m_bAppModified = FALSE;
294 FX_BOOL CPDFSDK_Widget::IsAppModified() const {
295 return m_bAppModified;
298 void CPDFSDK_Widget::ResetAppearance(const FX_WCHAR* sValue,
299 FX_BOOL bValueChanged) {
303 if (m_nAppAge > 999999)
308 int nFieldType = GetFieldType();
310 switch (nFieldType) {
311 case FIELDTYPE_PUSHBUTTON:
312 ResetAppearance_PushButton();
314 case FIELDTYPE_CHECKBOX:
315 ResetAppearance_CheckBox();
317 case FIELDTYPE_RADIOBUTTON:
318 ResetAppearance_RadioButton();
320 case FIELDTYPE_COMBOBOX:
321 ResetAppearance_ComboBox(sValue);
323 case FIELDTYPE_LISTBOX:
324 ResetAppearance_ListBox();
326 case FIELDTYPE_TEXTFIELD:
327 ResetAppearance_TextField(sValue);
331 ASSERT(m_pAnnot != NULL);
332 m_pAnnot->ClearCachedAP();
335 CFX_WideString CPDFSDK_Widget::OnFormat(FX_BOOL& bFormated) {
336 CPDF_FormField* pFormField = GetFormField();
337 ASSERT(pFormField != NULL);
338 return m_pInterForm->OnFormat(pFormField, bFormated);
341 void CPDFSDK_Widget::ResetFieldAppearance(FX_BOOL bValueChanged) {
342 CPDF_FormField* pFormField = GetFormField();
343 ASSERT(pFormField != NULL);
345 ASSERT(m_pInterForm != NULL);
347 m_pInterForm->ResetFieldAppearance(pFormField, NULL, bValueChanged);
350 void CPDFSDK_Widget::DrawAppearance(CFX_RenderDevice* pDevice,
351 const CPDF_Matrix* pUser2Device,
352 CPDF_Annot::AppearanceMode mode,
353 const CPDF_RenderOptions* pOptions) {
354 int nFieldType = GetFieldType();
356 if ((nFieldType == FIELDTYPE_CHECKBOX ||
357 nFieldType == FIELDTYPE_RADIOBUTTON) &&
358 mode == CPDF_Annot::Normal &&
359 !IsWidgetAppearanceValid(CPDF_Annot::Normal)) {
360 CFX_PathData pathData;
362 CPDF_Rect rcAnnot = GetRect();
364 pathData.AppendRect(rcAnnot.left, rcAnnot.bottom, rcAnnot.right,
367 CFX_GraphStateData gsd;
368 gsd.m_LineWidth = 0.0f;
370 pDevice->DrawPath(&pathData, pUser2Device, &gsd, 0, 0xFFAAAAAA,
373 CPDFSDK_Annot::DrawAppearance(pDevice, pUser2Device, mode, pOptions);
377 void CPDFSDK_Widget::UpdateField() {
378 CPDF_FormField* pFormField = GetFormField();
379 ASSERT(pFormField != NULL);
381 ASSERT(m_pInterForm != NULL);
382 m_pInterForm->UpdateField(pFormField);
385 void CPDFSDK_Widget::DrawShadow(CFX_RenderDevice* pDevice,
386 CPDFSDK_PageView* pPageView) {
387 ASSERT(m_pInterForm != NULL);
389 int nFieldType = GetFieldType();
390 if (m_pInterForm->IsNeedHighLight(nFieldType)) {
391 CPDF_Rect rc = GetRect();
392 FX_COLORREF color = m_pInterForm->GetHighlightColor(nFieldType);
393 uint8_t alpha = m_pInterForm->GetHighlightAlpha();
395 CFX_FloatRect rcDevice;
396 ASSERT(m_pInterForm->GetDocument());
397 CPDFDoc_Environment* pEnv = m_pInterForm->GetDocument()->GetEnv();
400 CFX_AffineMatrix page2device;
401 pPageView->GetCurrentMatrix(page2device);
402 page2device.Transform(((FX_FLOAT)rc.left), ((FX_FLOAT)rc.bottom),
403 rcDevice.left, rcDevice.bottom);
404 page2device.Transform(((FX_FLOAT)rc.right), ((FX_FLOAT)rc.top),
405 rcDevice.right, rcDevice.top);
407 rcDevice.Normalize();
409 FX_ARGB argb = ArgbEncode((int)alpha, color);
410 FX_RECT rcDev((int)rcDevice.left, (int)rcDevice.top, (int)rcDevice.right,
411 (int)rcDevice.bottom);
412 pDevice->FillRect(&rcDev, argb);
416 void CPDFSDK_Widget::ResetAppearance_PushButton() {
417 CPDF_FormControl* pControl = GetFormControl();
418 ASSERT(pControl != NULL);
420 CPDF_Rect rcWindow = GetRotatedRect();
424 switch (pControl->GetTextPosition()) {
429 nLayout = PPBL_ICONTOPLABELBOTTOM;
432 nLayout = PPBL_LABELTOPICONBOTTOM;
435 nLayout = PPBL_ICONLEFTLABELRIGHT;
438 nLayout = PPBL_LABELLEFTICONRIGHT;
440 case TEXTPOS_OVERLAID:
441 nLayout = PPBL_LABELOVERICON;
444 nLayout = PPBL_LABEL;
448 CPWL_Color crBackground, crBorder;
453 pControl->GetOriginalBackgroundColor(iColorType, fc);
455 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
457 pControl->GetOriginalBorderColor(iColorType, fc);
459 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
461 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
462 int32_t nBorderStyle = 0;
463 CPWL_Dash dsBorder(3, 0, 0);
464 CPWL_Color crLeftTop, crRightBottom;
466 switch (GetBorderStyle()) {
468 nBorderStyle = PBS_DASH;
469 dsBorder = CPWL_Dash(3, 3, 0);
472 nBorderStyle = PBS_BEVELED;
474 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
475 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
478 nBorderStyle = PBS_INSET;
480 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
481 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
484 nBorderStyle = PBS_UNDERLINED;
487 nBorderStyle = PBS_SOLID;
491 CPDF_Rect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
493 CPWL_Color crText(COLORTYPE_GRAY, 0);
495 FX_FLOAT fFontSize = 12.0f;
496 CFX_ByteString csNameTag;
498 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
500 da.GetColor(iColorType, fc);
501 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
505 da.GetFont(csNameTag, fFontSize);
507 CFX_WideString csWCaption;
508 CFX_WideString csNormalCaption, csRolloverCaption, csDownCaption;
510 if (pControl->HasMKEntry("CA")) {
511 csNormalCaption = pControl->GetNormalCaption();
513 if (pControl->HasMKEntry("RC")) {
514 csRolloverCaption = pControl->GetRolloverCaption();
516 if (pControl->HasMKEntry("AC")) {
517 csDownCaption = pControl->GetDownCaption();
520 CPDF_Stream* pNormalIcon = NULL;
521 CPDF_Stream* pRolloverIcon = NULL;
522 CPDF_Stream* pDownIcon = NULL;
524 if (pControl->HasMKEntry("I")) {
525 pNormalIcon = pControl->GetNormalIcon();
527 if (pControl->HasMKEntry("RI")) {
528 pRolloverIcon = pControl->GetRolloverIcon();
530 if (pControl->HasMKEntry("IX")) {
531 pDownIcon = pControl->GetDownIcon();
535 if (CPDF_Dictionary* pImageDict = pNormalIcon->GetDict()) {
536 if (pImageDict->GetString("Name").IsEmpty())
537 pImageDict->SetAtString("Name", "ImgA");
542 if (CPDF_Dictionary* pImageDict = pRolloverIcon->GetDict()) {
543 if (pImageDict->GetString("Name").IsEmpty())
544 pImageDict->SetAtString("Name", "ImgB");
549 if (CPDF_Dictionary* pImageDict = pDownIcon->GetDict()) {
550 if (pImageDict->GetString("Name").IsEmpty())
551 pImageDict->SetAtString("Name", "ImgC");
555 CPDF_IconFit iconFit = pControl->GetIconFit();
557 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
558 ASSERT(pDoc != NULL);
559 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
561 CBA_FontMap FontMap(this, pEnv->GetSysHandler());
564 FontMap.SetAPType("N");
566 CFX_ByteString csAP =
567 CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
568 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
569 crLeftTop, crRightBottom, nBorderStyle,
571 CPWL_Utils::GetPushButtonAppStream(
572 iconFit.GetFittingBounds() ? rcWindow : rcClient, &FontMap,
573 pNormalIcon, iconFit, csNormalCaption, crText, fFontSize, nLayout);
575 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP);
577 AddImageToAppearance("N", pNormalIcon);
579 CPDF_FormControl::HighlightingMode eHLM = pControl->GetHighlightingMode();
580 if (eHLM == CPDF_FormControl::Push || eHLM == CPDF_FormControl::Toggle) {
581 if (csRolloverCaption.IsEmpty() && !pRolloverIcon) {
582 csRolloverCaption = csNormalCaption;
583 pRolloverIcon = pNormalIcon;
586 FontMap.SetAPType("R");
588 csAP = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
589 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
590 crLeftTop, crRightBottom,
591 nBorderStyle, dsBorder) +
592 CPWL_Utils::GetPushButtonAppStream(
593 iconFit.GetFittingBounds() ? rcWindow : rcClient, &FontMap,
594 pRolloverIcon, iconFit, csRolloverCaption, crText, fFontSize,
597 WriteAppearance("R", GetRotatedRect(), GetMatrix(), csAP);
599 AddImageToAppearance("R", pRolloverIcon);
601 if (csDownCaption.IsEmpty() && !pDownIcon) {
602 csDownCaption = csNormalCaption;
603 pDownIcon = pNormalIcon;
606 switch (nBorderStyle) {
608 CPWL_Color crTemp = crLeftTop;
609 crLeftTop = crRightBottom;
610 crRightBottom = crTemp;
613 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
614 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
618 FontMap.SetAPType("D");
620 csAP = CPWL_Utils::GetRectFillAppStream(
621 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
622 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
623 crLeftTop, crRightBottom,
624 nBorderStyle, dsBorder) +
625 CPWL_Utils::GetPushButtonAppStream(
626 iconFit.GetFittingBounds() ? rcWindow : rcClient, &FontMap,
627 pDownIcon, iconFit, csDownCaption, crText, fFontSize, nLayout);
629 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP);
631 AddImageToAppearance("D", pDownIcon);
633 RemoveAppearance("D");
634 RemoveAppearance("R");
638 void CPDFSDK_Widget::ResetAppearance_CheckBox() {
639 CPDF_FormControl* pControl = GetFormControl();
640 ASSERT(pControl != NULL);
642 CPWL_Color crBackground, crBorder, crText;
647 pControl->GetOriginalBackgroundColor(iColorType, fc);
649 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
651 pControl->GetOriginalBorderColor(iColorType, fc);
653 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
655 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
656 int32_t nBorderStyle = 0;
657 CPWL_Dash dsBorder(3, 0, 0);
658 CPWL_Color crLeftTop, crRightBottom;
660 switch (GetBorderStyle()) {
662 nBorderStyle = PBS_DASH;
663 dsBorder = CPWL_Dash(3, 3, 0);
666 nBorderStyle = PBS_BEVELED;
668 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
669 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
672 nBorderStyle = PBS_INSET;
674 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
675 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
678 nBorderStyle = PBS_UNDERLINED;
681 nBorderStyle = PBS_SOLID;
685 CPDF_Rect rcWindow = GetRotatedRect();
686 CPDF_Rect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
688 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
690 da.GetColor(iColorType, fc);
691 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
696 CFX_WideString csWCaption = pControl->GetNormalCaption();
697 if (csWCaption.GetLength() > 0) {
698 switch (csWCaption[0]) {
706 nStyle = PCS_DIAMOND;
722 CFX_ByteString csAP_N_ON =
723 CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
724 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
725 crLeftTop, crRightBottom, nBorderStyle,
728 CFX_ByteString csAP_N_OFF = csAP_N_ON;
730 switch (nBorderStyle) {
732 CPWL_Color crTemp = crLeftTop;
733 crLeftTop = crRightBottom;
734 crRightBottom = crTemp;
737 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
738 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
742 CFX_ByteString csAP_D_ON =
743 CPWL_Utils::GetRectFillAppStream(
744 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
745 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
746 crLeftTop, crRightBottom, nBorderStyle,
749 CFX_ByteString csAP_D_OFF = csAP_D_ON;
751 csAP_N_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient, nStyle, crText);
752 csAP_D_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient, nStyle, crText);
754 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON,
755 pControl->GetCheckedAPState());
756 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
758 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON,
759 pControl->GetCheckedAPState());
760 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
762 CFX_ByteString csAS = GetAppState();
767 void CPDFSDK_Widget::ResetAppearance_RadioButton() {
768 CPDF_FormControl* pControl = GetFormControl();
769 ASSERT(pControl != NULL);
771 CPWL_Color crBackground, crBorder, crText;
776 pControl->GetOriginalBackgroundColor(iColorType, fc);
778 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
780 pControl->GetOriginalBorderColor(iColorType, fc);
782 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
784 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
785 int32_t nBorderStyle = 0;
786 CPWL_Dash dsBorder(3, 0, 0);
787 CPWL_Color crLeftTop, crRightBottom;
789 switch (GetBorderStyle()) {
791 nBorderStyle = PBS_DASH;
792 dsBorder = CPWL_Dash(3, 3, 0);
795 nBorderStyle = PBS_BEVELED;
797 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
798 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
801 nBorderStyle = PBS_INSET;
803 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
804 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
807 nBorderStyle = PBS_UNDERLINED;
810 nBorderStyle = PBS_SOLID;
814 CPDF_Rect rcWindow = GetRotatedRect();
815 CPDF_Rect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
817 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
819 da.GetColor(iColorType, fc);
820 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
825 CFX_WideString csWCaption = pControl->GetNormalCaption();
826 if (csWCaption.GetLength() > 0) {
827 switch (csWCaption[0]) {
835 nStyle = PCS_DIAMOND;
851 CFX_ByteString csAP_N_ON;
854 CPWL_Utils::DeflateRect(CPWL_Utils::GetCenterSquare(rcWindow), 1.0f);
856 if (nStyle == PCS_CIRCLE) {
857 if (nBorderStyle == PBS_BEVELED) {
858 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
859 crRightBottom = CPWL_Utils::SubstractColor(crBackground, 0.25f);
860 } else if (nBorderStyle == PBS_INSET) {
861 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5f);
862 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75f);
865 csAP_N_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter, crBackground) +
866 CPWL_Utils::GetCircleBorderAppStream(
867 rcCenter, fBorderWidth, crBorder, crLeftTop, crRightBottom,
868 nBorderStyle, dsBorder);
870 csAP_N_ON = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
871 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
872 crLeftTop, crRightBottom,
873 nBorderStyle, dsBorder);
876 CFX_ByteString csAP_N_OFF = csAP_N_ON;
878 switch (nBorderStyle) {
880 CPWL_Color crTemp = crLeftTop;
881 crLeftTop = crRightBottom;
882 crRightBottom = crTemp;
885 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
886 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
890 CFX_ByteString csAP_D_ON;
892 if (nStyle == PCS_CIRCLE) {
893 CPWL_Color crBK = CPWL_Utils::SubstractColor(crBackground, 0.25f);
894 if (nBorderStyle == PBS_BEVELED) {
895 crLeftTop = CPWL_Utils::SubstractColor(crBackground, 0.25f);
896 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
898 } else if (nBorderStyle == PBS_INSET) {
899 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
900 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
903 csAP_D_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter, crBK) +
904 CPWL_Utils::GetCircleBorderAppStream(
905 rcCenter, fBorderWidth, crBorder, crLeftTop, crRightBottom,
906 nBorderStyle, dsBorder);
908 csAP_D_ON = CPWL_Utils::GetRectFillAppStream(
909 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
910 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
911 crLeftTop, crRightBottom,
912 nBorderStyle, dsBorder);
915 CFX_ByteString csAP_D_OFF = csAP_D_ON;
917 csAP_N_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient, nStyle, crText);
918 csAP_D_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient, nStyle, crText);
920 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON,
921 pControl->GetCheckedAPState());
922 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
924 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON,
925 pControl->GetCheckedAPState());
926 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
928 CFX_ByteString csAS = GetAppState();
933 void CPDFSDK_Widget::ResetAppearance_ComboBox(const FX_WCHAR* sValue) {
934 CPDF_FormControl* pControl = GetFormControl();
935 ASSERT(pControl != NULL);
936 CPDF_FormField* pField = pControl->GetField();
937 ASSERT(pField != NULL);
939 CFX_ByteTextBuf sBody, sLines;
941 CPDF_Rect rcClient = GetClientRect();
942 CPDF_Rect rcButton = rcClient;
943 rcButton.left = rcButton.right - 13;
944 rcButton.Normalize();
946 if (IFX_Edit* pEdit = IFX_Edit::NewEdit()) {
947 pEdit->EnableRefresh(FALSE);
949 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
950 ASSERT(pDoc != NULL);
951 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
952 CBA_FontMap FontMap(this, pEnv->GetSysHandler());
954 pEdit->SetFontMap(&FontMap);
956 CPDF_Rect rcEdit = rcClient;
957 rcEdit.right = rcButton.left;
960 pEdit->SetPlateRect(rcEdit);
961 pEdit->SetAlignmentV(1);
963 FX_FLOAT fFontSize = GetFontSize();
964 if (IsFloatZero(fFontSize))
965 pEdit->SetAutoFontSize(TRUE);
967 pEdit->SetFontSize(fFontSize);
972 pEdit->SetText(sValue);
974 int32_t nCurSel = pField->GetSelectedIndex(0);
977 pEdit->SetText(pField->GetValue().c_str());
979 pEdit->SetText(pField->GetOptionLabel(nCurSel).c_str());
982 CPDF_Rect rcContent = pEdit->GetContentRect();
984 CFX_ByteString sEdit =
985 CPWL_Utils::GetEditAppStream(pEdit, CPDF_Point(0.0f, 0.0f));
986 if (sEdit.GetLength() > 0) {
989 if (rcContent.Width() > rcEdit.Width() ||
990 rcContent.Height() > rcEdit.Height()) {
991 sBody << rcEdit.left << " " << rcEdit.bottom << " " << rcEdit.Width()
992 << " " << rcEdit.Height() << " re\nW\nn\n";
995 CPWL_Color crText = GetTextPWLColor();
996 sBody << "BT\n" << CPWL_Utils::GetColorAppStream(crText) << sEdit
1001 IFX_Edit::DelEdit(pEdit);
1004 sBody << CPWL_Utils::GetDropButtonAppStream(rcButton);
1006 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
1007 sLines.GetByteString() + sBody.GetByteString();
1009 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
1012 void CPDFSDK_Widget::ResetAppearance_ListBox() {
1013 CPDF_FormControl* pControl = GetFormControl();
1014 ASSERT(pControl != NULL);
1015 CPDF_FormField* pField = pControl->GetField();
1016 ASSERT(pField != NULL);
1018 CPDF_Rect rcClient = GetClientRect();
1020 CFX_ByteTextBuf sBody, sLines;
1022 if (IFX_Edit* pEdit = IFX_Edit::NewEdit()) {
1023 pEdit->EnableRefresh(FALSE);
1025 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1026 ASSERT(pDoc != NULL);
1027 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
1029 CBA_FontMap FontMap(this, pEnv->GetSysHandler());
1031 pEdit->SetFontMap(&FontMap);
1033 pEdit->SetPlateRect(CPDF_Rect(rcClient.left, 0.0f, rcClient.right, 0.0f));
1035 FX_FLOAT fFontSize = GetFontSize();
1037 if (IsFloatZero(fFontSize))
1038 pEdit->SetFontSize(12.0f);
1040 pEdit->SetFontSize(fFontSize);
1042 pEdit->Initialize();
1044 CFX_ByteTextBuf sList;
1045 FX_FLOAT fy = rcClient.top;
1047 int32_t nTop = pField->GetTopVisibleIndex();
1048 int32_t nCount = pField->CountOptions();
1049 int32_t nSelCount = pField->CountSelectedItems();
1051 for (int32_t i = nTop; i < nCount; i++) {
1052 FX_BOOL bSelected = FALSE;
1053 for (int32_t j = 0; j < nSelCount; j++) {
1054 if (pField->GetSelectedIndex(j) == i) {
1060 pEdit->SetText(pField->GetOptionLabel(i).c_str());
1062 CPDF_Rect rcContent = pEdit->GetContentRect();
1063 FX_FLOAT fItemHeight = rcContent.Height();
1067 CPDF_Rect(rcClient.left, fy - fItemHeight, rcClient.right, fy);
1068 sList << "q\n" << CPWL_Utils::GetColorAppStream(
1069 CPWL_Color(COLORTYPE_RGB, 0, 51.0f / 255.0f,
1072 << rcItem.left << " " << rcItem.bottom << " " << rcItem.Width()
1073 << " " << rcItem.Height() << " re f\n"
1076 sList << "BT\n" << CPWL_Utils::GetColorAppStream(
1077 CPWL_Color(COLORTYPE_GRAY, 1), TRUE)
1078 << CPWL_Utils::GetEditAppStream(pEdit, CPDF_Point(0.0f, fy))
1081 CPWL_Color crText = GetTextPWLColor();
1082 sList << "BT\n" << CPWL_Utils::GetColorAppStream(crText, TRUE)
1083 << CPWL_Utils::GetEditAppStream(pEdit, CPDF_Point(0.0f, fy))
1090 if (sList.GetSize() > 0) {
1091 sBody << "/Tx BMC\n"
1092 << "q\n" << rcClient.left << " " << rcClient.bottom << " "
1093 << rcClient.Width() << " " << rcClient.Height() << " re\nW\nn\n";
1094 sBody << sList << "Q\nEMC\n";
1097 IFX_Edit::DelEdit(pEdit);
1100 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
1101 sLines.GetByteString() + sBody.GetByteString();
1103 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
1106 void CPDFSDK_Widget::ResetAppearance_TextField(const FX_WCHAR* sValue) {
1107 CPDF_FormControl* pControl = GetFormControl();
1108 ASSERT(pControl != NULL);
1109 CPDF_FormField* pField = pControl->GetField();
1110 ASSERT(pField != NULL);
1112 CFX_ByteTextBuf sBody, sLines;
1114 if (IFX_Edit* pEdit = IFX_Edit::NewEdit()) {
1115 pEdit->EnableRefresh(FALSE);
1117 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1118 ASSERT(pDoc != NULL);
1119 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
1121 CBA_FontMap FontMap(this, pEnv->GetSysHandler());
1123 pEdit->SetFontMap(&FontMap);
1125 CPDF_Rect rcClient = GetClientRect();
1126 pEdit->SetPlateRect(rcClient);
1127 pEdit->SetAlignmentH(pControl->GetControlAlignment());
1129 FX_DWORD dwFieldFlags = pField->GetFieldFlags();
1130 FX_BOOL bMultiLine = (dwFieldFlags >> 12) & 1;
1133 pEdit->SetMultiLine(TRUE);
1134 pEdit->SetAutoReturn(TRUE);
1136 pEdit->SetAlignmentV(1);
1139 FX_WORD subWord = 0;
1140 if ((dwFieldFlags >> 13) & 1) {
1142 pEdit->SetPasswordChar(subWord);
1145 int nMaxLen = pField->GetMaxLen();
1146 FX_BOOL bCharArray = (dwFieldFlags >> 24) & 1;
1147 FX_FLOAT fFontSize = GetFontSize();
1151 pEdit->SetCharArray(nMaxLen);
1153 if (IsFloatZero(fFontSize)) {
1154 fFontSize = CPWL_Edit::GetCharArrayAutoFontSize(FontMap.GetPDFFont(0),
1159 nMaxLen = wcslen((const wchar_t*)sValue);
1160 pEdit->SetLimitChar(nMaxLen);
1164 if (IsFloatZero(fFontSize))
1165 pEdit->SetAutoFontSize(TRUE);
1167 pEdit->SetFontSize(fFontSize);
1169 pEdit->Initialize();
1172 pEdit->SetText(sValue);
1174 pEdit->SetText(pField->GetValue().c_str());
1176 CPDF_Rect rcContent = pEdit->GetContentRect();
1178 CFX_ByteString sEdit = CPWL_Utils::GetEditAppStream(
1179 pEdit, CPDF_Point(0.0f, 0.0f), NULL, !bCharArray, subWord);
1181 if (sEdit.GetLength() > 0) {
1182 sBody << "/Tx BMC\n"
1184 if (rcContent.Width() > rcClient.Width() ||
1185 rcContent.Height() > rcClient.Height()) {
1186 sBody << rcClient.left << " " << rcClient.bottom << " "
1187 << rcClient.Width() << " " << rcClient.Height() << " re\nW\nn\n";
1189 CPWL_Color crText = GetTextPWLColor();
1190 sBody << "BT\n" << CPWL_Utils::GetColorAppStream(crText) << sEdit
1196 switch (GetBorderStyle()) {
1198 CFX_ByteString sColor =
1199 CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE);
1200 if (sColor.GetLength() > 0) {
1201 sLines << "q\n" << GetBorderWidth() << " w\n"
1202 << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE)
1205 for (int32_t i = 1; i < nMaxLen; i++) {
1206 sLines << rcClient.left +
1207 ((rcClient.right - rcClient.left) / nMaxLen) * i
1208 << " " << rcClient.bottom << " m\n"
1210 ((rcClient.right - rcClient.left) / nMaxLen) * i
1211 << " " << rcClient.top << " l S\n";
1218 CFX_ByteString sColor =
1219 CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE);
1220 if (sColor.GetLength() > 0) {
1221 CPWL_Dash dsBorder = CPWL_Dash(3, 3, 0);
1223 sLines << "q\n" << GetBorderWidth() << " w\n"
1224 << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE)
1225 << "[" << dsBorder.nDash << " " << dsBorder.nGap << "] "
1226 << dsBorder.nPhase << " d\n";
1228 for (int32_t i = 1; i < nMaxLen; i++) {
1229 sLines << rcClient.left +
1230 ((rcClient.right - rcClient.left) / nMaxLen) * i
1231 << " " << rcClient.bottom << " m\n"
1233 ((rcClient.right - rcClient.left) / nMaxLen) * i
1234 << " " << rcClient.top << " l S\n";
1243 IFX_Edit::DelEdit(pEdit);
1246 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
1247 sLines.GetByteString() + sBody.GetByteString();
1248 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
1251 CPDF_Rect CPDFSDK_Widget::GetClientRect() const {
1252 CPDF_Rect rcWindow = GetRotatedRect();
1253 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1254 switch (GetBorderStyle()) {
1257 fBorderWidth *= 2.0f;
1261 return CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
1264 CPDF_Rect CPDFSDK_Widget::GetRotatedRect() const {
1265 CPDF_Rect rectAnnot = GetRect();
1266 FX_FLOAT fWidth = rectAnnot.right - rectAnnot.left;
1267 FX_FLOAT fHeight = rectAnnot.top - rectAnnot.bottom;
1269 CPDF_FormControl* pControl = GetFormControl();
1270 CPDF_Rect rcPDFWindow;
1271 switch (abs(pControl->GetRotation() % 360)) {
1275 rcPDFWindow = CPDF_Rect(0, 0, fWidth, fHeight);
1279 rcPDFWindow = CPDF_Rect(0, 0, fHeight, fWidth);
1286 CFX_ByteString CPDFSDK_Widget::GetBackgroundAppStream() const {
1287 CPWL_Color crBackground = GetFillPWLColor();
1288 if (crBackground.nColorType != COLORTYPE_TRANSPARENT) {
1289 return CPWL_Utils::GetRectFillAppStream(GetRotatedRect(), crBackground);
1294 CFX_ByteString CPDFSDK_Widget::GetBorderAppStream() const {
1295 CPDF_Rect rcWindow = GetRotatedRect();
1296 CPWL_Color crBorder = GetBorderPWLColor();
1297 CPWL_Color crBackground = GetFillPWLColor();
1298 CPWL_Color crLeftTop, crRightBottom;
1300 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1301 int32_t nBorderStyle = 0;
1302 CPWL_Dash dsBorder(3, 0, 0);
1304 switch (GetBorderStyle()) {
1306 nBorderStyle = PBS_DASH;
1307 dsBorder = CPWL_Dash(3, 3, 0);
1310 nBorderStyle = PBS_BEVELED;
1312 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1313 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1316 nBorderStyle = PBS_INSET;
1318 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1319 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1322 nBorderStyle = PBS_UNDERLINED;
1325 nBorderStyle = PBS_SOLID;
1329 return CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1330 crLeftTop, crRightBottom, nBorderStyle,
1334 CPDF_Matrix CPDFSDK_Widget::GetMatrix() const {
1336 CPDF_FormControl* pControl = GetFormControl();
1337 ASSERT(pControl != NULL);
1339 CPDF_Rect rcAnnot = GetRect();
1340 FX_FLOAT fWidth = rcAnnot.right - rcAnnot.left;
1341 FX_FLOAT fHeight = rcAnnot.top - rcAnnot.bottom;
1343 switch (abs(pControl->GetRotation() % 360)) {
1346 mt = CPDF_Matrix(1, 0, 0, 1, 0, 0);
1349 mt = CPDF_Matrix(0, 1, -1, 0, fWidth, 0);
1352 mt = CPDF_Matrix(-1, 0, 0, -1, fWidth, fHeight);
1355 mt = CPDF_Matrix(0, -1, 1, 0, 0, fHeight);
1362 CPWL_Color CPDFSDK_Widget::GetTextPWLColor() const {
1363 CPWL_Color crText = CPWL_Color(COLORTYPE_GRAY, 0);
1365 CPDF_FormControl* pFormCtrl = GetFormControl();
1366 ASSERT(pFormCtrl != NULL);
1368 CPDF_DefaultAppearance da = pFormCtrl->GetDefaultAppearance();
1369 if (da.HasColor()) {
1372 da.GetColor(iColorType, fc);
1373 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1379 CPWL_Color CPDFSDK_Widget::GetBorderPWLColor() const {
1380 CPWL_Color crBorder;
1382 CPDF_FormControl* pFormCtrl = GetFormControl();
1383 ASSERT(pFormCtrl != NULL);
1387 pFormCtrl->GetOriginalBorderColor(iColorType, fc);
1389 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1394 CPWL_Color CPDFSDK_Widget::GetFillPWLColor() const {
1397 CPDF_FormControl* pFormCtrl = GetFormControl();
1398 ASSERT(pFormCtrl != NULL);
1402 pFormCtrl->GetOriginalBackgroundColor(iColorType, fc);
1404 crFill = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1409 void CPDFSDK_Widget::AddImageToAppearance(const CFX_ByteString& sAPType,
1410 CPDF_Stream* pImage) {
1411 ASSERT(pImage != NULL);
1413 CPDF_Document* pDoc = m_pPageView->GetPDFDocument();
1414 ASSERT(pDoc != NULL);
1416 CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDict("AP");
1417 ASSERT(pAPDict != NULL);
1419 CPDF_Stream* pStream = pAPDict->GetStream(sAPType);
1420 ASSERT(pStream != NULL);
1422 CPDF_Dictionary* pStreamDict = pStream->GetDict();
1423 ASSERT(pStreamDict != NULL);
1425 CFX_ByteString sImageAlias = "IMG";
1427 if (CPDF_Dictionary* pImageDict = pImage->GetDict()) {
1428 sImageAlias = pImageDict->GetString("Name");
1429 if (sImageAlias.IsEmpty())
1430 sImageAlias = "IMG";
1433 CPDF_Dictionary* pStreamResList = pStreamDict->GetDict("Resources");
1434 if (!pStreamResList) {
1435 pStreamResList = new CPDF_Dictionary();
1436 pStreamDict->SetAt("Resources", pStreamResList);
1439 if (pStreamResList) {
1440 CPDF_Dictionary* pXObject = new CPDF_Dictionary;
1441 pXObject->SetAtReference(sImageAlias, pDoc, pImage);
1442 pStreamResList->SetAt("XObject", pXObject);
1446 void CPDFSDK_Widget::RemoveAppearance(const CFX_ByteString& sAPType) {
1447 if (CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDict("AP")) {
1448 pAPDict->RemoveAt(sAPType);
1452 FX_BOOL CPDFSDK_Widget::OnAAction(CPDF_AAction::AActionType type,
1453 PDFSDK_FieldAction& data,
1454 CPDFSDK_PageView* pPageView) {
1455 CPDF_Action action = GetAAction(type);
1457 if (action && action.GetType() != CPDF_Action::Unknown) {
1458 CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
1459 CPDFDoc_Environment* pEnv = pDocument->GetEnv();
1460 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
1461 return pActionHandler->DoAction_Field(action, type, pDocument,
1462 GetFormField(), data);
1467 CPDF_Action CPDFSDK_Widget::GetAAction(CPDF_AAction::AActionType eAAT) {
1469 case CPDF_AAction::CursorEnter:
1470 case CPDF_AAction::CursorExit:
1471 case CPDF_AAction::ButtonDown:
1472 case CPDF_AAction::ButtonUp:
1473 case CPDF_AAction::GetFocus:
1474 case CPDF_AAction::LoseFocus:
1475 case CPDF_AAction::PageOpen:
1476 case CPDF_AAction::PageClose:
1477 case CPDF_AAction::PageVisible:
1478 case CPDF_AAction::PageInvisible:
1479 return CPDFSDK_Annot::GetAAction(eAAT);
1481 case CPDF_AAction::KeyStroke:
1482 case CPDF_AAction::Format:
1483 case CPDF_AAction::Validate:
1484 case CPDF_AAction::Calculate: {
1485 CPDF_FormField* pField = GetFormField();
1486 if (CPDF_AAction aa = pField->GetAdditionalAction())
1487 return aa.GetAction(eAAT);
1489 return CPDFSDK_Annot::GetAAction(eAAT);
1495 return CPDF_Action();
1498 CFX_WideString CPDFSDK_Widget::GetAlternateName() const {
1499 CPDF_FormField* pFormField = GetFormField();
1500 ASSERT(pFormField != NULL);
1502 return pFormField->GetAlternateName();
1505 int32_t CPDFSDK_Widget::GetAppearanceAge() const {
1509 int32_t CPDFSDK_Widget::GetValueAge() const {
1513 FX_BOOL CPDFSDK_Widget::HitTest(FX_FLOAT pageX, FX_FLOAT pageY) {
1514 CPDF_Annot* pAnnot = GetPDFAnnot();
1515 CFX_FloatRect annotRect;
1516 pAnnot->GetRect(annotRect);
1517 if (annotRect.Contains(pageX, pageY)) {
1521 int nFieldFlags = GetFieldFlags();
1522 if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY)
1530 CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_Document* pDocument)
1531 : m_pDocument(pDocument),
1535 ASSERT(m_pDocument != NULL);
1536 m_pInterForm = new CPDF_InterForm(m_pDocument->GetDocument(), FALSE);
1537 ASSERT(m_pInterForm != NULL);
1538 m_pInterForm->SetFormNotify(this);
1540 for (int i = 0; i < 6; i++)
1541 m_bNeedHightlight[i] = FALSE;
1542 m_iHighlightAlpha = 0;
1545 CPDFSDK_InterForm::~CPDFSDK_InterForm() {
1546 delete m_pInterForm;
1547 m_pInterForm = nullptr;
1551 FX_BOOL CPDFSDK_InterForm::HighlightWidgets() {
1555 CPDFSDK_Widget* CPDFSDK_InterForm::GetSibling(CPDFSDK_Widget* pWidget,
1556 FX_BOOL bNext) const {
1557 nonstd::unique_ptr<CBA_AnnotIterator> pIterator(
1558 new CBA_AnnotIterator(pWidget->GetPageView(), "Widget", ""));
1561 return (CPDFSDK_Widget*)pIterator->GetNextAnnot(pWidget);
1563 return (CPDFSDK_Widget*)pIterator->GetPrevAnnot(pWidget);
1566 CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl) const {
1567 if (!pControl || !m_pInterForm)
1570 CPDFSDK_Widget* pWidget = nullptr;
1571 const auto it = m_Map.find(pControl);
1572 if (it != m_Map.end())
1573 pWidget = it->second;
1578 CPDF_Dictionary* pControlDict = pControl->GetWidget();
1579 CPDF_Document* pDocument = m_pDocument->GetDocument();
1580 CPDFSDK_PageView* pPage = nullptr;
1582 if (CPDF_Dictionary* pPageDict = pControlDict->GetDict("P")) {
1583 int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum());
1584 if (nPageIndex >= 0) {
1585 pPage = m_pDocument->GetPageView(nPageIndex);
1590 int nPageIndex = GetPageIndexByAnnotDict(pDocument, pControlDict);
1591 if (nPageIndex >= 0) {
1592 pPage = m_pDocument->GetPageView(nPageIndex);
1598 return (CPDFSDK_Widget*)pPage->GetAnnotByDict(pControlDict);
1601 void CPDFSDK_InterForm::GetWidgets(const CFX_WideString& sFieldName,
1602 CFX_PtrArray& widgets) {
1603 ASSERT(m_pInterForm != NULL);
1605 for (int i = 0, sz = m_pInterForm->CountFields(sFieldName); i < sz; i++) {
1606 CPDF_FormField* pFormField = m_pInterForm->GetField(i, sFieldName);
1607 ASSERT(pFormField != NULL);
1609 GetWidgets(pFormField, widgets);
1613 void CPDFSDK_InterForm::GetWidgets(CPDF_FormField* pField,
1614 CFX_PtrArray& widgets) {
1615 ASSERT(pField != NULL);
1617 for (int i = 0, isz = pField->CountControls(); i < isz; i++) {
1618 CPDF_FormControl* pFormCtrl = pField->GetControl(i);
1619 ASSERT(pFormCtrl != NULL);
1621 CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl);
1624 widgets.Add(pWidget);
1628 int CPDFSDK_InterForm::GetPageIndexByAnnotDict(
1629 CPDF_Document* pDocument,
1630 CPDF_Dictionary* pAnnotDict) const {
1631 ASSERT(pDocument != NULL);
1632 ASSERT(pAnnotDict != NULL);
1634 for (int i = 0, sz = pDocument->GetPageCount(); i < sz; i++) {
1635 if (CPDF_Dictionary* pPageDict = pDocument->GetPage(i)) {
1636 if (CPDF_Array* pAnnots = pPageDict->GetArray("Annots")) {
1637 for (int j = 0, jsz = pAnnots->GetCount(); j < jsz; j++) {
1638 CPDF_Object* pDict = pAnnots->GetElementValue(j);
1639 if (pAnnotDict == pDict) {
1650 void CPDFSDK_InterForm::AddMap(CPDF_FormControl* pControl,
1651 CPDFSDK_Widget* pWidget) {
1652 m_Map[pControl] = pWidget;
1655 void CPDFSDK_InterForm::RemoveMap(CPDF_FormControl* pControl) {
1656 m_Map.erase(pControl);
1659 void CPDFSDK_InterForm::EnableCalculate(FX_BOOL bEnabled) {
1660 m_bCalculate = bEnabled;
1663 FX_BOOL CPDFSDK_InterForm::IsCalculateEnabled() const {
1664 return m_bCalculate;
1668 CPDF_Stream* CPDFSDK_InterForm::LoadImageFromFile(const CFX_WideString& sFile) {
1669 ASSERT(m_pDocument != NULL);
1670 CPDF_Document* pDocument = m_pDocument->GetDocument();
1671 ASSERT(pDocument != NULL);
1673 CPDF_Stream* pRetStream = NULL;
1675 if (CFX_DIBitmap* pBmp = CFX_WindowsDIB::LoadFromFile(sFile.c_str())) {
1676 int nWidth = pBmp->GetWidth();
1677 int nHeight = pBmp->GetHeight();
1679 CPDF_Image Image(pDocument);
1680 Image.SetImage(pBmp, FALSE);
1681 CPDF_Stream* pImageStream = Image.GetStream();
1683 if (pImageStream->GetObjNum() == 0)
1684 pDocument->AddIndirectObject(pImageStream);
1686 CPDF_Dictionary* pStreamDict = new CPDF_Dictionary();
1687 pStreamDict->SetAtName("Subtype", "Form");
1688 pStreamDict->SetAtName("Name", "IMG");
1689 CPDF_Array* pMatrix = new CPDF_Array();
1690 pStreamDict->SetAt("Matrix", pMatrix);
1691 pMatrix->AddInteger(1);
1692 pMatrix->AddInteger(0);
1693 pMatrix->AddInteger(0);
1694 pMatrix->AddInteger(1);
1695 pMatrix->AddInteger(-nWidth / 2);
1696 pMatrix->AddInteger(-nHeight / 2);
1697 CPDF_Dictionary* pResource = new CPDF_Dictionary();
1698 pStreamDict->SetAt("Resources", pResource);
1699 CPDF_Dictionary* pXObject = new CPDF_Dictionary();
1700 pResource->SetAt("XObject", pXObject);
1701 pXObject->SetAtReference("Img", pDocument, pImageStream);
1702 CPDF_Array* pProcSet = new CPDF_Array();
1703 pResource->SetAt("ProcSet", pProcSet);
1704 pProcSet->AddName("PDF");
1705 pProcSet->AddName("ImageC");
1706 pStreamDict->SetAtName("Type", "XObject");
1707 CPDF_Array* pBBox = new CPDF_Array();
1708 pStreamDict->SetAt("BBox", pBBox);
1709 pBBox->AddInteger(0);
1710 pBBox->AddInteger(0);
1711 pBBox->AddInteger(nWidth);
1712 pBBox->AddInteger(nHeight);
1713 pStreamDict->SetAtInteger("FormType", 1);
1715 pRetStream = new CPDF_Stream(NULL, 0, NULL);
1716 CFX_ByteString csStream;
1717 csStream.Format("q\n%d 0 0 %d 0 0 cm\n/Img Do\nQ", nWidth, nHeight);
1718 pRetStream->InitStream((uint8_t*)csStream.c_str(), csStream.GetLength(),
1720 pDocument->AddIndirectObject(pRetStream);
1730 void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) {
1731 ASSERT(m_pDocument != NULL);
1732 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
1734 if (!pEnv->IsJSInitiated())
1742 if (IsCalculateEnabled()) {
1743 IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
1744 ASSERT(pRuntime != NULL);
1746 pRuntime->SetReaderDocument(m_pDocument);
1748 int nSize = m_pInterForm->CountFieldsInCalculationOrder();
1749 for (int i = 0; i < nSize; i++) {
1750 if (CPDF_FormField* pField =
1751 m_pInterForm->GetFieldInCalculationOrder(i)) {
1752 int nType = pField->GetFieldType();
1753 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
1754 CPDF_AAction aAction = pField->GetAdditionalAction();
1755 if (aAction && aAction.ActionExist(CPDF_AAction::Calculate)) {
1756 CPDF_Action action = aAction.GetAction(CPDF_AAction::Calculate);
1758 CFX_WideString csJS = action.GetJavaScript();
1759 if (!csJS.IsEmpty()) {
1760 IJS_Context* pContext = pRuntime->NewContext();
1761 ASSERT(pContext != NULL);
1763 CFX_WideString sOldValue = pField->GetValue();
1764 CFX_WideString sValue = sOldValue;
1766 pContext->OnField_Calculate(pFormField, pField, sValue, bRC);
1768 CFX_WideString sInfo;
1769 FX_BOOL bRet = pContext->RunScript(csJS, &sInfo);
1770 pRuntime->ReleaseContext(pContext);
1774 if (sValue.Compare(sOldValue) != 0)
1775 pField->SetValue(sValue, TRUE);
1789 CFX_WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField,
1790 FX_BOOL& bFormated) {
1791 ASSERT(m_pDocument != NULL);
1792 ASSERT(pFormField != NULL);
1794 CFX_WideString sValue = pFormField->GetValue();
1795 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
1797 if (!pEnv->IsJSInitiated()) {
1802 IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
1803 ASSERT(pRuntime != NULL);
1805 pRuntime->SetReaderDocument(m_pDocument);
1807 if (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX) {
1808 if (pFormField->CountSelectedItems() > 0) {
1809 int index = pFormField->GetSelectedIndex(0);
1811 sValue = pFormField->GetOptionLabel(index);
1817 CPDF_AAction aAction = pFormField->GetAdditionalAction();
1818 if (aAction != NULL && aAction.ActionExist(CPDF_AAction::Format)) {
1819 CPDF_Action action = aAction.GetAction(CPDF_AAction::Format);
1821 CFX_WideString script = action.GetJavaScript();
1822 if (!script.IsEmpty()) {
1823 CFX_WideString Value = sValue;
1825 IJS_Context* pContext = pRuntime->NewContext();
1826 ASSERT(pContext != NULL);
1828 pContext->OnField_Format(pFormField, Value, TRUE);
1830 CFX_WideString sInfo;
1831 FX_BOOL bRet = pContext->RunScript(script, &sInfo);
1832 pRuntime->ReleaseContext(pContext);
1845 void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField,
1846 const FX_WCHAR* sValue,
1847 FX_BOOL bValueChanged) {
1848 ASSERT(pFormField != NULL);
1850 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
1851 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
1852 ASSERT(pFormCtrl != NULL);
1854 ASSERT(m_pInterForm != NULL);
1855 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl))
1856 pWidget->ResetAppearance(sValue, bValueChanged);
1860 void CPDFSDK_InterForm::UpdateField(CPDF_FormField* pFormField) {
1861 ASSERT(pFormField != NULL);
1863 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
1864 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
1865 ASSERT(pFormCtrl != NULL);
1867 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) {
1868 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
1869 CFFL_IFormFiller* pIFormFiller = pEnv->GetIFormFiller();
1871 CPDF_Page* pPage = pWidget->GetPDFPage();
1872 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, FALSE);
1874 FX_RECT rcBBox = pIFormFiller->GetViewBBox(pPageView, pWidget);
1876 pEnv->FFI_Invalidate(pPage, rcBBox.left, rcBBox.top, rcBBox.right,
1882 void CPDFSDK_InterForm::OnKeyStrokeCommit(CPDF_FormField* pFormField,
1883 CFX_WideString& csValue,
1885 ASSERT(pFormField != NULL);
1887 CPDF_AAction aAction = pFormField->GetAdditionalAction();
1888 if (aAction != NULL && aAction.ActionExist(CPDF_AAction::KeyStroke)) {
1889 CPDF_Action action = aAction.GetAction(CPDF_AAction::KeyStroke);
1891 ASSERT(m_pDocument != NULL);
1892 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
1893 ASSERT(pEnv != NULL);
1895 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
1896 ASSERT(pActionHandler != NULL);
1898 PDFSDK_FieldAction fa;
1899 fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
1900 fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
1901 fa.sValue = csValue;
1903 pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::KeyStroke,
1904 m_pDocument, pFormField, fa);
1910 void CPDFSDK_InterForm::OnValidate(CPDF_FormField* pFormField,
1911 CFX_WideString& csValue,
1913 ASSERT(pFormField != NULL);
1915 CPDF_AAction aAction = pFormField->GetAdditionalAction();
1916 if (aAction != NULL && aAction.ActionExist(CPDF_AAction::Validate)) {
1917 CPDF_Action action = aAction.GetAction(CPDF_AAction::Validate);
1919 ASSERT(m_pDocument != NULL);
1920 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
1921 ASSERT(pEnv != NULL);
1923 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
1924 ASSERT(pActionHandler != NULL);
1926 PDFSDK_FieldAction fa;
1927 fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
1928 fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
1929 fa.sValue = csValue;
1931 pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::Validate,
1932 m_pDocument, pFormField, fa);
1938 /* ----------------------------- action ----------------------------- */
1940 FX_BOOL CPDFSDK_InterForm::DoAction_Hide(const CPDF_Action& action) {
1943 CPDF_ActionFields af = action.GetWidgets();
1944 CFX_PtrArray fieldObjects;
1945 af.GetAllFields(fieldObjects);
1946 CFX_PtrArray widgetArray;
1947 CFX_PtrArray fields;
1948 GetFieldFromObjects(fieldObjects, fields);
1950 FX_BOOL bHide = action.GetHideStatus();
1952 FX_BOOL bChanged = FALSE;
1954 for (int i = 0, sz = fields.GetSize(); i < sz; i++) {
1955 CPDF_FormField* pField = (CPDF_FormField*)fields[i];
1956 ASSERT(pField != NULL);
1958 for (int j = 0, jsz = pField->CountControls(); j < jsz; j++) {
1959 CPDF_FormControl* pControl = pField->GetControl(j);
1960 ASSERT(pControl != NULL);
1962 if (CPDFSDK_Widget* pWidget = GetWidget(pControl)) {
1963 int nFlags = pWidget->GetFlags();
1965 nFlags &= (~ANNOTFLAG_INVISIBLE);
1966 nFlags &= (~ANNOTFLAG_NOVIEW);
1967 nFlags |= (ANNOTFLAG_HIDDEN);
1969 nFlags &= (~ANNOTFLAG_INVISIBLE);
1970 nFlags &= (~ANNOTFLAG_HIDDEN);
1971 nFlags &= (~ANNOTFLAG_NOVIEW);
1973 pWidget->SetFlags(nFlags);
1975 CPDFSDK_PageView* pPageView = pWidget->GetPageView();
1976 ASSERT(pPageView != NULL);
1978 pPageView->UpdateView(pWidget);
1988 FX_BOOL CPDFSDK_InterForm::DoAction_SubmitForm(const CPDF_Action& action) {
1989 ASSERT(m_pInterForm != NULL);
1990 CFX_WideString sDestination = action.GetFilePath();
1991 if (sDestination.IsEmpty())
1994 CPDF_Dictionary* pActionDict = action.GetDict();
1995 if (pActionDict->KeyExist("Fields")) {
1996 CPDF_ActionFields af = action.GetWidgets();
1997 FX_DWORD dwFlags = action.GetFlags();
1998 CFX_PtrArray fieldObjects;
1999 af.GetAllFields(fieldObjects);
2001 CFX_PtrArray fields;
2002 GetFieldFromObjects(fieldObjects, fields);
2003 if (fields.GetSize() != 0) {
2004 FX_BOOL bIncludeOrExclude = !(dwFlags & 0x01);
2005 if (m_pInterForm->CheckRequiredFields(&fields, bIncludeOrExclude))
2008 return SubmitFields(sDestination, fields, bIncludeOrExclude, FALSE);
2011 if (m_pInterForm->CheckRequiredFields())
2014 return SubmitForm(sDestination, FALSE);
2017 FX_BOOL CPDFSDK_InterForm::SubmitFields(const CFX_WideString& csDestination,
2018 const CFX_PtrArray& fields,
2019 FX_BOOL bIncludeOrExclude,
2020 FX_BOOL bUrlEncoded) {
2021 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2022 ASSERT(pEnv != NULL);
2024 CFX_ByteTextBuf textBuf;
2025 ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude, textBuf);
2027 uint8_t* pBuffer = textBuf.GetBuffer();
2028 FX_STRSIZE nBufSize = textBuf.GetLength();
2031 if (!FDFToURLEncodedData(pBuffer, nBufSize))
2035 pEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str());
2040 void CPDFSDK_InterForm::DoFDFBuffer(CFX_ByteString sBuffer) {
2041 ASSERT(m_pDocument != NULL);
2043 if (CFDF_Document* pFDFDocument = CFDF_Document::ParseMemory(
2044 (const unsigned char*)sBuffer.GetBuffer(sBuffer.GetLength()),
2045 sBuffer.GetLength())) {
2046 CPDF_Dictionary* pRootDic = pFDFDocument->GetRoot();
2048 CPDF_Dictionary* pFDFDict = pRootDic->GetDict("FDF");
2050 CPDF_Dictionary* pJSDict = pFDFDict->GetDict("JavaScript");
2052 CFX_WideString csJS;
2054 CPDF_Object* pJS = pJSDict->GetElementValue("Before");
2056 int iType = pJS->GetType();
2057 if (iType == PDFOBJ_STRING)
2058 csJS = pJSDict->GetUnicodeText("Before");
2059 else if (iType == PDFOBJ_STREAM)
2060 csJS = pJS->GetUnicodeText();
2065 delete pFDFDocument;
2068 sBuffer.ReleaseBuffer();
2071 FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(CFX_WideString csFDFFile,
2072 CFX_WideString csTxtFile) {
2076 FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf,
2077 FX_STRSIZE& nBufSize) {
2078 CFDF_Document* pFDF = CFDF_Document::ParseMemory(pBuf, nBufSize);
2080 CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDict("FDF");
2081 if (pMainDict == NULL)
2085 CPDF_Array* pFields = pMainDict->GetArray("Fields");
2086 if (pFields == NULL)
2089 CFX_ByteTextBuf fdfEncodedData;
2091 for (FX_DWORD i = 0; i < pFields->GetCount(); i++) {
2092 CPDF_Dictionary* pField = pFields->GetDict(i);
2095 CFX_WideString name;
2096 name = pField->GetUnicodeText("T");
2097 CFX_ByteString name_b = CFX_ByteString::FromUnicode(name);
2098 CFX_ByteString csBValue = pField->GetString("V");
2099 CFX_WideString csWValue = PDF_DecodeText(csBValue);
2100 CFX_ByteString csValue_b = CFX_ByteString::FromUnicode(csWValue);
2102 fdfEncodedData = fdfEncodedData << name_b.GetBuffer(name_b.GetLength());
2103 name_b.ReleaseBuffer();
2104 fdfEncodedData = fdfEncodedData << "=";
2105 fdfEncodedData = fdfEncodedData
2106 << csValue_b.GetBuffer(csValue_b.GetLength());
2107 csValue_b.ReleaseBuffer();
2108 if (i != pFields->GetCount() - 1)
2109 fdfEncodedData = fdfEncodedData << "&";
2112 nBufSize = fdfEncodedData.GetLength();
2113 pBuf = FX_Alloc(uint8_t, nBufSize);
2114 FXSYS_memcpy(pBuf, fdfEncodedData.GetBuffer(), nBufSize);
2119 FX_BOOL CPDFSDK_InterForm::ExportFieldsToFDFTextBuf(const CFX_PtrArray& fields,
2120 FX_BOOL bIncludeOrExclude,
2121 CFX_ByteTextBuf& textBuf) {
2122 ASSERT(m_pDocument != NULL);
2123 ASSERT(m_pInterForm != NULL);
2125 CFDF_Document* pFDF = m_pInterForm->ExportToFDF(
2126 m_pDocument->GetPath(), (CFX_PtrArray&)fields, bIncludeOrExclude);
2129 FX_BOOL bRet = pFDF->WriteBuf(textBuf);
2135 CFX_WideString CPDFSDK_InterForm::GetTemporaryFileName(
2136 const CFX_WideString& sFileExt) {
2137 CFX_WideString sFileName;
2141 FX_BOOL CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination,
2142 FX_BOOL bUrlEncoded) {
2143 if (sDestination.IsEmpty())
2146 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2147 ASSERT(pEnv != NULL);
2149 if (NULL == m_pDocument)
2151 CFX_WideString wsPDFFilePath = m_pDocument->GetPath();
2153 if (NULL == m_pInterForm)
2155 CFDF_Document* pFDFDoc = m_pInterForm->ExportToFDF(wsPDFFilePath);
2156 if (NULL == pFDFDoc)
2159 CFX_ByteTextBuf FdfBuffer;
2160 FX_BOOL bRet = pFDFDoc->WriteBuf(FdfBuffer);
2165 uint8_t* pBuffer = FdfBuffer.GetBuffer();
2166 FX_STRSIZE nBufSize = FdfBuffer.GetLength();
2169 if (!FDFToURLEncodedData(pBuffer, nBufSize))
2173 pEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str());
2183 FX_BOOL CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) {
2184 ASSERT(m_pInterForm != NULL);
2185 ASSERT(m_pDocument != NULL);
2187 CFDF_Document* pFDF = m_pInterForm->ExportToFDF(m_pDocument->GetPath());
2191 FX_BOOL bRet = pFDF->WriteBuf(textBuf);
2197 FX_BOOL CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) {
2200 CPDF_Dictionary* pActionDict = action.GetDict();
2201 if (pActionDict->KeyExist("Fields")) {
2202 CPDF_ActionFields af = action.GetWidgets();
2203 FX_DWORD dwFlags = action.GetFlags();
2205 CFX_PtrArray fieldObjects;
2206 af.GetAllFields(fieldObjects);
2207 CFX_PtrArray fields;
2208 GetFieldFromObjects(fieldObjects, fields);
2209 return m_pInterForm->ResetForm(fields, !(dwFlags & 0x01), TRUE);
2212 return m_pInterForm->ResetForm(TRUE);
2215 FX_BOOL CPDFSDK_InterForm::DoAction_ImportData(const CPDF_Action& action) {
2219 void CPDFSDK_InterForm::GetFieldFromObjects(const CFX_PtrArray& objects,
2220 CFX_PtrArray& fields) {
2221 ASSERT(m_pInterForm != NULL);
2223 int iCount = objects.GetSize();
2224 for (int i = 0; i < iCount; i++) {
2225 CPDF_Object* pObject = (CPDF_Object*)objects[i];
2226 if (pObject == NULL)
2229 int iType = pObject->GetType();
2230 if (iType == PDFOBJ_STRING) {
2231 CFX_WideString csName = pObject->GetUnicodeText();
2232 CPDF_FormField* pField = m_pInterForm->GetField(0, csName);
2235 } else if (iType == PDFOBJ_DICTIONARY) {
2236 if (m_pInterForm->IsValidFormField(pObject))
2237 fields.Add(pObject);
2242 /* ----------------------------- CPDF_FormNotify -----------------------------
2245 int CPDFSDK_InterForm::BeforeValueChange(const CPDF_FormField* pField,
2246 CFX_WideString& csValue) {
2247 CPDF_FormField* pFormField = (CPDF_FormField*)pField;
2248 int nType = pFormField->GetFieldType();
2249 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
2251 OnKeyStrokeCommit(pFormField, csValue, bRC);
2253 OnValidate(pFormField, csValue, bRC);
2254 return bRC ? 1 : -1;
2261 int CPDFSDK_InterForm::AfterValueChange(const CPDF_FormField* pField) {
2262 CPDF_FormField* pFormField = (CPDF_FormField*)pField;
2263 int nType = pFormField->GetFieldType();
2264 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
2265 OnCalculate(pFormField);
2266 FX_BOOL bFormated = FALSE;
2267 CFX_WideString sValue = OnFormat(pFormField, bFormated);
2269 ResetFieldAppearance(pFormField, sValue.c_str(), TRUE);
2271 ResetFieldAppearance(pFormField, NULL, TRUE);
2272 UpdateField(pFormField);
2277 int CPDFSDK_InterForm::BeforeSelectionChange(const CPDF_FormField* pField,
2278 CFX_WideString& csValue) {
2279 CPDF_FormField* pFormField = (CPDF_FormField*)pField;
2280 if (pFormField->GetFieldType() != FIELDTYPE_LISTBOX)
2284 OnKeyStrokeCommit(pFormField, csValue, bRC);
2288 OnValidate(pFormField, csValue, bRC);
2295 int CPDFSDK_InterForm::AfterSelectionChange(const CPDF_FormField* pField) {
2296 CPDF_FormField* pFormField = (CPDF_FormField*)pField;
2297 if (pFormField->GetFieldType() == FIELDTYPE_LISTBOX) {
2298 OnCalculate(pFormField);
2299 ResetFieldAppearance(pFormField, NULL, TRUE);
2300 UpdateField(pFormField);
2305 int CPDFSDK_InterForm::AfterCheckedStatusChange(
2306 const CPDF_FormField* pField,
2307 const CFX_ByteArray& statusArray) {
2308 CPDF_FormField* pFormField = (CPDF_FormField*)pField;
2309 int nType = pFormField->GetFieldType();
2310 if (nType == FIELDTYPE_CHECKBOX || nType == FIELDTYPE_RADIOBUTTON) {
2311 OnCalculate(pFormField);
2312 UpdateField(pFormField);
2317 int CPDFSDK_InterForm::BeforeFormReset(const CPDF_InterForm* pForm) {
2321 int CPDFSDK_InterForm::AfterFormReset(const CPDF_InterForm* pForm) {
2322 OnCalculate(nullptr);
2326 int CPDFSDK_InterForm::BeforeFormImportData(const CPDF_InterForm* pForm) {
2330 int CPDFSDK_InterForm::AfterFormImportData(const CPDF_InterForm* pForm) {
2331 OnCalculate(nullptr);
2335 FX_BOOL CPDFSDK_InterForm::IsNeedHighLight(int nFieldType) {
2336 if (nFieldType < 1 || nFieldType > 6)
2338 return m_bNeedHightlight[nFieldType - 1];
2341 void CPDFSDK_InterForm::RemoveAllHighLight() {
2342 memset((void*)m_bNeedHightlight, 0, 6 * sizeof(FX_BOOL));
2344 void CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, int nFieldType) {
2345 if (nFieldType < 0 || nFieldType > 6)
2347 switch (nFieldType) {
2349 for (int i = 0; i < 6; i++) {
2350 m_aHighlightColor[i] = clr;
2351 m_bNeedHightlight[i] = TRUE;
2356 m_aHighlightColor[nFieldType - 1] = clr;
2357 m_bNeedHightlight[nFieldType - 1] = TRUE;
2363 FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(int nFieldType) {
2364 if (nFieldType < 0 || nFieldType > 6)
2365 return FXSYS_RGB(255, 255, 255);
2366 if (nFieldType == 0)
2367 return m_aHighlightColor[0];
2368 return m_aHighlightColor[nFieldType - 1];
2371 /* ------------------------- CBA_AnnotIterator ------------------------- */
2373 CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView,
2374 const CFX_ByteString& sType,
2375 const CFX_ByteString& sSubType)
2376 : m_pPageView(pPageView),
2378 m_sSubType(sSubType),
2379 m_nTabs(BAI_STRUCTURE) {
2380 ASSERT(m_pPageView != NULL);
2382 CPDF_Page* pPDFPage = m_pPageView->GetPDFPage();
2383 ASSERT(pPDFPage != NULL);
2384 ASSERT(pPDFPage->m_pFormDict != NULL);
2386 CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetString("Tabs");
2390 } else if (sTabs == "C") {
2391 m_nTabs = BAI_COLUMN;
2393 m_nTabs = BAI_STRUCTURE;
2399 CBA_AnnotIterator::~CBA_AnnotIterator() {
2400 m_Annots.RemoveAll();
2403 CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() {
2404 if (m_Annots.GetSize() > 0)
2410 CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() {
2411 if (m_Annots.GetSize() > 0)
2412 return m_Annots[m_Annots.GetSize() - 1];
2417 CPDFSDK_Annot* CBA_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) {
2418 for (int i = 0, sz = m_Annots.GetSize(); i < sz; ++i) {
2419 if (m_Annots[i] == pAnnot)
2420 return (i + 1 < sz) ? m_Annots[i + 1] : m_Annots[0];
2425 CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) {
2426 for (int i = 0, sz = m_Annots.GetSize(); i < sz; ++i) {
2427 if (m_Annots[i] == pAnnot)
2428 return (i - 1 >= 0) ? m_Annots[i - 1] : m_Annots[sz - 1];
2433 int CBA_AnnotIterator::CompareByLeft(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) {
2437 CPDF_Rect rcAnnot1 = GetAnnotRect(p1);
2438 CPDF_Rect rcAnnot2 = GetAnnotRect(p2);
2440 if (rcAnnot1.left < rcAnnot2.left)
2442 if (rcAnnot1.left > rcAnnot2.left)
2447 int CBA_AnnotIterator::CompareByTop(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) {
2451 CPDF_Rect rcAnnot1 = GetAnnotRect(p1);
2452 CPDF_Rect rcAnnot2 = GetAnnotRect(p2);
2454 if (rcAnnot1.top < rcAnnot2.top)
2456 if (rcAnnot1.top > rcAnnot2.top)
2461 void CBA_AnnotIterator::GenerateResults() {
2462 ASSERT(m_pPageView != NULL);
2465 case BAI_STRUCTURE: {
2466 for (int i = 0, sz = m_pPageView->CountAnnots(); i < sz; i++) {
2467 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2468 ASSERT(pAnnot != NULL);
2470 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
2471 m_Annots.Add(pAnnot);
2475 CPDFSDK_SortAnnots sa;
2478 for (int i = 0, sz = m_pPageView->CountAnnots(); i < sz; i++) {
2479 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2480 ASSERT(pAnnot != NULL);
2482 if (pAnnot->GetType() == m_sType &&
2483 pAnnot->GetSubType() == m_sSubType)
2488 if (sa.GetSize() > 0) {
2489 sa.Sort(CBA_AnnotIterator::CompareByLeft);
2492 while (sa.GetSize() > 0) {
2493 int nLeftTopIndex = -1;
2496 FX_FLOAT fTop = 0.0f;
2498 for (int i = sa.GetSize() - 1; i >= 0; i--) {
2499 CPDFSDK_Annot* pAnnot = sa.GetAt(i);
2500 ASSERT(pAnnot != NULL);
2502 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot);
2504 if (rcAnnot.top > fTop) {
2511 if (nLeftTopIndex >= 0) {
2512 CPDFSDK_Annot* pLeftTopAnnot = sa.GetAt(nLeftTopIndex);
2513 ASSERT(pLeftTopAnnot != NULL);
2515 CPDF_Rect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
2517 m_Annots.Add(pLeftTopAnnot);
2518 sa.RemoveAt(nLeftTopIndex);
2520 CFX_ArrayTemplate<int> aSelect;
2523 for (int i = 0, sz = sa.GetSize(); i < sz; i++) {
2524 CPDFSDK_Annot* pAnnot = sa.GetAt(i);
2525 ASSERT(pAnnot != NULL);
2527 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot);
2529 FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f;
2531 if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top)
2537 for (int i = 0, sz = aSelect.GetSize(); i < sz; i++) {
2538 m_Annots.Add(sa[aSelect[i]]);
2543 for (int i = aSelect.GetSize() - 1; i >= 0; i--) {
2544 sa.RemoveAt(aSelect[i]);
2548 aSelect.RemoveAll();
2554 CPDFSDK_SortAnnots sa;
2557 for (int i = 0, sz = m_pPageView->CountAnnots(); i < sz; i++) {
2558 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2559 ASSERT(pAnnot != NULL);
2561 if (pAnnot->GetType() == m_sType &&
2562 pAnnot->GetSubType() == m_sSubType)
2567 if (sa.GetSize() > 0) {
2568 sa.Sort(CBA_AnnotIterator::CompareByTop, FALSE);
2571 while (sa.GetSize() > 0) {
2572 int nLeftTopIndex = -1;
2575 FX_FLOAT fLeft = -1.0f;
2577 for (int i = sa.GetSize() - 1; i >= 0; i--) {
2578 CPDFSDK_Annot* pAnnot = sa.GetAt(i);
2579 ASSERT(pAnnot != NULL);
2581 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot);
2585 fLeft = rcAnnot.left;
2586 } else if (rcAnnot.left < fLeft) {
2588 fLeft = rcAnnot.left;
2593 if (nLeftTopIndex >= 0) {
2594 CPDFSDK_Annot* pLeftTopAnnot = sa.GetAt(nLeftTopIndex);
2595 ASSERT(pLeftTopAnnot != NULL);
2597 CPDF_Rect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
2599 m_Annots.Add(pLeftTopAnnot);
2600 sa.RemoveAt(nLeftTopIndex);
2602 CFX_ArrayTemplate<int> aSelect;
2605 for (int i = 0, sz = sa.GetSize(); i < sz; i++) {
2606 CPDFSDK_Annot* pAnnot = sa.GetAt(i);
2607 ASSERT(pAnnot != NULL);
2609 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot);
2611 FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f;
2613 if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right)
2619 for (int i = 0, sz = aSelect.GetSize(); i < sz; i++) {
2620 m_Annots.Add(sa[aSelect[i]]);
2625 for (int i = aSelect.GetSize() - 1; i >= 0; i--) {
2626 sa.RemoveAt(aSelect[i]);
2630 aSelect.RemoveAll();
2638 CPDF_Rect CBA_AnnotIterator::GetAnnotRect(CPDFSDK_Annot* pAnnot) {
2639 ASSERT(pAnnot != NULL);
2641 CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
2642 ASSERT(pPDFAnnot != NULL);
2645 pPDFAnnot->GetRect(rcAnnot);