File: toolbar.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 (93 lines) | stat: -rwxr-xr-x 2,056 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
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
#! /usr/bin/env lua

-- Demonstrate a toolbar, callbacks, and a text view.

Mainwin = {}
Mainwin.__index = Mainwin

function Mainwin.new()
	local self = {}
	setmetatable(self, Mainwin)

	self.w = gtk.window_new(gtk.WINDOW_TOPLEVEL)
	self.w:set_title("Toolbar Demo")
	self.w:connect('destroy', gtk.main_quit)
	self.w:set_default_size(450, 400)

	local vbox = gtk.vbox_new(false, 0)
	self.w:add(vbox)

	local handle = gtk.handle_box_new()
	vbox:pack_start(handle, false, true, 0)

	local toolbar = gtk.toolbar_new()
	self.toolbar = toolbar
	handle:add(toolbar)

	local items = {
		{ "gtk-close", Mainwin.on_tool_close },
		{ "gtk-go-back", Mainwin.on_tool_back },
		{ "gtk-go-forward", Mainwin.on_tool_forward },
		{ "gtk-help", Mainwin.on_tool_help },
		{ "SEPARATOR", nil },
		{ "gtk-quit", Mainwin.on_tool_quit } }
	for _, item in pairs(items) do
		local stock = item[1]
		local handler = item[2]
		local button, id

		if stock == 'SEPARATOR' then
			button = gtk.separator_tool_item_new()
		else
			button = gtk.tool_button_new_from_stock(stock)
			id = button:connect("clicked", handler, self)
			-- print("connect id is", id)
			-- button:disconnect(id)
		end
		toolbar:insert(button, -1)
	end

	local sv = gtk.scrolled_window_new(nil, nil)
	vbox:pack_start(sv, true, true, 0)
	self.view = gtk.text_view_new()
	sv:add_with_viewport(self.view)
	self.buffer = self.view:get_buffer()


	self.w:show_all()
	return self
end

-- NOTE: for callbacks, self is the calling widget, i.e. the GtkToolButton.
--
-- insert a word at cursor position
function Mainwin:on_tool_close(mainwin)
	local s = "close\n"
	mainwin.buffer:insert_at_cursor(s, #s)
end

-- append something
function Mainwin:on_tool_back(mainwin)
	local iter = gtk.new "TextIter"
	mainwin.buffer:get_end_iter(iter)
	local s = "back\n"
	mainwin.buffer:insert(iter, s, #s)
end

function Mainwin:on_tool_forward()
	print "forward"
end

function Mainwin:on_tool_help()
	print "help"
end

function Mainwin:on_tool_quit()
	gtk.main_quit()
end

-- main --
require "gtk"
mainwin = Mainwin.new()
gtk.main()