| 1 | package Gtk2::Ex::FormFactory::Image; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use base qw( Gtk2::Ex::FormFactory::Widget ); | ||
| 6 | |||
| 7 | sub get_type { "image" } | ||
| 8 | |||
| 9 | sub get_with_frame { shift->{with_frame} } | ||
| 10 | sub get_bgcolor { shift->{bgcolor} } | ||
| 11 | sub get_scale_to_fit { shift->{scale_to_fit} } | ||
| 12 | sub get_max_width { shift->{max_width} } | ||
| 13 | sub get_max_height { shift->{max_height} } | ||
| 14 | sub get_widget_width { shift->{widget_width} } | ||
| 15 | sub get_widget_height { shift->{widget_height} } | ||
| 16 | sub get_scale { shift->{scale} } | ||
| 17 | sub get_scale_hook { shift->{scale_hook} } | ||
| 18 | sub get_gtk_event_box { shift->{gtk_event_box} } | ||
| 19 | |||
| 20 | sub set_with_frame { shift->{with_frame} = $_[1] } | ||
| 21 | sub set_bgcolor { shift->{bgcolor} = $_[1] } | ||
| 22 | sub set_scale_to_fit { shift->{scale_to_fit} = $_[1] } | ||
| 23 | sub set_max_width { shift->{max_width} = $_[1] } | ||
| 24 | sub set_max_height { shift->{max_height} = $_[1] } | ||
| 25 | sub set_widget_width { shift->{widget_width} = $_[1] } | ||
| 26 | sub set_widget_height { shift->{widget_height} = $_[1] } | ||
| 27 | sub set_scale { shift->{scale} = $_[1] } | ||
| 28 | sub set_scale_hook { shift->{scale_hook} = $_[1] } | ||
| 29 | sub set_gtk_event_box { shift->{gtk_event_box} = $_[1] } | ||
| 30 | |||
| 31 | sub get_gtk_signal_widget { | ||
| 32 | shift->get_gtk_event_box; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub new { | ||
| 36 | my $class = shift; | ||
| 37 | my %par = @_; | ||
| 38 | my ($with_frame, $bgcolor, $scale_to_fit, $max_width) = | ||
| 39 | @par{'with_frame','bgcolor','scale_to_fit','max_width'}; | ||
| 40 | my ($max_height, $scale, $scale_hook) = | ||
| 41 | @par{'max_height','scale','scale_hook'}; | ||
| 42 | |||
| 43 | my $self = $class->SUPER::new(@_); | ||
| 44 | |||
| 45 | $scale ||= 1 unless $scale_to_fit || $scale_hook; | ||
| 46 | |||
| 47 | $self->set_with_frame($with_frame); | ||
| 48 | $self->set_bgcolor($bgcolor); | ||
| 49 | $self->set_scale_to_fit($scale_to_fit); | ||
| 50 | $self->set_max_width($max_width); | ||
| 51 | $self->set_max_height($max_height); | ||
| 52 | $self->set_scale($scale); | ||
| 53 | $self->set_scale_hook($scale_hook); | ||
| 54 | |||
| 55 | return $self; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub object_to_widget { | ||
| 59 | my $self = shift; | ||
| 60 | |||
| 61 | my $filename = $self->get_object_value; | ||
| 62 | return $self->empty_widget unless $filename and -r $filename; | ||
| 63 | |||
| 64 | my $gtk_image = $self->get_gtk_widget; | ||
| 65 | my $gtk_pixbuf = Gtk2::Gdk::Pixbuf->new_from_file($filename); | ||
| 66 | |||
| 67 | my $scale_to_fit = $self->get_scale_to_fit; | ||
| 68 | my $max_width = $self->get_max_width; | ||
| 69 | my $max_height = $self->get_max_height; | ||
| 70 | my $scale = $self->get_scale; | ||
| 71 | my $scale_hook = $self->get_scale_hook; | ||
| 72 | |||
| 73 | if ( defined $scale ) { | ||
| 74 | my $image_width = $gtk_pixbuf->get_width; | ||
| 75 | my $image_height = $gtk_pixbuf->get_height; | ||
| 76 | |||
| 77 | if ( $scale == 1 ) { | ||
| 78 | $gtk_image->set_from_pixbuf ( $gtk_pixbuf ); | ||
| 79 | $gtk_image->set_size_request ($image_width, $image_height); | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | my $new_width = int($image_width * $scale); | ||
| 84 | my $new_height = int($image_height * $scale); | ||
| 85 | return if $new_width <= 0 or $new_height <= 0; | ||
| 86 | |||
| 87 | my $scaled_pixbuf = $gtk_pixbuf->scale_simple ( | ||
| 88 | $new_width, $new_height, "GDK_INTERP_BILINEAR" | ||
| 89 | ); | ||
| 90 | |||
| 91 | $gtk_image->set_from_pixbuf ( $scaled_pixbuf ); | ||
| 92 | $gtk_image->set_size_request ($image_width, $image_height); | ||
| 93 | |||
| 94 | } elsif ( $scale_hook ) { | ||
| 95 | my $scale = &$scale_hook($self, $gtk_pixbuf); | ||
| 96 | |||
| 97 | my $image_width = $gtk_pixbuf->get_width; | ||
| 98 | my $image_height = $gtk_pixbuf->get_height; | ||
| 99 | |||
| 100 | my $new_width = int($image_width * $scale); | ||
| 101 | my $new_height = int($image_height * $scale); | ||
| 102 | return if $new_width <= 0 or $new_height <= 0; | ||
| 103 | |||
| 104 | my $scaled_pixbuf = $gtk_pixbuf->scale_simple ( | ||
| 105 | $new_width, $new_height, "GDK_INTERP_BILINEAR" | ||
| 106 | ); | ||
| 107 | |||
| 108 | $gtk_image->set_from_pixbuf ( $scaled_pixbuf ); | ||
| 109 | |||
| 110 | } elsif ( $scale_to_fit or $max_width or $max_height ) { | ||
| 111 | my $image_width = $gtk_pixbuf->get_width; | ||
| 112 | my $image_height = $gtk_pixbuf->get_height; | ||
| 113 | |||
| 114 | my $widget_width = $self->get_widget_width; | ||
| 115 | my $widget_height = $self->get_widget_height; | ||
| 116 | |||
| 117 | $widget_width = $max_width if defined $max_width and | ||
| 118 | $max_width < $widget_width; | ||
| 119 | $widget_height = $max_height if defined $max_height and | ||
| 120 | $max_height < $widget_height; | ||
| 121 | |||
| 122 | my $width_scale = $widget_width / $image_width; | ||
| 123 | my $height_scale = $widget_height / $image_height; | ||
| 124 | |||
| 125 | my $scale = $width_scale < $height_scale ? | ||
| 126 | $width_scale : $height_scale; | ||
| 127 | |||
| 128 | my $new_width = int($image_width * $scale); | ||
| 129 | my $new_height = int($image_height * $scale); | ||
| 130 | return if $new_width <= 0 or $new_height <= 0; | ||
| 131 | |||
| 132 | my $scaled_pixbuf = $gtk_pixbuf->scale_simple ( | ||
| 133 | $new_width, $new_height, "GDK_INTERP_BILINEAR" | ||
| 134 | ); | ||
| 135 | |||
| 136 | $gtk_image->set_from_pixbuf ( $scaled_pixbuf ); | ||
| 137 | |||
| 138 | } else { | ||
| 139 | $gtk_image->set_from_pixbuf ( $gtk_pixbuf ); | ||
| 140 | } | ||
| 141 | |||
| 142 | 1; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub empty_widget { | ||
| 146 | my $self = shift; | ||
| 147 | |||
| 148 | $self->get_gtk_widget->set_from_pixbuf(undef); | ||
| 149 | |||
| 150 | 1; | ||
| 151 | } | ||
| 152 | |||
| 153 | 1; | ||
| 154 | |||
| 155 | __END__ |