1
0
mirror of https://github.com/chibicitiberiu/rainmeter-studio.git synced 2024-02-24 04:33:31 +00:00
rainmeter-studio/Library/lua/glue/LuaMeterWindow.cpp

239 lines
4.9 KiB
C++
Raw Normal View History

/*
Copyright (C) 2010 Matt King, Birunthan Mohanathas
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "../../StdAfx.h"
#include "../LuaManager.h"
#include "../../Rainmeter.h"
#include "../../MeterWindow.h"
#include "../../MeterString.h"
extern CRainmeter* Rainmeter;
#define DECLARE_SELF(L) \
void* selfData = lua_touserdata(L, 1); \
if (!selfData) return 0; \
CMeterWindow* self = *(CMeterWindow**)selfData;
static int Bang(lua_State* L)
{
DECLARE_SELF(L)
2012-03-08 15:46:36 +00:00
CConfigParser& parser = self->GetParser();
std::wstring bang = LuaManager::ToWide(L, 2);
int top = lua_gettop(L);
if (top == 2) // 1 argument
{
parser.ReplaceVariables(bang);
Rainmeter->ExecuteCommand(bang.c_str(), self);
}
else
{
const WCHAR* bangSz = bang.c_str();
if (*bangSz == L'!')
{
++bangSz; // Skip "!"
std::vector<std::wstring> args;
for (int i = 3; i <= top; ++i)
{
std::wstring tmpSz = LuaManager::ToWide(L, i);
parser.ReplaceVariables(tmpSz);
args.push_back(tmpSz);
}
Rainmeter->ExecuteBang(bangSz, args, self);
}
}
return 0;
}
static int GetMeter(lua_State* L)
{
DECLARE_SELF(L)
2012-03-08 15:46:36 +00:00
const std::wstring meterName = LuaManager::ToWide(L, 2);
2012-03-08 15:46:36 +00:00
CMeter* meter = self->GetMeter(meterName);
if (meter)
2012-03-08 15:46:36 +00:00
{
*(CMeter**)lua_newuserdata(L, sizeof(CMeter*)) = meter;
lua_getglobal(L, "CMeter");
lua_setmetatable(L, -2);
}
else
{
lua_pushnil(L);
2012-03-08 15:46:36 +00:00
}
return 1;
}
static int GetMeasure(lua_State* L)
{
DECLARE_SELF(L)
2012-03-08 15:46:36 +00:00
const std::wstring measureName = LuaManager::ToWide(L, 2);
CMeasure* measure = self->GetMeasure(measureName);
if (measure)
{
*(CMeasure**)lua_newuserdata(L, sizeof(CMeasure*)) = measure;
lua_getglobal(L, "CMeasure");
lua_setmetatable(L, -2);
}
else
{
lua_pushnil(L);
}
return 1;
}
static int GetVariable(lua_State* L)
{
DECLARE_SELF(L)
2012-03-08 15:46:36 +00:00
std::wstring strTmp = LuaManager::ToWide(L, 2);
2012-08-05 13:12:36 +00:00
const std::wstring* value = self->GetParser().GetVariable(strTmp);
if (value)
2012-03-08 15:46:36 +00:00
{
2012-08-05 13:12:36 +00:00
LuaManager::PushWide(L, (*value).c_str());
}
else
{
lua_pushnil(L);
2012-03-08 15:46:36 +00:00
}
return 1;
}
static int ReplaceVariables(lua_State* L)
{
DECLARE_SELF(L)
2012-03-08 15:46:36 +00:00
std::wstring strTmp = LuaManager::ToWide(L, 2);
self->GetParser().ReplaceVariables(strTmp);
self->GetParser().ReplaceMeasures(strTmp);
2012-03-08 15:46:36 +00:00
LuaManager::PushWide(L, strTmp.c_str());
return 1;
}
static int ParseFormula(lua_State* L)
{
DECLARE_SELF(L)
2012-03-08 15:46:36 +00:00
std::wstring strTmp = LuaManager::ToWide(L, 2);
2012-03-08 15:46:36 +00:00
double result;
if (!self->GetParser().ParseFormula(strTmp, &result))
2012-03-08 15:46:36 +00:00
{
result = lua_tonumber(L, 2);
2012-03-08 15:46:36 +00:00
}
lua_pushnumber(L, result);
return 1;
}
static int MoveWindow(lua_State* L)
{
DECLARE_SELF(L)
int x = (int)lua_tonumber(L, 2);
int y = (int)lua_tonumber(L, 3);
2012-03-08 15:46:36 +00:00
self->MoveWindow(x, y);
return 0;
}
static int FadeWindow(lua_State* L)
{
DECLARE_SELF(L)
int from = (int)lua_tonumber(L, 2);
int to = (int)lua_tonumber(L, 3);
2012-03-08 15:46:36 +00:00
self->FadeWindow(from, to);
2012-03-08 15:46:36 +00:00
return 0;
}
static int GetW(lua_State* L)
{
DECLARE_SELF(L)
lua_pushnumber(L, self->GetW());
return 1;
}
static int GetH(lua_State* L)
{
DECLARE_SELF(L)
lua_pushnumber(L, self->GetH());
return 1;
}
static int GetX(lua_State* L)
{
DECLARE_SELF(L)
lua_pushnumber(L, self->GetX());
2012-03-08 15:46:36 +00:00
return 1;
}
static int GetY(lua_State* L)
{
DECLARE_SELF(L)
lua_pushnumber(L, self->GetY());
return 1;
}
static int MakePathAbsolute(lua_State* L)
{
DECLARE_SELF(L)
2012-03-08 15:46:36 +00:00
std::wstring path = LuaManager::ToWide(L, 2);
self->MakePathAbsolute(path);
LuaManager::PushWide(L, path.c_str());
2012-03-08 15:46:36 +00:00
return 1;
}
void LuaManager::RegisterMeterWindow(lua_State* L)
{
2012-03-09 13:19:49 +00:00
const luaL_Reg functions[] =
{
{ "Bang", Bang },
{ "GetMeter", GetMeter },
{ "GetMeasure", GetMeasure },
{ "GetVariable", GetVariable },
{ "ReplaceVariables", ReplaceVariables },
{ "ParseFormula", ParseFormula },
{ "MoveWindow", MoveWindow },
{ "FadeWindow", FadeWindow },
{ "GetW", GetW },
{ "GetH", GetH },
{ "GetX", GetX },
{ "GetY", GetY },
{ "MakePathAbsolute", MakePathAbsolute },
{ NULL, NULL }
};
luaL_register(L, "CMeterWindow", functions);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_pop(L, 1);
}