Re: using own icons with Gtk2::UIManager



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.

-- 
Emmanuele Bassi,  E: ebassi gmail com
W: http://www.emmanuelebassi.net
B: http://log.emmanuelebassi.net




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