Reconciled all changes by Rainy and the branch with MattKing's LocalFont function. The trunk now contains all current code changes.

Added changes to RANDOM function based on Rainy's input.  If there were multiple RANDOM statements in multiple CALC measure, they would all get the same random number.  Fixed.

Added new RANDOM function and LocalFont to the help files and History.htm
This commit is contained in:
jsmorley 2009-09-03 00:33:46 +00:00
parent 9b949a12e0
commit dc365cfb6e
5 changed files with 181 additions and 31 deletions

View File

@ -22,17 +22,12 @@
#include "Rainmeter.h"
#include <cstdlib>
#include <ctime>
#include <sstream>
using namespace std;
hqStrMap* CMeasureCalc::c_VarMap = NULL;
int CMeasureCalc::m_Loop = 0;
//======================================
//MattKing Code Edit :: Start
double CMeasureCalc::m_Random = 0;
double CMeasureCalc::m_LowBound = 0;
double CMeasureCalc::m_HighBound = 0;
//MattKing Code Edit :: End
/*
** CMeasureCalc
**
@ -42,11 +37,8 @@ double CMeasureCalc::m_HighBound = 0;
CMeasureCalc::CMeasureCalc(CMeterWindow* meterWindow) : CMeasure(meterWindow)
{
m_Parser = MathParser_Create(NULL);
//======================================
//MattKing Code Edit :: Start
srand((unsigned)time(0));
//MattKing Code Edit :: End
rand();
}
@ -78,6 +70,10 @@ bool CMeasureCalc::Update()
if (!CMeasure::PreUpdate()) return false;
m_Parser->Parameters = c_VarMap;
if(m_UpdateRandom > 0)
{
RandomFormulaReplace();
}
char* errMsg = MathParser_Parse(m_Parser, ConvertToAscii(m_Formula.c_str()).c_str(), &m_Value);
if (errMsg != NULL)
@ -114,7 +110,6 @@ void CMeasureCalc::UpdateVariableMap(CMeterWindow& meterWindow)
// Add the counter
double counter = meterWindow.GetUpdateCounter();
StrMap_AddString(c_VarMap, "Counter", &counter);
StrMap_AddString(c_VarMap, "Random", &m_Random);
}
/*
@ -128,17 +123,40 @@ void CMeasureCalc::ReadConfig(CConfigParser& parser, const WCHAR* section)
CMeasure::ReadConfig(parser, section);
m_Formula = parser.ReadString(section, L"Formula", L"");
// Hold onto the formula, we are going to change it
m_FormulaHolder = m_Formula;
//MattKing Code Edit :: Start
m_LowBound = parser.ReadFloat(section, L"LowBound", 95);
m_HighBound = parser.ReadFloat(section, L"HighBound", 400);
m_LowBound = parser.ReadFloat(section, L"LowBound", 0);
m_HighBound = parser.ReadFloat(section, L"HighBound", 100);
m_UpdateRandom = parser.ReadInt(section, L"UpdateRandom", 0);
int range = (m_HighBound - m_LowBound);
m_Random = m_LowBound + int(range * rand()/(RAND_MAX + 1.0));
m_Random = m_LowBound + int(range * rand()/(RAND_MAX + 1.0));
// For some reason I need two calls to really randomize the number
// I don't have a fix as I dont know much about random numbers
//MattKing Code Edit :: End
RandomFormulaReplace();
}
/*
** RandomFormulaReplace
**
** This replaces the word Random in m_Formula with a random number
**
*/
void CMeasureCalc::RandomFormulaReplace()
{
//To implement random numbers the word "Random" in the string
//formula is being replaced by the random number value
m_Formula = m_FormulaHolder;
int loc = m_Formula.find(L"Random");
while(loc > -1)
{
int range = (m_HighBound - m_LowBound);
int randNumber = m_LowBound + (range * rand()/(RAND_MAX + 1.0));
std::wstring randomNumberString= (L"");
wchar_t buf[8];
_itow(randNumber,buf,10);
randomNumberString.append(buf);
m_Formula.replace(loc, 6, randomNumberString);
loc = m_Formula.find(L"Random");
}
}

View File

@ -34,18 +34,18 @@ public:
static void UpdateVariableMap(CMeterWindow& meterWindow);
private:
void RandomFormulaReplace();
std::wstring m_Formula;
std::wstring m_FormulaHolder;
hqMathParser* m_Parser;
static hqStrMap* c_VarMap;
static int m_Loop;
//======================================
//MattKing Code Edit :: Start
static double m_Random;
static double m_LowBound;
static double m_HighBound;
//MattKing Code Edit :: End
int m_UpdateRandom;
int m_LowBound;
int m_HighBound;
};
#endif

View File

