Examples
Here are some screenshots of the sample programs provided with the library.
Request2
This program uses the Glade library to build a GUI from an XML file that has been created using glade-3. The "Start" button starts retrieving the URL; the response body is shown in the textarea upon completion.
The function to build the GUI is shown below. As you can see, it derives the name of the glade file from the name of the Lua file (including the path). Then, The Glade library is used to read and parse the XML file into an internal representation, from which the widget tree starting at "window1" is created.
require "gtk"
require "gtk.glade"
function build_gui()
local fname, tree, widgets
fname = arg[1] or string.gsub(arg[0], "%.lua", ".glade")
tree = gtk.glade.read(fname)
widgets = gtk.glade.create(tree, "window1")
statusbar = widgets.statusbar1
statusbar_ctx = statusbar:get_context_id("progress")
statusbar:push(statusbar_ctx, "idle")
result_txt = widgets.result_txt
end
Iconview
A simple demo with a GtkIconView widget filled with a few
stock icons.
The code to obtain this (without Glade) is quite straightforward. Create a window, within it a scrolled window, and within that a GtkIconView. A list store is attached to it and filled with a few icons.
function MainWin.new()
local self = {}
setmetatable(self, MainWin)
self.w = gtk.window_new(gtk.GTK_WINDOW_TOPLEVEL)
self.w:connect('destroy', function() gtk.main_quit() end)
self.w:set_default_size(200, 250)
self.w:set_title("Icon View Demo")
local sw = gtk.scrolled_window_new(nil, nil)
sw:set_policy(gtk.GTK_POLICY_AUTOMATIC, gtk.GTK_POLICY_AUTOMATIC)
self.w:add(sw)
self.icon_view = gtk.icon_view_new()
sw:add(self.icon_view)
-- create store
self.store = gtk.list_store_new(3, gtk.G_TYPE_INT, gtk.G_TYPE_STRING,
gtk.gdk_pixbuf_get_type())
self.icon_view:set_model(self.store)
self.icon_view:set_text_column(1)
self.icon_view:set_pixbuf_column(2)
-- insert some items. see .../gtk/gtkstock.h
local iter = gtk.new "GtkTreeIter"
local pix
local names = { 'quit', 'open', 'redo', 'refresh', 'stop', 'save',
'save-as', 'select-color', 'yes', 'no', 'zoom-fit' }
for i, name in ipairs(names) do
self.store:append(iter)
pix = self.icon_view:render_icon('gtk-' .. name,
gtk.GTK_ICON_SIZE_DIALOG, "")
self.store:set(iter, 0, i, 1, name, 2, pix, -1)
end
self.w:show_all()
return self
end