1 // 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.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "xfa/fwl/theme/cfwl_scrollbartp.h"
8
9 #include "xfa/fwl/cfwl_scrollbar.h"
10 #include "xfa/fwl/cfwl_themebackground.h"
11 #include "xfa/fwl/cfwl_widget.h"
12 #include "xfa/fwl/ifwl_themeprovider.h"
13 #include "xfa/fxgraphics/cxfa_gecolor.h"
14 #include "xfa/fxgraphics/cxfa_gepath.h"
15
16 namespace {
17
18 const float kPawLength = 12.5f;
19
20 } // namespace
21
CFWL_ScrollBarTP()22 CFWL_ScrollBarTP::CFWL_ScrollBarTP() : m_pThemeData(new SBThemeData) {
23 SetThemeData();
24 }
25
~CFWL_ScrollBarTP()26 CFWL_ScrollBarTP::~CFWL_ScrollBarTP() {}
27
DrawBackground(CFWL_ThemeBackground * pParams)28 void CFWL_ScrollBarTP::DrawBackground(CFWL_ThemeBackground* pParams) {
29 if (!pParams)
30 return;
31
32 CFWL_Widget* pWidget = pParams->m_pWidget;
33 FWLTHEME_STATE eState = FWLTHEME_STATE_Normal;
34 if (pParams->m_dwStates & CFWL_PartState_Hovered)
35 eState = FWLTHEME_STATE_Hover;
36 else if (pParams->m_dwStates & CFWL_PartState_Pressed)
37 eState = FWLTHEME_STATE_Pressed;
38 else if (pParams->m_dwStates & CFWL_PartState_Disabled)
39 eState = FWLTHEME_STATE_Disable;
40
41 CXFA_Graphics* pGraphics = pParams->m_pGraphics;
42 CFX_RectF* pRect = &pParams->m_rtPart;
43 bool bVert = !!pWidget->GetStylesEx();
44 switch (pParams->m_iPart) {
45 case CFWL_Part::ForeArrow: {
46 DrawMaxMinBtn(pGraphics, pRect,
47 bVert ? FWLTHEME_DIRECTION_Up : FWLTHEME_DIRECTION_Left,
48 eState, &pParams->m_matrix);
49 break;
50 }
51 case CFWL_Part::BackArrow: {
52 DrawMaxMinBtn(pGraphics, pRect,
53 bVert ? FWLTHEME_DIRECTION_Down : FWLTHEME_DIRECTION_Right,
54 eState, &pParams->m_matrix);
55 break;
56 }
57 case CFWL_Part::Thumb: {
58 DrawThumbBtn(pGraphics, pRect, bVert, eState, true, &pParams->m_matrix);
59 break;
60 }
61 case CFWL_Part::LowerTrack: {
62 DrawTrack(pGraphics, pRect, bVert, eState, true, &pParams->m_matrix);
63 break;
64 }
65 case CFWL_Part::UpperTrack: {
66 DrawTrack(pGraphics, pRect, bVert, eState, false, &pParams->m_matrix);
67 break;
68 }
69 default:
70 break;
71 }
72 }
73
DrawThumbBtn(CXFA_Graphics * pGraphics,const CFX_RectF * pRect,bool bVert,FWLTHEME_STATE eState,bool bPawButton,CFX_Matrix * pMatrix)74 void CFWL_ScrollBarTP::DrawThumbBtn(CXFA_Graphics* pGraphics,
75 const CFX_RectF* pRect,
76 bool bVert,
77 FWLTHEME_STATE eState,
78 bool bPawButton,
79 CFX_Matrix* pMatrix) {
80 if (eState < FWLTHEME_STATE_Normal || eState > FWLTHEME_STATE_Disable)
81 return;
82
83 CXFA_GEPath path;
84 CFX_RectF rect(*pRect);
85 if (bVert) {
86 rect.Deflate(1, 0);
87 if (rect.IsEmpty(0.1f))
88 return;
89
90 path.AddRectangle(rect.left, rect.top, rect.width, rect.height);
91 DrawAxialShading(pGraphics, rect.left, rect.top, rect.right(), rect.top,
92 m_pThemeData->clrBtnBK[eState - 1][0],
93 m_pThemeData->clrBtnBK[eState - 1][1], &path,
94 FXFILL_WINDING, pMatrix);
95 pGraphics->SaveGraphState();
96 pGraphics->SetStrokeColor(
97 CXFA_GEColor(m_pThemeData->clrBtnBorder[eState - 1]));
98 pGraphics->StrokePath(&path, pMatrix);
99 pGraphics->RestoreGraphState();
100 } else {
101 rect.Deflate(0, 1);
102 if (rect.IsEmpty(0.1f))
103 return;
104
105 path.AddRectangle(rect.left, rect.top, rect.width, rect.height);
106 DrawAxialShading(pGraphics, rect.left, rect.top, rect.left, rect.bottom(),
107 m_pThemeData->clrBtnBK[eState - 1][0],
108 m_pThemeData->clrBtnBK[eState - 1][1], &path,
109 FXFILL_WINDING, pMatrix);
110 pGraphics->SaveGraphState();
111 pGraphics->SetStrokeColor(
112 CXFA_GEColor(m_pThemeData->clrBtnBorder[eState - 1]));
113 pGraphics->StrokePath(&path, pMatrix);
114 pGraphics->RestoreGraphState();
115 }
116 }
117
DrawPaw(CXFA_Graphics * pGraphics,const CFX_RectF * pRect,bool bVert,FWLTHEME_STATE eState,CFX_Matrix * pMatrix)118 void CFWL_ScrollBarTP::DrawPaw(CXFA_Graphics* pGraphics,
119 const CFX_RectF* pRect,
120 bool bVert,
121 FWLTHEME_STATE eState,
122 CFX_Matrix* pMatrix) {
123 CXFA_GEPath path;
124 if (bVert) {
125 float fPawLen = kPawLength;
126 if (pRect->width / 2 <= fPawLen) {
127 fPawLen = (pRect->width - 6) / 2;
128 }
129
130 float fX = pRect->left + pRect->width / 4;
131 float fY = pRect->top + pRect->height / 2;
132 path.MoveTo(CFX_PointF(fX, fY - 4));
133 path.LineTo(CFX_PointF(fX + fPawLen, fY - 4));
134 path.MoveTo(CFX_PointF(fX, fY - 2));
135 path.LineTo(CFX_PointF(fX + fPawLen, fY - 2));
136 path.MoveTo(CFX_PointF(fX, fY));
137 path.LineTo(CFX_PointF(fX + fPawLen, fY));
138 path.MoveTo(CFX_PointF(fX, fY + 2));
139 path.LineTo(CFX_PointF(fX + fPawLen, fY + 2));
140
141 pGraphics->SetLineWidth(1);
142 pGraphics->SetStrokeColor(
143 CXFA_GEColor(m_pThemeData->clrPawColorLight[eState - 1]));
144 pGraphics->StrokePath(&path, nullptr);
145 fX++;
146
147 path.Clear();
148 path.MoveTo(CFX_PointF(fX, fY - 3));
149 path.LineTo(CFX_PointF(fX + fPawLen, fY - 3));
150 path.MoveTo(CFX_PointF(fX, fY - 1));
151 path.LineTo(CFX_PointF(fX + fPawLen, fY - 1));
152 path.MoveTo(CFX_PointF(fX, fY + 1));
153 path.LineTo(CFX_PointF(fX + fPawLen, fY + 1));
154 path.MoveTo(CFX_PointF(fX, fY + 3));
155 path.LineTo(CFX_PointF(fX + fPawLen, fY + 3));
156
157 pGraphics->SetLineWidth(1);
158 pGraphics->SetStrokeColor(
159 CXFA_GEColor(m_pThemeData->clrPawColorDark[eState - 1]));
160 pGraphics->StrokePath(&path, pMatrix);
161 } else {
162 float fPawLen = kPawLength;
163 if (pRect->height / 2 <= fPawLen) {
164 fPawLen = (pRect->height - 6) / 2;
165 }
166
167 float fX = pRect->left + pRect->width / 2;
168 float fY = pRect->top + pRect->height / 4;
169 path.MoveTo(CFX_PointF(fX - 4, fY));
170 path.LineTo(CFX_PointF(fX - 4, fY + fPawLen));
171 path.MoveTo(CFX_PointF(fX - 2, fY));
172 path.LineTo(CFX_PointF(fX - 2, fY + fPawLen));
173 path.MoveTo(CFX_PointF(fX, fY));
174 path.LineTo(CFX_PointF(fX, fY + fPawLen));
175 path.MoveTo(CFX_PointF(fX + 2, fY));
176 path.LineTo(CFX_PointF(fX + 2, fY + fPawLen));
177
178 pGraphics->SetLineWidth(1);
179 pGraphics->SetStrokeColor(
180 CXFA_GEColor(m_pThemeData->clrPawColorLight[eState - 1]));
181 pGraphics->StrokePath(&path, pMatrix);
182 fY++;
183
184 path.Clear();
185 path.MoveTo(CFX_PointF(fX - 3, fY));
186 path.LineTo(CFX_PointF(fX - 3, fY + fPawLen));
187 path.MoveTo(CFX_PointF(fX - 1, fY));
188 path.LineTo(CFX_PointF(fX - 1, fY + fPawLen));
189 path.MoveTo(CFX_PointF(fX + 1, fY));
190 path.LineTo(CFX_PointF(fX + 1, fY + fPawLen));
191 path.MoveTo(CFX_PointF(fX + 3, fY));
192 path.LineTo(CFX_PointF(fX + 3, fY + fPawLen));
193
194 pGraphics->SetLineWidth(1);
195 pGraphics->SetStrokeColor(
196 CXFA_GEColor(m_pThemeData->clrPawColorDark[eState - 1]));
197 pGraphics->StrokePath(&path, pMatrix);
198 }
199 }
200
DrawTrack(CXFA_Graphics * pGraphics,const CFX_RectF * pRect,bool bVert,FWLTHEME_STATE eState,bool bLowerTrack,CFX_Matrix * pMatrix)201 void CFWL_ScrollBarTP::DrawTrack(CXFA_Graphics* pGraphics,
202 const CFX_RectF* pRect,
203 bool bVert,
204 FWLTHEME_STATE eState,
205 bool bLowerTrack,
206 CFX_Matrix* pMatrix) {
207 if (eState < FWLTHEME_STATE_Normal || eState > FWLTHEME_STATE_Disable)
208 return;
209
210 pGraphics->SaveGraphState();
211 CXFA_GEPath path;
212 float fRight = pRect->right();
213 float fBottom = pRect->bottom();
214 if (bVert) {
215 path.AddRectangle(pRect->left, pRect->top, 1, pRect->height);
216 path.AddRectangle(fRight - 1, pRect->top, 1, pRect->height);
217 } else {
218 path.AddRectangle(pRect->left, pRect->top, pRect->width, 1);
219 path.AddRectangle(pRect->left, fBottom - 1, pRect->width, 1);
220 }
221 pGraphics->SetFillColor(CXFA_GEColor(ArgbEncode(255, 238, 237, 229)));
222 pGraphics->FillPath(&path, FXFILL_WINDING, pMatrix);
223 path.Clear();
224 path.AddRectangle(pRect->left + 1, pRect->top, pRect->width - 2,
225 pRect->height);
226 float x1 = bVert ? pRect->left + 1 : pRect->left;
227 float y1 = bVert ? pRect->top : pRect->top + 1;
228 float x2 = bVert ? fRight - 1 : pRect->left;
229 float y2 = bVert ? pRect->top : fBottom - 1;
230 pGraphics->RestoreGraphState();
231 DrawAxialShading(pGraphics, x1, y1, x2, y2, m_pThemeData->clrTrackBKStart,
232 m_pThemeData->clrTrackBKEnd, &path, FXFILL_WINDING, pMatrix);
233 }
234
DrawMaxMinBtn(CXFA_Graphics * pGraphics,const CFX_RectF * pRect,FWLTHEME_DIRECTION eDict,FWLTHEME_STATE eState,CFX_Matrix * pMatrix)235 void CFWL_ScrollBarTP::DrawMaxMinBtn(CXFA_Graphics* pGraphics,
236 const CFX_RectF* pRect,
237 FWLTHEME_DIRECTION eDict,
238 FWLTHEME_STATE eState,
239 CFX_Matrix* pMatrix) {
240 DrawTrack(pGraphics, pRect,
241 eDict == FWLTHEME_DIRECTION_Up || eDict == FWLTHEME_DIRECTION_Down,
242 eState, true, pMatrix);
243 CFX_RectF rtArrowBtn(*pRect);
244 rtArrowBtn.Deflate(1, 1, 1, 1);
245 DrawArrowBtn(pGraphics, &rtArrowBtn, eDict, eState, pMatrix);
246 }
247
SetThemeData()248 void CFWL_ScrollBarTP::SetThemeData() {
249 m_pThemeData->clrPawColorLight[3] = ArgbEncode(0xff, 208, 223, 172);
250 m_pThemeData->clrPawColorDark[3] = ArgbEncode(0xff, 140, 157, 115);
251 m_pThemeData->clrBtnBK[3][0] = ArgbEncode(0xff, 164, 180, 139);
252 m_pThemeData->clrBtnBK[3][1] = ArgbEncode(0xff, 141, 157, 115);
253 m_pThemeData->clrBtnBorder[3] = ArgbEncode(0xff, 236, 233, 216);
254 m_pThemeData->clrPawColorLight[0] = ArgbEncode(0xff, 238, 244, 254);
255 m_pThemeData->clrPawColorDark[0] = ArgbEncode(0xff, 140, 176, 248);
256 m_pThemeData->clrBtnBK[0][0] = ArgbEncode(0xff, 197, 213, 252);
257 m_pThemeData->clrBtnBK[0][1] = ArgbEncode(0xff, 182, 205, 251);
258 m_pThemeData->clrBtnBorder[0] = ArgbEncode(0xff, 148, 176, 221);
259 m_pThemeData->clrPawColorLight[1] = ArgbEncode(0xff, 252, 253, 255);
260 m_pThemeData->clrPawColorDark[1] = ArgbEncode(0xff, 156, 197, 255);
261 m_pThemeData->clrBtnBK[1][0] = ArgbEncode(0xff, 216, 232, 255);
262 m_pThemeData->clrBtnBK[1][1] = ArgbEncode(0xff, 204, 225, 255);
263 m_pThemeData->clrBtnBorder[1] = ArgbEncode(0xff, 218, 230, 254);
264 m_pThemeData->clrPawColorLight[2] = ArgbEncode(0xff, 207, 221, 253);
265 m_pThemeData->clrPawColorDark[2] = ArgbEncode(0xff, 131, 158, 216);
266 m_pThemeData->clrBtnBK[2][0] = ArgbEncode(0xff, 167, 190, 245);
267 m_pThemeData->clrBtnBK[2][1] = ArgbEncode(0xff, 146, 179, 249);
268 m_pThemeData->clrBtnBorder[2] = ArgbEncode(0xff, 124, 159, 211);
269 m_pThemeData->clrTrackBKStart = ArgbEncode(0xff, 243, 241, 236);
270 m_pThemeData->clrTrackBKEnd = ArgbEncode(0xff, 254, 254, 251);
271 }
272