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 | 764ec51 | 2016-03-14 13:35:12 -0400 | [diff] [blame] | 7 | #include "core/fpdfdoc/doc_utils.h" |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 8 | #include "core/include/fpdfapi/cfdf_document.h" |
| 9 | #include "core/include/fpdfapi/cpdf_array.h" |
Tom Sepez | 310438f | 2016-03-08 13:10:55 -0800 | [diff] [blame] | 10 | #include "core/include/fpdfapi/cpdf_document.h" |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 11 | #include "core/include/fpdfapi/cpdf_number.h" |
Tom Sepez | 8388037 | 2016-03-09 10:47:45 -0800 | [diff] [blame] | 12 | #include "core/include/fpdfapi/cpdf_simple_parser.h" |
Tom Sepez | 5fc239a | 2016-03-10 14:10:38 -0800 | [diff] [blame] | 13 | #include "core/include/fpdfapi/cpdf_string.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 { |
| 21 | FX_DWORD dwFlags = pField->GetFieldFlags(); |
| 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(); |
| 39 | FX_DWORD flags = FPDF_GetFieldAttr(m_pDict, "Ff") |
| 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 | } |
| 264 | FX_DWORD CPDF_FormField::GetFieldFlags() { |
| 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: |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 308 | pValue = pValue->AsArray()->GetElementValue(0); |
| 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 | |
| 450 | CPDF_Object* elementValue = pArray->GetElementValue(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 | } |
| 461 | int nOpts = CountOptions(); |
| 462 | for (int i = 0; i < nOpts; i++) { |
| 463 | if (sel_value == GetOptionValue(i)) { |
| 464 | return i; |
| 465 | } |
| 466 | } |
| 467 | return -1; |
| 468 | } |
| 469 | FX_BOOL CPDF_FormField::ClearSelection(FX_BOOL bNotify) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 470 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 471 | int iRet = 0; |
| 472 | CFX_WideString csValue; |
| 473 | int iIndex = GetSelectedIndex(0); |
| 474 | if (iIndex >= 0) { |
| 475 | csValue = GetOptionLabel(iIndex); |
| 476 | } |
| 477 | if (GetType() == ListBox) { |
| 478 | iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue); |
| 479 | } |
| 480 | if (GetType() == ComboBox) { |
| 481 | iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue); |
| 482 | } |
| 483 | if (iRet < 0) { |
| 484 | return FALSE; |
| 485 | } |
| 486 | } |
| 487 | m_pDict->RemoveAt("V"); |
| 488 | m_pDict->RemoveAt("I"); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 489 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 490 | if (GetType() == ListBox) { |
| 491 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
| 492 | } |
| 493 | if (GetType() == ComboBox) { |
| 494 | m_pForm->m_pFormNotify->AfterValueChange(this); |
| 495 | } |
| 496 | } |
| 497 | if (CPDF_InterForm::m_bUpdateAP) { |
| 498 | UpdateAP(NULL); |
| 499 | } |
| 500 | m_pForm->m_bUpdated = TRUE; |
| 501 | return TRUE; |
| 502 | } |
| 503 | FX_BOOL CPDF_FormField::IsItemSelected(int index) { |
| 504 | ASSERT(GetType() == ComboBox || GetType() == ListBox); |
| 505 | if (index < 0 || index >= CountOptions()) { |
| 506 | return FALSE; |
| 507 | } |
| 508 | if (IsOptionSelected(index)) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 509 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 510 | } |
| 511 | CFX_WideString opt_value = GetOptionValue(index); |
| 512 | CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "V"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 513 | if (!pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 514 | pValue = FPDF_GetFieldAttr(m_pDict, "I"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 515 | if (!pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 516 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 517 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 518 | } |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 519 | |
| 520 | if (pValue->IsString()) |
Dan Sinclair | 710c909 | 2015-10-21 15:46:10 -0400 | [diff] [blame] | 521 | return pValue->GetUnicodeText() == opt_value; |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 522 | |
Dan Sinclair | 43ce903 | 2015-10-21 11:07:42 -0400 | [diff] [blame] | 523 | if (pValue->IsNumber()) { |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 524 | if (pValue->GetString().IsEmpty()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 525 | return FALSE; |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 526 | return (pValue->GetInteger() == index); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 527 | } |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 528 | |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 529 | CPDF_Array* pArray = pValue->AsArray(); |
| 530 | if (!pArray) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 531 | return FALSE; |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 532 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 533 | int iPos = -1; |
| 534 | for (int j = 0; j < CountSelectedOptions(); j++) { |
| 535 | if (GetSelectedOptionIndex(j) == index) { |
| 536 | iPos = j; |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | for (FX_DWORD i = 0; i < pArray->GetCount(); i++) |
| 541 | if (pArray->GetElementValue(i)->GetUnicodeText() == opt_value && |
| 542 | (int)i == iPos) { |
| 543 | return TRUE; |
| 544 | } |
| 545 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -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; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 578 | int iCount = CountOptions(); |
| 579 | for (int i = 0; i < iCount; i++) { |
| 580 | if (i != index) { |
| 581 | if (IsItemSelected(i)) { |
| 582 | opt_value = GetOptionValue(i); |
| 583 | pArray->AddString(PDF_EncodeText(opt_value)); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | if (pArray->GetCount() < 1) { |
| 588 | pArray->Release(); |
| 589 | } else { |
| 590 | m_pDict->SetAt("V", pArray); |
| 591 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 592 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 593 | } else if (m_Type == ComboBox) { |
| 594 | m_pDict->RemoveAt("V"); |
| 595 | m_pDict->RemoveAt("I"); |
| 596 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 597 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 598 | } else { |
| 599 | if (m_Type == ListBox) { |
| 600 | SelectOption(index, TRUE); |
| 601 | if (!(m_Flags & FORMLIST_MULTISELECT)) { |
| 602 | m_pDict->SetAtString("V", PDF_EncodeText(opt_value)); |
| 603 | } else { |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 604 | CPDF_Array* pArray = new CPDF_Array; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 605 | int iCount = CountOptions(); |
| 606 | for (int i = 0; i < iCount; i++) { |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 607 | FX_BOOL bSelected; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 608 | if (i != index) { |
| 609 | bSelected = IsItemSelected(i); |
| 610 | } else { |
| 611 | bSelected = TRUE; |
| 612 | } |
| 613 | if (bSelected) { |
| 614 | opt_value = GetOptionValue(i); |
| 615 | pArray->AddString(PDF_EncodeText(opt_value)); |
| 616 | } |
| 617 | } |
| 618 | m_pDict->SetAt("V", pArray); |
| 619 | } |
| 620 | } else if (m_Type == ComboBox) { |
| 621 | m_pDict->SetAtString("V", PDF_EncodeText(opt_value)); |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 622 | CPDF_Array* pI = new CPDF_Array; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 623 | pI->AddInteger(index); |
| 624 | m_pDict->SetAt("I", pI); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 625 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 626 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 627 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 628 | if (GetType() == ListBox) { |
| 629 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 630 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 631 | if (GetType() == ComboBox) { |
| 632 | m_pForm->m_pFormNotify->AfterValueChange(this); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 633 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 634 | } |
| 635 | if (CPDF_InterForm::m_bUpdateAP) { |
| 636 | UpdateAP(NULL); |
| 637 | } |
| 638 | m_pForm->m_bUpdated = TRUE; |
| 639 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 640 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 641 | FX_BOOL CPDF_FormField::IsItemDefaultSelected(int index) { |
| 642 | ASSERT(GetType() == ComboBox || GetType() == ListBox); |
| 643 | if (index < 0 || index >= CountOptions()) { |
| 644 | return FALSE; |
| 645 | } |
| 646 | int iDVIndex = GetDefaultSelectedItem(); |
| 647 | if (iDVIndex < 0) { |
| 648 | return FALSE; |
| 649 | } |
| 650 | return (iDVIndex == index); |
| 651 | } |
| 652 | int CPDF_FormField::GetDefaultSelectedItem() { |
| 653 | ASSERT(GetType() == ComboBox || GetType() == ListBox); |
| 654 | CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "DV"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 655 | if (!pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 656 | return -1; |
| 657 | } |
| 658 | CFX_WideString csDV = pValue->GetUnicodeText(); |
| 659 | if (csDV.IsEmpty()) { |
| 660 | return -1; |
| 661 | } |
| 662 | int iCount = CountOptions(); |
| 663 | for (int i = 0; i < iCount; i++) { |
| 664 | if (csDV == GetOptionValue(i)) { |
| 665 | return i; |
| 666 | } |
| 667 | } |
| 668 | return -1; |
| 669 | } |
| 670 | void CPDF_FormField::UpdateAP(CPDF_FormControl* pControl) { |
| 671 | if (m_Type == PushButton) { |
| 672 | return; |
| 673 | } |
| 674 | if (m_Type == RadioButton || m_Type == CheckBox) { |
| 675 | return; |
| 676 | } |
| 677 | if (!m_pForm->m_bGenerateAP) { |
| 678 | return; |
| 679 | } |
| 680 | for (int i = 0; i < CountControls(); i++) { |
| 681 | CPDF_FormControl* pControl = GetControl(i); |
| 682 | FPDF_GenerateAP(m_pForm->m_pDocument, pControl->m_pWidgetDict); |
| 683 | } |
| 684 | } |
| 685 | int CPDF_FormField::CountOptions() { |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 686 | CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict, "Opt")); |
| 687 | return pArray ? pArray->GetCount() : 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 688 | } |
| 689 | CFX_WideString CPDF_FormField::GetOptionText(int index, int sub_index) { |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 690 | CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict, "Opt")); |
| 691 | if (!pArray) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 692 | return CFX_WideString(); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 693 | |
| 694 | CPDF_Object* pOption = pArray->GetElementValue(index); |
| 695 | if (!pOption) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 696 | return CFX_WideString(); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 697 | if (CPDF_Array* pOptionArray = pOption->AsArray()) |
| 698 | pOption = pOptionArray->GetElementValue(sub_index); |
Dan Sinclair | 316eb86 | 2015-10-21 13:29:23 -0400 | [diff] [blame] | 699 | |
| 700 | CPDF_String* pString = ToString(pOption); |
| 701 | return pString ? pString->GetUnicodeText() : CFX_WideString(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 702 | } |
| 703 | CFX_WideString CPDF_FormField::GetOptionLabel(int index) { |
| 704 | return GetOptionText(index, 1); |
| 705 | } |
| 706 | CFX_WideString CPDF_FormField::GetOptionValue(int index) { |
| 707 | return GetOptionText(index, 0); |
| 708 | } |
| 709 | int CPDF_FormField::FindOption(CFX_WideString csOptLabel) { |
| 710 | int iCount = CountOptions(); |
| 711 | for (int i = 0; i < iCount; i++) { |
| 712 | CFX_WideString csValue = GetOptionValue(i); |
| 713 | if (csValue == csOptLabel) { |
| 714 | return i; |
| 715 | } |
| 716 | } |
| 717 | return -1; |
| 718 | } |
| 719 | int CPDF_FormField::FindOptionValue(const CFX_WideString& csOptValue, |
| 720 | int iStartIndex) { |
| 721 | if (iStartIndex < 0) { |
| 722 | iStartIndex = 0; |
| 723 | } |
| 724 | int iCount = CountOptions(); |
| 725 | for (; iStartIndex < iCount; iStartIndex++) { |
| 726 | CFX_WideString csValue = GetOptionValue(iStartIndex); |
| 727 | if (csValue == csOptValue) { |
| 728 | return iStartIndex; |
| 729 | } |
| 730 | } |
| 731 | return -1; |
| 732 | } |
Tom Sepez | 5c4c193 | 2015-11-25 12:15:38 -0800 | [diff] [blame] | 733 | #ifdef PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 734 | int CPDF_FormField::InsertOption(CFX_WideString csOptLabel, |
| 735 | int index, |
| 736 | FX_BOOL bNotify) { |
| 737 | if (csOptLabel.IsEmpty()) |
| 738 | return -1; |
| 739 | |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 740 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 741 | int iRet = 0; |
| 742 | if (GetType() == ListBox) |
| 743 | iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csOptLabel); |
| 744 | if (GetType() == ComboBox) |
| 745 | iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csOptLabel); |
| 746 | if (iRet < 0) |
| 747 | return -1; |
| 748 | } |
| 749 | |
| 750 | CFX_ByteString csStr = PDF_EncodeText(csOptLabel, csOptLabel.GetLength()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 751 | CPDF_Object* pValue = FPDF_GetFieldAttr(m_pDict, "Opt"); |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 752 | CPDF_Array* pOpt = ToArray(pValue); |
| 753 | if (!pOpt) { |
| 754 | pOpt = new CPDF_Array; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 755 | m_pDict->SetAt("Opt", pOpt); |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 758 | int iCount = (int)pOpt->GetCount(); |
| 759 | if (index < 0 || index >= iCount) { |
| 760 | pOpt->AddString(csStr); |
| 761 | index = iCount; |
| 762 | } else { |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 763 | CPDF_String* pString = new CPDF_String(csStr, FALSE); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 764 | pOpt->InsertAt(index, pString); |
| 765 | } |
| 766 | |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 767 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 768 | if (GetType() == ListBox) |
| 769 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
| 770 | if (GetType() == ComboBox) |
| 771 | m_pForm->m_pFormNotify->AfterValueChange(this); |
| 772 | } |
| 773 | m_pForm->m_bUpdated = TRUE; |
| 774 | return index; |
| 775 | } |
| 776 | FX_BOOL CPDF_FormField::ClearOptions(FX_BOOL bNotify) { |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 777 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 778 | int iRet = 0; |
| 779 | CFX_WideString csValue; |
| 780 | int iIndex = GetSelectedIndex(0); |
| 781 | if (iIndex >= 0) |
| 782 | csValue = GetOptionLabel(iIndex); |
| 783 | if (GetType() == ListBox) |
| 784 | iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue); |
| 785 | if (GetType() == ComboBox) |
| 786 | iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue); |
| 787 | if (iRet < 0) |
| 788 | return FALSE; |
| 789 | } |
| 790 | |
| 791 | m_pDict->RemoveAt("Opt"); |
| 792 | m_pDict->RemoveAt("V"); |
| 793 | m_pDict->RemoveAt("DV"); |
| 794 | m_pDict->RemoveAt("I"); |
| 795 | m_pDict->RemoveAt("TI"); |
| 796 | |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 797 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 798 | if (GetType() == ListBox) |
| 799 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
| 800 | if (GetType() == ComboBox) |
| 801 | m_pForm->m_pFormNotify->AfterValueChange(this); |
| 802 | } |
| 803 | |
| 804 | m_pForm->m_bUpdated = TRUE; |
| 805 | return TRUE; |
| 806 | } |
Tom Sepez | a2c42ce | 2015-11-25 15:52:28 -0800 | [diff] [blame] | 807 | #endif // PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 808 | FX_BOOL CPDF_FormField::CheckControl(int iControlIndex, |
Tom Sepez | ed5d7aa | 2016-02-02 16:02:03 -0800 | [diff] [blame] | 809 | bool bChecked, |
| 810 | bool bNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 811 | ASSERT(GetType() == CheckBox || GetType() == RadioButton); |
| 812 | CPDF_FormControl* pControl = GetControl(iControlIndex); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 813 | if (!pControl) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 814 | return FALSE; |
| 815 | } |
| 816 | if (!bChecked && pControl->IsChecked() == bChecked) { |
| 817 | return FALSE; |
| 818 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 819 | CFX_WideString csWExport = pControl->GetExportValue(); |
| 820 | CFX_ByteString csBExport = PDF_EncodeText(csWExport); |
| 821 | int iCount = CountControls(); |
| 822 | FX_BOOL bUnison = PDF_FormField_IsUnison(this); |
| 823 | for (int i = 0; i < iCount; i++) { |
| 824 | CPDF_FormControl* pCtrl = GetControl(i); |
| 825 | if (bUnison) { |
| 826 | CFX_WideString csEValue = pCtrl->GetExportValue(); |
| 827 | if (csEValue == csWExport) { |
| 828 | if (pCtrl->GetOnStateName() == pControl->GetOnStateName()) { |
| 829 | pCtrl->CheckControl(bChecked); |
| 830 | } else if (bChecked) { |
| 831 | pCtrl->CheckControl(FALSE); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 832 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 833 | } else if (bChecked) { |
| 834 | pCtrl->CheckControl(FALSE); |
| 835 | } |
| 836 | } else { |
| 837 | if (i == iControlIndex) { |
| 838 | pCtrl->CheckControl(bChecked); |
| 839 | } else if (bChecked) { |
| 840 | pCtrl->CheckControl(FALSE); |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | CPDF_Object* pOpt = FPDF_GetFieldAttr(m_pDict, "Opt"); |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 845 | if (!ToArray(pOpt)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 846 | if (bChecked) { |
| 847 | m_pDict->SetAtName("V", csBExport); |
| 848 | } else { |
| 849 | CFX_ByteString csV; |
| 850 | CPDF_Object* pV = FPDF_GetFieldAttr(m_pDict, "V"); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 851 | if (pV) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 852 | csV = pV->GetString(); |
| 853 | } |
| 854 | if (csV == csBExport) { |
| 855 | m_pDict->SetAtName("V", "Off"); |
| 856 | } |
| 857 | } |
| 858 | } else if (bChecked) { |
| 859 | CFX_ByteString csIndex; |
| 860 | csIndex.Format("%d", iControlIndex); |
| 861 | m_pDict->SetAtName("V", csIndex); |
| 862 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 863 | if (bNotify && m_pForm->m_pFormNotify) { |
Tom Sepez | ed5d7aa | 2016-02-02 16:02:03 -0800 | [diff] [blame] | 864 | m_pForm->m_pFormNotify->AfterCheckedStatusChange(this); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 865 | } |
| 866 | m_pForm->m_bUpdated = TRUE; |
| 867 | return TRUE; |
| 868 | } |
| 869 | CFX_WideString CPDF_FormField::GetCheckValue(FX_BOOL bDefault) { |
| 870 | ASSERT(GetType() == CheckBox || GetType() == RadioButton); |
| 871 | CFX_WideString csExport = L"Off"; |
| 872 | FX_BOOL bChecked; |
| 873 | int iCount = CountControls(); |
| 874 | for (int i = 0; i < iCount; i++) { |
| 875 | CPDF_FormControl* pControl = GetControl(i); |
| 876 | if (bDefault) { |
| 877 | bChecked = pControl->IsDefaultChecked(); |
| 878 | } else { |
| 879 | bChecked = pControl->IsChecked(); |
| 880 | } |
| 881 | if (bChecked) { |
| 882 | csExport = pControl->GetExportValue(); |
| 883 | break; |
| 884 | } |
| 885 | } |
| 886 | return csExport; |
| 887 | } |
| 888 | FX_BOOL CPDF_FormField::SetCheckValue(const CFX_WideString& value, |
| 889 | FX_BOOL bDefault, |
| 890 | FX_BOOL bNotify) { |
| 891 | ASSERT(GetType() == CheckBox || GetType() == RadioButton); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 892 | int iCount = CountControls(); |
| 893 | for (int i = 0; i < iCount; i++) { |
| 894 | CPDF_FormControl* pControl = GetControl(i); |
| 895 | CFX_WideString csExport = pControl->GetExportValue(); |
| 896 | if (csExport == value) { |
| 897 | if (bDefault) { |
| 898 | } else { |
| 899 | CheckControl(GetControlIndex(pControl), TRUE); |
| 900 | } |
| 901 | break; |
| 902 | } else { |
| 903 | if (bDefault) { |
| 904 | } else { |
| 905 | CheckControl(GetControlIndex(pControl), FALSE); |
| 906 | } |
| 907 | } |
| 908 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 909 | if (bNotify && m_pForm->m_pFormNotify) { |
Tom Sepez | ed5d7aa | 2016-02-02 16:02:03 -0800 | [diff] [blame] | 910 | m_pForm->m_pFormNotify->AfterCheckedStatusChange(this); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 911 | } |
| 912 | m_pForm->m_bUpdated = TRUE; |
| 913 | return TRUE; |
| 914 | } |
| 915 | int CPDF_FormField::GetTopVisibleIndex() { |
| 916 | CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "TI"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 917 | if (!pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 918 | return 0; |
| 919 | } |
| 920 | return pObj->GetInteger(); |
| 921 | } |
| 922 | int CPDF_FormField::CountSelectedOptions() { |
| 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 0; |
| 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 0; |
| 930 | } |
| 931 | return (int)pArray->GetCount(); |
| 932 | } |
| 933 | int CPDF_FormField::GetSelectedOptionIndex(int index) { |
| 934 | CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "I"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 935 | if (!pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 936 | return -1; |
| 937 | } |
| 938 | CPDF_Array* pArray = pObj->GetArray(); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 939 | if (!pArray) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 940 | return -1; |
| 941 | } |
| 942 | int iCount = (int)pArray->GetCount(); |
| 943 | if (iCount > 0 && index < iCount) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 944 | return pArray->GetIntegerAt(index); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 945 | } |
| 946 | return -1; |
| 947 | } |
| 948 | FX_BOOL CPDF_FormField::IsOptionSelected(int iOptIndex) { |
| 949 | CPDF_Object* pObj = FPDF_GetFieldAttr(m_pDict, "I"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 950 | if (!pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 951 | return FALSE; |
| 952 | } |
| 953 | CPDF_Array* pArray = pObj->GetArray(); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 954 | if (!pArray) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 955 | return FALSE; |
| 956 | } |
| 957 | int iCount = (int)pArray->GetCount(); |
| 958 | for (int i = 0; i < iCount; i++) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 959 | if (pArray->GetIntegerAt(i) == iOptIndex) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 960 | return TRUE; |
| 961 | } |
| 962 | } |
| 963 | return FALSE; |
| 964 | } |
| 965 | FX_BOOL CPDF_FormField::SelectOption(int iOptIndex, |
| 966 | FX_BOOL bSelected, |
| 967 | FX_BOOL bNotify) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 968 | CPDF_Array* pArray = m_pDict->GetArrayBy("I"); |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 969 | if (!pArray) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 970 | if (!bSelected) { |
| 971 | return TRUE; |
| 972 | } |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 973 | pArray = new CPDF_Array; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 974 | m_pDict->SetAt("I", pArray); |
| 975 | } |
| 976 | FX_BOOL bReturn = FALSE; |
| 977 | for (int i = 0; i < (int)pArray->GetCount(); i++) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 978 | int iFind = pArray->GetIntegerAt(i); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 979 | if (iFind == iOptIndex) { |
| 980 | if (bSelected) { |
| 981 | return TRUE; |
| 982 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 983 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 984 | int iRet = 0; |
| 985 | CFX_WideString csValue = GetOptionLabel(iOptIndex); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 986 | if (GetType() == ListBox) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 987 | iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 988 | } |
| 989 | if (GetType() == ComboBox) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 990 | iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 991 | } |
| 992 | if (iRet < 0) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 993 | return FALSE; |
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 | pArray->RemoveAt(i); |
| 997 | bReturn = TRUE; |
| 998 | break; |
| 999 | } else if (iFind > iOptIndex) { |
| 1000 | if (!bSelected) { |
| 1001 | continue; |
| 1002 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 1003 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1004 | int iRet = 0; |
| 1005 | CFX_WideString csValue = GetOptionLabel(iOptIndex); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1006 | if (GetType() == ListBox) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1007 | iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1008 | } |
| 1009 | if (GetType() == ComboBox) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1010 | iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1011 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1012 | if (iRet < 0) { |
| 1013 | return FALSE; |
| 1014 | } |
| 1015 | } |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 1016 | CPDF_Number* pNum = new CPDF_Number(iOptIndex); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1017 | pArray->InsertAt(i, pNum); |
| 1018 | bReturn = TRUE; |
| 1019 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1020 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1021 | } |
| 1022 | if (!bReturn) { |
| 1023 | if (bSelected) { |
| 1024 | pArray->AddInteger(iOptIndex); |
| 1025 | } |
| 1026 | if (pArray->GetCount() == 0) { |
| 1027 | m_pDict->RemoveAt("I"); |
| 1028 | } |
| 1029 | } |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 1030 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1031 | if (GetType() == ListBox) { |
| 1032 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
| 1033 | } |
| 1034 | if (GetType() == ComboBox) { |
| 1035 | m_pForm->m_pFormNotify->AfterValueChange(this); |
| 1036 | } |
| 1037 | } |
| 1038 | m_pForm->m_bUpdated = TRUE; |
| 1039 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1040 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1041 | FX_BOOL CPDF_FormField::ClearSelectedOptions(FX_BOOL bNotify) { |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 1042 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1043 | int iRet = 0; |
| 1044 | CFX_WideString csValue; |
| 1045 | int iIndex = GetSelectedIndex(0); |
| 1046 | if (iIndex >= 0) { |
| 1047 | csValue = GetOptionLabel(iIndex); |
Bo Xu | 22b31e0 | 2014-07-21 10:02:07 -0700 | [diff] [blame] | 1048 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1049 | if (GetType() == ListBox) { |
| 1050 | iRet = m_pForm->m_pFormNotify->BeforeSelectionChange(this, csValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1051 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1052 | if (GetType() == ComboBox) { |
| 1053 | iRet = m_pForm->m_pFormNotify->BeforeValueChange(this, csValue); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1054 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1055 | if (iRet < 0) { |
| 1056 | return FALSE; |
| 1057 | } |
| 1058 | } |
| 1059 | m_pDict->RemoveAt("I"); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 1060 | if (bNotify && m_pForm->m_pFormNotify) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1061 | if (GetType() == ListBox) { |
| 1062 | m_pForm->m_pFormNotify->AfterSelectionChange(this); |
| 1063 | } |
| 1064 | if (GetType() == ComboBox) { |
| 1065 | m_pForm->m_pFormNotify->AfterValueChange(this); |
| 1066 | } |
| 1067 | } |
| 1068 | m_pForm->m_bUpdated = TRUE; |
| 1069 | return TRUE; |
| 1070 | } |
| 1071 | void CPDF_FormField::LoadDA() { |
| 1072 | CFX_ByteString DA; |
| 1073 | if (CPDF_Object* pObj_t = FPDF_GetFieldAttr(m_pDict, "DA")) { |
| 1074 | DA = pObj_t->GetString(); |
| 1075 | } |
| 1076 | if (DA.IsEmpty() && m_pForm->m_pFormDict) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 1077 | DA = m_pForm->m_pFormDict->GetStringBy("DA"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1078 | } |
| 1079 | if (DA.IsEmpty()) { |
| 1080 | return; |
| 1081 | } |
| 1082 | CPDF_SimpleParser syntax(DA); |
Wei Li | 970c11e | 2016-02-16 14:26:22 -0800 | [diff] [blame] | 1083 | syntax.FindTagParamFromStart("Tf", 2); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1084 | CFX_ByteString font_name = syntax.GetWord(); |
| 1085 | CPDF_Dictionary* pFontDict = NULL; |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 1086 | if (m_pForm->m_pFormDict && m_pForm->m_pFormDict->GetDictBy("DR") && |
| 1087 | m_pForm->m_pFormDict->GetDictBy("DR")->GetDictBy("Font")) |
| 1088 | pFontDict = m_pForm->m_pFormDict->GetDictBy("DR") |
| 1089 | ->GetDictBy("Font") |
| 1090 | ->GetDictBy(font_name); |
Bo Xu | 287e11a | 2014-06-30 09:49:21 -0700 | [diff] [blame] | 1091 | |
Lei Zhang | 412e908 | 2015-12-14 18:34:00 -0800 | [diff] [blame] | 1092 | if (!pFontDict) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1093 | return; |
| 1094 | } |
| 1095 | m_pFont = m_pForm->m_pDocument->LoadFont(pFontDict); |
| 1096 | m_FontSize = FX_atof(syntax.GetWord()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1097 | } |