-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlua_quik_resources_main.pas
More file actions
166 lines (133 loc) · 5.03 KB
/
lua_quik_resources_main.pas
File metadata and controls
166 lines (133 loc) · 5.03 KB
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
156
157
158
159
160
161
162
163
164
165
166
{$include lua_quik_resources_defs.pas}
unit lua_quik_resources_main;
interface
uses windows, classes, sysutils, math,
LuaLib54, LuaHelpers,
lua_quik_resources_utils;
const package_name = 'quik_resources';
const lua_supported_libs : array[0..0] of pAnsiChar = ('Lua54.dll');
type tLuaQuikResources = class;
tLuaQuikResources = class(TLuaClass)
private
fQUIKWnd : HWND;
fResHndl : HModule;
function fGetQuikHandle: HWND;
protected
function GetQUIKResources(AContext: TLuaContext): THandle;
public
constructor create(hLib: HMODULE);
// LUA functions
function get_quik_handle(AContext: TLuaContext): integer;
function get_menu_state(AContext: TLuaContext): integer;
function get_dlg_title(AContext: TLuaContext): integer;
function set_dlg_item_text(AContext: TLuaContext): integer;
function post_message(AContext: TLuaContext): integer;
function send_message(AContext: TLuaContext): integer;
function get_child_handle(AContext: TLuaContext): integer;
property QUIKHandle: HWND read fGetQuikHandle;
end;
function initialize_quik_resources(ALuaInstance: Lua_State): integer;
implementation
const quik_resources_instance: tLuaQuikResources = nil;
{ tLuaQuikResources }
constructor tLuaQuikResources.create(hLib: HMODULE);
begin
inherited create(hLib);
fQUIKWnd:= 0;
fResHndl:= 0;
end;
function tLuaQuikResources.fGetQuikHandle: HWND;
begin
if (fQUIKWnd = 0) then fQUIKWnd:= get_quik_main_window();
result:= fQUIKWnd;
end;
function tLuaQuikResources.GetQUIKResources(AContext: TLuaContext): THandle;
const resources_library_name = 'lang_res.dll';
var res_lib_name : ansistring;
begin
if (fResHndl = 0) then begin
with AContext do res_lib_name:= Globals['quik_resources_lib'].AsString(resources_library_name);
fResHndl:= GetModuleHandleA(pAnsiChar(res_lib_name));
end;
result:= fResHndl;
end;
function tLuaQuikResources.get_quik_handle(AContext: TLuaContext): integer;
begin
with AContext do
result:= PushArgs([int64(QUIKHandle)]);
end;
function tLuaQuikResources.get_menu_state(AContext: TLuaContext): integer;
begin
with AContext do
result:= PushArgs([GetMenuItemState(Stack[1].AsInteger, Stack[2].AsInteger)]);
end;
function tLuaQuikResources.get_dlg_title(AContext: TLuaContext): integer;
begin
with AContext do
result:= PushArgs([GetDialogTitleFromResource(GetQUIKResources(AContext), QUIKHandle, WORD(Stack[1].AsInteger))]);
end;
function tLuaQuikResources.set_dlg_item_text(AContext: TLuaContext): integer;
var wnd : HWND;
begin
with AContext do begin
wnd:= HWND(Stack[1].AsInteger);
if (wnd <> 0) then SetDlgItemTextA(wnd, Stack[2].AsInteger, pAnsiChar(Stack[3].AsString));
end;
result:= 0;
end;
function tLuaQuikResources.post_message(AContext: TLuaContext): integer;
begin
with AContext do
PostMessageA(HWND(Stack[1].AsInteger), WPARAM(Stack[2].AsInteger), WPARAM(Stack[3].AsInteger), LPARAM(Stack[4].AsInteger));
result:= 0;
end;
function tLuaQuikResources.send_message(AContext: TLuaContext): integer;
begin
with AContext do
result:= PushArgs([SendMessageA(HWND(Stack[1].AsInteger), WPARAM(Stack[2].AsInteger), WPARAM(Stack[3].AsInteger), LPARAM(Stack[4].AsInteger))]);
end;
function tLuaQuikResources.get_child_handle(AContext: TLuaContext): integer;
begin
with AContext do
result:= PushArgs([int64(get_quik_child_window(HWND(Stack[1].AsInteger), Stack[2].AsString))]);
end;
{ initialization functions }
function initialize_lua_library: HMODULE;
var i : integer;
begin
result:= 0;
i:= low(lua_supported_libs);
while (i <= high(lua_supported_libs)) do begin
result:= GetModuleHandle(lua_supported_libs[i]);
if (result <> 0) then i:= high(lua_supported_libs) + 1
else inc(i);
end;
end;
function initialize_quik_resources(ALuaInstance: Lua_State): integer;
var hLib : HMODULE;
begin
result:= 0;
if not assigned(quik_resources_instance) then begin
hLib:= initialize_lua_library;
if (hLib <> 0) then quik_resources_instance:= tLuaQuikResources.Create(hLib)
else messagebox(0, pAnsiChar(format('ERROR: failed to find LUA library: %s', [lua_supported_libs[0]])), 'Error', 0);
end;
if assigned(quik_resources_instance) then
with quik_resources_instance do begin
StartRegister;
// register adapter functions
RegisterMethod('get_quik_handle', get_quik_handle);
RegisterMethod('get_menu_state', get_menu_state);
RegisterMethod('get_dlg_title', get_dlg_title);
RegisterMethod('set_dlg_item_text', set_dlg_item_text);
RegisterMethod('post_message', post_message);
RegisterMethod('send_message', send_message);
RegisterMethod('get_child_handle', get_child_handle);
result:= StopRegister(ALuaInstance, package_name, true);
end;
result:= min(result, 1);
end;
initialization
finalization
if assigned(quik_resources_instance) then freeandnil(quik_resources_instance);
end.