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 "../../include/fpdfdoc/fpdf_doc.h"
8 CFX_WideString GetFullName(CPDF_Dictionary* pFieldDict);
9 void SaveCheckedFieldStatus(CPDF_FormField* pField, CFX_ByteArray& statusArray);
10 FX_BOOL PDF_FormField_IsUnison(CPDF_FormField *pField)
12 FX_BOOL bUnison = FALSE;
13 if (pField->GetType() == CPDF_FormField::CheckBox) {
16 FX_DWORD dwFlags = pField->GetFieldFlags();
17 bUnison = ((dwFlags & 0x2000000) != 0);
21 CPDF_FormField::CPDF_FormField(CPDF_InterForm* pForm, CPDF_Dictionary* pDict)
30 CPDF_FormField::~CPDF_FormField()
33 void CPDF_FormField::SyncFieldFlags()
35 CFX_ByteString type_name = FPDF_GetFieldAttr(m_pDict, "FT") ? FPDF_GetFieldAttr(m_pDict, "FT")->GetString() : CFX_ByteString();
36 FX_DWORD flags = FPDF_GetFieldAttr(m_pDict, "Ff")? FPDF_GetFieldAttr(m_pDict, "Ff")->GetInteger() : 0;
39 m_Flags |= FORMFIELD_READONLY;
42 m_Flags |= FORMFIELD_REQUIRED;
45 m_Flags |= FORMFIELD_NOEXPORT;
47 if (type_name == "Btn") {
51 m_Flags |= FORMRADIO_NOTOGGLEOFF;
53 if (flags & 0x2000000) {
54 m_Flags |= FORMRADIO_UNISON;
56 } else if (flags & 0x10000) {
61 } else if (type_name == "Tx") {
62 if (flags & 0x100000) {
64 } else if (flags & 0x2000000) {
69 m_Flags |= FORMTEXT_MULTILINE;
72 m_Flags |= FORMTEXT_PASSWORD;
74 if (flags & 0x800000) {
75 m_Flags |= FORMTEXT_NOSCROLL;
77 if (flags & 0x100000) {
78 m_Flags |= FORMTEXT_COMB;
82 } else if (type_name == "Ch") {
83 if (flags & 0x20000) {
85 if (flags & 0x40000) {
86 m_Flags |= FORMCOMBO_EDIT;
90 if (flags & 0x200000) {
91 m_Flags |= FORMLIST_MULTISELECT;
95 } else if (type_name == "Sig") {
99 CFX_WideString CPDF_FormField::GetFullName()
101 return ::GetFullName(m_pDict);
103 FX_BOOL CPDF_FormField::ResetField(FX_BOOL bNotify)
106 case CPDF_FormField::CheckBox:
107 case CPDF_FormField::RadioButton: {
108 CFX_ByteArray statusArray;
109 if (bNotify && m_pForm->m_pFormNotify != NULL) {
110 SaveCheckedFieldStatus(this, statusArray);
112 int iCount = CountControls();
114 if (PDF_FormField_IsUnison(this)) {
115 for(int i = 0; i < iCount; i++) {
116 CheckControl(i, GetControl(i)->IsDefaultChecked(), FALSE);
119 for (int i = 0; i < iCount; i ++) {
120 CPDF_FormControl* pControl = GetControl(i);
121 FX_BOOL bChecked = pControl->IsDefaultChecked();
122 CheckControl(i, bChecked, FALSE);
126 if (bNotify && m_pForm->m_pFormNotify != NULL) {
127 m_pForm->m_pFormNotify->AfterCheckedStatusChange(this, statusArray);
131 case CPDF_FormField::ComboBox: {
132 CFX_WideString csValue;
134 int iIndex = GetDefaultSelectedItem();
136 csValue = GetOptionLabel(iIndex);
138 if (bNotify && m_pForm->m_pFormNotify != NULL) {
139 int iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue);
144 SetItemSelection(iIndex, TRUE);
145 if (bNotify && m_pForm->m_pFormNotify != NULL) {
146 m_pForm->m_pFormNotify->AfterValueChange(this);
150 case CPDF_FormField::ListBox: {
151 CFX_WideString csValue;
153 int iIndex = GetDefaultSelectedItem();
155 csValue = GetOptionLabel(iIndex);
157 if (bNotify && m_pForm->m_pFormNotify != NULL) {
158 int iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue);
163 SetItemSelection(iIndex, TRUE);
164 if (bNotify && m_pForm->m_pFormNotify != NULL) {
165 m_pForm->m_pFormNotify->AfterSelectionChange(this);
169 case CPDF_FormField::Text:
170 case CPDF_FormField::RichText:
171 case CPDF_FormField::File:
173 CPDF_Object* pDV = FPDF_GetFieldAttr(m_pDict, "DV");
174 CFX_WideString csDValue;
176 csDValue = pDV->GetUnicodeText();
178 CPDF_Object* pV = FPDF_GetFieldAttr(m_pDict, "V");
179 CFX_WideString csValue;
181 csValue = pV->GetUnicodeText();
183 CPDF_Object* pRV = FPDF_GetFieldAttr(m_pDict, "RV");
184 if (!pRV && (csDValue == csValue)) {
187 if (bNotify && m_pForm->m_pFormNotify != NULL) {
188 int iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csDValue);
194 m_pDict->RemoveAt("V");
195 m_pDict->RemoveAt("RV");
197 CPDF_Object* pClone = pDV->Clone();
198 if (pClone == NULL) {
201 m_pDict->SetAt("V", pClone);
203 CPDF_Object* pCloneR = pDV->Clone();
204 m_pDict->SetAt("RV", pCloneR);
207 if (bNotify && m_pForm->m_pFormNotify != NULL) {
208 m_pForm->m_pFormNotify->AfterValueChange(this);
210 m_pForm->m_bUpdated = TRUE;
216 int CPDF_FormField::GetControlIndex(const CPDF_FormControl* pControl)
218 if (pControl == NULL) {
221 int iCount = m_ControlList.GetSize();
222 for (int i = 0; i < iCount; i ++) {
223 CPDF_FormControl* pFind = (CPDF_FormControl*)m_ControlList.GetAt(i);
224 if (pFind == pControl) {
230 int CPDF_FormField::GetFieldType()
234 return FIELDTYPE_PUSHBUTTON;
236 return FIELDTYPE_CHECKBOX;
238 return FIELDTYPE_RADIOBUTTON;
240 return FIELDTYPE_COMBOBOX;
242 return FIELDTYPE_LISTBOX;
246 return FIELDTYPE_TEXTFIELD;
248 return FIELDTYPE_SIGNATURE;
252 return FIELDTYPE_UNKNOWN;
254 CPDF_AAction CPDF_FormField::GetAdditionalAction()
256 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "AA");
260 return pObj->GetDict();
262 CFX_WideString CPDF_FormField::GetAlternateName()
264 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "TU");
268 return pObj->GetUnicodeText();
270 CFX_WideString CPDF_FormField::GetMappingName()
272 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "TM");
276 return pObj->GetUnicodeText();
278 FX_DWORD CPDF_FormField::GetFieldFlags()
280 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "Ff");
284 return pObj->GetInteger();
286 CFX_ByteString CPDF_FormField::GetDefaultStyle()
288 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "DS");
292 return pObj->GetString();
294 CFX_WideString CPDF_FormField::GetRichTextString()
296 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "RV");
300 return pObj->GetUnicodeText();
302 CFX_WideString CPDF_FormField::GetValue(FX_BOOL bDefault)
304 if (GetType() == CheckBox || GetType() == RadioButton) {
305 return GetCheckValue(bDefault);
307 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, bDefault ? "DV" : "V");
308 if (pValue == NULL) {
310 if (m_Type == RichText) {
311 pValue = FPDF_GetFieldAttr(m_pDict, "V");
313 if (pValue == NULL && m_Type != Text) {
314 pValue = FPDF_GetFieldAttr(m_pDict, "DV");
317 if (pValue == NULL) {
318 return CFX_WideString();
321 switch (pValue->GetType()) {
324 return pValue->GetUnicodeText();
326 pValue = ((CPDF_Array*)pValue)->GetElementValue(0);
328 return pValue->GetUnicodeText();
332 return CFX_WideString();
334 CFX_WideString CPDF_FormField::GetValue()
336 return GetValue(FALSE);
338 CFX_WideString CPDF_FormField::GetDefaultValue()
340 return GetValue(TRUE);
342 FX_BOOL CPDF_FormField::SetValue(const CFX_WideString& value, FX_BOOL bDefault, FX_BOOL bNotify)
347 SetCheckValue(value, bDefault, bNotify);
354 CFX_WideString csValue = value;
355 if (bNotify && m_pForm->m_pFormNotify != NULL) {
356 int iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue);
361 int iIndex = FindOptionValue(csValue);
363 CFX_ByteString bsEncodeText = PDF_EncodeText(csValue);
364 m_pDict->SetAtString(bDefault ? "DV" : "V", bsEncodeText);
365 if (m_Type == RichText && !bDefault) {
366 m_pDict->SetAtString("RV", bsEncodeText);
368 m_pDict->RemoveAt("I");
370 m_pDict->SetAtString(bDefault ? "DV" : "V", PDF_EncodeText(csValue));
374 SetItemSelection(iIndex, TRUE);
377 if (bNotify && m_pForm->m_pFormNotify != NULL) {
378 m_pForm->m_pFormNotify->AfterValueChange(this);
380 m_pForm->m_bUpdated = TRUE;
384 int iIndex = FindOptionValue(value);
388 if (bDefault && iIndex == GetDefaultSelectedItem()) {
391 if (bNotify && m_pForm->m_pFormNotify != NULL) {
392 CFX_WideString csValue = value;
393 int iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue);
401 SetItemSelection(iIndex, TRUE);
403 if (bNotify && m_pForm->m_pFormNotify != NULL) {
404 m_pForm->m_pFormNotify->AfterSelectionChange(this);
406 m_pForm->m_bUpdated = TRUE;
412 if (CPDF_InterForm::m_bUpdateAP) {
417 FX_BOOL CPDF_FormField::SetValue(const CFX_WideString& value, FX_BOOL bNotify)
419 return SetValue(value, FALSE, bNotify);
421 int CPDF_FormField::GetMaxLen()
423 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "MaxLen");
425 int iCount = m_ControlList.GetSize();
426 for (int i = 0; i < iCount; i ++) {
427 CPDF_FormControl* pControl = (CPDF_FormControl*)m_ControlList.GetAt(i);
428 if (pControl == NULL) {
431 CPDF_Dictionary* pWidgetDict = pControl->m_pWidgetDict;
432 if (pWidgetDict->KeyExist("MaxLen")) {
433 return pWidgetDict->GetInteger("MaxLen");
438 return pObj->GetInteger();
440 int CPDF_FormField::CountSelectedItems()
442 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V");
443 if (pValue == NULL) {
444 pValue = FPDF_GetFieldAttr(m_pDict, "I");
445 if (pValue == NULL) {
449 if (pValue->GetType() == PDFOBJ_STRING) {
450 if (pValue->GetString().IsEmpty()) {
455 if (pValue->GetType() == PDFOBJ_NUMBER) {
456 if (pValue->GetString().IsEmpty()) {
461 if (pValue->GetType() != PDFOBJ_ARRAY) {
464 return ((CPDF_Array*)pValue)->GetCount();
466 int CPDF_FormField::GetSelectedIndex(int index)
468 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V");
469 if (pValue == NULL) {
470 pValue = FPDF_GetFieldAttr(m_pDict, "I");
471 if (pValue == NULL) {
475 CFX_WideString sel_value;
476 if (pValue->GetType() == PDFOBJ_STRING) {
480 sel_value = pValue->GetUnicodeText();
481 } else if (pValue->GetType() == PDFOBJ_NUMBER) {
482 return pValue->GetInteger();
484 if (pValue->GetType() != PDFOBJ_ARRAY) {
490 CPDF_Object* elementValue = ((CPDF_Array*)pValue)->GetElementValue(index);
491 sel_value = elementValue ? elementValue->GetUnicodeText() : CFX_WideString();
493 if (index < CountSelectedOptions()) {
494 int iOptIndex = GetSelectedOptionIndex(index);
495 CFX_WideString csOpt = GetOptionValue(iOptIndex);
496 if (csOpt == sel_value) {
500 int nOpts = CountOptions();
501 for (int i = 0; i < nOpts; i ++) {
502 if (sel_value == GetOptionValue(i)) {
508 FX_BOOL CPDF_FormField::ClearSelection(FX_BOOL bNotify)
510 if (bNotify && m_pForm->m_pFormNotify != NULL) {
512 CFX_WideString csValue;
513 int iIndex = GetSelectedIndex(0);
515 csValue = GetOptionLabel(iIndex);
517 if (GetType() == ListBox) {
518 iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue);
520 if (GetType() == ComboBox) {
521 iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue);
527 m_pDict->RemoveAt("V");
528 m_pDict->RemoveAt("I");
529 if (bNotify && m_pForm->m_pFormNotify != NULL) {
530 if (GetType() == ListBox) {
531 m_pForm->m_pFormNotify->AfterSelectionChange(this);
533 if (GetType() == ComboBox) {
534 m_pForm->m_pFormNotify->AfterValueChange(this);
537 if (CPDF_InterForm::m_bUpdateAP) {
540 m_pForm->m_bUpdated = TRUE;
543 FX_BOOL CPDF_FormField::IsItemSelected(int index)
545 ASSERT(GetType() == ComboBox || GetType() == ListBox);
546 if (index < 0 || index >= CountOptions()) {
549 if (IsOptionSelected(index)) {
552 CFX_WideString opt_value = GetOptionValue(index);
553 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V");
554 if (pValue == NULL) {
555 pValue = FPDF_GetFieldAttr(m_pDict, "I");
556 if (pValue == NULL) {
560 if (pValue->GetType() == PDFOBJ_STRING) {
561 if (pValue->GetUnicodeText() == opt_value) {
566 if (pValue->GetType() == PDFOBJ_NUMBER) {
567 if (pValue->GetString().IsEmpty()) {
570 if (pValue->GetInteger() == index) {
575 if (pValue->GetType() != PDFOBJ_ARRAY) {
578 CPDF_Array* pArray = (CPDF_Array*)pValue;
580 for (int j = 0; j < CountSelectedOptions(); j ++) {
581 if (GetSelectedOptionIndex(j) == index) {
586 for (FX_DWORD i = 0; i < pArray->GetCount(); i ++)
587 if (pArray->GetElementValue(i)->GetUnicodeText() == opt_value && (int)i == iPos) {
592 FX_BOOL CPDF_FormField::SetItemSelection(int index, FX_BOOL bSelected, FX_BOOL bNotify)
594 ASSERT(GetType() == ComboBox || GetType() == ListBox);
595 if (index < 0 || index >= CountOptions()) {
598 CFX_WideString opt_value = GetOptionValue(index);
599 if (bNotify && m_pForm->m_pFormNotify != NULL) {
601 if (GetType() == ListBox) {
602 iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, opt_value);
604 if (GetType() == ComboBox) {
605 iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, opt_value);
612 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V");
613 if (pValue != NULL) {
614 if (m_Type == ListBox) {
615 SelectOption(index, FALSE);
616 if (pValue->GetType() == PDFOBJ_STRING) {
617 if (pValue->GetUnicodeText() == opt_value) {
618 m_pDict->RemoveAt("V");
620 } else if (pValue->GetType() == PDFOBJ_ARRAY) {
621 CPDF_Array* pArray = CPDF_Array::Create();
622 if (pArray == NULL) {
625 int iCount = CountOptions();
626 for (int i = 0; i < iCount; i ++) {
628 if (IsItemSelected(i)) {
629 opt_value = GetOptionValue(i);
630 pArray->AddString(PDF_EncodeText(opt_value));
634 if (pArray->GetCount() < 1) {
637 m_pDict->SetAt("V", pArray);
640 } else if (m_Type == ComboBox) {
641 m_pDict->RemoveAt("V");
642 m_pDict->RemoveAt("I");
646 if (m_Type == ListBox) {
647 SelectOption(index, TRUE);
648 if (!(m_Flags & FORMLIST_MULTISELECT)) {
649 m_pDict->SetAtString("V", PDF_EncodeText(opt_value, opt_value.GetLength()));
651 CPDF_Array* pArray = CPDF_Array::Create();
652 if (pArray == NULL) {
656 int iCount = CountOptions();
657 for (int i = 0; i < iCount; i ++) {
659 bSelected = IsItemSelected(i);
664 opt_value = GetOptionValue(i);
665 pArray->AddString(PDF_EncodeText(opt_value));
668 m_pDict->SetAt("V", pArray);
670 } else if (m_Type == ComboBox) {
671 m_pDict->SetAtString("V", PDF_EncodeText(opt_value, opt_value.GetLength()));
672 CPDF_Array* pI = CPDF_Array::Create();
676 pI->AddInteger(index);
677 m_pDict->SetAt("I", pI);
680 if (bNotify && m_pForm->m_pFormNotify != NULL) {
681 if (GetType() == ListBox) {
682 m_pForm->m_pFormNotify->AfterSelectionChange(this);
684 if (GetType() == ComboBox) {
685 m_pForm->m_pFormNotify->AfterValueChange(this);
688 if (CPDF_InterForm::m_bUpdateAP) {
691 m_pForm->m_bUpdated = TRUE;
694 FX_BOOL CPDF_FormField::IsItemDefaultSelected(int index)
696 ASSERT(GetType() == ComboBox || GetType() == ListBox);
697 if (index < 0 || index >= CountOptions()) {
700 int iDVIndex = GetDefaultSelectedItem();
704 return (iDVIndex == index);
706 int CPDF_FormField::GetDefaultSelectedItem()
708 ASSERT(GetType() == ComboBox || GetType() == ListBox);
709 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "DV");
710 if (pValue == NULL) {
713 CFX_WideString csDV = pValue->GetUnicodeText();
714 if (csDV.IsEmpty()) {
717 int iCount = CountOptions();
718 for (int i = 0; i < iCount; i ++) {
719 if (csDV == GetOptionValue(i)) {
725 void CPDF_FormField::UpdateAP(CPDF_FormControl* pControl)
727 if (m_Type == PushButton) {
730 if (m_Type == RadioButton || m_Type == CheckBox) {
733 if (!m_pForm->m_bGenerateAP) {
736 for (int i = 0; i < CountControls(); i ++) {
737 CPDF_FormControl* pControl = GetControl(i);
738 FPDF_GenerateAP(m_pForm->m_pDocument, pControl->m_pWidgetDict);
741 int CPDF_FormField::CountOptions()
743 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "Opt");
744 if (pValue == NULL || pValue->GetType() != PDFOBJ_ARRAY) {
747 return ((CPDF_Array*)pValue)->GetCount();
749 CFX_WideString CPDF_FormField::GetOptionText(int index, int sub_index)
751 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "Opt");
752 if (pValue == NULL || pValue->GetType() != PDFOBJ_ARRAY) {
753 return CFX_WideString();
755 CPDF_Object* pOption = ((CPDF_Array*)pValue)->GetElementValue(index);
756 if (pOption == NULL) {
757 return CFX_WideString();
759 if (pOption->GetType() == PDFOBJ_ARRAY) {
760 pOption = ((CPDF_Array*)pOption)->GetElementValue(sub_index);
762 if (pOption == NULL || pOption->GetType() != PDFOBJ_STRING) {
763 return CFX_WideString();
765 return ((CPDF_String*)pOption)->GetUnicodeText();
767 CFX_WideString CPDF_FormField::GetOptionLabel(int index)
769 return GetOptionText(index, 1);
771 CFX_WideString CPDF_FormField::GetOptionValue(int index)
773 return GetOptionText(index, 0);
775 int CPDF_FormField::FindOption(CFX_WideString csOptLabel)
777 int iCount = CountOptions();
778 for (int i = 0; i < iCount; i ++) {
779 CFX_WideString csValue = GetOptionValue(i);
780 if (csValue == csOptLabel) {
786 int CPDF_FormField::FindOptionValue(FX_LPCWSTR csOptValue, int iStartIndex)
788 if (iStartIndex < 0) {
791 int iCount = CountOptions();
792 for (; iStartIndex < iCount; iStartIndex ++) {
793 CFX_WideString csValue = GetOptionValue(iStartIndex);
794 if (csValue == csOptValue) {
800 int CPDF_FormField::InsertOption(CFX_WideString csOptLabel, int index, FX_BOOL bNotify)
\r
802 if (csOptLabel.IsEmpty()) return -1;
\r
804 if (bNotify && m_pForm->m_pFormNotify != NULL)
\r
807 if (GetType() == ListBox) iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csOptLabel);
\r
808 if (GetType() == ComboBox) iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csOptLabel);
\r
809 if (iRet < 0) return -1;
\r
812 CFX_ByteString csStr = PDF_EncodeText(csOptLabel, csOptLabel.GetLength());
\r
813 CPDF_Array* pOpt = NULL;
\r
814 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "Opt");
\r
815 if (pValue == NULL || pValue->GetType() != PDFOBJ_ARRAY)
\r
817 pOpt = CPDF_Array::Create();
\r
818 if (pOpt == NULL) return -1;
\r
819 m_pDict->SetAt("Opt", pOpt);
\r
822 pOpt = (CPDF_Array*)pValue;
\r
823 int iCount = (int)pOpt->GetCount();
\r
824 if (index < 0 || index >= iCount)
\r
826 pOpt->AddString(csStr);
\r
830 CPDF_String* pString = CPDF_String::Create(csStr);
\r
831 if (pString == NULL) return -1;
\r
832 pOpt->InsertAt(index, pString);
\r
835 if (bNotify && m_pForm->m_pFormNotify != NULL)
\r
837 if (GetType() == ListBox) m_pForm->m_pFormNotify->AfterSelectionChange(this);
\r
838 if (GetType() == ComboBox) m_pForm->m_pFormNotify->AfterValueChange(this);
\r
840 m_pForm->m_bUpdated = TRUE;
\r
843 FX_BOOL CPDF_FormField::ClearOptions(FX_BOOL bNotify)
\r
845 if (bNotify && m_pForm->m_pFormNotify != NULL)
\r
848 CFX_WideString csValue;
\r
849 int iIndex = GetSelectedIndex(0);
\r
850 if (iIndex >= 0) csValue = GetOptionLabel(iIndex);
\r
851 if (GetType() == ListBox) iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue);
\r
852 if (GetType() == ComboBox) iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue);
\r
853 if (iRet < 0) return FALSE;
\r
856 m_pDict->RemoveAt("Opt");
\r
857 m_pDict->RemoveAt("V");
\r
858 m_pDict->RemoveAt("DV");
\r
859 m_pDict->RemoveAt("I");
\r
860 m_pDict->RemoveAt("TI");
\r
862 if (bNotify && m_pForm->m_pFormNotify != NULL)
\r
864 if (GetType() == ListBox) m_pForm->m_pFormNotify->AfterSelectionChange(this);
\r
865 if (GetType() == ComboBox) m_pForm->m_pFormNotify->AfterValueChange(this);
\r
868 m_pForm->m_bUpdated = TRUE;
\r
871 FX_BOOL CPDF_FormField::CheckControl(int iControlIndex, FX_BOOL bChecked, FX_BOOL bNotify)
873 ASSERT(GetType() == CheckBox || GetType() == RadioButton);
874 CPDF_FormControl* pControl = GetControl(iControlIndex);
875 if (pControl == NULL) {
878 if (!bChecked && pControl->IsChecked() == bChecked) {
881 CFX_ByteArray statusArray;
882 if (bNotify && m_pForm->m_pFormNotify != NULL) {
883 SaveCheckedFieldStatus(this, statusArray);
885 CFX_WideString csWExport = pControl->GetExportValue();
886 CFX_ByteString csBExport = PDF_EncodeText(csWExport);
887 int iCount = CountControls();
888 FX_BOOL bUnison = PDF_FormField_IsUnison(this);
889 for (int i = 0; i < iCount; i ++) {
890 CPDF_FormControl* pCtrl = GetControl(i);
892 CFX_WideString csEValue = pCtrl->GetExportValue();
893 if (csEValue == csWExport) {
894 if (pCtrl->GetOnStateName() == pControl->GetOnStateName()) {
895 pCtrl->CheckControl(bChecked);
896 } else if (bChecked) {
897 pCtrl->CheckControl(FALSE);
899 } else if (bChecked) {
900 pCtrl->CheckControl(FALSE);
903 if (i == iControlIndex) {
904 pCtrl->CheckControl(bChecked);
905 } else if (bChecked) {
906 pCtrl->CheckControl(FALSE);
910 CPDF_Object* pOpt = FPDF_GetFieldAttr(m_pDict, "Opt");
911 if (pOpt == NULL || pOpt->GetType() != PDFOBJ_ARRAY) {
913 m_pDict->SetAtName("V", csBExport);
916 CPDF_Object* pV = FPDF_GetFieldAttr(m_pDict, "V");
918 csV = pV->GetString();
920 if (csV == csBExport) {
921 m_pDict->SetAtName("V", "Off");
924 } else if (bChecked) {
925 CFX_ByteString csIndex;
926 csIndex.Format("%d", iControlIndex);
927 m_pDict->SetAtName("V", csIndex);
929 if (bNotify && m_pForm->m_pFormNotify != NULL) {
930 m_pForm->m_pFormNotify->AfterCheckedStatusChange(this, statusArray);
932 m_pForm->m_bUpdated = TRUE;
935 CFX_WideString CPDF_FormField::GetCheckValue(FX_BOOL bDefault)
937 ASSERT(GetType() == CheckBox || GetType() == RadioButton);
938 CFX_WideString csExport = L"Off";
940 int iCount = CountControls();
941 for (int i = 0; i < iCount; i ++) {
942 CPDF_FormControl* pControl = GetControl(i);
944 bChecked = pControl->IsDefaultChecked();
946 bChecked = pControl->IsChecked();
949 csExport = pControl->GetExportValue();
955 FX_BOOL CPDF_FormField::SetCheckValue(const CFX_WideString& value, FX_BOOL bDefault, FX_BOOL bNotify)
957 ASSERT(GetType() == CheckBox || GetType() == RadioButton);
958 CFX_ByteArray statusArray;
959 if (bNotify && m_pForm->m_pFormNotify != NULL) {
960 SaveCheckedFieldStatus(this, statusArray);
962 int iCount = CountControls();
963 for (int i = 0; i < iCount; i ++) {
964 CPDF_FormControl* pControl = GetControl(i);
965 CFX_WideString csExport = pControl->GetExportValue();
966 if (csExport == value) {
969 CheckControl(GetControlIndex(pControl), TRUE);
975 CheckControl(GetControlIndex(pControl), FALSE);
979 if (bNotify && m_pForm->m_pFormNotify != NULL) {
980 m_pForm->m_pFormNotify->AfterCheckedStatusChange(this, statusArray);
982 m_pForm->m_bUpdated = TRUE;
985 int CPDF_FormField::GetTopVisibleIndex()
987 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "TI");
991 return pObj->GetInteger();
993 int CPDF_FormField::CountSelectedOptions()
995 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "I");
999 CPDF_Array* pArray = pObj->GetArray();
1000 if (pArray == NULL) {
1003 return (int)pArray->GetCount();
1005 int CPDF_FormField::GetSelectedOptionIndex(int index)
1007 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "I");
1011 CPDF_Array* pArray = pObj->GetArray();
1012 if (pArray == NULL) {
1015 int iCount = (int)pArray->GetCount();
1016 if (iCount > 0 && index < iCount) {
1017 return pArray->GetInteger(index);
1021 FX_BOOL CPDF_FormField::IsOptionSelected(int iOptIndex)
1023 CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "I");
1027 CPDF_Array* pArray = pObj->GetArray();
1028 if (pArray == NULL) {
1031 int iCount = (int)pArray->GetCount();
1032 for (int i = 0; i < iCount; i ++) {
1033 if (pArray->GetInteger(i) == iOptIndex) {
1039 FX_BOOL CPDF_FormField::SelectOption(int iOptIndex, FX_BOOL bSelected, FX_BOOL bNotify)
1041 CPDF_Array* pArray = m_pDict->GetArray("I");
1042 if (pArray == NULL) {
1046 pArray = CPDF_Array::Create();
1047 if (pArray == NULL) {
1050 m_pDict->SetAt("I", pArray);
1052 FX_BOOL bReturn = FALSE;
1053 for (int i = 0; i < (int)pArray->GetCount(); i ++) {
1054 int iFind = pArray->GetInteger(i);
1055 if (iFind == iOptIndex) {
1059 if (bNotify && m_pForm->m_pFormNotify != NULL) {
1061 CFX_WideString csValue = GetOptionLabel(iOptIndex);
1062 if (GetType() == ListBox) {
1063 iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue);
1065 if (GetType() == ComboBox) {
1066 iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue);
1072 pArray->RemoveAt(i);
1075 } else if (iFind > iOptIndex) {
1079 if (bNotify && m_pForm->m_pFormNotify != NULL) {
1081 CFX_WideString csValue = GetOptionLabel(iOptIndex);
1082 if (GetType() == ListBox) {
1083 iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue);
1085 if (GetType() == ComboBox) {
1086 iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue);
1092 CPDF_Number* pNum = CPDF_Number::Create(iOptIndex);
1096 pArray->InsertAt(i, pNum);
1103 pArray->AddInteger(iOptIndex);
1105 if (pArray->GetCount() == 0) {
1106 m_pDict->RemoveAt("I");
1109 if (bNotify && m_pForm->m_pFormNotify != NULL) {
1110 if (GetType() == ListBox) {
1111 m_pForm->m_pFormNotify->AfterSelectionChange(this);
1113 if (GetType() == ComboBox) {
1114 m_pForm->m_pFormNotify->AfterValueChange(this);
1117 m_pForm->m_bUpdated = TRUE;
1120 FX_BOOL CPDF_FormField::ClearSelectedOptions(FX_BOOL bNotify)
1122 if (bNotify && m_pForm->m_pFormNotify != NULL) {
1124 CFX_WideString csValue;
1125 int iIndex = GetSelectedIndex(0);
1127 csValue = GetOptionLabel(iIndex);
1129 if (GetType() == ListBox) {
1130 iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue);
1132 if (GetType() == ComboBox) {
1133 iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue);
1139 m_pDict->RemoveAt("I");
1140 if (bNotify && m_pForm->m_pFormNotify != NULL) {
1141 if (GetType() == ListBox) {
1142 m_pForm->m_pFormNotify->AfterSelectionChange(this);
1144 if (GetType() == ComboBox) {
1145 m_pForm->m_pFormNotify->AfterValueChange(this);
1148 m_pForm->m_bUpdated = TRUE;
1151 void CPDF_FormField::LoadDA()
1154 if (CPDF_Object* pObj_t = FPDF_GetFieldAttr(m_pDict, "DA")) {
1155 DA = pObj_t->GetString();
1157 if (DA.IsEmpty() && m_pForm->m_pFormDict) {
1158 DA = m_pForm->m_pFormDict->GetString("DA");
1163 CPDF_SimpleParser syntax(DA);
1164 syntax.FindTagParam("Tf", 2);
1165 CFX_ByteString font_name = syntax.GetWord();
1166 CPDF_Dictionary* pFontDict = NULL;
1167 if (m_pForm->m_pFormDict && m_pForm->m_pFormDict->GetDict("DR") &&
1168 m_pForm->m_pFormDict->GetDict("DR")->GetDict("Font") )
1169 pFontDict = m_pForm->m_pFormDict->GetDict("DR")->GetDict("Font")->GetDict(font_name);
1171 if (pFontDict == NULL) {
1174 m_pFont = m_pForm->m_pDocument->LoadFont(pFontDict);
1175 m_FontSize = FX_atof(syntax.GetWord());