File: offscreen_scale.pl

package info (click to toggle)
libgtk2-perl 2%3A1.244-1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,524 kB
  • sloc: perl: 21,568; ansic: 122; makefile: 6
file content (130 lines) | stat: -rw-r--r-- 3,610 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/perl
use strict;
use warnings;
use Glib qw(TRUE FALSE);
use Gtk2 '-init';
use Cairo;

my $window = Gtk2::Window->new;
$window->set_title("Resize me");
$window->signal_connect( delete_event => sub { exit } );
$window->set_border_width(5);

my $scaled= Gtk2::Ex::ScaleBin->new;

my $entry=Gtk2::Entry->new;
my $button=Gtk2::Button->new_from_stock('gtk-ok');
$button->signal_connect(clicked=>sub {warn "clicked\n";});

my $vbox=Gtk2::VBox->new;
$vbox->add($entry);
$vbox->add($button);

$scaled->add($vbox);
$window->add($scaled);
$window->show_all;


Gtk2->main;



package Gtk2::Ex::ScaleBin;
use Gtk2;
use Glib::Object::Subclass
	Gtk2::EventBox::,
	signals =>
	{	size_allocate	=> \&do_size_allocate,
	};

sub INIT_INSTANCE {
	my $self=shift;
	$self->signal_connect( expose_event	=> \&expose_cb);
	$self->signal_connect( damage_event	=> \&damage_cb);
	$self->signal_connect( realize		=> \&realize_cb);
	return $self;
}

sub do_size_allocate {
	my ($self,$alloc)=@_;
	my $border= $self->get_border_width;
	my ($x,$y,$w,$h)=$alloc->values;
	my $olda=$self->allocation;
	 $olda->x($x); $olda->width($w);
	 $olda->y($y); $olda->height($h);
	$w-= 2*$border;
	$h-= 2*$border;
	$self->window->move_resize($x+$border,$y+$border,$w,$h) if $self->window;
	my ($reqw,$reqh)= ($w,$h);
	if (my $child=$self->child) {
		my $req= $child->size_request;
		$reqw= $req->width;
		$reqh= $req->height;
		my $rect=Gtk2::Gdk::Rectangle->new(0, 0, $reqw, $reqh);
		$self->child->size_allocate($rect);
	}
	$self->{zoom_x}= $w / $reqw;
	$self->{zoom_y}= $h / $reqh;
	if (my $offscreen= $self->{offscreen}) {
		$offscreen->{zoom_x}= $self->{zoom_x};
		$offscreen->{zoom_y}= $self->{zoom_y};
		$offscreen->move_resize(0,0, $reqw, $reqh);
		$offscreen->geometry_changed;
	}
}

sub damage_cb {
	my ($self,$event)=@_;
	my ($x,$y,$w,$h)=$event->area->values;
	my $zx= $self->{zoom_x};
	my $zy= $self->{zoom_y};
	my $rect=Gtk2::Gdk::Rectangle->new($x*$zx, $y*$zy, $w*$zx, $h*$zy);
	$self->window->invalidate_rect($rect,0);
	1;
}

sub realize_cb {
	my ($self)=@_;
	my ($x,$y,$w,$h)=$self->allocation->values;
	my %attr=
	 (	window_type	=> 'offscreen',
		wclass		=> 'output',
		x		=> 0,
		y		=> 0,
		width		=> $w,
		height		=> $h,
		event_mask	=> [qw/pointer-motion-mask button-press-mask button-release-mask exposure_mask/],
	 );
	$self->{offscreen}= my $offscreen= Gtk2::Gdk::Window->new($self->get_root_window,\%attr);
	$offscreen->set_user_data($self->Glib::Object::get_pointer());
	$self->window->signal_connect( pick_embedded_child =>sub { return $offscreen; });
	$self->child->set_parent_window($offscreen) if $self->child;
	$offscreen->set_embedder($self->window);
		$offscreen->{zoom_x}= $self->{zoom_x};
		$offscreen->{zoom_y}= $self->{zoom_y};
	$offscreen->signal_connect( to_embedder  => sub {my ($offscreen,$x,$y)=@_; return $x*$offscreen->{zoom_x},$y*$offscreen->{zoom_y} });
	$offscreen->signal_connect( from_embedder=> sub {my ($offscreen,$x,$y)=@_; return $x/$offscreen->{zoom_x},$y/$offscreen->{zoom_y} });
	$self->style->set_background($offscreen,'normal');
	$offscreen->show;
}

sub expose_cb {
	my ($self,$event)=@_;
	my $offscreen= $self->{offscreen};
	if ($event->window == $self->window) {
		my $pixmap = $offscreen->get_pixmap;
		return 1 unless $pixmap;
		my ($w,$h)= $pixmap->get_size;
		my $cr=Gtk2::Gdk::Cairo::Context->create($self->window);
		$cr->rectangle($event->area);
		$cr->clip;
		$cr->scale($self->{zoom_x},$self->{zoom_y});
		$cr->set_source_pixmap($pixmap,0,0);
		$cr->paint;
	}
	elsif ($event->window == $offscreen) {
		$self->propagate_expose($self->child,$event) if $self->child;
	}
	1;
}