| 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/fpdfapi/fpdf_parser/include/cfdf_document.h" |     7 #include "core/fpdfapi/fpdf_parser/include/cfdf_document.h" | 
|     8 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |     8 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | 
|     9 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |     9 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" | 
|    10 #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" |    10 #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" | 
| (...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   451     sel_value = |   451     sel_value = | 
|   452         elementValue ? elementValue->GetUnicodeText() : CFX_WideString(); |   452         elementValue ? elementValue->GetUnicodeText() : CFX_WideString(); | 
|   453   } |   453   } | 
|   454   if (index < CountSelectedOptions()) { |   454   if (index < CountSelectedOptions()) { | 
|   455     int iOptIndex = GetSelectedOptionIndex(index); |   455     int iOptIndex = GetSelectedOptionIndex(index); | 
|   456     CFX_WideString csOpt = GetOptionValue(iOptIndex); |   456     CFX_WideString csOpt = GetOptionValue(iOptIndex); | 
|   457     if (csOpt == sel_value) { |   457     if (csOpt == sel_value) { | 
|   458       return iOptIndex; |   458       return iOptIndex; | 
|   459     } |   459     } | 
|   460   } |   460   } | 
|   461   int nOpts = CountOptions(); |   461   for (int i = 0; i < CountOptions(); i++) { | 
|   462   for (int i = 0; i < nOpts; i++) { |   462     if (sel_value == GetOptionValue(i)) | 
|   463     if (sel_value == GetOptionValue(i)) { |  | 
|   464       return i; |   463       return i; | 
|   465     } |  | 
|   466   } |   464   } | 
|   467   return -1; |   465   return -1; | 
|   468 } |   466 } | 
|   469 FX_BOOL CPDF_FormField::ClearSelection(FX_BOOL bNotify) { |   467 FX_BOOL CPDF_FormField::ClearSelection(FX_BOOL bNotify) { | 
|   470   if (bNotify && m_pForm->m_pFormNotify) { |   468   if (bNotify && m_pForm->m_pFormNotify) { | 
|   471     int iRet = 0; |   469     int iRet = 0; | 
|   472     CFX_WideString csValue; |   470     CFX_WideString csValue; | 
|   473     int iIndex = GetSelectedIndex(0); |   471     int iIndex = GetSelectedIndex(0); | 
|   474     if (iIndex >= 0) { |   472     if (iIndex >= 0) { | 
|   475       csValue = GetOptionLabel(iIndex); |   473       csValue = GetOptionLabel(iIndex); | 
| (...skipping 17 matching lines...) Expand all  Loading... | 
|   493     if (GetType() == ComboBox) { |   491     if (GetType() == ComboBox) { | 
|   494       m_pForm->m_pFormNotify->AfterValueChange(this); |   492       m_pForm->m_pFormNotify->AfterValueChange(this); | 
|   495     } |   493     } | 
|   496   } |   494   } | 
|   497   if (CPDF_InterForm::m_bUpdateAP) { |   495   if (CPDF_InterForm::m_bUpdateAP) { | 
|   498     UpdateAP(NULL); |   496     UpdateAP(NULL); | 
|   499   } |   497   } | 
|   500   m_pForm->m_bUpdated = TRUE; |   498   m_pForm->m_bUpdated = TRUE; | 
|   501   return TRUE; |   499   return TRUE; | 
|   502 } |   500 } | 
 |   501  | 
|   503 FX_BOOL CPDF_FormField::IsItemSelected(int index) { |   502 FX_BOOL CPDF_FormField::IsItemSelected(int index) { | 
|   504   ASSERT(GetType() == ComboBox || GetType() == ListBox); |   503   ASSERT(GetType() == ComboBox || GetType() == ListBox); | 
|   505   if (index < 0 || index >= CountOptions()) { |   504   if (index < 0 || index >= CountOptions()) { | 
|   506     return FALSE; |   505     return FALSE; | 
|   507   } |   506   } | 
|   508   if (IsOptionSelected(index)) { |   507   if (IsOptionSelected(index)) { | 
|   509     return TRUE; |   508     return TRUE; | 
|   510   } |   509   } | 
|   511   CFX_WideString opt_value = GetOptionValue(index); |   510   CFX_WideString opt_value = GetOptionValue(index); | 
|   512   CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V"); |   511   CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V"); | 
| (...skipping 24 matching lines...) Expand all  Loading... | 
|   537       break; |   536       break; | 
|   538     } |   537     } | 
|   539   } |   538   } | 
|   540   for (uint32_t i = 0; i < pArray->GetCount(); i++) |   539   for (uint32_t i = 0; i < pArray->GetCount(); i++) | 
|   541     if (pArray->GetDirectObjectAt(i)->GetUnicodeText() == opt_value && |   540     if (pArray->GetDirectObjectAt(i)->GetUnicodeText() == opt_value && | 
|   542         (int)i == iPos) { |   541         (int)i == iPos) { | 
|   543       return TRUE; |   542       return TRUE; | 
|   544     } |   543     } | 
|   545   return FALSE; |   544   return FALSE; | 
|   546 } |   545 } | 
 |   546  | 
