File: radio.lua

package info (click to toggle)
lua-gtk 0.9%2B20100528-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,176 kB
  • ctags: 1,934
  • sloc: ansic: 9,571; sh: 373; makefile: 241
file content (51 lines) | stat: -rwxr-xr-x 1,260 bytes parent folder | download
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
#! /usr/bin/env lua
-- vim:sw=4:sts=4
require "gtk"

-- Example how to use GtkRadioButton, and shows some GSList functions, too.

function build_gui()
    local w, vbox, grp, r, b

    w = gtk.window_new(gtk.WINDOW_TOPLEVEL)
    w:connect('destroy', gtk.main_quit)
    vbox = gtk.vbox_new(true, 2)
    w:add(vbox)

    -- a GSList that contains all radio buttons of a group.
    grp = nil

    -- Create a few radio buttons.  The Lua proxy object gets the
    -- attribute "_id" set, an arbitrary key.
    for id, lbl in ipairs { "One", "Two", "Three", "Four" } do
	r = gtk.radio_button_new_with_label(grp, lbl)
	r._id = id
	vbox:pack_start(r, true, true, 0)
	-- get the start of the list.
	grp = r:get_group()
    end

    -- Add a button that, when clicked, shows which radio button is selected,
    -- then exits.
    b = gtk.button_new_from_stock(gtk.STOCK_OK)
    b:connect('clicked', function()
	-- the custom function returns 0 if found, 1 otherwise.
	item = grp:find_custom(nil, function(a, the_nil)
	    return a:get_active() and 0 or 1
	end)
	if item then
	    r = item.data:cast("GtkRadioButton")
	    print("You selected", r._id)
	end
	gtk.main_quit()
    end)

    vbox:pack_start(b, true, true, 0)

    w:show_all()
end


build_gui()
gtk.main()