>From fa9ef66cda88956043e498e6ab035cdd30eb1fe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20Sch=C3=B6nfeld?= Date: Sun, 17 Feb 2013 18:46:22 +0100 Subject: [PATCH] Add overrides for Gtk3::Image Based on a patch by Dave M. --- lib/Gtk3.pm | 71 ++++++++++++++++++++++--- t/zz-GtkImage.t | 157 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 221 insertions(+), 7 deletions(-) create mode 100644 t/zz-GtkImage.t diff --git a/lib/Gtk3.pm b/lib/Gtk3.pm index 3e9cde2..5af2248 100644 --- a/lib/Gtk3.pm +++ b/lib/Gtk3.pm @@ -70,15 +70,14 @@ my @_GTK_USE_GENERIC_SIGNAL_MARSHALLER_FOR = ( ['Gtk3::InfoBar', 'response', \&Gtk3::Dialog::_gtk3_perl_response_converter], ); +# GtkResponseType: id <-> nick my $_GTK_RESPONSE_ID_TO_NICK = sub { my ($id) = @_; { local $@; my $nick = eval { Glib::Object::Introspection->convert_enum_to_sv ( 'Gtk3::ResponseType', $id) }; - if (defined $nick) { - return $nick; - } + return $nick if defined $nick; } return $id; }; @@ -88,18 +87,47 @@ my $_GTK_RESPONSE_NICK_TO_ID = sub { local $@; my $id = eval { Glib::Object::Introspection->convert_sv_to_enum ( 'Gtk3::ResponseType', $nick) }; - if (defined $id) { - return $id; - } + return $id if defined $id; } return $nick; }; -# Converter for the "response" signal. + +# Converter for GtkDialog's "response" signal. sub Gtk3::Dialog::_gtk3_perl_response_converter { my ($dialog, $id) = @_; return ($dialog, $_GTK_RESPONSE_ID_TO_NICK->($id)); } +# GtkIconSize: id <-> nick +my $_GTK_ICON_SIZE_ID_TO_NICK = sub { + my ($id) = @_; + { + local $@; + my $nick = eval { Glib::Object::Introspection->convert_enum_to_sv ( + 'Gtk3::IconSize', $id) }; + return $nick if defined $nick; + } + { + my $nick = Gtk3::IconSize::get_name ($id); + return $nick if defined $nick; + } + return $id; +}; +my $_GTK_ICON_SIZE_NICK_TO_ID = sub { + my ($nick) = @_; + { + local $@; + my $id = eval { Glib::Object::Introspection->convert_sv_to_enum ( + 'Gtk3::IconSize', $nick) }; + return $id if defined $id; + } + { + my $id = Gtk3::IconSize::from_name ($nick); + return $id if $id;# if it's not zero + } + return $nick; +}; + # - gdk customization ------------------------------------------------------- # my @_GDK_HANDLE_SENTINEL_BOOLEAN_FOR = qw/ @@ -785,6 +813,35 @@ sub Gtk3::HBox::new { $_GTK_BASENAME, 'HBox', 'new', $class, $homogeneous, $spacing); } +# Gtk3::Image +{ + no strict qw(refs); + foreach my $ctor (qw/new_from_stock new_from_icon_set new_from_icon_name new_from_gicon/) { + *{'Gtk3::Image::' . $ctor} = sub { + my ($class, $thing, $size) = @_; + return Glib::Object::Introspection->invoke ( + $_GTK_BASENAME, 'Image', $ctor, $class, $thing, + $_GTK_ICON_SIZE_NICK_TO_ID->($size)); + } + } + foreach my $getter (qw/get_stock get_icon_set get_icon_name get_gicon/) { + *{'Gtk3::Image::' . $getter} = sub { + my ($image) = @_; + my ($thing, $size) = Glib::Object::Introspection->invoke ( + $_GTK_BASENAME, 'Image', $getter, $image); + return ($thing, $_GTK_ICON_SIZE_ID_TO_NICK->($size)); + } + } + foreach my $setter (qw/set_from_stock set_from_icon_set set_from_icon_name set_from_gicon/) { + *{'Gtk3::Image::' . $setter} = sub { + my ($image, $thing, $size) = @_; + Glib::Object::Introspection->invoke ( + $_GTK_BASENAME, 'Image', $setter, $image, $thing, + $_GTK_ICON_SIZE_NICK_TO_ID->($size)); + } + } +} + sub Gtk3::ImageMenuItem::new { my ($class, $mnemonic) = @_; if (defined $mnemonic) { diff --git a/t/zz-GtkImage.t b/t/zz-GtkImage.t new file mode 100644 index 0000000..f845da4 --- /dev/null +++ b/t/zz-GtkImage.t @@ -0,0 +1,157 @@ +#!/usr/bin/perl +# +# Originally copied from Gtk2/t/GtkImage.t +# + +BEGIN { require './t/inc/setup.pl' } + +use strict; +use warnings; + +plan tests => 32; + +# borrowed from xsane-icons.c +my @pixbuf_data = +( + " 20 20 4 1", + " none", + ". c #000000", + "+ c #208020", + "a c #ffffff", + " ", + " ................. ", + " .+++++++++++++++. ", + " .+ . +. ", + " .+ ... +. ", + " .+ . . . +. ", + " .+ . +. ", + " .+ . +. ", + " .+ . . . +. ", + " .+ . . . +. ", + " .+.............+. ", + " .+ . . . +. ", + " .+ . . . +. ", + " .+ . +. ", + " .+ . . . +. ", + " .+ ... +. ", + " .+ . +. ", + " .+++++++++++++++. ", + " ................. ", + " ", +); + +my $pixbuf = Gtk3::Gdk::Pixbuf->new_from_xpm_data (@pixbuf_data); + +# Plain old new ################################################################ + +ok (my $img = Gtk3::Image->new, 'Gtk3::Image->new'); + +is_deeply ([$img->get_icon_set], [undef, 'invalid'], 'get_icon_set empty'); +is ($img->get_pixbuf, undef, 'get_pixbuf empty'); +is_deeply ([$img->get_stock ()], [undef, 'invalid'], 'get_stock empty'); +is ($img->get_animation, undef, 'get_animation empty'); +is ($img->get_storage_type, 'empty', 'get_storage_type empty'); + +# new from stock ############################################################### + +ok ($img = Gtk3::Image->new_from_stock ('gtk-cancel', 'menu'), + 'Gtk3::Image->new_from_stock'); +is ($img->get_storage_type, 'stock', 'new_from_stock get_storage_type'); +is_deeply ([$img->get_stock ()], ['gtk-cancel', 'menu'], + 'new_from_stock get_stock'); + +# new from icon set ############################################################ + +my $iconset = Gtk3::IconSet->new_from_pixbuf ($pixbuf); +ok ($img = Gtk3::Image->new_from_icon_set ($iconset, 'small-toolbar'), + 'Gtk3::Image->new_from_icon_set'); +my @ret = $img->get_icon_set; +is (scalar (@ret), 2, 'new_from_icon_set get_icon_set num rets'); +isa_ok ($ret[0], 'Gtk3::IconSet', 'new_from_icon_set get_icon_set icon_set'); +is ($ret[1], 'small-toolbar', 'new_from_icon_set get_icon_set size'); + +# new from pixbuf ############################################################## + +ok ($img = Gtk3::Image->new_from_pixbuf ($pixbuf), + 'Gtk3::Image->new_from_pixbuf'); +isa_ok ($img->get_pixbuf, 'Gtk3::Gdk::Pixbuf', 'new_from_pixbuf get_pixbuf'); + +# set from stock ############################################################### + +$img->set_from_stock ('gtk-quit', 'dialog'); +is ($img->get_storage_type, 'stock', 'set_from_stock get_storage_type'); +ok (eq_array ([$img->get_stock ()], ['gtk-quit', 'dialog']), + 'set_from_stock get_stock'); + +# set from icon set ############################################################ + +$img->set_from_icon_set ($iconset, 'small-toolbar'); + ret = $img->get_icon_set; +is (scalar (@ret), 2, 'set_from_icon_set get_icon_set num rets'); +isa_ok ($ret[0], 'Gtk3::IconSet', 'set_from_icon_set get_icon_set icon_set'); +is ($ret[1], 'small-toolbar', 'set_from_icon_set get_icon_set size'); + +# set from pixbuf ############################################################## + +$img->set_from_pixbuf (undef); +$img->set_from_pixbuf ($pixbuf); +isa_ok ($img->get_pixbuf, 'Gtk3::Gdk::Pixbuf', 'set_from_pixbuf get_pixbuf'); + +# These require access to a file, so they may be skipped + +my $testfile = './gtk-demo/gnome-foot.png'; + +SKIP: +{ + skip "unable to find test file, $testfile", 7 + unless (-R $testfile); + + my $animation = Gtk3::Gdk::PixbufAnimation->new_from_file ($testfile); + + # new from file ############################################################## + + ok ($img = Gtk3::Image->new_from_file (''), + 'Gtk3::Image->new_from_file undef'); + ok ($img = Gtk3::Image->new_from_file ($testfile), + 'Gtk3::Image->new_from_file'); + isa_ok ($img->get_pixbuf, 'Gtk3::Gdk::Pixbuf', + 'new_from_file get_pixbuf'); + + # new from animation ######################################################### + + ok ($img = Gtk3::Image->new_from_animation ($animation), + 'Gtk3::Image->new_from_animation'); + isa_ok ($img->get_animation, 'Gtk3::Gdk::PixbufAnimation', + 'new_from_animation get_animationf'); + + # set from file ############################################################## + + $img->set_from_file (undef); + $img->set_from_file ($testfile); + isa_ok ($img->get_pixbuf, 'Gtk3::Gdk::Pixbuf', + 'set_from_file get_pixbuf'); + + # set from animation ######################################################### + + $img->set_from_animation ($animation); + isa_ok ($img->get_animation, 'Gtk3::Gdk::PixbufAnimation', + 'set_from_animation get_animation'); +} + +$img = Gtk3::Image->new_from_icon_name ('gtk-ok', 'button'); +isa_ok ($img, 'Gtk3::Image', 'new_from_icon_name isa Gtk3::Image'); +is_deeply ([$img->get_icon_name], ['gtk-ok', 'button'], 'deep get_icon_name'); + +$img->set_from_icon_name ('gtk-cancel', 'menu'); +is_deeply ([$img->get_icon_name], ['gtk-cancel', 'menu'], + 'get_icon_name from Gtk3::Image set_from_icon_name'); + +$img->set_pixel_size (23); +is ($img->get_pixel_size, 23, 'Gtk3::Image get_pixel_size'); + +$img->clear; + +__END__ + +Copyright (C) 2003-2013 by the gtk2-perl team (see the file AUTHORS for the +full list). See LICENSE for more information. -- 1.7.10.4