Re: Scaling an image with Gtk2::Gdk::Pixbuf->scale



Ok, second intractable problem:
 
2. The spec also wants to detect mouse clicks on the displayed image. With this I've barely made any progress at all.
 
I tried some obvious things. However, Gtk2::TextIter->get_pixbuf() only detects mouse motion of the left half of the image (for no obvious reason).  
 
The Gtk docs say: "The image will be counted as one character in character counts...". Alas, Gtk2::TextIter->get_line_offset() seems to count it as two characters - one for the left half of the image, one for the right half.
 
The script below demonstrates those two quirks. I also tried inserting Gtk2::TextMarks before and after the image, and for some reason that only applies to the right-hand half of the image.
 
I'm lost now. Any ideas?
 
---
 
#!/usr/bin/perl
package scaleme2;
 
use strict;
use diagnostics;
use warnings;
 
use Gtk2 '-init';
use Glib qw(TRUE FALSE);
 
# Display this image
my $path = 'change_this_directory/cat.bmp';
my $count = 0;
 
# Open a Gtk2 window with a Gtk2::TextView
my $window = Gtk2::Window->new('toplevel');
$window->set_title('scaleme');
$window->set_position('center');
$window->set_default_size(800, 600);
$window->signal_connect('delete-event' => sub {
 
    Gtk2->main_quit();
    exit;
});
            
my $frame = Gtk2::Frame->new();
$window->add($frame);
 
my $scrollWin = Gtk2::ScrolledWindow->new(undef, undef);
$frame->add($scrollWin);
$scrollWin->set_policy('automatic', 'automatic');     
$scrollWin->set_border_width(0);
 
my $textView = Gtk2::TextView->new;
$scrollWin->add_with_viewport($textView);
 
if (-e $path) {
 
    # Display an image
    my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file($path);
 
    my $buffer = $textView->get_buffer();
    $buffer->insert_pixbuf(
        $buffer->get_end_iter(),
        $pixbuf,
    );
}
 
# Detect mouse motion over the image
$textView->signal_connect(motion_notify_event => sub {
 
    my ($widget, $event) = @_;
 
    # Get mouse position over the textview buffer
    my ($xPos, $yPos) = $textView->window_to_buffer_coords(
        'widget',
        $event->x,
        $event->y,
    );
 
    my $iter = $textView->get_iter_at_location($xPos, $yPos);
 
    print "mouse position " . $iter->get_line_offset . " y " . $iter->get_line . "\n";
    if ($iter->get_pixbuf()) {
 
        print "mouse is hovering over the image! " . $count++ . "\n";
    }
});
 
$window->show_all();   
Gtk2->main();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]