|   547 FX_BOOL CPDF_FormField::SetItemSelection(int index, |   547 FX_BOOL CPDF_FormField::SetItemSelection(int index, | 
|   548                                          FX_BOOL bSelected, |   548                                          FX_BOOL bSelected, | 
|   549                                          FX_BOOL bNotify) { |   549                                          FX_BOOL bNotify) { | 
|   550   ASSERT(GetType() == ComboBox || GetType() == ListBox); |   550   ASSERT(GetType() == ComboBox || GetType() == ListBox); | 
|   551   if (index < 0 || index >= CountOptions()) { |   551   if (index < 0 || index >= CountOptions()) { | 
|   552     return FALSE; |   552     return FALSE; | 
|   553   } |   553   } | 
|   554   CFX_WideString opt_value = GetOptionValue(index); |   554   CFX_WideString opt_value = GetOptionValue(index); | 
|   555   if (bNotify && m_pForm->m_pFormNotify) { |   555   if (bNotify && m_pForm->m_pFormNotify) { | 
|   556     int iRet = 0; |   556     int iRet = 0; | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
|   568     CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V"); |   568     CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V"); | 
|   569     if (pValue) { |   569     if (pValue) { | 
|   570       if (m_Type == ListBox) { |   570       if (m_Type == ListBox) { | 
|   571         SelectOption(index, FALSE); |   571         SelectOption(index, FALSE); | 
|   572         if (pValue->IsString()) { |   572         if (pValue->IsString()) { | 
|   573           if (pValue->GetUnicodeText() == opt_value) { |   573           if (pValue->GetUnicodeText() == opt_value) { | 
|   574             m_pDict->RemoveAt("V"); |   574             m_pDict->RemoveAt("V"); | 
|   575           } |   575           } | 
|   576         } else if (pValue->IsArray()) { |   576         } else if (pValue->IsArray()) { | 
|   577           CPDF_Array* pArray = new CPDF_Array; |   577           CPDF_Array* pArray = new CPDF_Array; | 
|   578           int iCount = CountOptions(); |   578           for (int i = 0; i < CountOptions(); i++) { | 
|   579           for (int i = 0; i < iCount; i++) { |   579             if (i != index && IsItemSelected(i)) { | 
|   580             if (i != index) { |   580               opt_value = GetOptionValue(i); | 
|   581               if (IsItemSelected(i)) { |   581               pArray->AddString(PDF_EncodeText(opt_value)); | 
|   582                 opt_value = GetOptionValue(i); |  | 
|   583                 pArray->AddString(PDF_EncodeText(opt_value)); |  | 
|   584               } |  | 
|   585             } |   582             } | 
|   586           } |   583           } | 
|   587           if (pArray->GetCount() < 1) { |   584           if (pArray->GetCount() < 1) | 
|   588             pArray->Release(); |   585             pArray->Release(); | 
|   589           } else { |   586           else | 
|   590             m_pDict->SetAt("V", pArray); |   587             m_pDict->SetAt("V", pArray); | 
|   591           } |  | 
|   592         } |   588         } | 
|   593       } else if (m_Type == ComboBox) { |   589       } else if (m_Type == ComboBox) { | 
|   594         m_pDict->RemoveAt("V"); |   590         m_pDict->RemoveAt("V"); | 
|   595         m_pDict->RemoveAt("I"); |   591         m_pDict->RemoveAt("I"); | 
|   596       } |   592       } | 
|   597     } |   593     } | 
|   598   } else { |   594   } else { | 
|   599     if (m_Type == ListBox) { |   595     if (m_Type == ListBox) { | 
|   600       SelectOption(index, TRUE); |   596       SelectOption(index, TRUE); | 
|   601       if (!(m_Flags & FORMLIST_MULTISELECT)) { |   597       if (!(m_Flags & FORMLIST_MULTISELECT)) { | 
|   602         m_pDict->SetAtString("V", PDF_EncodeText(opt_value)); |   598         m_pDict->SetAtString("V", PDF_EncodeText(opt_value)); | 
|   603       } else { |   599       } else { | 
|   604         CPDF_Array* pArray = new CPDF_Array; |   600         CPDF_Array* pArray = new CPDF_Array; | 
|   605         int iCount = CountOptions(); |   601         for (int i = 0; i < CountOptions(); i++) { | 
|   606         for (int i = 0; i < iCount; i++) { |   602           if (i == index || IsItemSelected(i)) { | 
|   607           FX_BOOL bSelected; |  | 
|   608           if (i != index) { |  | 
|   609             bSelected = IsItemSelected(i); |  | 
|   610           } else { |  | 
|   611             bSelected = TRUE; |  | 
|   612           } |  | 
|   613           if (bSelected) { |  | 
|   614             opt_value = GetOptionValue(i); |   603             opt_value = GetOptionValue(i); | 
|   615             pArray->AddString(PDF_EncodeText(opt_value)); |   604             pArray->AddString(PDF_EncodeText(opt_value)); | 
|   616           } |   605           } | 
|   617         } |   606         } | 
|   618         m_pDict->SetAt("V", pArray); |   607         m_pDict->SetAt("V", pArray); | 
|   619       } |   608       } | 
|   620     } else if (m_Type == ComboBox) { |   609     } else if (m_Type == ComboBox) { | 
|   621       m_pDict->SetAtString("V", PDF_EncodeText(opt_value)); |   610       m_pDict->SetAtString("V", PDF_EncodeText(opt_value)); | 
|   622       CPDF_Array* pI = new CPDF_Array; |   611       CPDF_Array* pI = new CPDF_Array; | 
|   623       pI->AddInteger(index); |   612       pI->AddInteger(index); | 
|   624       m_pDict->SetAt("I", pI); |   613       m_pDict->SetAt("I", pI); | 
|   625     } |   614     } | 
|   626   } |   615   } | 
|   627   if (bNotify && m_pForm->m_pFormNotify) { |   616   if (bNotify && m_pForm->m_pFormNotify) { | 
|   628     if (GetType() == ListBox) { |   617     if (GetType() == ListBox) { | 
|   629       m_pForm->m_pFormNotify->AfterSelectionChange(this); |   618       m_pForm->m_pFormNotify->AfterSelectionChange(this); | 
|   630     } |   619     } | 
|   631     if (GetType() == ComboBox) { |   620     if (GetType() == ComboBox) { | 
|   632       m_pForm->m_pFormNotify->AfterValueChange(this); |   621       m_pForm->m_pFormNotify->AfterValueChange(this); | 
|   633     } |   622     } | 
|   634   } |   623   } | 
|   635   if (CPDF_InterForm::m_bUpdateAP) { |   624   if (CPDF_InterForm::m_bUpdateAP) { | 
|   636     UpdateAP(NULL); |   625     UpdateAP(NULL); | 
|   637   } |   626   } | 
|   638   m_pForm->m_bUpdated = TRUE; |   627   m_pForm->m_bUpdated = TRUE; | 
|   639   return TRUE; |   628   return TRUE; | 
|   640 } |   629 } | 
 |   630  | 
