Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfwl_checkbox.cpp
Go to the documentation of this file.
1// Copyright 2014 The PDFium Authors
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/cfwl_checkbox.h"
8
9#include <algorithm>
10#include <utility>
11
12#include "xfa/fde/cfde_textout.h"
13#include "xfa/fwl/cfwl_app.h"
14#include "xfa/fwl/cfwl_event.h"
15#include "xfa/fwl/cfwl_messagekey.h"
16#include "xfa/fwl/cfwl_messagemouse.h"
17#include "xfa/fwl/cfwl_notedriver.h"
18#include "xfa/fwl/cfwl_themebackground.h"
19#include "xfa/fwl/cfwl_themetext.h"
20#include "xfa/fwl/cfwl_widgetmgr.h"
21#include "xfa/fwl/fwl_widgetdef.h"
22#include "xfa/fwl/ifwl_themeprovider.h"
23
24namespace {
25
26const int kCaptionMargin = 5;
27
28} // namespace
29
30CFWL_CheckBox::CFWL_CheckBox(CFWL_App* app)
31 : CFWL_Widget(app, Properties(), nullptr) {
32 m_TTOStyles.single_line_ = true;
33}
34
35CFWL_CheckBox::~CFWL_CheckBox() = default;
36
37FWL_Type CFWL_CheckBox::GetClassID() const {
38 return FWL_Type::CheckBox;
39}
40
41void CFWL_CheckBox::SetBoxSize(float fHeight) {
42 m_fBoxHeight = fHeight;
43}
44
45void CFWL_CheckBox::Update() {
46 if (IsLocked())
47 return;
48
49 UpdateTextOutStyles();
50 Layout();
51}
52
53void CFWL_CheckBox::DrawWidget(CFGAS_GEGraphics* pGraphics,
54 const CFX_Matrix& matrix) {
55 if (!pGraphics)
56 return;
57
58 if (HasBorder())
59 DrawBorder(pGraphics, CFWL_ThemePart::Part::kBorder, matrix);
60
61 Mask<CFWL_PartState> dwStates = GetPartStates();
63 CFWL_ThemeBackground param(CFWL_ThemePart::Part::kBackground, this,
64 pGraphics);
65 param.m_dwStates = dwStates;
66 param.m_matrix = matrix;
67 param.m_PartRect = m_ClientRect;
69 param.m_pRtData = &m_FocusRect;
70 pTheme->DrawBackground(param);
71
72 CFWL_ThemeBackground checkParam(CFWL_ThemePart::Part::kCheckBox, this,
73 pGraphics);
74 checkParam.m_dwStates = dwStates;
75 checkParam.m_matrix = matrix;
76 checkParam.m_PartRect = m_BoxRect;
78 checkParam.m_pRtData = &m_FocusRect;
79 pTheme->DrawBackground(checkParam);
80
81 CFWL_ThemeText textParam(CFWL_ThemePart::Part::kCaption, this, pGraphics);
82 textParam.m_dwStates = dwStates;
83 textParam.m_matrix = matrix;
84 textParam.m_PartRect = m_CaptionRect;
85 textParam.m_wsText = L"Check box";
86 textParam.m_dwTTOStyles = m_TTOStyles;
87 textParam.m_iTTOAlign = m_iTTOAlign;
88 pTheme->DrawText(textParam);
89}
90
91void CFWL_CheckBox::SetCheckState(int32_t iCheck) {
93 switch (iCheck) {
94 case 1:
96 break;
97 case 2:
100 break;
101 default:
102 break;
103 }
104 RepaintRect(m_ClientRect);
105}
106
107void CFWL_CheckBox::Layout() {
110 m_ClientRect = GetClientRect();
111
112 float fTextLeft = m_ClientRect.left + m_fBoxHeight;
113 m_BoxRect = CFX_RectF(m_ClientRect.TopLeft(), m_fBoxHeight, m_fBoxHeight);
114 m_CaptionRect =
115 CFX_RectF(fTextLeft, m_ClientRect.top, m_ClientRect.right() - fTextLeft,
116 m_ClientRect.height);
117 m_CaptionRect.Inflate(-kCaptionMargin, -kCaptionMargin);
118
119 CFX_RectF rtFocus = m_CaptionRect;
120 CalcTextRect(L"Check box", m_TTOStyles, m_iTTOAlign, &rtFocus);
121 m_FocusRect = CFX_RectF(m_CaptionRect.TopLeft(),
122 std::max(m_CaptionRect.width, rtFocus.width),
123 std::min(m_CaptionRect.height, rtFocus.height));
124 m_FocusRect.Inflate(1, 1);
125}
126
127Mask<CFWL_PartState> CFWL_CheckBox::GetPartStates() const {
128 Mask<CFWL_PartState> dwStates = CFWL_PartState::kNormal;
131 dwStates = CFWL_PartState::kNeutral;
134 dwStates = CFWL_PartState::kChecked;
135 }
137 dwStates |= CFWL_PartState::kDisabled;
139 dwStates |= CFWL_PartState::kHovered;
141 dwStates |= CFWL_PartState::kPressed;
142 else
143 dwStates |= CFWL_PartState::kNormal;
145 dwStates |= CFWL_PartState::kFocused;
146 return dwStates;
147}
148
149void CFWL_CheckBox::UpdateTextOutStyles() {
150 m_iTTOAlign = FDE_TextAlignment::kTopLeft;
151 m_TTOStyles.Reset();
152 m_TTOStyles.single_line_ = true;
153}
154
155void CFWL_CheckBox::NextStates() {
156 uint32_t dwFirststate = m_Properties.m_dwStates;
161 }
162 } else {
171 } else {
174 else
176 }
177 }
178
179 RepaintRect(m_ClientRect);
180 if (dwFirststate == m_Properties.m_dwStates)
181 return;
182
183 CFWL_Event wmCheckBoxState(CFWL_Event::Type::CheckStateChanged, this);
184 DispatchEvent(&wmCheckBoxState);
185}
186
187void CFWL_CheckBox::OnProcessMessage(CFWL_Message* pMessage) {
188 switch (pMessage->GetType()) {
190 OnFocusGained();
191 break;
193 OnFocusLost();
194 break;
196 CFWL_MessageMouse* pMsg = static_cast<CFWL_MessageMouse*>(pMessage);
197 switch (pMsg->m_dwCmd) {
198 case CFWL_MessageMouse::MouseCommand::kLeftButtonDown:
199 OnLButtonDown();
200 break;
201 case CFWL_MessageMouse::MouseCommand::kLeftButtonUp:
202 OnLButtonUp(pMsg);
203 break;
204 case CFWL_MessageMouse::MouseCommand::kMove:
205 OnMouseMove(pMsg);
206 break;
207 case CFWL_MessageMouse::MouseCommand::kLeave:
208 OnMouseLeave();
209 break;
210 default:
211 break;
212 }
213 break;
214 }
216 CFWL_MessageKey* pKey = static_cast<CFWL_MessageKey*>(pMessage);
217 if (pKey->m_dwCmd == CFWL_MessageKey::KeyCommand::kKeyDown)
218 OnKeyDown(pKey);
219 break;
220 }
221 default:
222 break;
223 }
224 // Dst target could be |this|, continue only if not destroyed by above.
225 if (pMessage->GetDstTarget())
227}
228
229void CFWL_CheckBox::OnDrawWidget(CFGAS_GEGraphics* pGraphics,
230 const CFX_Matrix& matrix) {
231 DrawWidget(pGraphics, matrix);
232}
233
234void CFWL_CheckBox::OnFocusGained() {
236 RepaintRect(m_ClientRect);
237}
238
239void CFWL_CheckBox::OnFocusLost() {
241 RepaintRect(m_ClientRect);
242}
243
244void CFWL_CheckBox::OnLButtonDown() {
246 return;
247
248 m_bBtnDown = true;
251 RepaintRect(m_ClientRect);
252}
253
254void CFWL_CheckBox::OnLButtonUp(CFWL_MessageMouse* pMsg) {
255 if (!m_bBtnDown)
256 return;
257
258 m_bBtnDown = false;
259 if (!m_ClientRect.Contains(pMsg->m_pos))
260 return;
261
264 NextStates();
265}
266
267void CFWL_CheckBox::OnMouseMove(CFWL_MessageMouse* pMsg) {
269 return;
270
271 bool bRepaint = false;
272 if (m_bBtnDown) {
273 if (m_ClientRect.Contains(pMsg->m_pos)) {
275 bRepaint = true;
277 }
279 bRepaint = true;
281 }
282 } else {
284 bRepaint = true;
286 }
288 bRepaint = true;
290 }
291 }
292 } else {
293 if (m_ClientRect.Contains(pMsg->m_pos)) {
295 bRepaint = true;
297 }
298 }
299 }
300 if (bRepaint)
301 RepaintRect(m_BoxRect);
302}
303
304void CFWL_CheckBox::OnMouseLeave() {
305 if (m_bBtnDown)
307 else
309
310 RepaintRect(m_BoxRect);
311}
312
313void CFWL_CheckBox::OnKeyDown(CFWL_MessageKey* pMsg) {
315 return;
318 NextStates();
319 }
320}
FDE_TextAlignment
Definition cfde_data.h:12
#define FWL_STATE_CKB_CheckMask
#define FWL_STATE_CKB_Hovered
#define FWL_STATE_CKB_Unchecked
#define FWL_STATE_CKB_Pressed
#define FWL_STATE_CKB_Neutral
#define FWL_STATE_CKB_Checked
#define FWL_STYLEEXT_CKB_3State
#define FWL_STYLEEXT_CKB_RadioButton
CFWL_PartState
FWL_Type
Definition cfwl_widget.h:46
#define FWL_STATE_WGT_Focused
Definition cfwl_widget.h:42
#define FWL_STATE_WGT_Disabled
Definition cfwl_widget.h:41
void OnDrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
~CFWL_CheckBox() override
void SetBoxSize(float fHeight)
void DrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
void OnProcessMessage(CFWL_Message *pMessage) override
void Update() override
FWL_Type GetClassID() const override
const KeyCommand m_dwCmd
const uint32_t m_dwKeyCodeOrChar
const MouseCommand m_dwCmd
Type GetType() const
CFWL_Widget * GetDstTarget() const
CFX_RectF m_PartRect
CFX_Matrix m_matrix
FDE_TextStyle m_dwTTOStyles
FDE_TextAlignment m_iTTOAlign
CFWL_Widget(CFWL_App *app, const Properties &properties, CFWL_Widget *pOuter)
Properties m_Properties
void OnProcessMessage(CFWL_Message *pMessage) override
bool HasBorder() const
CFX_RectF m_WidgetRect
void DrawBorder(CFGAS_GEGraphics *pGraphics, CFWL_ThemePart::Part iPartBorder, const CFX_Matrix &pMatrix)
void RepaintRect(const CFX_RectF &pRect)
IFWL_ThemeProvider * GetThemeProvider() const
void CalcTextRect(const WideString &wsText, const FDE_TextStyle &dwTTOStyles, FDE_TextAlignment iTTOAlign, CFX_RectF *pRect)
virtual CFX_RectF GetClientRect()
void DispatchEvent(CFWL_Event *pEvent)
bool IsLocked() const
CFX_Matrix & operator=(const CFX_Matrix &other)=default
void Inflate(float x, float y)
CFX_RectF & operator=(const CFX_RectF &other)=default
float right() const
constexpr CFX_RectF(float dst_left, float dst_top, float dst_width, float dst_height)
virtual void DrawBackground(const CFWL_ThemeBackground &pParams)=0
virtual void DrawText(const CFWL_ThemeText &pParams)=0
@ XFA_FWL_VKEY_Space
@ XFA_FWL_VKEY_Return
@ XFA_FWL_VKEY_Tab
int FXSYS_roundf(float f)
Definition fx_system.cpp:92
bool single_line_
Definition cfde_data.h:29
void Reset()
Definition cfde_data.h:23