/** * @file XTPGaugeMeterType.cpp * * @copyright * (c) 1998-2025 Codejock Software, All Rights Reserved. * * This source file is the property of Codejock Software and must not be * redistributed by any means without the explicit written permission of * Codejock Software. * * The use of this source code is governed by the terms and conditions specified * in the Toolkit Pro license agreement. Codejock Software grants you, as a * single software developer, the limited right to use this software on one * computer only. * * Contact Information: * support@codejock.com * http://www.codejock.com * */ #include "stdafx.h" #include "Common/Base/Diagnostic/XTPDisableAdvancedWarnings.h" #include #include "Common/Base/Diagnostic/XTPEnableAdvancedWarnings.h" #include "Common/XTPTypeId.h" #include "Common/XTPCasting.h" #include "Common/XTPFramework.h" #include "Common/XTPSystemHelpers.h" #include "Common/XTPSynchro.h" #include "Common/XTPApplication.h" #include "Common/XTPSingleton.h" #include "Common/Math/XTPMathUtils.h" #include "Markup/XTPMarkupObject.h" #include "Markup/XTPMarkupContext.h" #include "Gauge/XTPGaugeBaseType.h" #include "Gauge/Types/XTPGaugeMeterType.h" #include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" #ifdef _DEBUG # undef THIS_FILE static char THIS_FILE[] = __FILE__; # define new DEBUG_NEW #endif IMPLEMENT_DYNCREATE(CXTPGaugeMeterType, CXTPGaugeBaseType); const double CXTPGaugeMeterType::m_dNan = XTP_NAN; CXTPGaugeMeterType::CXTPGaugeMeterType() : m_nDimCount(UINT_MAX) { } CXTPGaugeMeterType::~CXTPGaugeMeterType() { } LPCWSTR CXTPGaugeMeterType::GetTypeName() const { return L"Meter"; } double CXTPGaugeMeterType::GetMin(UINT nDimension) const { DimensionData* pd = const_cast(this)->GetDimData(nDimension); if (NULL == pd) return XTP_NAN; if (!XTP_ISNAN(pd->dMin)) return pd->dMin; COleVariant v; HRESULT hr = E_FAIL; if (UINT_MAX != nDimension) { hr = GetTrait(FormatJPath(nDimension, _T("min")), v); if (VT_NULL == v.vt) return XTP_NAN; } if (FAILED(hr)) hr = GetTrait(_T("min"), v); if (FAILED(hr)) AfxThrowOleException(hr); v.ChangeType(VT_R8); pd->dMin = v.dblVal; return pd->dMin; } BOOL CXTPGaugeMeterType::SetMin(UINT nDimension, double dValue) { DimensionData* pd = const_cast(this)->GetDimData(nDimension); if (NULL == pd) return FALSE; if (pd->dMin == dValue) return FALSE; if (UINT_MAX == nDimension) { if (!XTP_ASSERT_CHECK(!XTP_ISNAN(dValue))) return FALSE; HRESULT hr = SetTrait(_T("min"), dValue); if (FAILED(hr)) AfxThrowOleException(hr); pd->dMin = dValue; if (IsAutomaticUpdateEnabled()) Update(); return TRUE; } CString jPath = FormatJPath(nDimension, _T("min")); if (XTP_ISNAN(dValue)) { HRESULT hr = SetTrait(jPath, COleVariant()); if (FAILED(hr)) AfxThrowOleException(hr); pd->dMin = XTP_NAN; if (IsAutomaticUpdateEnabled()) Update(); return TRUE; } HRESULT hr = SetTrait(jPath, dValue); if (FAILED(hr)) AfxThrowOleException(hr); pd->dMin = dValue; if (IsAutomaticUpdateEnabled()) Update(); return TRUE; } double CXTPGaugeMeterType::GetMax(UINT nDimension) const { DimensionData* pd = const_cast(this)->GetDimData(nDimension); if (NULL == pd) return XTP_NAN; if (!XTP_ISNAN(pd->dMax)) return pd->dMax; COleVariant v; HRESULT hr = E_FAIL; if (UINT_MAX != nDimension) { hr = GetTrait(FormatJPath(nDimension, _T("max")), v); if (VT_NULL == v.vt) return XTP_NAN; } if (FAILED(hr)) hr = GetTrait(_T("max"), v); if (FAILED(hr)) AfxThrowOleException(hr); v.ChangeType(VT_R8); pd->dMax = v.dblVal; return pd->dMax; } BOOL CXTPGaugeMeterType::SetMax(UINT nDimension, double dValue) { DimensionData* pd = const_cast(this)->GetDimData(nDimension); if (NULL == pd) return FALSE; if (pd->dMax == dValue) return FALSE; if (UINT_MAX == nDimension) { if (!XTP_ASSERT_CHECK(!XTP_ISNAN(dValue))) return FALSE; HRESULT hr = SetTrait(_T("max"), dValue); if (FAILED(hr)) AfxThrowOleException(hr); pd->dMax = dValue; if (IsAutomaticUpdateEnabled()) Update(); return TRUE; } CString jPath = FormatJPath(nDimension, _T("max")); if (XTP_ISNAN(dValue)) { HRESULT hr = SetTrait(jPath, COleVariant()); if (FAILED(hr)) AfxThrowOleException(hr); pd->dMax = XTP_NAN; if (IsAutomaticUpdateEnabled()) Update(); return TRUE; } HRESULT hr = SetTrait(jPath, dValue); if (FAILED(hr)) AfxThrowOleException(hr); pd->dMax = dValue; if (IsAutomaticUpdateEnabled()) Update(); return TRUE; } double CXTPGaugeMeterType::GetCurrent(UINT nDimension) const { DimensionData* pd = const_cast(this)->GetDimData(nDimension); if (NULL == pd) return XTP_NAN; if (!XTP_ISNAN(pd->dCurrent)) return pd->dCurrent; COleVariant v; HRESULT hr = E_FAIL; if (UINT_MAX != nDimension) { hr = GetTrait(FormatJPath(nDimension, _T("current")), v); if (VT_NULL == v.vt) return XTP_NAN; } if (FAILED(hr)) hr = GetTrait(_T("current"), v); if (FAILED(hr)) AfxThrowOleException(hr); v.ChangeType(VT_R8); pd->dCurrent = v.dblVal; return pd->dCurrent; } BOOL CXTPGaugeMeterType::SetCurrent(UINT nDimension, double dValue) { DimensionData* pd = const_cast(this)->GetDimData(nDimension); if (NULL == pd) return FALSE; if (pd->dCurrent == dValue) return FALSE; if (!XTP_ISNAN(dValue)) { double dMin = GetMin(UINT_MAX); double dMax = GetMax(UINT_MAX); if (!XTP_ISNAN(dMin) && dValue < dMin) dValue = dMin; if (!XTP_ISNAN(dMax) && dMax < dValue) dValue = dMax; } if (UINT_MAX == nDimension) { if (!XTP_ASSERT_CHECK(!XTP_ISNAN(dValue))) return FALSE; HRESULT hr = SetTrait(_T("current"), dValue); if (FAILED(hr)) AfxThrowOleException(hr); pd->dCurrent = dValue; if (IsAutomaticUpdateEnabled()) Update(); return TRUE; } CString jPath = FormatJPath(nDimension, _T("current")); if (XTP_ISNAN(dValue)) { HRESULT hr = SetTrait(jPath, COleVariant()); if (FAILED(hr)) AfxThrowOleException(hr); pd->dCurrent = XTP_NAN; if (IsAutomaticUpdateEnabled()) Update(); return TRUE; } HRESULT hr = SetTrait(jPath, dValue); if (FAILED(hr)) AfxThrowOleException(hr); pd->dCurrent = dValue; if (IsAutomaticUpdateEnabled()) Update(); return TRUE; } UINT CXTPGaugeMeterType::GetDimensionCount() const { if (UINT_MAX != m_nDimCount) return m_nDimCount; COleVariant v; HRESULT hr = GetTrait(_T("dimensions.length"), v); if (FAILED(hr)) AfxThrowOleException(hr); v.ChangeType(VT_I4); const_cast(this)->m_nDimCount = static_cast(v.lVal); return m_nDimCount; } void CXTPGaugeMeterType::OnInit() { GetMin(); GetMax(); GetCurrent(); } CXTPGaugeMeterType::DimensionData::DimensionData() : dMin(XTP_NAN) , dMax(XTP_NAN) , dCurrent(XTP_NAN) { } CXTPGaugeMeterType::DimensionData* CXTPGaugeMeterType::GetDimData(UINT nDim) { if (UINT_MAX != nDim && GetDimensionCount() < nDim) return NULL; return &m_DimData[nDim]; } CString CXTPGaugeMeterType::FormatJPath(UINT nDim, LPCTSTR pRelJPath) { _ASSERTE(NULL != pRelJPath); if (UINT_MAX == nDim) return pRelJPath; CString jPath; jPath.Format(_T("dimensions[%u].%s"), nDim, pRelJPath); return jPath; }