~ubuntu-branches/ubuntu/wily/lua-lgi/wily

« back to all changes in this revision

Viewing changes to docs/overview.md

  • Committer: Package Import Robot
  • Author(s): Enrico Tassi
  • Date: 2012-04-02 13:16:07 UTC
  • Revision ID: package-import@ubuntu.com-20120402131607-qcd5l8n9rx8lsq59
Tags: upstream-0.4+29+g74cbbb1
ImportĀ upstreamĀ versionĀ 0.4+29+g74cbbb1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# LGI Overview
 
2
 
 
3
LGI is Lua binding to Gnome platform.  It is implemented as dynamic
 
4
binding using gobject-introspection.  This means that all libraries
 
5
with support for gobject-introspection can be used by LGI without any
 
6
need to compile/install anything, assuming that proper .typelib file
 
7
is installed and available.
 
8
 
 
9
## Installation
 
10
 
 
11
### Dependencies
 
12
 
 
13
LGI depends on `gobject-introspection >= 1.30` package.  To build,
 
14
gobject-introspection development package must also be installed.
 
15
Note that required gobject-introspection version is unfortunately
 
16
rather new, currently mostly available only in unreleased-yet versions
 
17
of major distributions (part of GNOME-3.2, e.g. Fedora 16).  There is
 
18
planned work to make LGI mostly work also with older
 
19
gobject-introspection versions, which are part of GNOME-3.0.  Pre-3.0
 
20
versions are not planned to be supported at all.
 
21
 
 
22
In order to be able to use assorted gobject-based libraries through
 
23
LGI, these libraries must have properly installed `.typelib` files.
 
24
Most, if not all distributions already do this properly.
 
25
 
 
26
### Supported platforms
 
27
 
 
28
LGI is currently tested on Linux (all sane Linux distributions should work
 
29
fine) and Cygwin.  There is no principal obstacle for supporting other
 
30
platforms, as long as gobject-introspection library (and of course Lua) is
 
31
ported and working there.
 
32
 
 
33
### Installing via LuaRocks
 
34
 
 
35
The preferred way to install LGI is using luarocks.  As of writing
 
36
this document, LGI is not yet available on public luarocks server, so
 
37
plain `luarocks install lgi` does not work yet, although it will be
 
38
preferred way to install LGI in the future.  Currently, LGI source
 
39
must be downloaded, unpacked and installed using `luarocks make`.
 
40
 
 
41
### Installing using Makefile
 
42
 
 
43
Another way to install LGI is using makefiles:
 
44
 
 
45
    make
 
46
    sudo make install [PREFIX=prefix-path] [DESTDIR=destir-path]
 
47
 
 
48
Default `PREFIX` is `/usr/local` and default `DESTDIR` is empty.
 
49
 
 
50
## Quick overview
 
51
 
 
52
All LGI functionality is available in Lua module lgi, which is loaded
 
53
by using Lua `require` construct:
 
54
 
 
55
    local lgi = require 'lgi'
 
56
 
 
57
All gobject-introspection accessible modules are now accessible in lgi table:
 
58
 
 
59
    local Gtk = lgi.Gtk
 
60
    local Gio = lgi.Gio
 
61
    local GLib = lgi.GLib
 
62
 
 
63
To create instance of the class, simply 'call' the class in the namespace:
 
64
 
 
65
    local window = Gtk.Window()
 
66
 
 
67
To access object properties and call methods on the object instances,
 
68
use normal Lua object access notation:
 
69
 
 
70
    window.title = 'I am a window'
 
71
    window:show_all()
 
72
    window.title = window.title .. ' made by Lgi'
 
73
 
 
74
Note that properties can have `-` (dash) character in them.  It is
 
75
illegal in Lua, so it is translated to `_` (underscore).
 
76
 
 
77
    window.has_resize_grip = true
 
78
 
 
79
It is also possible to assign properties during object construction:
 
80
 
 
81
    local window = Gtk.Window {
 
82
       title = 'I am a window made by Lgi',
 
83
       has_resize_grip = true
 
84
    }
 
85
 
 
86
Note that structures and unions are handled similarly to classes, but
 
87
structure fields are accessed instead of properties.
 
88
 
 
89
To connect signal to object instance, assign function to be run to
 
90
`on_signalname` object slot:
 
91
 
 
92
    window.on_destroy = function(object)
 
93
                           print('destroying', object)
 
94
                        end
 
95
 
 
96
Note that Lua has nice syntactic sugar for objects, so previous
 
97
construction can also be written like this:
 
98
 
 
99
    function window:on_destroy()
 
100
       print('destroying', self)
 
101
    end
 
102
 
 
103
Note that potential dashes in signal names are also translated to
 
104
underscores to cope well with Lua identifier rules.
 
105
 
 
106
Enumerations and bitflags are grouped in the enumeration name table,
 
107
and real names are enumeration nicks uppercased.  For example,
 
108
`GTK_WINDOW_TOPLEVEL` identifier is accessible as
 
109
`Gtk.WindowType.TOPLEVEL`.
 
110
 
 
111
There is no need to handle any kind of memory management; LGI handles
 
112
all reference counting internally in cooperation with Lua's garbage
 
113
collector.
 
114
 
 
115
For APIs which use callbacks, provide Lua function which will be
 
116
called when the callback is invoked.  It is also possible to pass
 
117
coroutine instance as callback argument, in this case, coroutine is
 
118
resumed and returning `coroutine.yield()` returns all arguments passed
 
119
to the callback.  The callback returns when coroutine yields again or
 
120
finishes.  Arguments passed to `coroutine.yield()` call or exit status
 
121
of the coroutine are then used as return value from the callback.
 
122
 
 
123
See examples in `samples` source directory to dive deeper into the LGI
 
124
features.