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/ifwl_pushbutton.h" | |
8 | |
9 #include <memory> | |
10 #include <utility> | |
11 | |
12 #include "third_party/base/ptr_util.h" | |
13 #include "xfa/fde/tto/fde_textout.h" | |
14 #include "xfa/fwl/core/cfwl_evtclick.h" | |
15 #include "xfa/fwl/core/cfwl_evtmouse.h" | |
16 #include "xfa/fwl/core/cfwl_msgkey.h" | |
17 #include "xfa/fwl/core/cfwl_msgmouse.h" | |
18 #include "xfa/fwl/core/cfwl_notedriver.h" | |
19 #include "xfa/fwl/core/cfwl_themebackground.h" | |
20 #include "xfa/fwl/core/cfwl_themetext.h" | |
21 #include "xfa/fwl/core/ifwl_themeprovider.h" | |
22 | |
23 IFWL_PushButton::IFWL_PushButton( | |
24 const CFWL_App* app, | |
25 std::unique_ptr<CFWL_WidgetProperties> properties) | |
26 : IFWL_Widget(app, std::move(properties), nullptr), | |
27 m_bBtnDown(false), | |
28 m_dwTTOStyles(FDE_TTOSTYLE_SingleLine), | |
29 m_iTTOAlign(FDE_TTOALIGNMENT_Center) { | |
30 m_rtClient.Set(0, 0, 0, 0); | |
31 m_rtCaption.Set(0, 0, 0, 0); | |
32 } | |
33 | |
34 IFWL_PushButton::~IFWL_PushButton() {} | |
35 | |
36 FWL_Type IFWL_PushButton::GetClassID() const { | |
37 return FWL_Type::PushButton; | |
38 } | |
39 | |
40 void IFWL_PushButton::GetWidgetRect(CFX_RectF& rect, bool bAutoSize) { | |
41 if (!bAutoSize) { | |
42 rect = m_pProperties->m_rtWidget; | |
43 return; | |
44 } | |
45 | |
46 rect.Set(0, 0, 0, 0); | |
47 if (!m_pProperties->m_pThemeProvider) | |
48 m_pProperties->m_pThemeProvider = GetAvailableTheme(); | |
49 | |
50 FX_FLOAT* fcaption = | |
51 static_cast<FX_FLOAT*>(GetThemeCapacity(CFWL_WidgetCapacity::Margin)); | |
52 rect.Inflate(*fcaption, *fcaption); | |
53 IFWL_Widget::GetWidgetRect(rect, true); | |
54 } | |
55 | |
56 void IFWL_PushButton::SetStates(uint32_t dwStates, bool bSet) { | |
57 if ((dwStates & FWL_WGTSTATE_Disabled) && bSet) { | |
58 m_pProperties->m_dwStates = FWL_WGTSTATE_Disabled; | |
59 return; | |
60 } | |
61 IFWL_Widget::SetStates(dwStates, bSet); | |
62 } | |
63 | |
64 void IFWL_PushButton::Update() { | |
65 if (IsLocked()) | |
66 return; | |
67 if (!m_pProperties->m_pThemeProvider) | |
68 m_pProperties->m_pThemeProvider = GetAvailableTheme(); | |
69 | |
70 UpdateTextOutStyles(); | |
71 GetClientRect(m_rtClient); | |
72 m_rtCaption = m_rtClient; | |
73 FX_FLOAT* fcaption = | |
74 static_cast<FX_FLOAT*>(GetThemeCapacity(CFWL_WidgetCapacity::Margin)); | |
75 m_rtCaption.Inflate(-*fcaption, -*fcaption); | |
76 } | |
77 | |
78 void IFWL_PushButton::DrawWidget(CFX_Graphics* pGraphics, | |
79 const CFX_Matrix* pMatrix) { | |
80 if (!pGraphics) | |
81 return; | |
82 if (!m_pProperties->m_pThemeProvider) | |
83 return; | |
84 | |
85 if (HasBorder()) { | |
86 DrawBorder(pGraphics, CFWL_Part::Border, m_pProperties->m_pThemeProvider, | |
87 pMatrix); | |
88 } | |
89 if (HasEdge()) { | |
90 DrawEdge(pGraphics, CFWL_Part::Edge, m_pProperties->m_pThemeProvider, | |
91 pMatrix); | |
92 } | |
93 DrawBkground(pGraphics, m_pProperties->m_pThemeProvider, pMatrix); | |
94 } | |
95 | |
96 void IFWL_PushButton::DrawBkground(CFX_Graphics* pGraphics, | |
97 IFWL_ThemeProvider* pTheme, | |
98 const CFX_Matrix* pMatrix) { | |
99 CFWL_ThemeBackground param; | |
100 param.m_pWidget = this; | |
101 param.m_iPart = CFWL_Part::Background; | |
102 param.m_dwStates = GetPartStates(); | |
103 param.m_pGraphics = pGraphics; | |
104 if (pMatrix) | |
105 param.m_matrix.Concat(*pMatrix); | |
106 param.m_rtPart = m_rtClient; | |
107 if (m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) | |
108 param.m_pData = &m_rtCaption; | |
109 pTheme->DrawBackground(¶m); | |
110 } | |
111 | |
112 uint32_t IFWL_PushButton::GetPartStates() { | |
113 uint32_t dwStates = CFWL_PartState_Normal; | |
114 if (m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) | |
115 dwStates |= CFWL_PartState_Focused; | |
116 if (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled) | |
117 dwStates = CFWL_PartState_Disabled; | |
118 else if (m_pProperties->m_dwStates & FWL_STATE_PSB_Pressed) | |
119 dwStates |= CFWL_PartState_Pressed; | |
120 else if (m_pProperties->m_dwStates & FWL_STATE_PSB_Hovered) | |
121 dwStates |= CFWL_PartState_Hovered; | |
122 else if (m_pProperties->m_dwStates & FWL_STATE_PSB_Default) | |
123 dwStates |= CFWL_PartState_Default; | |
124 return dwStates; | |
125 } | |
126 | |
127 void IFWL_PushButton::UpdateTextOutStyles() { | |
128 switch (m_pProperties->m_dwStyleExes & | |
129 (FWL_STYLEEXT_PSB_HLayoutMask | FWL_STYLEEXT_PSB_VLayoutMask)) { | |
130 case FWL_STYLEEXT_PSB_Left | FWL_STYLEEXT_PSB_Top: { | |
131 m_iTTOAlign = FDE_TTOALIGNMENT_TopLeft; | |
132 break; | |
133 } | |
134 case FWL_STYLEEXT_PSB_Center | FWL_STYLEEXT_PSB_Top: { | |
135 m_iTTOAlign = FDE_TTOALIGNMENT_TopCenter; | |
136 break; | |
137 } | |
138 case FWL_STYLEEXT_PSB_Right | FWL_STYLEEXT_PSB_Top: { | |
139 m_iTTOAlign = FDE_TTOALIGNMENT_TopRight; | |
140 break; | |
141 } | |
142 case FWL_STYLEEXT_PSB_Left | FWL_STYLEEXT_PSB_VCenter: { | |
143 m_iTTOAlign = FDE_TTOALIGNMENT_CenterLeft; | |
144 break; | |
145 } | |
146 case FWL_STYLEEXT_PSB_Right | FWL_STYLEEXT_PSB_VCenter: { | |
147 m_iTTOAlign = FDE_TTOALIGNMENT_CenterRight; | |
148 break; | |
149 } | |
150 case FWL_STYLEEXT_PSB_Left | FWL_STYLEEXT_PSB_Bottom: { | |
151 m_iTTOAlign = FDE_TTOALIGNMENT_BottomLeft; | |
152 break; | |
153 } | |
154 case FWL_STYLEEXT_PSB_Center | FWL_STYLEEXT_PSB_Bottom: { | |
155 m_iTTOAlign = FDE_TTOALIGNMENT_BottomCenter; | |
156 break; | |
157 } | |
158 case FWL_STYLEEXT_PSB_Right | FWL_STYLEEXT_PSB_Bottom: { | |
159 m_iTTOAlign = FDE_TTOALIGNMENT_BottomRight; | |
160 break; | |
161 } | |
162 case FWL_STYLEEXT_PSB_Center | FWL_STYLEEXT_PSB_VCenter: | |
163 default: { | |
164 m_iTTOAlign = FDE_TTOALIGNMENT_Center; | |
165 break; | |
166 } | |
167 } | |
168 m_dwTTOStyles = FDE_TTOSTYLE_SingleLine; | |
169 if (m_pProperties->m_dwStyleExes & FWL_WGTSTYLE_RTLReading) | |
170 m_dwTTOStyles |= FDE_TTOSTYLE_RTL; | |
171 } | |
172 | |
173 void IFWL_PushButton::OnProcessMessage(CFWL_Message* pMessage) { | |
174 if (!pMessage) | |
175 return; | |
176 if (!IsEnabled()) | |
177 return; | |
178 | |
179 CFWL_MessageType dwMsgCode = pMessage->GetClassID(); | |
180 switch (dwMsgCode) { | |
181 case CFWL_MessageType::SetFocus: | |
182 OnFocusChanged(pMessage, true); | |
183 break; | |
184 case CFWL_MessageType::KillFocus: | |
185 OnFocusChanged(pMessage, false); | |
186 break; | |
187 case CFWL_MessageType::Mouse: { | |
188 CFWL_MsgMouse* pMsg = static_cast<CFWL_MsgMouse*>(pMessage); | |
189 switch (pMsg->m_dwCmd) { | |
190 case FWL_MouseCommand::LeftButtonDown: | |
191 OnLButtonDown(pMsg); | |
192 break; | |
193 case FWL_MouseCommand::LeftButtonUp: | |
194 OnLButtonUp(pMsg); | |
195 break; | |
196 case FWL_MouseCommand::Move: | |
197 OnMouseMove(pMsg); | |
198 break; | |
199 case FWL_MouseCommand::Leave: | |
200 OnMouseLeave(pMsg); | |
201 break; | |
202 default: | |
203 break; | |
204 } | |
205 break; | |
206 } | |
207 case CFWL_MessageType::Key: { | |
208 CFWL_MsgKey* pKey = static_cast<CFWL_MsgKey*>(pMessage); | |
209 if (pKey->m_dwCmd == FWL_KeyCommand::KeyDown) | |
210 OnKeyDown(pKey); | |
211 break; | |
212 } | |
213 default: | |
214 break; | |
215 } | |
216 IFWL_Widget::OnProcessMessage(pMessage); | |
217 } | |
218 | |
219 void IFWL_PushButton::OnDrawWidget(CFX_Graphics* pGraphics, | |
220 const CFX_Matrix* pMatrix) { | |
221 DrawWidget(pGraphics, pMatrix); | |
222 } | |
223 | |
224 void IFWL_PushButton::OnFocusChanged(CFWL_Message* pMsg, bool bSet) { | |
225 if (bSet) | |
226 m_pProperties->m_dwStates |= FWL_WGTSTATE_Focused; | |
227 else | |
228 m_pProperties->m_dwStates &= ~FWL_WGTSTATE_Focused; | |
229 | |
230 Repaint(&m_rtClient); | |
231 } | |
232 | |
233 void IFWL_PushButton::OnLButtonDown(CFWL_MsgMouse* pMsg) { | |
234 if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) == 0) | |
235 SetFocus(true); | |
236 | |
237 m_bBtnDown = true; | |
238 m_pProperties->m_dwStates |= FWL_STATE_PSB_Hovered; | |
239 m_pProperties->m_dwStates |= FWL_STATE_PSB_Pressed; | |
240 Repaint(&m_rtClient); | |
241 } | |
242 | |
243 void IFWL_PushButton::OnLButtonUp(CFWL_MsgMouse* pMsg) { | |
244 m_bBtnDown = false; | |
245 if (m_rtClient.Contains(pMsg->m_fx, pMsg->m_fy)) { | |
246 m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Pressed; | |
247 m_pProperties->m_dwStates |= FWL_STATE_PSB_Hovered; | |
248 } else { | |
249 m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Hovered; | |
250 m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Pressed; | |
251 } | |
252 if (m_rtClient.Contains(pMsg->m_fx, pMsg->m_fy)) { | |
253 CFWL_EvtClick wmClick; | |
254 wmClick.m_pSrcTarget = this; | |
255 DispatchEvent(&wmClick); | |
256 } | |
257 Repaint(&m_rtClient); | |
258 } | |
259 | |
260 void IFWL_PushButton::OnMouseMove(CFWL_MsgMouse* pMsg) { | |
261 bool bRepaint = false; | |
262 if (m_bBtnDown) { | |
263 if (m_rtClient.Contains(pMsg->m_fx, pMsg->m_fy)) { | |
264 if ((m_pProperties->m_dwStates & FWL_STATE_PSB_Pressed) == 0) { | |
265 m_pProperties->m_dwStates |= FWL_STATE_PSB_Pressed; | |
266 bRepaint = true; | |
267 } | |
268 if (m_pProperties->m_dwStates & FWL_STATE_PSB_Hovered) { | |
269 m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Hovered; | |
270 bRepaint = true; | |
271 } | |
272 } else { | |
273 if (m_pProperties->m_dwStates & FWL_STATE_PSB_Pressed) { | |
274 m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Pressed; | |
275 bRepaint = true; | |
276 } | |
277 if ((m_pProperties->m_dwStates & FWL_STATE_PSB_Hovered) == 0) { | |
278 m_pProperties->m_dwStates |= FWL_STATE_PSB_Hovered; | |
279 bRepaint = true; | |
280 } | |
281 } | |
282 } else { | |
283 if (!m_rtClient.Contains(pMsg->m_fx, pMsg->m_fy)) | |
284 return; | |
285 if ((m_pProperties->m_dwStates & FWL_STATE_PSB_Hovered) == 0) { | |
286 m_pProperties->m_dwStates |= FWL_STATE_PSB_Hovered; | |
287 bRepaint = true; | |
288 } | |
289 } | |
290 if (bRepaint) | |
291 Repaint(&m_rtClient); | |
292 } | |
293 | |
294 void IFWL_PushButton::OnMouseLeave(CFWL_MsgMouse* pMsg) { | |
295 m_bBtnDown = false; | |
296 m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Hovered; | |
297 m_pProperties->m_dwStates &= ~FWL_STATE_PSB_Pressed; | |
298 Repaint(&m_rtClient); | |
299 } | |
300 | |
301 void IFWL_PushButton::OnKeyDown(CFWL_MsgKey* pMsg) { | |
302 if (pMsg->m_dwKeyCode == FWL_VKEY_Return) { | |
303 CFWL_EvtMouse wmMouse; | |
304 wmMouse.m_pSrcTarget = this; | |
305 wmMouse.m_dwCmd = FWL_MouseCommand::LeftButtonUp; | |
306 DispatchEvent(&wmMouse); | |
307 CFWL_EvtClick wmClick; | |
308 wmClick.m_pSrcTarget = this; | |
309 DispatchEvent(&wmClick); | |
310 return; | |
311 } | |
312 if (pMsg->m_dwKeyCode != FWL_VKEY_Tab) | |
313 return; | |
314 | |
315 DispatchKeyEvent(pMsg); | |
316 } | |
OLD | NEW |