John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 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. |
Lei Zhang | 95e854f | 2015-06-13 00:58:06 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Dan Sinclair | aa403d3 | 2016-03-15 14:57:22 -0400 | [diff] [blame] | 7 | #include "core/fpdfapi/fpdf_parser/include/cfdf_document.h" |
| 8 | #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
| 9 | #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
| 10 | #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" |
| 11 | #include "core/fpdfapi/fpdf_parser/include/cpdf_simple_parser.h" |
| 12 | #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" |
Dan Sinclair | 764ec51 | 2016-03-14 13:35:12 -0400 | [diff] [blame] | 13 | #include "core/fpdfdoc/doc_utils.h" |
Lei Zhang | a9fa50f | 2015-11-10 09:45:32 -0800 | [diff] [blame] | 14 | #include "core/include/fpdfdoc/fpdf_doc.h" |
Tom Sepez | eff208f | 2015-05-08 13:45:01 -0700 | [diff] [blame] | 15 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 16 | FX_BOOL PDF_FormField_IsUnison(CPDF_FormField* pField) { |
| 17 | FX_BOOL bUnison = FALSE; |
| 18 | if (pField->GetType() == CPDF_FormField::CheckBox) { |
| 19 | bUnison = TRUE; |
| 20 | } else { |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 21 | uint32_t dwFlags = pField->GetFieldFlags(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 22 | bUnison = ((dwFlags & 0x2000000) != 0); |
| 23 | } |
| 24 | return bUnison; |
| 25 | } |
| 26 | CPDF_FormField::CPDF_FormField(CPDF_InterForm* pForm, CPDF_Dictionary* pDict) { |
| 27 | m_pDict = pDict; |
| 28 | m_Type = Unknown; |
| 29 | m_pForm = pForm; |
| 30 | m_pFont = NULL; |
| 31 | m_FontSize = 0; |
| 32 | SyncFieldFlags(); |
| 33 | } |
| 34 | CPDF_FormField::~CPDF_FormField() {} |
| 35 | void CPDF_FormField::SyncFieldFlags() { |
| 36 | CFX_ByteString type_name = FPDF_GetFieldAttr(m_pDict, "FT") |
| 37 | ? FPDF_GetFieldAttr(m_pDict, "FT")->GetString() |
| 38 | : CFX_ByteString(); |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 39 | uint32_t flags = FPDF_GetFieldAttr(m_pDict, "Ff") |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 40 | ? FPDF_GetFieldAttr(m_pDict, "Ff")->GetInteger() |
| 41 | : 0; |
| 42 | m_Flags = 0; |
| 43 | if (flags & 1) { |
| 44 | m_Flags |= FORMFIELD_READONLY; |
| 45 | } |
| 46 | if (flags & 2) { |
| 47 | m_Flags |= FORMFIELD_REQUIRED; |
| 48 | } |
| 49 | if (flags & 4) { |
| 50 | m_Flags |= FORMFIELD_NOEXPORT; |
| 51 | } |
| 52 | if (type_name == "Btn") { |
| 53 | if (flags & 0x8000) { |
| 54 | m_Type = RadioButton; |
| 55 | if (flags & 0x4000) { |
| 56 | m_Flags |= FORMRADIO_NOTOGGLEOFF; |
| 57 | } |
| 58 | if (flags & 0x2000000) { |
| 59 | m_Flags |= FORMRADIO_UNISON; |
| 60 | } |
| 61 | } else if (flags & 0x10000) { |
| 62 | m_Type = PushButton; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 63 | } else { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 64 | m_Type = CheckBox; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 65 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 66 | } else if (type_name == "Tx") { |
| 67 | if (flags & 0x100000) { |
| 68 | m_Type = File; |
| 69 | } else if (flags & 0x2000000) { |
| 70 | m_Type = RichText; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 71 | } else { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 72 | m_Type = Text; |
| 73 | if (flags & 0x1000) { |
| 74 | m_Flags |= FORMTEXT_MULTILINE; |
| 75 | } |
| 76 | if (flags & 0x2000) { |
| 77 | m_Flags |= FORMTEXT_PASSWORD; |
| 78 | } |
| 79 | if (flags & 0x800000) { |
| 80 | m_Flags |= FORMTEXT_NOSCROLL; |
| 81 | } |
| 82 | if (flags & 0x100000) { |
| 83 | m_Flags |= FORMTEXT_COMB; |
| 84 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 85 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 86 | LoadDA(); |
| 87 | } else if (type_name == "Ch") { |
| 88 | if (flags & 0x20000) { |
| 89 | m_Type = ComboBox; |
| 90 | if (flags & 0x40000) { |
| 91 | m_Flags |= FORMCOMBO_EDIT; |
| 92 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 93 | } else { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 94 | m_Type = ListBox; |
| 95 | if (flags & 0x200000) { |
| 96 | m_Flags |= FORMLIST_MULTISELECT; |
| 97 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 98 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 99 | LoadDA(); |
| 100 | } else if (type_name == "Sig") { |
| 101 | m_Type = Sign; |
| 102 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 103 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 104 | CFX_WideString CPDF_FormField::GetFullName() { |
| 105 | return ::GetFullName(m_pDict); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 106 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 107 | FX_BOOL CPDF_FormField::ResetField(FX_BOOL bNotify) { |
| 108 | switch (m_Type) { |
| 109 | case CPDF_FormField::CheckBox: |
| 110 | case CPDF_FormField::RadioButton: { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 111 | int iCount = CountControls(); |
| 112 | if (iCount) { |
Wei Li | 97da976 | 2016-03-11 17:00:48 -0800 | [diff] [blame] | 113 | // TODO(weili): Check whether anything special needs to be done for |
| 114 | // unison field. Otherwise, merge these branches. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 115 | if (PDF_FormField_IsUnison(this)) { |
| 116 | for (int i = 0; i < iCount; i++) { |
| 117 | CheckControl(i, GetControl(i)->IsDefaultChecked(), FALSE); |
| 118 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 119 | } else { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 120 | for (int i = 0; i < iCount; i++) { |
Wei Li | 97da976 | 2016-03-11 17:00:48 -0800 | [diff] [blame] | 121 | CheckControl(i, GetControl(i)->IsDefaultChecked(), FALSE); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 122 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 123 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 124 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 125 | if (bNotify && m_pForm->m_pFormNotify) { |
Tom Sepez | ed5d7aa | 2016-02-02 16:02:03 -0800 | [diff] [blame] | 126 | m_pForm->m_pFormNotify->AfterCheckedStatusChange(this); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 127 | } |
| 128 | } break; |
| 129 | case CPDF_FormField::ComboBox: { |
| 130 | CFX_WideString csValue; |
| 131 | ClearSelection(); |
| 132 | int iIndex = GetDefaultSelectedItem(); |
| 133 | if (iIndex >= 0) { |
| 134 | csValue = GetOptionLabel(iIndex); |
| 135 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 136 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 137 | int iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue); |
| 138 | if (iRet < 0) { |
| 139 | return FALSE; |
| 140 | } |
| 141 | } |
| 142 | SetItemSelection(iIndex, TRUE); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 143 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 144 | m_pForm->m_pFormNotify->AfterValueChange(this); |
| 145 | } |
| 146 | } break; |
| 147 | case CPDF_FormField::ListBox: { |
| 148 | CFX_WideString csValue; |
| 149 | ClearSelection(); |
| 150 | int iIndex = GetDefaultSelectedItem(); |
| 151 | if (iIndex >= 0) { |
| 152 | csValue = GetOptionLabel(iIndex); |
| 153 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 154 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 155 | int iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue); |
| 156 | if (iRet < 0) { |
| 157 | return FALSE; |
| 158 | } |
| 159 | } |
| 160 | SetItemSelection(iIndex, TRUE); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 161 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 162 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
| 163 | } |
| 164 | } break; |
| 165 | case CPDF_FormField::Text: |
| 166 | case CPDF_FormField::RichText: |
| 167 | case CPDF_FormField::File: |
| 168 | default: { |
| 169 | CPDF_Object* pDV = FPDF_GetFieldAttr(m_pDict, "DV"); |
| 170 | CFX_WideString csDValue; |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 171 | if (pDV) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 172 | csDValue = pDV->GetUnicodeText(); |
| 173 | } |
| 174 | CPDF_Object* pV = FPDF_GetFieldAttr(m_pDict, "V"); |
| 175 | CFX_WideString csValue; |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 176 | if (pV) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 177 | csValue = pV->GetUnicodeText(); |
| 178 | } |
| 179 | CPDF_Object* pRV = FPDF_GetFieldAttr(m_pDict, "RV"); |
| 180 | if (!pRV && (csDValue == csValue)) { |
| 181 | return FALSE; |
| 182 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 183 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 184 | int iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csDValue); |
| 185 | if (iRet < 0) { |
| 186 | return FALSE; |
| 187 | } |
| 188 | } |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 189 | if (pDV) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 190 | CPDF_Object* pClone = pDV->Clone(); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 191 | if (!pClone) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 192 | return FALSE; |
| 193 | } |
| 194 | m_pDict->SetAt("V", pClone); |
| 195 | if (pRV) { |
| 196 | CPDF_Object* pCloneR = pDV->Clone(); |
| 197 | m_pDict->SetAt("RV", pCloneR); |
| 198 | } |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 199 | } else { |
| 200 | m_pDict->RemoveAt("V"); |
| 201 | m_pDict->RemoveAt("RV"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 202 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 203 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 204 | m_pForm->m_pFormNotify->AfterValueChange(this); |
| 205 | } |
| 206 | m_pForm->m_bUpdated = TRUE; |
| 207 | } break; |
| 208 | } |
| 209 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 210 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 211 | int CPDF_FormField::GetControlIndex(const CPDF_FormControl* pControl) { |
Tom Sepez | bd573f1 | 2015-12-09 16:39:40 -0800 | [diff] [blame] | 212 | if (!pControl) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 213 | return -1; |
| 214 | } |
Tom Sepez | bd573f1 | 2015-12-09 16:39:40 -0800 | [diff] [blame] | 215 | for (int i = 0; i < m_ControlList.GetSize(); i++) { |
| 216 | if (m_ControlList.GetAt(i) == pControl) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 217 | return i; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 218 | } |
| 219 | return -1; |
| 220 | } |
| 221 | int CPDF_FormField::GetFieldType() { |
| 222 | switch (m_Type) { |
| 223 | case PushButton: |
| 224 | return FIELDTYPE_PUSHBUTTON; |
| 225 | case CheckBox: |
| 226 | return FIELDTYPE_CHECKBOX; |
| 227 | case RadioButton: |
| 228 | return FIELDTYPE_RADIOBUTTON; |
| 229 | case ComboBox: |
| 230 | return FIELDTYPE_COMBOBOX; |
| 231 | case ListBox: |
| 232 | return FIELDTYPE_LISTBOX; |
| 233 | case Text: |
| 234 | case RichText: |
| 235 | case File: |
| 236 | return FIELDTYPE_TEXTFIELD; |
| 237 | case Sign: |
| 238 | return FIELDTYPE_SIGNATURE; |
| 239 | default: |
| 240 | break; |
| 241 | } |
| 242 | return FIELDTYPE_UNKNOWN; |
| 243 | } |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 244 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 245 | CPDF_AAction CPDF_FormField::GetAdditionalAction() { |
| 246 | CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "AA"); |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 247 | return CPDF_AAction(pObj ? pObj->GetDict() : nullptr); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 248 | } |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 249 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 250 | CFX_WideString CPDF_FormField::GetAlternateName() { |
| 251 | CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "TU"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 252 | if (!pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 253 | return L""; |
| 254 | } |
| 255 | return pObj->GetUnicodeText(); |
| 256 | } |
| 257 | CFX_WideString CPDF_FormField::GetMappingName() { |
| 258 | CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "TM"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 259 | if (!pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 260 | return L""; |
| 261 | } |
| 262 | return pObj->GetUnicodeText(); |
| 263 | } |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 264 | uint32_t CPDF_FormField::GetFieldFlags() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 265 | CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "Ff"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 266 | if (!pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | return pObj->GetInteger(); |
| 270 | } |
| 271 | CFX_ByteString CPDF_FormField::GetDefaultStyle() { |
| 272 | CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "DS"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 273 | if (!pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 274 | return ""; |
| 275 | } |
| 276 | return pObj->GetString(); |
| 277 | } |
| 278 | CFX_WideString CPDF_FormField::GetRichTextString() { |
| 279 | CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "RV"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 280 | if (!pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 281 | return L""; |
| 282 | } |
| 283 | return pObj->GetUnicodeText(); |
| 284 | } |
| 285 | CFX_WideString CPDF_FormField::GetValue(FX_BOOL bDefault) { |
| 286 | if (GetType() == CheckBox || GetType() == RadioButton) { |
| 287 | return GetCheckValue(bDefault); |
| 288 | } |
| 289 | CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, bDefault ? "DV" : "V"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 290 | if (!pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 291 | if (!bDefault) { |
| 292 | if (m_Type == RichText) { |
| 293 | pValue = FPDF_GetFieldAttr(m_pDict, "V"); |
| 294 | } |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 295 | if (!pValue && m_Type != Text) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 296 | pValue = FPDF_GetFieldAttr(m_pDict, "DV"); |
| 297 | } |
| 298 | } |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 299 | if (!pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 300 | return CFX_WideString(); |
| 301 | } |
| 302 | } |
| 303 | switch (pValue->GetType()) { |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 304 | case CPDF_Object::STRING: |
| 305 | case CPDF_Object::STREAM: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 306 | return pValue->GetUnicodeText(); |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 307 | case CPDF_Object::ARRAY: |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 308 | pValue = pValue->AsArray()->GetDirectObjectAt(0); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 309 | if (pValue) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 310 | return pValue->GetUnicodeText(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 311 | break; |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 312 | default: |
| 313 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 314 | } |
| 315 | return CFX_WideString(); |
| 316 | } |
| 317 | CFX_WideString CPDF_FormField::GetValue() { |
| 318 | return GetValue(FALSE); |
| 319 | } |
| 320 | CFX_WideString CPDF_FormField::GetDefaultValue() { |
| 321 | return GetValue(TRUE); |
| 322 | } |
| 323 | FX_BOOL CPDF_FormField::SetValue(const CFX_WideString& value, |
| 324 | FX_BOOL bDefault, |
| 325 | FX_BOOL bNotify) { |
| 326 | switch (m_Type) { |
| 327 | case CheckBox: |
| 328 | case RadioButton: { |
| 329 | SetCheckValue(value, bDefault, bNotify); |
| 330 | return TRUE; |
| 331 | } |
| 332 | case File: |
| 333 | case RichText: |
| 334 | case Text: |
| 335 | case ComboBox: { |
| 336 | CFX_WideString csValue = value; |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 337 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 338 | int iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue); |
| 339 | if (iRet < 0) { |
| 340 | return FALSE; |
| 341 | } |
| 342 | } |
| 343 | int iIndex = FindOptionValue(csValue); |
| 344 | if (iIndex < 0) { |
| 345 | CFX_ByteString bsEncodeText = PDF_EncodeText(csValue); |
| 346 | m_pDict->SetAtString(bDefault ? "DV" : "V", bsEncodeText); |
| 347 | if (m_Type == RichText && !bDefault) { |
| 348 | m_pDict->SetAtString("RV", bsEncodeText); |
| 349 | } |
| 350 | m_pDict->RemoveAt("I"); |
| 351 | } else { |
| 352 | m_pDict->SetAtString(bDefault ? "DV" : "V", PDF_EncodeText(csValue)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 353 | if (bDefault) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 354 | } else { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 355 | ClearSelection(); |
| 356 | SetItemSelection(iIndex, TRUE); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 357 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 358 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 359 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 360 | m_pForm->m_pFormNotify->AfterValueChange(this); |
| 361 | } |
| 362 | m_pForm->m_bUpdated = TRUE; |
| 363 | } break; |
| 364 | case ListBox: { |
| 365 | int iIndex = FindOptionValue(value); |
| 366 | if (iIndex < 0) { |
| 367 | return FALSE; |
| 368 | } |
| 369 | if (bDefault && iIndex == GetDefaultSelectedItem()) { |
| 370 | return FALSE; |
| 371 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 372 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 373 | CFX_WideString csValue = value; |
| 374 | int iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue); |
| 375 | if (iRet < 0) { |
| 376 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 377 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 378 | } |
| 379 | if (bDefault) { |
| 380 | } else { |
| 381 | ClearSelection(); |
| 382 | SetItemSelection(iIndex, TRUE); |
| 383 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 384 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 385 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
| 386 | } |
| 387 | m_pForm->m_bUpdated = TRUE; |
| 388 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 389 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 390 | default: |
| 391 | break; |
| 392 | } |
| 393 | if (CPDF_InterForm::m_bUpdateAP) { |
| 394 | UpdateAP(NULL); |
| 395 | } |
| 396 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 397 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 398 | FX_BOOL CPDF_FormField::SetValue(const CFX_WideString& value, FX_BOOL bNotify) { |
| 399 | return SetValue(value, FALSE, bNotify); |
| 400 | } |
| 401 | int CPDF_FormField::GetMaxLen() { |
Tom Sepez | bd573f1 | 2015-12-09 16:39:40 -0800 | [diff] [blame] | 402 | if (CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "MaxLen")) |
| 403 | return pObj->GetInteger(); |
| 404 | |
| 405 | for (int i = 0; i < m_ControlList.GetSize(); i++) { |
| 406 | CPDF_FormControl* pControl = m_ControlList.GetAt(i); |
| 407 | if (!pControl) |
| 408 | continue; |
| 409 | |
| 410 | CPDF_Dictionary* pWidgetDict = pControl->m_pWidgetDict; |
| 411 | if (pWidgetDict->KeyExist("MaxLen")) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 412 | return pWidgetDict->GetIntegerBy("MaxLen"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 413 | } |
Tom Sepez | bd573f1 | 2015-12-09 16:39:40 -0800 | [diff] [blame] | 414 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 415 | } |
| 416 | int CPDF_FormField::CountSelectedItems() { |
| 417 | CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V"); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 418 | if (!pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 419 | pValue = FPDF_GetFieldAttr(m_pDict, "I"); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 420 | if (!pValue) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 421 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 422 | } |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 423 | |
| 424 | if (pValue->IsString() || pValue->IsNumber()) |
| 425 | return pValue->GetString().IsEmpty() ? 0 : 1; |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 426 | if (CPDF_Array* pArray = pValue->AsArray()) |
| 427 | return pArray->GetCount(); |
| 428 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 429 | } |
| 430 | int CPDF_FormField::GetSelectedIndex(int index) { |
| 431 | CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V"); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 432 | if (!pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 433 | pValue = FPDF_GetFieldAttr(m_pDict, "I"); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 434 | if (!pValue) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 435 | return -1; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 436 | } |
Dan Sinclair | 43ce903 | 2015-10-21 11:07:42 -0400 | [diff] [blame] | 437 | if (pValue->IsNumber()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 438 | return pValue->GetInteger(); |
Dan Sinclair | 43ce903 | 2015-10-21 11:07:42 -0400 | [diff] [blame] | 439 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 440 | CFX_WideString sel_value; |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 441 | if (pValue->IsString()) { |
| 442 | if (index != 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 443 | return -1; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 444 | sel_value = pValue->GetUnicodeText(); |
| 445 | } else { |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 446 | CPDF_Array* pArray = pValue->AsArray(); |
| 447 | if (!pArray || index < 0) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 448 | return -1; |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 449 | |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 450 | CPDF_Object* elementValue = pArray->GetDirectObjectAt(index); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 451 | sel_value = |
| 452 | elementValue ? elementValue->GetUnicodeText() : CFX_WideString(); |
| 453 | } |
| 454 | if (index < CountSelectedOptions()) { |
| 455 | int iOptIndex = GetSelectedOptionIndex(index); |
| 456 | CFX_WideString csOpt = GetOptionValue(iOptIndex); |
| 457 | if (csOpt == sel_value) { |
| 458 | return iOptIndex; |
| 459 | } |
| 460 | } |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 461 | for (int i = 0; i < CountOptions(); i++) { |
| 462 | if (sel_value == GetOptionValue(i)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 463 | return i; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 464 | } |
| 465 | return -1; |
| 466 | } |
| 467 | FX_BOOL CPDF_FormField::ClearSelection(FX_BOOL bNotify) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 468 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 469 | int iRet = 0; |
| 470 | CFX_WideString csValue; |
| 471 | int iIndex = GetSelectedIndex(0); |
| 472 | if (iIndex >= 0) { |
| 473 | csValue = GetOptionLabel(iIndex); |
| 474 | } |
| 475 | if (GetType() == ListBox) { |
| 476 | iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue); |
| 477 | } |
| 478 | if (GetType() == ComboBox) { |
| 479 | iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue); |
| 480 | } |
| 481 | if (iRet < 0) { |
| 482 | return FALSE; |
| 483 | } |
| 484 | } |
| 485 | m_pDict->RemoveAt("V"); |
| 486 | m_pDict->RemoveAt("I"); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 487 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 488 | if (GetType() == ListBox) { |
| 489 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
| 490 | } |
| 491 | if (GetType() == ComboBox) { |
| 492 | m_pForm->m_pFormNotify->AfterValueChange(this); |
| 493 | } |
| 494 | } |
| 495 | if (CPDF_InterForm::m_bUpdateAP) { |
| 496 | UpdateAP(NULL); |
| 497 | } |
| 498 | m_pForm->m_bUpdated = TRUE; |
| 499 | return TRUE; |
| 500 | } |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 501 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 502 | FX_BOOL CPDF_FormField::IsItemSelected(int index) { |
| 503 | ASSERT(GetType() == ComboBox || GetType() == ListBox); |
| 504 | if (index < 0 || index >= CountOptions()) { |
| 505 | return FALSE; |
| 506 | } |
| 507 | if (IsOptionSelected(index)) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 508 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 509 | } |
| 510 | CFX_WideString opt_value = GetOptionValue(index); |
| 511 | CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 512 | if (!pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 513 | pValue = FPDF_GetFieldAttr(m_pDict, "I"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 514 | if (!pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 515 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 516 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 517 | } |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 518 | |
| 519 | if (pValue->IsString()) |
Dan Sinclair | 710c909 | 2015-10-21 15:46:10 -0400 | [diff] [blame] | 520 | return pValue->GetUnicodeText() == opt_value; |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 521 | |
Dan Sinclair | 43ce903 | 2015-10-21 11:07:42 -0400 | [diff] [blame] | 522 | if (pValue->IsNumber()) { |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 523 | if (pValue->GetString().IsEmpty()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 524 | return FALSE; |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 525 | return (pValue->GetInteger() == index); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 526 | } |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 527 | |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 528 | CPDF_Array* pArray = pValue->AsArray(); |
| 529 | if (!pArray) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 530 | return FALSE; |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 531 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 532 | int iPos = -1; |
| 533 | for (int j = 0; j < CountSelectedOptions(); j++) { |
| 534 | if (GetSelectedOptionIndex(j) == index) { |
| 535 | iPos = j; |
| 536 | break; |
| 537 | } |
| 538 | } |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 539 | for (uint32_t i = 0; i < pArray->GetCount(); i++) |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 540 | if (pArray->GetDirectObjectAt(i)->GetUnicodeText() == opt_value && |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 541 | (int)i == iPos) { |
| 542 | return TRUE; |
| 543 | } |
| 544 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 545 | } |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 546 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 547 | FX_BOOL CPDF_FormField::SetItemSelection(int index, |
| 548 | FX_BOOL bSelected, |
| 549 | FX_BOOL bNotify) { |
| 550 | ASSERT(GetType() == ComboBox || GetType() == ListBox); |
| 551 | if (index < 0 || index >= CountOptions()) { |
| 552 | return FALSE; |
| 553 | } |
| 554 | CFX_WideString opt_value = GetOptionValue(index); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 555 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 556 | int iRet = 0; |
| 557 | if (GetType() == ListBox) { |
| 558 | iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, opt_value); |
| 559 | } |
| 560 | if (GetType() == ComboBox) { |
| 561 | iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, opt_value); |
| 562 | } |
| 563 | if (iRet < 0) { |
| 564 | return FALSE; |
| 565 | } |
| 566 | } |
| 567 | if (!bSelected) { |
| 568 | CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V"); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 569 | if (pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 570 | if (m_Type == ListBox) { |
| 571 | SelectOption(index, FALSE); |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 572 | if (pValue->IsString()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 573 | if (pValue->GetUnicodeText() == opt_value) { |
| 574 | m_pDict->RemoveAt("V"); |
| 575 | } |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 576 | } else if (pValue->IsArray()) { |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 577 | CPDF_Array* pArray = new CPDF_Array; |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 578 | for (int i = 0; i < CountOptions(); i++) { |
| 579 | if (i != index && IsItemSelected(i)) { |
| 580 | opt_value = GetOptionValue(i); |
| 581 | pArray->AddString(PDF_EncodeText(opt_value)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 582 | } |
| 583 | } |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 584 | if (pArray->GetCount() < 1) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 585 | pArray->Release(); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 586 | else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 587 | m_pDict->SetAt("V", pArray); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 588 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 589 | } else if (m_Type == ComboBox) { |
| 590 | m_pDict->RemoveAt("V"); |
| 591 | m_pDict->RemoveAt("I"); |
| 592 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 593 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 594 | } else { |
| 595 | if (m_Type == ListBox) { |
| 596 | SelectOption(index, TRUE); |
| 597 | if (!(m_Flags & FORMLIST_MULTISELECT)) { |
| 598 | m_pDict->SetAtString("V", PDF_EncodeText(opt_value)); |
| 599 | } else { |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 600 | CPDF_Array* pArray = new CPDF_Array; |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 601 | for (int i = 0; i < CountOptions(); i++) { |
| 602 | if (i == index || IsItemSelected(i)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 603 | opt_value = GetOptionValue(i); |
| 604 | pArray->AddString(PDF_EncodeText(opt_value)); |
| 605 | } |
| 606 | } |
| 607 | m_pDict->SetAt("V", pArray); |
| 608 | } |
| 609 | } else if (m_Type == ComboBox) { |
| 610 | m_pDict->SetAtString("V", PDF_EncodeText(opt_value)); |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 611 | CPDF_Array* pI = new CPDF_Array; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 612 | pI->AddInteger(index); |
| 613 | m_pDict->SetAt("I", pI); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 614 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 615 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 616 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 617 | if (GetType() == ListBox) { |
| 618 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 619 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 620 | if (GetType() == ComboBox) { |
| 621 | m_pForm->m_pFormNotify->AfterValueChange(this); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 622 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 623 | } |
| 624 | if (CPDF_InterForm::m_bUpdateAP) { |
| 625 | UpdateAP(NULL); |
| 626 | } |
| 627 | m_pForm->m_bUpdated = TRUE; |
| 628 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 629 | } |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 630 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 631 | FX_BOOL CPDF_FormField::IsItemDefaultSelected(int index) { |
| 632 | ASSERT(GetType() == ComboBox || GetType() == ListBox); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 633 | if (index < 0 || index >= CountOptions()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 634 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 635 | int iDVIndex = GetDefaultSelectedItem(); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 636 | return iDVIndex >= 0 && iDVIndex == index; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 637 | } |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 638 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 639 | int CPDF_FormField::GetDefaultSelectedItem() { |
| 640 | ASSERT(GetType() == ComboBox || GetType() == ListBox); |
| 641 | CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "DV"); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 642 | if (!pValue) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 643 | return -1; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 644 | CFX_WideString csDV = pValue->GetUnicodeText(); |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 645 | if (csDV.IsEmpty()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 646 | return -1; |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 647 | for (int i = 0; i < CountOptions(); i++) { |
| 648 | if (csDV == GetOptionValue(i)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 649 | return i; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 650 | } |
| 651 | return -1; |
| 652 | } |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 653 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 654 | void CPDF_FormField::UpdateAP(CPDF_FormControl* pControl) { |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 655 | if (m_Type == PushButton || m_Type == RadioButton || m_Type == CheckBox) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 656 | return; |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 657 | if (!m_pForm->m_bGenerateAP) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 658 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 659 | for (int i = 0; i < CountControls(); i++) { |
| 660 | CPDF_FormControl* pControl = GetControl(i); |
| 661 | FPDF_GenerateAP(m_pForm->m_pDocument, pControl->m_pWidgetDict); |
| 662 | } |
| 663 | } |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 664 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 665 | int CPDF_FormField::CountOptions() { |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 666 | CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict, "Opt")); |
| 667 | return pArray ? pArray->GetCount() : 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 668 | } |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 669 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 670 | CFX_WideString CPDF_FormField::GetOptionText(int index, int sub_index) { |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 671 | CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict, "Opt")); |
| 672 | if (!pArray) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 673 | return CFX_WideString(); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 674 | |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 675 | CPDF_Object* pOption = pArray->GetDirectObjectAt(index); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 676 | if (!pOption) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 677 | return CFX_WideString(); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 678 | if (CPDF_Array* pOptionArray = pOption->AsArray()) |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 679 | pOption = pOptionArray->GetDirectObjectAt(sub_index); |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 680 | |
| 681 | CPDF_String* pString = ToString(pOption); |
| 682 | return pString ? pString->GetUnicodeText() : CFX_WideString(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 683 | } |
| 684 | CFX_WideString CPDF_FormField::GetOptionLabel(int index) { |
| 685 | return GetOptionText(index, 1); |
| 686 | } |
| 687 | CFX_WideString CPDF_FormField::GetOptionValue(int index) { |
| 688 | return GetOptionText(index, 0); |
| 689 | } |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 690 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 691 | int CPDF_FormField::FindOption(CFX_WideString csOptLabel) { |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 692 | for (int i = 0; i < CountOptions(); i++) { |
| 693 | if (GetOptionValue(i) == csOptLabel) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 694 | return i; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 695 | } |
| 696 | return -1; |
| 697 | } |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 698 | |
| 699 | int CPDF_FormField::FindOptionValue(const CFX_WideString& csOptValue) { |
| 700 | for (int i = 0; i < CountOptions(); i++) { |
| 701 | if (GetOptionValue(i) == csOptValue) |
| 702 | return i; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 703 | } |
| 704 | return -1; |
| 705 | } |
Wei Li | 05d53f0 | 2016-03-29 16:42:53 -0700 | [diff] [blame^] | 706 | |
Tom Sepez | 5c4c193 | 2015-11-25 12:15:38 -0800 | [diff] [blame] | 707 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 708 | int CPDF_FormField::InsertOption(CFX_WideString csOptLabel, |
| 709 | int index, |
| 710 | FX_BOOL bNotify) { |
| 711 | if (csOptLabel.IsEmpty()) |
| 712 | return -1; |
| 713 | |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 714 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 715 | int iRet = 0; |
| 716 | if (GetType() == ListBox) |
| 717 | iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csOptLabel); |
| 718 | if (GetType() == ComboBox) |
| 719 | iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csOptLabel); |
| 720 | if (iRet < 0) |
| 721 | return -1; |
| 722 | } |
| 723 | |
| 724 | CFX_ByteString csStr = PDF_EncodeText(csOptLabel, csOptLabel.GetLength()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 725 | CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "Opt"); |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 726 | CPDF_Array* pOpt = ToArray(pValue); |
| 727 | if (!pOpt) { |
| 728 | pOpt = new CPDF_Array; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 729 | m_pDict->SetAt("Opt", pOpt); |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 732 | int iCount = (int)pOpt->GetCount(); |
| 733 | if (index < 0 || index >= iCount) { |
| 734 | pOpt->AddString(csStr); |
| 735 | index = iCount; |
| 736 | } else { |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 737 | CPDF_String* pString = new CPDF_String(csStr, FALSE); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 738 | pOpt->InsertAt(index, pString); |
| 739 | } |
| 740 | |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 741 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 742 | if (GetType() == ListBox) |
| 743 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
| 744 | if (GetType() == ComboBox) |
| 745 | m_pForm->m_pFormNotify->AfterValueChange(this); |
| 746 | } |
| 747 | m_pForm->m_bUpdated = TRUE; |
| 748 | return index; |
| 749 | } |
| 750 | FX_BOOL CPDF_FormField::ClearOptions(FX_BOOL bNotify) { |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 751 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 752 | int iRet = 0; |
| 753 | CFX_WideString csValue; |
| 754 | int iIndex = GetSelectedIndex(0); |
| 755 | if (iIndex >= 0) |
| 756 | csValue = GetOptionLabel(iIndex); |
| 757 | if (GetType() == ListBox) |
| 758 | iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue); |
| 759 | if (GetType() == ComboBox) |
| 760 | iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue); |
| 761 | if (iRet < 0) |
| 762 | return FALSE; |
| 763 | } |
| 764 | |
| 765 | m_pDict->RemoveAt("Opt"); |
| 766 | m_pDict->RemoveAt("V"); |
| 767 | m_pDict->RemoveAt("DV"); |
| 768 | m_pDict->RemoveAt("I"); |
| 769 | m_pDict->RemoveAt("TI"); |
| 770 | |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 771 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 772 | if (GetType() == ListBox) |
| 773 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
| 774 | if (GetType() == ComboBox) |
| 775 | m_pForm->m_pFormNotify->AfterValueChange(this); |
| 776 | } |
| 777 | |
| 778 | m_pForm->m_bUpdated = TRUE; |
| 779 | return TRUE; |
| 780 | } |
Tom Sepez | a2c42ce | 2015-11-25 15:52:28 -0800 | [diff] [blame] | 781 | #endif // PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 782 | FX_BOOL CPDF_FormField::CheckControl(int iControlIndex, |
Tom Sepez | ed5d7aa | 2016-02-02 16:02:03 -0800 | [diff] [blame] | 783 | bool bChecked, |
| 784 | bool bNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 785 | ASSERT(GetType() == CheckBox || GetType() == RadioButton); |
| 786 | CPDF_FormControl* pControl = GetControl(iControlIndex); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 787 | if (!pControl) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 788 | return FALSE; |
| 789 | } |
| 790 | if (!bChecked && pControl->IsChecked() == bChecked) { |
| 791 | return FALSE; |
| 792 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 793 | CFX_WideString csWExport = pControl->GetExportValue(); |
| 794 | CFX_ByteString csBExport = PDF_EncodeText(csWExport); |
| 795 | int iCount = CountControls(); |
| 796 | FX_BOOL bUnison = PDF_FormField_IsUnison(this); |
| 797 | for (int i = 0; i < iCount; i++) { |
| 798 | CPDF_FormControl* pCtrl = GetControl(i); |
| 799 | if (bUnison) { |
| 800 | CFX_WideString csEValue = pCtrl->GetExportValue(); |
| 801 | if (csEValue == csWExport) { |
| 802 | if (pCtrl->GetOnStateName() == pControl->GetOnStateName()) { |
| 803 | pCtrl->CheckControl(bChecked); |
| 804 | } else if (bChecked) { |
| 805 | pCtrl->CheckControl(FALSE); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 806 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 807 | } else if (bChecked) { |
| 808 | pCtrl->CheckControl(FALSE); |
| 809 | } |
| 810 | } else { |
| 811 | if (i == iControlIndex) { |
| 812 | pCtrl->CheckControl(bChecked); |
| 813 | } else if (bChecked) { |
| 814 | pCtrl->CheckControl(FALSE); |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | CPDF_Object* pOpt = FPDF_GetFieldAttr(m_pDict, "Opt"); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 819 | if (!ToArray(pOpt)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 820 | if (bChecked) { |
| 821 | m_pDict->SetAtName("V", csBExport); |
| 822 | } else { |
| 823 | CFX_ByteString csV; |
| 824 | CPDF_Object* pV = FPDF_GetFieldAttr(m_pDict, "V"); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 825 | if (pV) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 826 | csV = pV->GetString(); |
| 827 | } |
| 828 | if (csV == csBExport) { |
| 829 | m_pDict->SetAtName("V", "Off"); |
| 830 | } |
| 831 | } |
| 832 | } else if (bChecked) { |
| 833 | CFX_ByteString csIndex; |
| 834 | csIndex.Format("%d", iControlIndex); |
| 835 | m_pDict->SetAtName("V", csIndex); |
| 836 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 837 | if (bNotify && m_pForm->m_pFormNotify) { |
Tom Sepez | ed5d7aa | 2016-02-02 16:02:03 -0800 | [diff] [blame] | 838 | m_pForm->m_pFormNotify->AfterCheckedStatusChange(this); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 839 | } |
| 840 | m_pForm->m_bUpdated = TRUE; |
| 841 | return TRUE; |
| 842 | } |
| 843 | CFX_WideString CPDF_FormField::GetCheckValue(FX_BOOL bDefault) { |
| 844 | ASSERT(GetType() == CheckBox || GetType() == RadioButton); |
| 845 | CFX_WideString csExport = L"Off"; |
| 846 | FX_BOOL bChecked; |
| 847 | int iCount = CountControls(); |
| 848 | for (int i = 0; i < iCount; i++) { |
| 849 | CPDF_FormControl* pControl = GetControl(i); |
| 850 | if (bDefault) { |
| 851 | bChecked = pControl->IsDefaultChecked(); |
| 852 | } else { |
| 853 | bChecked = pControl->IsChecked(); |
| 854 | } |
| 855 | if (bChecked) { |
| 856 | csExport = pControl->GetExportValue(); |
| 857 | break; |
| 858 | } |
| 859 | } |
| 860 | return csExport; |
| 861 | } |
| 862 | FX_BOOL CPDF_FormField::SetCheckValue(const CFX_WideString& value, |
| 863 | FX_BOOL bDefault, |
| 864 | FX_BOOL bNotify) { |
| 865 | ASSERT(GetType() == CheckBox || GetType() == RadioButton); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 866 | int iCount = CountControls(); |
| 867 | for (int i = 0; i < iCount; i++) { |
| 868 | CPDF_FormControl* pControl = GetControl(i); |
| 869 | CFX_WideString csExport = pControl->GetExportValue(); |
| 870 | if (csExport == value) { |
| 871 | if (bDefault) { |
| 872 | } else { |
| 873 | CheckControl(GetControlIndex(pControl), TRUE); |
| 874 | } |
| 875 | break; |
| 876 | } else { |
| 877 | if (bDefault) { |
| 878 | } else { |
| 879 | CheckControl(GetControlIndex(pControl), FALSE); |
| 880 | } |
| 881 | } |
| 882 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 883 | if (bNotify && m_pForm->m_pFormNotify) { |
Tom Sepez | ed5d7aa | 2016-02-02 16:02:03 -0800 | [diff] [blame] | 884 | m_pForm->m_pFormNotify->AfterCheckedStatusChange(this); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 885 | } |
| 886 | m_pForm->m_bUpdated = TRUE; |
| 887 | return TRUE; |
| 888 | } |
| 889 | int CPDF_FormField::GetTopVisibleIndex() { |
| 890 | CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "TI"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 891 | if (!pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 892 | return 0; |
| 893 | } |
| 894 | return pObj->GetInteger(); |
| 895 | } |
| 896 | int CPDF_FormField::CountSelectedOptions() { |
| 897 | CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "I"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 898 | if (!pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 899 | return 0; |
| 900 | } |
| 901 | CPDF_Array* pArray = pObj->GetArray(); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 902 | if (!pArray) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 903 | return 0; |
| 904 | } |
| 905 | return (int)pArray->GetCount(); |
| 906 | } |
| 907 | int CPDF_FormField::GetSelectedOptionIndex(int index) { |
| 908 | CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "I"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 909 | if (!pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 910 | return -1; |
| 911 | } |
| 912 | CPDF_Array* pArray = pObj->GetArray(); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 913 | if (!pArray) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 914 | return -1; |
| 915 | } |
| 916 | int iCount = (int)pArray->GetCount(); |
| 917 | if (iCount > 0 && index < iCount) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 918 | return pArray->GetIntegerAt(index); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 919 | } |
| 920 | return -1; |
| 921 | } |
| 922 | FX_BOOL CPDF_FormField::IsOptionSelected(int iOptIndex) { |
| 923 | CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "I"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 924 | if (!pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 925 | return FALSE; |
| 926 | } |
| 927 | CPDF_Array* pArray = pObj->GetArray(); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 928 | if (!pArray) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 929 | return FALSE; |
| 930 | } |
| 931 | int iCount = (int)pArray->GetCount(); |
| 932 | for (int i = 0; i < iCount; i++) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 933 | if (pArray->GetIntegerAt(i) == iOptIndex) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 934 | return TRUE; |
| 935 | } |
| 936 | } |
| 937 | return FALSE; |
| 938 | } |
| 939 | FX_BOOL CPDF_FormField::SelectOption(int iOptIndex, |
| 940 | FX_BOOL bSelected, |
| 941 | FX_BOOL bNotify) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 942 | CPDF_Array* pArray = m_pDict->GetArrayBy("I"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 943 | if (!pArray) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 944 | if (!bSelected) { |
| 945 | return TRUE; |
| 946 | } |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 947 | pArray = new CPDF_Array; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 948 | m_pDict->SetAt("I", pArray); |
| 949 | } |
| 950 | FX_BOOL bReturn = FALSE; |
| 951 | for (int i = 0; i < (int)pArray->GetCount(); i++) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 952 | int iFind = pArray->GetIntegerAt(i); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 953 | if (iFind == iOptIndex) { |
| 954 | if (bSelected) { |
| 955 | return TRUE; |
| 956 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 957 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 958 | int iRet = 0; |
| 959 | CFX_WideString csValue = GetOptionLabel(iOptIndex); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 960 | if (GetType() == ListBox) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 961 | iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 962 | } |
| 963 | if (GetType() == ComboBox) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 964 | iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 965 | } |
| 966 | if (iRet < 0) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 967 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 968 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 969 | } |
| 970 | pArray->RemoveAt(i); |
| 971 | bReturn = TRUE; |
| 972 | break; |
| 973 | } else if (iFind > iOptIndex) { |
| 974 | if (!bSelected) { |
| 975 | continue; |
| 976 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 977 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 978 | int iRet = 0; |
| 979 | CFX_WideString csValue = GetOptionLabel(iOptIndex); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 980 | if (GetType() == ListBox) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 981 | iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 982 | } |
| 983 | if (GetType() == ComboBox) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 984 | iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 985 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 986 | if (iRet < 0) { |
| 987 | return FALSE; |
| 988 | } |
| 989 | } |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 990 | CPDF_Number* pNum = new CPDF_Number(iOptIndex); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 991 | pArray->InsertAt(i, pNum); |
| 992 | bReturn = TRUE; |
| 993 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 994 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 995 | } |
| 996 | if (!bReturn) { |
| 997 | if (bSelected) { |
| 998 | pArray->AddInteger(iOptIndex); |
| 999 | } |
| 1000 | if (pArray->GetCount() == 0) { |
| 1001 | m_pDict->RemoveAt("I"); |
| 1002 | } |
| 1003 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 1004 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1005 | if (GetType() == ListBox) { |
| 1006 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
| 1007 | } |
| 1008 | if (GetType() == ComboBox) { |
| 1009 | m_pForm->m_pFormNotify->AfterValueChange(this); |
| 1010 | } |
| 1011 | } |
| 1012 | m_pForm->m_bUpdated = TRUE; |
| 1013 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1014 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1015 | FX_BOOL CPDF_FormField::ClearSelectedOptions(FX_BOOL bNotify) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 1016 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1017 | int iRet = 0; |
| 1018 | CFX_WideString csValue; |
| 1019 | int iIndex = GetSelectedIndex(0); |
| 1020 | if (iIndex >= 0) { |
| 1021 | csValue = GetOptionLabel(iIndex); |
Bo Xu | 22b31e0 | 2014-07-21 10:02:07 -0700 | [diff] [blame] | 1022 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1023 | if (GetType() == ListBox) { |
| 1024 | iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1025 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1026 | if (GetType() == ComboBox) { |
| 1027 | iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1028 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1029 | if (iRet < 0) { |
| 1030 | return FALSE; |
| 1031 | } |
| 1032 | } |
| 1033 | m_pDict->RemoveAt("I"); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 1034 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1035 | if (GetType() == ListBox) { |
| 1036 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
| 1037 | } |
| 1038 | if (GetType() == ComboBox) { |
| 1039 | m_pForm->m_pFormNotify->AfterValueChange(this); |
| 1040 | } |
| 1041 | } |
| 1042 | m_pForm->m_bUpdated = TRUE; |
| 1043 | return TRUE; |
| 1044 | } |
| 1045 | void CPDF_FormField::LoadDA() { |
| 1046 | CFX_ByteString DA; |
| 1047 | if (CPDF_Object* pObj_t = FPDF_GetFieldAttr(m_pDict, "DA")) { |
| 1048 | DA = pObj_t->GetString(); |
| 1049 | } |
| 1050 | if (DA.IsEmpty() && m_pForm->m_pFormDict) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 1051 | DA = m_pForm->m_pFormDict->GetStringBy("DA"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1052 | } |
| 1053 | if (DA.IsEmpty()) { |
| 1054 | return; |
| 1055 | } |
| 1056 | CPDF_SimpleParser syntax(DA); |
Wei Li | 970c11e | 2016-02-16 14:26:22 -0800 | [diff] [blame] | 1057 | syntax.FindTagParamFromStart("Tf", 2); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1058 | CFX_ByteString font_name = syntax.GetWord(); |
| 1059 | CPDF_Dictionary* pFontDict = NULL; |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 1060 | if (m_pForm->m_pFormDict && m_pForm->m_pFormDict->GetDictBy("DR") && |
| 1061 | m_pForm->m_pFormDict->GetDictBy("DR")->GetDictBy("Font")) |
| 1062 | pFontDict = m_pForm->m_pFormDict->GetDictBy("DR") |
| 1063 | ->GetDictBy("Font") |
| 1064 | ->GetDictBy(font_name); |
Bo Xu | 287e11a | 2014-06-30 09:49:21 -0700 | [diff] [blame] | 1065 | |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 1066 | if (!pFontDict) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1067 | return; |
| 1068 | } |
| 1069 | m_pFont = m_pForm->m_pDocument->LoadFont(pFontDict); |
| 1070 | m_FontSize = FX_atof(syntax.GetWord()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1071 | } |