1
return function(parent, dir)
3
local lgi = require 'lgi'
4
local GObject = lgi.GObject
6
local GdkPixbuf = lgi.GdkPixbuf
11
stock_id = 'demo-gtk-logo',
16
local logo_pixbuf = GdkPixbuf.Pixbuf.new_from_file(
17
dir:get_child('gtk-logo-rgb.gif'):get_path())
18
logo_pixbuf = logo_pixbuf and logo_pixbuf:add_alpha(true, 0xff, 0xff, 0xff)
19
local icon_factory = Gtk.IconFactory()
20
icon_factory:add_default()
21
icon_factory:add('demo-gtk-logo', Gtk.IconSet.new_from_pixbuf(logo_pixbuf))
23
local window = Gtk.Window {
24
title = "Application Window",
25
icon_name = 'document-open',
30
local function activate_action(action)
31
local dialog = Gtk.MessageDialog {
32
transient_for = window, destroy_with_parent = true,
33
message_type = 'INFO', buttons = 'CLOSE',
34
text = ("You activated action: '%s' of type '%s'"):format(
35
action.name, GObject.Type.name(action.type)),
36
on_response = Gtk.Widget.destroy
41
local message_label = Gtk.Label()
43
local function change_radio_action(action)
45
message_label.label = (("You activated radio action: '%s' of type '%s'."
46
.."\nCurrent value: %d"):format(
47
action.name, GObject.Type.name(action.type),
49
window.child.infobar.message_type = action.value
50
window.child.infobar:show()
54
local function set_theme(action)
55
local settings = Gtk.Settings.get_default()
56
settings.gtk_application_prefer_dark_theme = action.active
59
local function about_cb()
60
local about = Gtk.AboutDialog {
61
program_name = "GTK+ Lgi Code Demos",
62
version = ("Running against GTK+ %d.%d.%d, Lgi %s"):format(
63
Gtk.get_major_version(),
64
Gtk.get_minor_version(),
65
Gtk.get_micro_version(),
67
copyright = "(C) 2012 Pavel Holejsovsky",
68
license_type = 'MIT_X11',
69
website = 'http://github.org/pavouk/lgi',
70
comments = "Port of original GTK+ demo, (C) 1997-2009 The GTK+ Team",
81
"Matthias Clasen <mclasen@redhat.com>",
85
title = "About GTK+ Code Demos",
91
local color = { RED = 1, GREEN = 2, BLUE = 3 }
92
local shape = { SQUARE = 1, RECTANGLE = 2, OVAL = 3 }
94
local action_group = Gtk.ActionGroup {
95
name = 'AppWindowActions',
96
Gtk.Action { name = 'FileMenu', label = "_File" },
97
Gtk.Action { name = 'OpenMenu', label = "_Open" },
98
Gtk.Action { name = 'PreferencesMenu', label = "_Preferences" },
99
Gtk.Action { name = 'ColorMenu', label = "_Color" },
100
Gtk.Action { name = 'ShapeMenu', label = "_Shape" },
101
Gtk.Action { name = 'HelpMenu', label = "_Help" },
102
{ Gtk.Action { name = 'New', label = "_New", stock_id = Gtk.STOCK_NEW,
103
tooltip = "Create a new file",
104
on_activate = activate_action },
105
accelerator = '<control>N' },
106
Gtk.Action { name = 'File1', label = "File1", tooltip = "Open first file",
107
on_activate = activate_action },
108
Gtk.Action { name = 'Open', label = "_Open", stock_id = Gtk.STOCK_OPEN,
109
tooltip = "Open a file",
110
on_activate = activate_action },
111
Gtk.Action { name = 'File1', label = "File1", tooltip = "Open first file",
112
on_activate = activate_action },
113
{ Gtk.Action { name = 'Save', label = "_Save",
114
tooltip = "Save current file", stock_id = Gtk.STOCK_SAVE,
115
on_activate = activate_action },
116
accelerator = '<control>S' },
117
Gtk.Action { name = 'SaveAs', label = "Save _As",
118
tooltip = "Save to a file", stock_id = Gtk.STOCK_SAVE,
119
on_activate = activate_action },
120
{ Gtk.Action { name = 'Quit', label = "_Quit",
121
tooltip = "Quit", stock_id = Gtk.STOCK_QUIT,
122
on_activate = activate_action },
123
accelerator = '<control>Q' },
124
Gtk.Action { name = 'About', label = "_About",
126
on_activate = about_cb },
127
Gtk.Action { name = 'Logo', stock_id = 'demo-gtk-logo',
128
tooltip = "GTK+ on LGI",
129
on_activate = activate_action },
131
{ Gtk.ToggleAction { name = 'Bold', stock_id = Gtk.STOCK_BOLD,
132
label = "_Bold", tooltip = "Bold",
133
on_activate = activate_action,
135
accelerator = '<control>B' },
136
Gtk.ToggleAction { name = 'DarkTheme', label = "_Prefer dark theme",
137
tooltip = "Prefer dark theme", active = false,
138
on_activate = set_theme },
140
{ Gtk.RadioAction { name = 'Red', label = "_Red", tooltip = "Blood",
141
value = color.RED, },
142
accelerator = '<control>R' },
143
{ Gtk.RadioAction { name = 'Green', label = "_Green", tooltip = "Grass",
144
value = color.GREEN, },
145
accelerator = '<control>G' },
146
{ Gtk.RadioAction { name = 'Blue', label = "_Blue", tooltip = "Sky",
147
value = color.BLUE, },
148
accelerator = '<control>B' },
149
on_change = change_radio_action,
153
{ Gtk.RadioAction { name = 'Square', label = "_Square",
155
value = shape.SQUARE, },
156
accelerator = '<control>S' },
157
{ Gtk.RadioAction { name = 'Rectangle', label = "_Rectangle",
158
tooltip = "Rectangle", value = shape.RECTANGLE, },
159
accelerator = '<control>R' },
160
{ Gtk.RadioAction { name = 'Oval', label = "_Oval",
161
tooltip = "Oval", value = shape.OVAL, },
162
accelerator = '<control>O' },
163
on_change = change_radio_action,
167
local merge = Gtk.UIManager {}
168
merge:insert_action_group (action_group, 0)
169
window:add_accel_group(merge:get_accel_group())
170
assert(merge:add_ui_from_string([[
172
<menubar name='MenuBar'>
173
<menu action='FileMenu'>
174
<menuitem action='New'/>
175
<menuitem action='Open'/>
176
<menuitem action='Save'/>
177
<menuitem action='SaveAs'/>
179
<menuitem action='Quit'/>
181
<menu action='PreferencesMenu'>
182
<menuitem action='DarkTheme'/>
183
<menu action='ColorMenu'>
184
<menuitem action='Red'/>
185
<menuitem action='Green'/>
186
<menuitem action='Blue'/>
188
<menu action='ShapeMenu'>
189
<menuitem action='Square'/>
190
<menuitem action='Rectangle'/>
191
<menuitem action='Oval'/>
193
<menuitem action='Bold'/>
195
<menu action='HelpMenu'>
196
<menuitem action='About'/>
199
<toolbar name='ToolBar'>
200
<toolitem action='Open'>
201
<menu action='OpenMenu'>
202
<menuitem action='File1'/>
205
<toolitem action='Quit'/>
206
<separator action='Sep1'/>
207
<toolitem action='Logo'/>
211
local menubar = merge:get_widget('/MenuBar')
212
menubar.halign = 'FILL'
213
local toolbar = merge:get_widget('/ToolBar')
214
toolbar.halign = 'FILL'
216
window.child = Gtk.Grid {
217
orientation = 'VERTICAL',
224
on_response = Gtk.Widget.hide
228
halign = 'FILL', valign = 'FILL',
240
local buffer = window.child.view.buffer
241
local statusbar = window.child.statusbar
243
-- Updates statusbar according to the buffer of the view
244
local function update_statusbar()
246
local iter = buffer:get_iter_at_mark(buffer:get_insert())
249
0, ("Cursor at row %d column %d - %d chars in document"):format(
250
iter:get_line(), iter:get_line_offset(), buffer:get_char_count()))
253
function buffer:on_changed()
257
function buffer:on_mark_set()
261
-- Perform initial statusbar update.
265
local info_area = window.child.infobar:get_content_area()
266
info_area:add(message_label)
267
window.child.infobar:add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK)
270
window.child.view:grab_focus()
275
"Application main window",
277
[[Demonstrates a typical application window with menubar, toolbar, statusbar.]]