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