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