Re: using own icons with Gtk2::UIManager



Am Sonntag, 10. September 2006 01:21 schrieb Emmanuele Bassi:
On Sat, 2006-09-09 at 23:11 +0200, Michael Hartmann wrote:
Hi everybody,

is it possible to use own icons with Gtk2::UIManager?
I want to use some Icons for a menu and a toolbar. I've read the
manpages, but haven't found anything useful yet.

You must use stock icons as Gtk2::Action can only use those - so you
have to add your own icons to the stock icons list using Gtk2::IconSet
and Gtk2::IconFactory:

  my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file(...);
  my $set = Gtk2::IconSet->new_from_pixbuf($pixbuf);
  my $factory = Gtk2::IconFactory->new();

  $factory->add('your-stock-id', $set);
  $factory->add_default(); # this adds the factory to the queried list

You have to keep the icon factory around for the duration of the
application, so it's safe to use a main initialisation function:

  my $IconFactory = undef;

  sub register_icon {
      my ($stock_id, $path);

      return unless defined $IconFactory;

      my $icon;
      eval { $icon = Gtk2::Gdk::Pixbuf->new_from_file($path) };
      if ($@) {
          croak("Unable to load icon at `$path': $@");
      }

      my $set = Gtk2::IconSet->new_from_pixbuf($icon);
      $IconFactory->add($stock_id, $set);
  }

  sub init_icons {
    return if defined $IconFactory;

    $IconFactory = Gtk2::IconFactory->new();
    $IconFactory->add_default();

    register_icon('foo', '/path/to/foo.png');
    register_icon('bar', '/path/to/bar.png');
  }

Ciao,
 Emmanuele.

Thank you for your answer. It finally works.
At the moment I have the problem that I don't know how to check what cell in a 
TreeView Widget selected is. 
However, the main problem at the moment is that I don't know where to look for 
help. The manpages are quite useful when you exactly know what you're looking 
for, the gtk2-perl study guide is also very useful, but not always as 
detailed as I need it. The gtk2 tutorial is not very often helpful.
Googling just shows PODs. Any advice



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]