Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa / src / fwl / src / basewidget / fwl_caretimp.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4 \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #include "../../../foxitlib.h"\r
8 #include "../core/include/fwl_targetimp.h"\r
9 #include "../core/include/fwl_noteimp.h"\r
10 #include "../core/include/fwl_widgetimp.h"\r
11 #include "include/fwl_caretimp.h"\r
12 IFWL_Caret* IFWL_Caret::Create()\r
13 {\r
14     return new IFWL_Caret;\r
15 }\r
16 IFWL_Caret::IFWL_Caret()\r
17 {\r
18     m_pData = NULL;\r
19 }\r
20 IFWL_Caret::~IFWL_Caret()\r
21 {\r
22     if (m_pData) {\r
23         delete (CFWL_CaretImp*)m_pData;\r
24         m_pData = NULL;\r
25     }\r
26 }\r
27 FWL_ERR IFWL_Caret::Initialize(IFWL_Widget *pOuter)\r
28 {\r
29     m_pData = FX_NEW CFWL_CaretImp(pOuter);\r
30     ((CFWL_CaretImp*)m_pData)->SetInterface(this);\r
31     return ((CFWL_CaretImp*)m_pData)->Initialize();\r
32 }\r
33 FWL_ERR IFWL_Caret::Initialize(const CFWL_WidgetImpProperties &properties, IFWL_Widget *pOuter)\r
34 {\r
35     m_pData = FX_NEW CFWL_CaretImp(properties, pOuter);\r
36     ((CFWL_CaretImp*)m_pData)->SetInterface(this);\r
37     return ((CFWL_CaretImp*)m_pData)->Initialize();\r
38 }\r
39 FWL_ERR IFWL_Caret::ShowCaret(FX_BOOL bFlag)\r
40 {\r
41     return ((CFWL_CaretImp*)m_pData)->ShowCaret(bFlag);\r
42 }\r
43 FWL_ERR IFWL_Caret::GetFrequency(FX_DWORD &elapse)\r
44 {\r
45     return ((CFWL_CaretImp*)m_pData)->GetFrequency(elapse);\r
46 }\r
47 FWL_ERR IFWL_Caret::SetFrequency(FX_DWORD elapse)\r
48 {\r
49     return ((CFWL_CaretImp*)m_pData)->SetFrequency(elapse);\r
50 }\r
51 FWL_ERR IFWL_Caret::SetColor(CFX_Color crFill)\r
52 {\r
53     return ((CFWL_CaretImp*)m_pData)->SetColor(crFill);\r
54 }\r
55 CFWL_CaretImp::CFWL_CaretImp(IFWL_Widget *pOuter)\r
56     : m_hTimer(NULL)\r
57 {\r
58     m_dwElapse = 400;\r
59     m_pTimer = FX_NEW CFWL_CaretTimer(this);\r
60     m_bSetColor = FALSE;\r
61     SetStates(FWL_STATE_CAT_HightLight);\r
62 }\r
63 CFWL_CaretImp::CFWL_CaretImp(const CFWL_WidgetImpProperties &properties, IFWL_Widget *pOuter): m_hTimer(NULL)\r
64 {\r
65     m_dwElapse = 400;\r
66     m_pTimer = FX_NEW CFWL_CaretTimer(this);\r
67     m_bSetColor = FALSE;\r
68     SetStates(FWL_STATE_CAT_HightLight);\r
69 }\r
70 CFWL_CaretImp::~CFWL_CaretImp()\r
71 {\r
72     if (m_pTimer) {\r
73         delete m_pTimer;\r
74         m_pTimer = NULL;\r
75     }\r
76 }\r
77 FWL_ERR CFWL_CaretImp::GetClassName(CFX_WideString &wsClass) const\r
78 {\r
79     wsClass = FWL_CLASS_Caret;\r
80     return FWL_ERR_Succeeded;\r
81 }\r
82 FX_DWORD CFWL_CaretImp::GetClassID() const\r
83 {\r
84     return FWL_CLASSHASH_Caret;\r
85 }\r
86 FWL_ERR CFWL_CaretImp::Initialize()\r
87 {\r
88     _FWL_ERR_CHECK_RETURN_VALUE_IF_FAIL(CFWL_WidgetImp::Initialize(), FWL_ERR_Indefinite);\r
89     m_pDelegate = (IFWL_WidgetDelegate*) FX_NEW CFWL_CaretImpDelegate(this);\r
90     return FWL_ERR_Succeeded;\r
91 }\r
92 FWL_ERR CFWL_CaretImp::Finalize()\r
93 {\r
94     if (m_hTimer) {\r
95         FWL_StopTimer(m_hTimer);\r
96         m_hTimer = NULL;\r
97     }\r
98     if (m_pDelegate) {\r
99         delete (CFWL_CaretImpDelegate*)m_pDelegate;\r
100         m_pDelegate = NULL;\r
101     }\r
102     return CFWL_WidgetImp::Finalize();\r
103 }\r
104 FWL_ERR CFWL_CaretImp::DrawWidget(CFX_Graphics *pGraphics, const CFX_Matrix *pMatrix)\r
105 {\r
106     _FWL_RETURN_VALUE_IF_FAIL(pGraphics, FWL_ERR_Indefinite);\r
107     if (!m_pProperties->m_pThemeProvider) {\r
108         m_pProperties->m_pThemeProvider = GetAvailableTheme();\r
109     }\r
110     _FWL_RETURN_VALUE_IF_FAIL(m_pProperties->m_pThemeProvider, FWL_ERR_Indefinite);\r
111     DrawCaretBK(pGraphics, m_pProperties->m_pThemeProvider, pMatrix);\r
112     return FWL_ERR_Succeeded;\r
113 }\r
114 FWL_ERR CFWL_CaretImp::ShowCaret(FX_BOOL bFlag)\r
115 {\r
116     if (m_hTimer) {\r
117         FWL_StopTimer(m_hTimer);\r
118         m_hTimer = NULL;\r
119     }\r
120     if (bFlag) {\r
121         m_hTimer = FWL_StartTimer((IFWL_Timer *)m_pTimer, m_dwElapse);\r
122     }\r
123     return SetStates(FWL_WGTSTATE_Invisible, !bFlag);\r
124 }\r
125 FWL_ERR CFWL_CaretImp::GetFrequency(FX_DWORD &elapse)\r
126 {\r
127     elapse = m_dwElapse;\r
128     return FWL_ERR_Succeeded;\r
129 }\r
130 FWL_ERR CFWL_CaretImp::SetFrequency(FX_DWORD elapse)\r
131 {\r
132     m_dwElapse = elapse;\r
133     return FWL_ERR_Succeeded;\r
134 }\r
135 FWL_ERR CFWL_CaretImp::SetColor(CFX_Color crFill)\r
136 {\r
137     m_bSetColor = TRUE;\r
138     m_crFill = crFill;\r
139     return FWL_ERR_Succeeded;\r
140 }\r
141 FX_BOOL CFWL_CaretImp::DrawCaretBK(CFX_Graphics *pGraphics, IFWL_ThemeProvider *pTheme, const CFX_Matrix *pMatrix)\r
142 {\r
143     CFX_RectF rect;\r
144     GetWidgetRect(rect);\r
145     rect.Set(0, 0, rect.width, rect.height);\r
146     CFWL_ThemeBackground param;\r
147     param.m_pWidget = m_pInterface;\r
148     param.m_pGraphics = pGraphics;\r
149     param.m_rtPart = rect;\r
150     if (m_bSetColor) {\r
151         param.m_pData =  &m_crFill;\r
152     }\r
153     if (!(m_pProperties->m_dwStates & FWL_STATE_CAT_HightLight)) {\r
154         return FWL_ERR_Succeeded;\r
155     }\r
156     param.m_iPart = FWL_PART_CAT_Background;\r
157     param.m_dwStates = FWL_PARTSTATE_CAT_HightLight;\r
158     if (pMatrix) {\r
159         param.m_matrix.Concat(*pMatrix);\r
160     }\r
161     pTheme->DrawBackground(&param);\r
162     return FWL_ERR_Succeeded;\r
163 }\r
164 CFWL_CaretImp::CFWL_CaretTimer::CFWL_CaretTimer(CFWL_CaretImp *m_pCaret)\r
165 {\r
166     this->m_pCaret = m_pCaret;\r
167 }\r
168 FX_INT32 CFWL_CaretImp::CFWL_CaretTimer::Run(FWL_HTIMER hTimer)\r
169 {\r
170     if (m_pCaret->GetStates() & FWL_STATE_CAT_HightLight) {\r
171         m_pCaret->SetStates(FWL_STATE_CAT_HightLight, FALSE);\r
172     } else {\r
173         m_pCaret->SetStates(FWL_STATE_CAT_HightLight);\r
174     }\r
175     CFX_RectF rt;\r
176     m_pCaret->GetWidgetRect(rt);\r
177     rt.Set(0, 0, rt.width + 1, rt.height);\r
178     m_pCaret->Repaint(&rt);\r
179     return 1;\r
180 }\r
181 CFWL_CaretImpDelegate::CFWL_CaretImpDelegate(CFWL_CaretImp *pOwner)\r
182     : m_pOwner(pOwner)\r
183 {\r
184 }\r
185 FX_INT32 CFWL_CaretImpDelegate::OnProcessMessage(CFWL_Message *pMessage)\r
186 {\r
187     return 1;\r
188 }\r
189 FWL_ERR CFWL_CaretImpDelegate::OnDrawWidget(CFX_Graphics *pGraphics, const CFX_Matrix *pMatrix)\r
190 {\r
191     return m_pOwner->DrawWidget(pGraphics, pMatrix);\r
192 }\r