Added cache for the fonts.

Fixed a memory leak in the string meter.
Changed the library project to use precompiled headers.
This commit is contained in:
Kimmo Pekkola 2009-10-07 16:45:14 +00:00
parent 61df4e1bad
commit 0b4ba32924
38 changed files with 595 additions and 335 deletions

View File

@ -18,6 +18,9 @@
#pragma warning(disable : 4996)
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include "resource.h"
#include "..\Library\Rainmeter.h"
@ -50,6 +53,9 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
WCHAR* WinName = L"Rainmeter control window";
HWND hWnd;
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
// _CrtSetBreakAlloc(5055);
if(!hPrevInstance)
{
if (!InitApplication(hInstance, WinClass)) return FALSE;

View File

@ -16,9 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "Rainmeter.h"
#include "MeterWindow.h"
#include "Measure.h"

View File

@ -16,9 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "ConfigParser.h"
#include "Litestep.h"
#include "Rainmeter.h"

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "Error.h"
#include <stdio.h>

File diff suppressed because it is too large Load Diff

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "Litestep.h"
#include "Error.h"
#include "Rainmeter.h"

View File

@ -18,6 +18,7 @@
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "Measure.h"
#include "MeasureCPU.h"
#include "MeasureMemory.h"

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasureCPU.h"
#include "Rainmeter.h"

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasureCalc.h"
#include "Rainmeter.h"
#include <cstdlib>

View File

@ -18,6 +18,7 @@
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasureDiskSpace.h"
#include "Rainmeter.h"

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasureMemory.h"
/*

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasureNet.h"
#include "Rainmeter.h"

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasureNetIn.h"
/*

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasureNetOut.h"
/*

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasureNetTotal.h"
/*

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasurePhysicalMemory.h"
/*

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasurePlugin.h"
#include "Rainmeter.h"
#include "Error.h"

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasureRegistry.h"
#include "Rainmeter.h"
#include "Error.h"

View File

@ -18,6 +18,7 @@
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasureTime.h"
#include "Rainmeter.h"
#include <time.h>

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasureUptime.h"
#include "Rainmeter.h"
#include <time.h>

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeasureVirtualMemory.h"
/*

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "Error.h"
#include "Meter.h"
#include "MeterBitmap.h"

View File

@ -16,9 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeterBar.h"
#include "Measure.h"
#include "Error.h"

View File

@ -16,9 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeterBitmap.h"
#include "Measure.h"
#include "Error.h"

View File

@ -16,9 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeterButton.h"
#include "Measure.h"
#include "Rainmeter.h"

View File

@ -16,9 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeterHistogram.h"
#include "Measure.h"
#include "Error.h"
@ -83,6 +81,7 @@ void CMeterHistogram::Initialize()
// Load the bitmaps if defined
if(!m_PrimaryImageName.empty())
{
if (m_PrimaryBitmap) delete m_PrimaryBitmap;
m_PrimaryBitmap = new Bitmap(m_PrimaryImageName.c_str());
Status status = m_PrimaryBitmap->GetLastStatus();
if(Ok != status)
@ -96,6 +95,7 @@ void CMeterHistogram::Initialize()
if(!m_SecondaryImageName.empty())
{
if (m_SecondaryBitmap) delete m_SecondaryBitmap;
m_SecondaryBitmap = new Bitmap(m_SecondaryImageName.c_str());
Status status = m_SecondaryBitmap->GetLastStatus();
if(Ok != status)
@ -106,6 +106,7 @@ void CMeterHistogram::Initialize()
if(!m_BothImageName.empty())
{
if (m_BothBitmap) delete m_BothBitmap;
m_BothBitmap = new Bitmap(m_BothImageName.c_str());
Status status = m_BothBitmap->GetLastStatus();
if(Ok != status)
@ -121,10 +122,12 @@ void CMeterHistogram::Initialize()
}
// 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);
}

View File

@ -16,9 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeterImage.h"
#include "Measure.h"
#include "Error.h"

View File

@ -16,9 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeterLine.h"
#include "Measure.h"
#include "Error.h"

View File

@ -16,9 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeterRotator.h"
#include "Measure.h"
#include "Error.h"

View File

@ -16,8 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeterRoundLine.h"
#include "Measure.h"
#include "Error.h"

View File

@ -16,9 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "MeterString.h"
#include "Rainmeter.h"
#include "Measure.h"
@ -26,6 +24,9 @@
using namespace Gdiplus;
std::map<std::wstring, Gdiplus::FontFamily*> CMeterString::c_FontFamilies;
std::map<std::wstring, Gdiplus::Font*> CMeterString::c_Fonts;
/*
** CMeterString
**
@ -60,8 +61,6 @@ CMeterString::CMeterString(CMeterWindow* meterWindow) : CMeter(meterWindow)
*/
CMeterString::~CMeterString()
{
if(m_Font) delete m_Font;
if(m_FontFamily) delete m_FontFamily;
}
/*
@ -106,28 +105,39 @@ void CMeterString::Initialize()
{
CMeter::Initialize();
if(m_FontFamily) delete m_FontFamily;
m_FontFamily = new FontFamily(m_FontFace.c_str());
Status status = m_FontFamily->GetLastStatus();
// It couldn't find the font family
// Therefore we look in the privatefontcollection of this meters MeterWindow
if(Ok != status)
// Check if the font family is in the cache and use it
std::map<std::wstring, Gdiplus::FontFamily*>::iterator iter = c_FontFamilies.find(m_FontFace);
if (iter != c_FontFamilies.end())
{
m_FontFamily = new FontFamily(m_FontFace.c_str(), m_MeterWindow->GetPrivateFontCollection());
status = m_FontFamily->GetLastStatus();
m_FontFamily = (*iter).second;
}
else
{
m_FontFamily = new FontFamily(m_FontFace.c_str());
Status status = m_FontFamily->GetLastStatus();
// It couldn't find the font family: Log it.
// It couldn't find the font family
// Therefore we look in the privatefontcollection of this meters MeterWindow
if(Ok != status)
{
std::wstring error = L"Error: Couldn't load font family: ";
error += m_FontFace;
DebugLog(error.c_str());
{
delete m_FontFamily;
m_FontFamily = NULL;
m_FontFamily = new FontFamily(m_FontFace.c_str(), m_MeterWindow->GetPrivateFontCollection());
status = m_FontFamily->GetLastStatus();
// It couldn't find the font family: Log it.
if(Ok != status)
{
std::wstring error = L"Error: Couldn't load font family: ";
error += m_FontFace;
DebugLog(error.c_str());
delete m_FontFamily;
m_FontFamily = NULL;
}
}
c_FontFamilies[m_FontFace] = m_FontFamily;
}
FontStyle style = FontStyleRegular;
@ -154,18 +164,25 @@ void CMeterString::Initialize()
REAL size = (REAL)m_FontSize * (96.0f / (REAL)dpi);
if (m_FontFamily)
std::map<std::wstring, Gdiplus::Font*>::iterator iter2 = c_Fonts.find(FontPropertiesToString(size, style));
if (iter2 != c_Fonts.end())
{
if(m_Font) delete m_Font;
m_Font = new Gdiplus::Font(m_FontFamily, size, style);
m_Font = (*iter2).second;
}
else
{
if(m_Font) delete m_Font;
m_Font = new Gdiplus::Font(FontFamily::GenericSansSerif(), size, style);
if (m_FontFamily)
{
m_Font = new Gdiplus::Font(m_FontFamily, size, style);
}
else
{
m_Font = new Gdiplus::Font(FontFamily::GenericSansSerif(), size, style);
}
c_Fonts[FontPropertiesToString(size, style)] = m_Font;
}
status = m_Font->GetLastStatus();
Status status = m_Font->GetLastStatus();
if(Ok != status)
{
throw CError(std::wstring(L"Unable to create font: ") + m_FontFace, __LINE__, __FILE__);
@ -192,6 +209,8 @@ void CMeterString::ReadConfig(const WCHAR* section)
CConfigParser& parser = m_MeterWindow->GetParser();
m_MeasureNames.clear();
// Check for extra measures
int i = 2;
bool loop = true;
@ -530,3 +549,41 @@ void CMeterString::BindMeasure(std::list<CMeasure*>& measures)
}
}
}
/*
** FreeFontCache
**
** Static function which frees the font cache.
**
*/
void CMeterString::FreeFontCache()
{
std::map<std::wstring, Gdiplus::FontFamily*>::iterator iter = c_FontFamilies.begin();
for ( ; iter != c_FontFamilies.end(); iter++)
{
delete (*iter).second;
}
c_FontFamilies.clear();
std::map<std::wstring, Gdiplus::Font*>::iterator iter2 = c_Fonts.begin();
for ( ; iter2 != c_Fonts.end(); iter2++)
{
delete (*iter2).second;
}
c_Fonts.clear();
}
/*
** FontPropertiesToString
**
** Static helper to convert font properties to a string so it can be used as a key for the cache map.
**
*/
std::wstring CMeterString::FontPropertiesToString(REAL size, FontStyle style)
{
std::wstringstream stream;
stream << size << L"-" << (int)style;
return stream.str();
}
// EOF

View File

@ -40,6 +40,8 @@ public:
virtual bool Draw(Gdiplus::Graphics& graphics);
virtual void BindMeasure(std::list<CMeasure*>& measures);
static void FreeFontCache();
private:
enum TEXTSTYLE
{
@ -83,6 +85,10 @@ private:
std::vector<std::wstring> m_MeasureNames;
std::vector<CMeasure*> m_Measures;
static std::wstring FontPropertiesToString(Gdiplus::REAL size, Gdiplus::FontStyle style);
static std::map<std::wstring, Gdiplus::FontFamily*> c_FontFamilies; // Cache for the font families
static std::map<std::wstring, Gdiplus::Font*> c_Fonts; // Cache for the fonts
};
#endif

View File

@ -16,9 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include <windows.h>
#include <gdiplus.h>
#include "MeterWindow.h"
@ -1551,11 +1549,14 @@ void CMeterWindow::ReadSkin()
{
// It's a measure
CMeasure* measure = CMeasure::Create(measureName.c_str(), this);
measure->SetName(strSection.c_str());
measure->ReadConfig(m_Parser, strSection.c_str());
m_Measures.push_back(measure);
if (measure)
{
measure->SetName(strSection.c_str());
measure->ReadConfig(m_Parser, strSection.c_str());
m_Measures.push_back(measure);
m_Parser.AddMeasure(measure);
m_Parser.AddMeasure(measure);
}
}
catch (CError& error)
{
@ -1568,9 +1569,12 @@ void CMeterWindow::ReadSkin()
{
// It's a meter
CMeter* meter = CMeter::Create(meterName.c_str(), this);
meter->SetName(strSection.c_str());
meter->ReadConfig(strSection.c_str());
m_Meters.push_back(meter);
if (meter)
{
meter->SetName(strSection.c_str());
meter->ReadConfig(strSection.c_str());
m_Meters.push_back(meter);
}
}
catch (CError& error)
{

View File

@ -16,13 +16,12 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4996)
#pragma warning(disable: 4786)
#include "StdAfx.h"
#include "Rainmeter.h"
#include "Error.h"
#include "AboutDialog.h"
#include "MeasureNet.h"
#include "MeterString.h"
#include "Resource.h"
#include "UpdateCheck.h"
#include <assert.h>
@ -656,6 +655,8 @@ CRainmeter::~CRainmeter()
WriteStats(true);
CMeterString::FreeFontCache();
GdiplusShutdown(m_GDIplusToken);
}

19
Library/StdAfx.cpp Normal file
View File

@ -0,0 +1,19 @@
/*
Copyright (C) 2001 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.
*/
#include "StdAfx.h"

67
Library/StdAfx.h Normal file
View File

@ -0,0 +1,67 @@
/*
Copyright (C) 2009 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.
*/
#ifndef __STDAFX_H__
#define __STDAFX_H__
#define _WIN32_IE 0x0600
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
// WINAPI
#include <windows.h>
#include <gdiplus.h>
#include <comdef.h>
#include <Iphlpapi.h>
#include <commctrl.h>
#include <shellapi.h>
#include <algorithm>
#include <Mmsystem.h>
#include <tchar.h>
#include <shlobj.h>
#include <winperf.h>
#include <Windns.h>
#include <Ipexport.h>
#include <Powrprof.h>
// STL
#include <map>
#include <string>
#include <vector>
#include <hash_map>
#include <list>
#include <sstream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <iostream>
// RUNTIME
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <io.h>
#endif

View File

@ -16,10 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#define _WIN32_IE 0x0600
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include "TrayWindow.h"
#include "Resource.h"
#include "Litestep.h"

View File

@ -16,9 +16,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma warning(disable: 4786)
#pragma warning(disable: 4996)
#include "StdAfx.h"
#include <windows.h>
#include "Litestep.h"
#include "Rainmeter.h"