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