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_checkboxtp.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/theme/cfwl_checkboxtp.h"
8
9#include <math.h>
10
11#include <array>
12#include <iterator>
13
14#include "core/fxge/cfx_path.h"
15#include "xfa/fde/cfde_textout.h"
16#include "xfa/fgas/graphics/cfgas_gecolor.h"
17#include "xfa/fgas/graphics/cfgas_gegraphics.h"
18#include "xfa/fgas/graphics/cfgas_gepath.h"
19#include "xfa/fwl/cfwl_checkbox.h"
20#include "xfa/fwl/cfwl_themebackground.h"
21#include "xfa/fwl/cfwl_themetext.h"
22#include "xfa/fwl/cfwl_widget.h"
23
24namespace pdfium {
25
26namespace {
27
28constexpr int kSignPath = 100;
29
30CFX_PointF ScaleBezierPoint(const CFX_PointF& point) {
31 CFX_PointF scaled_point(point);
32 scaled_point.x *= FXSYS_BEZIER;
33 scaled_point.y *= FXSYS_BEZIER;
34 return scaled_point;
35}
36
37} // namespace
38
39CFWL_CheckBoxTP::CFWL_CheckBoxTP() = default;
40
41CFWL_CheckBoxTP::~CFWL_CheckBoxTP() = default;
42
43void CFWL_CheckBoxTP::DrawText(const CFWL_ThemeText& pParams) {
44 EnsureTTOInitialized(pParams.GetWidget()->GetThemeProvider());
45 m_pTextOut->SetTextColor(pParams.m_dwStates & CFWL_PartState::kDisabled
49}
50
51void CFWL_CheckBoxTP::DrawSignCheck(CFGAS_GEGraphics* pGraphics,
52 const CFX_RectF& rtSign,
53 FX_ARGB argbFill,
54 const CFX_Matrix& matrix) {
55 EnsureCheckPathInitialized(rtSign.width);
56 DCHECK(m_pCheckPath);
57
58 CFX_Matrix mt;
59 mt.Translate(rtSign.left, rtSign.top);
60 mt.Concat(matrix);
61
62 CFGAS_GEGraphics::StateRestorer restorer(pGraphics);
63 pGraphics->SetFillColor(CFGAS_GEColor(argbFill));
64 pGraphics->FillPath(*m_pCheckPath, CFX_FillRenderOptions::FillType::kWinding,
65 mt);
66}
67
68void CFWL_CheckBoxTP::DrawSignCircle(CFGAS_GEGraphics* pGraphics,
69 const CFX_RectF& rtSign,
70 FX_ARGB argbFill,
71 const CFX_Matrix& matrix) {
72 CFGAS_GEPath path;
73 path.AddEllipse(rtSign);
74
75 CFGAS_GEGraphics::StateRestorer restorer(pGraphics);
76 pGraphics->SetFillColor(CFGAS_GEColor(argbFill));
78}
79
80void CFWL_CheckBoxTP::DrawSignCross(CFGAS_GEGraphics* pGraphics,
81 const CFX_RectF& rtSign,
82 FX_ARGB argbFill,
83 const CFX_Matrix& matrix) {
84 CFGAS_GEPath path;
85 const float fRight = rtSign.right();
86 const float fBottom = rtSign.bottom();
87 path.AddLine(rtSign.TopLeft(), CFX_PointF(fRight, fBottom));
88 path.AddLine(CFX_PointF(rtSign.left, fBottom),
89 CFX_PointF(fRight, rtSign.top));
90
91 CFGAS_GEGraphics::StateRestorer restorer(pGraphics);
92 pGraphics->SetStrokeColor(CFGAS_GEColor(argbFill));
93 pGraphics->SetLineWidth(1.0f);
94 pGraphics->StrokePath(path, matrix);
95}
96
97void CFWL_CheckBoxTP::DrawSignDiamond(CFGAS_GEGraphics* pGraphics,
98 const CFX_RectF& rtSign,
99 FX_ARGB argbFill,
100 const CFX_Matrix& matrix) {
101 CFGAS_GEPath path;
102 const float fWidth = rtSign.width;
103 const float fHeight = rtSign.height;
104 const float fBottom = rtSign.bottom();
105 path.MoveTo(CFX_PointF(rtSign.left + fWidth / 2, rtSign.top));
106 path.LineTo(CFX_PointF(rtSign.left, rtSign.top + fHeight / 2));
107 path.LineTo(CFX_PointF(rtSign.left + fWidth / 2, fBottom));
108 path.LineTo(CFX_PointF(rtSign.right(), rtSign.top + fHeight / 2));
109 path.LineTo(CFX_PointF(rtSign.left + fWidth / 2, rtSign.top));
110
111 CFGAS_GEGraphics::StateRestorer restorer(pGraphics);
112 pGraphics->SetFillColor(CFGAS_GEColor(argbFill));
114}
115
116void CFWL_CheckBoxTP::DrawSignSquare(CFGAS_GEGraphics* pGraphics,
117 const CFX_RectF& rtSign,
118 FX_ARGB argbFill,
119 const CFX_Matrix& matrix) {
120 CFGAS_GEPath path;
121 path.AddRectangle(rtSign.left, rtSign.top, rtSign.width, rtSign.height);
122
123 CFGAS_GEGraphics::StateRestorer restorer(pGraphics);
124 pGraphics->SetFillColor(CFGAS_GEColor(argbFill));
126}
127
128void CFWL_CheckBoxTP::DrawSignStar(CFGAS_GEGraphics* pGraphics,
129 const CFX_RectF& rtSign,
130 FX_ARGB argbFill,
131 const CFX_Matrix& matrix) {
132 CFGAS_GEPath path;
133 float fBottom = rtSign.bottom();
134 float fRadius = (rtSign.top - fBottom) / (1 + cosf(FXSYS_PI / 5.0f));
135 CFX_PointF ptCenter((rtSign.left + rtSign.right()) / 2.0f,
136 (rtSign.top + fBottom) / 2.0f);
137
138 std::array<CFX_PointF, 5> points;
139 float fAngle = FXSYS_PI / 10.0f;
140 for (auto& point : points) {
141 point =
142 ptCenter + CFX_PointF(fRadius * cosf(fAngle), fRadius * sinf(fAngle));
143 fAngle += FXSYS_PI * 2 / 5.0f;
144 }
145
146 path.MoveTo(points[0]);
147 int next = 0;
148 for (size_t i = 0; i < std::size(points); ++i) {
149 next = (next + 2) % std::size(points);
150 path.LineTo(points[next]);
151 }
152
153 CFGAS_GEGraphics::StateRestorer restorer(pGraphics);
154 pGraphics->SetFillColor(CFGAS_GEColor(argbFill));
156}
157
158void CFWL_CheckBoxTP::EnsureCheckPathInitialized(float fCheckLen) {
159 if (m_pCheckPath)
160 return;
161
162 m_pCheckPath = std::make_unique<CFGAS_GEPath>();
163
164 float fWidth = kSignPath;
165 float fHeight = -kSignPath;
166 float fBottom = kSignPath;
167 CFX_PointF pt1(fWidth / 15.0f, fBottom + fHeight * 2 / 5.0f);
168 CFX_PointF pt2(fWidth / 4.5f, fBottom + fHeight / 16.0f);
169 CFX_PointF pt3(fWidth / 3.0f, fBottom);
170 CFX_PointF pt4(fWidth * 14 / 15.0f, fBottom + fHeight * 15 / 16.0f);
171 CFX_PointF pt5(fWidth / 3.6f, fBottom + fHeight / 3.5f);
172 CFX_PointF pt12(fWidth / 7.0f, fBottom + fHeight * 2 / 7.0f);
173 CFX_PointF pt21(fWidth / 5.0f, fBottom + fHeight / 5.0f);
174 CFX_PointF pt23(fWidth / 4.4f, fBottom + fHeight * 0 / 16.0f);
175 CFX_PointF pt32(fWidth / 4.0f, fBottom);
176 CFX_PointF pt34(fWidth * (1 / 7.0f + 7 / 15.0f),
177 fBottom + fHeight * 4 / 5.0f);
178 CFX_PointF pt43(fWidth * (1 / 7.0f + 7 / 15.0f),
179 fBottom + fHeight * 4 / 5.0f);
180 CFX_PointF pt45(fWidth * 7 / 15.0f, fBottom + fHeight * 8 / 7.0f);
181 CFX_PointF pt54(fWidth / 3.4f, fBottom + fHeight / 3.5f);
182 CFX_PointF pt51(fWidth / 3.6f, fBottom + fHeight / 4.0f);
183 CFX_PointF pt15(fWidth / 3.5f, fBottom + fHeight * 3.5f / 5.0f);
184 m_pCheckPath->MoveTo(pt1);
185
186 CFX_PointF p1 = ScaleBezierPoint(pt12 - pt1);
187 CFX_PointF p2 = ScaleBezierPoint(pt21 - pt2);
188 m_pCheckPath->BezierTo(pt1 + p1, pt2 + p2, pt2);
189
190 p1 = ScaleBezierPoint(pt23 - pt2);
191 p2 = ScaleBezierPoint(pt32 - pt3);
192 m_pCheckPath->BezierTo(pt2 + p1, pt3 + p2, pt3);
193
194 p1 = ScaleBezierPoint(pt34 - pt3);
195 p2 = ScaleBezierPoint(pt43 - pt4);
196 m_pCheckPath->BezierTo(pt3 + p1, pt4 + p2, pt4);
197
198 p1 = ScaleBezierPoint(pt45 - pt4);
199 p2 = ScaleBezierPoint(pt54 - pt5);
200 m_pCheckPath->BezierTo(pt4 + p1, pt5 + p2, pt5);
201
202 p1 = ScaleBezierPoint(pt51 - pt5);
203 p2 = ScaleBezierPoint(pt15 - pt1);
204 m_pCheckPath->BezierTo(pt5 + p1, pt1 + p2, pt1);
205
206 float fScale = fCheckLen / kSignPath;
207 CFX_Matrix mt;
208 mt.Scale(fScale, fScale);
209 m_pCheckPath->TransformBy(mt);
210}
211
212void CFWL_CheckBoxTP::DrawBackground(const CFWL_ThemeBackground& pParams) {
214 return;
215
216 if ((pParams.m_dwStates & CFWL_PartState::kChecked) ||
217 (pParams.m_dwStates & CFWL_PartState::kNeutral)) {
218 DrawCheckSign(pParams.GetWidget(), pParams.GetGraphics(),
219 pParams.m_PartRect, pParams.m_dwStates, pParams.m_matrix);
220 }
221}
222
223void CFWL_CheckBoxTP::DrawCheckSign(CFWL_Widget* pWidget,
224 CFGAS_GEGraphics* pGraphics,
225 const CFX_RectF& pRtBox,
226 Mask<CFWL_PartState> iState,
227 const CFX_Matrix& matrix) {
228 CFX_RectF rtSign(pRtBox);
229 uint32_t dwColor =
230 iState & CFWL_PartState::kNeutral ? 0xFFA9A9A9 : 0xFF000000;
231 uint32_t dwStyle = pWidget->GetStyleExts();
232 rtSign.Deflate(rtSign.width / 4, rtSign.height / 4);
233 switch (dwStyle & FWL_STYLEEXT_CKB_SignShapeMask) {
235 DrawSignCheck(pGraphics, rtSign, dwColor, matrix);
236 break;
238 DrawSignCircle(pGraphics, rtSign, dwColor, matrix);
239 break;
241 DrawSignCross(pGraphics, rtSign, dwColor, matrix);
242 break;
244 DrawSignDiamond(pGraphics, rtSign, dwColor, matrix);
245 break;
247 DrawSignSquare(pGraphics, rtSign, dwColor, matrix);
248 break;
250 DrawSignStar(pGraphics, rtSign, dwColor, matrix);
251 break;
252 default:
253 break;
254 }
255}
256
257} // namespace pdfium
#define FWL_STYLEEXT_CKB_SignShapeMask
#define FWL_STYLEEXT_CKB_SignShapeSquare
#define FWL_STYLEEXT_CKB_SignShapeCheck
#define FWL_STYLEEXT_CKB_SignShapeStar
#define FWL_STYLEEXT_CKB_SignShapeCircle
#define FWL_STYLEEXT_CKB_SignShapeDiamond
#define FWL_STYLEEXT_CKB_SignShapeCross
#define FWLTHEME_CAPACITY_TextDisColor
Definition cfwl_utils.h:27
#define FWLTHEME_CAPACITY_TextColor
Definition cfwl_utils.h:26
#define DCHECK
Definition check.h:33
CFGAS_GEColor(FX_ARGB argb)
StateRestorer(CFGAS_GEGraphics *graphics)
void FillPath(const CFGAS_GEPath &path, CFX_FillRenderOptions::FillType fill_type, const CFX_Matrix &matrix)
void SetLineWidth(float lineWidth)
void SetStrokeColor(const CFGAS_GEColor &color)
void SetFillColor(const CFGAS_GEColor &color)
void StrokePath(const CFGAS_GEPath &path, const CFX_Matrix &matrix)
void AddLine(const CFX_PointF &p1, const CFX_PointF &p2)
void MoveTo(const CFX_PointF &point)
void AddRectangle(float left, float top, float width, float height)
void AddEllipse(const CFX_RectF &rect)
void LineTo(const CFX_PointF &point)
void Scale(float sx, float sy)
void Translate(float x, float y)
void Concat(const CFX_Matrix &right)
CFX_RectF(const CFX_RectF &other)=default
float bottom() const
float right() const
void Deflate(float x, float y)
void DrawBackground(const CFWL_ThemeBackground &pParams) override
void DrawText(const CFWL_ThemeText &pParams) override
CFGAS_GEGraphics * GetGraphics() const
CFWL_Widget * GetWidget() const
void EnsureTTOInitialized(IFWL_ThemeProvider *pProvider)
virtual void DrawText(const CFWL_ThemeText &pParams)
uint32_t GetStyleExts() const
CFX_PTemplate< float > CFX_PointF
uint32_t FX_ARGB
Definition fx_dib.h:36
#define FXSYS_BEZIER
Definition fx_system.h:45
#define FXSYS_PI
Definition fx_system.h:44