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