-
Notifications
You must be signed in to change notification settings - Fork 15
/
winapi_demo.lua
432 lines (357 loc) · 9.35 KB
/
winapi_demo.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
setfenv(1, require'winapi')
--functions
require'winapi.monitor'
require'winapi.cursor'
--standard controls
require'winapi.windowclass'
require'winapi.menuclass'
require'winapi.buttonclass'
require'winapi.toolbarclass'
require'winapi.groupboxclass'
require'winapi.checkboxclass'
require'winapi.radiobuttonclass'
require'winapi.editclass'
require'winapi.tabcontrolclass'
require'winapi.listboxclass'
require'winapi.comboboxclass'
require'winapi.labelclass'
require'winapi.listviewclass'
require'winapi.trackbarclass'
--panels
require'winapi.bitmappanel'
local have_wgl = pcall(require, 'winapi.wglpanel')
local have_cairo = pcall(require, 'winapi.cairopanel')
--get the monitor where the mouse is right now. ------------------------------
local mon = MonitorFromPoint(GetCursorPos(), MONITOR_DEFAULTTONEAREST)
local moninfo = GetMonitorInfo(mon)
--create the main window -----------------------------------------------------
local w, h = 550, 550
local win = Window{
x = (moninfo.work_rect.w - w) / 2, --center the window on the monitor
y = (moninfo.work_rect.h - h) / 2,
w = w,
h = h,
title = (' '):rep(50)..' ┬┴┬┴┤ Lua Rulez ├┬┴┬┴',
autoquit = true, --quit the app when the window is closed
}
--create and show an about box -----------------------------------------------
local function about_box()
local w, h = 300, 200
local aboutwin = Window{
x = win.x + (win.w - w) / 2, --center the window on its parent
y = win.y + (win.h - h) / 2,
w = 300,
h = 200,
title = 'About winapi',
maximizable = false,
minimizable = false,
resizeable = false,
owner = win, --don't show it in taskbar
tool_window = true,
}
local w, h = 100, 24
local okbtn = Button{
x = (aboutwin.client_w - w) / 2,
y = aboutwin.client_h * 5/6 - h,
w = w, h = h,
parent = aboutwin,
default = true, --respond to pressing Enter
}
local lb = Label{
x = 0, y = 40,
w = aboutwin.client_w,
h = aboutwin.client_h,
parent = aboutwin,
align = 'center',
text = 'Windows API Binding Demo',
}
--make it modal
function okbtn:on_click()
aboutwin:close()
end
function aboutwin:on_close()
win:enable()
end
aboutwin.__wantallkeys = true --give us the ESC key!
function okbtn:on_key_down(vk)
if vk == VK_ESCAPE then
aboutwin:close()
end
end
win:disable()
okbtn:focus()
end
--create menus for the main menu bar -----------------------------------------
local filemenu = Menu{
items = {
{text = '&Close', on_click = function() win:close() end},
},
}
local aboutmenu = Menu{
items = {
{text = '&About', on_click = about_box},
},
}
--create the main menu bar ---------------------------------------------------
local menubar = MenuBar()
menubar.items:add{text = '&File', submenu = filemenu}
menubar.items:add{text = '&About', submenu = aboutmenu}
win.menu = menubar
--create a toolbar -----------------------------------------------------------
require'winapi.showcase'
local toolbar = Toolbar{
parent = win,
image_list = ImageList{w = 16, h = 16, masked = true, colors = '32bit'},
items = {
--NOTE: using `iBitmap` instead of `i` because `i` counts from 1
{iBitmap = STD_FILENEW, text = 'New'},
{iBitmap = STD_FILEOPEN, text = 'Open'},
{iBitmap = STD_FILESAVE, text = 'Save'},
},
}
toolbar:load_images(IDB_STD_SMALL_COLOR)
--create a group box ---------------------------------------------------------
local groupbox1 = GroupBox{
parent = win,
x = 20,
y = 180,
w = 100,
h = 170,
text = 'Group 1',
}
local groupbox2 = GroupBox{
parent = win,
x = 140,
y = 180,
w = 100,
h = 170,
text = 'Group 2',
}
--create radio buttons -------------------------------------------------------
for i = 1, 5 do
RadioButton{
parent = groupbox1,
x = 20,
y = 10 + i * 22,
w = 60,
text = 'Option &'..i,
checked = i == 2,
}
end
--create check boxes ---------------------------------------------------------
for i = 1, 5 do
CheckBox{
parent = groupbox2,
x = 20,
y = 10 + i * 22,
w = 60,
text = 'Option &'..i,
checked = i == 3 or i == 5,
}
end
--create a tab control -------------------------------------------------------
local tabs = TabControl{
parent = win,
x = 380,
y = 70,
w = 100,
h = 100,
anchors = 'ltr',
items = {
{text = 'Tab1',},
{text = 'Tab2',},
},
}
local tablabel = Label{
parent = tabs,
x = 10, y = 30,
w = 50, h = 50,
anchors = 'ltr',
}
function tabs:on_tab_change()
tablabel.text = 'Selected tab: '..tostring(self.selected_index)
end
tabs:on_tab_change()
--create a bitmap panel ------------------------------------------------------
local bmppanel = BitmapPanel{w = 100, h = 100, x = 20, y = 70, parent = win}
function bmppanel:on_bitmap_paint(bmp)
local p = self.cursor_pos
local pixels = ffi.cast('uint8_t*', bmp.data)
for y = 0, bmp.h - 1 do
for x = 0, bmp.w - 1 do
pixels[y * bmp.stride + x * 4 + 0] = x + p.x - 100
pixels[y * bmp.stride + x * 4 + 1] = y + p.y - 100
pixels[y * bmp.stride + x * 4 + 2] = x + p.x - 100
end
end
end
function bmppanel:on_mouse_move(x, y)
self:invalidate()
end
--create a cairo panel -------------------------------------------------------
if have_cairo then
local cairo = require'cairo'
local cairopanel = CairoPanel{w = 100, h = 100, x = 140, y = 70, parent = win}
local r = 0
function cairopanel:on_cairo_paint(cr)
cr:rgba(0,0,0,1)
cr:paint()
cr:identity_matrix()
cr:translate(self.w/2, self.h/2)
r = r + .02
cr:rotate(r)
cr:translate(-self.w/2, -self.h/2)
cr:scale(0.4, 0.4)
cr:rgba(0,0.7,0,1)
cr:line_width(40.96)
cr:move_to(76.8, 84.48)
cr:rel_line_to(51.2, -51.2)
cr:rel_line_to(51.2, 51.2)
cr:line_join'miter'
cr:stroke()
cr:move_to(76.8, 161.28)
cr:rel_line_to(51.2, -51.2)
cr:rel_line_to(51.2, 51.2)
cr:line_join'bevel'
cr:stroke()
cr:move_to(76.8, 238.08)
cr:rel_line_to(51.2, -51.2)
cr:rel_line_to(51.2, 51.2)
cr:line_join'round'
cr:stroke()
end
win:settimer(1/30, function()
cairopanel:invalidate()
end)
end
--create a wglpanel ----------------------------------------------------------
if have_wgl then
local wglpanel = WGLPanel{w = 100, h = 100, x = 260, y = 70, parent = win}
local function cube(w, r)
gl.glPushMatrix()
gl.glTranslated(0,0,-3)
gl.glScaled(w, w, 1)
gl.glRotated(r,1,r,1)
gl.glTranslated(0,0,2)
local function face(c)
gl.glBegin(gl.GL_QUADS)
gl.glColor4d(c,0,0,.5)
gl.glVertex3d(-1, -1, -1)
gl.glColor4d(0,c,0,.5)
gl.glVertex3d(1, -1, -1)
gl.glColor4d(0,0,c,.5)
gl.glVertex3d(1, 1, -1)
gl.glColor4d(c,0,c,.5)
gl.glVertex3d(-1, 1, -1)
gl.glEnd()
end
gl.glTranslated(0,0,-2)
face(1)
gl.glTranslated(0,0,2)
face(1)
gl.glTranslated(0,0,-2)
gl.glRotated(-90,0,1,0)
face(1)
gl.glTranslated(0,0,2)
face(1)
gl.glRotated(-90,1,0,0)
gl.glTranslated(0,2,0)
face(1)
gl.glTranslated(0,0,2)
face(1)
gl.glPopMatrix()
end
function wglpanel:on_set_viewport()
local w, h = self.w, self.h
gl.glViewport(0, 0, w, h)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
gl.glFrustum(-1, 1, -1, 1, 1, 100) --so fov is 90 deg
gl.glScaled(1, w/h, 1)
end
local r = 0
function wglpanel:on_render()
gl.glClearColor(0, 0, 0, 1)
gl.glClear(bit.bor(gl.GL_COLOR_BUFFER_BIT, gl.GL_DEPTH_BUFFER_BIT))
gl.glEnable(gl.GL_BLEND)
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_SRC_ALPHA)
gl.glDisable(gl.GL_DEPTH_TEST)
gl.glDisable(gl.GL_CULL_FACE)
gl.glDisable(gl.GL_LIGHTING)
gl.glMatrixMode(gl.GL_MODELVIEW)
gl.glLoadIdentity()
gl.glTranslated(0,0,-1)
r = r + 2
cube(2, r)
end
win:settimer(1/30, function()
wglpanel:invalidate()
end)
end
--create some buttons --------------------------------------------------------
local closebtn = Button{
x = win.client_w - 130,
y = win.client_h - 40,
w = 100,
text = '&Close',
parent = win,
anchors = 'rb',
}
function closebtn:on_click()
win:close()
end
win.min_cw = win.client_w
win.min_ch = win.client_h
--create a list box ----------------------------------------------------------
local lb = ListBox{parent = win, x = 260, y = 187, h = 160, hextent = 100}
for i = 1,100 do
lb.items:add('ListBox item '..i)
end
--create an edit box ---------------------------------------------------------
local edit = Edit{parent = win, x = 380, y = 187, cue = 'Edit me'}
--create a combo box ---------------------------------------------------------
local combo = ComboBox{
parent = win,
x = 380,
y = 220,
type = 'dropdownlist',
items = {
{text = 'First item'},
{text = 'Second item'},
},
selected_index = 2,
}
for i = 1, 10 do
combo.items:add{text = 'Item '..i}
end
--create a slider ------------------------------------------------------------
local slider = Trackbar{
parent = win,
x = 370,
y = 260,
w = 120,
}
--create some labels ---------------------------------------------------------
local lb1 = Label{parent = win, x = 20, y = 50, text = 'BitmapPanel'}
local lb2 = Label{parent = win, x = 140, y = 50, text = 'CairoPanel'}
local lb3 = Label{parent = win, x = 260, y = 50, text = 'WGLPanel'}
--create an accelerator ------------------------------------------------------
win.accelerators:add{
hotkey = 'escape', --VK_ESCAPE
handler = function() win:close() end,
}
--create some list views -----------------------------------------------------
local rlv = ReportListView{
parent = win,
x = 20,
y = 360,
w = 220,
h = 100,
columns = {'name', 'address'},
items = {
{text = 'Louis Armstrong', subitems = {'Basin Street'}},
{text = 'Django Reinhardt', subitems = {'Beyond The Sea'}},
},
}
--start the message loop -----------------------------------------------------
os.exit(MessageLoop())