Here is a sample script.
require 'gtk2' toolbar = Gtk::Toolbar.new toolbar.append(Gtk::Stock::NEW) do toolbar.children.child.children.set(Gtk::Stock::QUIT, Gtk::IconSize::SMALL_TOOLBAR) #(1) end Gtk::Window.new.add(toolbar).show_all Gtk.main
Click the button, and then icon will be changed. Instead of line (1) you can also write:
button = toolbar.children vbox = button.child image = vbox.children image.set(Gtk::Stock::QUIT, Gtk::IconSize::SMALL_TOOLBAR)
Of course, you can use both ways as you like. But the first one uses the Ruby call chaining feature. And the following figure shows relationships amongst these objects:
- Gtk::Toolbar is a subclass of Gtk::Container, so you can get all child widgets by using Gtk::Container#children. Here, the first child is the target object (Gtk::Button), so you can retrieve it with toolbar.children.
- Gtk::Button is a subclass of Gtk::Bin, so it has only one child. You can retrieve the child widget with Gtk::Bin#child. In this case, the child widget is a Gtk::VBox.
- Gtk::VBox is a also subclass of Gtk::Container. So you get the Gtk::Image object by using vbox.children.
- Finally, use Gtk::Image#set to change the icon.