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
55 // Prevents destruction of the CXFA_ContentLayoutItem that owns |this|.
56 RetainPtr<CXFA_ContentLayoutItem> retain_layout(m_pLayoutItem.Get());
57
58 auto pNew = pdfium::MakeUnique<CFWL_PushButton>(GetFWLApp());
59 CFWL_PushButton* pPushButton = pNew.get();
60 m_pOldDelegate = pPushButton->GetDelegate();
61 pPushButton->SetDelegate(this);
62 SetNormalWidget(std::move(pNew));
63 pPushButton->SetAdapterIface(this);
64
65 CFWL_NoteDriver* pNoteDriver = pPushButton->GetOwnerApp()->GetNoteDriver();
66 pNoteDriver->RegisterEventTarget(pPushButton, pPushButton);
67
68 {
69 CFWL_Widget::ScopedUpdateLock update_lock(pPushButton);
70 UpdateWidgetProperty();
71 LoadHighlightCaption();
72 }
73
74 return CXFA_FFField::LoadWidget();
75 }
76
UpdateWidgetProperty()77 void CXFA_FFPushButton::UpdateWidgetProperty() {
78 uint32_t dwStyleEx = 0;
79 switch (button_->GetHighlight()) {
80 case XFA_AttributeValue::Inverted:
81 dwStyleEx = XFA_FWL_PSBSTYLEEXT_HiliteInverted;
82 break;
83 case XFA_AttributeValue::Outline:
84 dwStyleEx = XFA_FWL_PSBSTYLEEXT_HiliteOutLine;
85 break;
86 case XFA_AttributeValue::Push:
87 dwStyleEx = XFA_FWL_PSBSTYLEEXT_HilitePush;
88 break;
89 default:
90 break;
91 }
92 GetNormalWidget()->ModifyStylesEx(dwStyleEx, 0xFFFFFFFF);
93 }
94
PerformLayout()95 bool CXFA_FFPushButton::PerformLayout() {
96 CXFA_FFWidget::PerformLayout();
97 CFX_RectF rtWidget = GetRectWithoutRotate();
98
99 m_rtUI = rtWidget;
100 CXFA_Margin* margin = m_pNode->GetMarginIfExists();
101 XFA_RectWithoutMargin(&rtWidget, margin);
102
103 m_rtCaption = rtWidget;
104
105 CXFA_Caption* caption = m_pNode->GetCaptionIfExists();
106 CXFA_Margin* captionMargin = caption ? caption->GetMarginIfExists() : nullptr;
107 XFA_RectWithoutMargin(&m_rtCaption, captionMargin);
108
109 LayoutHighlightCaption();
110 SetFWLRect();
111 if (GetNormalWidget())
112 GetNormalWidget()->Update();
113
114 return true;
115 }
116
GetLineWidth()117 float CXFA_FFPushButton::GetLineWidth() {
118 CXFA_Border* border = m_pNode->GetBorderIfExists();
119 if (border && border->GetPresence() == XFA_AttributeValue::Visible) {
120 CXFA_Edge* edge = border->GetEdgeIfExists(0);
121 return edge ? edge->GetThickness() : 0;
122 }
123 return 0;
124 }
125
GetLineColor()126 FX_ARGB CXFA_FFPushButton::GetLineColor() {
127 return 0xFF000000;
128 }
129
GetFillColor()130 FX_ARGB CXFA_FFPushButton::GetFillColor() {
131 return 0xFFFFFFFF;
132 }
133
LoadHighlightCaption()134 void CXFA_FFPushButton::LoadHighlightCaption() {
135 CXFA_Caption* caption = m_pNode->GetCaptionIfExists();
136 if (!caption || caption->IsHidden())
137 return;
138
139 if (m_pNode->HasButtonRollover()) {
140 if (!m_pRollProvider) {
141 m_pRollProvider = pdfium::MakeUnique<CXFA_TextProvider>(
142 m_pNode.Get(), XFA_TEXTPROVIDERTYPE_Rollover);
143 }
144 m_pRolloverTextLayout =
145 pdfium::MakeUnique<CXFA_TextLayout>(GetDoc(), m_pRollProvider.get());
146 }
147
148 if (m_pNode->HasButtonDown()) {
149 if (!m_pDownProvider) {
150 m_pDownProvider = pdfium::MakeUnique<CXFA_TextProvider>(
151 m_pNode.Get(), XFA_TEXTPROVIDERTYPE_Down);
152 }
153 m_pDownTextLayout =
154 pdfium::MakeUnique<CXFA_TextLayout>(GetDoc(), m_pDownProvider.get());
155 }
156 }
157
LayoutHighlightCaption()158 void CXFA_FFPushButton::LayoutHighlightCaption() {
159 CFX_SizeF sz(m_rtCaption.width, m_rtCaption.height);
160 LayoutCaption();
161 if (m_pRolloverTextLayout)
162 m_pRolloverTextLayout->Layout(sz);
163 if (m_pDownTextLayout)
164 m_pDownTextLayout->Layout(sz);
165 }
166
RenderHighlightCaption(CXFA_Graphics * pGS,CFX_Matrix * pMatrix)167 void CXFA_FFPushButton::RenderHighlightCaption(CXFA_Graphics* pGS,
168 CFX_Matrix* pMatrix) {
169 CXFA_TextLayout* pCapTextLayout = m_pNode->GetCaptionTextLayout();
170 CXFA_Caption* caption = m_pNode->GetCaptionIfExists();
171 if (!caption || !caption->IsVisible())
172 return;
173
174 CFX_RenderDevice* pRenderDevice = pGS->GetRenderDevice();
175 CFX_RectF rtClip = m_rtCaption;
176 rtClip.Intersect(GetRectWithoutRotate());
177 CFX_Matrix mt(1, 0, 0, 1, m_rtCaption.left, m_rtCaption.top);
178 if (pMatrix) {
179 rtClip = pMatrix->TransformRect(rtClip);
180 mt.Concat(*pMatrix);
181 }
182
183 uint32_t dwState = GetNormalWidget()->GetStates();
184 if (m_pDownTextLayout && (dwState & FWL_STATE_PSB_Pressed) &&
185 (dwState & FWL_STATE_PSB_Hovered)) {
186 if (m_pDownTextLayout->DrawString(pRenderDevice, mt, rtClip, 0))
187 return;
188 } else if (m_pRolloverTextLayout && (dwState & FWL_STATE_PSB_Hovered)) {
189 if (m_pRolloverTextLayout->DrawString(pRenderDevice, mt, rtClip, 0))
190 return;
191 }
192
193 if (pCapTextLayout)
194 pCapTextLayout->DrawString(pRenderDevice, mt, rtClip, 0);
195 }
196
OnProcessMessage(CFWL_Message * pMessage)197 void CXFA_FFPushButton::OnProcessMessage(CFWL_Message* pMessage) {
198 // Prevents destruction of the CXFA_ContentLayoutItem that owns |this|.
199 RetainPtr<CXFA_ContentLayoutItem> retainer(m_pLayoutItem.Get());
200
201 m_pOldDelegate->OnProcessMessage(pMessage);
202 }
203
OnProcessEvent(CFWL_Event * pEvent)204 void CXFA_FFPushButton::OnProcessEvent(CFWL_Event* pEvent) {
205 // Prevents destruction of the CXFA_ContentLayoutItem that owns |this|.
206 RetainPtr<CXFA_ContentLayoutItem> retain_layout(m_pLayoutItem.Get());
207
208 m_pOldDelegate->OnProcessEvent(pEvent);
209 CXFA_FFField::OnProcessEvent(pEvent);
210 }
211
OnDrawWidget(CXFA_Graphics * pGraphics,const CFX_Matrix & matrix)212 void CXFA_FFPushButton::OnDrawWidget(CXFA_Graphics* pGraphics,
213 const CFX_Matrix& matrix) {
214 // Prevents destruction of the CXFA_ContentLayoutItem that owns |this|.
215 RetainPtr<CXFA_ContentLayoutItem> retainer(m_pLayoutItem.Get());
216
217 auto* pWidget = GetNormalWidget();
218 if (pWidget->GetStylesEx() & XFA_FWL_PSBSTYLEEXT_HiliteInverted) {
219 if ((pWidget->GetStates() & FWL_STATE_PSB_Pressed) &&
220 (pWidget->GetStates() & FWL_STATE_PSB_Hovered)) {
221 CFX_RectF rtFill(0, 0, pWidget->GetWidgetRect().Size());
222 float fLineWith = GetLineWidth();
223 rtFill.Deflate(fLineWith, fLineWith);
224 CXFA_GEPath path;
225 path.AddRectangle(rtFill.left, rtFill.top, rtFill.width, rtFill.height);
226 pGraphics->SetFillColor(CXFA_GEColor(ArgbEncode(128, 128, 255, 255)));
227 pGraphics->FillPath(&path, FXFILL_WINDING, &matrix);
228 }
229 return;
230 }
231
232 if (pWidget->GetStylesEx() & XFA_FWL_PSBSTYLEEXT_HiliteOutLine) {
233 if ((pWidget->GetStates() & FWL_STATE_PSB_Pressed) &&
234 (pWidget->GetStates() & FWL_STATE_PSB_Hovered)) {
235 float fLineWidth = GetLineWidth();
236 pGraphics->SetStrokeColor(CXFA_GEColor(ArgbEncode(255, 128, 255, 255)));
237 pGraphics->SetLineWidth(fLineWidth);
238
239 CXFA_GEPath path;
240 CFX_RectF rect = pWidget->GetWidgetRect();
241 path.AddRectangle(0, 0, rect.width, rect.height);
242 pGraphics->StrokePath(&path, &matrix);
243 }
244 }
245 }
246
GetFormFieldType()247 FormFieldType CXFA_FFPushButton::GetFormFieldType() {
248 return FormFieldType::kXFA_PushButton;
249 }
250