ruby-****@sourc*****
ruby-****@sourc*****
2003年 9月 11日 (木) 16:37:58 JST
------------------------- REMOTE_ADDR = 195.0.122.201 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/?tips_toolbar_icon ------------------------- = Changes an icon on a button of a toolbar - Gtk::Toolbar and also a lot of widgets is a subclass of Gtk::Container or Gtk::Bin. They have child widgets and you can get each child widgets and send methods to the child widgets. + Gtk::Toolbar, like a lot of other widgets, is either a subclass of Gtk::Container or Gtk::Bin. They can contain child widgets and you can iterate over each child and send methods to it. Here is a sample script. require 'gtk2' Gtk.init toolbar = Gtk::Toolbar.new toolbar.append(Gtk::Stock::NEW) do toolbar.children[0].child.children[0].set(Gtk::Stock::QUIT, Gtk::IconSize::SMALL_TOOLBAR) #(1) end Gtk::Window.new.add(toolbar).show_all Gtk.main - Click button, then icon will be changed. - a line of (1) can also write like as follows: - #Of course, you can use both way as you like. + Click the button, and then icon will be changed. + Instead of line (1) you can also write: button = toolbar.children[0] vbox = button.child image = vbox.children[0] image.set(Gtk::Stock::QUIT, Gtk::IconSize::SMALL_TOOLBAR) - And follow image shows these objects relationship. + 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: ((<Containers|URL:/ja/hiki.cgi?c=plugin;plugin=attach_download;p=tips_toolbar;file_name=containers.jpg>)) {{br}} - (1) Gtk::Toolbar is a subclass of Gtk::Container. So you can get all child widgets by Gtk::Container#children. Then, the first child is the target object(Gtk::Button). So you get here by toolbar.children[0]. - (2) Gtk::Button is a subclass of Gtk::Bin, so it has a child only. You can get the child widget by Gtk::Bin#child. In this case, the child widget is Gtk::VBox. - (3) Gtk::VBox is a subclass of Gtk::Container, too. So you get Gtk::Image by vbox.children[0]. - (4) Use Gtk::Image#set to change the icon. + (1) 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[0]. + (2) 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. + (3) Gtk::VBox is a also subclass of Gtk::Container. So you get the Gtk::Image object by using vbox.children[0]. + (4) Finally, use Gtk::Image#set to change the icon. == ChangeLog + :2003-09-11 ((<Laurent|lrz>)) + Fixed English. :2003-09-11 ((<Masao>)) Initial release.