|   641 FX_BOOL CPDF_FormField::IsItemDefaultSelected(int index) { |   631 FX_BOOL CPDF_FormField::IsItemDefaultSelected(int index) { | 
|   642   ASSERT(GetType() == ComboBox || GetType() == ListBox); |   632   ASSERT(GetType() == ComboBox || GetType() == ListBox); | 
|   643   if (index < 0 || index >= CountOptions()) { |   633   if (index < 0 || index >= CountOptions()) | 
|   644     return FALSE; |   634     return FALSE; | 
|   645   } |  | 
|   646   int iDVIndex = GetDefaultSelectedItem(); |   635   int iDVIndex = GetDefaultSelectedItem(); | 
|   647   if (iDVIndex < 0) { |   636   return iDVIndex >= 0 && iDVIndex == index; | 
|   648     return FALSE; |  | 
|   649   } |  | 
|   650   return (iDVIndex == index); |  | 
|   651 } |   637 } | 
 |   638  | 
|   652 int CPDF_FormField::GetDefaultSelectedItem() { |   639 int CPDF_FormField::GetDefaultSelectedItem() { | 
|   653   ASSERT(GetType() == ComboBox || GetType() == ListBox); |   640   ASSERT(GetType() == ComboBox || GetType() == ListBox); | 
|   654   CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "DV"); |   641   CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "DV"); | 
|   655   if (!pValue) { |   642   if (!pValue) | 
|   656     return -1; |   643     return -1; | 
|   657   } |  | 
|   658   CFX_WideString csDV = pValue->GetUnicodeText(); |   644   CFX_WideString csDV = pValue->GetUnicodeText(); | 
|   659   if (csDV.IsEmpty()) { |   645   if (csDV.IsEmpty()) | 
|   660     return -1; |   646     return -1; | 
|   661   } |   647   for (int i = 0; i < CountOptions(); i++) { | 
|   662   int iCount = CountOptions(); |   648     if (csDV == GetOptionValue(i)) | 
|   663   for (int i = 0; i < iCount; i++) { |  | 
|   664     if (csDV == GetOptionValue(i)) { |  | 
|   665       return i; |   649       return i; | 
|   666     } |  | 
|   667   } |   650   } | 
|   668   return -1; |   651   return -1; | 
|   669 } |   652 } | 
 |   653  | 
