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
373
374
375
376
|
// 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/cfwl_spinbutton.h"
#include <memory>
#include <utility>
#include "third_party/base/ptr_util.h"
#include "xfa/fwl/core/cfwl_event.h"
#include "xfa/fwl/core/cfwl_msgkey.h"
#include "xfa/fwl/core/cfwl_msgmouse.h"
#include "xfa/fwl/core/cfwl_notedriver.h"
#include "xfa/fwl/core/cfwl_themebackground.h"
#include "xfa/fwl/core/cfwl_timerinfo.h"
#include "xfa/fwl/core/cfwl_widgetproperties.h"
#include "xfa/fwl/core/ifwl_themeprovider.h"
namespace {
const int kMinWidth = 18;
const int kMinHeight = 32;
const int kElapseTime = 200;
} // namespace
CFWL_SpinButton::CFWL_SpinButton(
const CFWL_App* app,
std::unique_ptr<CFWL_WidgetProperties> properties)
: CFWL_Widget(app, std::move(properties), nullptr),
m_dwUpState(CFWL_PartState_Normal),
m_dwDnState(CFWL_PartState_Normal),
m_iButtonIndex(0),
m_bLButtonDwn(false),
m_pTimerInfo(nullptr),
m_Timer(this) {
m_rtClient.Reset();
m_rtUpButton.Reset();
m_rtDnButton.Reset();
m_pProperties->m_dwStyleExes |= FWL_STYLEEXE_SPB_Vert;
}
CFWL_SpinButton::~CFWL_SpinButton() {}
FWL_Type CFWL_SpinButton::GetClassID() const {
return FWL_Type::SpinButton;
}
void CFWL_SpinButton::GetWidgetRect(CFX_RectF& rect, bool bAutoSize) {
if (!bAutoSize) {
rect = m_pProperties->m_rtWidget;
return;
}
rect.Set(0, 0, kMinWidth, kMinHeight);
InflateWidgetRect(rect);
}
void CFWL_SpinButton::Update() {
if (IsLocked())
return;
GetClientRect(m_rtClient);
if (m_pProperties->m_dwStyleExes & FWL_STYLEEXE_SPB_Vert) {
m_rtUpButton.Set(m_rtClient.top, m_rtClient.left, m_rtClient.width,
m_rtClient.height / 2);
m_rtDnButton.Set(m_rtClient.left, m_rtClient.top + m_rtClient.height / 2,
m_rtClient.width, m_rtClient.height / 2);
} else {
m_rtUpButton.Set(m_rtClient.left, m_rtClient.top, m_rtClient.width / 2,
m_rtClient.height);
m_rtDnButton.Set(m_rtClient.left + m_rtClient.width / 2, m_rtClient.top,
m_rtClient.width / 2, m_rtClient.height);
}
}
FWL_WidgetHit CFWL_SpinButton::HitTest(FX_FLOAT fx, FX_FLOAT fy) {
if (m_rtClient.Contains(fx, fy))
return FWL_WidgetHit::Client;
if (HasBorder() && (m_rtClient.Contains(fx, fy)))
return FWL_WidgetHit::Border;
if (HasEdge()) {
CFX_RectF rtEdge;
GetEdgeRect(rtEdge);
if (rtEdge.Contains(fx, fy))
return FWL_WidgetHit::Left;
}
if (m_rtUpButton.Contains(fx, fy))
return FWL_WidgetHit::UpButton;
if (m_rtDnButton.Contains(fx, fy))
return FWL_WidgetHit::DownButton;
return FWL_WidgetHit::Unknown;
}
void CFWL_SpinButton::DrawWidget(CFX_Graphics* pGraphics,
const CFX_Matrix* pMatrix) {
if (!pGraphics)
return;
CFX_RectF rtClip(m_rtClient);
if (pMatrix)
pMatrix->TransformRect(rtClip);
IFWL_ThemeProvider* pTheme = GetAvailableTheme();
if (HasBorder())
DrawBorder(pGraphics, CFWL_Part::Border, pTheme, pMatrix);
if (HasEdge())
DrawEdge(pGraphics, CFWL_Part::Edge, pTheme, pMatrix);
DrawUpButton(pGraphics, pTheme, pMatrix);
DrawDownButton(pGraphics, pTheme, pMatrix);
}
void CFWL_SpinButton::DisableButton() {
m_dwDnState = CFWL_PartState_Disabled;
}
bool CFWL_SpinButton::IsUpButtonEnabled() {
return m_dwUpState != CFWL_PartState_Disabled;
}
bool CFWL_SpinButton::IsDownButtonEnabled() {
return m_dwDnState != CFWL_PartState_Disabled;
}
void CFWL_SpinButton::DrawUpButton(CFX_Graphics* pGraphics,
IFWL_ThemeProvider* pTheme,
const CFX_Matrix* pMatrix) {
CFWL_ThemeBackground params;
params.m_pWidget = this;
params.m_iPart = CFWL_Part::UpButton;
params.m_pGraphics = pGraphics;
params.m_dwStates = m_dwUpState + 1;
if (pMatrix)
params.m_matrix.Concat(*pMatrix);
params.m_rtPart = m_rtUpButton;
pTheme->DrawBackground(¶ms);
}
void CFWL_SpinButton::DrawDownButton(CFX_Graphics* pGraphics,
IFWL_ThemeProvider* pTheme,
const CFX_Matrix* pMatrix) {
CFWL_ThemeBackground params;
params.m_pWidget = this;
params.m_iPart = CFWL_Part::DownButton;
params.m_pGraphics = pGraphics;
params.m_dwStates = m_dwDnState + 1;
if (pMatrix)
params.m_matrix.Concat(*pMatrix);
params.m_rtPart = m_rtDnButton;
pTheme->DrawBackground(¶ms);
}
void CFWL_SpinButton::OnProcessMessage(CFWL_Message* pMessage) {
if (!pMessage)
return;
switch (pMessage->GetType()) {
case CFWL_Message::Type::SetFocus: {
OnFocusChanged(pMessage, true);
break;
}
case CFWL_Message::Type::KillFocus: {
OnFocusChanged(pMessage, false);
break;
}
case CFWL_Message::Type::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_Message::Type::Key: {
CFWL_MsgKey* pKey = static_cast<CFWL_MsgKey*>(pMessage);
if (pKey->m_dwCmd == FWL_KeyCommand::KeyDown)
OnKeyDown(pKey);
break;
}
default:
break;
}
CFWL_Widget::OnProcessMessage(pMessage);
}
void CFWL_SpinButton::OnDrawWidget(CFX_Graphics* pGraphics,
const CFX_Matrix* pMatrix) {
DrawWidget(pGraphics, pMatrix);
}
void CFWL_SpinButton::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 CFWL_SpinButton::OnLButtonDown(CFWL_MsgMouse* pMsg) {
m_bLButtonDwn = true;
SetGrab(true);
SetFocus(true);
bool bUpPress =
(m_rtUpButton.Contains(pMsg->m_fx, pMsg->m_fy) && IsUpButtonEnabled());
bool bDnPress =
(m_rtDnButton.Contains(pMsg->m_fx, pMsg->m_fy) && IsDownButtonEnabled());
if (!bUpPress && !bDnPress)
return;
if (bUpPress) {
m_iButtonIndex = 0;
m_dwUpState = CFWL_PartState_Pressed;
}
if (bDnPress) {
m_iButtonIndex = 1;
m_dwDnState = CFWL_PartState_Pressed;
}
CFWL_Event wmPosChanged(CFWL_Event::Type::Click, this);
DispatchEvent(&wmPosChanged);
Repaint(bUpPress ? &m_rtUpButton : &m_rtDnButton);
m_pTimerInfo = m_Timer.StartTimer(kElapseTime, true);
}
void CFWL_SpinButton::OnLButtonUp(CFWL_MsgMouse* pMsg) {
if (m_pProperties->m_dwStates & CFWL_PartState_Disabled)
return;
m_bLButtonDwn = false;
SetGrab(false);
SetFocus(false);
if (m_pTimerInfo) {
m_pTimerInfo->StopTimer();
m_pTimerInfo = nullptr;
}
bool bRepaint = false;
CFX_RectF rtInvalidate;
if (m_dwUpState == CFWL_PartState_Pressed && IsUpButtonEnabled()) {
m_dwUpState = CFWL_PartState_Normal;
bRepaint = true;
rtInvalidate = m_rtUpButton;
} else if (m_dwDnState == CFWL_PartState_Pressed && IsDownButtonEnabled()) {
m_dwDnState = CFWL_PartState_Normal;
bRepaint = true;
rtInvalidate = m_rtDnButton;
}
if (bRepaint)
Repaint(&rtInvalidate);
}
void CFWL_SpinButton::OnMouseMove(CFWL_MsgMouse* pMsg) {
if (m_bLButtonDwn)
return;
bool bRepaint = false;
CFX_RectF rtInvlidate;
rtInvlidate.Reset();
if (m_rtUpButton.Contains(pMsg->m_fx, pMsg->m_fy)) {
if (IsUpButtonEnabled()) {
if (m_dwUpState == CFWL_PartState_Hovered) {
m_dwUpState = CFWL_PartState_Hovered;
bRepaint = true;
rtInvlidate = m_rtUpButton;
}
if (m_dwDnState != CFWL_PartState_Normal && IsDownButtonEnabled()) {
m_dwDnState = CFWL_PartState_Normal;
if (bRepaint)
rtInvlidate.Union(m_rtDnButton);
else
rtInvlidate = m_rtDnButton;
bRepaint = true;
}
}
if (!IsDownButtonEnabled())
DisableButton();
} else if (m_rtDnButton.Contains(pMsg->m_fx, pMsg->m_fy)) {
if (IsDownButtonEnabled()) {
if (m_dwDnState != CFWL_PartState_Hovered) {
m_dwDnState = CFWL_PartState_Hovered;
bRepaint = true;
rtInvlidate = m_rtDnButton;
}
if (m_dwUpState != CFWL_PartState_Normal && IsUpButtonEnabled()) {
m_dwUpState = CFWL_PartState_Normal;
if (bRepaint)
rtInvlidate.Union(m_rtUpButton);
else
rtInvlidate = m_rtUpButton;
bRepaint = true;
}
}
} else if (m_dwUpState != CFWL_PartState_Normal ||
m_dwDnState != CFWL_PartState_Normal) {
if (m_dwUpState != CFWL_PartState_Normal) {
m_dwUpState = CFWL_PartState_Normal;
bRepaint = true;
rtInvlidate = m_rtUpButton;
}
if (m_dwDnState != CFWL_PartState_Normal) {
m_dwDnState = CFWL_PartState_Normal;
if (bRepaint)
rtInvlidate.Union(m_rtDnButton);
else
rtInvlidate = m_rtDnButton;
bRepaint = true;
}
}
if (bRepaint)
Repaint(&rtInvlidate);
}
void CFWL_SpinButton::OnMouseLeave(CFWL_MsgMouse* pMsg) {
if (!pMsg)
return;
if (m_dwUpState != CFWL_PartState_Normal && IsUpButtonEnabled())
m_dwUpState = CFWL_PartState_Normal;
if (m_dwDnState != CFWL_PartState_Normal && IsDownButtonEnabled())
m_dwDnState = CFWL_PartState_Normal;
Repaint(&m_rtClient);
}
void CFWL_SpinButton::OnKeyDown(CFWL_MsgKey* pMsg) {
bool bUp =
pMsg->m_dwKeyCode == FWL_VKEY_Up || pMsg->m_dwKeyCode == FWL_VKEY_Left;
bool bDown =
pMsg->m_dwKeyCode == FWL_VKEY_Down || pMsg->m_dwKeyCode == FWL_VKEY_Right;
if (!bUp && !bDown)
return;
bool bUpEnable = IsUpButtonEnabled();
bool bDownEnable = IsDownButtonEnabled();
if (!bUpEnable && !bDownEnable)
return;
CFWL_Event wmPosChanged(CFWL_Event::Type::Click, this);
DispatchEvent(&wmPosChanged);
Repaint(bUpEnable ? &m_rtUpButton : &m_rtDnButton);
}
CFWL_SpinButton::Timer::Timer(CFWL_SpinButton* pToolTip)
: CFWL_Timer(pToolTip) {}
void CFWL_SpinButton::Timer::Run(CFWL_TimerInfo* pTimerInfo) {
CFWL_SpinButton* pButton = static_cast<CFWL_SpinButton*>(m_pWidget);
if (!pButton->m_pTimerInfo)
return;
CFWL_Event wmPosChanged(CFWL_Event::Type::Click, pButton);
pButton->DispatchEvent(&wmPosChanged);
}
|