OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fpdfdoc/cpdf_formfield.h" | 7 #include "core/fpdfdoc/cpdf_formfield.h" |
8 | 8 |
| 9 #include <memory> |
9 #include <set> | 10 #include <set> |
| 11 #include <utility> |
10 | 12 |
11 #include "core/fpdfapi/parser/cfdf_document.h" | 13 #include "core/fpdfapi/parser/cfdf_document.h" |
12 #include "core/fpdfapi/parser/cpdf_array.h" | 14 #include "core/fpdfapi/parser/cpdf_array.h" |
13 #include "core/fpdfapi/parser/cpdf_document.h" | 15 #include "core/fpdfapi/parser/cpdf_document.h" |
| 16 #include "core/fpdfapi/parser/cpdf_name.h" |
14 #include "core/fpdfapi/parser/cpdf_number.h" | 17 #include "core/fpdfapi/parser/cpdf_number.h" |
15 #include "core/fpdfapi/parser/cpdf_simple_parser.h" | 18 #include "core/fpdfapi/parser/cpdf_simple_parser.h" |
16 #include "core/fpdfapi/parser/cpdf_string.h" | 19 #include "core/fpdfapi/parser/cpdf_string.h" |
17 #include "core/fpdfapi/parser/fpdf_parser_decode.h" | 20 #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
18 #include "core/fpdfdoc/cpdf_formcontrol.h" | 21 #include "core/fpdfdoc/cpdf_formcontrol.h" |
19 #include "core/fpdfdoc/cpdf_interform.h" | 22 #include "core/fpdfdoc/cpdf_interform.h" |
20 #include "core/fpdfdoc/cpvt_generateap.h" | 23 #include "core/fpdfdoc/cpvt_generateap.h" |
21 #include "third_party/base/stl_util.h" | 24 #include "third_party/base/stl_util.h" |
22 | 25 |
23 namespace { | 26 namespace { |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 return false; | 219 return false; |
217 | 220 |
218 if (bNotify && !NotifyBeforeValueChange(csDValue)) | 221 if (bNotify && !NotifyBeforeValueChange(csDValue)) |
219 return false; | 222 return false; |
220 | 223 |
221 if (pDV) { | 224 if (pDV) { |
222 std::unique_ptr<CPDF_Object> pClone = pDV->Clone(); | 225 std::unique_ptr<CPDF_Object> pClone = pDV->Clone(); |
223 if (!pClone) | 226 if (!pClone) |
224 return false; | 227 return false; |
225 | 228 |
226 m_pDict->SetFor("V", pClone.release()); | 229 m_pDict->SetFor("V", std::move(pClone)); |
227 if (pRV) { | 230 if (pRV) |
228 std::unique_ptr<CPDF_Object> pCloneR = pDV->Clone(); | 231 m_pDict->SetFor("RV", pDV->Clone()); |
229 m_pDict->SetFor("RV", pCloneR.release()); | |
230 } | |
231 } else { | 232 } else { |
232 m_pDict->RemoveFor("V"); | 233 m_pDict->RemoveFor("V"); |
233 m_pDict->RemoveFor("RV"); | 234 m_pDict->RemoveFor("RV"); |
234 } | 235 } |
235 if (bNotify) | 236 if (bNotify) |
236 NotifyAfterValueChange(); | 237 NotifyAfterValueChange(); |
237 break; | 238 break; |
238 } | 239 } |
239 } | 240 } |
240 return true; | 241 return true; |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 return true; | 355 return true; |
355 } | 356 } |
356 case File: | 357 case File: |
357 case RichText: | 358 case RichText: |
358 case Text: | 359 case Text: |
359 case ComboBox: { | 360 case ComboBox: { |
360 CFX_WideString csValue = value; | 361 CFX_WideString csValue = value; |
361 if (bNotify && !NotifyBeforeValueChange(csValue)) | 362 if (bNotify && !NotifyBeforeValueChange(csValue)) |
362 return false; | 363 return false; |
363 | 364 |
| 365 CFX_ByteString key(bDefault ? "DV" : "V"); |
364 int iIndex = FindOptionValue(csValue); | 366 int iIndex = FindOptionValue(csValue); |
365 if (iIndex < 0) { | 367 if (iIndex < 0) { |
366 CFX_ByteString bsEncodeText = PDF_EncodeText(csValue); | 368 CFX_ByteString bsEncodeText = PDF_EncodeText(csValue); |
367 m_pDict->SetStringFor(bDefault ? "DV" : "V", bsEncodeText); | 369 m_pDict->SetNewFor<CPDF_String>(key, bsEncodeText, false); |
368 if (m_Type == RichText && !bDefault) | 370 if (m_Type == RichText && !bDefault) |
369 m_pDict->SetStringFor("RV", bsEncodeText); | 371 m_pDict->SetNewFor<CPDF_String>("RV", bsEncodeText, false); |
370 m_pDict->RemoveFor("I"); | 372 m_pDict->RemoveFor("I"); |
371 } else { | 373 } else { |
372 m_pDict->SetStringFor(bDefault ? "DV" : "V", PDF_EncodeText(csValue)); | 374 m_pDict->SetNewFor<CPDF_String>(key, PDF_EncodeText(csValue), false); |
373 if (!bDefault) { | 375 if (!bDefault) { |
374 ClearSelection(); | 376 ClearSelection(); |
375 SetItemSelection(iIndex, true); | 377 SetItemSelection(iIndex, true); |
376 } | 378 } |
377 } | 379 } |
378 if (bNotify) | 380 if (bNotify) |
379 NotifyAfterValueChange(); | 381 NotifyAfterValueChange(); |
380 break; | 382 break; |
381 } | 383 } |
382 case ListBox: { | 384 case ListBox: { |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 return false; | 544 return false; |
543 | 545 |
544 CFX_WideString opt_value = GetOptionValue(index); | 546 CFX_WideString opt_value = GetOptionValue(index); |
545 if (bNotify && !NotifyListOrComboBoxBeforeChange(opt_value)) | 547 if (bNotify && !NotifyListOrComboBoxBeforeChange(opt_value)) |
546 return false; | 548 return false; |
547 | 549 |
548 if (bSelected) { | 550 if (bSelected) { |
549 if (GetType() == ListBox) { | 551 if (GetType() == ListBox) { |
550 SelectOption(index, true); | 552 SelectOption(index, true); |
551 if (!(m_Flags & kFormListMultiSelect)) { | 553 if (!(m_Flags & kFormListMultiSelect)) { |
552 m_pDict->SetStringFor("V", PDF_EncodeText(opt_value)); | 554 m_pDict->SetNewFor<CPDF_String>("V", PDF_EncodeText(opt_value), false); |
553 } else { | 555 } else { |
554 CPDF_Array* pArray = new CPDF_Array; | 556 CPDF_Array* pArray = m_pDict->SetNewFor<CPDF_Array>("V"); |
555 for (int i = 0; i < CountOptions(); i++) { | 557 for (int i = 0; i < CountOptions(); i++) { |
556 if (i == index || IsItemSelected(i)) { | 558 if (i == index || IsItemSelected(i)) { |
557 opt_value = GetOptionValue(i); | 559 opt_value = GetOptionValue(i); |
558 pArray->AddNew<CPDF_String>(PDF_EncodeText(opt_value), false); | 560 pArray->AddNew<CPDF_String>(PDF_EncodeText(opt_value), false); |
559 } | 561 } |
560 } | 562 } |
561 m_pDict->SetFor("V", pArray); | |
562 } | 563 } |
563 } else { | 564 } else { |
564 m_pDict->SetStringFor("V", PDF_EncodeText(opt_value)); | 565 m_pDict->SetNewFor<CPDF_String>("V", PDF_EncodeText(opt_value), false); |
565 CPDF_Array* pI = new CPDF_Array; | 566 CPDF_Array* pI = m_pDict->SetNewFor<CPDF_Array>("I"); |
566 pI->AddNew<CPDF_Number>(index); | 567 pI->AddNew<CPDF_Number>(index); |
567 m_pDict->SetFor("I", pI); | |
568 } | 568 } |
569 } else { | 569 } else { |
570 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V"); | 570 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V"); |
571 if (pValue) { | 571 if (pValue) { |
572 if (GetType() == ListBox) { | 572 if (GetType() == ListBox) { |
573 SelectOption(index, false); | 573 SelectOption(index, false); |
574 if (pValue->IsString()) { | 574 if (pValue->IsString()) { |
575 if (pValue->GetUnicodeText() == opt_value) | 575 if (pValue->GetUnicodeText() == opt_value) |
576 m_pDict->RemoveFor("V"); | 576 m_pDict->RemoveFor("V"); |
577 } else if (pValue->IsArray()) { | 577 } else if (pValue->IsArray()) { |
578 std::unique_ptr<CPDF_Array> pArray(new CPDF_Array); | 578 std::unique_ptr<CPDF_Array> pArray(new CPDF_Array); |
579 for (int i = 0; i < CountOptions(); i++) { | 579 for (int i = 0; i < CountOptions(); i++) { |
580 if (i != index && IsItemSelected(i)) { | 580 if (i != index && IsItemSelected(i)) { |
581 opt_value = GetOptionValue(i); | 581 opt_value = GetOptionValue(i); |
582 pArray->AddNew<CPDF_String>(PDF_EncodeText(opt_value), false); | 582 pArray->AddNew<CPDF_String>(PDF_EncodeText(opt_value), false); |
583 } | 583 } |
584 } | 584 } |
585 if (pArray->GetCount() > 0) | 585 if (pArray->GetCount() > 0) |
586 m_pDict->SetFor("V", pArray.release()); // std::move someday | 586 m_pDict->SetFor("V", std::move(pArray)); |
587 } | 587 } |
588 } else { | 588 } else { |
589 m_pDict->RemoveFor("V"); | 589 m_pDict->RemoveFor("V"); |
590 m_pDict->RemoveFor("I"); | 590 m_pDict->RemoveFor("I"); |
591 } | 591 } |
592 } | 592 } |
593 } | 593 } |
594 if (bNotify) | 594 if (bNotify) |
595 NotifyListOrComboBoxAfterChange(); | 595 NotifyListOrComboBoxAfterChange(); |
596 return true; | 596 return true; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 int index, | 668 int index, |
669 bool bNotify) { | 669 bool bNotify) { |
670 if (csOptLabel.IsEmpty()) | 670 if (csOptLabel.IsEmpty()) |
671 return -1; | 671 return -1; |
672 | 672 |
673 if (bNotify && !NotifyListOrComboBoxBeforeChange(csOptLabel)) | 673 if (bNotify && !NotifyListOrComboBoxBeforeChange(csOptLabel)) |
674 return -1; | 674 return -1; |
675 | 675 |
676 CFX_ByteString csStr = | 676 CFX_ByteString csStr = |
677 PDF_EncodeText(csOptLabel.c_str(), csOptLabel.GetLength()); | 677 PDF_EncodeText(csOptLabel.c_str(), csOptLabel.GetLength()); |
678 CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "Opt"); | 678 CPDF_Array* pOpt = ToArray(FPDF_GetFieldAttr(m_pDict, "Opt")); |
679 CPDF_Array* pOpt = ToArray(pValue); | 679 if (!pOpt) |
680 if (!pOpt) { | 680 pOpt = m_pDict->SetNewFor<CPDF_Array>("Opt"); |
681 pOpt = new CPDF_Array; | |
682 m_pDict->SetFor("Opt", pOpt); | |
683 } | |
684 | 681 |
685 int iCount = pdfium::base::checked_cast<int>(pOpt->GetCount()); | 682 int iCount = pdfium::base::checked_cast<int>(pOpt->GetCount()); |
686 if (index >= iCount) { | 683 if (index >= iCount) { |
687 pOpt->AddNew<CPDF_String>(csStr, false); | 684 pOpt->AddNew<CPDF_String>(csStr, false); |
688 index = iCount; | 685 index = iCount; |
689 } else { | 686 } else { |
690 pOpt->InsertNewAt<CPDF_String>(index, csStr, false); | 687 pOpt->InsertNewAt<CPDF_String>(index, csStr, false); |
691 } | 688 } |
692 | 689 |
693 if (bNotify) | 690 if (bNotify) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 if (i == iControlIndex) | 745 if (i == iControlIndex) |
749 pCtrl->CheckControl(bChecked); | 746 pCtrl->CheckControl(bChecked); |
750 else if (bChecked) | 747 else if (bChecked) |
751 pCtrl->CheckControl(false); | 748 pCtrl->CheckControl(false); |
752 } | 749 } |
753 } | 750 } |
754 | 751 |
755 CPDF_Object* pOpt = FPDF_GetFieldAttr(m_pDict, "Opt"); | 752 CPDF_Object* pOpt = FPDF_GetFieldAttr(m_pDict, "Opt"); |
756 if (!ToArray(pOpt)) { | 753 if (!ToArray(pOpt)) { |
757 if (bChecked) { | 754 if (bChecked) { |
758 m_pDict->SetNameFor("V", csBExport); | 755 m_pDict->SetNewFor<CPDF_Name>("V", csBExport); |
759 } else { | 756 } else { |
760 CFX_ByteString csV; | 757 CFX_ByteString csV; |
761 CPDF_Object* pV = FPDF_GetFieldAttr(m_pDict, "V"); | 758 CPDF_Object* pV = FPDF_GetFieldAttr(m_pDict, "V"); |
762 if (pV) | 759 if (pV) |
763 csV = pV->GetString(); | 760 csV = pV->GetString(); |
764 if (csV == csBExport) | 761 if (csV == csBExport) |
765 m_pDict->SetNameFor("V", "Off"); | 762 m_pDict->SetNewFor<CPDF_Name>("V", "Off"); |
766 } | 763 } |
767 } else if (bChecked) { | 764 } else if (bChecked) { |
768 CFX_ByteString csIndex; | 765 CFX_ByteString csIndex; |
769 csIndex.Format("%d", iControlIndex); | 766 csIndex.Format("%d", iControlIndex); |
770 m_pDict->SetNameFor("V", csIndex); | 767 m_pDict->SetNewFor<CPDF_Name>("V", csIndex); |
771 } | 768 } |
772 if (bNotify && m_pForm->m_pFormNotify) | 769 if (bNotify && m_pForm->m_pFormNotify) |
773 m_pForm->m_pFormNotify->AfterCheckedStatusChange(this); | 770 m_pForm->m_pFormNotify->AfterCheckedStatusChange(this); |
774 return true; | 771 return true; |
775 } | 772 } |
776 | 773 |
777 CFX_WideString CPDF_FormField::GetCheckValue(bool bDefault) const { | 774 CFX_WideString CPDF_FormField::GetCheckValue(bool bDefault) const { |
778 ASSERT(GetType() == CheckBox || GetType() == RadioButton); | 775 ASSERT(GetType() == CheckBox || GetType() == RadioButton); |
779 CFX_WideString csExport = L"Off"; | 776 CFX_WideString csExport = L"Off"; |
780 int iCount = CountControls(); | 777 int iCount = CountControls(); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
841 } | 838 } |
842 return false; | 839 return false; |
843 } | 840 } |
844 | 841 |
845 bool CPDF_FormField::SelectOption(int iOptIndex, bool bSelected, bool bNotify) { | 842 bool CPDF_FormField::SelectOption(int iOptIndex, bool bSelected, bool bNotify) { |
846 CPDF_Array* pArray = m_pDict->GetArrayFor("I"); | 843 CPDF_Array* pArray = m_pDict->GetArrayFor("I"); |
847 if (!pArray) { | 844 if (!pArray) { |
848 if (!bSelected) | 845 if (!bSelected) |
849 return true; | 846 return true; |
850 | 847 |
851 pArray = new CPDF_Array; | 848 pArray = m_pDict->SetNewFor<CPDF_Array>("I"); |
852 m_pDict->SetFor("I", pArray); | |
853 } | 849 } |
854 | 850 |
855 bool bReturn = false; | 851 bool bReturn = false; |
856 for (size_t i = 0; i < pArray->GetCount(); i++) { | 852 for (size_t i = 0; i < pArray->GetCount(); i++) { |
857 int iFind = pArray->GetIntegerAt(i); | 853 int iFind = pArray->GetIntegerAt(i); |
858 if (iFind == iOptIndex) { | 854 if (iFind == iOptIndex) { |
859 if (bSelected) | 855 if (bSelected) |
860 return true; | 856 return true; |
861 | 857 |
862 if (bNotify && m_pForm->m_pFormNotify) { | 858 if (bNotify && m_pForm->m_pFormNotify) { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
988 case ListBox: | 984 case ListBox: |
989 NotifyAfterSelectionChange(); | 985 NotifyAfterSelectionChange(); |
990 break; | 986 break; |
991 case ComboBox: | 987 case ComboBox: |
992 NotifyAfterValueChange(); | 988 NotifyAfterValueChange(); |
993 break; | 989 break; |
994 default: | 990 default: |
995 break; | 991 break; |
996 } | 992 } |
997 } | 993 } |
OLD | NEW |