|   670 void CPDF_FormField::UpdateAP(CPDF_FormControl* pControl) { |   654 void CPDF_FormField::UpdateAP(CPDF_FormControl* pControl) { | 
|   671   if (m_Type == PushButton) { |   655   if (m_Type == PushButton || m_Type == RadioButton || m_Type == CheckBox) | 
|   672     return; |   656     return; | 
|   673   } |   657   if (!m_pForm->m_bGenerateAP) | 
|   674   if (m_Type == RadioButton || m_Type == CheckBox) { |  | 
|   675     return; |   658     return; | 
|   676   } |  | 
|   677   if (!m_pForm->m_bGenerateAP) { |  | 
|   678     return; |  | 
|   679   } |  | 
|   680   for (int i = 0; i < CountControls(); i++) { |   659   for (int i = 0; i < CountControls(); i++) { | 
|   681     CPDF_FormControl* pControl = GetControl(i); |   660     CPDF_FormControl* pControl = GetControl(i); | 
|   682     FPDF_GenerateAP(m_pForm->m_pDocument, pControl->m_pWidgetDict); |   661     FPDF_GenerateAP(m_pForm->m_pDocument, pControl->m_pWidgetDict); | 
|   683   } |   662   } | 
|   684 } |   663 } | 
 |   664  | 
|   685 int CPDF_FormField::CountOptions() { |   665 int CPDF_FormField::CountOptions() { | 
|   686   CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict, "Opt")); |   666   CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict, "Opt")); | 
|   687   return pArray ? pArray->GetCount() : 0; |   667   return pArray ? pArray->GetCount() : 0; | 
|   688 } |   668 } | 
 |   669  | 
|   689 CFX_WideString CPDF_FormField::GetOptionText(int index, int sub_index) { |   670 CFX_WideString CPDF_FormField::GetOptionText(int index, int sub_index) { | 
|   690   CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict, "Opt")); |   671   CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict, "Opt")); | 
|   691   if (!pArray) |   672   if (!pArray) | 
|   692     return CFX_WideString(); |   673     return CFX_WideString(); | 
|   693  |   674  | 
|   694   CPDF_Object* pOption = pArray->GetDirectObjectAt(index); |   675   CPDF_Object* pOption = pArray->GetDirectObjectAt(index); | 
|   695   if (!pOption) |   676   if (!pOption) | 
|   696     return CFX_WideString(); |   677     return CFX_WideString(); | 
|   697   if (CPDF_Array* pOptionArray = pOption->AsArray()) |   678   if (CPDF_Array* pOptionArray = pOption->AsArray()) | 
|   698     pOption = pOptionArray->GetDirectObjectAt(sub_index); |   679     pOption = pOptionArray->GetDirectObjectAt(sub_index); | 
|   699  |   680  | 
|   700   CPDF_String* pString = ToString(pOption); |   681   CPDF_String* pString = ToString(pOption); | 
|   701   return pString ? pString->GetUnicodeText() : CFX_WideString(); |   682   return pString ? pString->GetUnicodeText() : CFX_WideString(); | 
|   702 } |   683 } | 
|   703 CFX_WideString CPDF_FormField::GetOptionLabel(int index) { |   684 CFX_WideString CPDF_FormField::GetOptionLabel(int index) { | 
|   704   return GetOptionText(index, 1); |   685   return GetOptionText(index, 1); | 
|   705 } |   686 } | 
|   706 CFX_WideString CPDF_FormField::GetOptionValue(int index) { |   687 CFX_WideString CPDF_FormField::GetOptionValue(int index) { | 
|   707   return GetOptionText(index, 0); |   688   return GetOptionText(index, 0); | 
|   708 } |   689 } | 
 |   690  | 
