-
Notifications
You must be signed in to change notification settings - Fork 0
/
winapi.lua
202 lines (160 loc) · 5.95 KB
/
winapi.lua
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
local ffi = require"ffi"
local bit = require"bit"
winapi= winapi or {}
ffi.cdef[[
static const int OFN_FILEMUSTEXIST = 0x1000;
static const int OFN_NOCHANGEDIR = 8;
static const int OFN_PATHMUSTEXIST = 0x800;
typedef bool BOOL;
typedef char CHAR;
typedef uint16_t WORD;
typedef unsigned long DWORD;
typedef void *PVOID;
typedef void *LPVOID;
typedef void *LPOFNHOOKPROC;
typedef unsigned long HANDLE;
typedef HANDLE HWND;
typedef HANDLE HINSTANCE;
typedef const char *LPCSTR;
typedef const char *LPCTSTR;
typedef char *LPSTR;
typedef char *LPTSTR;
typedef unsigned long LPARAM;
typedef struct {
DWORD lStructSize;
HWND hwndOwner;
HINSTANCE hInstance;
LPCTSTR lpstrFilter;
LPTSTR lpstrCustomFilter;
DWORD nMaxCustFilter;
DWORD nFilterIndex;
LPTSTR lpstrFile;
DWORD nMaxFile;
LPTSTR lpstrFileTitle;
DWORD nMaxFileTitle;
LPCTSTR lpstrInitialDir;
LPCTSTR lpstrTitle;
DWORD flags;
WORD nFileOffset;
WORD nFileExtension;
LPCTSTR lpstrDefExt;
LPARAM lCustData;
LPOFNHOOKPROC lpfnHook;
LPCTSTR lpTemplateName;
LPVOID pvReserved;
DWORD dwReserved;
DWORD flagsEx;
}OPENFILENAME;
BOOL GetSaveFileNameA( OPENFILENAME *lpofn );
BOOL GetOpenFileNameA( OPENFILENAME *lpofn );
]]
com=ffi.load("comdlg32")
ffi.cdef([[
DWORD GetLastError(void);
]])
krnl=ffi.load("kernel32")
function winapi:OpenDialog(handle)
Ofn=ffi.new("OPENFILENAME")
ffi.fill(Ofn,ffi.sizeof(Ofn)) --zero fill the structure
local szFile = ffi.new("char[260]","\0")
local szFilename = ffi.new("char[100]","\0")
local hwnd = ffi.new("HWND",handle or 0)
Ofn.lStructSize = ffi.sizeof(Ofn)
Ofn.hwndOwner = hwnd
Ofn.lpstrFile = szFile
Ofn.nMaxFile = 260
Ofn.lpstrFilter = "All kind of thingies -- *.*\0*.*\0"..
"Wavefront OBJ -- *.obj\0*.obj\0"
Ofn.nFilterIndex = 2
Ofn.lpstrFileTitle = szFilename
Ofn.nMaxFileTitle = 100
Ofn.lpstrInitialDir = nil
Ofn.flags = bit.bor(com.OFN_PATHMUSTEXIST, com.OFN_FILEMUSTEXIST, com.OFN_NOCHANGEDIR)
if com.GetOpenFileNameA(Ofn) then --luajit converts bool automatically
return ffi.string(Ofn.lpstrFile),0
end
return false,krnl.GetLastError()
end
function winapi:SaveDialog(handle)
Ofn=ffi.new("OPENFILENAME")
ffi.fill(Ofn,ffi.sizeof(Ofn)) --zero fill the structure
local szFile = ffi.new("char[260]","\0")
local szFilename = ffi.new("char[100]","\0")
local hwnd = ffi.new("HWND",handle or 0)
Ofn.lStructSize = ffi.sizeof(Ofn)
Ofn.hwndOwner = hwnd
Ofn.lpstrFile = szFile
Ofn.nMaxFile = 260
Ofn.lpstrFilter = "All kind of thingies -- *.*\0*.*\0"..
"Wavefront OBJ -- *.obj\0*.obj\0"
Ofn.nFilterIndex = 2
Ofn.lpstrFileTitle = szFilename
Ofn.nMaxFileTitle = 100
Ofn.lpstrInitialDir = nil
Ofn.flags = bit.bor(com.OFN_PATHMUSTEXIST, com.OFN_FILEMUSTEXIST, com.OFN_NOCHANGEDIR)
if com.GetSaveFileNameA(Ofn) then --luajit converts bool automatically
return ffi.string(Ofn.lpstrFile),0
end
return false,krnl.GetLastError()
end
function winapi:GetHandle(title,class)
ffi.cdef([[
HWND FindWindowExA(
HWND hwndParent,
HWND hwndChildAfter,
LPCTSTR lpszClass,
LPCTSTR lpszWindow
);
]])
user=ffi.load("user32")
lpszClass = (title=="" and ffi.cast("char *", class) or nil)
lpszWindow = (class=="" and ffi.cast("char *", title) or nil)
return user.FindWindowExA(0,0,
lpszClass,
lpszWindow)
end
ffi.cdef[[
static const int MB_ICONQUESTION = 32;
static const int MB_OK = 0;
static const int MB_ABORTRETRYIGNORE = 2;
static const int MB_APPLMODAL = 0;
static const int MB_DEFAULT_DESKTOP_ONLY = 0x20000;
static const int MB_HELP = 0x4000;
static const int MB_RIGHT = 0x80000;
static const int MB_RTLREADING = 0x100000;
static const int MB_TOPMOST = 0x40000;
static const int MB_DEFBUTTON1 = 0;
static const int MB_DEFBUTTON2 = 256;
static const int MB_DEFBUTTON3 = 512;
static const int MB_DEFBUTTON4 = 0x300;
static const int MB_ICONINFORMATION = 64;
static const int MB_ICONSTOP = 16;
static const int MB_OKCANCEL = 1;
static const int MB_RETRYCANCEL = 5;
static const int MB_YESNO = 4;
static const int IDABORT = 3;
static const int IDCANCEL = 2;
static const int IDCLOSE = 8;
static const int IDHELP = 9;
static const int IDIGNORE = 5;
static const int IDNO = 7;
static const int IDOK = 1;
static const int IDRETRY = 4;
static const int IDYES = 6;
typedef unsigned int UINT;
int MessageBoxA(HWND,LPCSTR,LPCSTR,UINT);
]]
--user=ffi.load("user32")
function winapi:messagebox(text)
local gg=user.MessageBoxA(handle,
"Are you really sure you want to reload the map file?\r\nAll the unsaved changes will be lost, forever.",
"Oops!",
bit.bor(user.MB_TOPMOST, user.MB_ICONQUESTION, user.MB_YESNO, user.MB_DEFBUTTON2, user.MB_APPLMODAL)
)
--print("=>>>>>>>>>>>>>>>>> "..gg)
if gg == user.IDYES then
return true
else
return false
end
end