demo-comboboxes.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local GObject = lgi.GObject
  4. local Gtk = lgi.Gtk
  5. local Gdk = lgi.Gdk
  6. local GdkPixbuf = lgi.GdkPixbuf
  7. local ComboColumn = {
  8. PIXBUF = 1,
  9. TEXT = 2,
  10. }
  11. local function create_stock_icon_store()
  12. local cellview = Gtk.CellView {}
  13. local store = Gtk.ListStore.new {
  14. [ComboColumn.PIXBUF] = GdkPixbuf.Pixbuf,
  15. [ComboColumn.TEXT] = GObject.Type.STRING
  16. }
  17. for _, stock in ipairs {
  18. Gtk.STOCK_DIALOG_WARNING,
  19. Gtk.STOCK_STOP,
  20. Gtk.STOCK_NEW,
  21. Gtk.STOCK_CLEAR,
  22. '',
  23. Gtk.STOCK_OPEN,
  24. } do
  25. if stock ~= '' then
  26. store:append {
  27. [ComboColumn.PIXBUF] = cellview:render_icon_pixbuf(
  28. stock, Gtk.IconSize.BUTTON),
  29. [ComboColumn.TEXT] = Gtk.stock_lookup(stock).label:gsub('_', '')
  30. }
  31. else
  32. store:append {
  33. [ComboColumn.TEXT] = 'separator'
  34. }
  35. end
  36. end
  37. return store
  38. end
  39. local function set_sensitive(layout, cell, model, iter)
  40. local indices = model:get_path(iter):get_indices()
  41. cell.sensitive = (indices[1] ~= 1)
  42. end
  43. local function create_capital_store()
  44. local store = Gtk.TreeStore.new { GObject.Type.STRING }
  45. for _, group in ipairs {
  46. { name = "A - B",
  47. "Albany", "Annapolis", "Atlanta", "Augusta", "Austin",
  48. "Baton Rouge", "Bismarck", "Boise", "Boston" },
  49. { name = "C - D",
  50. "Carson City", "Charleston", "Cheyenne", "Columbia", "Columbus",
  51. "Concord", "Denver", "Des Moines", "Dover" },
  52. { name = "E - J",
  53. "Frankfort", "Harrisburg", "Hartford", "Helena", "Honolulu",
  54. "Indianapolis", "Jackson", "Jefferson City", "Juneau" },
  55. { name = "K - O",
  56. "Lansing", "Lincoln", "Little Rock", "Madison", "Montgomery",
  57. "Montpelier", "Nashville", "Oklahoma City", "Olympia" },
  58. { name = "P - S",
  59. "Phoenix", "Pierre", "Providence", "Raleigh", "Richmond",
  60. "Sacramento", "Salem", "Salt Lake City", "Santa Fe",
  61. "Springfield", "St. Paul" },
  62. { name = "T - Z",
  63. "Tallahassee", "Topeka", "Trenton" },
  64. } do
  65. local gi = store:append(nil, { [1] = group.name })
  66. for _, city in ipairs(group) do
  67. store:append(gi, { [1] = city })
  68. end
  69. end
  70. return store
  71. end
  72. local function is_capital_sensitive(layout, cell, model, iter)
  73. cell.sensitive = not model:iter_has_child(iter)
  74. end
  75. local window = Gtk.Window {
  76. title = "Combo boxes",
  77. border_width = 10,
  78. Gtk.Box {
  79. orientation = 'VERTICAL',
  80. spacing = 10,
  81. Gtk.Frame {
  82. label = "Some stock icons",
  83. Gtk.Box {
  84. orientation = 'VERTICAL',
  85. border_width = 5,
  86. Gtk.ComboBox {
  87. id = 'icons',
  88. model = create_stock_icon_store(),
  89. active = 0,
  90. cells = {
  91. {
  92. Gtk.CellRendererPixbuf(),
  93. { pixbuf = ComboColumn.PIXBUF },
  94. align = 'start',
  95. data_func = set_sensitive,
  96. },
  97. {
  98. Gtk.CellRendererText(),
  99. { text = ComboColumn.TEXT },
  100. align = 'start',
  101. data_func = set_sensitive,
  102. },
  103. },
  104. },
  105. },
  106. },
  107. Gtk.Frame {
  108. label = "Where are we?",
  109. Gtk.Box {
  110. orientation = 'VERTICAL',
  111. border_width = 5,
  112. Gtk.ComboBox {
  113. id = 'capitals',
  114. model = create_capital_store(),
  115. cells = {
  116. {
  117. Gtk.CellRendererText(),
  118. { text = 1 },
  119. align = 'start',
  120. data_func = is_capital_sensitive,
  121. }
  122. }
  123. },
  124. },
  125. },
  126. Gtk.Frame {
  127. label = "Editable",
  128. Gtk.Box {
  129. orientation = 'VERTICAL',
  130. border_width = 5,
  131. Gtk.ComboBoxText {
  132. id = 'entry',
  133. has_entry = true,
  134. entry_text_column = 0,
  135. },
  136. },
  137. },
  138. Gtk.Frame {
  139. label = "String IDs",
  140. Gtk.Box {
  141. orientation = 'VERTICAL',
  142. border_width = 5,
  143. Gtk.ComboBoxText {
  144. id = 'stringids',
  145. entry_text_column = 0,
  146. id_column = 1,
  147. },
  148. Gtk.Entry { id = 'ids_entry' },
  149. },
  150. },
  151. },
  152. }
  153. window.child.icons:set_row_separator_func(
  154. function(model, iter)
  155. return model:get_path(iter):get_indices()[1] == 4
  156. end)
  157. local capitals = window.child.capitals
  158. capitals:set_active_iter(capitals.model:get_iter(
  159. Gtk.TreePath.new_from_string('0:8')))
  160. for _, label in ipairs { "One", "Two", "2½", "Three" } do
  161. window.child.entry:append_text(label)
  162. end
  163. local entry = window.child.entry:get_child()
  164. local allowed = { ["One"] = true, ["Two"] = true,
  165. ["2½"] = true, ["Three"] = true }
  166. function entry:on_changed()
  167. local color
  168. if not self.text:match('^[0-9]*$') and not allowed[self.text] then
  169. color = Gdk.RGBA { red = 1, green = 0.9, blue = 0.9, alpha = 1 }
  170. end
  171. self:override_color(0, color)
  172. end
  173. for id, text in pairs {
  174. never = "Not visible",
  175. when_active = "Visible when active",
  176. always = "Always visible",
  177. } do
  178. window.child.stringids:append(id, text)
  179. end
  180. window.child.stringids:bind_property(
  181. 'active-id', window.child.ids_entry, 'text', 'BIDIRECTIONAL')
  182. window:show_all()
  183. return window
  184. end,
  185. "Combo boxes",
  186. table.concat {
  187. "The ComboBox widget allows to select one option out of a list. ",
  188. "The ComboBoxEntry additionally allows the user to enter a value ",
  189. "that is not in the list of options.\n",
  190. "How the options are displayed is controlled by cell renderers.",
  191. }