| 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 "fpdfsdk/javascript/Field.h" | 7 #include "fpdfsdk/javascript/Field.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 } | 258 } |
| 259 | 259 |
| 260 void Field::UpdateFormField(CPDFSDK_FormFillEnvironment* pFormFillEnv, | 260 void Field::UpdateFormField(CPDFSDK_FormFillEnvironment* pFormFillEnv, |
| 261 CPDF_FormField* pFormField, | 261 CPDF_FormField* pFormField, |
| 262 bool bChangeMark, | 262 bool bChangeMark, |
| 263 bool bResetAP, | 263 bool bResetAP, |
| 264 bool bRefresh) { | 264 bool bRefresh) { |
| 265 CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm(); | 265 CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm(); |
| 266 | 266 |
| 267 if (bResetAP) { | 267 if (bResetAP) { |
| 268 std::vector<CPDFSDK_Widget*> widgets; | 268 std::vector<CPDFSDK_Annot::ObservedPtr> widgets; |
| 269 pInterForm->GetWidgets(pFormField, &widgets); | 269 pInterForm->GetWidgets(pFormField, &widgets); |
| 270 | 270 |
| 271 int nFieldType = pFormField->GetFieldType(); | 271 int nFieldType = pFormField->GetFieldType(); |
| 272 if (nFieldType == FIELDTYPE_COMBOBOX || nFieldType == FIELDTYPE_TEXTFIELD) { | 272 if (nFieldType == FIELDTYPE_COMBOBOX || nFieldType == FIELDTYPE_TEXTFIELD) { |
| 273 for (CPDFSDK_Annot* pAnnot : widgets) { | 273 for (auto& pObserved : widgets) { |
| 274 bool bFormatted = false; | |
| 275 CPDFSDK_Annot::ObservedPtr pObserved(pAnnot); | |
| 276 CFX_WideString sValue = | |
| 277 static_cast<CPDFSDK_Widget*>(pObserved.Get())->OnFormat(bFormatted); | |
| 278 if (pObserved) { | 274 if (pObserved) { |
| 279 static_cast<CPDFSDK_Widget*>(pObserved.Get()) | 275 bool bFormatted = false; |
| 280 ->ResetAppearance(bFormatted ? &sValue : nullptr, false); | 276 CFX_WideString sValue = static_cast<CPDFSDK_Widget*>(pObserved.Get()) |
| 277 ->OnFormat(bFormatted); |
| 278 if (pObserved) { // Not redundant, may be clobbered by OnFormat. |
| 279 static_cast<CPDFSDK_Widget*>(pObserved.Get()) |
| 280 ->ResetAppearance(bFormatted ? &sValue : nullptr, false); |
| 281 } |
| 281 } | 282 } |
| 282 } | 283 } |
| 283 } else { | 284 } else { |
| 284 for (CPDFSDK_Widget* pWidget : widgets) { | 285 for (auto& pObserved : widgets) { |
| 285 pWidget->ResetAppearance(nullptr, false); | 286 if (pObserved) { |
| 287 static_cast<CPDFSDK_Widget*>(pObserved.Get()) |
| 288 ->ResetAppearance(nullptr, false); |
| 289 } |
| 286 } | 290 } |
| 287 } | 291 } |
| 288 } | 292 } |
| 289 | 293 |
| 290 if (bRefresh) { | 294 if (bRefresh) { |
| 291 // Refresh the widget list. The calls in |bResetAP| may have caused widgets | 295 // Refresh the widget list. The calls in |bResetAP| may have caused widgets |
| 292 // to be removed from the list. We need to call |GetWidgets| again to be | 296 // to be removed from the list. We need to call |GetWidgets| again to be |
| 293 // sure none of the widgets have been deleted. | 297 // sure none of the widgets have been deleted. |
| 294 std::vector<CPDFSDK_Widget*> widgets; | 298 std::vector<CPDFSDK_Annot::ObservedPtr> widgets; |
| 295 pInterForm->GetWidgets(pFormField, &widgets); | 299 pInterForm->GetWidgets(pFormField, &widgets); |
| 296 | 300 |
| 297 // TODO(dsinclair): Determine if all widgets share the same | 301 // TODO(dsinclair): Determine if all widgets share the same |
| 298 // CPDFSDK_InterForm. If that's the case, we can move the code to | 302 // CPDFSDK_InterForm. If that's the case, we can move the code to |
| 299 // |GetFormFillEnv| out of the loop. | 303 // |GetFormFillEnv| out of the loop. |
| 300 for (CPDFSDK_Widget* pWidget : widgets) { | 304 for (auto& pObserved : widgets) { |
| 301 pWidget->GetInterForm() | 305 if (pObserved) { |
| 302 ->GetFormFillEnv() | 306 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pObserved.Get()); |
| 303 ->UpdateAllViews(nullptr, pWidget); | 307 pWidget->GetInterForm()->GetFormFillEnv()->UpdateAllViews(nullptr, |
| 308 pWidget); |
| 309 } |
| 304 } | 310 } |
| 305 } | 311 } |
| 306 | 312 |
| 307 if (bChangeMark) | 313 if (bChangeMark) |
| 308 pFormFillEnv->SetChangeMark(); | 314 pFormFillEnv->SetChangeMark(); |
| 309 } | 315 } |
| 310 | 316 |
| 311 void Field::UpdateFormControl(CPDFSDK_FormFillEnvironment* pFormFillEnv, | 317 void Field::UpdateFormControl(CPDFSDK_FormFillEnvironment* pFormFillEnv, |
| 312 CPDF_FormControl* pFormControl, | 318 CPDF_FormControl* pFormControl, |
| 313 bool bChangeMark, | 319 bool bChangeMark, |
| (...skipping 1482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1796 if (pFormField->GetFieldType() != FIELDTYPE_COMBOBOX && | 1802 if (pFormField->GetFieldType() != FIELDTYPE_COMBOBOX && |
| 1797 pFormField->GetFieldType() != FIELDTYPE_LISTBOX) { | 1803 pFormField->GetFieldType() != FIELDTYPE_LISTBOX) { |
| 1798 return false; | 1804 return false; |
| 1799 } | 1805 } |
| 1800 | 1806 |
| 1801 vp << (int32_t)pFormField->CountOptions(); | 1807 vp << (int32_t)pFormField->CountOptions(); |
| 1802 return true; | 1808 return true; |
| 1803 } | 1809 } |
| 1804 | 1810 |
| 1805 bool Field::page(IJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) { | 1811 bool Field::page(IJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) { |
| 1806 if (!vp.IsGetting()) | 1812 if (!vp.IsGetting()) { |
| 1813 sError = JSGetStringFromID(IDS_STRING_JSREADONLY); |
| 1807 return false; | 1814 return false; |
| 1815 } |
| 1808 | 1816 |
| 1809 std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); | 1817 std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName); |
| 1810 if (FieldArray.empty()) | 1818 if (FieldArray.empty()) |
| 1811 return false; | 1819 return false; |
| 1812 | 1820 |
| 1813 CPDF_FormField* pFormField = FieldArray[0]; | 1821 CPDF_FormField* pFormField = FieldArray[0]; |
| 1814 if (!pFormField) | 1822 if (!pFormField) |
| 1815 return false; | 1823 return false; |
| 1816 | 1824 |
| 1817 std::vector<CPDFSDK_Widget*> widgets; | 1825 std::vector<CPDFSDK_Annot::ObservedPtr> widgets; |
| 1818 m_pFormFillEnv->GetInterForm()->GetWidgets(pFormField, &widgets); | 1826 m_pFormFillEnv->GetInterForm()->GetWidgets(pFormField, &widgets); |
| 1819 | |
| 1820 if (widgets.empty()) { | 1827 if (widgets.empty()) { |
| 1821 vp << (int32_t)-1; | 1828 vp << (int32_t)-1; |
| 1822 return true; | 1829 return true; |
| 1823 } | 1830 } |
| 1824 | 1831 |
| 1825 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); | 1832 CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); |
| 1826 CJS_Array PageArray; | 1833 CJS_Array PageArray; |
| 1827 for (size_t i = 0; i < widgets.size(); ++i) { | 1834 int i = 0; |
| 1828 CPDFSDK_PageView* pPageView = widgets[i]->GetPageView(); | 1835 for (const auto& pObserved : widgets) { |
| 1836 if (!pObserved) { |
| 1837 sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT); |
| 1838 return false; |
| 1839 } |
| 1840 |
| 1841 auto pWidget = static_cast<CPDFSDK_Widget*>(pObserved.Get()); |
| 1842 CPDFSDK_PageView* pPageView = pWidget->GetPageView(); |
| 1829 if (!pPageView) | 1843 if (!pPageView) |
| 1830 return false; | 1844 return false; |
| 1831 | 1845 |
| 1832 PageArray.SetElement( | 1846 PageArray.SetElement( |
| 1833 pRuntime, i, CJS_Value(pRuntime, (int32_t)pPageView->GetPageIndex())); | 1847 pRuntime, i, CJS_Value(pRuntime, (int32_t)pPageView->GetPageIndex())); |
| 1848 ++i; |
| 1834 } | 1849 } |
| 1835 | 1850 |
| 1836 vp << PageArray; | 1851 vp << PageArray; |
| 1837 return true; | 1852 return true; |
| 1838 } | 1853 } |
| 1839 | 1854 |
| 1840 bool Field::password(IJS_Context* cc, | 1855 bool Field::password(IJS_Context* cc, |
| 1841 CJS_PropValue& vp, | 1856 CJS_PropValue& vp, |
| 1842 CFX_WideString& sError) { | 1857 CFX_WideString& sError) { |
| 1843 ASSERT(m_pFormFillEnv); | 1858 ASSERT(m_pFormFillEnv); |
| (...skipping 1620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3464 } | 3479 } |
| 3465 } | 3480 } |
| 3466 | 3481 |
| 3467 void Field::AddField(CPDFSDK_FormFillEnvironment* pFormFillEnv, | 3482 void Field::AddField(CPDFSDK_FormFillEnvironment* pFormFillEnv, |
| 3468 int nPageIndex, | 3483 int nPageIndex, |
| 3469 int nFieldType, | 3484 int nFieldType, |
| 3470 const CFX_WideString& sName, | 3485 const CFX_WideString& sName, |
| 3471 const CFX_FloatRect& rcCoords) { | 3486 const CFX_FloatRect& rcCoords) { |
| 3472 // Not supported. | 3487 // Not supported. |
| 3473 } | 3488 } |
| OLD | NEW |