2009-02-10 18:37:48 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2002 Kimmo Pekkola
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2009-10-07 16:45:14 +00:00
|
|
|
#include "StdAfx.h"
|
2009-02-10 18:37:48 +00:00
|
|
|
#include "MeterImage.h"
|
|
|
|
#include "Measure.h"
|
|
|
|
#include "Error.h"
|
|
|
|
#include "Rainmeter.h"
|
|
|
|
|
|
|
|
extern CRainmeter* Rainmeter;
|
|
|
|
|
|
|
|
using namespace Gdiplus;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** CMeterImage
|
|
|
|
**
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
2011-02-15 16:26:54 +00:00
|
|
|
CMeterImage::CMeterImage(CMeterWindow* meterWindow, const WCHAR* name) : CMeter(meterWindow, name),
|
2011-01-29 00:11:01 +00:00
|
|
|
m_NeedsReload(false),
|
|
|
|
m_WidthDefined(false),
|
|
|
|
m_HeightDefined(false),
|
|
|
|
m_PreserveAspectRatio(false),
|
|
|
|
m_Tile(false),
|
|
|
|
m_ScaleMargins()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** ~CMeterImage
|
|
|
|
**
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
CMeterImage::~CMeterImage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Initialize
|
|
|
|
**
|
|
|
|
** Load the image and get the dimensions of the meter from it.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CMeterImage::Initialize()
|
|
|
|
{
|
|
|
|
CMeter::Initialize();
|
|
|
|
|
2010-12-05 15:34:37 +00:00
|
|
|
if (!m_Measure && !m_DynamicVariables && !m_ImageName.empty())
|
|
|
|
{
|
|
|
|
m_ImageNameResult = m_Path;
|
|
|
|
m_ImageNameResult += m_ImageName;
|
|
|
|
m_ImageNameResult = m_MeterWindow->MakePathAbsolute(m_ImageNameResult);
|
|
|
|
LoadImage(m_ImageNameResult, true);
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-10-17 17:51:14 +00:00
|
|
|
** LoadImage
|
2009-02-10 18:37:48 +00:00
|
|
|
**
|
|
|
|
** Loads the image from disk
|
|
|
|
**
|
|
|
|
*/
|
2010-12-05 15:34:37 +00:00
|
|
|
void CMeterImage::LoadImage(const std::wstring& imageName, bool bLoadAlways)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-12-05 15:34:37 +00:00
|
|
|
m_Image.LoadImage(imageName, bLoadAlways);
|
2010-11-27 19:53:23 +00:00
|
|
|
|
|
|
|
if (m_Image.IsLoaded())
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-11-27 19:53:23 +00:00
|
|
|
// Calculate size of the meter
|
|
|
|
Bitmap* bitmap = m_Image.GetImage();
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2010-11-27 19:53:23 +00:00
|
|
|
int imageW = bitmap->GetWidth();
|
|
|
|
int imageH = bitmap->GetHeight();
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2010-11-27 19:53:23 +00:00
|
|
|
if (m_WidthDefined)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-11-27 19:53:23 +00:00
|
|
|
if (!m_HeightDefined)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-12-04 15:07:28 +00:00
|
|
|
m_H = (imageW == 0) ? 0 : (m_Tile) ? imageH : (int)(m_W * imageH / (double)imageW);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-27 19:53:23 +00:00
|
|
|
if (m_HeightDefined)
|
2010-02-24 14:58:35 +00:00
|
|
|
{
|
2010-12-04 15:07:28 +00:00
|
|
|
m_W = (imageH == 0) ? 0 : (m_Tile) ? imageW : (int)(m_H * imageW / (double)imageH);
|
2009-08-28 07:42:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-27 19:53:23 +00:00
|
|
|
m_W = imageW;
|
|
|
|
m_H = imageH;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-24 14:58:35 +00:00
|
|
|
}
|
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
/*
|
|
|
|
** ReadConfig
|
|
|
|
**
|
|
|
|
** Read the meter-specific configs from the ini-file.
|
|
|
|
**
|
|
|
|
*/
|
2011-02-15 16:26:54 +00:00
|
|
|
void CMeterImage::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
// Read common configs
|
2011-02-15 16:26:54 +00:00
|
|
|
CMeter::ReadConfig(parser, section);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2010-12-05 15:34:37 +00:00
|
|
|
// Check for extra measures
|
|
|
|
if (!m_Initialized && !m_MeasureName.empty())
|
|
|
|
{
|
|
|
|
ReadMeasureNames(parser, section, m_MeasureNames);
|
|
|
|
}
|
|
|
|
|
2009-09-04 16:37:51 +00:00
|
|
|
m_Path = parser.ReadString(section, L"Path", L"");
|
2009-07-24 08:08:52 +00:00
|
|
|
if (!m_Path.empty())
|
|
|
|
{
|
2010-08-06 07:40:43 +00:00
|
|
|
WCHAR ch = m_Path[m_Path.length() - 1];
|
|
|
|
if (ch != L'\\' && ch != L'/')
|
2009-07-24 08:08:52 +00:00
|
|
|
{
|
|
|
|
m_Path += L"\\";
|
|
|
|
}
|
|
|
|
}
|
2009-09-07 16:37:58 +00:00
|
|
|
|
2010-12-05 15:34:37 +00:00
|
|
|
m_ImageName = parser.ReadString(section, L"ImageName", L"");
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2009-09-04 16:37:51 +00:00
|
|
|
m_PreserveAspectRatio = 0!=parser.ReadInt(section, L"PreserveAspectRatio", 0);
|
2010-12-04 15:07:28 +00:00
|
|
|
m_Tile = 0!=parser.ReadInt(section, L"Tile", 0);
|
2009-08-28 07:42:28 +00:00
|
|
|
|
2010-12-13 08:19:45 +00:00
|
|
|
static const RECT defMargins = {0};
|
|
|
|
m_ScaleMargins = parser.ReadRECT(section, L"ScaleMargins", defMargins);
|
|
|
|
|
2010-12-04 15:07:28 +00:00
|
|
|
if (parser.IsValueDefined(section, L"W"))
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2009-08-28 07:42:28 +00:00
|
|
|
m_WidthDefined = true;
|
|
|
|
}
|
2010-12-04 15:07:28 +00:00
|
|
|
if (parser.IsValueDefined(section, L"H"))
|
2009-08-28 07:42:28 +00:00
|
|
|
{
|
|
|
|
m_HeightDefined = true;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2010-02-24 14:58:35 +00:00
|
|
|
|
2010-11-27 19:53:23 +00:00
|
|
|
// Read tinting configs
|
|
|
|
m_Image.ReadConfig(parser, section);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Update
|
|
|
|
**
|
|
|
|
** Updates the value(s) from the measures.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
bool CMeterImage::Update()
|
|
|
|
{
|
2009-09-07 16:37:58 +00:00
|
|
|
if (CMeter::Update())
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-12-05 15:34:37 +00:00
|
|
|
if (m_Measure || m_DynamicVariables)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-12-05 15:34:37 +00:00
|
|
|
// Store the current values so we know if the image needs to be updated
|
|
|
|
std::wstring oldResult = m_ImageNameResult;
|
|
|
|
|
|
|
|
if (m_Measure) // read from the measures
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-01-19 15:31:45 +00:00
|
|
|
std::wstring val = m_Measure->GetStringValue(AUTOSCALE_OFF, 1, 0, false);
|
2010-12-05 15:34:37 +00:00
|
|
|
|
|
|
|
if (m_ImageName.empty())
|
2009-09-07 16:37:58 +00:00
|
|
|
{
|
2010-12-05 15:34:37 +00:00
|
|
|
m_ImageNameResult = val;
|
2009-09-07 16:37:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-05 15:34:37 +00:00
|
|
|
std::vector<std::wstring> stringValues;
|
|
|
|
|
|
|
|
stringValues.push_back(val);
|
|
|
|
|
|
|
|
// Get the values for the other measures
|
2011-02-15 13:22:19 +00:00
|
|
|
for (size_t i = 0, isize = m_Measures.size(); i < isize; ++i)
|
2010-12-05 15:34:37 +00:00
|
|
|
{
|
2011-01-19 15:31:45 +00:00
|
|
|
stringValues.push_back(m_Measures[i]->GetStringValue(AUTOSCALE_OFF, 1, 0, false));
|
2010-12-05 15:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_ImageNameResult = m_ImageName;
|
|
|
|
if (!ReplaceMeasures(stringValues, m_ImageNameResult))
|
|
|
|
{
|
|
|
|
// ImageName doesn't contain any measures, so use the result of MeasureName.
|
|
|
|
m_ImageNameResult = val;
|
|
|
|
}
|
2009-09-07 16:37:58 +00:00
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2010-12-05 15:34:37 +00:00
|
|
|
else // read from the skin
|
2010-11-27 19:53:23 +00:00
|
|
|
{
|
2010-12-05 15:34:37 +00:00
|
|
|
m_ImageNameResult = m_ImageName;
|
2010-11-27 19:53:23 +00:00
|
|
|
}
|
2010-12-05 15:34:37 +00:00
|
|
|
|
|
|
|
if (!m_ImageNameResult.empty())
|
|
|
|
{
|
|
|
|
m_ImageNameResult.insert(0, m_Path);
|
|
|
|
m_ImageNameResult = m_MeterWindow->MakePathAbsolute(m_ImageNameResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadImage(m_ImageNameResult, oldResult != m_ImageNameResult);
|
2009-09-07 16:37:58 +00:00
|
|
|
return true;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Draw
|
|
|
|
**
|
|
|
|
** Draws the meter on the double buffer
|
|
|
|
**
|
|
|
|
*/
|
2009-07-27 11:48:57 +00:00
|
|
|
bool CMeterImage::Draw(Graphics& graphics)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-03-29 19:21:57 +00:00
|
|
|
if (!CMeter::Draw(graphics)) return false;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2010-11-27 19:53:23 +00:00
|
|
|
if (m_Image.IsLoaded())
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
// Copy the image over the doublebuffer
|
2010-11-27 19:53:23 +00:00
|
|
|
Bitmap* drawBitmap = m_Image.GetImage();
|
|
|
|
|
2010-02-24 14:58:35 +00:00
|
|
|
int imageW = drawBitmap->GetWidth();
|
|
|
|
int imageH = drawBitmap->GetHeight();
|
2009-08-28 07:42:28 +00:00
|
|
|
|
2010-12-04 15:07:28 +00:00
|
|
|
if (imageW == 0 || imageH == 0 || m_W == 0 || m_H == 0) return true;
|
2009-08-28 07:42:28 +00:00
|
|
|
|
2010-12-04 15:07:28 +00:00
|
|
|
int x = GetX();
|
|
|
|
int y = GetY();
|
2009-08-28 07:42:28 +00:00
|
|
|
|
2010-12-04 15:07:28 +00:00
|
|
|
int drawW = m_W;
|
|
|
|
int drawH = m_H;
|
2009-08-28 07:42:28 +00:00
|
|
|
|
2010-12-04 15:07:28 +00:00
|
|
|
if (m_Tile)
|
|
|
|
{
|
2010-12-13 08:19:45 +00:00
|
|
|
ImageAttributes imgAttr;
|
2010-12-04 15:07:28 +00:00
|
|
|
imgAttr.SetWrapMode(WrapModeTile);
|
2010-12-13 08:19:45 +00:00
|
|
|
|
|
|
|
Rect r(x, y, drawW, drawH);
|
|
|
|
graphics.DrawImage(drawBitmap, r, 0, 0, drawW, drawH, UnitPixel, &imgAttr);
|
2009-08-28 07:42:28 +00:00
|
|
|
}
|
2010-12-04 15:07:28 +00:00
|
|
|
else if (m_PreserveAspectRatio)
|
2009-08-28 07:42:28 +00:00
|
|
|
{
|
2010-12-04 15:07:28 +00:00
|
|
|
if (m_WidthDefined && m_HeightDefined)
|
|
|
|
{
|
|
|
|
REAL imageRatio = imageW / (REAL)imageH;
|
|
|
|
REAL meterRatio = m_W / (REAL)m_H;
|
|
|
|
|
|
|
|
if (imageRatio >= meterRatio)
|
|
|
|
{
|
|
|
|
drawW = m_W;
|
|
|
|
drawH = m_W * imageH / imageW;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
drawW = m_H * imageW / imageH;
|
|
|
|
drawH = m_H;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Centering
|
|
|
|
x += (m_W - drawW) / 2;
|
|
|
|
y += (m_H - drawH) / 2;
|
|
|
|
}
|
2010-12-13 08:19:45 +00:00
|
|
|
|
|
|
|
Rect r(x, y, drawW, drawH);
|
|
|
|
graphics.DrawImage(drawBitmap, r, 0, 0, imageW, imageH, UnitPixel);
|
2009-08-28 07:42:28 +00:00
|
|
|
}
|
2010-12-13 08:19:45 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const RECT m = m_ScaleMargins;
|
|
|
|
|
2011-03-29 19:21:57 +00:00
|
|
|
if (m.top > 0)
|
2010-12-13 08:19:45 +00:00
|
|
|
{
|
2011-03-29 19:21:57 +00:00
|
|
|
if (m.left > 0)
|
2010-12-13 08:19:45 +00:00
|
|
|
{
|
|
|
|
// Top-Left
|
|
|
|
Rect r(x, y, m.left, m.top);
|
|
|
|
graphics.DrawImage(drawBitmap, r, 0, 0, m.left, m.top, UnitPixel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Top
|
|
|
|
Rect r(x + m.left, y, drawW - m.left - m.right, m.top);
|
|
|
|
graphics.DrawImage(drawBitmap, r, m.left, 0, imageW - m.left - m.right, m.top, UnitPixel);
|
|
|
|
|
2011-03-29 19:21:57 +00:00
|
|
|
if (m.right > 0)
|
2010-12-13 08:19:45 +00:00
|
|
|
{
|
|
|
|
// Top-Right
|
|
|
|
Rect r(x + drawW - m.right, y, m.right, m.top);
|
|
|
|
graphics.DrawImage(drawBitmap, r, imageW - m.right, 0, m.right, m.top, UnitPixel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-29 19:21:57 +00:00
|
|
|
if (m.left > 0)
|
2010-12-13 08:19:45 +00:00
|
|
|
{
|
|
|
|
// Left
|
|
|
|
Rect r(x, y + m.top, m.left, drawH - m.top - m.bottom);
|
|
|
|
graphics.DrawImage(drawBitmap, r, 0, m.top, m.left, imageH - m.top - m.bottom, UnitPixel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Center
|
|
|
|
Rect r(x + m.left, y + m.top, drawW - m.left - m.right, drawH - m.top - m.bottom);
|
|
|
|
graphics.DrawImage(drawBitmap, r, m.left, m.top, imageW - m.left - m.right, imageH - m.top - m.bottom, UnitPixel);
|
|
|
|
|
2011-03-29 19:21:57 +00:00
|
|
|
if (m.right > 0)
|
2010-12-13 08:19:45 +00:00
|
|
|
{
|
|
|
|
// Right
|
|
|
|
Rect r(x + drawW - m.right, y + m.top, m.right, drawH - m.top - m.bottom);
|
|
|
|
graphics.DrawImage(drawBitmap, r, imageW - m.right, m.top, m.right, imageH - m.top - m.bottom, UnitPixel);
|
|
|
|
}
|
2011-03-29 19:21:57 +00:00
|
|
|
|
|
|
|
if (m.bottom > 0)
|
2010-12-13 08:19:45 +00:00
|
|
|
{
|
2011-03-29 19:21:57 +00:00
|
|
|
if (m.left > 0)
|
2010-12-13 08:19:45 +00:00
|
|
|
{
|
|
|
|
// Bottom-Left
|
|
|
|
Rect r(x, y + drawH - m.bottom, m.left, m.bottom);
|
|
|
|
graphics.DrawImage(drawBitmap, r, 0, imageH - m.bottom, m.left, m.bottom, UnitPixel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bottom
|
|
|
|
Rect r(x + m.left, y + drawH - m.bottom, drawW - m.left - m.right, m.bottom);
|
|
|
|
graphics.DrawImage(drawBitmap, r, m.left, imageH - m.bottom, imageW - m.left - m.right, m.bottom, UnitPixel);
|
2009-08-28 07:42:28 +00:00
|
|
|
|
2011-03-29 19:21:57 +00:00
|
|
|
if (m.right > 0)
|
2010-12-13 08:19:45 +00:00
|
|
|
{
|
|
|
|
// Bottom-Right
|
|
|
|
Rect r(x + drawW - m.right, y + drawH - m.bottom, m.right, m.bottom);
|
|
|
|
graphics.DrawImage(drawBitmap, r, imageW - m.right, imageH - m.bottom, m.right, m.bottom, UnitPixel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** BindMeasure
|
|
|
|
**
|
|
|
|
** Overridden method. The Image meters need not to be bound on anything
|
|
|
|
**
|
|
|
|
*/
|
2010-11-19 07:33:58 +00:00
|
|
|
void CMeterImage::BindMeasure(const std::list<CMeasure*>& measures)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-12-05 15:34:37 +00:00
|
|
|
if (m_MeasureName.empty()) return; // Allow NULL measure binding
|
|
|
|
|
|
|
|
CMeter::BindMeasure(measures);
|
|
|
|
|
|
|
|
std::vector<std::wstring>::const_iterator j = m_MeasureNames.begin();
|
|
|
|
for (; j != m_MeasureNames.end(); ++j)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-12-05 15:34:37 +00:00
|
|
|
// Go through the list and check it there is a secondary measures for us
|
|
|
|
std::list<CMeasure*>::const_iterator i = measures.begin();
|
2011-03-29 19:21:57 +00:00
|
|
|
for ( ; i != measures.end(); ++i)
|
2010-12-05 15:34:37 +00:00
|
|
|
{
|
2011-03-29 19:21:57 +00:00
|
|
|
if (_wcsicmp((*i)->GetName(), (*j).c_str()) == 0)
|
2010-12-05 15:34:37 +00:00
|
|
|
{
|
|
|
|
m_Measures.push_back(*i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == measures.end())
|
|
|
|
{
|
|
|
|
std::wstring error = L"The meter [" + m_Name;
|
|
|
|
error += L"] cannot be bound with [";
|
|
|
|
error += (*j);
|
|
|
|
error += L"]!";
|
|
|
|
throw CError(error, __LINE__, __FILE__);
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2010-12-05 15:34:37 +00:00
|
|
|
CMeter::SetAllMeasures(m_Measures);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|