xref: /aosp_15_r20/external/pdfium/xfa/fwl/theme/cfwl_pushbuttontp.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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_pushbuttontp.h"
8 
9 #include "xfa/fgas/graphics/cfgas_gecolor.h"
10 #include "xfa/fgas/graphics/cfgas_gegraphics.h"
11 #include "xfa/fgas/graphics/cfgas_gepath.h"
12 #include "xfa/fwl/cfwl_pushbutton.h"
13 #include "xfa/fwl/cfwl_themebackground.h"
14 #include "xfa/fwl/cfwl_widget.h"
15 #include "xfa/fwl/ifwl_themeprovider.h"
16 
17 namespace {
18 
19 constexpr float kPushbuttonSizeCorner = 2.0f;
20 
21 }  // namespace
22 
CFWL_PushButtonTP()23 CFWL_PushButtonTP::CFWL_PushButtonTP() : m_pThemeData(new PBThemeData) {
24   SetThemeData();
25 }
26 
27 CFWL_PushButtonTP::~CFWL_PushButtonTP() = default;
28 
DrawBackground(const CFWL_ThemeBackground & pParams)29 void CFWL_PushButtonTP::DrawBackground(const CFWL_ThemeBackground& pParams) {
30   switch (pParams.GetPart()) {
31     case CFWL_ThemePart::Part::kBorder: {
32       DrawBorder(pParams.GetGraphics(), pParams.m_PartRect, pParams.m_matrix);
33       break;
34     }
35     case CFWL_ThemePart::Part::kBackground: {
36       const CFX_RectF& rect = pParams.m_PartRect;
37       float fRight = rect.right();
38       float fBottom = rect.bottom();
39 
40       CFGAS_GEPath strokePath;
41       strokePath.MoveTo(
42           CFX_PointF(rect.left + kPushbuttonSizeCorner, rect.top));
43       strokePath.LineTo(CFX_PointF(fRight - kPushbuttonSizeCorner, rect.top));
44       strokePath.LineTo(CFX_PointF(fRight, rect.top + kPushbuttonSizeCorner));
45       strokePath.LineTo(CFX_PointF(fRight, fBottom - kPushbuttonSizeCorner));
46       strokePath.LineTo(CFX_PointF(fRight - kPushbuttonSizeCorner, fBottom));
47       strokePath.LineTo(CFX_PointF(rect.left + kPushbuttonSizeCorner, fBottom));
48       strokePath.LineTo(CFX_PointF(rect.left, fBottom - kPushbuttonSizeCorner));
49       strokePath.LineTo(
50           CFX_PointF(rect.left, rect.top + kPushbuttonSizeCorner));
51       strokePath.LineTo(
52           CFX_PointF(rect.left + kPushbuttonSizeCorner, rect.top));
53 
54       CFGAS_GEPath fillPath;
55       fillPath.AddSubpath(strokePath);
56 
57       CFGAS_GEGraphics* pGraphics = pParams.GetGraphics();
58       CFGAS_GEGraphics::StateRestorer restorer(pGraphics);
59       CFX_RectF rtInner(rect);
60       rtInner.Deflate(kPushbuttonSizeCorner + 1, kPushbuttonSizeCorner + 1,
61                       kPushbuttonSizeCorner, kPushbuttonSizeCorner);
62       fillPath.AddRectangle(rtInner.left, rtInner.top, rtInner.width,
63                             rtInner.height);
64 
65       int32_t iColor = GetColorID(pParams.m_dwStates);
66       FillSolidRect(pGraphics, m_pThemeData->clrEnd[iColor], rect,
67                     pParams.m_matrix);
68 
69       pGraphics->SetStrokeColor(CFGAS_GEColor(m_pThemeData->clrBorder[iColor]));
70       pGraphics->StrokePath(strokePath, pParams.m_matrix);
71 
72       fillPath.Clear();
73       fillPath.AddRectangle(rtInner.left, rtInner.top, rtInner.width,
74                             rtInner.height);
75 
76       pGraphics->SetFillColor(CFGAS_GEColor(m_pThemeData->clrFill[iColor]));
77       pGraphics->FillPath(fillPath, CFX_FillRenderOptions::FillType::kWinding,
78                           pParams.m_matrix);
79       if (pParams.m_dwStates & CFWL_PartState::kFocused) {
80         rtInner.Inflate(1, 1, 0, 0);
81         DrawFocus(pGraphics, rtInner, pParams.m_matrix);
82       }
83       break;
84     }
85     default:
86       break;
87   }
88 }
89 
SetThemeData()90 void CFWL_PushButtonTP::SetThemeData() {
91   m_pThemeData->clrBorder[0] = ArgbEncode(255, 0, 60, 116);
92   m_pThemeData->clrBorder[1] = ArgbEncode(255, 0, 60, 116);
93   m_pThemeData->clrBorder[2] = ArgbEncode(255, 0, 60, 116);
94   m_pThemeData->clrBorder[3] = ArgbEncode(255, 0, 60, 116);
95   m_pThemeData->clrBorder[4] = ArgbEncode(255, 201, 199, 186);
96   m_pThemeData->clrStart[0] = ArgbEncode(255, 255, 255, 255);
97   m_pThemeData->clrStart[1] = ArgbEncode(255, 209, 204, 193);
98   m_pThemeData->clrStart[2] = ArgbEncode(255, 255, 240, 207);
99   m_pThemeData->clrStart[3] = ArgbEncode(255, 206, 231, 255);
100   m_pThemeData->clrStart[4] = ArgbEncode(255, 245, 244, 234);
101   m_pThemeData->clrEnd[0] = ArgbEncode(255, 214, 208, 197);
102   m_pThemeData->clrEnd[1] = ArgbEncode(255, 242, 241, 238);
103   m_pThemeData->clrEnd[2] = ArgbEncode(255, 229, 151, 0);
104   m_pThemeData->clrEnd[3] = ArgbEncode(255, 105, 130, 238);
105   m_pThemeData->clrEnd[4] = ArgbEncode(255, 245, 244, 234);
106   m_pThemeData->clrFill[0] = ArgbEncode(255, 255, 255, 255);
107   m_pThemeData->clrFill[1] = ArgbEncode(255, 226, 225, 218);
108   m_pThemeData->clrFill[2] = ArgbEncode(255, 255, 255, 255);
109   m_pThemeData->clrFill[3] = ArgbEncode(255, 255, 255, 255);
110   m_pThemeData->clrFill[4] = ArgbEncode(255, 245, 244, 234);
111 }
112 
GetColorID(Mask<CFWL_PartState> dwStates) const113 int32_t CFWL_PushButtonTP::GetColorID(Mask<CFWL_PartState> dwStates) const {
114   int32_t color = 0;
115   if (dwStates & CFWL_PartState::kDisabled)
116     color += 4;
117   if (dwStates & CFWL_PartState::kDefault) {
118     color += 3;
119   } else {
120     if (dwStates & CFWL_PartState::kHovered)
121       color += 2;
122     if (dwStates & CFWL_PartState::kPressed)
123       color += 1;
124   }
125   return color;
126 }
127