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

« back to all changes in this revision

Viewing changes to samples/gtk-demo/demo-application.lua

  • 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
return function(parent, dir)
 
2
 
 
3
local lgi = require 'lgi'
 
4
local GObject = lgi.GObject
 
5
local Gtk = lgi.Gtk
 
6
local GdkPixbuf = lgi.GdkPixbuf
 
7
 
 
8
-- Register icon.
 
9
Gtk.stock_add {
 
10
   Gtk.StockItem {
 
11
      stock_id = 'demo-gtk-logo',
 
12
      label = "_GTK!",
 
13
   }
 
14
}
 
15
 
 
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))
 
22
 
 
23
local window = Gtk.Window {
 
24
   title = "Application Window",
 
25
   icon_name = 'document-open',
 
26
   default_width = 200,
 
27
   default_height = 200,
 
28
}
 
29
 
 
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
 
37
   }
 
38
   dialog:show()
 
39
end
 
40
 
 
41
local message_label = Gtk.Label()
 
42
message_label:show()
 
43
local function change_radio_action(action)
 
44
   if action.active then
 
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),
 
48
                              action.value))
 
49
      window.child.infobar.message_type = action.value
 
50
      window.child.infobar:show()
 
51
   end
 
52
end
 
53
 
 
54
local function set_theme(action)
 
55
   local settings = Gtk.Settings.get_default()
 
56
   settings.gtk_application_prefer_dark_theme = action.active
 
57
end
 
58
 
 
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(),
 
66
         lgi._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",
 
71
      authors = {
 
72
         "Pavel Holejsovsky",
 
73
         "Peter Mattis",
 
74
         "Spencer Kimball",
 
75
         "Josh MacDonald",
 
76
         "and many more...",
 
77
      },
 
78
      documenters = {
 
79
         "Owen Taylor",
 
80
         "Tony Gale",
 
81
         "Matthias Clasen <mclasen@redhat.com>",
 
82
         "and many more...",
 
83
      },
 
84
      logo = logo_pixbuf,
 
85
      title = "About GTK+ Code Demos",
 
86
   }
 
87
   about:run()
 
88
   about:hide()
 
89
end
 
90
 
 
91
local color = { RED = 1, GREEN = 2, BLUE = 3 }
 
92
local shape = { SQUARE = 1, RECTANGLE = 2, OVAL = 3 }
 
93
 
 
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",
 
125
                tooltip = "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 },
 
130
 
 
131
   { Gtk.ToggleAction { name = 'Bold', stock_id = Gtk.STOCK_BOLD,
 
132
                        label = "_Bold", tooltip = "Bold",
 
133
                        on_activate = activate_action,
 
134
                        active = true },
 
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 },
 
139
   {
 
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,
 
150
   },
 
151
 
 
152
   {
 
153
      { Gtk.RadioAction { name = 'Square', label = "_Square",
 
154
                          tooltip = "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,
 
164
   },
 
165
}
 
166
 
 
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([[
 
171
<ui>
 
172
  <menubar name='MenuBar'>
 
173
    <menu action='FileMenu'>
 
174
      <menuitem action='New'/>
 
175
      <menuitem action='Open'/>
 
176
      <menuitem action='Save'/>
 
177
      <menuitem action='SaveAs'/>
 
178
      <separator/>
 
179
      <menuitem action='Quit'/>
 
180
    </menu>
 
181
    <menu action='PreferencesMenu'>
 
182
      <menuitem action='DarkTheme'/>
 
183
      <menu action='ColorMenu'>
 
184
       <menuitem action='Red'/>
 
185
       <menuitem action='Green'/>
 
186
       <menuitem action='Blue'/>
 
187
      </menu>
 
188
      <menu action='ShapeMenu'>
 
189
        <menuitem action='Square'/>
 
190
        <menuitem action='Rectangle'/>
 
191
        <menuitem action='Oval'/>
 
192
      </menu>
 
193
      <menuitem action='Bold'/>
 
194
    </menu>
 
195
    <menu action='HelpMenu'>
 
196
      <menuitem action='About'/>
 
197
    </menu>
 
198
  </menubar>
 
199
  <toolbar name='ToolBar'>
 
200
    <toolitem action='Open'>
 
201
      <menu action='OpenMenu'>
 
202
        <menuitem action='File1'/>
 
203
      </menu>
 
204
    </toolitem>
 
205
    <toolitem action='Quit'/>
 
206
    <separator action='Sep1'/>
 
207
    <toolitem action='Logo'/>
 
208
  </toolbar>
 
209
</ui>]], -1))
 
210
 
 
211
local menubar = merge:get_widget('/MenuBar')
 
212
menubar.halign = 'FILL'
 
213
local toolbar = merge:get_widget('/ToolBar')
 
214
toolbar.halign = 'FILL'
 
215
 
 
216
window.child = Gtk.Grid {
 
217
   orientation = 'VERTICAL',
 
218
   menubar,
 
219
   toolbar,
 
220
   Gtk.InfoBar {
 
221
      id = 'infobar',
 
222
      no_show_all = true,
 
223
      halign = 'FILL',
 
224
      on_response = Gtk.Widget.hide
 
225
   },
 
226
   Gtk.ScrolledWindow {
 
227
      shadow_type = 'IN',
 
228
      halign = 'FILL', valign = 'FILL',
 
229
      expand = true,
 
230
      Gtk.TextView {
 
231
         id = 'view',
 
232
      },
 
233
   },
 
234
   Gtk.Statusbar {
 
235
      id = 'statusbar',
 
236
      halign = 'FILL',
 
237
   },
 
238
}
 
239
 
 
240
local buffer = window.child.view.buffer
 
241
local statusbar = window.child.statusbar
 
242
 
 
243
-- Updates statusbar according to the buffer of the view
 
244
local function update_statusbar()
 
245
   statusbar:pop(0)
 
246
   local iter = buffer:get_iter_at_mark(buffer:get_insert())
 
247
   local msg =
 
248
   statusbar:push(
 
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()))
 
251
end
 
252
 
 
253
function buffer:on_changed()
 
254
   update_statusbar()
 
255
end
 
256
 
 
257
function buffer:on_mark_set()
 
258
   update_statusbar()
 
259
end
 
260
 
 
261
-- Perform initial statusbar update.
 
262
update_statusbar()
 
263
 
 
264
-- Add infobar area.
 
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)
 
268
 
 
269
window:show_all()
 
270
window.child.view:grab_focus()
 
271
return window
 
272
 
 
273
end,
 
274
 
 
275
"Application main window",
 
276
 
 
277
[[Demonstrates a typical application window with menubar, toolbar, statusbar.]]