1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "xfa/fwl/core/ifwl_pushbutton.h"
#include <memory>
#include <utility>
#include "third_party/base/ptr_util.h"
#include "xfa/fde/tto/fde_textout.h"
#include "xfa/fwl/core/cfwl_evtclick.h"
#include "xfa/fwl/core/cfwl_evtmouse.h"
#include "xfa/fwl/core/cfwl_msgkey.h"
#include "xfa/fwl/core/cfwl_msgmouse.h"
#include "xfa/fwl/core/cfwl_themebackground.h"
#include "xfa/fwl/core/cfwl_themetext.h"
#include "xfa/fwl/core/fwl_noteimp.h"
#include "xfa/fwl/core/ifwl_themeprovider.h"
IFWL_PushButton::IFWL_PushButton(
const IFWL_App* app,
std::unique_ptr<CFWL_WidgetProperties> properties)
: IFWL_Widget(app, std::move(properties), nullptr),
m_bBtnDown(false),
m_dwTTOStyles(FDE_TTOSTYLE_SingleLine),
m_iTTOAlign(FDE_TTOALIGNMENT_Center) {
m_rtClient.Set(0, 0, 0, 0);
m_rtCaption.Set(0, 0, 0, 0);
}
IFWL_PushButton::~IFWL_PushButton() {}
FWL_Type IFWL_PushButton::GetClassID() const {
return FWL_Type::PushButton;
}
void IFWL_PushButton::GetWidgetRect(CFX_RectF& rect, bool bAutoSize) {
if (!bAutoSize) {
rect = m_pProperties->m_rtWidget;
return;
}
rect.Set(0, 0, 0, 0);
if (!m_pProperties->m_pThemeProvider)
m_pProperties->m_pThemeProvider = GetAvailableTheme();
CFX_WideString wsCaption;
if (m_pProperties->m_pDataProvider)
m_pProperties->m_pDataProvider->GetCaption(this, wsCaption);
int32_t iLen = wsCaption.GetLength();
if (iLen > 0) {
CFX_SizeF sz = CalcTextSize(wsCaption, m_pProperties->m_pThemeProvider);
rect.Set(0, 0, sz.x, sz.y);
}
FX_FLOAT* fcaption =
static_cast<FX_FLOAT*>(GetThemeCapacity(CFWL_WidgetCapacity::Margin));
rect.Inflate(*fcaption, *fcaption);
IFWL_Widget::GetWidgetRect(rect, true);
}
void IFWL_PushButton::SetStates(uint32_t dwStates, bool bSet) {
if ((dwStates & FWL_WGTSTATE_Disabled) && bSet) {
m_pProperties->m_dwStates = FWL_WGTSTATE_Disabled;
return;
}
IFWL_Widget::SetStates(dwStates, bSet);
}
void IFWL_PushButton::Update() {
if (IsLocked())
return;
if (!m_pProperties->m_pThemeProvider)
m_pProperties->m_pThemeProvider = GetAvailableTheme();
UpdateTextOutStyles();
GetClientRect(m_rtClient);
m_rtCaption = m_rtClient;
FX_FLOAT* fcaption =
static_cast<FX_FLOAT*>(GetThemeCapacity(CFWL_WidgetCapacity::Margin));
m_rtCaption.Inflate(-*fcaption, -*fcaption);
}
void IFWL_PushButton::DrawWidget(CFX_Graphics* pGraphics,
const CFX_Matrix* pMatrix) {
if (!pGraphics)
return;
if (!m_pProperties->m_pThemeProvider)
return;
IFWL_ThemeProvider* pTheme = m_pProperties->m_pThemeProvider;
if (HasBorder()) {
DrawBorder(pGraphics, CFWL_Part::Border, m_pProperties->m_pThemeProvider,
pMatrix);
}
if (HasEdge()) {
DrawEdge(pGraphics, CFWL_Part::Edge, m_pProperties->m_pThemeProvider,
pMatrix);
}
DrawBkground(pGraphics, m_pProperties->m_pThemeProvider, pMatrix);
CFX_Matrix matrix;
matrix.Concat(*pMatrix);
CFX_WideString wsCaption;
if (m_pProperties->m_pDataProvider)
m_pProperties->m_pDataProvider->GetCaption(this, wsCaption);
CFX_RectF rtText;
rtText.Set(0, 0, 0, 0);
if (!wsCaption.IsEmpty())
CalcTextRect(wsCaption, pTheme, 0, m_iTTOAlign, rtText);
switch (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_PSB_ModeMask) {
case FWL_STYLEEXT_PSB_TextOnly:
DrawText(pGraphics, m_pProperties->m_pThemeProvider, &matrix);
break;
case FWL_STYLEEXT_PSB_IconOnly:
case FWL_STYLEEXT_PSB_TextIcon:
break;
}
}
void IFWL_PushButton::DrawBkground(CFX_Graphics* pGraphics,
IFWL_ThemeProvider* pTheme,
const CFX_Matrix* pMatrix) {
CFWL_ThemeBackground param;
param.m_pWidget = this;
param.m_iPart = CFWL_Part::Background;
param.m_dwStates = GetPartStates();
param.m_pGraphics = pGraphics;
if (pMatrix)
param.m_matrix.Concat(*pMatrix);
param.m_rtPart = m_rtClient;
if (m_pProperties->m_dwStates & FWL_WGTSTATE_Focused)
param.m_pData = &m_rtCaption;
pTheme->DrawBackground(¶m);
}
void IFWL_PushButton::DrawText(CFX_Graphics* pGraphics,
IFWL_ThemeProvider* pTheme,
const CFX_Matrix* pMatrix) {
if (!m_pProperties->m_pDataProvider)
return;
CFX_WideString wsCaption;
m_pProperties->m_pDataProvider->GetCaption(this, wsCaption);
if (wsCaption.IsEmpty())
return;
CFWL_ThemeText param;
param.m_pWidget = this;
param.m_iPart = CFWL_Part::Caption;
param.m_dwStates = GetPartStates();
param.m_pGraphics = pGraphics;
if (pMatrix)
param.m_matrix.Concat(*pMatrix);
param.m_rtPart = m_rtCaption;
param.m_wsText = wsCaption;
param.m_dwTTOStyles = m_dwTTOStyles;
param.m_iTTOAlign = m_iTTOAlign;
pTheme->DrawText(¶m);
}
uint32_t IFWL_PushButton::GetPartStates() {
uint32_t dwStates = CFWL_PartState_Normal;
if (m_pProperties->m_dwStates & FWL_WGTSTATE_Focused)
dwStates |= CFWL_PartState_Focused;
if (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled)
dwStates = CFWL_PartState_Disabled;
else if (m_pProperties->m_dwStates & FWL_STATE_PSB_Pressed)
dwStates |= CFWL_PartState_Pressed;
else if (m_pProperties->m_dwStates & FWL_STATE_PSB_Hovered)
dwStates |= CFWL_PartState_Hovered;
else if (m_pProperties->m_dwStates & FWL_STATE_PSB_Default)
dwStates |= CFWL_PartState_Default;
return dwStates;
}
void IFWL_PushButton::UpdateTextOutStyles() {
switch (m_pProperties->m_dwStyleExes &
(FWL_STYLEEXT_PSB_HLayoutMask | FWL_STYLEEXT_PSB_VLayoutMask)) {
case FWL_STYLEEXT_PSB_Left | FWL_STYLEEXT_PSB_Top: {
m_iTTOAlign = FDE_TTOALIGNMENT_TopLeft;
break;
}
case FWL_STYLEEXT_PSB_Center | FWL_STYLEEXT_PSB_Top: {
m_iTTOAlign = FDE_TTOALIGNMENT_TopCenter;
break;
}
case FWL_STYLEEXT_PSB_Right | FWL_STYLEEXT_PSB_Top: {
m_iTTOAlign = FDE_TTOALIGNMENT_TopRight;
break;
}
case FWL_STYLEEXT_PSB_Left | FWL_STYLEEXT_PSB_VCenter: {
m_iTTOAlign = FDE_TTOALIGNMENT_CenterLeft;
break;
}
case FWL_STYLEEXT_PSB_Right | FWL_STYLEEXT_PSB_VCenter: {
m_iTTOAlign = FDE_TTOALIGNMENT_CenterRight;
break;
}
case FWL_STYLEEXT_PSB_Left | FWL_STYLEEXT_PSB_Bottom: {
m_iTTOAlign = FDE_TTOALIGNMENT_BottomLeft;
break;
}
case FWL_STYLEEXT_PSB_Center | FWL_STYLEEXT_PSB_Bottom: {
m_iTTOAlign = FDE_TTOALIGNMENT_BottomCenter;
break;
}
case FWL_STYLEEXT_PSB_Right | FWL_STYLEEXT_PSB_Bottom: {
m_iTTOAlign = FDE_TTOALIGNMENT_BottomRight;
break;
}
case FWL_STYLEEXT_PSB_Center | FWL_STYLEEXT_PSB_VCenter:
default: {
m_iTTOAlign = FDE_TTOALIGNMENT_Center;
break;
}
}
m_dwTTOStyles = FDE_TTOSTYLE_SingleLine;
if (m_pProperties->m_dwStyleExes & FWL_WGTSTYLE_RTLReading)
m_dwTTOStyles |= FDE_TTOSTYLE_RTL;
}
void IFWL_PushButton::OnProcessMessage(CFWL_Message* pMessage) {
if (!pMessage)
return;
if (!IsEnabled())
return;
CFWL_MessageType dwMsgCode = pMessage->GetClassID();
switch (dwMsgCode) {
case CFWL_MessageType::SetFocus:
OnFocusChanged(pMessage, true);
break;
case CFWL_MessageType::KillFocus:
OnFocusChanged(pMessage, false);
break;
case CFWL_MessageType::Mouse: {
CFWL_MsgMouse* pMsg = static_cast<CFWL_MsgMouse*>(pMessage);
switch (pMsg->m_dwCmd) {
case FWL_MouseCommand::LeftButtonDown:
OnLButtonDown(pMsg);
break;
case FWL_MouseCommand::LeftButtonUp:
OnLButtonUp(pMsg);
break;
case FWL_MouseCommand::Move:
OnMouseMove(pMsg);
break;
case FWL_MouseCommand::Leave:
OnMouseLeave(pMsg);
break;
default:
break;
}
break;
}
case CFWL_MessageType::Key: {
CFWL_MsgKey* pKey = static_cast<CFWL_MsgKey*>(pMessage);
if (pKey->m_dwCmd == FWL_KeyCommand::KeyDown)
OnKeyDown(pKey);
break;
}
default:
break;
}
IFWL_Widget::OnProcessMessage(pMessage);
}
void IFWL_PushButton::OnDrawWidget(CFX_Graphics* pGraphics,
const CFX_Matrix* pMatrix) {
DrawWidget(pGraphics, pMatrix);
}
void IFWL_PushButton::OnFocusChanged(CFWL_Message* pMsg, bool bSet) {
if (bSet)
m_pProperties->m_dwStates |= FWL_WGTSTATE_Focused;
else
m_pProperties->m_dwStates &= ~FWL_WGTSTATE_Focused;
Repaint(&m_rtClient);
}
void IFWL_PushButton::OnLButtonDown(CFWL_MsgMouse* pMsg) {
if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) == 0)
SetFocus(true);
m_bBtnDown = true;
m_pProperties->m_dwStates |= FWL_STATE_PSB_Hovered;
m_pProperties->m_dwStates |= FWL_STATE_PSB_Pressed;
Repaint(&m_rtClient);
}
void IFWL_PushButton::OnLButtonUp(CFWL_MsgMouse* pMsg) {
m_bBtnDown = false;
if (m_rtClient.Contains(pMsg->m_fx, pMsg->m_fy)) {
m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Pressed;
m_pProperties->m_dwStates |= FWL_STATE_PSB_Hovered;
} else {
m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Hovered;
m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Pressed;
}
if (m_rtClient.Contains(pMsg->m_fx, pMsg->m_fy)) {
CFWL_EvtClick wmClick;
wmClick.m_pSrcTarget = this;
DispatchEvent(&wmClick);
}
Repaint(&m_rtClient);
}
void IFWL_PushButton::OnMouseMove(CFWL_MsgMouse* pMsg) {
bool bRepaint = false;
if (m_bBtnDown) {
if (m_rtClient.Contains(pMsg->m_fx, pMsg->m_fy)) {
if ((m_pProperties->m_dwStates & FWL_STATE_PSB_Pressed) == 0) {
m_pProperties->m_dwStates |= FWL_STATE_PSB_Pressed;
bRepaint = true;
}
if (m_pProperties->m_dwStates & FWL_STATE_PSB_Hovered) {
m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Hovered;
bRepaint = true;
}
} else {
if (m_pProperties->m_dwStates & FWL_STATE_PSB_Pressed) {
m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Pressed;
bRepaint = true;
}
if ((m_pProperties->m_dwStates & FWL_STATE_PSB_Hovered) == 0) {
m_pProperties->m_dwStates |= FWL_STATE_PSB_Hovered;
bRepaint = true;
}
}
} else {
if (!m_rtClient.Contains(pMsg->m_fx, pMsg->m_fy))
return;
if ((m_pProperties->m_dwStates & FWL_STATE_PSB_Hovered) == 0) {
m_pProperties->m_dwStates |= FWL_STATE_PSB_Hovered;
bRepaint = true;
}
}
if (bRepaint)
Repaint(&m_rtClient);
}
void IFWL_PushButton::OnMouseLeave(CFWL_MsgMouse* pMsg) {
m_bBtnDown = false;
m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Hovered;
m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Pressed;
Repaint(&m_rtClient);
}
void IFWL_PushButton::OnKeyDown(CFWL_MsgKey* pMsg) {
if (pMsg->m_dwKeyCode == FWL_VKEY_Return) {
CFWL_EvtMouse wmMouse;
wmMouse.m_pSrcTarget = this;
wmMouse.m_dwCmd = FWL_MouseCommand::LeftButtonUp;
DispatchEvent(&wmMouse);
CFWL_EvtClick wmClick;
wmClick.m_pSrcTarget = this;
DispatchEvent(&wmClick);
return;
}
if (pMsg->m_dwKeyCode != FWL_VKEY_Tab)
return;
DispatchKeyEvent(pMsg);
}
|