blob: 3275dd1b33e46b7fb113044beb94e1e8fb0c98d7 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhang633a3b72017-06-02 15:27:22 -07007#include "fpdfsdk/pdfwindow/cpwl_caret.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -04008
dsinclair74a34fc2016-09-29 16:41:42 -07009#include "core/fxge/cfx_graphstatedata.h"
10#include "core/fxge/cfx_pathdata.h"
11#include "core/fxge/cfx_renderdevice.h"
Lei Zhang633a3b72017-06-02 15:27:22 -070012#include "fpdfsdk/pdfwindow/cpwl_utils.h"
13#include "fpdfsdk/pdfwindow/cpwl_wnd.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014
Nico Weber9d8ec5a2015-08-04 13:00:21 -070015#define PWL_CARET_FLASHINTERVAL 500
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016
Dan Sinclairf528eee2017-02-14 11:52:07 -050017PWL_CARET_INFO::PWL_CARET_INFO() : bVisible(false) {}
18
tsepez4cf55152016-11-02 14:37:54 -070019CPWL_Caret::CPWL_Caret() : m_bFlash(false), m_fWidth(0.4f), m_nDelay(0) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070020
21CPWL_Caret::~CPWL_Caret() {}
22
23CFX_ByteString CPWL_Caret::GetClassName() const {
24 return "CPWL_Caret";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025}
26
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027void CPWL_Caret::GetThisAppearanceStream(CFX_ByteTextBuf& sAppStream) {
Dan Sinclairf528eee2017-02-14 11:52:07 -050028 GetCaretApp(sAppStream, CFX_PointF());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029}
30
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031void CPWL_Caret::DrawThisAppearance(CFX_RenderDevice* pDevice,
Tom Sepez60d909e2015-12-10 15:34:55 -080032 CFX_Matrix* pUser2Device) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 if (IsVisible() && m_bFlash) {
Tom Sepez281a9ea2016-02-26 14:24:28 -080034 CFX_FloatRect rcRect = GetCaretRect();
35 CFX_FloatRect rcClip = GetClipRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 CFX_PathData path;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037
Dan Sinclair05df0752017-03-14 14:43:42 -040038 float fCaretX = rcRect.left + m_fWidth * 0.5f;
39 float fCaretTop = rcRect.top;
40 float fCaretBottom = rcRect.bottom;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 if (!rcClip.IsEmpty()) {
42 rcRect.Intersect(rcClip);
dan sinclairb147e072017-02-22 19:56:15 -050043 if (rcRect.IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 return;
dan sinclairb147e072017-02-22 19:56:15 -050045
46 fCaretTop = rcRect.top;
47 fCaretBottom = rcRect.bottom;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 }
49
dan sinclairb147e072017-02-22 19:56:15 -050050 path.AppendPoint(CFX_PointF(fCaretX, fCaretBottom), FXPT_TYPE::MoveTo,
51 false);
52 path.AppendPoint(CFX_PointF(fCaretX, fCaretTop), FXPT_TYPE::LineTo, false);
53
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 CFX_GraphStateData gsd;
55 gsd.m_LineWidth = m_fWidth;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 pDevice->DrawPath(&path, pUser2Device, &gsd, 0, ArgbEncode(255, 0, 0, 0),
57 FXFILL_ALTERNATE);
58 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059}
60
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061void CPWL_Caret::GetCaretApp(CFX_ByteTextBuf& sAppStream,
Dan Sinclairf528eee2017-02-14 11:52:07 -050062 const CFX_PointF& ptOffset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 if (IsVisible() && m_bFlash) {
64 CFX_ByteTextBuf sCaret;
65
Tom Sepez281a9ea2016-02-26 14:24:28 -080066 CFX_FloatRect rcRect = GetCaretRect();
67 CFX_FloatRect rcClip = GetClipRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068
69 rcRect = CPWL_Utils::OffsetRect(rcRect, ptOffset.x, ptOffset.y);
70 rcClip = CPWL_Utils::OffsetRect(rcClip, ptOffset.x, ptOffset.y);
71
72 sCaret << "q\n";
73 if (!rcClip.IsEmpty()) {
74 sCaret << rcClip.left << " " << rcClip.bottom + 2.5f << " "
75 << rcClip.right - rcClip.left << " "
76 << rcClip.top - rcClip.bottom - 4.5f << " re W n\n";
77 }
78 sCaret << m_fWidth << " w\n0 G\n";
79 sCaret << rcRect.left + m_fWidth / 2 << " " << rcRect.bottom << " m\n";
80 sCaret << rcRect.left + m_fWidth / 2 << " " << rcRect.top << " l S\nQ\n";
81
82 sAppStream << sCaret;
83 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084}
85
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086CFX_ByteString CPWL_Caret::GetCaretAppearanceStream(
Dan Sinclairf528eee2017-02-14 11:52:07 -050087 const CFX_PointF& ptOffset) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 CFX_ByteTextBuf sCaret;
89 GetCaretApp(sCaret, ptOffset);
tsepez71a452f2016-05-13 17:51:27 -070090 return sCaret.MakeString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091}
92
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093void CPWL_Caret::TimerProc() {
94 if (m_nDelay > 0) {
95 m_nDelay--;
96 } else {
97 m_bFlash = !m_bFlash;
98 InvalidateRect();
99 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100}
101
Tom Sepez281a9ea2016-02-26 14:24:28 -0800102CFX_FloatRect CPWL_Caret::GetCaretRect() const {
103 return CFX_FloatRect(m_ptFoot.x, m_ptFoot.y, m_ptHead.x + m_fWidth,
104 m_ptHead.y);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700105}
106
tsepez4cf55152016-11-02 14:37:54 -0700107void CPWL_Caret::SetCaret(bool bVisible,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500108 const CFX_PointF& ptHead,
109 const CFX_PointF& ptFoot) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 if (bVisible) {
111 if (IsVisible()) {
tsepez63f545c2016-09-13 16:08:49 -0700112 if (m_ptHead != ptHead || m_ptFoot != ptFoot) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 m_ptHead = ptHead;
114 m_ptFoot = ptFoot;
tsepez4cf55152016-11-02 14:37:54 -0700115 m_bFlash = true;
116 Move(m_rcInvalid, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 }
118 } else {
119 m_ptHead = ptHead;
120 m_ptFoot = ptFoot;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121 EndTimer();
122 BeginTimer(PWL_CARET_FLASHINTERVAL);
tsepez4cf55152016-11-02 14:37:54 -0700123 CPWL_Wnd::SetVisible(true);
124 m_bFlash = true;
125 Move(m_rcInvalid, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 }
127 } else {
Dan Sinclairf528eee2017-02-14 11:52:07 -0500128 m_ptHead = CFX_PointF();
129 m_ptFoot = CFX_PointF();
tsepez4cf55152016-11-02 14:37:54 -0700130 m_bFlash = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 if (IsVisible()) {
132 EndTimer();
tsepez4cf55152016-11-02 14:37:54 -0700133 CPWL_Wnd::SetVisible(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 }
135 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700136}
137
Tom Sepez281a9ea2016-02-26 14:24:28 -0800138void CPWL_Caret::InvalidateRect(CFX_FloatRect* pRect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 if (pRect) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800140 CFX_FloatRect rcRefresh = CPWL_Utils::InflateRect(*pRect, 0.5f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141 rcRefresh.top += 1;
142 rcRefresh.bottom -= 1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143 CPWL_Wnd::InvalidateRect(&rcRefresh);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800144 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 CPWL_Wnd::InvalidateRect(pRect);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800146 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147}