summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/m_icolib.cpp
blob: a83c9877d7e5709e6631a3c632129c06b14f933f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "stdafx.h"

static void MakeSKINICONDESC(lua_State *L, SKINICONDESC &sid)
{
	lua_getfield(L, -1, "Flags");
	sid.flags = lua_tointeger(L, -1);
	lua_pop(L, 1);

	if (!(sid.flags & SIDF_ALL_UNICODE))
		sid.flags |= SIDF_ALL_UNICODE;

	lua_getfield(L, -1, "Name");
	sid.pszName = mir_utf8decodeA(luaL_checkstring(L, -1));
	lua_pop(L, 1);

	lua_getfield(L, -1, "Description");
	sid.description.w = mir_utf8decodeW(luaL_checkstring(L, -1));
	lua_pop(L, 1);

	lua_getfield(L, -1, "Section");
	sid.section.w = mir_utf8decodeW(luaL_optstring(L, 3, MODULENAME));
	lua_pop(L, 1);

	lua_getfield(L, -1, "DefaultFile");
	sid.defaultFile.w = mir_utf8decodeW(lua_tostring(L, -1));
	lua_pop(L, 1);

	if (sid.defaultFile.w == nullptr)
	{
		sid.defaultFile.w = (wchar_t*)mir_calloc(MAX_PATH + 1);
		GetModuleFileName(g_plugin.getInst(), sid.defaultFile.w, MAX_PATH);
	}

	lua_getfield(L, -1, "DefaultIndex");
	sid.iDefaultIndex = lua_tointeger(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, -1, "DefaultIcon");
	sid.hDefaultIcon = (HICON)lua_touserdata(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, -1, "SizeX");
	sid.iDefaultIndex = lua_tointeger(L, -1);
	lua_pop(L, 1);

	lua_getfield(L, -1, "SizeY");
	sid.iDefaultIndex = lua_tointeger(L, -1);
	lua_pop(L, 1);
}

static int lua_AddIcon(lua_State *L)
{
	SKINICONDESC sid = { };

	if (lua_type(L, 1) == LUA_TSTRING)
	{
		sid.flags = SIDF_ALL_UNICODE;
		sid.pszName = mir_utf8decodeA(luaL_checkstring(L, 1));
		sid.description.w = mir_utf8decodeW(luaL_checkstring(L, 2));
		sid.section.w = mir_utf8decodeW(luaL_optstring(L, 3, MODULENAME));
		sid.defaultFile.w = mir_utf8decodeW(lua_tostring(L, 4));
		sid.hDefaultIcon = GetIcon(IDI_SCRIPT);

		if (sid.defaultFile.w == nullptr)
		{
			sid.defaultFile.w = (wchar_t*)mir_calloc(MAX_PATH + 1);
			GetModuleFileName(g_plugin.getInst(), sid.defaultFile.w, MAX_PATH);
		}
	}
	else if (lua_type(L, 1) == LUA_TTABLE)
		MakeSKINICONDESC(L, sid);
	else
		luaL_argerror(L, 1, luaL_typename(L, 1));

	int hScriptLangpack = CMLuaEnvironment::GetEnvironmentId(L);
	HANDLE res = IcoLib_AddIcon(&sid, hScriptLangpack);
	lua_pushlightuserdata(L, res);

	mir_free((void*)sid.pszName);
	mir_free((void*)sid.description.w);
	mir_free((void*)sid.section.w);
	mir_free((void*)sid.defaultFile.w);

	return 1;
}

static int lua_GetIcon(lua_State *L)
{
	bool big = luaM_toboolean(L, 2);

	HICON hIcon = nullptr;
	switch (lua_type(L, 1))
	{
	case LUA_TLIGHTUSERDATA:
		hIcon = IcoLib_GetIconByHandle(lua_touserdata(L, 1), big);
		break;
	case LUA_TSTRING:
		hIcon = IcoLib_GetIcon(lua_tostring(L, 1), big);
		break;
	default:
		luaL_argerror(L, 1, luaL_typename(L, 1));
	}

	if (hIcon)
		lua_pushlightuserdata(L, hIcon);
	else
		lua_pushnil(L);

	return 1;
}

static int lua_GetIconHandle(lua_State *L)
{
	const char *name = luaL_checkstring(L, 1);

	HANDLE res = IcoLib_GetIconHandle(name);
	lua_pushlightuserdata(L, res);

	return 1;
}

static int lua_RemoveIcon(lua_State *L)
{
	switch (lua_type(L, 1))
	{
	case LUA_TLIGHTUSERDATA:
		IcoLib_RemoveIconByHandle(lua_touserdata(L, 1));
		break;
	case LUA_TSTRING:
		IcoLib_RemoveIcon(luaL_checkstring(L, 1));
		break;
	default:
		luaL_argerror(L, 1, luaL_typename(L, 1));
	}

	return 0;
}

static luaL_Reg icolibApi[] =
{
	{ "AddIcon", lua_AddIcon },
	{ "GetIcon", lua_GetIcon },
	{ "GetHandle", lua_GetIconHandle },
	{ "GetIconHandle", lua_GetIconHandle },
	{ "RemoveIcon", lua_RemoveIcon },

	{ nullptr, nullptr }
};

LUAMOD_API int luaopen_m_icolib(lua_State *L)
{
	luaL_newlib(L, icolibApi);

	return 1;
}