demo-treeview-editable.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local GObject = lgi.GObject
  4. local Gtk = lgi.Gtk
  5. local ItemColumn = {
  6. NUMBER = 1,
  7. PRODUCT = 2,
  8. YUMMY = 3,
  9. }
  10. local NumberColumn = {
  11. TEXT = 1,
  12. NUMBER = 2,
  13. }
  14. -- Fill store with initial items.
  15. local item_store = Gtk.ListStore.new {
  16. [ItemColumn.NUMBER] = GObject.Type.INT,
  17. [ItemColumn.PRODUCT] = GObject.Type.STRING,
  18. [ItemColumn.YUMMY] = GObject.Type.INT,
  19. }
  20. for _, item in ipairs {
  21. {
  22. [ItemColumn.NUMBER] = 3,
  23. [ItemColumn.PRODUCT] = "bottles of coke",
  24. [ItemColumn.YUMMY] = 20,
  25. },
  26. {
  27. [ItemColumn.NUMBER] = 5,
  28. [ItemColumn.PRODUCT] = "packages of noodles",
  29. [ItemColumn.YUMMY] = 50,
  30. },
  31. {
  32. [ItemColumn.NUMBER] = 2,
  33. [ItemColumn.PRODUCT] = "packages of chocolate chip cookies",
  34. [ItemColumn.YUMMY] = 90,
  35. },
  36. {
  37. [ItemColumn.NUMBER] = 1,
  38. [ItemColumn.PRODUCT] = "can vanilla ice cream",
  39. [ItemColumn.YUMMY] = 60,
  40. },
  41. {
  42. [ItemColumn.NUMBER] = 6,
  43. [ItemColumn.PRODUCT] = "eggs",
  44. [ItemColumn.YUMMY] = 10,
  45. },
  46. } do
  47. item_store:append(item)
  48. end
  49. -- Fill store with numbers.
  50. local number_store = Gtk.ListStore.new {
  51. [NumberColumn.TEXT] = GObject.Type.INT,
  52. [NumberColumn.NUMBER] = GObject.Type.STRING,
  53. }
  54. for i = 1, 10 do
  55. number_store:append {
  56. [NumberColumn.TEXT] = i,
  57. [NumberColumn.NUMBER] = i,
  58. }
  59. end
  60. local window = Gtk.Window {
  61. title = "Shopping list",
  62. default_width = 320,
  63. default_height = 200,
  64. border_width = 5,
  65. Gtk.Box {
  66. orientation = 'VERTICAL',
  67. spacing = 5,
  68. Gtk.Label {
  69. label = "Shopping list (you can edit the cells!)",
  70. },
  71. Gtk.ScrolledWindow {
  72. shadow_type = 'ETCHED_IN',
  73. expand = true,
  74. Gtk.TreeView {
  75. id = 'view',
  76. model = item_store,
  77. Gtk.TreeViewColumn {
  78. title = "Number",
  79. {
  80. Gtk.CellRendererCombo {
  81. id = 'number_renderer',
  82. model = number_store,
  83. text_column = NumberColumn.TEXT,
  84. has_entry = false,
  85. editable = true
  86. },
  87. { text = ItemColumn.NUMBER, }
  88. },
  89. },
  90. Gtk.TreeViewColumn {
  91. title = "Product",
  92. {
  93. Gtk.CellRendererText {
  94. id = 'product_renderer',
  95. editable = true,
  96. },
  97. { text = ItemColumn.PRODUCT, }
  98. }
  99. },
  100. Gtk.TreeViewColumn {
  101. title = "Yummy",
  102. {
  103. Gtk.CellRendererProgress {},
  104. {
  105. value = ItemColumn.YUMMY,
  106. },
  107. }
  108. },
  109. },
  110. },
  111. Gtk.Box {
  112. orientation = 'HORIZONTAL',
  113. spacing = 4,
  114. homogeneous = true,
  115. Gtk.Button {
  116. id = 'add',
  117. label = "Add item",
  118. },
  119. Gtk.Button {
  120. id = 'remove',
  121. label = "Remove item",
  122. },
  123. },
  124. }
  125. }
  126. function window.child.number_renderer:on_edited(path_string, new_text)
  127. local path = Gtk.TreePath.new_from_string(path_string)
  128. item_store[path][ItemColumn.NUMBER] = new_text
  129. end
  130. function window.child.number_renderer:on_editing_started(editable, path)
  131. editable:set_row_separator_func(
  132. function(model, iter)
  133. return model:get_path(iter):get_indices()[1] == 5
  134. end)
  135. end
  136. function window.child.product_renderer:on_edited(path_string, new_text)
  137. local path = Gtk.TreePath.new_from_string(path_string)
  138. item_store[path][ItemColumn.PRODUCT] = new_text
  139. end
  140. local selection = window.child.view:get_selection()
  141. selection.mode = 'SINGLE'
  142. function window.child.add:on_clicked()
  143. item_store:append {
  144. [ItemColumn.NUMBER] = 0,
  145. [ItemColumn.PRODUCT] = "Description here",
  146. [ItemColumn.YUMMY] = 50,
  147. }
  148. end
  149. function window.child.remove:on_clicked()
  150. local model, iter = selection:get_selected()
  151. if model and iter then
  152. model:remove(iter)
  153. end
  154. end
  155. window:show_all()
  156. return window
  157. end,
  158. "Tree View/Editable Cells",
  159. table.concat {
  160. [[This demo demonstrates the use of editable cells in a Gtk.TreeView. ]],
  161. [[If you're new to the Gtk.TreeView widgets and associates, look into ]],
  162. [[the Gtk.ListStore example first. It also shows how to use ]],
  163. [[the Gtk.CellRenderer::editing-started signal to do custom setup of ]],
  164. [[the editable widget.
  165. The cell renderers used in this demo are Gtk.CellRendererText, ]],
  166. [[Gtk.CellRendererCombo and GtkCell.RendererProgress.]]
  167. }