|   709 int CPDF_FormField::FindOption(CFX_WideString csOptLabel) { |   691 int CPDF_FormField::FindOption(CFX_WideString csOptLabel) { | 
|   710   int iCount = CountOptions(); |   692   for (int i = 0; i < CountOptions(); i++) { | 
|   711   for (int i = 0; i < iCount; i++) { |   693     if (GetOptionValue(i) == csOptLabel) | 
|   712     CFX_WideString csValue = GetOptionValue(i); |  | 
|   713     if (csValue == csOptLabel) { |  | 
|   714       return i; |   694       return i; | 
|   715     } |  | 
|   716   } |   695   } | 
|   717   return -1; |   696   return -1; | 
|   718 } |   697 } | 
|   719 int CPDF_FormField::FindOptionValue(const CFX_WideString& csOptValue, |   698  | 
|   720                                     int iStartIndex) { |   699 int CPDF_FormField::FindOptionValue(const CFX_WideString& csOptValue) { | 
|   721   if (iStartIndex < 0) { |   700   for (int i = 0; i < CountOptions(); i++) { | 
|   722     iStartIndex = 0; |   701     if (GetOptionValue(i) == csOptValue) | 
|   723   } |   702       return i; | 
|   724   int iCount = CountOptions(); |  | 
|   725   for (; iStartIndex < iCount; iStartIndex++) { |  | 
|   726     CFX_WideString csValue = GetOptionValue(iStartIndex); |  | 
|   727     if (csValue == csOptValue) { |  | 
|   728       return iStartIndex; |  | 
|   729     } |  | 
|   730   } |   703   } | 
|   731   return -1; |   704   return -1; | 
|   732 } |   705 } | 
 |   706  | 
|   733 #ifdef PDF_ENABLE_XFA |   707 #ifdef PDF_ENABLE_XFA | 
|   734 int CPDF_FormField::InsertOption(CFX_WideString csOptLabel, |   708 int CPDF_FormField::InsertOption(CFX_WideString csOptLabel, | 
|   735                                  int index, |   709                                  int index, | 
|   736                                  FX_BOOL bNotify) { |   710                                  FX_BOOL bNotify) { | 
|   737   if (csOptLabel.IsEmpty()) |   711   if (csOptLabel.IsEmpty()) | 
|   738     return -1; |   712     return -1; | 
|   739  |   713  | 
|   740   if (bNotify && m_pForm->m_pFormNotify) { |   714   if (bNotify && m_pForm->m_pFormNotify) { | 
|   741     int iRet = 0; |   715     int iRet = 0; | 
|   742     if (GetType() == ListBox) |   716     if (GetType() == ListBox) | 
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1088     pFontDict = m_pForm->m_pFormDict->GetDictBy("DR") |  1062     pFontDict = m_pForm->m_pFormDict->GetDictBy("DR") | 
|  1089                     ->GetDictBy("Font") |  1063                     ->GetDictBy("Font") | 
|  1090                     ->GetDictBy(font_name); |  1064                     ->GetDictBy(font_name); | 
|  1091  |  1065  | 
|  1092   if (!pFontDict) { |  1066   if (!pFontDict) { | 
|  1093     return; |  1067     return; | 
|  1094   } |  1068   } | 
|  1095   m_pFont = m_pForm->m_pDocument->LoadFont(pFontDict); |  1069   m_pFont = m_pForm->m_pDocument->LoadFont(pFontDict); | 
|  1096   m_FontSize = FX_atof(syntax.GetWord()); |  1070   m_FontSize = FX_atof(syntax.GetWord()); | 
|  1097 } |  1071 } | 
| OLD | NEW |