OLD | NEW |
| (Empty) |
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/fwl/basewidget/fwl_spinbuttonimp.h" | |
8 | |
9 #include "xfa/fwl/basewidget/ifwl_spinbutton.h" | |
10 #include "xfa/fwl/core/cfwl_message.h" | |
11 #include "xfa/fwl/core/cfwl_themebackground.h" | |
12 #include "xfa/fwl/core/cfwl_widgetimpproperties.h" | |
13 #include "xfa/fwl/core/fwl_noteimp.h" | |
14 #include "xfa/fwl/core/fwl_widgetimp.h" | |
15 #include "xfa/fwl/core/ifwl_themeprovider.h" | |
16 #include "xfa/fwl/core/ifwl_themeprovider.h" | |
17 #include "xfa/fwl/core/ifwl_timer.h" | |
18 | |
19 namespace { | |
20 | |
21 const int kMinWidth = 18; | |
22 const int kMinHeight = 32; | |
23 const int kElapseTime = 200; | |
24 | |
25 } // namespace | |
26 | |
27 // static | |
28 IFWL_SpinButton* IFWL_SpinButton::Create( | |
29 const CFWL_WidgetImpProperties& properties, | |
30 IFWL_Widget* pOuter) { | |
31 IFWL_SpinButton* pSpinButton = new IFWL_SpinButton; | |
32 CFWL_SpinButtonImp* pSpinButtonImpl = | |
33 new CFWL_SpinButtonImp(properties, nullptr); | |
34 pSpinButton->SetImpl(pSpinButtonImpl); | |
35 pSpinButtonImpl->SetInterface(pSpinButton); | |
36 return pSpinButton; | |
37 } | |
38 IFWL_SpinButton::IFWL_SpinButton() {} | |
39 FWL_Error IFWL_SpinButton::EnableButton(FX_BOOL bEnable, FX_BOOL bUp) { | |
40 return static_cast<CFWL_SpinButtonImp*>(GetImpl()) | |
41 ->EnableButton(bEnable, bUp); | |
42 } | |
43 FX_BOOL IFWL_SpinButton::IsButtonEnable(FX_BOOL bUp) { | |
44 return static_cast<CFWL_SpinButtonImp*>(GetImpl())->IsButtonEnable(bUp); | |
45 } | |
46 | |
47 CFWL_SpinButtonImp::CFWL_SpinButtonImp( | |
48 const CFWL_WidgetImpProperties& properties, | |
49 IFWL_Widget* pOuter) | |
50 : CFWL_WidgetImp(properties, pOuter), | |
51 m_dwUpState(CFWL_PartState_Normal), | |
52 m_dwDnState(CFWL_PartState_Normal), | |
53 m_iButtonIndex(0), | |
54 m_bLButtonDwn(FALSE), | |
55 m_pTimerInfo(nullptr) { | |
56 m_rtClient.Reset(); | |
57 m_rtUpButton.Reset(); | |
58 m_rtDnButton.Reset(); | |
59 m_pProperties->m_dwStyleExes |= FWL_STYLEEXE_SPB_Vert; | |
60 } | |
61 | |
62 CFWL_SpinButtonImp::~CFWL_SpinButtonImp() {} | |
63 | |
64 FWL_Error CFWL_SpinButtonImp::GetClassName(CFX_WideString& wsClass) const { | |
65 wsClass = FWL_CLASS_SpinButton; | |
66 return FWL_Error::Succeeded; | |
67 } | |
68 | |
69 FWL_Type CFWL_SpinButtonImp::GetClassID() const { | |
70 return FWL_Type::SpinButton; | |
71 } | |
72 | |
73 FWL_Error CFWL_SpinButtonImp::Initialize() { | |
74 if (CFWL_WidgetImp::Initialize() != FWL_Error::Succeeded) | |
75 return FWL_Error::Indefinite; | |
76 | |
77 m_pDelegate = new CFWL_SpinButtonImpDelegate(this); | |
78 return FWL_Error::Succeeded; | |
79 } | |
80 | |
81 FWL_Error CFWL_SpinButtonImp::Finalize() { | |
82 delete m_pDelegate; | |
83 m_pDelegate = nullptr; | |
84 return CFWL_WidgetImp::Finalize(); | |
85 } | |
86 FWL_Error CFWL_SpinButtonImp::GetWidgetRect(CFX_RectF& rect, | |
87 FX_BOOL bAutoSize) { | |
88 if (bAutoSize) { | |
89 rect.Set(0, 0, kMinWidth, kMinHeight); | |
90 CFWL_WidgetImp::GetWidgetRect(rect, TRUE); | |
91 } else { | |
92 rect = m_pProperties->m_rtWidget; | |
93 } | |
94 return FWL_Error::Succeeded; | |
95 } | |
96 FWL_Error CFWL_SpinButtonImp::Update() { | |
97 if (IsLocked()) { | |
98 return FWL_Error::Indefinite; | |
99 } | |
100 GetClientRect(m_rtClient); | |
101 if (m_pProperties->m_dwStyleExes & FWL_STYLEEXE_SPB_Vert) { | |
102 m_rtUpButton.Set(m_rtClient.top, m_rtClient.left, m_rtClient.width, | |
103 m_rtClient.height / 2); | |
104 m_rtDnButton.Set(m_rtClient.left, m_rtClient.top + m_rtClient.height / 2, | |
105 m_rtClient.width, m_rtClient.height / 2); | |
106 } else { | |
107 m_rtUpButton.Set(m_rtClient.left, m_rtClient.top, m_rtClient.width / 2, | |
108 m_rtClient.height); | |
109 m_rtDnButton.Set(m_rtClient.left + m_rtClient.width / 2, m_rtClient.top, | |
110 m_rtClient.width / 2, m_rtClient.height); | |
111 } | |
112 return FWL_Error::Succeeded; | |
113 } | |
114 FWL_WidgetHit CFWL_SpinButtonImp::HitTest(FX_FLOAT fx, FX_FLOAT fy) { | |
115 if (m_rtClient.Contains(fx, fy)) | |
116 return FWL_WidgetHit::Client; | |
117 if (HasBorder() && (m_rtClient.Contains(fx, fy))) | |
118 return FWL_WidgetHit::Border; | |
119 if (HasEdge()) { | |
120 CFX_RectF rtEdge; | |
121 GetEdgeRect(rtEdge); | |
122 if (rtEdge.Contains(fx, fy)) | |
123 return FWL_WidgetHit::Left; | |
124 } | |
125 if (m_rtUpButton.Contains(fx, fy)) | |
126 return FWL_WidgetHit::UpButton; | |
127 if (m_rtDnButton.Contains(fx, fy)) | |
128 return FWL_WidgetHit::DownButton; | |
129 return FWL_WidgetHit::Unknown; | |
130 } | |
131 FWL_Error CFWL_SpinButtonImp::DrawWidget(CFX_Graphics* pGraphics, | |
132 const CFX_Matrix* pMatrix) { | |
133 if (!pGraphics) | |
134 return FWL_Error::Indefinite; | |
135 CFX_RectF rtClip(m_rtClient); | |
136 if (pMatrix) { | |
137 pMatrix->TransformRect(rtClip); | |
138 } | |
139 IFWL_ThemeProvider* pTheme = GetAvailableTheme(); | |
140 if (HasBorder()) { | |
141 DrawBorder(pGraphics, CFWL_Part::Border, pTheme, pMatrix); | |
142 } | |
143 if (HasEdge()) { | |
144 DrawEdge(pGraphics, CFWL_Part::Edge, pTheme, pMatrix); | |
145 } | |
146 DrawUpButton(pGraphics, pTheme, pMatrix); | |
147 DrawDownButton(pGraphics, pTheme, pMatrix); | |
148 return FWL_Error::Succeeded; | |
149 } | |
150 | |
151 void CFWL_SpinButtonImp::Run(IFWL_TimerInfo* pTimerInfo) { | |
152 if (!m_pTimerInfo) | |
153 return; | |
154 | |
155 CFWL_EvtSpbClick wmPosChanged; | |
156 wmPosChanged.m_pSrcTarget = m_pInterface; | |
157 wmPosChanged.m_bUp = m_iButtonIndex == 0; | |
158 DispatchEvent(&wmPosChanged); | |
159 } | |
160 | |
161 FWL_Error CFWL_SpinButtonImp::EnableButton(FX_BOOL bEnable, FX_BOOL bUp) { | |
162 if (bUp) { | |
163 if (bEnable) { | |
164 m_dwUpState = CFWL_PartState_Normal; | |
165 } else { | |
166 m_dwUpState = CFWL_PartState_Disabled; | |
167 } | |
168 } else { | |
169 if (bEnable) { | |
170 m_dwDnState = CFWL_PartState_Normal; | |
171 } else { | |
172 m_dwDnState = CFWL_PartState_Disabled; | |
173 } | |
174 } | |
175 return FWL_Error::Succeeded; | |
176 } | |
177 FX_BOOL CFWL_SpinButtonImp::IsButtonEnable(FX_BOOL bUp) { | |
178 if (bUp) { | |
179 return (m_dwUpState != CFWL_PartState_Disabled); | |
180 } | |
181 return (m_dwDnState != CFWL_PartState_Disabled); | |
182 } | |
183 void CFWL_SpinButtonImp::DrawUpButton(CFX_Graphics* pGraphics, | |
184 IFWL_ThemeProvider* pTheme, | |
185 const CFX_Matrix* pMatrix) { | |
186 CFWL_ThemeBackground params; | |
187 params.m_pWidget = m_pInterface; | |
188 params.m_iPart = CFWL_Part::UpButton; | |
189 params.m_pGraphics = pGraphics; | |
190 params.m_dwStates = m_dwUpState + 1; | |
191 if (pMatrix) { | |
192 params.m_matrix.Concat(*pMatrix); | |
193 } | |
194 params.m_rtPart = m_rtUpButton; | |
195 pTheme->DrawBackground(¶ms); | |
196 } | |
197 void CFWL_SpinButtonImp::DrawDownButton(CFX_Graphics* pGraphics, | |
198 IFWL_ThemeProvider* pTheme, | |
199 const CFX_Matrix* pMatrix) { | |
200 CFWL_ThemeBackground params; | |
201 params.m_pWidget = m_pInterface; | |
202 params.m_iPart = CFWL_Part::DownButton; | |
203 params.m_pGraphics = pGraphics; | |
204 params.m_dwStates = m_dwDnState + 1; | |
205 if (pMatrix) { | |
206 params.m_matrix.Concat(*pMatrix); | |
207 } | |
208 params.m_rtPart = m_rtDnButton; | |
209 pTheme->DrawBackground(¶ms); | |
210 } | |
211 | |
212 CFWL_SpinButtonImpDelegate::CFWL_SpinButtonImpDelegate( | |
213 CFWL_SpinButtonImp* pOwner) | |
214 : m_pOwner(pOwner) {} | |
215 | |
216 void CFWL_SpinButtonImpDelegate::OnProcessMessage(CFWL_Message* pMessage) { | |
217 if (!pMessage) | |
218 return; | |
219 | |
220 CFWL_MessageType dwMsgCode = pMessage->GetClassID(); | |
221 switch (dwMsgCode) { | |
222 case CFWL_MessageType::SetFocus: { | |
223 OnFocusChanged(pMessage, TRUE); | |
224 break; | |
225 } | |
226 case CFWL_MessageType::KillFocus: { | |
227 OnFocusChanged(pMessage, FALSE); | |
228 break; | |
229 } | |
230 case CFWL_MessageType::Mouse: { | |
231 CFWL_MsgMouse* pMsg = static_cast<CFWL_MsgMouse*>(pMessage); | |
232 switch (pMsg->m_dwCmd) { | |
233 case FWL_MouseCommand::LeftButtonDown: { | |
234 OnLButtonDown(pMsg); | |
235 break; | |
236 } | |
237 case FWL_MouseCommand::LeftButtonUp: { | |
238 OnLButtonUp(pMsg); | |
239 break; | |
240 } | |
241 case FWL_MouseCommand::Move: { | |
242 OnMouseMove(pMsg); | |
243 break; | |
244 } | |
245 case FWL_MouseCommand::Leave: { | |
246 OnMouseLeave(pMsg); | |
247 break; | |
248 } | |
249 default: | |
250 break; | |
251 } | |
252 break; | |
253 } | |
254 case CFWL_MessageType::Key: { | |
255 CFWL_MsgKey* pKey = static_cast<CFWL_MsgKey*>(pMessage); | |
256 if (pKey->m_dwCmd == FWL_KeyCommand::KeyDown) | |
257 OnKeyDown(pKey); | |
258 break; | |
259 } | |
260 default: { | |
261 break; | |
262 } | |
263 } | |
264 CFWL_WidgetImpDelegate::OnProcessMessage(pMessage); | |
265 } | |
266 | |
267 void CFWL_SpinButtonImpDelegate::OnProcessEvent(CFWL_Event* pEvent) {} | |
268 | |
269 void CFWL_SpinButtonImpDelegate::OnDrawWidget(CFX_Graphics* pGraphics, | |
270 const CFX_Matrix* pMatrix) { | |
271 m_pOwner->DrawWidget(pGraphics, pMatrix); | |
272 } | |
273 | |
274 void CFWL_SpinButtonImpDelegate::OnFocusChanged(CFWL_Message* pMsg, | |
275 FX_BOOL bSet) { | |
276 if (bSet) { | |
277 m_pOwner->m_pProperties->m_dwStates |= (FWL_WGTSTATE_Focused); | |
278 } else { | |
279 m_pOwner->m_pProperties->m_dwStates &= ~(FWL_WGTSTATE_Focused); | |
280 } | |
281 m_pOwner->Repaint(&m_pOwner->m_rtClient); | |
282 } | |
283 void CFWL_SpinButtonImpDelegate::OnLButtonDown(CFWL_MsgMouse* pMsg) { | |
284 m_pOwner->m_bLButtonDwn = TRUE; | |
285 m_pOwner->SetGrab(TRUE); | |
286 m_pOwner->SetFocus(TRUE); | |
287 if (!m_pOwner->m_pProperties->m_pDataProvider) | |
288 return; | |
289 FX_BOOL bUpPress = (m_pOwner->m_rtUpButton.Contains(pMsg->m_fx, pMsg->m_fy) && | |
290 m_pOwner->IsButtonEnable(TRUE)); | |
291 FX_BOOL bDnPress = (m_pOwner->m_rtDnButton.Contains(pMsg->m_fx, pMsg->m_fy) && | |
292 m_pOwner->IsButtonEnable(FALSE)); | |
293 if (!bUpPress && !bDnPress) { | |
294 return; | |
295 } | |
296 if (bUpPress) { | |
297 m_pOwner->m_iButtonIndex = 0; | |
298 m_pOwner->m_dwUpState = CFWL_PartState_Pressed; | |
299 } | |
300 if (bDnPress) { | |
301 m_pOwner->m_iButtonIndex = 1; | |
302 m_pOwner->m_dwDnState = CFWL_PartState_Pressed; | |
303 } | |
304 CFWL_EvtSpbClick wmPosChanged; | |
305 wmPosChanged.m_pSrcTarget = m_pOwner->m_pInterface; | |
306 wmPosChanged.m_bUp = bUpPress; | |
307 m_pOwner->DispatchEvent(&wmPosChanged); | |
308 m_pOwner->Repaint(bUpPress ? &m_pOwner->m_rtUpButton | |
309 : &m_pOwner->m_rtDnButton); | |
310 m_pOwner->m_pTimerInfo = m_pOwner->StartTimer(kElapseTime, true); | |
311 } | |
312 | |
313 void CFWL_SpinButtonImpDelegate::OnLButtonUp(CFWL_MsgMouse* pMsg) { | |
314 if (m_pOwner->m_pProperties->m_dwStates & CFWL_PartState_Disabled) { | |
315 return; | |
316 } | |
317 m_pOwner->m_bLButtonDwn = FALSE; | |
318 m_pOwner->SetGrab(FALSE); | |
319 m_pOwner->SetFocus(FALSE); | |
320 if (m_pOwner->m_pTimerInfo) { | |
321 m_pOwner->m_pTimerInfo->StopTimer(); | |
322 m_pOwner->m_pTimerInfo = nullptr; | |
323 } | |
324 FX_BOOL bRepaint = FALSE; | |
325 CFX_RectF rtInvalidate; | |
326 if (m_pOwner->m_dwUpState == CFWL_PartState_Pressed && | |
327 m_pOwner->IsButtonEnable(TRUE)) { | |
328 m_pOwner->m_dwUpState = CFWL_PartState_Normal; | |
329 bRepaint = TRUE; | |
330 rtInvalidate = m_pOwner->m_rtUpButton; | |
331 } else if (m_pOwner->m_dwDnState == CFWL_PartState_Pressed && | |
332 m_pOwner->IsButtonEnable(FALSE)) { | |
333 m_pOwner->m_dwDnState = CFWL_PartState_Normal; | |
334 bRepaint = TRUE; | |
335 rtInvalidate = m_pOwner->m_rtDnButton; | |
336 } | |
337 if (bRepaint) { | |
338 m_pOwner->Repaint(&rtInvalidate); | |
339 } | |
340 } | |
341 void CFWL_SpinButtonImpDelegate::OnMouseMove(CFWL_MsgMouse* pMsg) { | |
342 if (!m_pOwner->m_pProperties->m_pDataProvider) | |
343 return; | |
344 if (m_pOwner->m_bLButtonDwn) { | |
345 return; | |
346 } | |
347 FX_BOOL bRepaint = FALSE; | |
348 CFX_RectF rtInvlidate; | |
349 rtInvlidate.Reset(); | |
350 if (m_pOwner->m_rtUpButton.Contains(pMsg->m_fx, pMsg->m_fy)) { | |
351 if (m_pOwner->IsButtonEnable(TRUE)) { | |
352 if (m_pOwner->m_dwUpState == CFWL_PartState_Hovered) { | |
353 m_pOwner->m_dwUpState = CFWL_PartState_Hovered; | |
354 bRepaint = TRUE; | |
355 rtInvlidate = m_pOwner->m_rtUpButton; | |
356 } | |
357 if (m_pOwner->m_dwDnState != CFWL_PartState_Normal && | |
358 m_pOwner->IsButtonEnable(FALSE)) { | |
359 m_pOwner->m_dwDnState = CFWL_PartState_Normal; | |
360 if (bRepaint) { | |
361 rtInvlidate.Union(m_pOwner->m_rtDnButton); | |
362 } else { | |
363 rtInvlidate = m_pOwner->m_rtDnButton; | |
364 } | |
365 bRepaint = TRUE; | |
366 } | |
367 } | |
368 if (!m_pOwner->IsButtonEnable(FALSE)) { | |
369 m_pOwner->EnableButton(FALSE, FALSE); | |
370 } | |
371 } else if (m_pOwner->m_rtDnButton.Contains(pMsg->m_fx, pMsg->m_fy)) { | |
372 if (m_pOwner->IsButtonEnable(FALSE)) { | |
373 if (m_pOwner->m_dwDnState != CFWL_PartState_Hovered) { | |
374 m_pOwner->m_dwDnState = CFWL_PartState_Hovered; | |
375 bRepaint = TRUE; | |
376 rtInvlidate = m_pOwner->m_rtDnButton; | |
377 } | |
378 if (m_pOwner->m_dwUpState != CFWL_PartState_Normal && | |
379 m_pOwner->IsButtonEnable(TRUE)) { | |
380 m_pOwner->m_dwUpState = CFWL_PartState_Normal; | |
381 if (bRepaint) { | |
382 rtInvlidate.Union(m_pOwner->m_rtUpButton); | |
383 } else { | |
384 rtInvlidate = m_pOwner->m_rtUpButton; | |
385 } | |
386 bRepaint = TRUE; | |
387 } | |
388 } | |
389 } else if (m_pOwner->m_dwUpState != CFWL_PartState_Normal || | |
390 m_pOwner->m_dwDnState != CFWL_PartState_Normal) { | |
391 if (m_pOwner->m_dwUpState != CFWL_PartState_Normal) { | |
392 m_pOwner->m_dwUpState = CFWL_PartState_Normal; | |
393 bRepaint = TRUE; | |
394 rtInvlidate = m_pOwner->m_rtUpButton; | |
395 } | |
396 if (m_pOwner->m_dwDnState != CFWL_PartState_Normal) { | |
397 m_pOwner->m_dwDnState = CFWL_PartState_Normal; | |
398 if (bRepaint) { | |
399 rtInvlidate.Union(m_pOwner->m_rtDnButton); | |
400 } else { | |
401 rtInvlidate = m_pOwner->m_rtDnButton; | |
402 } | |
403 bRepaint = TRUE; | |
404 } | |
405 } | |
406 if (bRepaint) { | |
407 m_pOwner->Repaint(&rtInvlidate); | |
408 } | |
409 } | |
410 void CFWL_SpinButtonImpDelegate::OnMouseLeave(CFWL_MsgMouse* pMsg) { | |
411 if (!pMsg) | |
412 return; | |
413 if (m_pOwner->m_dwUpState != CFWL_PartState_Normal && | |
414 m_pOwner->IsButtonEnable(TRUE)) { | |
415 m_pOwner->m_dwUpState = CFWL_PartState_Normal; | |
416 } | |
417 if (m_pOwner->m_dwDnState != CFWL_PartState_Normal && | |
418 m_pOwner->IsButtonEnable(FALSE)) { | |
419 m_pOwner->m_dwDnState = CFWL_PartState_Normal; | |
420 } | |
421 m_pOwner->Repaint(&m_pOwner->m_rtClient); | |
422 } | |
423 void CFWL_SpinButtonImpDelegate::OnKeyDown(CFWL_MsgKey* pMsg) { | |
424 if (!m_pOwner->m_pProperties->m_pDataProvider) | |
425 return; | |
426 FX_BOOL bUp = | |
427 pMsg->m_dwKeyCode == FWL_VKEY_Up || pMsg->m_dwKeyCode == FWL_VKEY_Left; | |
428 FX_BOOL bDown = | |
429 pMsg->m_dwKeyCode == FWL_VKEY_Down || pMsg->m_dwKeyCode == FWL_VKEY_Right; | |
430 if (!bUp && !bDown) { | |
431 return; | |
432 } | |
433 FX_BOOL bUpEnable = m_pOwner->IsButtonEnable(TRUE); | |
434 FX_BOOL bDownEnable = m_pOwner->IsButtonEnable(FALSE); | |
435 if (!bUpEnable && !bDownEnable) { | |
436 return; | |
437 } | |
438 CFWL_EvtSpbClick wmPosChanged; | |
439 wmPosChanged.m_pSrcTarget = m_pOwner->m_pInterface; | |
440 wmPosChanged.m_bUp = bUpEnable; | |
441 m_pOwner->DispatchEvent(&wmPosChanged); | |
442 m_pOwner->Repaint(bUpEnable ? &m_pOwner->m_rtUpButton | |
443 : &m_pOwner->m_rtDnButton); | |
444 } | |
OLD | NEW |