demo-clipboard.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local Gtk = lgi.Gtk
  4. local Gdk = lgi.Gdk
  5. local log = lgi.log.domain 'gtk-demo'
  6. local function get_image_pixbuf(image)
  7. if image.storage_type == 'PIXBUF' then
  8. return image.pixbuf
  9. elseif image.storage_type == 'STOCK' then
  10. return image:render_icon_pixbuf(image.stock, image.icon_size)
  11. else
  12. log.warning(('Image storage type "%s" not handled'):format(
  13. image.storage_type))
  14. end
  15. end
  16. local function drag_begin(ebox, context)
  17. Gtk.drag_set_icon_pixbuf(context, get_image_pixbuf(ebox:get_child()),
  18. -2, -2)
  19. end
  20. local function drag_data_get(ebox, context, selection_data)
  21. selection_data:set_pixbuf(get_image_pixbuf(ebox:get_child()))
  22. end
  23. local function drag_data_received(ebox, context, x, y, selection_data)
  24. if selection_data:get_length() > 0 then
  25. ebox:get_child().pixbuf = selection_data:get_pixbuf()
  26. end
  27. end
  28. local function button_press(ebox, event_button)
  29. if event_button.button ~= 3 then return false end
  30. local clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
  31. local image = ebox:get_child()
  32. local menu = Gtk.Menu {
  33. Gtk.ImageMenuItem {
  34. use_stock = true, label = Gtk.STOCK_COPY,
  35. on_activate = function()
  36. local pixbuf = get_image_pixbuf(image)
  37. clipboard:set_image(pixbuf)
  38. end
  39. },
  40. Gtk.ImageMenuItem {
  41. use_stock = true, label = Gtk.STOCK_PASTE,
  42. on_activate = function()
  43. local pixbuf = clipboard:wait_for_image()
  44. if pixbuf then image.pixbuf = pixbuf end
  45. end
  46. },
  47. }
  48. menu:show_all()
  49. menu:popup(nil, nil, nil, event_button.button, event_button.time)
  50. end
  51. local window = Gtk.Window {
  52. title = "Clipboard demo",
  53. Gtk.Box {
  54. orientation = 'VERTICAL',
  55. border_width = 8,
  56. Gtk.Label { label = "\"Copy\" will copy the text\n" ..
  57. "in the entry to the clipboard" },
  58. Gtk.Box {
  59. orientation = 'HORIZONTAL',
  60. spacing = 4,
  61. border_width = 8,
  62. Gtk.Entry { id = 'copy_entry', hexpand = true },
  63. Gtk.Button {
  64. id = 'copy_button', use_stock = true, label = Gtk.STOCK_COPY
  65. },
  66. },
  67. Gtk.Label { label = "\"Paste\" will paste the text from " ..
  68. "the clipboard to the entry" },
  69. Gtk.Box {
  70. orientation = 'HORIZONTAL',
  71. spacing = 4,
  72. border_width = 8,
  73. Gtk.Entry { id = 'paste_entry', hexpand = true },
  74. Gtk.Button {
  75. id = 'paste_button', use_stock = true, label = Gtk.STOCK_PASTE
  76. },
  77. },
  78. Gtk.Label { label = "Images can be transferred via the clipboard, too" },
  79. Gtk.Box {
  80. orientation = 'HORIZONTAL',
  81. spacing = 4,
  82. border_width = 8,
  83. Gtk.EventBox {
  84. id = 'ebox1',
  85. on_drag_begin = drag_begin,
  86. on_drag_data_get = drag_data_get,
  87. on_drag_data_received = drag_data_received,
  88. on_button_press_event = button_press,
  89. Gtk.Image { stock = Gtk.STOCK_DIALOG_WARNING,
  90. icon_size = Gtk.IconSize.BUTTON }
  91. },
  92. Gtk.EventBox {
  93. id = 'ebox2',
  94. on_drag_begin = drag_begin,
  95. on_drag_data_get = drag_data_get,
  96. on_drag_data_received = drag_data_received,
  97. on_button_press_event = button_press,
  98. Gtk.Image { stock = Gtk.STOCK_STOP,
  99. icon_size = Gtk.IconSize.BUTTON }
  100. },
  101. },
  102. }
  103. }
  104. function window.child.copy_button:on_clicked()
  105. local entry = window.child.copy_entry
  106. local clipboard = entry:get_clipboard(Gdk.SELECTION_CLIPBOARD)
  107. clipboard:set_text(entry.text, -1)
  108. end
  109. function window.child.paste_button:on_clicked()
  110. local entry = window.child.paste_entry
  111. local clipboard = entry:get_clipboard(Gdk.SELECTION_CLIPBOARD)
  112. clipboard:request_text(function(clipboard, text)
  113. if text then entry.text = text end
  114. end)
  115. end
  116. -- Make eboxes drag sources and destinations.
  117. for _, ebox in ipairs { window.child.ebox1, window.child.ebox2 } do
  118. ebox:drag_source_set('BUTTON1_MASK', nil, 'COPY')
  119. ebox:drag_source_add_image_targets()
  120. ebox:drag_dest_set('ALL', nil, 'COPY')
  121. ebox:drag_dest_add_image_targets(ebox)
  122. end
  123. -- Tell the clipboard manager to make the data persistent.
  124. Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD):set_can_store(nil)
  125. window:show_all()
  126. return window
  127. end,
  128. "Clipboard",
  129. table.concat {
  130. "Gtk.Clipboard is used for clipboard handling. This demo shows how to ",
  131. "copy and paste text to and from the clipboard.\n",
  132. "It also shows how to transfer images via the clipboard or via ",
  133. "drag-and-drop, and how to make clipboard contents persist after ",
  134. "the application exits. Clipboard persistence requires a clipboard ",
  135. "manager to run."
  136. }