Skip to content

Commit 9697e4a

Browse files
committed
view creation reworked: eventFunc must be given in world:newView()
1 parent f92e6fd commit 9697e4a

File tree

10 files changed

+338
-289
lines changed

10 files changed

+338
-289
lines changed

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,32 @@ application and also in the same window by embedding different view objects.
4040

4141
local world = lpugl.newWorld("Hello World App")
4242

43-
local view = world:newView {
43+
local view = world:newView
44+
{
4445
title = "Hello World Window",
45-
resizable = true
46-
}
47-
view:setSize(300, 100)
48-
view:setEventFunc(function(event, ...)
49-
print(event, ...)
50-
if event == "EXPOSE" then
51-
local cairo = view:getDrawContext()
52-
local w, h = view:getSize()
53-
cairo:set_source_rgb(0.9, 0.9, 0.9)
54-
cairo:rectangle(0, 0, w, h)
55-
cairo:fill()
56-
cairo:set_source_rgb(0, 0, 0)
57-
cairo:select_font_face("sans-serif", "normal", "normal")
58-
cairo:set_font_size(24)
59-
local text = "Hello World!"
60-
local ext = cairo:text_extents(text)
61-
cairo:move_to((w - ext.width)/2, (h - ext.height)/2 + ext.height)
62-
cairo:show_text(text)
63-
elseif event == "CLOSE" then
64-
view:close()
46+
size = {300, 100},
47+
resizable = true,
48+
49+
eventFunc = function(view, event, ...)
50+
print(event, ...)
51+
if event == "EXPOSE" then
52+
local cairo = view:getDrawContext()
53+
local w, h = view:getSize()
54+
cairo:set_source_rgb(0.9, 0.9, 0.9)
55+
cairo:rectangle(0, 0, w, h)
56+
cairo:fill()
57+
cairo:set_source_rgb(0, 0, 0)
58+
cairo:select_font_face("sans-serif", "normal", "normal")
59+
cairo:set_font_size(24)
60+
local text = "Hello World!"
61+
local ext = cairo:text_extents(text)
62+
cairo:move_to((w - ext.width)/2, (h - ext.height)/2 + ext.height)
63+
cairo:show_text(text)
64+
elseif event == "CLOSE" then
65+
view:close()
66+
end
6567
end
66-
end)
67-
68+
}
6869
view:show()
6970

7071
while world:hasViews() do

doc/README.md

