kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 1 | // Copyright 2014 The PDFium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
| 7 | #include "fpdfsdk/pwl/cpwl_list_ctrl.h" |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <utility> |
| 11 | |
| 12 | #include "core/fpdfdoc/cpvt_word.h" |
| 13 | #include "core/fxcrt/fx_extension.h" |
| 14 | #include "core/fxcrt/stl_util.h" |
| 15 | #include "fpdfsdk/pwl/cpwl_edit_impl.h" |
| 16 | #include "fpdfsdk/pwl/cpwl_list_box.h" |
| 17 | #include "third_party/base/numerics/safe_conversions.h" |
| 18 | |
| 19 | CPWL_ListCtrl::NotifyIface::~NotifyIface() = default; |
| 20 | |
| 21 | CPWL_ListCtrl::Item::Item() : m_pEdit(std::make_unique<CPWL_EditImpl>()) { |
| 22 | m_pEdit->SetAlignmentV(1); |
| 23 | m_pEdit->Initialize(); |
| 24 | } |
| 25 | |
| 26 | CPWL_ListCtrl::Item::~Item() = default; |
| 27 | |
| 28 | void CPWL_ListCtrl::Item::SetFontMap(IPVT_FontMap* pFontMap) { |
| 29 | m_pEdit->SetFontMap(pFontMap); |
| 30 | } |
| 31 | |
| 32 | void CPWL_ListCtrl::Item::SetText(const WideString& text) { |
| 33 | m_pEdit->SetText(text); |
| 34 | m_pEdit->Paint(); |
| 35 | } |
| 36 | |
| 37 | void CPWL_ListCtrl::Item::SetFontSize(float fFontSize) { |
| 38 | m_pEdit->SetFontSize(fFontSize); |
| 39 | m_pEdit->Paint(); |
| 40 | } |
| 41 | |
| 42 | float CPWL_ListCtrl::Item::GetItemHeight() const { |
| 43 | return m_pEdit->GetContentRect().Height(); |
| 44 | } |
| 45 | |
| 46 | uint16_t CPWL_ListCtrl::Item::GetFirstChar() const { |
| 47 | CPVT_Word word; |
| 48 | CPWL_EditImpl::Iterator* pIterator = m_pEdit->GetIterator(); |
| 49 | pIterator->SetAt(1); |
| 50 | pIterator->GetWord(word); |
| 51 | return word.Word; |
| 52 | } |
| 53 | |
| 54 | WideString CPWL_ListCtrl::Item::GetText() const { |
| 55 | return m_pEdit->GetText(); |
| 56 | } |
| 57 | |
| 58 | CPWL_ListCtrl::SelectState::SelectState() = default; |
| 59 | |
| 60 | CPWL_ListCtrl::SelectState::~SelectState() = default; |
| 61 | |
| 62 | void CPWL_ListCtrl::SelectState::Add(int32_t nItemIndex) { |
| 63 | m_Items[nItemIndex] = SELECTING; |
| 64 | } |
| 65 | |
| 66 | void CPWL_ListCtrl::SelectState::Add(int32_t nBeginIndex, int32_t nEndIndex) { |
| 67 | if (nBeginIndex > nEndIndex) |
| 68 | std::swap(nBeginIndex, nEndIndex); |
| 69 | |
| 70 | for (int32_t i = nBeginIndex; i <= nEndIndex; ++i) |
| 71 | Add(i); |
| 72 | } |
| 73 | |
| 74 | void CPWL_ListCtrl::SelectState::Sub(int32_t nItemIndex) { |
| 75 | auto it = m_Items.find(nItemIndex); |
| 76 | if (it != m_Items.end()) |
| 77 | it->second = DESELECTING; |
| 78 | } |
| 79 | |
| 80 | void CPWL_ListCtrl::SelectState::Sub(int32_t nBeginIndex, int32_t nEndIndex) { |
| 81 | if (nBeginIndex > nEndIndex) |
| 82 | std::swap(nBeginIndex, nEndIndex); |
| 83 | |
| 84 | for (int32_t i = nBeginIndex; i <= nEndIndex; ++i) |
| 85 | Sub(i); |
| 86 | } |
| 87 | |
| 88 | void CPWL_ListCtrl::SelectState::DeselectAll() { |
| 89 | for (auto& item : m_Items) |
| 90 | item.second = DESELECTING; |
| 91 | } |
| 92 | |
| 93 | void CPWL_ListCtrl::SelectState::Done() { |
| 94 | auto it = m_Items.begin(); |
| 95 | while (it != m_Items.end()) { |
| 96 | if (it->second == DESELECTING) |
| 97 | it = m_Items.erase(it); |
| 98 | else |
| 99 | (it++)->second = NORMAL; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | CPWL_ListCtrl::CPWL_ListCtrl() = default; |
| 104 | |
| 105 | CPWL_ListCtrl::~CPWL_ListCtrl() { |
| 106 | m_ListItems.clear(); |
| 107 | InvalidateItem(-1); |
| 108 | } |
| 109 | |
| 110 | CFX_PointF CPWL_ListCtrl::InToOut(const CFX_PointF& point) const { |
| 111 | CFX_FloatRect rcPlate = m_rcPlate; |
| 112 | return CFX_PointF(point.x - (m_ptScrollPos.x - rcPlate.left), |
| 113 | point.y - (m_ptScrollPos.y - rcPlate.top)); |
| 114 | } |
| 115 | |
| 116 | CFX_PointF CPWL_ListCtrl::OutToIn(const CFX_PointF& point) const { |
| 117 | CFX_FloatRect rcPlate = m_rcPlate; |
| 118 | return CFX_PointF(point.x + (m_ptScrollPos.x - rcPlate.left), |
| 119 | point.y + (m_ptScrollPos.y - rcPlate.top)); |
| 120 | } |
| 121 | |
| 122 | CFX_FloatRect CPWL_ListCtrl::InToOut(const CFX_FloatRect& rect) const { |
| 123 | CFX_PointF ptLeftBottom = InToOut(CFX_PointF(rect.left, rect.bottom)); |
| 124 | CFX_PointF ptRightTop = InToOut(CFX_PointF(rect.right, rect.top)); |
| 125 | return CFX_FloatRect(ptLeftBottom.x, ptLeftBottom.y, ptRightTop.x, |
| 126 | ptRightTop.y); |
| 127 | } |
| 128 | |
| 129 | CFX_FloatRect CPWL_ListCtrl::OutToIn(const CFX_FloatRect& rect) const { |
| 130 | CFX_PointF ptLeftBottom = OutToIn(CFX_PointF(rect.left, rect.bottom)); |
| 131 | CFX_PointF ptRightTop = OutToIn(CFX_PointF(rect.right, rect.top)); |
| 132 | return CFX_FloatRect(ptLeftBottom.x, ptLeftBottom.y, ptRightTop.x, |
| 133 | ptRightTop.y); |
| 134 | } |
| 135 | |
| 136 | CFX_PointF CPWL_ListCtrl::InnerToOuter(const CFX_PointF& point) const { |
| 137 | return CFX_PointF(point.x + GetBTPoint().x, GetBTPoint().y - point.y); |
| 138 | } |
| 139 | |
| 140 | CFX_PointF CPWL_ListCtrl::OuterToInner(const CFX_PointF& point) const { |
| 141 | return CFX_PointF(point.x - GetBTPoint().x, GetBTPoint().y - point.y); |
| 142 | } |
| 143 | |
| 144 | CFX_FloatRect CPWL_ListCtrl::InnerToOuter(const CFX_FloatRect& rect) const { |
| 145 | CFX_PointF ptLeftTop = InnerToOuter(CFX_PointF(rect.left, rect.top)); |
| 146 | CFX_PointF ptRightBottom = InnerToOuter(CFX_PointF(rect.right, rect.bottom)); |
| 147 | return CFX_FloatRect(ptLeftTop.x, ptRightBottom.y, ptRightBottom.x, |
| 148 | ptLeftTop.y); |
| 149 | } |
| 150 | |
| 151 | CFX_FloatRect CPWL_ListCtrl::OuterToInner(const CFX_FloatRect& rect) const { |
| 152 | CFX_PointF ptLeftTop = OuterToInner(CFX_PointF(rect.left, rect.top)); |
| 153 | CFX_PointF ptRightBottom = OuterToInner(CFX_PointF(rect.right, rect.bottom)); |
| 154 | return CFX_FloatRect(ptLeftTop.x, ptRightBottom.y, ptRightBottom.x, |
| 155 | ptLeftTop.y); |
| 156 | } |
| 157 | |
| 158 | void CPWL_ListCtrl::OnMouseDown(const CFX_PointF& point, |
| 159 | bool bShift, |
| 160 | bool bCtrl) { |
| 161 | int32_t nHitIndex = GetItemIndex(point); |
| 162 | |
| 163 | if (IsMultipleSel()) { |
| 164 | if (bCtrl) { |
| 165 | if (IsItemSelected(nHitIndex)) { |
| 166 | m_SelectState.Sub(nHitIndex); |
| 167 | SelectItems(); |
| 168 | m_bCtrlSel = false; |
| 169 | } else { |
| 170 | m_SelectState.Add(nHitIndex); |
| 171 | SelectItems(); |
| 172 | m_bCtrlSel = true; |
| 173 | } |
| 174 | |
| 175 | m_nFootIndex = nHitIndex; |
| 176 | } else if (bShift) { |
| 177 | m_SelectState.DeselectAll(); |
| 178 | m_SelectState.Add(m_nFootIndex, nHitIndex); |
| 179 | SelectItems(); |
| 180 | } else { |
| 181 | m_SelectState.DeselectAll(); |
| 182 | m_SelectState.Add(nHitIndex); |
| 183 | SelectItems(); |
| 184 | |
| 185 | m_nFootIndex = nHitIndex; |
| 186 | } |
| 187 | |
| 188 | SetCaret(nHitIndex); |
| 189 | } else { |
| 190 | SetSingleSelect(nHitIndex); |
| 191 | } |
| 192 | |
| 193 | if (!IsItemVisible(nHitIndex)) |
| 194 | ScrollToListItem(nHitIndex); |
| 195 | } |
| 196 | |
| 197 | void CPWL_ListCtrl::OnMouseMove(const CFX_PointF& point, |
| 198 | bool bShift, |
| 199 | bool bCtrl) { |
| 200 | int32_t nHitIndex = GetItemIndex(point); |
| 201 | |
| 202 | if (IsMultipleSel()) { |
| 203 | if (bCtrl) { |
| 204 | if (m_bCtrlSel) |
| 205 | m_SelectState.Add(m_nFootIndex, nHitIndex); |
| 206 | else |
| 207 | m_SelectState.Sub(m_nFootIndex, nHitIndex); |
| 208 | |
| 209 | SelectItems(); |
| 210 | } else { |
| 211 | m_SelectState.DeselectAll(); |
| 212 | m_SelectState.Add(m_nFootIndex, nHitIndex); |
| 213 | SelectItems(); |
| 214 | } |
| 215 | |
| 216 | SetCaret(nHitIndex); |
| 217 | } else { |
| 218 | SetSingleSelect(nHitIndex); |
| 219 | } |
| 220 | |
| 221 | if (!IsItemVisible(nHitIndex)) |
| 222 | ScrollToListItem(nHitIndex); |
| 223 | } |
| 224 | |
| 225 | void CPWL_ListCtrl::OnVK(int32_t nItemIndex, bool bShift, bool bCtrl) { |
| 226 | if (IsMultipleSel()) { |
| 227 | if (nItemIndex >= 0 && nItemIndex < GetCount()) { |
| 228 | if (bCtrl) { |
| 229 | } else if (bShift) { |
| 230 | m_SelectState.DeselectAll(); |
| 231 | m_SelectState.Add(m_nFootIndex, nItemIndex); |
| 232 | SelectItems(); |
| 233 | } else { |
| 234 | m_SelectState.DeselectAll(); |
| 235 | m_SelectState.Add(nItemIndex); |
| 236 | SelectItems(); |
| 237 | m_nFootIndex = nItemIndex; |
| 238 | } |
| 239 | |
| 240 | SetCaret(nItemIndex); |
| 241 | } |
| 242 | } else { |
| 243 | SetSingleSelect(nItemIndex); |
| 244 | } |
| 245 | |
| 246 | if (!IsItemVisible(nItemIndex)) |
| 247 | ScrollToListItem(nItemIndex); |
| 248 | } |
| 249 | |
| 250 | void CPWL_ListCtrl::OnVK_UP(bool bShift, bool bCtrl) { |
| 251 | OnVK(IsMultipleSel() ? GetCaret() - 1 : GetSelect() - 1, bShift, bCtrl); |
| 252 | } |
| 253 | |
| 254 | void CPWL_ListCtrl::OnVK_DOWN(bool bShift, bool bCtrl) { |
| 255 | OnVK(IsMultipleSel() ? GetCaret() + 1 : GetSelect() + 1, bShift, bCtrl); |
| 256 | } |
| 257 | |
| 258 | void CPWL_ListCtrl::OnVK_LEFT(bool bShift, bool bCtrl) { |
| 259 | OnVK(0, bShift, bCtrl); |
| 260 | } |
| 261 | |
| 262 | void CPWL_ListCtrl::OnVK_RIGHT(bool bShift, bool bCtrl) { |
| 263 | OnVK(GetCount() - 1, bShift, bCtrl); |
| 264 | } |
| 265 | |
| 266 | void CPWL_ListCtrl::OnVK_HOME(bool bShift, bool bCtrl) { |
| 267 | OnVK(0, bShift, bCtrl); |
| 268 | } |
| 269 | |
| 270 | void CPWL_ListCtrl::OnVK_END(bool bShift, bool bCtrl) { |
| 271 | OnVK(GetCount() - 1, bShift, bCtrl); |
| 272 | } |
| 273 | |
| 274 | bool CPWL_ListCtrl::OnChar(uint16_t nChar, bool bShift, bool bCtrl) { |
| 275 | int32_t nIndex = GetLastSelected(); |
| 276 | int32_t nFindIndex = FindNext(nIndex, nChar); |
| 277 | |
| 278 | if (nFindIndex != nIndex) { |
| 279 | OnVK(nFindIndex, bShift, bCtrl); |
| 280 | return true; |
| 281 | } |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | void CPWL_ListCtrl::SetPlateRect(const CFX_FloatRect& rect) { |
| 286 | m_rcPlate = rect; |
| 287 | m_ptScrollPos.x = rect.left; |
| 288 | SetScrollPos(CFX_PointF(rect.left, rect.top)); |
| 289 | ReArrange(0); |
| 290 | InvalidateItem(-1); |
| 291 | } |
| 292 | |
| 293 | CFX_FloatRect CPWL_ListCtrl::GetItemRect(int32_t nIndex) const { |
| 294 | return InToOut(GetItemRectInternal(nIndex)); |
| 295 | } |
| 296 | |
| 297 | CFX_FloatRect CPWL_ListCtrl::GetItemRectInternal(int32_t nIndex) const { |
| 298 | if (!IsValid(nIndex)) |
| 299 | return CFX_FloatRect(); |
| 300 | |
| 301 | CFX_FloatRect rcItem = m_ListItems[nIndex]->GetRect(); |
| 302 | rcItem.left = 0.0f; |
| 303 | rcItem.right = m_rcPlate.Width(); |
| 304 | return InnerToOuter(rcItem); |
| 305 | } |
| 306 | |
| 307 | void CPWL_ListCtrl::AddString(const WideString& str) { |
| 308 | AddItem(str); |
| 309 | ReArrange(GetCount() - 1); |
| 310 | } |
| 311 | |
| 312 | void CPWL_ListCtrl::SetMultipleSelect(int32_t nItemIndex, bool bSelected) { |
| 313 | if (!IsValid(nItemIndex)) |
| 314 | return; |
| 315 | |
| 316 | if (bSelected != IsItemSelected(nItemIndex)) { |
| 317 | if (bSelected) { |
| 318 | SetItemSelect(nItemIndex, true); |
| 319 | InvalidateItem(nItemIndex); |
| 320 | } else { |
| 321 | SetItemSelect(nItemIndex, false); |
| 322 | InvalidateItem(nItemIndex); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | void CPWL_ListCtrl::SetSingleSelect(int32_t nItemIndex) { |
| 328 | if (!IsValid(nItemIndex)) |
| 329 | return; |
| 330 | |
| 331 | if (m_nSelItem != nItemIndex) { |
| 332 | if (m_nSelItem >= 0) { |
| 333 | SetItemSelect(m_nSelItem, false); |
| 334 | InvalidateItem(m_nSelItem); |
| 335 | } |
| 336 | |
| 337 | SetItemSelect(nItemIndex, true); |
| 338 | InvalidateItem(nItemIndex); |
| 339 | m_nSelItem = nItemIndex; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | void CPWL_ListCtrl::SetCaret(int32_t nItemIndex) { |
| 344 | if (!IsValid(nItemIndex)) |
| 345 | return; |
| 346 | |
| 347 | if (IsMultipleSel()) { |
| 348 | int32_t nOldIndex = m_nCaretIndex; |
| 349 | |
| 350 | if (nOldIndex != nItemIndex) { |
| 351 | m_nCaretIndex = nItemIndex; |
| 352 | InvalidateItem(nOldIndex); |
| 353 | InvalidateItem(nItemIndex); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void CPWL_ListCtrl::InvalidateItem(int32_t nItemIndex) { |
| 359 | if (m_pNotify) { |
| 360 | if (nItemIndex == -1) { |
| 361 | if (!m_bNotifyFlag) { |
| 362 | m_bNotifyFlag = true; |
| 363 | CFX_FloatRect rcRefresh = m_rcPlate; |
| 364 | m_pNotify->OnInvalidateRect(rcRefresh); |
| 365 | m_bNotifyFlag = false; |
| 366 | } |
| 367 | } else { |
| 368 | if (!m_bNotifyFlag) { |
| 369 | m_bNotifyFlag = true; |
| 370 | CFX_FloatRect rcRefresh = GetItemRect(nItemIndex); |
| 371 | rcRefresh.left -= 1.0f; |
| 372 | rcRefresh.right += 1.0f; |
| 373 | rcRefresh.bottom -= 1.0f; |
| 374 | rcRefresh.top += 1.0f; |
| 375 | |
| 376 | m_pNotify->OnInvalidateRect(rcRefresh); |
| 377 | m_bNotifyFlag = false; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void CPWL_ListCtrl::SelectItems() { |
| 384 | for (const auto& item : m_SelectState) { |
| 385 | if (item.second != SelectState::NORMAL) |
| 386 | SetMultipleSelect(item.first, item.second == SelectState::SELECTING); |
| 387 | } |
| 388 | m_SelectState.Done(); |
| 389 | } |
| 390 | |
| 391 | void CPWL_ListCtrl::Select(int32_t nItemIndex) { |
| 392 | if (!IsValid(nItemIndex)) |
| 393 | return; |
| 394 | |
| 395 | if (IsMultipleSel()) { |
| 396 | m_SelectState.Add(nItemIndex); |
| 397 | SelectItems(); |
| 398 | } else { |
| 399 | SetSingleSelect(nItemIndex); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void CPWL_ListCtrl::Deselect(int32_t nItemIndex) { |
| 404 | if (!IsItemSelected(nItemIndex)) |
| 405 | return; |
| 406 | |
| 407 | SetMultipleSelect(nItemIndex, false); |
| 408 | |
| 409 | if (!IsMultipleSel()) |
| 410 | m_nSelItem = -1; |
| 411 | } |
| 412 | |
| 413 | bool CPWL_ListCtrl::IsItemVisible(int32_t nItemIndex) const { |
| 414 | CFX_FloatRect rcPlate = m_rcPlate; |
| 415 | CFX_FloatRect rcItem = GetItemRect(nItemIndex); |
| 416 | |
| 417 | return rcItem.bottom >= rcPlate.bottom && rcItem.top <= rcPlate.top; |
| 418 | } |
| 419 | |
| 420 | void CPWL_ListCtrl::ScrollToListItem(int32_t nItemIndex) { |
| 421 | if (!IsValid(nItemIndex)) |
| 422 | return; |
| 423 | |
| 424 | CFX_FloatRect rcPlate = m_rcPlate; |
| 425 | CFX_FloatRect rcItem = GetItemRectInternal(nItemIndex); |
| 426 | CFX_FloatRect rcItemCtrl = GetItemRect(nItemIndex); |
| 427 | |
| 428 | if (FXSYS_IsFloatSmaller(rcItemCtrl.bottom, rcPlate.bottom)) { |
| 429 | if (FXSYS_IsFloatSmaller(rcItemCtrl.top, rcPlate.top)) { |
| 430 | SetScrollPosY(rcItem.bottom + rcPlate.Height()); |
| 431 | } |
| 432 | } else if (FXSYS_IsFloatBigger(rcItemCtrl.top, rcPlate.top)) { |
| 433 | if (FXSYS_IsFloatBigger(rcItemCtrl.bottom, rcPlate.bottom)) { |
| 434 | SetScrollPosY(rcItem.top); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | void CPWL_ListCtrl::SetScrollInfo() { |
| 440 | if (m_pNotify) { |
| 441 | CFX_FloatRect rcPlate = m_rcPlate; |
| 442 | CFX_FloatRect rcContent = GetContentRectInternal(); |
| 443 | |
| 444 | if (!m_bNotifyFlag) { |
| 445 | m_bNotifyFlag = true; |
| 446 | m_pNotify->OnSetScrollInfoY(rcPlate.bottom, rcPlate.top, rcContent.bottom, |
| 447 | rcContent.top, GetFirstHeight(), |
| 448 | rcPlate.Height()); |
| 449 | m_bNotifyFlag = false; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | void CPWL_ListCtrl::SetScrollPos(const CFX_PointF& point) { |
| 455 | SetScrollPosY(point.y); |
| 456 | } |
| 457 | |
| 458 | void CPWL_ListCtrl::SetScrollPosY(float fy) { |
| 459 | if (!FXSYS_IsFloatEqual(m_ptScrollPos.y, fy)) { |
| 460 | CFX_FloatRect rcPlate = m_rcPlate; |
| 461 | CFX_FloatRect rcContent = GetContentRectInternal(); |
| 462 | |
| 463 | if (rcPlate.Height() > rcContent.Height()) { |
| 464 | fy = rcPlate.top; |
| 465 | } else { |
| 466 | if (FXSYS_IsFloatSmaller(fy - rcPlate.Height(), rcContent.bottom)) { |
| 467 | fy = rcContent.bottom + rcPlate.Height(); |
| 468 | } else if (FXSYS_IsFloatBigger(fy, rcContent.top)) { |
| 469 | fy = rcContent.top; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | m_ptScrollPos.y = fy; |
| 474 | InvalidateItem(-1); |
| 475 | |
| 476 | if (m_pNotify) { |
| 477 | if (!m_bNotifyFlag) { |
| 478 | m_bNotifyFlag = true; |
| 479 | m_pNotify->OnSetScrollPosY(fy); |
| 480 | m_bNotifyFlag = false; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | CFX_FloatRect CPWL_ListCtrl::GetContentRectInternal() const { |
| 487 | return InnerToOuter(m_rcContent); |
| 488 | } |
| 489 | |
| 490 | CFX_FloatRect CPWL_ListCtrl::GetContentRect() const { |
| 491 | return InToOut(GetContentRectInternal()); |
| 492 | } |
| 493 | |
| 494 | void CPWL_ListCtrl::ReArrange(int32_t nItemIndex) { |
| 495 | float fPosY = 0.0f; |
| 496 | if (IsValid(nItemIndex - 1)) |
| 497 | fPosY = m_ListItems[nItemIndex - 1]->GetRect().bottom; |
| 498 | |
| 499 | for (const auto& pListItem : m_ListItems) { |
| 500 | float fListItemHeight = pListItem->GetItemHeight(); |
| 501 | pListItem->SetRect( |
| 502 | CFX_FloatRect(0.0f, fPosY + fListItemHeight, 0.0f, fPosY)); |
| 503 | fPosY += fListItemHeight; |
| 504 | } |
| 505 | m_rcContent = CFX_FloatRect(0.0f, fPosY, 0.0f, 0.0f); |
| 506 | SetScrollInfo(); |
| 507 | } |
| 508 | |
| 509 | void CPWL_ListCtrl::SetTopItem(int32_t nIndex) { |
| 510 | if (IsValid(nIndex)) { |
| 511 | CFX_FloatRect rcItem = GetItemRectInternal(nIndex); |
| 512 | SetScrollPosY(rcItem.top); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | int32_t CPWL_ListCtrl::GetTopItem() const { |
| 517 | int32_t nItemIndex = GetItemIndex(GetBTPoint()); |
| 518 | if (!IsItemVisible(nItemIndex) && IsItemVisible(nItemIndex + 1)) |
| 519 | nItemIndex += 1; |
| 520 | |
| 521 | return nItemIndex; |
| 522 | } |
| 523 | |
| 524 | int32_t CPWL_ListCtrl::GetItemIndex(const CFX_PointF& point) const { |
| 525 | CFX_PointF pt = OuterToInner(OutToIn(point)); |
| 526 | bool bFirst = true; |
| 527 | bool bLast = true; |
| 528 | for (const auto& pListItem : m_ListItems) { |
| 529 | CFX_FloatRect rcListItem = pListItem->GetRect(); |
| 530 | if (FXSYS_IsFloatBigger(pt.y, rcListItem.top)) |
| 531 | bFirst = false; |
| 532 | if (FXSYS_IsFloatSmaller(pt.y, rcListItem.bottom)) |
| 533 | bLast = false; |
| 534 | if (pt.y >= rcListItem.top && pt.y < rcListItem.bottom) { |
| 535 | return pdfium::base::checked_cast<int32_t>(&pListItem - |
| 536 | &m_ListItems.front()); |
| 537 | } |
| 538 | } |
| 539 | if (bFirst) |
| 540 | return 0; |
| 541 | if (bLast) |
| 542 | return GetCount() - 1; |
| 543 | return -1; |
| 544 | } |
| 545 | |
| 546 | WideString CPWL_ListCtrl::GetText() const { |
| 547 | if (IsMultipleSel()) |
| 548 | return GetItemText(m_nCaretIndex); |
| 549 | return GetItemText(m_nSelItem); |
| 550 | } |
| 551 | |
| 552 | void CPWL_ListCtrl::AddItem(const WideString& str) { |
| 553 | auto pListItem = std::make_unique<Item>(); |
| 554 | pListItem->SetFontMap(m_pFontMap); |
| 555 | pListItem->SetFontSize(m_fFontSize); |
| 556 | pListItem->SetText(str); |
| 557 | m_ListItems.push_back(std::move(pListItem)); |
| 558 | } |
| 559 | |
| 560 | CPWL_EditImpl* CPWL_ListCtrl::GetItemEdit(int32_t nIndex) const { |
| 561 | if (!IsValid(nIndex)) |
| 562 | return nullptr; |
| 563 | return m_ListItems[nIndex]->GetEdit(); |
| 564 | } |
| 565 | |
| 566 | int32_t CPWL_ListCtrl::GetCount() const { |
| 567 | return fxcrt::CollectionSize<int32_t>(m_ListItems); |
| 568 | } |
| 569 | |
| 570 | float CPWL_ListCtrl::GetFirstHeight() const { |
| 571 | if (m_ListItems.empty()) |
| 572 | return 1.0f; |
| 573 | return m_ListItems.front()->GetItemHeight(); |
| 574 | } |
| 575 | |
| 576 | int32_t CPWL_ListCtrl::GetFirstSelected() const { |
| 577 | int32_t i = 0; |
| 578 | for (const auto& pListItem : m_ListItems) { |
| 579 | if (pListItem->IsSelected()) |
| 580 | return i; |
| 581 | ++i; |
| 582 | } |
| 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | int32_t CPWL_ListCtrl::GetLastSelected() const { |
| 587 | for (auto iter = m_ListItems.rbegin(); iter != m_ListItems.rend(); ++iter) { |
| 588 | if ((*iter)->IsSelected()) |
| 589 | return pdfium::base::checked_cast<int32_t>(&*iter - &m_ListItems.front()); |
| 590 | } |
| 591 | return -1; |
| 592 | } |
| 593 | |
| 594 | int32_t CPWL_ListCtrl::FindNext(int32_t nIndex, wchar_t nChar) const { |
| 595 | int32_t nCircleIndex = nIndex; |
| 596 | int32_t sz = GetCount(); |
| 597 | for (int32_t i = 0; i < sz; i++) { |
| 598 | nCircleIndex++; |
| 599 | if (nCircleIndex >= sz) |
| 600 | nCircleIndex = 0; |
| 601 | |
| 602 | if (Item* pListItem = m_ListItems[nCircleIndex].get()) { |
| 603 | if (FXSYS_towupper(pListItem->GetFirstChar()) == FXSYS_towupper(nChar)) |
| 604 | return nCircleIndex; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | return nCircleIndex; |
| 609 | } |
| 610 | |
| 611 | bool CPWL_ListCtrl::IsItemSelected(int32_t nIndex) const { |
| 612 | return IsValid(nIndex) && m_ListItems[nIndex]->IsSelected(); |
| 613 | } |
| 614 | |
| 615 | void CPWL_ListCtrl::SetItemSelect(int32_t nIndex, bool bSelected) { |
| 616 | if (IsValid(nIndex)) |
| 617 | m_ListItems[nIndex]->SetSelect(bSelected); |
| 618 | } |
| 619 | |
| 620 | bool CPWL_ListCtrl::IsValid(int32_t nItemIndex) const { |
| 621 | return fxcrt::IndexInBounds(m_ListItems, nItemIndex); |
| 622 | } |
| 623 | |
| 624 | WideString CPWL_ListCtrl::GetItemText(int32_t nIndex) const { |
| 625 | if (IsValid(nIndex)) |
| 626 | return m_ListItems[nIndex]->GetText(); |
| 627 | return WideString(); |
| 628 | } |