gtkclipboard.lua 914 B

123456789101112131415161718192021222324252627282930313233343536
  1. #! /usr/bin/env lua
  2. -- Gtk clipboard sample, adapted from Vala clipboard sample at
  3. -- http://live.gnome.org/Vala/GTKSample#Clipboard
  4. local lgi = require 'lgi'
  5. local Gtk = lgi.require('Gtk', '3.0')
  6. local Gdk = lgi.Gdk
  7. local app = Gtk.Application { application_id = 'org.lgi.samples.gtkclipboard' }
  8. function app:on_activate()
  9. local entry = Gtk.Entry {}
  10. local window = Gtk.Window {
  11. application = self,
  12. title = 'Clipboard',
  13. default_width = 300, default_height = 20,
  14. child = entry
  15. }
  16. window:show_all()
  17. local display = window:get_display()
  18. local clipboard = Gtk.Clipboard.get_for_display(
  19. display, Gdk.SELECTION_CLIPBOARD)
  20. -- Get text from clipboard
  21. entry.text = clipboard:wait_for_text() or ''
  22. -- If the user types something, set text to clipboard
  23. function entry:on_changed()
  24. clipboard:set_text(entry.text, -1)
  25. end
  26. end
  27. app:run { arg[0], ... }