cool_gtk.lua 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #! /usr/bin/env lua
  2. --
  3. -- Sample GTK Hello program
  4. --
  5. -- Based on test from LuiGI code. Thanks Adrian!
  6. --
  7. local lgi = require 'lgi'
  8. local Gtk = lgi.require('Gtk')
  9. -- Create top level window with some properties and connect its 'destroy'
  10. -- signal to the event loop termination.
  11. local window = Gtk.Window {
  12. title = 'window',
  13. default_width = 400,
  14. default_height = 300,
  15. on_destroy = Gtk.main_quit
  16. }
  17. if tonumber(Gtk._version) >= 3 then
  18. window.has_resize_grip = true
  19. end
  20. -- Create some more widgets for the window.
  21. local status_bar = Gtk.Statusbar()
  22. local toolbar = Gtk.Toolbar()
  23. local ctx = status_bar:get_context_id('default')
  24. status_bar:push(ctx, 'This is statusbar message.')
  25. -- When clicking at the toolbar 'quit' button, destroy the main window.
  26. toolbar:insert(Gtk.ToolButton {
  27. stock_id = 'gtk-quit',
  28. on_clicked = function() window:destroy() end,
  29. }, -1)
  30. -- About button in toolbar and its handling.
  31. local about_button = Gtk.ToolButton { stock_id = 'gtk-about' }
  32. function about_button:on_clicked()
  33. local dlg = Gtk.AboutDialog {
  34. program_name = 'LGI Demo',
  35. title = 'About...',
  36. name = 'LGI Hello',
  37. copyright = '(C) Copyright 2010, 2011 Pavel Holejšovský',
  38. authors = { 'Adrian Perez de Castro', 'Pavel Holejšovský', },
  39. }
  40. if tonumber(Gtk._version) >= 3 then
  41. dlg.license_type = Gtk.License.MIT_X11
  42. end
  43. dlg:run()
  44. dlg:hide()
  45. end
  46. toolbar:insert(about_button, -1)
  47. -- Pack everything into the window.
  48. local vbox = Gtk.VBox()
  49. vbox:pack_start(toolbar, false, false, 0)
  50. vbox:pack_start(Gtk.Label { label = 'Contents' }, true, true, 0)
  51. vbox:pack_end(status_bar, false, false, 0)
  52. window:add(vbox)
  53. -- Show window and start the loop.
  54. window:show_all()
  55. Gtk.main()