demo-treeview-liststore.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local GLib = lgi.GLib
  4. local GObject = lgi.GObject
  5. local Gtk = lgi.Gtk
  6. local Column = {
  7. FIXED = 1,
  8. NUMBER = 2,
  9. SEVERITY = 3,
  10. DESCRIPTION = 4,
  11. PULSE = 5,
  12. ICON = 6,
  13. ACTIVE = 7,
  14. SENSITIVE = 8,
  15. }
  16. -- Create the list store.
  17. local store = Gtk.ListStore.new {
  18. [Column.FIXED] = GObject.Type.BOOLEAN,
  19. [Column.NUMBER] = GObject.Type.UINT,
  20. [Column.SEVERITY] = GObject.Type.STRING,
  21. [Column.DESCRIPTION] = GObject.Type.STRING,
  22. [Column.PULSE] = GObject.Type.UINT,
  23. [Column.ICON] = GObject.Type.STRING,
  24. [Column.ACTIVE] = GObject.Type.BOOLEAN,
  25. [Column.SENSITIVE] = GObject.Type.BOOLEAN,
  26. }
  27. -- Populate it with sample data.
  28. for i, item in ipairs {
  29. { false, 60482, "Normal", "scrollable notebooks and hidden tabs" },
  30. { false, 60620, "Critical", "gdk_window_clear_area (gdkwindow-win32.c) is not thread-safe" },
  31. { false, 50214, "Major", "Xft support does not clean up correctly" },
  32. { true, 52877, "Major", "GtkFileSelection needs a refresh method. " },
  33. { false, 56070, "Normal", "Can't click button after setting in sensitive" },
  34. { true, 56355, "Normal", "GtkLabel - Not all changes propagate correctly" },
  35. { false, 50055, "Normal", "Rework width/height computations for TreeView" },
  36. { false, 58278, "Normal", "gtk_dialog_set_response_sensitive () doesn't work" },
  37. { false, 55767, "Normal", "Getters for all setters" },
  38. { false, 56925, "Normal", "Gtkcalender size" },
  39. { false, 56221, "Normal", "Selectable label needs right-click copy menu" },
  40. { true, 50939, "Normal", "Add shift clicking to GtkTextView" },
  41. { false, 6112, "Enhancement","netscape-like collapsable toolbars" },
  42. { false, 1, "Normal", "First bug :=)" },
  43. } do
  44. if i == 2 or i == 4 then
  45. item[Column.ICON] = 'battery-caution-charging-symbolic'
  46. end
  47. item[Column.SENSITIVE] = (i ~= 4)
  48. store:append(item)
  49. end
  50. local window = Gtk.Window {
  51. title = "Gtk.ListStore demo",
  52. default_width = 280,
  53. default_height = 250,
  54. border_width = 8,
  55. Gtk.Box {
  56. orientation = 'VERTICAL',
  57. spacing = 8,
  58. Gtk.Label {
  59. label = "This is the bug list (note: not based on real data, "
  60. .. "it would be nice to have a nice ODBC interface to bugzilla "
  61. .. "or so, though"
  62. },
  63. Gtk.ScrolledWindow {
  64. shadow_type = 'ETCHED_IN',
  65. hscrollbar_policy = 'NEVER',
  66. expand = true,
  67. Gtk.TreeView {
  68. id = 'view',
  69. model = store,
  70. Gtk.TreeViewColumn {
  71. title = "Fixed?",
  72. sizing = 'FIXED',
  73. fixed_width = 50,
  74. {
  75. Gtk.CellRendererToggle { id = 'fixed_renderer' },
  76. { active = Column.FIXED },
  77. },
  78. },
  79. Gtk.TreeViewColumn {
  80. title = "Bug number",
  81. sort_column_id = Column.NUMBER - 1,
  82. {
  83. Gtk.CellRendererText {},
  84. { text = Column.NUMBER },
  85. },
  86. },
  87. Gtk.TreeViewColumn {
  88. title = "Severity",
  89. sort_column_id = Column.SEVERITY - 1,
  90. {
  91. Gtk.CellRendererText {},
  92. { text = Column.SEVERITY },
  93. },
  94. },
  95. Gtk.TreeViewColumn {
  96. title = "Description",
  97. sort_column_id = Column.DESCRIPTION - 1,
  98. {
  99. Gtk.CellRendererText {},
  100. { text = Column.DESCRIPTION },
  101. },
  102. },
  103. Gtk.TreeViewColumn {
  104. title = "Spinning",
  105. sort_column_id = Column.PULSE - 1,
  106. {
  107. Gtk.CellRendererSpinner {},
  108. {
  109. pulse = Column.PULSE,
  110. active = Column.ACTIVE
  111. },
  112. },
  113. },
  114. Gtk.TreeViewColumn {
  115. title = "Symbolic icon",
  116. sort_column_id = Column.ICON - 1,
  117. {
  118. Gtk.CellRendererPixbuf { follow_state = true },
  119. {
  120. icon_name = Column.ICON,
  121. sensitive = Column.SENSITIVE,
  122. },
  123. },
  124. },
  125. },
  126. },
  127. },
  128. }
  129. function window.child.fixed_renderer:on_toggled(path_str)
  130. local path = Gtk.TreePath.new_from_string(path_str)
  131. -- Change current value.
  132. store[path][Column.FIXED] = not store[path][Column.FIXED]
  133. end
  134. -- Add 'animation' for the spinner.
  135. local timer = GLib.timeout_add(
  136. GLib.PRIORITY_DEFAULT, 80,
  137. function()
  138. local row = store[store:get_iter_first()]
  139. local pulse = row[Column.PULSE]
  140. pulse = (pulse > 32768) and 0 or pulse + 1
  141. row[Column.PULSE] = pulse
  142. row[Column.ACTIVE] = true
  143. return true
  144. end)
  145. function window:on_destroy()
  146. GLib.source_remove(timer)
  147. end
  148. window:show_all()
  149. return window
  150. end,
  151. "Tree View/List Store",
  152. table.concat {
  153. [[The Gtk.ListStore is used to store data in list form, to be used later ]],
  154. [[on by a Gtk.TreeView to display it. This demo builds a simple ]],
  155. [[Gtk.ListStore and displays it. See the Stock Browser demo for a more ]],
  156. [[advanced example.]],
  157. }