Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa / src / fwl / src / theme / scrollbartp.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4 \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #include "../../../foxitlib.h"\r
8 #define FWL_SCROLL_PawLen       12.5f\r
9 CFWL_ScrollBarTP::CFWL_ScrollBarTP()\r
10 {\r
11     m_pThemeData = FX_NEW SBThemeData;\r
12     SetThemeData(0);\r
13 }\r
14 CFWL_ScrollBarTP::~CFWL_ScrollBarTP()\r
15 {\r
16     if (m_pThemeData) {\r
17         delete m_pThemeData;\r
18         m_pThemeData  = NULL;\r
19     }\r
20 }\r
21 FX_BOOL CFWL_ScrollBarTP::IsValidWidget(IFWL_Widget *pWidget)\r
22 {\r
23     _FWL_RETURN_VALUE_IF_FAIL(pWidget, FALSE);\r
24     return pWidget->GetClassID() == FWL_CLASSHASH_ScrollBar;\r
25 }\r
26 FX_LPVOID CFWL_ScrollBarTP::GetCapacity(CFWL_ThemePart *pThemePart, FX_DWORD dwCapacity)\r
27 {\r
28     if (dwCapacity == FWL_CAPACITY_SCB_Size) {\r
29         m_fValue = 5;\r
30         return &m_fValue;\r
31     }\r
32     return CFWL_WidgetTP::GetCapacity(pThemePart, dwCapacity);\r
33 }\r
34 FX_DWORD CFWL_ScrollBarTP::SetThemeID(IFWL_Widget *pWidget, FX_DWORD dwThemeID, FX_BOOL bChildren )\r
35 {\r
36     if (m_pThemeData) {\r
37         SetThemeData(FWL_GetThemeColor(dwThemeID));\r
38     }\r
39     return CFWL_WidgetTP::SetThemeID(pWidget, dwThemeID, bChildren);\r
40 }\r
41 FX_BOOL CFWL_ScrollBarTP::DrawBackground(CFWL_ThemeBackground *pParams)\r
42 {\r
43     _FWL_RETURN_VALUE_IF_FAIL(pParams, FALSE);\r
44     IFWL_Widget *pWidget = pParams->m_pWidget;\r
45     FX_DWORD dwStyleEx = pWidget->GetStylesEx();\r
46     FWLTHEME_STATE eState = FWLTHEME_STATE_Normal;\r
47     switch (pParams->m_dwStates & 0x03) {\r
48         case FWL_PARTSTATE_SCB_Hovered: {\r
49                 eState = FWLTHEME_STATE_Hover;\r
50                 break;\r
51             }\r
52         case FWL_PARTSTATE_SCB_Pressed: {\r
53                 eState = FWLTHEME_STATE_Pressed;\r
54                 break;\r
55             }\r
56         case FWL_PARTSTATE_SCB_Disabled: {\r
57                 eState = FWLTHEME_STATE_Disabale;\r
58                 break;\r
59             }\r
60     }\r
61     CFX_Graphics *pGraphics = pParams->m_pGraphics;\r
62     CFX_RectF *pRect = &pParams->m_rtPart;\r
63     FX_BOOL bVert = pWidget->GetStylesEx();\r
64     switch (pParams->m_iPart) {\r
65         case FWL_PART_SCB_ForeArrow: {\r
66                 DrawMaxMinBtn(pGraphics, pRect, bVert ? FWLTHEME_DIRECTION_Up : FWLTHEME_DIRECTION_Left, eState, &pParams->m_matrix);\r
67                 break;\r
68             }\r
69         case FWL_PART_SCB_BackArrow: {\r
70                 DrawMaxMinBtn(pGraphics, pRect, bVert ? FWLTHEME_DIRECTION_Down : FWLTHEME_DIRECTION_Right, eState, &pParams->m_matrix);\r
71                 break;\r
72             }\r
73         case FWL_PART_SCB_Thumb: {\r
74                 DrawThumbBtn(pGraphics, pRect, bVert, eState, TRUE, &pParams->m_matrix);\r
75                 break;\r
76             }\r
77         case FWL_PART_SCB_LowerTrack: {\r
78                 DrawTrack(pGraphics, pRect, bVert, eState, TRUE, &pParams->m_matrix);\r
79                 break;\r
80             }\r
81         case FWL_PART_SCB_UpperTrack: {\r
82                 DrawTrack(pGraphics, pRect, bVert, eState, FALSE, &pParams->m_matrix);\r
83                 break;\r
84             }\r
85         default: {\r
86             }\r
87     }\r
88     return TRUE;\r
89 }\r
90 #ifdef THEME_XPSimilar\r
91 void CFWL_ScrollBarTP::DrawThumbBtn(CFX_Graphics *pGraphics, const CFX_RectF *pRect, FX_BOOL bVert, FWLTHEME_STATE eState, FX_BOOL bPawButton, CFX_Matrix *pMatrix )\r
92 {\r
93     if (eState < FWLTHEME_STATE_Normal || eState > FWLTHEME_STATE_Disabale) {\r
94         return;\r
95     }\r
96     CFX_Path path;\r
97     path.Create();\r
98     CFX_RectF rect(*pRect);\r
99     if (bVert) {\r
100         rect.Deflate(1, 0);\r
101         if (rect.IsEmpty(0.1f)) {\r
102             return;\r
103         }\r
104         path.AddRectangle(rect.left, rect.top, rect.width, rect.height);\r
105         DrawAxialShading(pGraphics,\r
106                          rect.left,\r
107                          rect.top,\r
108                          rect.right(),\r
109                          rect.top,\r
110                          m_pThemeData->clrBtnBK[eState - 1][0],\r
111                          m_pThemeData->clrBtnBK[eState - 1][1],\r
112                          &path,\r
113                          FXFILL_WINDING,\r
114                          pMatrix);\r
115         CFX_Color rcStroke;\r
116         rcStroke.Set(m_pThemeData->clrBtnBorder[eState - 1]);\r
117         pGraphics->SaveGraphState();\r
118         pGraphics->SetStrokeColor(&rcStroke);\r
119         pGraphics->StrokePath(&path, pMatrix);\r
120         pGraphics->RestoreGraphState();\r
121     } else {\r
122         rect.Deflate(0, 1);\r
123         if (rect.IsEmpty(0.1f)) {\r
124             return;\r
125         }\r
126         path.AddRectangle(rect.left, rect.top, rect.width, rect.height);\r
127         DrawAxialShading(pGraphics,\r
128                          rect.left,\r
129                          rect.top,\r
130                          rect.left,\r
131                          rect.bottom(),\r
132                          m_pThemeData->clrBtnBK[eState - 1][0],\r
133                          m_pThemeData->clrBtnBK[eState - 1][1],\r
134                          &path,\r
135                          FXFILL_WINDING,\r
136                          pMatrix);\r
137         CFX_Color rcStroke;\r
138         rcStroke.Set(m_pThemeData->clrBtnBorder[eState - 1]);\r
139         pGraphics->SaveGraphState();\r
140         pGraphics->SetStrokeColor(&rcStroke);\r
141         pGraphics->StrokePath(&path, pMatrix);\r
142         pGraphics->RestoreGraphState();\r
143     }\r
144 }\r
145 void CFWL_ScrollBarTP::DrawPaw(CFX_Graphics *pGraphics, const CFX_RectF *pRect, FX_BOOL bVert, FWLTHEME_STATE eState, CFX_Matrix *pMatrix )\r
146 {\r
147     CFX_Path path;\r
148     path.Create();\r
149     if (bVert) {\r
150         FX_FLOAT fPawLen = FWL_SCROLL_PawLen;\r
151         if (pRect->width / 2 <= fPawLen) {\r
152             fPawLen = (pRect->width - 6) / 2 ;\r
153         }\r
154         FX_FLOAT fX = pRect->left + pRect->width / 4;\r
155         FX_FLOAT fY = pRect->top + pRect->height / 2;\r
156         path.MoveTo(fX, fY - 4);\r
157         path.LineTo(fX + fPawLen, fY - 4);\r
158         path.MoveTo(fX, fY - 2);\r
159         path.LineTo(fX + fPawLen, fY - 2);\r
160         path.MoveTo(fX , fY);\r
161         path.LineTo(fX + fPawLen, fY);\r
162         path.MoveTo(fX, fY + 2);\r
163         path.LineTo(fX + fPawLen, fY + 2);\r
164         CFX_Color clrLight(m_pThemeData->clrPawColorLight[eState - 1]);\r
165         pGraphics->SetLineWidth(1);\r
166         pGraphics->SetStrokeColor(&clrLight);\r
167         pGraphics->StrokePath(&path);\r
168         fX++;\r
169         path.Clear();\r
170         path.MoveTo(fX, fY - 3);\r
171         path.LineTo(fX + fPawLen, fY - 3);\r
172         path.MoveTo(fX, fY - 1);\r
173         path.LineTo(fX + fPawLen, fY - 1);\r
174         path.MoveTo(fX, fY + 1);\r
175         path.LineTo(fX + fPawLen, fY + 1);\r
176         path.MoveTo(fX, fY + 3);\r
177         path.LineTo(fX + fPawLen, fY + 3);\r
178         CFX_Color clrDark(m_pThemeData->clrPawColorDark[eState - 1]);\r
179         pGraphics->SetLineWidth(1);\r
180         pGraphics->SetStrokeColor(&clrDark);\r
181         pGraphics->StrokePath(&path, pMatrix);\r
182     } else {\r
183         FX_FLOAT fPawLen = FWL_SCROLL_PawLen;\r
184         if (pRect->height / 2 <= fPawLen) {\r
185             fPawLen = (pRect->height - 6) / 2;\r
186         }\r
187         FX_FLOAT fX = pRect->left + pRect->width / 2;\r
188         FX_FLOAT fY = pRect->top + pRect->height / 4;\r
189         path.MoveTo(fX - 4, fY);\r
190         path.LineTo(fX - 4, fY + fPawLen);\r
191         path.MoveTo(fX - 2, fY);\r
192         path.LineTo(fX - 2, fY + fPawLen);\r
193         path.MoveTo(fX, fY);\r
194         path.LineTo(fX, fY + fPawLen);\r
195         path.MoveTo(fX + 2, fY);\r
196         path.LineTo(fX + 2, fY + fPawLen);\r
197         CFX_Color clrLight(m_pThemeData->clrPawColorLight[eState - 1]);\r
198         pGraphics->SetLineWidth(1);\r
199         pGraphics->SetStrokeColor(&clrLight);\r
200         pGraphics->StrokePath(&path, pMatrix);\r
201         fY ++;\r
202         path.Clear();\r
203         path.MoveTo(fX - 3, fY);\r
204         path.LineTo(fX - 3, fY + fPawLen);\r
205         path.MoveTo(fX - 1, fY);\r
206         path.LineTo(fX - 1, fY + fPawLen);\r
207         path.MoveTo(fX + 1, fY);\r
208         path.LineTo(fX + 1, fY + fPawLen);\r
209         path.MoveTo(fX + 3, fY);\r
210         path.LineTo(fX + 3, fY + fPawLen);\r
211         CFX_Color clrDark(m_pThemeData->clrPawColorDark[eState - 1]);\r
212         pGraphics->SetLineWidth(1);\r
213         pGraphics->SetStrokeColor(&clrDark);\r
214         pGraphics->StrokePath(&path, pMatrix);\r
215     }\r
216 }\r
217 void CFWL_ScrollBarTP::DrawTrack(CFX_Graphics *pGraphics, const CFX_RectF *pRect, FX_BOOL bVert, FWLTHEME_STATE eState, FX_BOOL bLowerTrack, CFX_Matrix *pMatrix )\r
218 {\r
219     if (eState < FWLTHEME_STATE_Normal || eState > FWLTHEME_STATE_Disabale) {\r
220         return;\r
221     }\r
222     pGraphics->SaveGraphState();\r
223     CFX_Color colorLine(ArgbEncode(255, 238, 237, 229));\r
224     CFX_Path path;\r
225     path.Create();\r
226     FX_FLOAT fRight = pRect->right();\r
227     FX_FLOAT fBottom = pRect->bottom();\r
228     if (bVert) {\r
229         path.AddRectangle(pRect->left, pRect->top, 1, pRect->height);\r
230         path.AddRectangle(fRight - 1, pRect->top, 1, pRect->height);\r
231     } else {\r
232         path.AddRectangle(pRect->left, pRect->top, pRect->width, 1);\r
233         path.AddRectangle(pRect->left, fBottom - 1, pRect->width, 1);\r
234     }\r
235     pGraphics->SetFillColor(&colorLine);\r
236     pGraphics->FillPath(&path, FXFILL_WINDING, pMatrix);\r
237     path.Clear();\r
238     path.AddRectangle(pRect->left + 1, pRect->top, pRect->width - 2, pRect->height);\r
239     FX_FLOAT x1 = bVert ? pRect->left + 1 : pRect->left;\r
240     FX_FLOAT y1 = bVert ? pRect->top : pRect->top + 1;\r
241     FX_FLOAT x2 = bVert ? fRight - 1 : pRect->left;\r
242     FX_FLOAT y2 = bVert ? pRect->top : fBottom - 1;\r
243     pGraphics->RestoreGraphState();\r
244     DrawAxialShading(pGraphics,\r
245                      x1, y1, x2, y2,\r
246                      m_pThemeData->clrTrackBKStart,\r
247                      m_pThemeData->clrTrackBKEnd,\r
248                      &path,\r
249                      FXFILL_WINDING,\r
250                      pMatrix);\r
251 }\r
252 void CFWL_ScrollBarTP::DrawMaxMinBtn(CFX_Graphics *pGraphics, const CFX_RectF *pRect,  FWLTHEME_DIRECTION eDict, FWLTHEME_STATE eState, CFX_Matrix *pMatrix )\r
253 {\r
254     DrawTrack(pGraphics,\r
255               pRect,\r
256               eDict == FWLTHEME_DIRECTION_Up || eDict == FWLTHEME_DIRECTION_Down,\r
257               eState,\r
258               TRUE,\r
259               pMatrix);\r
260     CFX_RectF rtArrowBtn(*pRect);\r
261     rtArrowBtn.Deflate(1, 1, 1, 1);\r
262     DrawArrowBtn(pGraphics, &rtArrowBtn, eDict, eState, pMatrix);\r
263 }\r
264 #else\r
265 void CFWL_ScrollBarTP::DrawThumbBtn(CFX_Graphics *pGraphics, const CFX_RectF *pRect, FX_BOOL bVert, FWLTHEME_STATE eState, FX_BOOL bPawButton, CFX_Matrix *pMatrix )\r
266 {\r
267     if (pRect->IsEmpty()) {\r
268         return;\r
269     }\r
270     CFX_RectF rtThumb(*pRect);\r
271     FX_FLOAT fWidth = 2;\r
272     Draw3DRect(pGraphics, FWLTHEME_EDGE_Raised, fWidth, pRect, FWLTHEME_COLOR_EDGELT1, FWLTHEME_COLOR_EDGELT2, FWLTHEME_COLOR_EDGERB1, FWLTHEME_COLOR_EDGERB2, pMatrix);\r
273     CFX_Path path;\r
274     path.Create();\r
275     path.AddRectangle(pRect->left + fWidth, pRect->top + fWidth, pRect->width - 2 * fWidth, pRect->height - 2 * fWidth);\r
276     pGraphics->SaveGraphState();\r
277     CFX_Color crFill(FWLTHEME_COLOR_Background);\r
278     pGraphics->SetFillColor(&crFill);\r
279     pGraphics->FillPath(&path, FXFILL_WINDING, pMatrix);\r
280     pGraphics->RestoreGraphState();\r
281 }\r
282 void CFWL_ScrollBarTP::DrawTrack(CFX_Graphics *pGraphics, const CFX_RectF *pRect, FX_BOOL bVert, FWLTHEME_STATE eState, FX_BOOL bLowerTrack, CFX_Matrix *pMatrix )\r
283 {\r
284     if (pRect->IsEmpty()) {\r
285         return;\r
286     }\r
287     CFX_Path path;\r
288     path.Create();\r
289     path.AddRectangle(pRect->left, pRect->top, pRect->width, pRect->height);\r
290     pGraphics->SaveGraphState();\r
291     CFX_Color clrFill(0xFFF0F0F0);\r
292     pGraphics->SetFillColor(&clrFill);\r
293     pGraphics->FillPath(&path, FXFILL_WINDING, pMatrix);\r
294     pGraphics->RestoreGraphState();\r
295 }\r
296 void CFWL_ScrollBarTP::DrawMaxMinBtn(CFX_Graphics *pGraphics, const CFX_RectF *pRect, FWLTHEME_DIRECTION eDict, FWLTHEME_STATE eState, CFX_Matrix *pMatrix )\r
297 {\r
298     CFX_RectF rtThumb(*pRect);\r
299     FX_FLOAT fWidth = eState == FWLTHEME_STATE_Pressed ? 1.0f : 2.0f;\r
300     FWLTHEME_EDGE eType = eState == FWLTHEME_STATE_Pressed ? FWLTHEME_EDGE_Flat : FWLTHEME_EDGE_Raised;\r
301     Draw3DRect(pGraphics, eType, fWidth, pRect, FWLTHEME_COLOR_EDGELT1, FWLTHEME_COLOR_EDGELT2, FWLTHEME_COLOR_EDGERB1, FWLTHEME_COLOR_EDGERB2, pMatrix);\r
302     CFX_Path path;\r
303     path.Create();\r
304     path.AddRectangle(pRect->left + fWidth, pRect->top + fWidth, pRect->width - 2 * fWidth, pRect->height - 2 * fWidth);\r
305     pGraphics->SaveGraphState();\r
306     CFX_Color crFill(FWLTHEME_COLOR_Background);\r
307     pGraphics->SetFillColor(&crFill);\r
308     pGraphics->FillPath(&path, FXFILL_WINDING, pMatrix);\r
309     pGraphics->RestoreGraphState();\r
310     DrawArrow(pGraphics,\r
311               pRect,\r
312               eDict,\r
313               eState == FWLTHEME_STATE_Disabale ? 0xFFA0A0A0 : 0xFF000000,\r
314               eState == FWLTHEME_STATE_Pressed,\r
315               pMatrix);\r
316 }\r
317 #endif\r
318 void CFWL_ScrollBarTP::SetThemeData(FX_DWORD dwID)\r
319 {\r
320     m_pThemeData->clrPawColorLight[3] = ArgbEncode(0xff, 208, 223, 172);\r
321     m_pThemeData->clrPawColorDark[3] = ArgbEncode(0xff, 140, 157, 115);\r
322     m_pThemeData->clrBtnBK[3][0] = ArgbEncode(0xff, 164, 180, 139);\r
323     m_pThemeData->clrBtnBK[3][1] = ArgbEncode(0xff, 141, 157, 115);\r
324     m_pThemeData->clrBtnBorder[3] = ArgbEncode(0xff, 236, 233, 216);\r
325     if (dwID) {\r
326         m_pThemeData->clrPawColorLight[0] = ArgbEncode(0xff, 208, 223, 172);\r
327         m_pThemeData->clrPawColorDark[0] = ArgbEncode(0xff, 140, 157, 115);\r
328         m_pThemeData->clrBtnBK[0][0] = ArgbEncode(0xff, 162, 179, 141);\r
329         m_pThemeData->clrBtnBK[0][1] = ArgbEncode(0xff, 149, 167, 117);\r
330         m_pThemeData->clrBtnBorder[0] = ArgbEncode(0xff, 142, 153, 125);\r
331         m_pThemeData->clrPawColorLight[1] = ArgbEncode(0xff, 235, 245, 212);\r
332         m_pThemeData->clrPawColorDark[1] = ArgbEncode(0xff, 182, 198, 142);\r
333         m_pThemeData->clrBtnBK[1][0] = ArgbEncode(0xff, 200, 213, 170);\r
334         m_pThemeData->clrBtnBK[1][1] = ArgbEncode(0xff, 195, 208, 150);\r
335         m_pThemeData->clrBtnBorder[1] = ArgbEncode(0xff, 189, 203, 150);\r
336         m_pThemeData->clrPawColorLight[2] = ArgbEncode(0xff, 208, 223, 172);\r
337         m_pThemeData->clrPawColorDark[2] = ArgbEncode(0xff, 140, 157, 115);\r
338         m_pThemeData->clrBtnBK[2][0] = ArgbEncode(0xff, 164, 180, 139);\r
339         m_pThemeData->clrBtnBK[2][1] = ArgbEncode(0xff, 141, 157, 115);\r
340         m_pThemeData->clrBtnBorder[2] = ArgbEncode(0xff, 128, 146, 102);\r
341         m_pThemeData->clrTrackBKStart = ArgbEncode(0xff, 243, 241, 236);\r
342         m_pThemeData->clrTrackBKEnd = ArgbEncode(0xff, 254, 254, 251);\r
343     } else {\r
344         m_pThemeData->clrPawColorLight[0] = ArgbEncode(0xff, 238, 244, 254);\r
345         m_pThemeData->clrPawColorDark[0] = ArgbEncode(0xff, 140, 176, 248);\r
346         m_pThemeData->clrBtnBK[0][0] = ArgbEncode(0xff, 197, 213, 252);\r
347         m_pThemeData->clrBtnBK[0][1] = ArgbEncode(0xff, 182, 205, 251);\r
348         m_pThemeData->clrBtnBorder[0] = ArgbEncode(0xff, 148, 176, 221);\r
349         m_pThemeData->clrPawColorLight[1] = ArgbEncode(0xff, 252, 253, 255);\r
350         m_pThemeData->clrPawColorDark[1] = ArgbEncode(0xff, 156, 197, 255);\r
351         m_pThemeData->clrBtnBK[1][0] = ArgbEncode(0xff, 216, 232, 255);\r
352         m_pThemeData->clrBtnBK[1][1] = ArgbEncode(0xff, 204, 225, 255);\r
353         m_pThemeData->clrBtnBorder[1] = ArgbEncode(0xff, 218, 230, 254);\r
354         m_pThemeData->clrPawColorLight[2] = ArgbEncode(0xff, 207, 221, 253);\r
355         m_pThemeData->clrPawColorDark[2] = ArgbEncode(0xff, 131, 158, 216);\r
356         m_pThemeData->clrBtnBK[2][0] = ArgbEncode(0xff, 167, 190, 245);\r
357         m_pThemeData->clrBtnBK[2][1] = ArgbEncode(0xff, 146, 179, 249);\r
358         m_pThemeData->clrBtnBorder[2] = ArgbEncode(0xff, 124, 159, 211);\r
359         m_pThemeData->clrTrackBKStart = ArgbEncode(0xff, 243, 241, 236);\r
360         m_pThemeData->clrTrackBKEnd = ArgbEncode(0xff, 254, 254, 251);\r
361     }\r
362 }\r