Re: Size of a GtkImage
| From: | cyril dot cottet at toyota-tiee dot com | Date: | Fri, 10 Feb 2006 14:31:26 +0000 |
| Subject: | Re: Size of a GtkImage | ||
| References: | 1 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-6708@lists.php.net to get a copy of this message | ||
Here is a script I did recently for testing. There is only one problem
with it which is that scrollbar don't take in account when image is
resized .... I cannot figure out why.
<?php
$imgPath = "/path/to/img.jpg";
$imgPanel = new ZoomableImagePanel($imgPath);
$window = new GtkWindow();
$window->set_border_width(5);
$window->set_title('Zoomable Image example');
$window->set_default_size(800, 400);
$window->set_resizable(true);
$window->connect_object('destroy', array('gtk', 'main_quit'));
$window->add($imgPanel);
$window->show_all();
gtk::main();
class ZoomableImagePanel extends GtkVBox
{
protected $vbox = null;
protected $toolbar = null;
protected $zoomInButton = null;
protected $zoomOutButton = null;
protected $imageDrawable = null;
protected $imageScrollWindow = null;
protected $originalPixbuf = null;
protected $currentZoomRatio = null;
protected $previousRefreshRatio = null;
protected function get_toolbar()
{
if ( $this->toolbar == null ) {
$this->toolbar = new GtkToolbar();
}
$this->toolbar->set_orientation(Gtk::ORIENTATION_HORIZONTAL);
$this->toolbar->set_tooltips(True);
$this->toolbar->set_show_arrow(True);
$this->toolbar->insert($this->get_zoomInButton(),-1);
$this->toolbar->insert($this->get_zoomOutButton(),-1);
return $this->toolbar;
}
protected function get_zoomInButton()
{
if ( $this->zoomInButton == null ) {
$this->zoomInButton = new GtkToolButton();
}
$this->zoomInButton->set_stock_id("gtk-zoom-in");
$this->zoomInButton->set_visible_horizontal(True);
$this->zoomInButton->set_visible_vertical(True);
$this->zoomInButton->set_is_important(False);
$this->zoomInButton->connect('clicked',array($this,
'zoom_image_in'));
return $this->zoomInButton;
}
protected function get_zoomOutButton()
{
if ( $this->zoomOutButton == null ) {
$this->zoomOutButton = new GtkToolButton();
}
$this->zoomOutButton->set_stock_id("gtk-zoom-out");
$this->zoomOutButton->set_visible_horizontal(True);
$this->zoomOutButton->set_visible_vertical(True);
$this->zoomOutButton->set_is_important(False);
$this->zoomOutButton->connect('clicked',array($this,
'zoom_image_out'));
return $this->zoomOutButton;
}
protected function get_image_scroll_window()
{
if ( $this->imageScrollWindow == null ) {
$this->imageScrollWindow = new GtkScrolledWindow(null,null);
}
$this->imageScrollWindow->set_policy(Gtk::POLICY_ALWAYS,Gtk::POLICY_ALWAYS);
$this->imageScrollWindow->add_with_viewport($this->get_image());
return $this->imageScrollWindow;
}
protected function get_image()
{
if ( $this->imageDrawable == null ) {
$this->imageDrawable = new
GtkDrawingArea();
$this->imageDrawable->connect('expose-event',array($this,
'refreshImage'));
//$this->imageDrawable->set_size_request(1000,1000);
}
return $this->imageDrawable;
}
public function refreshImage() {
$ratio = $this->get_current_zoom_ratio();
if ($ratio != $this->previousRefreshRatio) {
$buf = $this->originalPixbuf;
$curWi = $buf->get_width();
$curHe = $buf->get_height();
$newWi = round($curWi*$ratio);
$newHe = round($curHe*$ratio);
$newWi = ($newWi > 0)?$newWi:1;
$newHe = ($newHe > 0)?$newHe:1;
$buf = $buf->scale_simple($newWi,$newHe,
Gdk::INTERP_NEAREST);
$this->imageDrawable->window->clear();
$this->imageDrawable->window->draw_pixbuf(new
GdkGC($this->imageDrawable->window),$buf,0,0,0,0);
$this->previousRefreshRatio = $ratio;
}
}
protected function zoom_image_by_ratio($ratio) {
$this->set_current_zoom_ratio($ratio);
$this->refreshImage();
}
protected function zoom_image_by_constraint($wid,$hei,$biggest=false)
{
$buf = $this->originalPixbuf;
$curWi = $buf->get_width();
$curHe = $buf->get_height();
$ratWi = $curWi/$wid;
$ratHe = $curHe/$hei;
$refRat = (($ratWi>$ratHe)&&($biggest))?$ratWi:$ratHe;
$this->zoom_image_by_ratio($refRat);
}
public function zoom_image_in() {
$newRatio = $this->get_current_zoom_ratio()*1.2;
$this->zoom_image_by_ratio($newRatio);
}
public function zoom_image_out() {
$newRatio = $this->get_current_zoom_ratio()/1.2;
$this->zoom_image_by_ratio($newRatio);
}
protected function set_current_zoom_ratio($ratio) {
$this->currentZoomRatio = $ratio;
}
protected function get_current_zoom_ratio() {
return $this->currentZoomRatio;
}
function __construct($imgPath)
{
parent::__construct(False,0);
$this->set_homogeneous(False);
$this->set_spacing(0);
try {
$pixbuf =
GdkPixbuf::new_from_file($imgPath);
$this->originalPixbuf = $pixbuf;
$this->set_current_zoom_ratio(1);
} catch (PhpGtkGErrorException $e) {
die("Exception : ".$e->message .
"\n");
}
$this->pack_start($this->get_toolbar(),false,false);
$this->pack_start($this->get_image_scroll_window());
}
}
?>
Breizh Micro Services <francois@bms-guer.com>
10/02/2006 15:16
To
PHP-Gtk General List <php-gtk-general@lists.php.net>
cc
Subject
[PHP-GTK] Size of a GtkImage
Maybe it is an idiot question, but I do not know how to make a zoom on a
GtkImage?
I wish to display an image and with two buttons, zoom in ou out the
image...
Is it possible ?
Thanks.
Breizh.
--
PHP-GTK General Mailing List (http://gtk.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php