From c7cd6125028f78c4e2bae203f1e8c57c65ac1a35 Mon Sep 17 00:00:00 2001 From: spx Date: Thu, 3 Jun 2010 14:14:53 +0000 Subject: [PATCH] Fixed the issue that dynamic image name is not correctly applied in most meters. (Issue 157) An error message is now output to the log instead of showing the dialog box. --- Library/MeterBar.cpp | 19 +++-- Library/MeterBitmap.cpp | 50 ++++++++---- Library/MeterButton.cpp | 69 ++++++++++------ Library/MeterHistogram.cpp | 160 ++++++++++++++++++++++++++++++++----- Library/MeterHistogram.h | 2 + Library/MeterImage.cpp | 13 +++ Library/MeterRotator.cpp | 12 ++- 7 files changed, 256 insertions(+), 69 deletions(-) diff --git a/Library/MeterBar.cpp b/Library/MeterBar.cpp index a660cb89..f08a296b 100644 --- a/Library/MeterBar.cpp +++ b/Library/MeterBar.cpp @@ -72,19 +72,24 @@ void CMeterBar::Initialize() Status status = m_Bitmap->GetLastStatus(); if(Ok != status) { + DebugLog(L"Bitmap image not found: %s", m_ImageName.c_str()); + delete m_Bitmap; m_Bitmap = NULL; - - throw CError(std::wstring(L"Bitmap image not found: ") + m_ImageName, __LINE__, __FILE__); } - - m_W = m_Bitmap->GetWidth(); - m_H = m_Bitmap->GetHeight(); + else + { + m_W = m_Bitmap->GetWidth(); + m_H = m_Bitmap->GetHeight(); + } } else { - if (m_Bitmap != NULL) delete m_Bitmap; - m_Bitmap = NULL; + if (m_Bitmap) + { + delete m_Bitmap; + m_Bitmap = NULL; + } } } diff --git a/Library/MeterBitmap.cpp b/Library/MeterBitmap.cpp index ea4dd29a..6ca2d820 100644 --- a/Library/MeterBitmap.cpp +++ b/Library/MeterBitmap.cpp @@ -75,22 +75,32 @@ void CMeterBitmap::Initialize() Status status = m_Bitmap->GetLastStatus(); if(Ok != status) { - delete m_Bitmap; + DebugLog(L"Bitmap image not found: %s", m_ImageName.c_str()); + + delete m_Bitmap; m_Bitmap = NULL; - - throw CError(std::wstring(L"Bitmap image not found: ") + m_ImageName, __LINE__, __FILE__); - } - - m_W = m_Bitmap->GetWidth(); - m_H = m_Bitmap->GetHeight(); - - if(m_H > m_W) - { - m_H = m_H / m_FrameCount; } else { - m_W = m_W / m_FrameCount; + m_W = m_Bitmap->GetWidth(); + m_H = m_Bitmap->GetHeight(); + + if(m_H > m_W) + { + m_H = m_H / m_FrameCount; + } + else + { + m_W = m_W / m_FrameCount; + } + } + } + else + { + if (m_Bitmap) + { + delete m_Bitmap; + m_Bitmap = NULL; } } } @@ -167,6 +177,8 @@ void CMeterBitmap::ReadConfig(const WCHAR* section) { // Store the current values so we know if the image needs to be updated std::wstring oldImageName = m_ImageName; + int oldW = m_W; + int oldH = m_H; // Read common configs CMeter::ReadConfig(section); @@ -205,10 +217,18 @@ void CMeterBitmap::ReadConfig(const WCHAR* section) throw CError(std::wstring(L"No such BitmapAlign: ") + align, __LINE__, __FILE__); } - if (m_Initialized && - oldImageName != m_ImageName) + if (m_Initialized) { - Initialize(); // Reload the image + if (oldImageName != m_ImageName) + { + Initialize(); // Reload the image + } + else + { + // Reset to old dimensions + m_W = oldW; + m_H = oldH; + } } } diff --git a/Library/MeterButton.cpp b/Library/MeterButton.cpp index 36ea9e18..0fcd7ac4 100644 --- a/Library/MeterButton.cpp +++ b/Library/MeterButton.cpp @@ -88,47 +88,64 @@ void CMeterButton::Initialize() } } if (m_Bitmap != NULL) delete m_Bitmap; - m_Bitmap = new Bitmap(m_ImageName.c_str()); Status status = m_Bitmap->GetLastStatus(); if(Ok != status) { + DebugLog(L"Bitmap image not found: %s", m_ImageName.c_str()); + delete m_Bitmap; m_Bitmap = NULL; - - throw CError(std::wstring(L"Bitmap image not found: ") + m_ImageName, __LINE__, __FILE__); - } - - m_W = m_Bitmap->GetWidth(); - m_H = m_Bitmap->GetHeight(); - - if(m_H > m_W) - { - m_H = m_H / BUTTON_FRAMES; } else { - m_W = m_W / BUTTON_FRAMES; - } + m_W = m_Bitmap->GetWidth(); + m_H = m_Bitmap->GetHeight(); - // Separate the frames - Graphics desktopGraphics(GetDesktopWindow()); - - for (int i = 0; i < BUTTON_FRAMES; ++i) - { - Bitmap bitmapPart(m_W, m_H, PixelFormat32bppARGB); - Graphics graphics(&bitmapPart); - Rect r(0, 0, m_W, m_H); - - if(m_Bitmap->GetHeight() > m_Bitmap->GetWidth()) + if(m_H > m_W) { - graphics.DrawImage(m_Bitmap, r, 0, m_H * i, m_W, m_H, UnitPixel); + m_H = m_H / BUTTON_FRAMES; } else { - graphics.DrawImage(m_Bitmap, r, m_W * i, 0, m_W, m_H, UnitPixel); + m_W = m_W / BUTTON_FRAMES; } - m_Bitmaps[i] = new CachedBitmap(&bitmapPart, &graphics); + + // Separate the frames + Graphics desktopGraphics(GetDesktopWindow()); + + for (int i = 0; i < BUTTON_FRAMES; ++i) + { + Bitmap bitmapPart(m_W, m_H, PixelFormat32bppARGB); + Graphics graphics(&bitmapPart); + Rect r(0, 0, m_W, m_H); + + if(m_Bitmap->GetHeight() > m_Bitmap->GetWidth()) + { + graphics.DrawImage(m_Bitmap, r, 0, m_H * i, m_W, m_H, UnitPixel); + } + else + { + graphics.DrawImage(m_Bitmap, r, m_W * i, 0, m_W, m_H, UnitPixel); + } + m_Bitmaps[i] = new CachedBitmap(&bitmapPart, &graphics); + } + } + } + else + { + for (int i = 0; i < BUTTON_FRAMES; ++i) + { + if (m_Bitmaps[i]) + { + delete m_Bitmaps[i]; + m_Bitmaps[i] = NULL; + } + } + if (m_Bitmap) + { + delete m_Bitmap; + m_Bitmap = NULL; } } } diff --git a/Library/MeterHistogram.cpp b/Library/MeterHistogram.cpp index 09f5dfc6..772dc912 100644 --- a/Library/MeterHistogram.cpp +++ b/Library/MeterHistogram.cpp @@ -50,6 +50,7 @@ CMeterHistogram::CMeterHistogram(CMeterWindow* meterWindow) : CMeter(meterWindow m_MinPrimaryValue = 0.0; m_MaxSecondaryValue = 1.0; m_MinSecondaryValue = 0.0; + m_WidthChanged = false; } /* @@ -86,11 +87,31 @@ void CMeterHistogram::Initialize() Status status = m_PrimaryBitmap->GetLastStatus(); if(Ok != status) { - throw CError(std::wstring(L"PrimaryImage not found: ") + m_PrimaryImageName, __LINE__, __FILE__); - } + DebugLog(L"PrimaryImage not found: %s", m_PrimaryImageName.c_str()); - m_W = m_PrimaryBitmap->GetWidth(); - m_H = m_PrimaryBitmap->GetHeight(); + delete m_PrimaryBitmap; + m_PrimaryBitmap = NULL; + } + else + { + int oldW = m_W; + + m_W = m_PrimaryBitmap->GetWidth(); + m_H = m_PrimaryBitmap->GetHeight(); + + if (oldW != m_W) + { + m_WidthChanged = true; + } + } + } + else + { + if (m_PrimaryBitmap) + { + delete m_PrimaryBitmap; + m_PrimaryBitmap = NULL; + } } if(!m_SecondaryImageName.empty()) @@ -100,7 +121,18 @@ void CMeterHistogram::Initialize() Status status = m_SecondaryBitmap->GetLastStatus(); if(Ok != status) { - throw CError(std::wstring(L"SecondaryImage not found: ") + m_SecondaryImageName, __LINE__, __FILE__); + DebugLog(L"SecondaryImage not found: %s", m_SecondaryImageName.c_str()); + + delete m_SecondaryBitmap; + m_SecondaryBitmap = NULL; + } + } + else + { + if (m_SecondaryBitmap) + { + delete m_SecondaryBitmap; + m_SecondaryBitmap = NULL; } } @@ -111,25 +143,81 @@ void CMeterHistogram::Initialize() Status status = m_BothBitmap->GetLastStatus(); if(Ok != status) { - throw CError(std::wstring(L"BothImage not found: ") + m_BothImageName, __LINE__, __FILE__); + DebugLog(L"BothImage not found: %s", m_BothImageName.c_str()); + + delete m_BothBitmap; + m_BothBitmap = NULL; + } + } + else + { + if (m_BothBitmap) + { + delete m_BothBitmap; + m_BothBitmap = NULL; } } // A sanity check if (m_SecondaryMeasure && !m_PrimaryImageName.empty() && (m_BothImageName.empty() || m_SecondaryImageName.empty())) { - throw CError(std::wstring(L"You need to define SecondaryImage and BothImage also!"), __LINE__, __FILE__); + LSLog(LOG_DEBUG, L"Rainmeter", L"You need to define SecondaryImage and BothImage also!"); + + if (m_PrimaryBitmap) + { + delete m_PrimaryBitmap; + m_PrimaryBitmap = NULL; + } + if (m_SecondaryBitmap) + { + delete m_SecondaryBitmap; + m_SecondaryBitmap = NULL; + } + if (m_BothBitmap) + { + delete m_BothBitmap; + m_BothBitmap = NULL; + } } - // Create buffers for values - if (m_PrimaryValues) delete [] m_PrimaryValues; - m_PrimaryValues = new double[m_W]; - memset(m_PrimaryValues, 0, sizeof(double) * m_W); - if (m_SecondaryMeasure) + if ((!m_PrimaryImageName.empty() && !m_PrimaryBitmap) || + (!m_SecondaryImageName.empty() && !m_SecondaryBitmap) || + (!m_BothImageName.empty() && !m_BothBitmap)) { - if (m_SecondaryValues) delete [] m_SecondaryValues; - m_SecondaryValues = new double[m_W]; - memset(m_SecondaryValues, 0, sizeof(double) * m_W); + // Reset current position + m_MeterPos = 0; + + // Delete buffers + if (m_PrimaryValues) + { + delete [] m_PrimaryValues; + m_PrimaryValues = NULL; + } + if (m_SecondaryValues) + { + delete [] m_SecondaryValues; + m_SecondaryValues = NULL; + } + + m_WidthChanged = false; + } + else if (m_WidthChanged) + { + // Reset current position + m_MeterPos = 0; + + // Create buffers for values + if (m_PrimaryValues) delete [] m_PrimaryValues; + m_PrimaryValues = new double[m_W]; + memset(m_PrimaryValues, 0, sizeof(double) * m_W); + if (m_SecondaryMeasure) + { + if (m_SecondaryValues) delete [] m_SecondaryValues; + m_SecondaryValues = new double[m_W]; + memset(m_SecondaryValues, 0, sizeof(double) * m_W); + } + + m_WidthChanged = false; } } @@ -141,6 +229,13 @@ void CMeterHistogram::Initialize() */ void CMeterHistogram::ReadConfig(const WCHAR* section) { + // Store the current values so we know if the image needs to be updated + std::wstring oldPrimaryImageName = m_PrimaryImageName; + std::wstring oldSecondaryImageName = m_SecondaryImageName; + std::wstring oldBothImageName = m_BothImageName; + int oldW = m_W; + int oldH = m_H; + // Read common configs CMeter::ReadConfig(section); @@ -163,6 +258,31 @@ void CMeterHistogram::ReadConfig(const WCHAR* section) m_Autoscale = 0!=parser.ReadInt(section, L"AutoScale", 0); m_Flip = 0!=parser.ReadInt(section, L"Flip", 0); + + if (m_Initialized) + { + if (m_PrimaryImageName.empty()) + { + if (oldW != m_W) + { + m_WidthChanged = true; + Initialize(); // Reload the image + } + } + else + { + // Reset to old dimensions + m_W = oldW; + m_H = oldH; + + if (oldPrimaryImageName != m_PrimaryImageName || + oldSecondaryImageName != m_SecondaryImageName || + oldBothImageName != m_BothImageName) + { + Initialize(); // Reload the image + } + } + } } /* @@ -173,12 +293,12 @@ void CMeterHistogram::ReadConfig(const WCHAR* section) */ bool CMeterHistogram::Update() { - if (CMeter::Update() && m_Measure) + if (CMeter::Update() && m_Measure && m_PrimaryValues) { // Gather values m_PrimaryValues[m_MeterPos] = m_Measure->GetValue(); - if (m_SecondaryMeasure != NULL) + if (m_SecondaryMeasure && m_SecondaryValues) { m_SecondaryValues[m_MeterPos] = m_SecondaryMeasure->GetValue(); } @@ -213,7 +333,7 @@ bool CMeterHistogram::Update() m_MaxPrimaryValue *= 2; } - if (m_SecondaryMeasure) + if (m_SecondaryMeasure && m_SecondaryValues) { for (int i = 0; i != m_W; ++i) { @@ -241,7 +361,9 @@ bool CMeterHistogram::Update() */ bool CMeterHistogram::Draw(Graphics& graphics) { - if(!CMeter::Draw(graphics)) return false; + if(!CMeter::Draw(graphics) || + (m_Measure && !m_PrimaryValues) || + (m_SecondaryMeasure && !m_SecondaryValues)) return false; Pen primaryPen(m_PrimaryColor); Pen secondaryPen(m_SecondaryColor); diff --git a/Library/MeterHistogram.h b/Library/MeterHistogram.h index 32c92393..a861fd14 100644 --- a/Library/MeterHistogram.h +++ b/Library/MeterHistogram.h @@ -60,6 +60,8 @@ private: double m_MinPrimaryValue; double m_MaxSecondaryValue; double m_MinSecondaryValue; + + bool m_WidthChanged; }; #endif diff --git a/Library/MeterImage.cpp b/Library/MeterImage.cpp index cd1f7598..2d6eb142 100644 --- a/Library/MeterImage.cpp +++ b/Library/MeterImage.cpp @@ -250,6 +250,19 @@ void CMeterImage::LoadImage(bool bLoadAlways) } } } + else + { + if (m_Bitmap) + { + delete m_Bitmap; + m_Bitmap = NULL; + } + if (m_BitmapTint) + { + delete m_BitmapTint; + m_BitmapTint = NULL; + } + } } /* diff --git a/Library/MeterRotator.cpp b/Library/MeterRotator.cpp index 80fd1a8c..3584f706 100644 --- a/Library/MeterRotator.cpp +++ b/Library/MeterRotator.cpp @@ -67,11 +67,19 @@ void CMeterRotator::Initialize() m_Bitmap = new Bitmap(m_ImageName.c_str()); Status status = m_Bitmap->GetLastStatus(); if(Ok != status) + { + DebugLog(L"Bitmap image not found: %s", m_ImageName.c_str()); + + delete m_Bitmap; + m_Bitmap = NULL; + } + } + else + { + if (m_Bitmap) { delete m_Bitmap; m_Bitmap = NULL; - - throw CError(std::wstring(L"Bitmap image not found: ") + m_ImageName, __LINE__, __FILE__); } } }