Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfwl_edit.cpp
Go to the documentation of this file.
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 "xfa/fwl/cfwl_edit.h"
8
9#include <algorithm>
10#include <memory>
11#include <utility>
12#include <vector>
13
14#include "build/build_config.h"
15#include "core/fxcrt/check.h"
16#include "core/fxcrt/numerics/safe_conversions.h"
17#include "core/fxge/cfx_renderdevice.h"
18#include "core/fxge/text_char_pos.h"
19#include "v8/include/cppgc/visitor.h"
20#include "xfa/fde/cfde_textout.h"
21#include "xfa/fgas/font/cfgas_gefont.h"
22#include "xfa/fgas/graphics/cfgas_gegraphics.h"
23#include "xfa/fgas/graphics/cfgas_gepath.h"
24#include "xfa/fwl/cfwl_app.h"
25#include "xfa/fwl/cfwl_caret.h"
26#include "xfa/fwl/cfwl_event.h"
27#include "xfa/fwl/cfwl_eventtextwillchange.h"
28#include "xfa/fwl/cfwl_eventvalidate.h"
29#include "xfa/fwl/cfwl_messagekey.h"
30#include "xfa/fwl/cfwl_messagemouse.h"
31#include "xfa/fwl/cfwl_themebackground.h"
32#include "xfa/fwl/cfwl_themepart.h"
33#include "xfa/fwl/cfwl_widgetmgr.h"
34#include "xfa/fwl/fwl_widgetdef.h"
35#include "xfa/fwl/ifwl_themeprovider.h"
36#include "xfa/fwl/theme/cfwl_utils.h"
37
38namespace pdfium {
39
40namespace {
41
42constexpr int kEditMargin = 3;
43
44#if BUILDFLAG(IS_APPLE)
45constexpr XFA_FWL_KeyFlag kEditingModifier = XFA_FWL_KeyFlag::kCommand;
46#else
47constexpr XFA_FWL_KeyFlag kEditingModifier = XFA_FWL_KeyFlag::kCtrl;
48#endif
49
50} // namespace
51
52CFWL_Edit::CFWL_Edit(CFWL_App* app,
53 const Properties& properties,
54 CFWL_Widget* pOuter)
55 : CFWL_Widget(app, properties, pOuter),
57 m_pEditEngine->SetDelegate(this);
58}
59
60CFWL_Edit::~CFWL_Edit() = default;
61
63 m_pEditEngine->SetDelegate(nullptr);
65 HideCaret(nullptr);
67}
68
69void CFWL_Edit::Trace(cppgc::Visitor* visitor) const {
70 CFWL_Widget::Trace(visitor);
71 visitor->Trace(m_pVertScrollBar);
72 visitor->Trace(m_pCaret);
73}
74
76 return FWL_Type::Edit;
77}
78
82 float scrollbarWidth = GetThemeProvider()->GetScrollBarWidth();
83 if (IsShowVertScrollBar()) {
84 rect.width += scrollbarWidth;
85 rect.width += kEditMargin;
86 }
87 }
88 return rect;
89}
90
92 CFX_RectF rect;
93 if (m_pEditEngine->GetLength() > 0) {
94 CFX_SizeF size = CalcTextSize(
95 m_pEditEngine->GetText(),
96 !!(m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_MultiLine));
97 rect = CFX_RectF(0, 0, size);
98 }
100 return rect;
101}
102
103void CFWL_Edit::SetStates(uint32_t dwStates) {
106 HideCaret(nullptr);
107 }
108 CFWL_Widget::SetStates(dwStates);
109}
110
112 if (IsLocked())
113 return;
114
115 Layout();
116 if (m_ClientRect.IsEmpty())
117 return;
118
119 UpdateEditEngine();
120 UpdateVAlignment();
121 UpdateScroll();
122 InitCaret();
123}
124
127 if (IsShowVertScrollBar()) {
128 if (m_pVertScrollBar->GetWidgetRect().Contains(point))
130 }
131 }
132 if (m_ClientRect.Contains(point))
133 return FWL_WidgetHit::Edit;
135}
136
138 const CFX_Matrix& matrix) {
139 if (!pGraphics)
140 return;
141
142 if (m_ClientRect.IsEmpty())
143 return;
144
145 DrawContent(pGraphics, matrix);
146 if (HasBorder())
147 DrawBorder(pGraphics, CFWL_ThemePart::Part::kBorder, matrix);
148}
149
150void CFWL_Edit::SetText(const WideString& wsText) {
151 m_pEditEngine->Clear();
152 m_pEditEngine->Insert(0, wsText,
153 CFDE_TextEditEngine::RecordOperation::kInsertRecord);
154}
155
157 m_pEditEngine->Clear();
158 m_pEditEngine->Insert(0, wsText,
159 CFDE_TextEditEngine::RecordOperation::kSkipNotify);
160}
161
163 return m_pEditEngine->GetLength();
164}
165
167 return m_pEditEngine->GetText();
168}
169
171 m_pEditEngine->Clear();
172}
173
175 m_pEditEngine->SelectAll();
176}
177
178bool CFWL_Edit::HasSelection() const {
179 return m_pEditEngine->HasSelection();
180}
181
183 return m_pEditEngine->GetSelection();
184}
185
187 return m_pEditEngine->ClearSelection();
188}
189
190int32_t CFWL_Edit::GetLimit() const {
191 return m_nLimit;
192}
193
194void CFWL_Edit::SetLimit(int32_t nLimit) {
195 m_nLimit = nLimit;
196
197 if (m_nLimit > 0) {
198 m_pEditEngine->SetHasCharacterLimit(true);
199 m_pEditEngine->SetCharacterLimit(nLimit);
200 } else {
201 m_pEditEngine->SetHasCharacterLimit(false);
202 }
203}
204
205void CFWL_Edit::SetAliasChar(wchar_t wAlias) {
206 m_pEditEngine->SetAliasChar(wAlias);
207}
208
210 if (!m_pEditEngine->HasSelection())
211 return std::nullopt;
212
213 return m_pEditEngine->GetSelectedText();
214}
215
217 if (!m_pEditEngine->HasSelection())
218 return std::nullopt;
219
220 WideString cut_text = m_pEditEngine->DeleteSelectedText();
221 UpdateCaret();
222 return cut_text;
223}
224
225bool CFWL_Edit::Paste(const WideString& wsPaste) {
226 if (m_pEditEngine->HasSelection())
227 m_pEditEngine->ReplaceSelectedText(wsPaste);
228 else
229 m_pEditEngine->Insert(m_CursorPosition, wsPaste);
230
231 return true;
232}
233
235 return CanUndo() && m_pEditEngine->Undo();
236}
237
239 return CanRedo() && m_pEditEngine->Redo();
240}
241
243 return m_pEditEngine->CanUndo();
244}
245
247 return m_pEditEngine->CanRedo();
248}
249
254
256 if (m_EngineRect.IsEmpty())
257 return;
259 return;
260
261 bool bRepaintContent = UpdateOffset();
262 UpdateCaret();
263 CFX_RectF rtInvalid;
264 bool bRepaintScroll = false;
266 CFWL_ScrollBar* pScroll = UpdateScroll();
267 if (pScroll) {
268 rtInvalid = pScroll->GetWidgetRect();
269 bRepaintScroll = true;
270 }
271 }
272 if (bRepaintContent || bRepaintScroll) {
273 if (bRepaintContent)
274 rtInvalid.Union(m_EngineRect);
275 RepaintRect(rtInvalid);
276 }
277}
278
279void CFWL_Edit::OnTextWillChange(CFDE_TextEditEngine::TextChange* change) {
280 CFWL_EventTextWillChange event(this, change->text, change->previous_text,
281 change->selection_start,
282 change->selection_end);
283 DispatchEvent(&event);
284
285 change->text = event.GetChangeText();
286 change->selection_start = event.GetSelectionStart();
287 change->selection_end = event.GetSelectionEnd();
288 change->cancelled = event.GetCancelled();
289}
290
293 UpdateVAlignment();
294
295 LayoutScrollBar();
297}
298
302
303bool CFWL_Edit::OnValidate(const WideString& wsText) {
304 CFWL_EventValidate event(this, wsText);
305 DispatchEvent(&event);
306 return event.GetValidate();
307}
308
309void CFWL_Edit::SetScrollOffset(float fScrollOffset) {
310 m_fScrollOffsetY = fScrollOffset;
311}
312
313void CFWL_Edit::DrawContent(CFGAS_GEGraphics* pGraphics,
314 const CFX_Matrix& mtMatrix) {
315 DrawContentNonComb(pGraphics, mtMatrix);
317 CFGAS_GEGraphics::StateRestorer restorer(pGraphics);
318 CFGAS_GEPath path;
319 const int32_t iLimit = m_nLimit > 0 ? m_nLimit : 1;
320 const float fStep = m_EngineRect.width / iLimit;
321 float fLeft = m_EngineRect.left + 1;
322 for (int32_t i = 1; i < iLimit; i++) {
323 fLeft += fStep;
324 path.AddLine(CFX_PointF(fLeft, m_ClientRect.top),
325 CFX_PointF(fLeft, m_ClientRect.bottom()));
326 }
327 CFWL_ThemeBackground param(CFWL_ThemePart::Part::kCombTextLine, this,
328 pGraphics);
329 param.m_matrix = mtMatrix;
330 param.SetPath(&path);
331 GetThemeProvider()->DrawBackground(param);
332 }
333}
334
335void CFWL_Edit::DrawContentNonComb(CFGAS_GEGraphics* pGraphics,
336 const CFX_Matrix& mtMatrix) {
337 CFGAS_GEGraphics::StateRestorer restorer(pGraphics);
338 CFX_RectF rtClip = m_EngineRect;
339 float fOffSetX = m_EngineRect.left - m_fScrollOffsetX;
340 float fOffSetY = m_EngineRect.top - m_fScrollOffsetY + m_fVAlignOffset;
341 CFX_Matrix mt(1, 0, 0, 1, fOffSetX, fOffSetY);
342 rtClip = mtMatrix.TransformRect(rtClip);
343 mt.Concat(mtMatrix);
344
346 if (bShowSel && m_pEditEngine->HasSelection()) {
347 auto [sel_start, count] = m_pEditEngine->GetSelection();
348 std::vector<CFX_RectF> rects = m_pEditEngine->GetCharacterRectsInRange(
349 checked_cast<int32_t>(sel_start), checked_cast<int32_t>(count));
350
351 CFGAS_GEPath path;
352 for (auto& rect : rects) {
353 rect.left += fOffSetX;
354 rect.top += fOffSetY;
355 path.AddRectangle(rect.left, rect.top, rect.width, rect.height);
356 }
357 pGraphics->SetClipRect(rtClip);
358
359 CFWL_ThemeBackground param(CFWL_ThemePart::Part::kBackground, this,
360 pGraphics);
361 param.m_matrix = mtMatrix;
362 param.SetPath(&path);
363 GetThemeProvider()->DrawBackground(param);
364 }
365
366 CFX_RenderDevice* pRenderDev = pGraphics->GetRenderDevice();
367 RenderText(pRenderDev, rtClip, mt);
368}
369
370void CFWL_Edit::RenderText(CFX_RenderDevice* pRenderDev,
371 const CFX_RectF& clipRect,
372 const CFX_Matrix& mt) {
373 DCHECK(pRenderDev);
374
375 RetainPtr<CFGAS_GEFont> font = m_pEditEngine->GetFont();
376 if (!font)
377 return;
378
379 pRenderDev->SetClip_Rect(clipRect.GetOuterRect());
380
381 CFX_RectF rtDocClip = clipRect;
382 if (rtDocClip.IsEmpty()) {
383 rtDocClip.left = 0;
384 rtDocClip.top = 0;
385 rtDocClip.width = static_cast<float>(pRenderDev->GetWidth());
386 rtDocClip.height = static_cast<float>(pRenderDev->GetHeight());
387 }
388 rtDocClip = mt.GetInverse().TransformRect(rtDocClip);
389
390 for (const FDE_TEXTEDITPIECE& info : m_pEditEngine->GetTextPieces()) {
391 // If this character is outside the clip, skip it.
392 if (!rtDocClip.IntersectWith(info.rtPiece))
393 continue;
394
395 std::vector<TextCharPos> char_pos = m_pEditEngine->GetDisplayPos(info);
396 if (char_pos.empty())
397 continue;
398
399 CFDE_TextOut::DrawString(pRenderDev, m_pEditEngine->GetFontColor(), font,
400 char_pos, m_pEditEngine->GetFontSize(), mt);
401 }
402}
403
404void CFWL_Edit::UpdateEditEngine() {
405 UpdateEditParams();
406 UpdateEditLayout();
407}
408
409void CFWL_Edit::UpdateEditParams() {
410 m_pEditEngine->SetAvailableWidth(m_EngineRect.width);
411 m_pEditEngine->SetCombText(
412 !!(m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_CombText));
413
414 m_pEditEngine->EnableValidation(
415 !!(m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_Validate));
416 m_pEditEngine->EnablePasswordMode(
417 !!(m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_Password));
418
419 uint32_t alignment = 0;
422 alignment |= CFX_TxtLineAlignment_Left;
423 break;
424 }
426 alignment |= CFX_TxtLineAlignment_Center;
427 break;
428 }
430 alignment |= CFX_TxtLineAlignment_Right;
431 break;
432 }
433 default:
434 break;
435 }
439 break;
440 }
441 default:
442 break;
443 }
444 m_pEditEngine->SetAlignment(alignment);
445
446 bool auto_hscroll =
449 m_pEditEngine->EnableMultiLine(true);
450 m_pEditEngine->EnableLineWrap(!auto_hscroll);
451 m_pEditEngine->LimitVerticalScroll(
452 (m_Properties.m_dwStyles & FWL_STYLE_WGT_VScroll) == 0 &&
453 (m_Properties.m_dwStyleExts & FWL_STYLEEXT_EDT_AutoVScroll) == 0);
454 } else {
455 m_pEditEngine->EnableMultiLine(false);
456 m_pEditEngine->EnableLineWrap(false);
457 m_pEditEngine->LimitVerticalScroll(false);
458 }
459 m_pEditEngine->LimitHorizontalScroll(!auto_hscroll);
460
463 m_fFontSize = theme->GetFontSize(part);
464
465 RetainPtr<CFGAS_GEFont> pFont = theme->GetFont(part);
466 if (!pFont)
467 return;
468
469 m_pEditEngine->SetFont(pFont);
470 m_pEditEngine->SetFontColor(theme->GetTextColor(part));
471 m_pEditEngine->SetFontSize(m_fFontSize);
472 m_pEditEngine->SetLineSpace(theme->GetLineHeight(part));
473 m_pEditEngine->SetTabWidth(m_fFontSize);
474 m_pEditEngine->SetVisibleLineCount(m_EngineRect.height /
475 theme->GetLineHeight(part));
476}
477
478void CFWL_Edit::UpdateEditLayout() {
479 m_pEditEngine->Layout();
480}
481
482bool CFWL_Edit::UpdateOffset() {
483 CFX_RectF rtCaret = m_CaretRect;
484
485 float fOffSetX = m_EngineRect.left - m_fScrollOffsetX;
486 float fOffSetY = m_EngineRect.top - m_fScrollOffsetY + m_fVAlignOffset;
487 rtCaret.Offset(fOffSetX, fOffSetY);
488
489 const CFX_RectF& edit_bounds = m_EngineRect;
490 if (edit_bounds.Contains(rtCaret)) {
491 CFX_RectF contents_bounds = m_pEditEngine->GetContentsBoundingBox();
492 contents_bounds.Offset(fOffSetX, fOffSetY);
493 if (contents_bounds.right() < edit_bounds.right() && m_fScrollOffsetX > 0) {
494 m_fScrollOffsetX += contents_bounds.right() - edit_bounds.right();
495 m_fScrollOffsetX = std::max(m_fScrollOffsetX, 0.0f);
496 }
497 if (contents_bounds.bottom() < edit_bounds.bottom() &&
498 m_fScrollOffsetY > 0) {
499 m_fScrollOffsetY += contents_bounds.bottom() - edit_bounds.bottom();
500 m_fScrollOffsetY = std::max(m_fScrollOffsetY, 0.0f);
501 }
502 return false;
503 }
504
505 float offsetX = 0.0;
506 float offsetY = 0.0;
507 if (rtCaret.left < edit_bounds.left)
508 offsetX = rtCaret.left - edit_bounds.left;
509 if (rtCaret.right() > edit_bounds.right())
510 offsetX = rtCaret.right() - edit_bounds.right();
511 if (rtCaret.top < edit_bounds.top)
512 offsetY = rtCaret.top - edit_bounds.top;
513 if (rtCaret.bottom() > edit_bounds.bottom())
514 offsetY = rtCaret.bottom() - edit_bounds.bottom();
515
516 m_fScrollOffsetX += offsetX;
517 m_fScrollOffsetY += offsetY;
518 if (m_fFontSize > m_EngineRect.height)
519 m_fScrollOffsetY = 0;
520
521 return true;
522}
523
524bool CFWL_Edit::UpdateOffset(CFWL_ScrollBar* pScrollBar, float fPosChanged) {
525 m_fScrollOffsetY += fPosChanged;
526 return true;
527}
528
529void CFWL_Edit::UpdateVAlignment() {
532 const CFX_SizeF pSpace = theme->GetSpaceAboveBelow(part);
533 const float fSpaceAbove = pSpace.width >= 0.1f ? pSpace.width : 0.0f;
534 const float fSpaceBelow = pSpace.height >= 0.1f ? pSpace.height : 0.0f;
535 float fOffsetY = 0.0f;
536 CFX_RectF contents_bounds = m_pEditEngine->GetContentsBoundingBox();
538 fOffsetY = (m_EngineRect.height - contents_bounds.height) / 2.0f;
539 if (fOffsetY < (fSpaceAbove + fSpaceBelow) / 2.0f &&
540 fSpaceAbove < fSpaceBelow) {
541 return;
542 }
543 fOffsetY += (fSpaceAbove - fSpaceBelow) / 2.0f;
545 fOffsetY = (m_EngineRect.height - contents_bounds.height);
546 fOffsetY -= fSpaceBelow;
547 } else {
548 fOffsetY += fSpaceAbove;
549 }
550 m_fVAlignOffset = std::max(fOffsetY, 0.0f);
551}
552
553void CFWL_Edit::UpdateCaret() {
554 CFX_RectF rtCaret = m_CaretRect;
555 rtCaret.Offset(m_EngineRect.left - m_fScrollOffsetX,
556 m_EngineRect.top - m_fScrollOffsetY + m_fVAlignOffset);
557
558 CFX_RectF rtClient = GetClientRect();
559 rtCaret.Intersect(rtClient);
560 if (rtCaret.left > rtClient.right()) {
561 float right = rtCaret.right();
562 rtCaret.left = rtClient.right() - 1;
563 rtCaret.width = right - rtCaret.left;
564 }
565
567 ShowCaret(&rtCaret);
568 else
569 HideCaret(&rtCaret);
570}
571
572CFWL_ScrollBar* CFWL_Edit::UpdateScroll() {
573 bool bShowVert = m_pVertScrollBar && m_pVertScrollBar->IsVisible();
574 if (!bShowVert)
575 return nullptr;
576
577 CFX_RectF contents_bounds = m_pEditEngine->GetContentsBoundingBox();
578 CFX_RectF rtScroll = m_pVertScrollBar->GetWidgetRect();
579 if (rtScroll.height < contents_bounds.height) {
580 float fStep = m_pEditEngine->GetLineSpace();
581 float fRange =
582 std::max(contents_bounds.height - m_EngineRect.height, fStep);
583 m_pVertScrollBar->SetRange(0.0f, fRange);
584 float fPos = std::clamp(m_fScrollOffsetY, 0.0f, fRange);
585 m_pVertScrollBar->SetPos(fPos);
586 m_pVertScrollBar->SetTrackPos(fPos);
587 m_pVertScrollBar->SetPageSize(rtScroll.height);
588 m_pVertScrollBar->SetStepSize(fStep);
589 m_pVertScrollBar->RemoveStates(FWL_STATE_WGT_Disabled);
590 m_pVertScrollBar->Update();
591 return m_pVertScrollBar;
592 }
593 if ((m_pVertScrollBar->GetStates() & FWL_STATE_WGT_Disabled) == 0) {
594 m_pVertScrollBar->SetRange(0, -1);
595 m_pVertScrollBar->SetStates(FWL_STATE_WGT_Disabled);
596 m_pVertScrollBar->Update();
597 return m_pVertScrollBar;
598 }
599 return nullptr;
600}
601
602bool CFWL_Edit::IsShowVertScrollBar() const {
603 const bool bShow =
608 IsContentHeightOverflow();
609}
610
611bool CFWL_Edit::IsContentHeightOverflow() const {
612 return m_pEditEngine->GetContentsBoundingBox().height >
613 m_EngineRect.height + 1.0f;
614}
615
616void CFWL_Edit::Layout() {
617 m_ClientRect = GetClientRect();
618 m_EngineRect = m_ClientRect;
619
621 float fWidth = theme->GetScrollBarWidth();
622 if (!GetOuter()) {
624 CFX_RectF pUIMargin = theme->GetUIMargin(part);
625 m_EngineRect.Deflate(pUIMargin.left, pUIMargin.top, pUIMargin.width,
626 pUIMargin.height);
627 } else if (GetOuter()->GetClassID() == FWL_Type::DateTimePicker) {
629 CFX_RectF pUIMargin = theme->GetUIMargin(part);
630 m_EngineRect.Deflate(pUIMargin.left, pUIMargin.top, pUIMargin.width,
631 pUIMargin.height);
632 }
633
634 bool bShowVertScrollbar = IsShowVertScrollBar();
635 if (bShowVertScrollbar) {
636 InitVerticalScrollBar();
637
638 CFX_RectF rtVertScr;
640 rtVertScr = CFX_RectF(m_ClientRect.right() + kEditMargin,
641 m_ClientRect.top, fWidth, m_ClientRect.height);
642 } else {
643 rtVertScr = CFX_RectF(m_ClientRect.right() - fWidth, m_ClientRect.top,
644 fWidth, m_ClientRect.height);
645 m_EngineRect.width -= fWidth;
646 }
647
648 m_pVertScrollBar->SetWidgetRect(rtVertScr);
649 m_pVertScrollBar->RemoveStates(FWL_STATE_WGT_Invisible);
650 m_pVertScrollBar->Update();
651 } else if (m_pVertScrollBar) {
652 m_pVertScrollBar->SetStates(FWL_STATE_WGT_Invisible);
653 }
654}
655
656void CFWL_Edit::LayoutScrollBar() {
658 return;
659
660 bool bShowVertScrollbar = IsShowVertScrollBar();
662 float fWidth = theme->GetScrollBarWidth();
663 if (bShowVertScrollbar) {
664 if (!m_pVertScrollBar) {
665 InitVerticalScrollBar();
666 CFX_RectF rtVertScr;
668 rtVertScr = CFX_RectF(m_ClientRect.right() + kEditMargin,
669 m_ClientRect.top, fWidth, m_ClientRect.height);
670 } else {
671 rtVertScr = CFX_RectF(m_ClientRect.right() - fWidth, m_ClientRect.top,
672 fWidth, m_ClientRect.height);
673 }
674 m_pVertScrollBar->SetWidgetRect(rtVertScr);
675 m_pVertScrollBar->Update();
676 }
677 m_pVertScrollBar->RemoveStates(FWL_STATE_WGT_Invisible);
678 } else if (m_pVertScrollBar) {
679 m_pVertScrollBar->SetStates(FWL_STATE_WGT_Invisible);
680 }
681 if (bShowVertScrollbar)
682 UpdateScroll();
683}
684
685CFX_PointF CFWL_Edit::DeviceToEngine(const CFX_PointF& pt) {
686 return pt + CFX_PointF(m_fScrollOffsetX - m_EngineRect.left,
687 m_fScrollOffsetY - m_EngineRect.top - m_fVAlignOffset);
688}
689
690void CFWL_Edit::InitVerticalScrollBar() {
691 if (m_pVertScrollBar)
692 return;
693
694 m_pVertScrollBar = cppgc::MakeGarbageCollected<CFWL_ScrollBar>(
695 GetFWLApp()->GetHeap()->GetAllocationHandle(), GetFWLApp(),
696 Properties{0, FWL_STYLEEXT_SCB_Vert,
698 this);
699}
700
702 if (m_pCaret) {
703 m_pCaret->ShowCaret();
704 if (!pRect->IsEmpty())
705 m_pCaret->SetWidgetRect(*pRect);
706 RepaintRect(m_EngineRect);
707 return;
708 }
709
710 CFWL_Widget* pOuter = this;
712 while (pOuter->GetOuter()) {
713 pOuter = pOuter->GetOuter();
714 CFX_RectF rtOuter = pOuter->GetWidgetRect();
715 pRect->Offset(rtOuter.left, rtOuter.top);
716 }
717
718 CFWL_Widget::AdapterIface* pXFAWidget = pOuter->GetAdapterIface();
719 if (!pXFAWidget)
720 return;
721
723 pXFAWidget->DisplayCaret(true, &rt);
724}
725
727 if (m_pCaret) {
728 m_pCaret->HideCaret();
729 RepaintRect(m_EngineRect);
730 return;
731 }
732
733 CFWL_Widget* pOuter = this;
734 while (pOuter->GetOuter())
735 pOuter = pOuter->GetOuter();
736
737 CFWL_Widget::AdapterIface* pXFAWidget = pOuter->GetAdapterIface();
738 if (!pXFAWidget)
739 return;
740
741 pXFAWidget->DisplayCaret(false, pRect);
742}
743
744void CFWL_Edit::InitCaret() {
745 if (m_pCaret)
746 return;
747
748 m_pCaret = cppgc::MakeGarbageCollected<CFWL_Caret>(
749 GetFWLApp()->GetHeap()->GetAllocationHandle(), GetFWLApp(), Properties(),
750 this);
751 m_pCaret->SetStates(m_Properties.m_dwStates);
752 UpdateCursorRect();
753}
754
755void CFWL_Edit::UpdateCursorRect() {
756 int32_t bidi_level;
757 if (m_pEditEngine->CanGenerateCharacterInfo()) {
758 std::tie(bidi_level, m_CaretRect) = m_pEditEngine->GetCharacterInfo(
759 checked_cast<int32_t>(m_CursorPosition));
760 } else {
761 bidi_level = 0;
762 m_CaretRect = CFX_RectF();
763 }
764
765 // TODO(dsinclair): This should handle bidi level ...
766
767 m_CaretRect.width = 1.0f;
768
769 // TODO(hnakashima): Handle correctly edits with empty text instead of using
770 // these defaults.
771 if (m_CaretRect.height == 0)
772 m_CaretRect.height = 8.0f;
773}
774
775void CFWL_Edit::SetCursorPosition(size_t position) {
776 if (m_CursorPosition == position)
777 return;
778
779 m_CursorPosition = std::min(position, m_pEditEngine->GetLength());
780 UpdateCursorRect();
782}
783
785 switch (pMessage->GetType()) {
787 OnFocusGained();
788 break;
790 OnFocusLost();
791 break;
793 CFWL_MessageMouse* pMsg = static_cast<CFWL_MessageMouse*>(pMessage);
794 switch (pMsg->m_dwCmd) {
795 case CFWL_MessageMouse::MouseCommand::kLeftButtonDown:
796 OnLButtonDown(pMsg);
797 break;
798 case CFWL_MessageMouse::MouseCommand::kLeftButtonUp:
799 OnLButtonUp(pMsg);
800 break;
801 case CFWL_MessageMouse::MouseCommand::kLeftButtonDblClk:
802 OnButtonDoubleClick(pMsg);
803 break;
804 case CFWL_MessageMouse::MouseCommand::kMove:
805 OnMouseMove(pMsg);
806 break;
807 case CFWL_MessageMouse::MouseCommand::kRightButtonDown:
808 DoRButtonDown(pMsg);
809 break;
810 default:
811 break;
812 }
813 break;
814 }
816 CFWL_MessageKey* pKey = static_cast<CFWL_MessageKey*>(pMessage);
817 if (pKey->m_dwCmd == CFWL_MessageKey::KeyCommand::kKeyDown)
818 OnKeyDown(pKey);
819 else if (pKey->m_dwCmd == CFWL_MessageKey::KeyCommand::kChar)
820 OnChar(pKey);
821 break;
822 }
823 default:
824 break;
825 }
826 // Dst target could be |this|, continue only if not destroyed by above.
827 if (pMessage->GetDstTarget())
829}
830
832 if (!pEvent || pEvent->GetType() != CFWL_Event::Type::Scroll)
833 return;
834
835 CFWL_Widget* pSrcTarget = pEvent->GetSrcTarget();
836 if ((pSrcTarget == m_pVertScrollBar && m_pVertScrollBar)) {
837 CFWL_EventScroll* pScrollEvent = static_cast<CFWL_EventScroll*>(pEvent);
838 OnScroll(static_cast<CFWL_ScrollBar*>(pSrcTarget),
839 pScrollEvent->GetScrollCode(), pScrollEvent->GetPos());
840 }
841}
842
844 const CFX_Matrix& matrix) {
845 DrawWidget(pGraphics, matrix);
846}
847
848void CFWL_Edit::DoRButtonDown(CFWL_MessageMouse* pMsg) {
849 SetCursorPosition(
850 m_pEditEngine->GetIndexForPoint(DeviceToEngine(pMsg->m_pos)));
851}
852
853void CFWL_Edit::OnFocusGained() {
855 UpdateVAlignment();
856 UpdateOffset();
857 UpdateCaret();
858 LayoutScrollBar();
859}
860
861void CFWL_Edit::OnFocusLost() {
862 bool bRepaint = false;
865 HideCaret(nullptr);
866 if (HasSelection()) {
868 bRepaint = true;
869 }
870 UpdateOffset();
871 }
872 LayoutScrollBar();
873 if (!bRepaint)
874 return;
875
877}
878
879void CFWL_Edit::OnLButtonDown(CFWL_MessageMouse* pMsg) {
881 return;
882
883 m_bLButtonDown = true;
884 SetGrab(true);
885
886 bool bRepaint = false;
887 if (m_pEditEngine->HasSelection()) {
888 m_pEditEngine->ClearSelection();
889 bRepaint = true;
890 }
891
892 size_t index_at_click =
893 m_pEditEngine->GetIndexForPoint(DeviceToEngine(pMsg->m_pos));
894
895 if (index_at_click != m_CursorPosition &&
896 !!(pMsg->m_dwFlags & XFA_FWL_KeyFlag::kShift)) {
897 size_t start = std::min(m_CursorPosition, index_at_click);
898 size_t end = std::max(m_CursorPosition, index_at_click);
899
900 m_pEditEngine->SetSelection(start, end - start);
901 bRepaint = true;
902 } else {
903 SetCursorPosition(index_at_click);
904 }
905
906 if (bRepaint)
907 RepaintRect(m_EngineRect);
908}
909
910void CFWL_Edit::OnLButtonUp(CFWL_MessageMouse* pMsg) {
911 m_bLButtonDown = false;
912 SetGrab(false);
913}
914
915void CFWL_Edit::OnButtonDoubleClick(CFWL_MessageMouse* pMsg) {
916 size_t click_idx =
917 m_pEditEngine->GetIndexForPoint(DeviceToEngine(pMsg->m_pos));
918 auto [start_idx, count] = m_pEditEngine->BoundsForWordAt(click_idx);
919
920 m_pEditEngine->SetSelection(start_idx, count);
921 m_CursorPosition = start_idx + count;
922 RepaintRect(m_EngineRect);
923}
924
925void CFWL_Edit::OnMouseMove(CFWL_MessageMouse* pMsg) {
926 bool shift = !!(pMsg->m_dwFlags & XFA_FWL_KeyFlag::kShift);
927 if (!m_bLButtonDown || !shift)
928 return;
929
930 size_t old_cursor_pos = m_CursorPosition;
931 SetCursorPosition(
932 m_pEditEngine->GetIndexForPoint(DeviceToEngine(pMsg->m_pos)));
933 if (old_cursor_pos == m_CursorPosition)
934 return;
935
936 size_t length = m_pEditEngine->GetLength();
937 if (m_CursorPosition > length)
938 SetCursorPosition(length);
939
940 size_t sel_start = 0;
941 size_t count = 0;
942 if (m_pEditEngine->HasSelection())
943 std::tie(sel_start, count) = m_pEditEngine->GetSelection();
944 else
945 sel_start = old_cursor_pos;
946
947 size_t start_pos = std::min(sel_start, m_CursorPosition);
948 size_t end_pos = std::max(sel_start, m_CursorPosition);
949 m_pEditEngine->SetSelection(start_pos, end_pos - start_pos);
950}
951
952void CFWL_Edit::OnKeyDown(CFWL_MessageKey* pMsg) {
953 bool bShift = !!(pMsg->m_dwFlags & XFA_FWL_KeyFlag::kShift);
954 bool bCtrl = !!(pMsg->m_dwFlags & XFA_FWL_KeyFlag::kCtrl);
955
956 size_t sel_start = m_CursorPosition;
957 if (m_pEditEngine->HasSelection()) {
958 auto [start_idx, count] = m_pEditEngine->GetSelection();
959 sel_start = start_idx;
960 }
961
962 switch (pMsg->m_dwKeyCodeOrChar) {
963 case XFA_FWL_VKEY_Left:
964 SetCursorPosition(m_pEditEngine->GetIndexLeft(m_CursorPosition));
965 break;
966 case XFA_FWL_VKEY_Right:
967 SetCursorPosition(m_pEditEngine->GetIndexRight(m_CursorPosition));
968 break;
969 case XFA_FWL_VKEY_Up:
970 SetCursorPosition(m_pEditEngine->GetIndexUp(m_CursorPosition));
971 break;
972 case XFA_FWL_VKEY_Down:
973 SetCursorPosition(m_pEditEngine->GetIndexDown(m_CursorPosition));
974 break;
975 case XFA_FWL_VKEY_Home:
976 SetCursorPosition(
977 bCtrl ? 0 : m_pEditEngine->GetIndexAtStartOfLine(m_CursorPosition));
978 break;
979 case XFA_FWL_VKEY_End:
980 SetCursorPosition(
981 bCtrl ? m_pEditEngine->GetLength()
982 : m_pEditEngine->GetIndexAtEndOfLine(m_CursorPosition));
983 break;
984 case XFA_FWL_VKEY_Delete: {
987 break;
988 }
989
990 m_pEditEngine->Delete(m_CursorPosition, 1);
991 UpdateCaret();
992 break;
993 }
995 case XFA_FWL_VKEY_F2:
996 case XFA_FWL_VKEY_Tab:
997 default:
998 break;
999 }
1000
1001 // Update the selection.
1002 if (bShift && sel_start != m_CursorPosition) {
1003 m_pEditEngine->SetSelection(std::min(sel_start, m_CursorPosition),
1004 std::max(sel_start, m_CursorPosition));
1005 RepaintRect(m_EngineRect);
1006 }
1007}
1008
1009void CFWL_Edit::OnChar(CFWL_MessageKey* pMsg) {
1012 return;
1013 }
1014
1015 wchar_t c = static_cast<wchar_t>(pMsg->m_dwKeyCodeOrChar);
1016 switch (c) {
1017 case L'\b':
1018 if (m_CursorPosition > 0) {
1019 SetCursorPosition(m_CursorPosition - 1);
1020 m_pEditEngine->Delete(m_CursorPosition, 1);
1021 UpdateCaret();
1022 }
1023 break;
1024 case L'\n':
1025 case 27: // Esc
1026 case 127: // Delete
1027 break;
1028 case L'\t':
1029 m_pEditEngine->Insert(m_CursorPosition, L"\t");
1030 SetCursorPosition(m_CursorPosition + 1);
1031 break;
1032 case L'\r':
1034 m_pEditEngine->Insert(m_CursorPosition, L"\n");
1035 SetCursorPosition(m_CursorPosition + 1);
1036 }
1037 break;
1038 default: {
1039 if (pMsg->m_dwFlags & kEditingModifier)
1040 break;
1041
1042 m_pEditEngine->Insert(m_CursorPosition, WideString(c));
1043 SetCursorPosition(m_CursorPosition + 1);
1044 break;
1045 }
1046 }
1047}
1048
1049bool CFWL_Edit::OnScroll(CFWL_ScrollBar* pScrollBar,
1050 CFWL_EventScroll::Code dwCode,
1051 float fPos) {
1052 float fMin;
1053 float fMax;
1054 pScrollBar->GetRange(&fMin, &fMax);
1055 float iCurPos = pScrollBar->GetPos();
1056 float fStep = pScrollBar->GetStepSize();
1057 switch (dwCode) {
1058 case CFWL_EventScroll::Code::Min: {
1059 fPos = fMin;
1060 break;
1061 }
1062 case CFWL_EventScroll::Code::Max: {
1063 fPos = fMax;
1064 break;
1065 }
1066 case CFWL_EventScroll::Code::StepBackward: {
1067 fPos -= fStep;
1068 if (fPos < fMin + fStep / 2) {
1069 fPos = fMin;
1070 }
1071 break;
1072 }
1073 case CFWL_EventScroll::Code::StepForward: {
1074 fPos += fStep;
1075 if (fPos > fMax - fStep / 2) {
1076 fPos = fMax;
1077 }
1078 break;
1079 }
1080 case CFWL_EventScroll::Code::PageBackward: {
1081 fPos -= pScrollBar->GetPageSize();
1082 if (fPos < fMin) {
1083 fPos = fMin;
1084 }
1085 break;
1086 }
1087 case CFWL_EventScroll::Code::PageForward: {
1088 fPos += pScrollBar->GetPageSize();
1089 if (fPos > fMax) {
1090 fPos = fMax;
1091 }
1092 break;
1093 }
1094 case CFWL_EventScroll::Code::Pos:
1095 case CFWL_EventScroll::Code::TrackPos:
1096 case CFWL_EventScroll::Code::None:
1097 break;
1098 case CFWL_EventScroll::Code::EndScroll:
1099 return false;
1100 }
1101 if (iCurPos == fPos)
1102 return true;
1103
1104 pScrollBar->SetPos(fPos);
1105 pScrollBar->SetTrackPos(fPos);
1106 UpdateOffset(pScrollBar, fPos - iCurPos);
1107 UpdateCaret();
1108
1110 RepaintRect(CFX_RectF(0, 0, rect.width + 2, rect.height + 2));
1111 return true;
1112}
1113
1114} // namespace pdfium
@ CFX_TxtLineAlignment_Justified
@ CFX_TxtLineAlignment_Right
@ CFX_TxtLineAlignment_Center
@ CFX_TxtLineAlignment_Left
#define FWL_STYLEEXT_EDT_MultiLine
Definition cfwl_edit.h:24
#define FWL_STYLEEXT_EDT_HFar
Definition cfwl_edit.h:34
#define FWL_STYLEEXT_EDT_VAlignMask
Definition cfwl_edit.h:40
#define FWL_STYLEEXT_EDT_HAlignModeMask
Definition cfwl_edit.h:41
#define FWL_STYLEEXT_EDT_HAlignMask
Definition cfwl_edit.h:39
#define FWL_STYLEEXT_EDT_ShowScrollbarFocus
Definition cfwl_edit.h:42
#define FWL_STYLEEXT_EDT_WantReturn
Definition cfwl_edit.h:25
#define FWL_STYLEEXT_EDT_VCenter
Definition cfwl_edit.h:36
#define FWL_STYLEEXT_EDT_OuterScrollbar
Definition cfwl_edit.h:43
#define FWL_STYLEEXT_EDT_AutoHScroll
Definition cfwl_edit.h:26
#define FWL_STYLEEXT_EDT_ReadOnly
Definition cfwl_edit.h:23
#define FWL_STYLEEXT_EDT_VFar
Definition cfwl_edit.h:37
#define FWL_STYLEEXT_EDT_AutoVScroll
Definition cfwl_edit.h:27
#define FWL_STYLEEXT_EDT_Validate
Definition cfwl_edit.h:28
#define FWL_STYLEEXT_EDT_HNear
Definition cfwl_edit.h:32
#define FWL_STYLEEXT_EDT_Password
Definition cfwl_edit.h:29
#define FWL_STYLEEXT_EDT_Justified
Definition cfwl_edit.h:38
#define FWL_STYLEEXT_EDT_CombText
Definition cfwl_edit.h:31
#define FWL_STYLEEXT_EDT_HCenter
Definition cfwl_edit.h:33
#define FWL_STYLEEXT_SCB_Vert
#define FWL_STATE_WGT_Invisible
Definition cfwl_widget.h:45
#define FWL_STYLE_WGT_VScroll
Definition cfwl_widget.h:39
#define FWL_STATE_WGT_Focused
Definition cfwl_widget.h:44
#define FWL_STATE_WGT_Disabled
Definition cfwl_widget.h:43
#define DCHECK
Definition check.h:33
StateRestorer(CFGAS_GEGraphics *graphics)
CFX_RenderDevice * GetRenderDevice()
void SetClipRect(const CFX_RectF &rect)
void AddLine(const CFX_PointF &p1, const CFX_PointF &p2)
CFX_Matrix & operator=(const CFX_Matrix &other)=default
CFX_RectF TransformRect(const CFX_RectF &rect) const
CFX_Matrix GetInverse() const
constexpr CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1)
void Concat(const CFX_Matrix &right)
void Offset(float dx, float dy)
constexpr CFX_RectF()=default
bool IsEmpty() const
bool Contains(const CFX_RectF &rt) const
void Intersect(const CFX_RectF &rt)
FX_RECT GetOuterRect() const
void Deflate(float off_left, float off_top, float off_right, float off_bottom)
CFX_RectF & operator=(const CFX_RectF &other)=default
float bottom() const
bool Contains(const PointType &p) const
float right() const
constexpr CFX_RectF(float dst_left, float dst_top, float dst_width, float dst_height)
void Union(const CFX_RectF &rt)
bool SetClip_Rect(const FX_RECT &pRect)
int GetHeight() const
void OnProcessMessage(CFWL_Message *pMessage) override
void SetAliasChar(wchar_t wAlias)
void SetScrollOffset(float fScrollOffset) override
void OnTextWillChange(CFDE_TextEditEngine::TextChange *change) override
FWL_WidgetHit HitTest(const CFX_PointF &point) override
WideString GetText() const
void OnDrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
CFX_RectF GetWidgetRect() override
Definition cfwl_edit.cpp:79
virtual void SetTextSkipNotify(const WideString &wsText)
bool OnValidate(const WideString &wsText) override
void DrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
std::pair< size_t, size_t > GetSelection() const
void Trace(cppgc::Visitor *visitor) const override
Definition cfwl_edit.cpp:69
bool Paste(const WideString &wsPaste)
void NotifyTextFull() override
void Update() override
void OnTextChanged() override
CFWL_Edit(CFWL_App *app, const Properties &properties, CFWL_Widget *pOuter)
Definition cfwl_edit.cpp:52
void OnSelChanged() override
~CFWL_Edit() override
int32_t GetLimit() const
std::optional< WideString > Copy()
size_t GetTextLength() const
bool HasSelection() const
std::optional< WideString > Cut()
void SetStates(uint32_t dwStates) override
FWL_Type GetClassID() const override
Definition cfwl_edit.cpp:75
void SetLimit(int32_t nLimit)
CFX_RectF GetAutosizedWidgetRect() override
Definition cfwl_edit.cpp:91
void ShowCaret(CFX_RectF *pRect)
void HideCaret(CFX_RectF *pRect)
void PreFinalize() override
Definition cfwl_edit.cpp:62
virtual void SetText(const WideString &wsText)
void OnCaretChanged() override
void OnProcessEvent(CFWL_Event *pEvent) override
Type GetType() const
Definition cfwl_event.h:41
CFWL_Widget * GetSrcTarget() const
Definition cfwl_event.h:42
const uint32_t m_dwKeyCodeOrChar
const KeyCommand m_dwCmd
Type GetType() const
CFWL_Widget * GetDstTarget() const
void SetTrackPos(float fTrackPos)
void SetPos(float fPos)
float GetPageSize() const
float GetStepSize() const
void GetRange(float *fMin, float *fMax) const
void SetPath(const CFGAS_GEPath *pPath)
virtual void DisplayCaret(bool bVisible, const CFX_RectF *pRtAnchor)=0
virtual CFX_Matrix GetRotateMatrix()=0
void OnProcessMessage(CFWL_Message *pMessage) override
virtual CFX_RectF GetWidgetRect()
void RepaintRect(const CFX_RectF &pRect)
bool HasBorder() const
void DispatchEvent(CFWL_Event *pEvent)
CFWL_Widget * GetOuter() const
void SetGrab(bool bSet)
CFWL_Widget(CFWL_App *app, const Properties &properties, CFWL_Widget *pOuter)
AdapterIface * GetAdapterIface() const
void DrawBorder(CFGAS_GEGraphics *pGraphics, CFWL_ThemePart::Part iPartBorder, const CFX_Matrix &pMatrix)
virtual void PreFinalize()
virtual CFX_RectF GetClientRect()
IFWL_ThemeProvider * GetThemeProvider() const
bool IsLocked() const
void InflateWidgetRect(CFX_RectF &rect)
Properties m_Properties
virtual void SetStates(uint32_t dwStates)
virtual float GetScrollBarWidth() const =0
virtual float GetFontSize(const CFWL_ThemePart &pThemePart) const =0
virtual CFX_RectF GetUIMargin(const CFWL_ThemePart &pThemePart) const =0
CFX_PTemplate< float > CFX_PointF
CFX_STemplate< float > CFX_SizeF
@ XFA_FWL_VKEY_Insert
@ XFA_FWL_VKEY_Tab
@ XFA_FWL_VKEY_Delete
@ XFA_FWL_VKEY_F2
fxcrt::WideString WideString
Definition widestring.h:207