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