cfwl_scrollbartp.cpp revision 33357cad1fd1321a2b38d2963e2585f27ce980a2
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/cfx_color.h"
14#include "xfa/fxgraphics/cfx_path.h"
15
16namespace {
17
18const float kPawLength = 12.5f;
19
20}  // namespace
21
22CFWL_ScrollBarTP::CFWL_ScrollBarTP() : m_pThemeData(new SBThemeData) {
23  SetThemeData();
24}
25
26CFWL_ScrollBarTP::~CFWL_ScrollBarTP() {}
27
28void 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  CFX_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
74void CFWL_ScrollBarTP::DrawThumbBtn(CFX_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  CFX_Path 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    CFX_Color rcStroke;
96    rcStroke.Set(m_pThemeData->clrBtnBorder[eState - 1]);
97    pGraphics->SaveGraphState();
98    pGraphics->SetStrokeColor(&rcStroke);
99    pGraphics->StrokePath(&path, pMatrix);
100    pGraphics->RestoreGraphState();
101  } else {
102    rect.Deflate(0, 1);
103    if (rect.IsEmpty(0.1f))
104      return;
105
106    path.AddRectangle(rect.left, rect.top, rect.width, rect.height);
107    DrawAxialShading(pGraphics, rect.left, rect.top, rect.left, rect.bottom(),
108                     m_pThemeData->clrBtnBK[eState - 1][0],
109                     m_pThemeData->clrBtnBK[eState - 1][1], &path,
110                     FXFILL_WINDING, pMatrix);
111    CFX_Color rcStroke;
112    rcStroke.Set(m_pThemeData->clrBtnBorder[eState - 1]);
113    pGraphics->SaveGraphState();
114    pGraphics->SetStrokeColor(&rcStroke);
115    pGraphics->StrokePath(&path, pMatrix);
116    pGraphics->RestoreGraphState();
117  }
118}
119
120void CFWL_ScrollBarTP::DrawPaw(CFX_Graphics* pGraphics,
121                               const CFX_RectF* pRect,
122                               bool bVert,
123                               FWLTHEME_STATE eState,
124                               CFX_Matrix* pMatrix) {
125  CFX_Path path;
126  if (bVert) {
127    FX_FLOAT fPawLen = kPawLength;
128    if (pRect->width / 2 <= fPawLen) {
129      fPawLen = (pRect->width - 6) / 2;
130    }
131
132    FX_FLOAT fX = pRect->left + pRect->width / 4;
133    FX_FLOAT fY = pRect->top + pRect->height / 2;
134    path.MoveTo(CFX_PointF(fX, fY - 4));
135    path.LineTo(CFX_PointF(fX + fPawLen, fY - 4));
136    path.MoveTo(CFX_PointF(fX, fY - 2));
137    path.LineTo(CFX_PointF(fX + fPawLen, fY - 2));
138    path.MoveTo(CFX_PointF(fX, fY));
139    path.LineTo(CFX_PointF(fX + fPawLen, fY));
140    path.MoveTo(CFX_PointF(fX, fY + 2));
141    path.LineTo(CFX_PointF(fX + fPawLen, fY + 2));
142
143    CFX_Color clrLight(m_pThemeData->clrPawColorLight[eState - 1]);
144    pGraphics->SetLineWidth(1);
145    pGraphics->SetStrokeColor(&clrLight);
146    pGraphics->StrokePath(&path);
147    fX++;
148
149    path.Clear();
150    path.MoveTo(CFX_PointF(fX, fY - 3));
151    path.LineTo(CFX_PointF(fX + fPawLen, fY - 3));
152    path.MoveTo(CFX_PointF(fX, fY - 1));
153    path.LineTo(CFX_PointF(fX + fPawLen, fY - 1));
154    path.MoveTo(CFX_PointF(fX, fY + 1));
155    path.LineTo(CFX_PointF(fX + fPawLen, fY + 1));
156    path.MoveTo(CFX_PointF(fX, fY + 3));
157    path.LineTo(CFX_PointF(fX + fPawLen, fY + 3));
158
159    CFX_Color clrDark(m_pThemeData->clrPawColorDark[eState - 1]);
160    pGraphics->SetLineWidth(1);
161    pGraphics->SetStrokeColor(&clrDark);
162    pGraphics->StrokePath(&path, pMatrix);
163  } else {
164    FX_FLOAT fPawLen = kPawLength;
165    if (pRect->height / 2 <= fPawLen) {
166      fPawLen = (pRect->height - 6) / 2;
167    }
168
169    FX_FLOAT fX = pRect->left + pRect->width / 2;
170    FX_FLOAT fY = pRect->top + pRect->height / 4;
171    path.MoveTo(CFX_PointF(fX - 4, fY));
172    path.LineTo(CFX_PointF(fX - 4, fY + fPawLen));
173    path.MoveTo(CFX_PointF(fX - 2, fY));
174    path.LineTo(CFX_PointF(fX - 2, fY + fPawLen));
175    path.MoveTo(CFX_PointF(fX, fY));
176    path.LineTo(CFX_PointF(fX, fY + fPawLen));
177    path.MoveTo(CFX_PointF(fX + 2, fY));
178    path.LineTo(CFX_PointF(fX + 2, fY + fPawLen));
179
180    CFX_Color clrLight(m_pThemeData->clrPawColorLight[eState - 1]);
181    pGraphics->SetLineWidth(1);
182    pGraphics->SetStrokeColor(&clrLight);
183    pGraphics->StrokePath(&path, pMatrix);
184    fY++;
185
186    path.Clear();
187    path.MoveTo(CFX_PointF(fX - 3, fY));
188    path.LineTo(CFX_PointF(fX - 3, 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 + 1, fY));
192    path.LineTo(CFX_PointF(fX + 1, fY + fPawLen));
193    path.MoveTo(CFX_PointF(fX + 3, fY));
194    path.LineTo(CFX_PointF(fX + 3, fY + fPawLen));
195
196    CFX_Color clrDark(m_pThemeData->clrPawColorDark[eState - 1]);
197    pGraphics->SetLineWidth(1);
198    pGraphics->SetStrokeColor(&clrDark);
199    pGraphics->StrokePath(&path, pMatrix);
200  }
201}
202
203void CFWL_ScrollBarTP::DrawTrack(CFX_Graphics* pGraphics,
204                                 const CFX_RectF* pRect,
205                                 bool bVert,
206                                 FWLTHEME_STATE eState,
207                                 bool bLowerTrack,
208                                 CFX_Matrix* pMatrix) {
209  if (eState < FWLTHEME_STATE_Normal || eState > FWLTHEME_STATE_Disable)
210    return;
211
212  pGraphics->SaveGraphState();
213  CFX_Color colorLine(ArgbEncode(255, 238, 237, 229));
214  CFX_Path path;
215  FX_FLOAT fRight = pRect->right();
216  FX_FLOAT fBottom = pRect->bottom();
217  if (bVert) {
218    path.AddRectangle(pRect->left, pRect->top, 1, pRect->height);
219    path.AddRectangle(fRight - 1, pRect->top, 1, pRect->height);
220  } else {
221    path.AddRectangle(pRect->left, pRect->top, pRect->width, 1);
222    path.AddRectangle(pRect->left, fBottom - 1, pRect->width, 1);
223  }
224  pGraphics->SetFillColor(&colorLine);
225  pGraphics->FillPath(&path, FXFILL_WINDING, pMatrix);
226  path.Clear();
227  path.AddRectangle(pRect->left + 1, pRect->top, pRect->width - 2,
228                    pRect->height);
229  FX_FLOAT x1 = bVert ? pRect->left + 1 : pRect->left;
230  FX_FLOAT y1 = bVert ? pRect->top : pRect->top + 1;
231  FX_FLOAT x2 = bVert ? fRight - 1 : pRect->left;
232  FX_FLOAT y2 = bVert ? pRect->top : fBottom - 1;
233  pGraphics->RestoreGraphState();
234  DrawAxialShading(pGraphics, x1, y1, x2, y2, m_pThemeData->clrTrackBKStart,
235                   m_pThemeData->clrTrackBKEnd, &path, FXFILL_WINDING, pMatrix);
236}
237
238void CFWL_ScrollBarTP::DrawMaxMinBtn(CFX_Graphics* pGraphics,
239                                     const CFX_RectF* pRect,
240                                     FWLTHEME_DIRECTION eDict,
241                                     FWLTHEME_STATE eState,
242                                     CFX_Matrix* pMatrix) {
243  DrawTrack(pGraphics, pRect,
244            eDict == FWLTHEME_DIRECTION_Up || eDict == FWLTHEME_DIRECTION_Down,
245            eState, true, pMatrix);
246  CFX_RectF rtArrowBtn(*pRect);
247  rtArrowBtn.Deflate(1, 1, 1, 1);
248  DrawArrowBtn(pGraphics, &rtArrowBtn, eDict, eState, pMatrix);
249}
250
251void CFWL_ScrollBarTP::SetThemeData() {
252  m_pThemeData->clrPawColorLight[3] = ArgbEncode(0xff, 208, 223, 172);
253  m_pThemeData->clrPawColorDark[3] = ArgbEncode(0xff, 140, 157, 115);
254  m_pThemeData->clrBtnBK[3][0] = ArgbEncode(0xff, 164, 180, 139);
255  m_pThemeData->clrBtnBK[3][1] = ArgbEncode(0xff, 141, 157, 115);
256  m_pThemeData->clrBtnBorder[3] = ArgbEncode(0xff, 236, 233, 216);
257  m_pThemeData->clrPawColorLight[0] = ArgbEncode(0xff, 238, 244, 254);
258  m_pThemeData->clrPawColorDark[0] = ArgbEncode(0xff, 140, 176, 248);
259  m_pThemeData->clrBtnBK[0][0] = ArgbEncode(0xff, 197, 213, 252);
260  m_pThemeData->clrBtnBK[0][1] = ArgbEncode(0xff, 182, 205, 251);
261  m_pThemeData->clrBtnBorder[0] = ArgbEncode(0xff, 148, 176, 221);
262  m_pThemeData->clrPawColorLight[1] = ArgbEncode(0xff, 252, 253, 255);
263  m_pThemeData->clrPawColorDark[1] = ArgbEncode(0xff, 156, 197, 255);
264  m_pThemeData->clrBtnBK[1][0] = ArgbEncode(0xff, 216, 232, 255);
265  m_pThemeData->clrBtnBK[1][1] = ArgbEncode(0xff, 204, 225, 255);
266  m_pThemeData->clrBtnBorder[1] = ArgbEncode(0xff, 218, 230, 254);
267  m_pThemeData->clrPawColorLight[2] = ArgbEncode(0xff, 207, 221, 253);
268  m_pThemeData->clrPawColorDark[2] = ArgbEncode(0xff, 131, 158, 216);
269  m_pThemeData->clrBtnBK[2][0] = ArgbEncode(0xff, 167, 190, 245);
270  m_pThemeData->clrBtnBK[2][1] = ArgbEncode(0xff, 146, 179, 249);
271  m_pThemeData->clrBtnBorder[2] = ArgbEncode(0xff, 124, 159, 211);
272  m_pThemeData->clrTrackBKStart = ArgbEncode(0xff, 243, 241, 236);
273  m_pThemeData->clrTrackBKEnd = ArgbEncode(0xff, 254, 254, 251);
274}
275