@ -109,11 +109,29 @@ void CMeterString::Initialize()
if(m_FontFamily) delete m_FontFamily;
m_FontFamily = new FontFamily(m_FontFace.c_str());
Status status = m_FontFamily->GetLastStatus();
//===================================================
/* Matt King Code */
// It couldn't find the font family
// Therefore we look in the privatefontcollection of this meters MeterWindow
if(Ok != status)
{
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;
}
}
/* Matt King end */
//===================================================
FontStyle style = FontStyleRegular;

View File

@ -165,6 +165,13 @@ CMeterWindow::~CMeterWindow()
if(m_Window) DestroyWindow(m_Window);
//===================================================
// Matt King Code
if(m_FontCollection) delete m_FontCollection;
// Matt King Code End
//===================================================
FreeLibrary(m_User32Library);
m_InstanceCount--;
@ -1406,6 +1413,103 @@ void CMeterWindow::ReadSkin()
m_WindowUpdate = m_Parser.ReadInt(L"Rainmeter", L"Update", m_WindowUpdate);
m_TransitionUpdate = m_Parser.ReadInt(L"Rainmeter", L"TransitionUpdate", m_TransitionUpdate);
//=====================================================
//Matt King Code
// Checking for localfonts
std::wstring localFont1 = m_Parser.ReadString(L"Rainmeter", L"LocalFont", L"");
// If there is a local font we want to load it
if(!localFont1.empty())
{
// We want to check the fonts folder first
// !!!!!!! - We may want to fix the method in which I get the path to
// Rainmeter/fonts
std::wstring szFontFile = m_Rainmeter->GetPath().c_str();
m_FontCollection = new Gdiplus::PrivateFontCollection();
int nResults = 0;
nResults = m_FontCollection->AddFontFile(szFontFile.c_str());
// It wasn't found in the fonts folder, check the local folder
if(nResults != Ok)
{
szFontFile = m_SkinPath; // Get the local path
szFontFile += m_SkinName;
szFontFile += L"\\";
szFontFile += localFont1;
nResults = m_FontCollection->AddFontFile(szFontFile.c_str());
// The font wasn't found, check full path.
if(nResults != Ok)
{
szFontFile = localFont1.c_str();
nResults = m_FontCollection->AddFontFile(szFontFile.c_str());
if(nResults != Ok)
{
std::wstring error = L"Error: Couldn't load font file: ";
error += localFont1;
DebugLog(error.c_str());
}
}
}
// Here we are checking to see if there are more than one local font
// to be loaded. They will be named LocalFont2, LocalFont 3, etc.
WCHAR tmpName[256];
int i = 2;
bool loop = true;
do
{
swprintf(tmpName, L"LocalFont%i", i);
std::wstring LocalFont = m_Parser.ReadString(L"Rainmeter", tmpName, L"");
// There is a key called LocalFont%i
if (!LocalFont.empty())
{
// We want to check the fonts folder first
// !!!!!!! - We may want to fix the method in which I get the path to
// Rainmeter/fonts
std::wstring szFontFile = m_Rainmeter->GetPath().c_str();
szFontFile += L"Fonts\\";
szFontFile += LocalFont;
int nResults = 0;
nResults = m_FontCollection->AddFontFile(szFontFile.c_str());
// It wasn't found in the fonts folder, check the local folder
if(nResults != Ok)
{
szFontFile = m_SkinPath; // Get the local path
szFontFile += m_SkinName;
szFontFile += LocalFont;
nResults = m_FontCollection->AddFontFile(szFontFile.c_str());
// The font wasn't found, check full path.
if(nResults != Ok)
{
szFontFile = LocalFont.c_str();
nResults = m_FontCollection->AddFontFile(szFontFile.c_str());
// The font file wasn't found anywhere, log the error
if(nResults != Ok)
{
std::wstring error = L"Error: Couldn't load font file: ";
error += LocalFont;
DebugLog(error.c_str());
}
}
}
}
// There were no extra Local Fonts found: exit loop.
else
{
loop = false;
}
i++;
} while(loop);
}
//Matt King code end
//=====================================================
// Create the meters and measures
// Get all the sections (i.e. different meters)

View File

@ -175,6 +175,11 @@ public:
std::wstring MakePathAbsolute(std::wstring path);
//========================================================
//MattKing Code
Gdiplus::PrivateFontCollection* GetPrivateFontCollection(){ return m_FontCollection; }
//MattKing Code End
protected:
static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
@ -316,6 +321,11 @@ private:
CRainmeter* m_Rainmeter; // Pointer to the main object
static int m_InstanceCount;
//==========================================================
//MattKing code
Gdiplus::PrivateFontCollection* m_FontCollection;
//MattKing end
};
#endif