Lines changed: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535
* [View Methods](#view-methods)
3636
* [view:show()](#view_show)
3737
* [view:hide()](#view_hide)
38+
* [view:setTitle()](#view_setTitle)
3839
* [view:setMinSize()](#view_setMinSize)
3940
* [view:setMaxSize()](#view_setMaxSize)
4041
* [view:setSize()](#view_setSize)
4142
* [view:getSize()](#view_getSize)
4243
* [view:setFrame()](#view_setFrame)
4344
* [view:getFrame()](#view_getFrame)
44-
* [view:setEventFunc()](#view_setEventFunc)
4545
* [view:getLayoutContext()](#view_getLayoutContext)
4646
* [view:getDrawContext()](#view_getDrawContext)
4747
* [view:getScreenScale()](#view_getScreenScale)
@@ -187,7 +187,7 @@ TODO
187187

188188
* **<a id="lpugl_MOD_">Key modifier flags</a>**
189189

190-
The current key modifier state (e.g. as delivered in a [BUTTON_PRESS](#event_BUTTON_PRESS) event)
190+
The current key modifier state (e.g. as delivered in a [*BUTTON_PRESS*](#event_BUTTON_PRESS) event)
191191
is an integer value where each key modifier is presented as a bit flag.
192192
You may use the function [lpugl.btest()](#lpugl_btest) for testing which
193193
flags are set in a key modifier state.
@@ -246,27 +246,60 @@ TODO
246246
[*view:close()*](#view_close) or if the world becomes garbage collected or is closed
247247
explicitly via [*world:close()*](#world_close)
248248

249-
* *initArgs* - lua table with initial key-value parameters. Can be ommitted, if a
250-
default backend was set via
251-
[*world:setDefaultBackend()*](#world_setDefaultBackend),
252-
otherwise at least a backend must be given.
249+
* *initArgs* - lua table with initial key-value parameters.
253250

254251
The parameter *initArgs* may contain the following parameters as key value pairs:
255252

256-
* *backend* - backend object to be used for the created view.
257-
* *title* - title for the created window.
258-
* *resizable* - *true* if the created window should be resizable.
259-
* *parent* - optional parent view. If given the created view is embedded
260-
into the parent view. Otherwise a top level window
261-
is created.
262-
* *popupFor* - TODO
263-
* *transientFor* - TODO
264-
* *dontMergeRects* - TODO
265-
266-
The parameters *title* and *resizable* have no effect if *parent* or
267-
*popupFor* is given.
268-
269-
Only one of the parameters *parent*, *popupFor* or *transientFor* can be set.
253+
* <a id="newView_backend">**`backend = lpuglBackend`**</a> - backend object to be used for the
254+
created view. Can be ommitted, if a default backend was set via
255+
[*world:setDefaultBackend()*](#world_setDefaultBackend)
256+
257+
* <a id="newView_title">**`title = titleString`**</a> - optional string, initial title for the
258+
created window. This parameter should not be set if [*parent*](#newView_parent) or
259+
[*popupFor*](#newView_popupFor) is given. The title may afterwards be changed using
260+
[*view:setTitle()*](#view_setTitle).
261+
262+
* <a id="newView_size">**`size = {width, height}`**</a> - optional table containing
263+
width and height as first and second table entry. Sets the initial size for the new view.
264+
The size may afterwards be changed using [*view:setSize()*](#view_setSize).
265+
266+
* <a id="newView_resizable">**`resizable = flag`**</a> - *true* if the created window should be
267+
resizable. This parameter should not be set if [*parent*](#newView_parent) or
268+
[*popupFor*](#newView_popupFor) is given.
269+
270+
* <a id="newView_parent">**`parent = view`**</a> - optional parent view. If given the created
271+
view is embedded into the parent view. Otherwise a top level window is created. This parameter
272+
cannot be combined with [*popupFor*](#newView_popupFor) or [*transientFor*](#newView_transientFor).
273+
274+
* <a id="newView_popupFor">**`popupFor = view`**</a> - TODO - This parameter
275+
cannot be combined with [*parent*](#newView_parent) or [*transientFor*](#newView_transientFor).
276+
277+
* <a id="newView_transientFor">**`transientFor = view`**</a> - TODO - This parameter
278+
cannot be combined with [*parent*](#newView_parent) or [*popupFor*](#newView_popupFor).
279+
280+
* <a id="newView_dontMergeRects">**`dontMergeRects = flag`**</a> - TODO
281+
282+
* <a id="newView_eventFunc">**`eventFunc = func | {func, ...}`**</a> - sets a function for
283+
handling the view's [event processing](#event-processing). The value for *eventFunc* may
284+
be a function or a table with it's first entry being the event handling function. The other
285+
entries with index > 1 in this table are context parameters that are given to the
286+
event handling function for each invocation.
287+
288+
If for the example the event handling function *func* is set up by the following
289+
invocation:
290+
```lua
291+
view = world:newView { eventFunc = {func, "foo1", "foo2"} }
292+
```
293+
and if an [mouse motion event](#event_MOTION) for the view at position 100, 200 occurs, then
294+
*func* will be called with the arguments `"foo1", "foo2", view, "MOTION", 100, 200`. If
295+
the event handling function is set up without context parameters, e.g.:
296+
```lua
297+
view = world:newView { eventFunc = func }
298+
```
299+
the above mouse motion event would lead to an invocation of *func* with the arguments
300+
`view, "MOTION", 100, 200`.
301+
302+
270303

271304
<!-- ---------------------------------------------------------------------------------------- -->
272305

@@ -387,7 +420,7 @@ TODO
387420

388421
* *func* - a error reporting function that takes two arguments: first argument is a string
389422
containing the error message and stack traceback, second argument is the original
390-
error object.
423+
error object that caused the error.
391424

392425
<!-- ---------------------------------------------------------------------------------------- -->
393426

@@ -451,6 +484,13 @@ TODO
451484

452485
<!-- ---------------------------------------------------------------------------------------- -->
453486

487+
* <a id="view_setTitle">**` view:setTitle(titleString)
488+
`**</a>
489+
490+
Sets the title of the window.
491+
492+
<!-- ---------------------------------------------------------------------------------------- -->
493+
454494
* <a id="view_setMinSize">**` view:setMinSize(width, height)
455495
`**</a>
456496

@@ -503,32 +543,6 @@ TODO
503543

504544
<!-- ---------------------------------------------------------------------------------------- -->
505545

506-
* <a id="view_setEventFunc">**` view:setEventFunc(func, ...)
507-
`**</a>
508-
509-
Sets an event handling function for a view.
510-
511-
* *func* - an event handling function. This function will be invoked for
512-
any event that occurs for the view.
513-
* *...* - optional context parameters: these arguments are given to the event
514-
handling function for each invocation.
515-
516-
When an event for the view occurs, the function *func* is called with the optional
517-
context parameters as first arguments. After the context parameters, the event name
518-
followed by event specific parameters is given.
519-
520-
*Example:*
521-
* if the event handling function *func* is set by the following invocation:
522-
523-
```lua
524-
view:setEventFunc(func, "foo1", "foo2")
525-
```
526-
and if an mouse motion event for the view at position 100, 200 occurs, then
527-
*func* will be called with the arguments `"foo1", "foo2", "MOTION", 100, 200`
528-
529-
For further details see [*Event Processing*](#event-processing).
530-
531-
<!-- ---------------------------------------------------------------------------------------- -->
532546

533547
* <a id="view_getLayoutContext">**` view:getLayoutContext()
534548
`**</a>
@@ -616,12 +630,12 @@ TODO
616630
<!-- ---------------------------------------------------------------------------------------- -->
617631

618632
Events for a view object are processed by the event handling function that
619-
was set with [*view:setEventFunc()*](#view_setEventFunc).
633+
was set as [*eventFunc*](#newView_eventFunc) in [*world:newView()*](#world_newView).
620634

621635
When an event for the view occurs, the event handling function is called with the optional
622-
context parameters given to [*view:setEventFunc()*](#view_setEventFunc) as first arguments.
623-
After the context parameters, the event name followed by event specific paramters is given
624-
to the event handling function.
636+
context parameters given to [*eventFunc*](#newView_eventFunc) as first arguments.
637+
After the context parameters, the view and then the event name followed by event specific
638+
paramaters are given to the event handling function.
625639

626640
The event handling function is called with the following possible events (event names
627641
followed by event specific parameters):
@@ -650,7 +664,7 @@ TODO
650664
otherwise configure the context, but not to draw anything.
651665

652666
* *x, y* - new position, parent-relative if the view is embedded into a parent view (i.e.
653-
*parent* was given in [world:newView()](#world_newView)). Otherwise the position
667+
*parent* was given in [*world:newView()*](#world_newView)). Otherwise the position
654668
is in absolute screen coordinates (the view is a top level window or popup in
655669
these cases).
656670
* *width*, *height* - new width and height

example/example01.lua

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,32 @@ local lpugl = require"lpugl_cairo"
22

33
local world = lpugl.newWorld("Hello World App")
44

5-
local view = world:newView {
5+
local view = world:newView
6+
{
67
title = "Hello World Window",
7-
resizable = true
8-
}
9-
view:setSize(300, 100)
10-
view:setEventFunc(function(event, ...)
11-
print(event, ...)
12-
if event == "EXPOSE" then
13-
local cairo = view:getDrawContext()
14-
local w, h = view:getSize()
15-
cairo:set_source_rgb(0.9, 0.9, 0.9)
16-
cairo:rectangle(0, 0, w, h)
17-
cairo:fill()
18-
cairo:set_source_rgb(0, 0, 0)
19-
cairo:select_font_face("sans-serif", "normal", "normal")
20-
cairo:set_font_size(24)
21-
local text = "Hello World!"
22-
local ext = cairo:text_extents(text)
23-
cairo:move_to((w - ext.width)/2, (h - ext.height)/2 + ext.height)
24-
cairo:show_text(text)
25-
elseif event == "CLOSE" then
26-
view:close()
8+
size = {300, 100},
9+
resizable = true,
10+
11+
eventFunc = function(view, event, ...)
12+
print(event, ...)
13+
if event == "EXPOSE" then
14+
local cairo = view:getDrawContext()
15+
local w, h = view:getSize()
16+
cairo:set_source_rgb(0.9, 0.9, 0.9)
17+
cairo:rectangle(0, 0, w, h)
18+
cairo:fill()
19+
cairo:set_source_rgb(0, 0, 0)
20+
cairo:select_font_face("sans-serif", "normal", "normal")
21+
cairo:set_font_size(24)
22+
local text = "Hello World!"
23+
local ext = cairo:text_extents(text)
24+
cairo:move_to((w - ext.width)/2, (h - ext.height)/2 + ext.height)
25+
cairo:show_text(text)
26+
elseif event == "CLOSE" then
27+
view:close()
28+
end
2729
end
28-
end)
29-
30+
}
3031
view:show()
3132

3233
while world:hasViews() do

example/example02.lua

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,41 @@ local glu = require"luaglu"
44

55
local world = lpugl.newWorld("OpenGL Example App")
66

7-
local view = world:newView {
7+
local view = world:newView
8+
{
89
title = "OpenGL Example Window",
9-
resizable = true
10-
}
11-
view:setSize(300, 100)
12-
view:setEventFunc(function(event, ...)
13-
print(event, ...)
14-
if event == "EXPOSE" then
15-
local w, h = view:getSize()
16-
gl.Viewport(0, 0, w, h)
17-
gl.MatrixMode('PROJECTION')
18-
gl.LoadIdentity()
19-
glu.Perspective(80, w / h, 1, 5000)
20-
gl.MatrixMode('MODELVIEW')
21-
gl.Clear('COLOR_BUFFER_BIT,DEPTH_BUFFER_BIT')
22-
gl.LoadIdentity()
23-
gl.Translate(-1.5, 0, -6)
24-
gl.Begin('TRIANGLES')
25-
gl.Vertex( 0, 1, 0)
26-
gl.Vertex(-1, -1, 0)
27-
gl.Vertex( 1, -1, 0)
28-
gl.End()
29-
gl.Translate(3, 0, 0)
30-
gl.Begin('QUADS')
31-
gl.Vertex(-1, 1, 0)
32-
gl.Vertex( 1, 1, 0)
33-
gl.Vertex( 1, -1, 0)
34-
gl.Vertex(-1, -1, 0)
35-
gl.End()
36-
elseif event == "CLOSE" then
37-
view:close()
10+
size = {300, 100},
11+
resizable = true,
12+
13+
eventFunc = function(view, event, ...)
14+
print(event, ...)
15+
if event == "EXPOSE" then
16+
local w, h = view:getSize()
17+
gl.Viewport(0, 0, w, h)
18+
gl.MatrixMode('PROJECTION')
19+
gl.LoadIdentity()
20+
glu.Perspective(80, w / h, 1, 5000)
21+
gl.MatrixMode('MODELVIEW')
22+
gl.Clear('COLOR_BUFFER_BIT,DEPTH_BUFFER_BIT')
23+
gl.LoadIdentity()
24+
gl.Translate(-1.5, 0, -6)
25+
gl.Begin('TRIANGLES')
26+
gl.Vertex( 0, 1, 0)
27+
gl.Vertex(-1, -1, 0)
28+
gl.Vertex( 1, -1, 0)
29+
gl.End()
30+
gl.Translate(3, 0, 0)
31+
gl.Begin('QUADS')
32+
gl.Vertex(-1, 1, 0)
33+
gl.Vertex( 1, 1, 0)
34+
gl.Vertex( 1, -1, 0)
35+
gl.Vertex(-1, -1, 0)
36+
gl.End()
37+
elseif event == "CLOSE" then
38+
view:close()
39+
end
3840
end
39-
end)
41+
}
4042

4143
view:show()
4244

0 commit comments

Comments
 (0)