gtkpad.lua 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #! /usr/bin/env lua
  2. --[[--------------------------------------------------------------------------
  3. Sample GTK Application program, simple notepad implementation.
  4. Copyright (c) 2010 Pavel Holejsovsky
  5. Licensed under the MIT license:
  6. http://www.opensource.org/licenses/mit-license.php
  7. --]]--------------------------------------------------------------------------
  8. local lgi = require 'lgi'
  9. local Gtk = lgi.require('Gtk', '3.0')
  10. local Gio = lgi.Gio
  11. local app = Gtk.Application.new('org.lgi.GtkPad',
  12. Gio.ApplicationFlags.HANDLES_OPEN)
  13. local function new_editor(file)
  14. local contents = file and file:load_contents()
  15. local window = Gtk.Window {
  16. type = Gtk.WindowType.TOPLEVEL,
  17. default_width = 400,
  18. default_height = 300,
  19. application = app,
  20. title = file and file:get_parse_name() or '<Untitled>',
  21. child = Gtk.ScrolledWindow {
  22. child = Gtk.TextView {
  23. buffer = Gtk.TextBuffer {
  24. text = contents and tostring(contents) or ''
  25. }
  26. }
  27. }
  28. }
  29. window:show_all()
  30. return window
  31. end
  32. function app:on_activate()
  33. new_editor()
  34. end
  35. function app:on_open(files)
  36. for i = 1, #files do new_editor(files[i]) end
  37. end
  38. return app:run {arg[0], ...}