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/src/fwl/basewidget/fwl_barcodeimp.h" | |
8 | |
9 #include "xfa/include/fwl/core/fwl_theme.h" | |
10 #include "xfa/src/fwl/basewidget/fwl_editimp.h" | |
11 #include "xfa/src/fwl/core/fwl_noteimp.h" | |
12 #include "xfa/src/fwl/core/fwl_targetimp.h" | |
13 #include "xfa/src/fwl/core/fwl_widgetimp.h" | |
14 | |
15 // static | |
16 IFWL_Barcode* IFWL_Barcode::Create(const CFWL_WidgetImpProperties& properties) { | |
17 IFWL_Barcode* pBarcode = new IFWL_Barcode; | |
18 CFWL_BarcodeImp* pBarcodeImpl = new CFWL_BarcodeImp(properties, nullptr); | |
19 pBarcode->SetImpl(pBarcodeImpl); | |
20 pBarcodeImpl->SetInterface(pBarcode); | |
21 return pBarcode; | |
22 } | |
23 IFWL_Barcode::IFWL_Barcode() {} | |
24 void IFWL_Barcode::SetType(BC_TYPE type) { | |
25 static_cast<CFWL_BarcodeImp*>(GetImpl())->SetType(type); | |
26 } | |
27 FX_BOOL IFWL_Barcode::IsProtectedType() { | |
28 return static_cast<CFWL_BarcodeImp*>(GetImpl())->IsProtectedType(); | |
29 } | |
30 | |
31 CFWL_BarcodeImp::CFWL_BarcodeImp(const CFWL_WidgetImpProperties& properties, | |
32 IFWL_Widget* pOuter) | |
33 : CFWL_EditImp(properties, pOuter), | |
34 m_pBarcodeEngine(NULL), | |
35 m_dwStatus(0), | |
36 m_type(BC_UNKNOWN) {} | |
37 CFWL_BarcodeImp::~CFWL_BarcodeImp() { | |
38 ReleaseBarcodeEngine(); | |
39 } | |
40 FWL_ERR CFWL_BarcodeImp::GetClassName(CFX_WideString& wsClass) const { | |
41 wsClass = FWL_CLASS_Barcode; | |
42 return FWL_ERR_Succeeded; | |
43 } | |
44 FX_DWORD CFWL_BarcodeImp::GetClassID() const { | |
45 return FWL_CLASSHASH_Barcode; | |
46 } | |
47 FWL_ERR CFWL_BarcodeImp::Initialize() { | |
48 if (!m_pDelegate) { | |
49 m_pDelegate = new CFWL_BarcodeImpDelegate(this); | |
50 } | |
51 if (CFWL_EditImp::Initialize() != FWL_ERR_Succeeded) | |
52 return FWL_ERR_Indefinite; | |
53 return FWL_ERR_Succeeded; | |
54 } | |
55 FWL_ERR CFWL_BarcodeImp::Finalize() { | |
56 delete m_pDelegate; | |
57 m_pDelegate = nullptr; | |
58 ReleaseBarcodeEngine(); | |
59 return CFWL_EditImp::Finalize(); | |
60 } | |
61 FWL_ERR CFWL_BarcodeImp::Update() { | |
62 if (IsLocked()) { | |
63 return FWL_ERR_Indefinite; | |
64 } | |
65 FWL_ERR ret = CFWL_EditImp::Update(); | |
66 GenerateBarcodeImageCache(); | |
67 return ret; | |
68 } | |
69 FWL_ERR CFWL_BarcodeImp::DrawWidget(CFX_Graphics* pGraphics, | |
70 const CFX_Matrix* pMatrix) { | |
71 if (!pGraphics) | |
72 return FWL_ERR_Indefinite; | |
73 if (!m_pProperties->m_pThemeProvider) | |
74 return FWL_ERR_Indefinite; | |
75 if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) == 0) { | |
76 GenerateBarcodeImageCache(); | |
77 if (!m_pBarcodeEngine || (m_dwStatus & XFA_BCS_EncodeSuccess) == 0) { | |
78 return FWL_ERR_Succeeded; | |
79 } | |
80 CFX_Matrix mt; | |
81 mt.e = m_rtClient.left; | |
82 mt.f = m_rtClient.top; | |
83 if (pMatrix) { | |
84 mt.Concat(*pMatrix); | |
85 } | |
86 int32_t errorCode = 0; | |
87 if (!m_pBarcodeEngine->RenderDevice(pGraphics->GetRenderDevice(), pMatrix, | |
88 errorCode)) { | |
89 return FWL_ERR_Indefinite; | |
90 } | |
91 return FWL_ERR_Succeeded; | |
92 } | |
93 return CFWL_EditImp::DrawWidget(pGraphics, pMatrix); | |
94 } | |
95 void CFWL_BarcodeImp::GenerateBarcodeImageCache() { | |
96 if ((m_dwStatus & XFA_BCS_NeedUpdate) == 0) | |
97 return; | |
98 m_dwStatus = 0; | |
99 CreateBarcodeEngine(); | |
100 IFWL_BarcodeDP* pData = | |
101 static_cast<IFWL_BarcodeDP*>(m_pProperties->m_pDataProvider); | |
102 if (!pData) | |
103 return; | |
104 if (!m_pBarcodeEngine) | |
105 return; | |
106 CFX_WideString wsText; | |
107 if (GetText(wsText) != FWL_ERR_Succeeded) | |
108 return; | |
109 CFWL_ThemePart part; | |
110 part.m_pWidget = m_pInterface; | |
111 IFWL_ThemeProvider* pTheme = GetAvailableTheme(); | |
112 IFX_Font* pFont = | |
113 static_cast<IFX_Font*>(pTheme->GetCapacity(&part, FWL_WGTCAPACITY_Font)); | |
114 CFX_Font* pCXFont = | |
115 pFont ? static_cast<CFX_Font*>(pFont->GetDevFont()) : nullptr; | |
116 if (pCXFont) { | |
117 m_pBarcodeEngine->SetFont(pCXFont); | |
118 } | |
119 FX_FLOAT* pFontSize = static_cast<FX_FLOAT*>( | |
120 pTheme->GetCapacity(&part, FWL_WGTCAPACITY_FontSize)); | |
121 if (pFontSize) { | |
122 m_pBarcodeEngine->SetFontSize(*pFontSize); | |
123 } | |
124 FX_ARGB* pFontColor = static_cast<FX_ARGB*>( | |
125 pTheme->GetCapacity(&part, FWL_WGTCAPACITY_TextColor)); | |
126 if (pFontColor) { | |
127 m_pBarcodeEngine->SetFontColor(*pFontColor); | |
128 } | |
129 m_pBarcodeEngine->SetHeight(int32_t(m_rtClient.height)); | |
130 m_pBarcodeEngine->SetWidth(int32_t(m_rtClient.width)); | |
131 FX_DWORD dwAttributeMask = pData->GetBarcodeAttributeMask(); | |
132 if (dwAttributeMask & FWL_BCDATTRIBUTE_CHARENCODING) { | |
133 m_pBarcodeEngine->SetCharEncoding(pData->GetCharEncoding()); | |
134 } | |
135 if (dwAttributeMask & FWL_BCDATTRIBUTE_MODULEHEIGHT) { | |
136 m_pBarcodeEngine->SetModuleHeight(pData->GetModuleHeight()); | |
137 } | |
138 if (dwAttributeMask & FWL_BCDATTRIBUTE_MODULEWIDTH) { | |
139 m_pBarcodeEngine->SetModuleWidth(pData->GetModuleWidth()); | |
140 } | |
141 if (dwAttributeMask & FWL_BCDATTRIBUTE_DATALENGTH) { | |
142 m_pBarcodeEngine->SetDataLength(pData->GetDataLength()); | |
143 } | |
144 if (dwAttributeMask & FWL_BCDATTRIBUTE_CALCHECKSUM) { | |
145 m_pBarcodeEngine->SetCalChecksum(pData->GetCalChecksum()); | |
146 } | |
147 if (dwAttributeMask & FWL_BCDATTRIBUTE_PRINTCHECKSUM) { | |
148 m_pBarcodeEngine->SetPrintChecksum(pData->GetPrintChecksum()); | |
149 } | |
150 if (dwAttributeMask & FWL_BCDATTRIBUTE_TEXTLOCATION) { | |
151 m_pBarcodeEngine->SetTextLocation(pData->GetTextLocation()); | |
152 } | |
153 if (dwAttributeMask & FWL_BCDATTRIBUTE_WIDENARROWRATIO) { | |
154 m_pBarcodeEngine->SetWideNarrowRatio(pData->GetWideNarrowRatio()); | |
155 } | |
156 if (dwAttributeMask & FWL_BCDATTRIBUTE_STARTCHAR) { | |
157 m_pBarcodeEngine->SetStartChar(pData->GetStartChar()); | |
158 } | |
159 if (dwAttributeMask & FWL_BCDATTRIBUTE_ENDCHAR) { | |
160 m_pBarcodeEngine->SetEndChar(pData->GetEndChar()); | |
161 } | |
162 if (dwAttributeMask & FWL_BCDATTRIBUTE_VERSION) { | |
163 m_pBarcodeEngine->SetVersion(pData->GetVersion()); | |
164 } | |
165 if (dwAttributeMask & FWL_BCDATTRIBUTE_ECLEVEL) { | |
166 m_pBarcodeEngine->SetErrorCorrectionLevel(pData->GetErrorCorrectionLevel()); | |
167 } | |
168 if (dwAttributeMask & FWL_BCDATTRIBUTE_TRUNCATED) { | |
169 m_pBarcodeEngine->SetTruncated(pData->GetTruncated()); | |
170 } | |
171 int32_t errorCode = 0; | |
172 m_dwStatus = m_pBarcodeEngine->Encode(wsText, TRUE, errorCode) | |
173 ? XFA_BCS_EncodeSuccess | |
174 : 0; | |
175 } | |
176 void CFWL_BarcodeImp::CreateBarcodeEngine() { | |
177 if ((m_pBarcodeEngine == NULL) && (m_type != BC_UNKNOWN)) { | |
178 m_pBarcodeEngine = FX_Barcode_Create(m_type); | |
179 } | |
180 } | |
181 void CFWL_BarcodeImp::ReleaseBarcodeEngine() { | |
182 if (m_pBarcodeEngine) { | |
183 m_pBarcodeEngine->Release(); | |
184 m_pBarcodeEngine = NULL; | |
185 } | |
186 } | |
187 void CFWL_BarcodeImp::SetType(BC_TYPE type) { | |
188 if (m_type == type) { | |
189 return; | |
190 } | |
191 ReleaseBarcodeEngine(); | |
192 m_type = type; | |
193 m_dwStatus = XFA_BCS_NeedUpdate; | |
194 } | |
195 FWL_ERR CFWL_BarcodeImp::SetText(const CFX_WideString& wsText) { | |
196 ReleaseBarcodeEngine(); | |
197 m_dwStatus = XFA_BCS_NeedUpdate; | |
198 return CFWL_EditImp::SetText(wsText); | |
199 } | |
200 FX_BOOL CFWL_BarcodeImp::IsProtectedType() { | |
201 if (!m_pBarcodeEngine) { | |
202 return TRUE; | |
203 } | |
204 BC_TYPE tEngineType = m_pBarcodeEngine->GetType(); | |
205 if (tEngineType == BC_QR_CODE || tEngineType == BC_PDF417 || | |
206 tEngineType == BC_DATAMATRIX) { | |
207 return TRUE; | |
208 } | |
209 return FALSE; | |
210 } | |
211 CFWL_BarcodeImpDelegate::CFWL_BarcodeImpDelegate(CFWL_BarcodeImp* pOwner) | |
212 : CFWL_EditImpDelegate(pOwner) {} | |
213 FWL_ERR CFWL_BarcodeImpDelegate::OnProcessEvent(CFWL_Event* pEvent) { | |
214 FX_DWORD dwFlag = pEvent->GetClassID(); | |
215 if (dwFlag == FWL_EVTHASH_EDT_TextChanged) { | |
216 CFWL_BarcodeImp* pOwner = static_cast<CFWL_BarcodeImp*>(m_pOwner); | |
217 pOwner->ReleaseBarcodeEngine(); | |
218 pOwner->m_dwStatus = XFA_BCS_NeedUpdate; | |
219 } | |
220 return CFWL_EditImpDelegate::OnProcessEvent(pEvent); | |
221 } | |
OLD | NEW |