rainmeter-studio/Library/lua/glue/LuaMeterWindow.cpp

241 lines
4.8 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"
#define DECLARE_SELF(L) \
void* selfData = lua_touserdata(L, 1); \
if (!selfData) return 0; \
2013-05-31 14:18:52 +00:00
MeterWindow* self = *(MeterWindow**)selfData;
static int Bang(lua_State* L)
{
DECLARE_SELF(L)
2013-05-31 14:18:52 +00:00
ConfigParser& parser = self->GetParser();
std::wstring bang = LuaManager::ToWide(2);
int top = lua_gettop(L);
if (top == 2) // 1 argument
{
parser.ReplaceVariables(bang);
GetRainmeter().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(i);
parser.ReplaceVariables(tmpSz);
args.push_back(tmpSz);
}
GetRainmeter().ExecuteBang(bangSz, args, self);
}
}
return 0;
}
static int GetMeter(lua_State* L)
{
DECLARE_SELF(L)
const std::wstring meterName = LuaManager::ToWide(2);
2013-05-31 14:18:52 +00:00
Meter* meter = self->GetMeter(meterName);
if (meter)
2012-03-08 15:46:36 +00:00
{
2013-05-31 14:18:52 +00:00
*(Meter**)lua_newuserdata(L, sizeof(Meter*)) = meter;
lua_getglobal(L, "Meter");
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)
const std::wstring measureName = LuaManager::ToWide(2);
2012-03-08 15:46:36 +00:00
2013-05-31 14:18:52 +00:00
Measure* measure = self->GetMeasure(measureName);
if (measure)
{
2013-05-31 14:18:52 +00:00
*(Measure**)lua_newuserdata(L, sizeof(Measure*)) = measure;
lua_getglobal(L, "Measure");
lua_setmetatable(L, -2);
}
else
{
lua_pushnil(L);
}
return 1;
}
static int GetVariable(lua_State* L)
{
DECLARE_SELF(L)
const std::wstring name = LuaManager::ToWide(2);
const std::wstring* value = self->GetParser().GetVariable(name);
2012-08-05 13:12:36 +00:00
if (value)
2012-03-08 15:46:36 +00:00
{
LuaManager::PushWide(*value);
}
else if (lua_gettop(L) >= 3)
{
lua_pushvalue(L, 3);
}
else
{
lua_pushnil(L);
2012-03-08 15:46:36 +00:00
}
return 1;
}
static int ReplaceVariables(lua_State* L)
{
DECLARE_SELF(L)
std::wstring strTmp = LuaManager::ToWide(2);
2012-03-08 15:46:36 +00:00
self->GetParser().ReplaceVariables(strTmp);
self->GetParser().ReplaceMeasures(strTmp);
LuaManager::PushWide(strTmp);
return 1;
}
static int ParseFormula(lua_State* L)
{
DECLARE_SELF(L)
std::wstring strTmp = LuaManager::ToWide(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)
std::wstring path = LuaManager::ToWide(2);
2012-03-08 15:46:36 +00:00
self->MakePathAbsolute(path);
LuaManager::PushWide(path);
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 },
2013-05-31 14:28:39 +00:00
{ nullptr, nullptr }
};
2013-05-31 14:18:52 +00:00
luaL_register(L, "MeterWindow", functions);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_pop(L, 1);
}