demo-iconview-editing.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. return function(parent, dir)
  2. local math = require 'math'
  3. local lgi = require 'lgi'
  4. local GObject = lgi.GObject
  5. local Gtk = lgi.Gtk
  6. local Gdk = lgi.Gdk
  7. local GdkPixbuf = lgi.GdkPixbuf
  8. local store = Gtk.ListStore.new { GObject.Type.STRING }
  9. for _, name in ipairs { "Red", "Green", "Blue", "Yellow" } do
  10. store:append { name }
  11. end
  12. local function edited(cell, path_string, text)
  13. store[Gtk.TreePath.new_from_string(path_string)][1] = text
  14. end
  15. local function set_cell_color(layout, cell, model, iter)
  16. local rgba, pixel = Gdk.RGBA(), 0
  17. local label = model[iter][1]
  18. if label and rgba:parse(label) then
  19. pixel =
  20. math.floor(rgba.red * 255) * (256 * 256 * 256) +
  21. math.floor(rgba.green * 255) * (256 * 256) +
  22. math.floor(rgba.blue * 255) * 256
  23. end
  24. cell.pixbuf = GdkPixbuf.Pixbuf.new('RGB', false, 8, 24, 24)
  25. cell.pixbuf:fill(pixel)
  26. end
  27. local window = Gtk.Window {
  28. title = "Editing and Drag-and-Drop",
  29. Gtk.IconView {
  30. id = 'icon_view',
  31. expand = true,
  32. model = store,
  33. selection_mode = 'SINGLE',
  34. item_orientation = 'HORIZONTAL',
  35. columns = 2,
  36. reorderable = true,
  37. cells = {
  38. {
  39. align = 'start', expand = true,
  40. Gtk.CellRendererPixbuf(),
  41. set_cell_color,
  42. },
  43. {
  44. align = 'start', expand = true,
  45. Gtk.CellRendererText {
  46. editable = true,
  47. on_edited = edited,
  48. },
  49. { text = 1 }
  50. },
  51. },
  52. },
  53. }
  54. window:show_all()
  55. return window
  56. end,
  57. "Icon View/Editing and Drag-and-Drop",
  58. table.concat {
  59. [[The Gtk.IconView widget supports Editing and Drag-and-Drop. ]],
  60. [[This example also demonstrates using the generic Gtk.CellLayout ]],
  61. [[interface to set up cell renderers in an icon view.]]
  62. }