Tuesday, October 23, 2007

Browsing gnome theme icons in Ruby-GTK

In my previous post I hard-coded an icon name into the Autotest application hook, using this code:
$tray_icon.icon_name = 'apple-red'

Of course, I'm not sure where that icon comes from or whether it's present in all packagings of GNOME.
So, in order to browse for my own icon easily, I wrote the following script that should run as long as you have the latest version of Ruby-GTK installed (you'll need to chmod +x it):
#!/usr/bin/env ruby

require 'gtk2'

module Gtk
w = Window.new
w.signal_connect("destroy") { Gtk.main_quit; exit! }
sw = ScrolledWindow.new
w.add(sw)
icon_list = TreeView.new
sw.add(icon_list)
icon_list.model = icons = ListStore.new(String,Gdk::Pixbuf)
tr = CellRendererText.new
tcol = TreeViewColumn.new('Name',tr,:text => 0)
pr = CellRendererPixbuf.new
pcol = TreeViewColumn.new('Icon',pr,:pixbuf => 1)
icon_list.append_column(tcol)
icon_list.append_column(pcol)
theme = IconTheme.default
theme.icons.sort.each do |icon| item = icons.append; item[0] = icon; item[1] = theme.load_icon(icon,32,0); end
w.show_all
main
end

That'll give you something like this:

Then you can just use the name of the icon you like in place of 'apple-red' in the code. There might be other uses for this too.

0 comments: