| 1 | # Copyright 2010, 2011, 2012 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Image-Base-Gtk2. | ||
| 4 | # | ||
| 5 | # Image-Base-Gtk2 is free software; you can redistribute it and/or modify it | ||
| 6 | # under the terms of the GNU General Public License as published by the Free | ||
| 7 | # Software Foundation; either version 3, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # Image-Base-Gtk2 is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 13 | # for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Image-Base-Gtk2. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | |||
| 19 | package Image::Base::Gtk2::Gdk::Pixmap; | ||
| 20 | use 5.008; | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | use Carp; | ||
| 24 | use base 'Image::Base::Gtk2::Gdk::Drawable'; | ||
| 25 | |||
| 26 | # uncomment this to run the ### lines | ||
| 27 | #use Smart::Comments; | ||
| 28 | |||
| 29 | our $VERSION = 11; | ||
| 30 | |||
| 31 | sub new { | ||
| 32 | my ($class, %params) = @_; | ||
| 33 | ### Gdk-Pixmap new: \%params | ||
| 34 | |||
| 35 | # $obj->new(...) means make a copy, with some extra settings | ||
| 36 | if (ref $class) { | ||
| 37 | my $self = $class; | ||
| 38 | # $class = ref $class; | ||
| 39 | if (! defined $params{'-pixmap'}) { | ||
| 40 | # $self->get('-gc') creates and retains a gc. Doing that seems better | ||
| 41 | # than letting _drawable_clone_to_pixmap() create and destroy one, | ||
| 42 | # though if there was one already in Gtk2::GC with the right depth | ||
| 43 | # then could just use that. | ||
| 44 | $params{'-pixmap'} = _drawable_clone_to_pixmap ($self->get('-pixmap'), | ||
| 45 | $self->get('-gc')); | ||
| 46 | } | ||
| 47 | # inherit everything else, but don't share gc | ||
| 48 | %params = (%$self, | ||
| 49 | -gc => undef, | ||
| 50 | %params); | ||
| 51 | } | ||
| 52 | |||
| 53 | if (exists $params{'-pixmap'}) { | ||
| 54 | $params{'-drawable'} = delete $params{'-pixmap'}; | ||
| 55 | } | ||
| 56 | |||
| 57 | if (! exists $params{'-drawable'}) { | ||
| 58 | ### create new pixmap | ||
| 59 | |||
| 60 | my $screen = delete $params{'-screen'}; | ||
| 61 | my $for_drawable = delete $params{'-for_drawable'}; | ||
| 62 | my $for_widget = delete $params{'-for_widget'}; | ||
| 63 | my $depth = delete $params{'-depth'}; | ||
| 64 | ### for_widget: "$for_widget" | ||
| 65 | |||
| 66 | $screen ||= ($for_widget && $for_widget->get_screen); | ||
| 67 | |||
| 68 | $for_drawable | ||
| 69 | ||= ($for_widget && $for_widget->window) | ||
| 70 | || ($screen && $screen->get_default_root_window) | ||
| 71 | || Gtk2::Gdk->get_default_root_window; | ||
| 72 | |||
| 73 | if (! exists $params{'-colormap'}) { | ||
| 74 | if (my $default_colormap | ||
| 75 | = ($for_drawable && $for_drawable->get_colormap) | ||
| 76 | || ($for_widget && $for_widget->get_colormap) | ||
| 77 | || ($screen && $screen->get_default_colormap)) { | ||
| 78 | if (! defined $depth | ||
| 79 | || $depth == $default_colormap->get_visual->depth) { | ||
| 80 | $params{'-colormap'} = $default_colormap; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | } | ||
| 84 | if (! defined $params{'-colormap'}) { | ||
| 85 | delete $params{'-colormap'}; | ||
| 86 | } | ||
| 87 | |||
| 88 | if (! defined $depth) { | ||
| 89 | if ($params{'-colormap'}) { | ||
| 90 | $depth = $params{'-colormap'}->get_visual->depth; | ||
| 91 | } elsif ($for_drawable) { | ||
| 92 | $depth = $for_drawable->get_depth; | ||
| 93 | } else { | ||
| 94 | $depth = -1; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | ### $depth | ||
| 98 | |||
| 99 | $params{'-drawable'} = Gtk2::Gdk::Pixmap->new ($for_drawable, | ||
| 100 | delete $params{'-width'}, | ||
| 101 | delete $params{'-height'}, | ||
| 102 | $depth); | ||
| 103 | # -colormap is applied in Drawable new() doing set() | ||
| 104 | } | ||
| 105 | |||
| 106 | return $class->SUPER::new (%params); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub new_from_image { | ||
| 110 | my $self = shift; | ||
| 111 | my $new_class = shift; | ||
| 112 | if ($new_class eq __PACKAGE__ | ||
| 113 | || $new_class eq 'Image::Base::Gtk2::Gdk::Drawable') { | ||
| 114 | return bless $self->new(@_), $new_class; | ||
| 115 | } | ||
| 116 | return $self->SUPER::new_from_image ($new_class, @_); | ||
| 117 | } | ||
| 118 | |||
| 119 | # $pixmap is a Gtk2::Gdk::Pixmap | ||
| 120 | # create and return a clone of it | ||
| 121 | # $gc is used to copy the contents, or a temporary gc used if $gc not given | ||
| 122 | # | ||
| 123 | sub _drawable_clone_to_pixmap { | ||
| 124 | my ($drawable, $gc) = @_; | ||
| 125 | my ($width, $height) = $drawable->get_size; | ||
| 126 | my $new_pixmap = Gtk2::Gdk::Pixmap->new ($drawable, $drawable->get_size, -1); | ||
| 127 | |||
| 128 | # Perl-Gtk 1.220 set_colormap() doesn't accept undef, so must check | ||
| 129 | if (my $colormap = $drawable->get_colormap) { | ||
| 130 | $new_pixmap->set_colormap ($colormap); | ||
| 131 | } | ||
| 132 | |||
| 133 | # gtk_gc_get() only uses colormap to determine the screen | ||
| 134 | # is there any value trying for a shared one? | ||
| 135 | # it'd share with someone else using an empty values hash presumably | ||
| 136 | # for similar copying | ||
| 137 | $gc ||= Gtk2::GC->get ($drawable->get_depth, | ||
| 138 | $drawable->get_screen->get_default_colormap); | ||
| 139 | # $gc ||= Gtk2::Gdk::GC->new ($drawable); | ||
| 140 | |||
| 141 | $new_pixmap->draw_drawable ($gc, $drawable, 0,0, 0,0, $width,$height); | ||
| 142 | return $new_pixmap; | ||
| 143 | } | ||
| 144 | |||
| 145 | 1; | ||
| 146 | __END__ |