demo-stockbrowser.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. return function(parent, dir)
  2. local table = require 'table'
  3. local lgi = require 'lgi'
  4. local GLib = lgi.GLib
  5. local GObject = lgi.GObject
  6. local Gtk = lgi.Gtk
  7. local GdkPixbuf = lgi.GdkPixbuf
  8. local BrowserColumn = {
  9. ID = 1,
  10. LABEL = 2,
  11. SMALL_ICON = 3,
  12. ACCEL_STR = 4,
  13. MACRO = 5,
  14. }
  15. local function create_model()
  16. local store = Gtk.ListStore.new {
  17. [BrowserColumn.ID] = GObject.Type.STRING,
  18. [BrowserColumn.LABEL] = GObject.Type.STRING,
  19. [BrowserColumn.SMALL_ICON] = GdkPixbuf.Pixbuf,
  20. [BrowserColumn.ACCEL_STR] = GObject.Type.STRING,
  21. [BrowserColumn.MACRO] = GObject.Type.STRING,
  22. }
  23. local ids = Gtk.stock_list_ids()
  24. table.sort(ids)
  25. local icon_width, icon_height = Gtk.IconSize.lookup(Gtk.IconSize.MENU)
  26. for _, id in ipairs(ids) do
  27. local item = Gtk.stock_lookup(id)
  28. local macro = GLib.ascii_strup(id, -1):gsub(
  29. '^GTK%-', 'Gtk.STOCK_'):gsub('%-', '_')
  30. local small_icon
  31. local icon_set = Gtk.IconFactory.lookup_default(id)
  32. if icon_set then
  33. -- Prefer menu size if it exists, otherwise take the first
  34. -- available size.
  35. local sizes = icon_set:get_sizes()
  36. local size = sizes[0]
  37. for i = 1, #sizes do
  38. if sizes[i] == Gtk.IconSize.MENU then
  39. size = Gtk.IconSize.MENU
  40. break
  41. end
  42. end
  43. small_icon = parent:render_icon_pixbuf(id, size)
  44. if size ~= Gtk.IconSize.MENU then
  45. -- Make the result proper size for thumbnail.
  46. small_icon = small_icon:scale_simple(icon_width, icon_height,
  47. 'BILINEAR')
  48. end
  49. end
  50. local accel_str
  51. if item and item.keyval ~= 0 then
  52. accel_str = Gtk.accelerator_name(item.keyval, item.modifier)
  53. end
  54. store:append {
  55. [BrowserColumn.ID] = id,
  56. [BrowserColumn.LABEL] = item and item.label,
  57. [BrowserColumn.SMALL_ICON] = small_icon,
  58. [BrowserColumn.ACCEL_STR] = accel_str,
  59. [BrowserColumn.MACRO] = macro,
  60. }
  61. end
  62. return store
  63. end
  64. local window = Gtk.Window {
  65. title = "Stock Icons and Items",
  66. default_height = 500,
  67. border_width = 8,
  68. Gtk.Box {
  69. orientation = 'HORIZONTAL',
  70. spacing = 8,
  71. Gtk.ScrolledWindow {
  72. hscrollbar_policy = 'NEVER',
  73. expand = true,
  74. Gtk.TreeView {
  75. id = 'treeview',
  76. model = create_model(),
  77. Gtk.TreeViewColumn {
  78. title = "Identifier",
  79. {
  80. Gtk.CellRendererPixbuf {},
  81. align = 'start',
  82. { stock_id = BrowserColumn.ID },
  83. },
  84. {
  85. Gtk.CellRendererText {},
  86. expand = true,
  87. align = 'start',
  88. { text = BrowserColumn.MACRO },
  89. },
  90. },
  91. Gtk.TreeViewColumn {
  92. title = "Label",
  93. {
  94. Gtk.CellRendererText {},
  95. { text = BrowserColumn.LABEL },
  96. },
  97. },
  98. Gtk.TreeViewColumn {
  99. title = "Accel",
  100. {
  101. Gtk.CellRendererText {},
  102. { text = BrowserColumn.ACCEL_STR },
  103. },
  104. },
  105. Gtk.TreeViewColumn {
  106. title = "ID",
  107. {
  108. Gtk.CellRendererText {},
  109. { text = BrowserColumn.ID },
  110. },
  111. },
  112. },
  113. },
  114. Gtk.Box {
  115. orientation = 'VERTICAL',
  116. spacing = 8,
  117. border_width = 4,
  118. Gtk.Label {
  119. id = 'type_label',
  120. },
  121. Gtk.Image {
  122. id = 'icon_image',
  123. },
  124. Gtk.Label {
  125. id = 'accel_label',
  126. use_underline = true,
  127. },
  128. Gtk.Label {
  129. id = 'macro_label',
  130. },
  131. Gtk.Label {
  132. id = 'id_label',
  133. },
  134. },
  135. }
  136. }
  137. local display = {
  138. type_label = window.child.type_label,
  139. icon_image = window.child.icon_image,
  140. accel_label = window.child.accel_label,
  141. macro_label = window.child.macro_label,
  142. id_label = window.child.id_label,
  143. }
  144. local selection = window.child.treeview:get_selection()
  145. selection.mode = 'BROWSE'
  146. function selection:on_changed()
  147. local model, iter = self:get_selected()
  148. local view = self:get_tree_view()
  149. if model and iter then
  150. local row = model[iter]
  151. if row[BrowserColumn.SMALL_ICON] and row[BrowserColumn.LABEL] then
  152. display.type_label.label = "Icon and Item"
  153. elseif row[BrowserColumn.SMALL_ICON] then
  154. display.type_label.label = "Icon only"
  155. elseif row[BrowserColumn.LABEL] then
  156. display.type_label.label = "Item Only"
  157. else
  158. display.type_label.label = ''
  159. end
  160. display.id_label.label = row[BrowserColumn.ID]
  161. display.macro_label.label = row[BrowserColumn.MACRO]
  162. if row[BrowserColumn.LABEL] then
  163. display.accel_label.label = ("%s %s"):format(
  164. row[BrowserColumn.LABEL], row[BrowserColumn.ACCEL_STR] or '')
  165. else
  166. display.accel_label.label = ''
  167. end
  168. if row[BrowserColumn.SMALL_ICON] then
  169. -- Find the larget available icon size.
  170. local best_size, best_pixels = Gtk.IconSize.INVALID, 0
  171. for _, size in ipairs(Gtk.IconFactory.lookup_default(
  172. row[BrowserColumn.ID]):get_sizes()) do
  173. local width, height = Gtk.IconSize.lookup(size)
  174. if width * height > best_pixels then
  175. best_pixels = width * height
  176. best_size = size
  177. end
  178. end
  179. display.icon_image.stock = row[BrowserColumn.ID]
  180. display.icon_image.icon_size = best_size
  181. else
  182. display.icon_image.pixbuf = nil
  183. end
  184. else
  185. display.type_label.label = "No selected item"
  186. display.macro_label.label = ''
  187. display.id_label.label = ''
  188. display.accel_label.label = ''
  189. display.icon_image.pixbuf = nil
  190. end
  191. end
  192. window:show_all()
  193. return window
  194. end,
  195. "Stock Item and Icon Browser",
  196. table.concat {
  197. [[This source code for this demo doesn't demonstrate anything ]],
  198. [[particularly useful in applications. The purpose of the "demo" ]],
  199. [[is just to provide a handy place to browse the available stock ]],
  200. [[icons and stock items.]]
  201. }