demo-treeview-treestore.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local GObject = lgi.GObject
  4. local Gtk = lgi.Gtk
  5. local Column = {
  6. HOLIDAY_NAME = 1,
  7. ALEX = 2,
  8. HAVOC = 3,
  9. TIM = 4,
  10. OWEN = 5,
  11. DAVE = 6,
  12. VISIBLE = 7,
  13. WORLD = 8,
  14. }
  15. local store = Gtk.TreeStore.new {
  16. [Column.HOLIDAY_NAME] = GObject.Type.STRING,
  17. [Column.ALEX] = GObject.Type.BOOLEAN,
  18. [Column.HAVOC] = GObject.Type.BOOLEAN,
  19. [Column.TIM] = GObject.Type.BOOLEAN,
  20. [Column.OWEN] = GObject.Type.BOOLEAN,
  21. [Column.DAVE] = GObject.Type.BOOLEAN,
  22. [Column.VISIBLE] = GObject.Type.BOOLEAN,
  23. [Column.WORLD] = GObject.Type.BOOLEAN,
  24. }
  25. for _, month in ipairs {
  26. { "January", {
  27. { "New Years Day", true, true, true, true, false, true, },
  28. { "Presidential Inauguration", false, true, false, true, false, false },
  29. {"Martin Luther King Jr. day", false, true, false, true, false, false },
  30. }
  31. },
  32. { "February", {
  33. { "Presidents' Day", false, true, false, true, false, false },
  34. { "Groundhog Day", false, false, false, false, false, false },
  35. { "Valentine's Day", false, false, false, false, true, true },
  36. }
  37. },
  38. { "March", {
  39. { "National Tree Planting Day", false, false, false, false, false, false },
  40. { "St Patrick's Day", false, false, false, false, false, true },
  41. }
  42. },
  43. { "April", {
  44. { "April Fools' Day", false, false, false, false, false, true },
  45. { "Army Day", false, false, false, false, false, false },
  46. { "Earth Day", false, false, false, false, false, true },
  47. { "Administrative Professionals' Day", false, false, false, false, false, false },
  48. }
  49. },
  50. { "May", {
  51. { "Nurses' Day", false, false, false, false, false, false },
  52. { "National Day of Prayer", false, false, false, false, false, false },
  53. { "Mothers' Day", false, false, false, false, false, true },
  54. { "Armed Forces Day", false, false, false, false, false, false },
  55. { "Memorial Day", true, true, true, true, false, true },
  56. }
  57. },
  58. { "June", {
  59. { "June Fathers' Day", false, false, false, false, false, true },
  60. { "Juneteenth (Liberation of Slaves)", false, false, false, false, false, false },
  61. { "Flag Day", false, true, false, true, false, false },
  62. }
  63. },
  64. { "July", {
  65. { "Parents' Day", false, false, false, false, false, true },
  66. { "Independence Day", false, true, false, true, false, false },
  67. }
  68. },
  69. { "August", {
  70. { "Air Force Day", false, false, false, false, false, false },
  71. { "Coast Guard Day", false, false, false, false, false, false },
  72. { "Friendship Day", false, false, false, false, false, false },
  73. }
  74. },
  75. { "September", {
  76. { "Grandparents' Day", false, false, false, false, false, true },
  77. { "Citizenship Day or Constitution Day", false, false, false, false, false, false },
  78. { "Labor Day", true, true, true, true, false, true },
  79. }
  80. },
  81. { "October", {
  82. { "National Children's Day", false, false, false, false, false, false },
  83. { "Bosses' Day", false, false, false, false, false, false },
  84. { "Sweetest Day", false, false, false, false, false, false },
  85. { "Mother-in-Law's Day", false, false, false, false, false, false },
  86. { "Navy Day", false, false, false, false, false, false },
  87. { "Columbus Day", false, true, false, true, false, false },
  88. { "Halloween", false, false, false, false, false, true },
  89. }
  90. },
  91. { "November", {
  92. { "Marine Corps Day", false, false, false, false, false, false },
  93. { "Veterans' Day", true, true, true, true, false, true },
  94. { "Thanksgiving", false, true, false, true, false, false },
  95. }
  96. },
  97. { "December", {
  98. { "Pearl Harbor Remembrance Day", false, false, false, false, false, false },
  99. { "Christmas", true, true, true, true, false, true },
  100. { "Kwanzaa", false, false, false, false, false, false },
  101. }
  102. },
  103. } do
  104. local iter = store:append(nil, { [Column.HOLIDAY_NAME] = month[1] })
  105. for _, holiday in ipairs(month[2]) do
  106. store:append(iter, {
  107. [Column.HOLIDAY_NAME] = holiday[1],
  108. [Column.ALEX] = holiday[2],
  109. [Column.HAVOC] = holiday[3],
  110. [Column.TIM] = holiday[4],
  111. [Column.OWEN] = holiday[5],
  112. [Column.DAVE] = holiday[6],
  113. [Column.VISIBLE] = true,
  114. [Column.WORLD] = holiday[7],
  115. })
  116. end
  117. end
  118. local window = Gtk.Window {
  119. title = "Card planning sheet",
  120. default_width = 650,
  121. default_height = 400,
  122. Gtk.Box {
  123. orientation = 'VERTICAL',
  124. spacing = 8,
  125. border_width = 8,
  126. Gtk.Label {
  127. label = "Jonathan's Holiday Card Planning Sheet",
  128. },
  129. Gtk.ScrolledWindow {
  130. shadow_type = 'ETCHED_IN',
  131. expand = true,
  132. Gtk.TreeView {
  133. id = 'view',
  134. model = store,
  135. rules_hint = true,
  136. Gtk.TreeViewColumn {
  137. title = "Holiday",
  138. { Gtk.CellRendererText {}, { text = Column.HOLIDAY_NAME } },
  139. },
  140. },
  141. },
  142. }
  143. }
  144. local view = window.child.view
  145. local selection = view:get_selection()
  146. selection.mode = 'MULTIPLE'
  147. -- Add columns programmatically.
  148. for _, info in ipairs {
  149. { Column.ALEX, "Alex", true },
  150. { Column.HAVOC, "Havoc" },
  151. { Column.TIM, "Tim", true },
  152. { Column.OWEN, "Owen" },
  153. { Column.DAVE, "Dave" },
  154. } do
  155. -- Prepare renderer and connect its on_toggled signal.
  156. local col = info[1]
  157. local renderer = Gtk.CellRendererToggle {
  158. xalign = 0,
  159. }
  160. function renderer:on_toggled(path_str)
  161. local row = store[Gtk.TreePath.new_from_string(path_str)]
  162. row[col] = not row[col]
  163. end
  164. -- Add new column to the view.
  165. view:append_column(
  166. Gtk.TreeViewColumn {
  167. title = info[2],
  168. sizing = 'FIXED',
  169. fixed_width = 50,
  170. clickable = true,
  171. {
  172. renderer,
  173. {
  174. active = col,
  175. visible = Column.VISIBLE,
  176. activatable = info[3] and Column.WORLD or nil,
  177. }
  178. }
  179. })
  180. end
  181. -- Expand all rows after treeview has been realized.
  182. view.on_realize = view.expand_all
  183. window:show_all()
  184. return window
  185. end,
  186. "Tree View/Tree Store",
  187. table.concat {
  188. [[The Gtk.TreeStore is used to store data in tree form, to be used later ]],
  189. [[on by a Gtk.TreeView to display it. This demo builds a simple ]],
  190. [[Gtk.TreeStore and displays it. If you're new to the Gtk.TreeView ]],
  191. [[widgets and associates, look into the Gtk.ListStore example first.]],
  192. }