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/core/cfwl_checkbox.h" | |
8 | |
9 #include <algorithm> | |
10 #include <memory> | |
11 #include <utility> | |
12 | |
13 #include "third_party/base/ptr_util.h" | |
14 #include "xfa/fde/tto/fde_textout.h" | |
15 #include "xfa/fwl/core/cfwl_app.h" | |
16 #include "xfa/fwl/core/cfwl_event.h" | |
17 #include "xfa/fwl/core/cfwl_msgkey.h" | |
18 #include "xfa/fwl/core/cfwl_msgmouse.h" | |
19 #include "xfa/fwl/core/cfwl_notedriver.h" | |
20 #include "xfa/fwl/core/cfwl_themebackground.h" | |
21 #include "xfa/fwl/core/cfwl_themetext.h" | |
22 #include "xfa/fwl/core/cfwl_widgetmgr.h" | |
23 #include "xfa/fwl/core/ifwl_themeprovider.h" | |
24 | |
25 namespace { | |
26 | |
27 const int kCaptionMargin = 5; | |
28 | |
29 } // namespace | |
30 | |
31 CFWL_CheckBox::CFWL_CheckBox(const CFWL_App* app) | |
32 : CFWL_Widget(app, pdfium::MakeUnique<CFWL_WidgetProperties>(), nullptr), | |
33 m_dwTTOStyles(FDE_TTOSTYLE_SingleLine), | |
34 m_iTTOAlign(FDE_TTOALIGNMENT_Center), | |
35 m_bBtnDown(false), | |
36 m_fBoxHeight(16.0f) { | |
37 m_rtClient.Reset(); | |
38 m_rtBox.Reset(); | |
39 m_rtCaption.Reset(); | |
40 m_rtFocus.Reset(); | |
41 } | |
42 | |
43 CFWL_CheckBox::~CFWL_CheckBox() {} | |
44 | |
45 FWL_Type CFWL_CheckBox::GetClassID() const { | |
46 return FWL_Type::CheckBox; | |
47 } | |
48 | |
49 void CFWL_CheckBox::SetBoxSize(FX_FLOAT fHeight) { | |
50 m_fBoxHeight = fHeight; | |
51 } | |
52 | |
53 void CFWL_CheckBox::Update() { | |
54 if (IsLocked()) | |
55 return; | |
56 if (!m_pProperties->m_pThemeProvider) | |
57 m_pProperties->m_pThemeProvider = GetAvailableTheme(); | |
58 | |
59 UpdateTextOutStyles(); | |
60 Layout(); | |
61 } | |
62 | |
63 void CFWL_CheckBox::DrawWidget(CFX_Graphics* pGraphics, | |
64 const CFX_Matrix* pMatrix) { | |
65 if (!pGraphics) | |
66 return; | |
67 if (!m_pProperties->m_pThemeProvider) | |
68 return; | |
69 | |
70 IFWL_ThemeProvider* pTheme = m_pProperties->m_pThemeProvider; | |
71 if (HasBorder()) { | |
72 DrawBorder(pGraphics, CFWL_Part::Border, m_pProperties->m_pThemeProvider, | |
73 pMatrix); | |
74 } | |
75 if (HasEdge()) | |
76 DrawEdge(pGraphics, CFWL_Part::Edge, pTheme, pMatrix); | |
77 | |
78 int32_t dwStates = GetPartStates(); | |
79 | |
80 CFWL_ThemeBackground param; | |
81 param.m_pWidget = this; | |
82 param.m_iPart = CFWL_Part::Background; | |
83 param.m_dwStates = dwStates; | |
84 param.m_pGraphics = pGraphics; | |
85 if (pMatrix) | |
86 param.m_matrix.Concat(*pMatrix); | |
87 param.m_rtPart = m_rtClient; | |
88 if (m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) | |
89 param.m_pData = &m_rtFocus; | |
90 pTheme->DrawBackground(¶m); | |
91 | |
92 param.m_iPart = CFWL_Part::CheckBox; | |
93 param.m_rtPart = m_rtBox; | |
94 pTheme->DrawBackground(¶m); | |
95 | |
96 CFWL_ThemeText textParam; | |
97 textParam.m_pWidget = this; | |
98 textParam.m_iPart = CFWL_Part::Caption; | |
99 textParam.m_dwStates = dwStates; | |
100 textParam.m_pGraphics = pGraphics; | |
101 if (pMatrix) | |
102 textParam.m_matrix.Concat(*pMatrix); | |
103 textParam.m_rtPart = m_rtCaption; | |
104 textParam.m_wsText = L"Check box"; | |
105 textParam.m_dwTTOStyles = m_dwTTOStyles; | |
106 textParam.m_iTTOAlign = m_iTTOAlign; | |
107 pTheme->DrawText(&textParam); | |
108 } | |
109 | |
110 void CFWL_CheckBox::SetCheckState(int32_t iCheck) { | |
111 m_pProperties->m_dwStates &= ~FWL_STATE_CKB_CheckMask; | |
112 switch (iCheck) { | |
113 case 1: | |
114 m_pProperties->m_dwStates |= FWL_STATE_CKB_Checked; | |
115 break; | |
116 case 2: | |
117 if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_CKB_3State) | |
118 m_pProperties->m_dwStates |= FWL_STATE_CKB_Neutral; | |
119 break; | |
120 default: | |
121 break; | |
122 } | |
123 Repaint(&m_rtClient); | |
124 } | |
125 | |
126 void CFWL_CheckBox::Layout() { | |
127 m_pProperties->m_rtWidget.width = | |
128 FXSYS_round(m_pProperties->m_rtWidget.width); | |
129 m_pProperties->m_rtWidget.height = | |
130 FXSYS_round(m_pProperties->m_rtWidget.height); | |
131 GetClientRect(m_rtClient); | |
132 | |
133 FX_FLOAT fBoxTop = m_rtClient.top; | |
134 FX_FLOAT fClientBottom = m_rtClient.bottom(); | |
135 | |
136 switch (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_CKB_VLayoutMask) { | |
137 case FWL_STYLEEXT_CKB_Top: | |
138 break; | |
139 case FWL_STYLEEXT_CKB_Bottom: { | |
140 fBoxTop = fClientBottom - m_fBoxHeight; | |
141 break; | |
142 } | |
143 case FWL_STYLEEXT_CKB_VCenter: | |
144 default: { | |
145 fBoxTop = m_rtClient.top + (m_rtClient.height - m_fBoxHeight) / 2; | |
146 fBoxTop = FXSYS_floor(fBoxTop); | |
147 break; | |
148 } | |
149 } | |
150 | |
151 FX_FLOAT fBoxLeft = m_rtClient.left; | |
152 FX_FLOAT fTextLeft = 0.0; | |
153 FX_FLOAT fTextRight = 0.0; | |
154 FX_FLOAT fClientRight = m_rtClient.right(); | |
155 if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_CKB_LeftText) { | |
156 fBoxLeft = fClientRight - m_fBoxHeight; | |
157 fTextLeft = m_rtClient.left; | |
158 fTextRight = fBoxLeft; | |
159 } else { | |
160 fTextLeft = fBoxLeft + m_fBoxHeight; | |
161 fTextRight = fClientRight; | |
162 } | |
163 m_rtBox.Set(fBoxLeft, fBoxTop, m_fBoxHeight, m_fBoxHeight); | |
164 m_rtCaption.Set(fTextLeft, m_rtClient.top, fTextRight - fTextLeft, | |
165 m_rtClient.height); | |
166 m_rtCaption.Inflate(-kCaptionMargin, -kCaptionMargin); | |
167 | |
168 CFX_RectF rtFocus; | |
169 rtFocus.Set(m_rtCaption.left, m_rtCaption.top, m_rtCaption.width, | |
170 m_rtCaption.height); | |
171 | |
172 CalcTextRect(L"Check box", m_pProperties->m_pThemeProvider, m_dwTTOStyles, | |
173 m_iTTOAlign, rtFocus); | |
174 if ((m_pProperties->m_dwStyleExes & FWL_STYLEEXT_CKB_MultiLine) == 0) { | |
175 FX_FLOAT fWidth = std::max(m_rtCaption.width, rtFocus.width); | |
176 FX_FLOAT fHeight = std::min(m_rtCaption.height, rtFocus.height); | |
177 FX_FLOAT fLeft = m_rtCaption.left; | |
178 FX_FLOAT fTop = m_rtCaption.top; | |
179 if ((m_pProperties->m_dwStyleExes & FWL_STYLEEXT_CKB_HLayoutMask) == | |
180 FWL_STYLEEXT_CKB_Center) { | |
181 fLeft = m_rtCaption.left + (m_rtCaption.width - fWidth) / 2; | |
182 } else if ((m_pProperties->m_dwStyleExes & FWL_STYLEEXT_CKB_HLayoutMask) == | |
183 FWL_STYLEEXT_CKB_Right) { | |
184 fLeft = m_rtCaption.right() - fWidth; | |
185 } | |
186 if ((m_pProperties->m_dwStyleExes & FWL_STYLEEXT_CKB_VLayoutMask) == | |
187 FWL_STYLEEXT_CKB_VCenter) { | |
188 fTop = m_rtCaption.top + (m_rtCaption.height - fHeight) / 2; | |
189 } else if ((m_pProperties->m_dwStyleExes & FWL_STYLEEXT_CKB_VLayoutMask) == | |
190 FWL_STYLEEXT_CKB_Bottom) { | |
191 fTop = m_rtCaption.bottom() - fHeight; | |
192 } | |
193 m_rtFocus.Set(fLeft, fTop, fWidth, fHeight); | |
194 } else { | |
195 m_rtFocus.Set(rtFocus.left, rtFocus.top, rtFocus.width, rtFocus.height); | |
196 } | |
197 m_rtFocus.Inflate(1, 1); | |
198 } | |
199 | |
200 uint32_t CFWL_CheckBox::GetPartStates() const { | |
201 int32_t dwStates = CFWL_PartState_Normal; | |
202 if ((m_pProperties->m_dwStates & FWL_STATE_CKB_CheckMask) == | |
203 FWL_STATE_CKB_Neutral) { | |
204 dwStates = CFWL_PartState_Neutral; | |
205 } else if ((m_pProperties->m_dwStates & FWL_STATE_CKB_CheckMask) == | |
206 FWL_STATE_CKB_Checked) { | |
207 dwStates = CFWL_PartState_Checked; | |
208 } | |
209 if (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled) | |
210 dwStates |= CFWL_PartState_Disabled; | |
211 else if (m_pProperties->m_dwStates & FWL_STATE_CKB_Hovered) | |
212 dwStates |= CFWL_PartState_Hovered; | |
213 else if (m_pProperties->m_dwStates & FWL_STATE_CKB_Pressed) | |
214 dwStates |= CFWL_PartState_Pressed; | |
215 else | |
216 dwStates |= CFWL_PartState_Normal; | |
217 if (m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) | |
218 dwStates |= CFWL_PartState_Focused; | |
219 return dwStates; | |
220 } | |
221 | |
222 void CFWL_CheckBox::UpdateTextOutStyles() { | |
223 switch (m_pProperties->m_dwStyleExes & | |
224 (FWL_STYLEEXT_CKB_HLayoutMask | FWL_STYLEEXT_CKB_VLayoutMask)) { | |
225 case FWL_STYLEEXT_CKB_Left | FWL_STYLEEXT_CKB_Top: { | |
226 m_iTTOAlign = FDE_TTOALIGNMENT_TopLeft; | |
227 break; | |
228 } | |
229 case FWL_STYLEEXT_CKB_Center | FWL_STYLEEXT_CKB_Top: { | |
230 m_iTTOAlign = FDE_TTOALIGNMENT_TopCenter; | |
231 break; | |
232 } | |
233 case FWL_STYLEEXT_CKB_Right | FWL_STYLEEXT_CKB_Top: { | |
234 m_iTTOAlign = FDE_TTOALIGNMENT_TopRight; | |
235 break; | |
236 } | |
237 case FWL_STYLEEXT_CKB_Left | FWL_STYLEEXT_CKB_VCenter: { | |
238 m_iTTOAlign = FDE_TTOALIGNMENT_CenterLeft; | |
239 break; | |
240 } | |
241 case FWL_STYLEEXT_CKB_Right | FWL_STYLEEXT_CKB_VCenter: { | |
242 m_iTTOAlign = FDE_TTOALIGNMENT_CenterRight; | |
243 break; | |
244 } | |
245 case FWL_STYLEEXT_CKB_Left | FWL_STYLEEXT_CKB_Bottom: { | |
246 m_iTTOAlign = FDE_TTOALIGNMENT_BottomLeft; | |
247 break; | |
248 } | |
249 case FWL_STYLEEXT_CKB_Center | FWL_STYLEEXT_CKB_Bottom: { | |
250 m_iTTOAlign = FDE_TTOALIGNMENT_BottomCenter; | |
251 break; | |
252 } | |
253 case FWL_STYLEEXT_CKB_Right | FWL_STYLEEXT_CKB_Bottom: { | |
254 m_iTTOAlign = FDE_TTOALIGNMENT_BottomRight; | |
255 break; | |
256 } | |
257 case FWL_STYLEEXT_CKB_Center | FWL_STYLEEXT_CKB_VCenter: | |
258 default: { | |
259 m_iTTOAlign = FDE_TTOALIGNMENT_Center; | |
260 break; | |
261 } | |
262 } | |
263 m_dwTTOStyles = 0; | |
264 if (m_pProperties->m_dwStyleExes & FWL_WGTSTYLE_RTLReading) | |
265 m_dwTTOStyles |= FDE_TTOSTYLE_RTL; | |
266 if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_CKB_MultiLine) | |
267 m_dwTTOStyles |= FDE_TTOSTYLE_LineWrap; | |
268 else | |
269 m_dwTTOStyles |= FDE_TTOSTYLE_SingleLine; | |
270 } | |
271 | |
272 void CFWL_CheckBox::NextStates() { | |
273 uint32_t dwFirststate = m_pProperties->m_dwStates; | |
274 if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_CKB_RadioButton) { | |
275 if ((m_pProperties->m_dwStates & FWL_STATE_CKB_CheckMask) == | |
276 FWL_STATE_CKB_Unchecked) { | |
277 CFWL_WidgetMgr* pWidgetMgr = GetOwnerApp()->GetWidgetMgr(); | |
278 if (!pWidgetMgr->IsFormDisabled()) { | |
279 CFX_ArrayTemplate<CFWL_Widget*> radioarr; | |
280 pWidgetMgr->GetSameGroupRadioButton(this, radioarr); | |
281 CFWL_CheckBox* pCheckBox = nullptr; | |
282 int32_t iCount = radioarr.GetSize(); | |
283 for (int32_t i = 0; i < iCount; i++) { | |
284 pCheckBox = static_cast<CFWL_CheckBox*>(radioarr[i]); | |
285 if (pCheckBox != this && | |
286 pCheckBox->GetStates() & FWL_STATE_CKB_Checked) { | |
287 pCheckBox->SetCheckState(0); | |
288 CFX_RectF rt = pCheckBox->GetWidgetRect(); | |
289 rt.left = rt.top = 0; | |
290 m_pWidgetMgr->RepaintWidget(pCheckBox, &rt); | |
291 break; | |
292 } | |
293 } | |
294 } | |
295 m_pProperties->m_dwStates |= FWL_STATE_CKB_Checked; | |
296 } | |
297 } else { | |
298 if ((m_pProperties->m_dwStates & FWL_STATE_CKB_CheckMask) == | |
299 FWL_STATE_CKB_Neutral) { | |
300 m_pProperties->m_dwStates &= ~FWL_STATE_CKB_CheckMask; | |
301 if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_CKB_3State) | |
302 m_pProperties->m_dwStates |= FWL_STATE_CKB_Checked; | |
303 } else if ((m_pProperties->m_dwStates & FWL_STATE_CKB_CheckMask) == | |
304 FWL_STATE_CKB_Checked) { | |
305 m_pProperties->m_dwStates &= ~FWL_STATE_CKB_CheckMask; | |
306 } else { | |
307 if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_CKB_3State) | |
308 m_pProperties->m_dwStates |= FWL_STATE_CKB_Neutral; | |
309 else | |
310 m_pProperties->m_dwStates |= FWL_STATE_CKB_Checked; | |
311 } | |
312 } | |
313 | |
314 Repaint(&m_rtClient); | |
315 if (dwFirststate == m_pProperties->m_dwStates) | |
316 return; | |
317 | |
318 CFWL_Event wmCheckBoxState(CFWL_Event::Type::CheckStateChanged, this); | |
319 DispatchEvent(&wmCheckBoxState); | |
320 } | |
321 | |
322 void CFWL_CheckBox::OnProcessMessage(CFWL_Message* pMessage) { | |
323 if (!pMessage) | |
324 return; | |
325 | |
326 switch (pMessage->GetType()) { | |
327 case CFWL_Message::Type::SetFocus: | |
328 OnFocusChanged(true); | |
329 break; | |
330 case CFWL_Message::Type::KillFocus: | |
331 OnFocusChanged(false); | |
332 break; | |
333 case CFWL_Message::Type::Mouse: { | |
334 CFWL_MsgMouse* pMsg = static_cast<CFWL_MsgMouse*>(pMessage); | |
335 switch (pMsg->m_dwCmd) { | |
336 case FWL_MouseCommand::LeftButtonDown: | |
337 OnLButtonDown(); | |
338 break; | |
339 case FWL_MouseCommand::LeftButtonUp: | |
340 OnLButtonUp(pMsg); | |
341 break; | |
342 case FWL_MouseCommand::Move: | |
343 OnMouseMove(pMsg); | |
344 break; | |
345 case FWL_MouseCommand::Leave: | |
346 OnMouseLeave(); | |
347 break; | |
348 default: | |
349 break; | |
350 } | |
351 break; | |
352 } | |
353 case CFWL_Message::Type::Key: { | |
354 CFWL_MsgKey* pKey = static_cast<CFWL_MsgKey*>(pMessage); | |
355 if (pKey->m_dwCmd == FWL_KeyCommand::KeyDown) | |
356 OnKeyDown(pKey); | |
357 break; | |
358 } | |
359 default: | |
360 break; | |
361 } | |
362 | |
363 CFWL_Widget::OnProcessMessage(pMessage); | |
364 } | |
365 | |
366 void CFWL_CheckBox::OnDrawWidget(CFX_Graphics* pGraphics, | |
367 const CFX_Matrix* pMatrix) { | |
368 DrawWidget(pGraphics, pMatrix); | |
369 } | |
370 | |
371 void CFWL_CheckBox::OnFocusChanged(bool bSet) { | |
372 if (bSet) | |
373 m_pProperties->m_dwStates |= FWL_WGTSTATE_Focused; | |
374 else | |
375 m_pProperties->m_dwStates &= ~FWL_WGTSTATE_Focused; | |
376 | |
377 Repaint(&m_rtClient); | |
378 } | |
379 | |
380 void CFWL_CheckBox::OnLButtonDown() { | |
381 if (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled) | |
382 return; | |
383 if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) == 0) | |
384 SetFocus(true); | |
385 | |
386 m_bBtnDown = true; | |
387 m_pProperties->m_dwStates &= ~FWL_STATE_CKB_Hovered; | |
388 m_pProperties->m_dwStates |= FWL_STATE_CKB_Pressed; | |
389 Repaint(&m_rtClient); | |
390 } | |
391 | |
392 void CFWL_CheckBox::OnLButtonUp(CFWL_MsgMouse* pMsg) { | |
393 if (!m_bBtnDown) | |
394 return; | |
395 | |
396 m_bBtnDown = false; | |
397 if (!m_rtClient.Contains(pMsg->m_fx, pMsg->m_fy)) | |
398 return; | |
399 | |
400 m_pProperties->m_dwStates |= FWL_STATE_CKB_Hovered; | |
401 m_pProperties->m_dwStates &= ~FWL_STATE_CKB_Pressed; | |
402 NextStates(); | |
403 } | |
404 | |
405 void CFWL_CheckBox::OnMouseMove(CFWL_MsgMouse* pMsg) { | |
406 if (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled) | |
407 return; | |
408 | |
409 bool bRepaint = false; | |
410 if (m_bBtnDown) { | |
411 if (m_rtClient.Contains(pMsg->m_fx, pMsg->m_fy)) { | |
412 if ((m_pProperties->m_dwStates & FWL_STATE_CKB_Pressed) == 0) { | |
413 bRepaint = true; | |
414 m_pProperties->m_dwStates |= FWL_STATE_CKB_Pressed; | |
415 } | |
416 if ((m_pProperties->m_dwStates & FWL_STATE_CKB_Hovered)) { | |
417 bRepaint = true; | |
418 m_pProperties->m_dwStates &= ~FWL_STATE_CKB_Hovered; | |
419 } | |
420 } else { | |
421 if (m_pProperties->m_dwStates & FWL_STATE_CKB_Pressed) { | |
422 bRepaint = true; | |
423 m_pProperties->m_dwStates &= ~FWL_STATE_CKB_Pressed; | |
424 } | |
425 if ((m_pProperties->m_dwStates & FWL_STATE_CKB_Hovered) == 0) { | |
426 bRepaint = true; | |
427 m_pProperties->m_dwStates |= FWL_STATE_CKB_Hovered; | |
428 } | |
429 } | |
430 } else { | |
431 if (m_rtClient.Contains(pMsg->m_fx, pMsg->m_fy)) { | |
432 if ((m_pProperties->m_dwStates & FWL_STATE_CKB_Hovered) == 0) { | |
433 bRepaint = true; | |
434 m_pProperties->m_dwStates |= FWL_STATE_CKB_Hovered; | |
435 } | |
436 } | |
437 } | |
438 if (bRepaint) | |
439 Repaint(&m_rtBox); | |
440 } | |
441 | |
442 void CFWL_CheckBox::OnMouseLeave() { | |
443 if (m_bBtnDown) | |
444 m_pProperties->m_dwStates |= FWL_STATE_CKB_Hovered; | |
445 else | |
446 m_pProperties->m_dwStates &= ~FWL_STATE_CKB_Hovered; | |
447 | |
448 Repaint(&m_rtBox); | |
449 } | |
450 | |
451 void CFWL_CheckBox::OnKeyDown(CFWL_MsgKey* pMsg) { | |
452 if (pMsg->m_dwKeyCode == FWL_VKEY_Tab) | |
453 return; | |
454 if (pMsg->m_dwKeyCode == FWL_VKEY_Return || | |
455 pMsg->m_dwKeyCode == FWL_VKEY_Space) { | |
456 NextStates(); | |
457 } | |
458 } | |
OLD | NEW |