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/fxfa/cxfa_ffpushbutton.h"
8
9 #include <utility>
10
11 #include "core/fxge/render_defines.h"
12 #include "third_party/base/ptr_util.h"
13 #include "xfa/fwl/cfwl_notedriver.h"
14 #include "xfa/fwl/cfwl_pushbutton.h"
15 #include "xfa/fwl/cfwl_widgetmgr.h"
16 #include "xfa/fxfa/cxfa_ffapp.h"
17 #include "xfa/fxfa/cxfa_fffield.h"
18 #include "xfa/fxfa/cxfa_ffpageview.h"
19 #include "xfa/fxfa/cxfa_ffwidget.h"
20 #include "xfa/fxfa/cxfa_textlayout.h"
21 #include "xfa/fxfa/cxfa_textprovider.h"
22 #include "xfa/fxfa/parser/cxfa_border.h"
23 #include "xfa/fxfa/parser/cxfa_button.h"
24 #include "xfa/fxfa/parser/cxfa_caption.h"
25 #include "xfa/fxfa/parser/cxfa_edge.h"
26 #include "xfa/fxgraphics/cxfa_gecolor.h"
27 #include "xfa/fxgraphics/cxfa_gepath.h"
28
CXFA_FFPushButton(CXFA_Node * pNode,CXFA_Button * button)29 CXFA_FFPushButton::CXFA_FFPushButton(CXFA_Node* pNode, CXFA_Button* button)
30 : CXFA_FFField(pNode), button_(button) {}
31
32 CXFA_FFPushButton::~CXFA_FFPushButton() = default;
33
RenderWidget(CXFA_Graphics * pGS,const CFX_Matrix & matrix,HighlightOption highlight)34 void CXFA_FFPushButton::RenderWidget(CXFA_Graphics* pGS,
35 const CFX_Matrix& matrix,
36 HighlightOption highlight) {
37 if (!HasVisibleStatus())
38 return;
39
40 CFX_Matrix mtRotate = GetRotateMatrix();
41 mtRotate.Concat(matrix);
42
43 CXFA_FFWidget::RenderWidget(pGS, mtRotate, highlight);
44 RenderHighlightCaption(pGS, &mtRotate);
45
46 CFX_RectF rtWidget = GetRectWithoutRotate();
47 CFX_Matrix mt(1, 0, 0, 1, rtWidget.left, rtWidget.top);
48 mt.Concat(mtRotate);
49 GetApp()->GetFWLWidgetMgr()->OnDrawWidget(GetNormalWidget(), pGS, mt);
50 }
51
LoadWidget()52 bool CXFA_FFPushButton::LoadWidget() {
53 ASSERT(!IsLoaded());
54 auto pNew = pdfium::MakeUnique<CFWL_PushButton>(GetFWLApp());
55 CFWL_PushButton* pPushButton = pNew.get();
56 m_pOldDelegate = pPushButton->GetDelegate();
57 pPushButton->SetDelegate(this);
58 SetNormalWidget(std::move(pNew));
59 pPushButton->SetAdapterIface(this);
60
61 CFWL_NoteDriver* pNoteDriver = pPushButton->GetOwnerApp()->GetNoteDriver();
62 pNoteDriver->RegisterEventTarget(pPushButton, pPushButton);
63
64 {
65 CFWL_Widget::ScopedUpdateLock update_lock(pPushButton);
66 UpdateWidgetProperty();
67 LoadHighlightCaption();
68 }
69
70 return CXFA_FFField::LoadWidget();
71 }
72
UpdateWidgetProperty()73 void CXFA_FFPushButton::UpdateWidgetProperty() {
74 uint32_t dwStyleEx = 0;
75 switch (button_->GetHighlight()) {
76 case XFA_AttributeValue::Inverted:
77 dwStyleEx = XFA_FWL_PSBSTYLEEXT_HiliteInverted;
78 break;
79 case XFA_AttributeValue::Outline:
80 dwStyleEx = XFA_FWL_PSBSTYLEEXT_HiliteOutLine;
81 break;
82 case XFA_AttributeValue::Push:
83 dwStyleEx = XFA_FWL_PSBSTYLEEXT_HilitePush;
84 break;
85 default:
86 break;
87 }
88 GetNormalWidget()->ModifyStylesEx(dwStyleEx, 0xFFFFFFFF);
89 }
90
PerformLayout()91 bool CXFA_FFPushButton::PerformLayout() {
92 CXFA_FFWidget::PerformLayout();
93 CFX_RectF rtWidget = GetRectWithoutRotate();
94
95 m_rtUI = rtWidget;
96 CXFA_Margin* margin = m_pNode->GetMarginIfExists();
97 XFA_RectWithoutMargin(&rtWidget, margin);
98
99 m_rtCaption = rtWidget;
100
101 CXFA_Caption* caption = m_pNode->GetCaptionIfExists();
102 CXFA_Margin* captionMargin = caption ? caption->GetMarginIfExists() : nullptr;
103 XFA_RectWithoutMargin(&m_rtCaption, captionMargin);
104
105 LayoutHighlightCaption();
106 SetFWLRect();
107 if (GetNormalWidget())
108 GetNormalWidget()->Update();
109
110 return true;
111 }
112
GetLineWidth()113 float CXFA_FFPushButton::GetLineWidth() {
114 CXFA_Border* border = m_pNode->GetBorderIfExists();
115 if (border && border->GetPresence() == XFA_AttributeValue::Visible) {
116 CXFA_Edge* edge = border->GetEdgeIfExists(0);
117 return edge ? edge->GetThickness() : 0;
118 }
119 return 0;
120 }
121
GetLineColor()122 FX_ARGB CXFA_FFPushButton::GetLineColor() {
123 return 0xFF000000;
124 }
125
GetFillColor()126 FX_ARGB CXFA_FFPushButton::GetFillColor() {
127 return 0xFFFFFFFF;
128 }
129
LoadHighlightCaption()130 void CXFA_FFPushButton::LoadHighlightCaption() {
131 CXFA_Caption* caption = m_pNode->GetCaptionIfExists();
132 if (!caption || caption->IsHidden())
133 return;
134
135 if (m_pNode->HasButtonRollover()) {
136 if (!m_pRollProvider) {
137 m_pRollProvider = pdfium::MakeUnique<CXFA_TextProvider>(
138 m_pNode.Get(), XFA_TEXTPROVIDERTYPE_Rollover);
139 }
140 m_pRolloverTextLayout =
141 pdfium::MakeUnique<CXFA_TextLayout>(GetDoc(), m_pRollProvider.get());
142 }
143
144 if (m_pNode->HasButtonDown()) {
145 if (!m_pDownProvider) {
146 m_pDownProvider = pdfium::MakeUnique<CXFA_TextProvider>(
147 m_pNode.Get(), XFA_TEXTPROVIDERTYPE_Down);
148 }
149 m_pDownTextLayout =
150 pdfium::MakeUnique<CXFA_TextLayout>(GetDoc(), m_pDownProvider.get());
151 }
152 }
153
LayoutHighlightCaption()154 void CXFA_FFPushButton::LayoutHighlightCaption() {
155 CFX_SizeF sz(m_rtCaption.width, m_rtCaption.height);
156 LayoutCaption();
157 if (m_pRolloverTextLayout)
158 m_pRolloverTextLayout->Layout(sz);
159 if (m_pDownTextLayout)
160 m_pDownTextLayout->Layout(sz);
161 }
162
RenderHighlightCaption(CXFA_Graphics * pGS,CFX_Matrix * pMatrix)163 void CXFA_FFPushButton::RenderHighlightCaption(CXFA_Graphics* pGS,
164 CFX_Matrix* pMatrix) {
165 CXFA_TextLayout* pCapTextLayout = m_pNode->GetCaptionTextLayout();
166 CXFA_Caption* caption = m_pNode->GetCaptionIfExists();
167 if (!caption || !caption->IsVisible())
168 return;
169
170 CFX_RenderDevice* pRenderDevice = pGS->GetRenderDevice();
171 CFX_RectF rtClip = m_rtCaption;
172 rtClip.Intersect(GetRectWithoutRotate());
173 CFX_Matrix mt(1, 0, 0, 1, m_rtCaption.left, m_rtCaption.top);
174 if (pMatrix) {
175 rtClip = pMatrix->TransformRect(rtClip);
176 mt.Concat(*pMatrix);
177 }
178
179 uint32_t dwState = GetNormalWidget()->GetStates();
180 if (m_pDownTextLayout && (dwState & FWL_STATE_PSB_Pressed) &&
181 (dwState & FWL_STATE_PSB_Hovered)) {
182 if (m_pDownTextLayout->DrawString(pRenderDevice, mt, rtClip, 0))
183 return;
184 } else if (m_pRolloverTextLayout && (dwState & FWL_STATE_PSB_Hovered)) {
185 if (m_pRolloverTextLayout->DrawString(pRenderDevice, mt, rtClip, 0))
186 return;
187 }
188
189 if (pCapTextLayout)
190 pCapTextLayout->DrawString(pRenderDevice, mt, rtClip, 0);
191 }
192
OnProcessMessage(CFWL_Message * pMessage)193 void CXFA_FFPushButton::OnProcessMessage(CFWL_Message* pMessage) {
194 m_pOldDelegate->OnProcessMessage(pMessage);
195 }
196
OnProcessEvent(CFWL_Event * pEvent)197 void CXFA_FFPushButton::OnProcessEvent(CFWL_Event* pEvent) {
198 m_pOldDelegate->OnProcessEvent(pEvent);
199 CXFA_FFField::OnProcessEvent(pEvent);
200 }
201
OnDrawWidget(CXFA_Graphics * pGraphics,const CFX_Matrix & matrix)202 void CXFA_FFPushButton::OnDrawWidget(CXFA_Graphics* pGraphics,
203 const CFX_Matrix& matrix) {
204 auto* pWidget = GetNormalWidget();
205 if (pWidget->GetStylesEx() & XFA_FWL_PSBSTYLEEXT_HiliteInverted) {
206 if ((pWidget->GetStates() & FWL_STATE_PSB_Pressed) &&
207 (pWidget->GetStates() & FWL_STATE_PSB_Hovered)) {
208 CFX_RectF rtFill(0, 0, pWidget->GetWidgetRect().Size());
209 float fLineWith = GetLineWidth();
210 rtFill.Deflate(fLineWith, fLineWith);
211 CXFA_GEPath path;
212 path.AddRectangle(rtFill.left, rtFill.top, rtFill.width, rtFill.height);
213 pGraphics->SetFillColor(CXFA_GEColor(ArgbEncode(128, 128, 255, 255)));
214 pGraphics->FillPath(&path, FXFILL_WINDING, &matrix);
215 }
216 return;
217 }
218
219 if (pWidget->GetStylesEx() & XFA_FWL_PSBSTYLEEXT_HiliteOutLine) {
220 if ((pWidget->GetStates() & FWL_STATE_PSB_Pressed) &&
221 (pWidget->GetStates() & FWL_STATE_PSB_Hovered)) {
222 float fLineWidth = GetLineWidth();
223 pGraphics->SetStrokeColor(CXFA_GEColor(ArgbEncode(255, 128, 255, 255)));
224 pGraphics->SetLineWidth(fLineWidth);
225
226 CXFA_GEPath path;
227 CFX_RectF rect = pWidget->GetWidgetRect();
228 path.AddRectangle(0, 0, rect.width, rect.height);
229 pGraphics->StrokePath(&path, &matrix);
230 }
231 }
232 }
233
GetFormFieldType()234 FormFieldType CXFA_FFPushButton::GetFormFieldType() {
235 return FormFieldType::kXFA_PushButton;
236 }
237