1 // Copyright 2017 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 "fpdfsdk/formfiller/cffl_button.h"
8 
9 #include "core/fpdfdoc/cpdf_formcontrol.h"
10 
CFFL_Button(CPDFSDK_FormFillEnvironment * pFormFillEnv,CPDFSDK_Widget * pWidget)11 CFFL_Button::CFFL_Button(CPDFSDK_FormFillEnvironment* pFormFillEnv,
12                          CPDFSDK_Widget* pWidget)
13     : CFFL_FormFiller(pFormFillEnv, pWidget),
14       m_bMouseIn(false),
15       m_bMouseDown(false) {}
16 
~CFFL_Button()17 CFFL_Button::~CFFL_Button() {}
18 
OnMouseEnter(CPDFSDK_PageView * pPageView)19 void CFFL_Button::OnMouseEnter(CPDFSDK_PageView* pPageView) {
20   m_bMouseIn = true;
21   InvalidateRect(GetViewBBox(pPageView));
22 }
23 
OnMouseExit(CPDFSDK_PageView * pPageView)24 void CFFL_Button::OnMouseExit(CPDFSDK_PageView* pPageView) {
25   m_bMouseIn = false;
26   InvalidateRect(GetViewBBox(pPageView));
27   m_pTimer.reset();
28   ASSERT(m_pWidget);
29 }
30 
OnLButtonDown(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,uint32_t nFlags,const CFX_PointF & point)31 bool CFFL_Button::OnLButtonDown(CPDFSDK_PageView* pPageView,
32                                 CPDFSDK_Annot* pAnnot,
33                                 uint32_t nFlags,
34                                 const CFX_PointF& point) {
35   if (!pAnnot->GetRect().Contains(point))
36     return false;
37 
38   m_bMouseDown = true;
39   m_bValid = true;
40   InvalidateRect(GetViewBBox(pPageView));
41   return true;
42 }
43 
OnLButtonUp(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,uint32_t nFlags,const CFX_PointF & point)44 bool CFFL_Button::OnLButtonUp(CPDFSDK_PageView* pPageView,
45                               CPDFSDK_Annot* pAnnot,
46                               uint32_t nFlags,
47                               const CFX_PointF& point) {
48   if (!pAnnot->GetRect().Contains(point))
49     return false;
50 
51   m_bMouseDown = false;
52   m_pWidget->GetPDFPage();
53   InvalidateRect(GetViewBBox(pPageView));
54   return true;
55 }
56 
OnMouseMove(CPDFSDK_PageView * pPageView,uint32_t nFlags,const CFX_PointF & point)57 bool CFFL_Button::OnMouseMove(CPDFSDK_PageView* pPageView,
58                               uint32_t nFlags,
59                               const CFX_PointF& point) {
60   return true;
61 }
62 
OnDraw(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,CFX_RenderDevice * pDevice,const CFX_Matrix & mtUser2Device)63 void CFFL_Button::OnDraw(CPDFSDK_PageView* pPageView,
64                          CPDFSDK_Annot* pAnnot,
65                          CFX_RenderDevice* pDevice,
66                          const CFX_Matrix& mtUser2Device) {
67   ASSERT(pPageView);
68   CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot);
69   CPDF_FormControl* pCtrl = pWidget->GetFormControl();
70   if (pCtrl->GetHighlightingMode() != CPDF_FormControl::Push) {
71     pWidget->DrawAppearance(pDevice, mtUser2Device, CPDF_Annot::Normal,
72                             nullptr);
73     return;
74   }
75   if (m_bMouseDown) {
76     if (pWidget->IsWidgetAppearanceValid(CPDF_Annot::Down)) {
77       pWidget->DrawAppearance(pDevice, mtUser2Device, CPDF_Annot::Down,
78                               nullptr);
79     } else {
80       pWidget->DrawAppearance(pDevice, mtUser2Device, CPDF_Annot::Normal,
81                               nullptr);
82     }
83     return;
84   }
85   if (m_bMouseIn) {
86     if (pWidget->IsWidgetAppearanceValid(CPDF_Annot::Rollover)) {
87       pWidget->DrawAppearance(pDevice, mtUser2Device, CPDF_Annot::Rollover,
88                               nullptr);
89     } else {
90       pWidget->DrawAppearance(pDevice, mtUser2Device, CPDF_Annot::Normal,
91                               nullptr);
92     }
93     return;
94   }
95 
96   pWidget->DrawAppearance(pDevice, mtUser2Device, CPDF_Annot::Normal, nullptr);
97 }
98 
OnDrawDeactive(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,CFX_RenderDevice * pDevice,const CFX_Matrix & mtUser2Device)99 void CFFL_Button::OnDrawDeactive(CPDFSDK_PageView* pPageView,
100                                  CPDFSDK_Annot* pAnnot,
101                                  CFX_RenderDevice* pDevice,
102                                  const CFX_Matrix& mtUser2Device) {
103   OnDraw(pPageView, pAnnot, pDevice, mtUser2Device);
104 }
105