[Perl + Gtk2] Image disappears after scrolling / minimizing window


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Perl + Gtk2] Image disappears after scrolling / minimizing window
# 1  
Old 02-21-2012
[Perl + Gtk2] Image disappears after scrolling / minimizing window

Hello everyone

I don't know if it's the right place to ask, but I will try. I want to use Gtk2 in my Perl chart-drawing script, I found a sample code that could be very useful for me somewhere, but I noticed there is one problem with that - when I draw something, and then scroll the window or minimize it, the image disappears. Is it possible to make this image "permament"? I'm totally new to Gtk so maybe my question is stupid.

The full code of this script is visible below (after clicking "Draw" a single line appears, but after you scroll/minimize it - it's gone.

Code:
#!/usr/bin/perl
use warnings;
use strict;
use Glib qw/TRUE FALSE/;
use Gtk2 -init;

# the basic drawing area. It's failing is that
# it is emphemeral... it disappears if scrolled
# out of view, or is obscured then re-exposed,
# as in a minimize - restore window manager event.

# It is simple, and useful for simple non-scrolled 
# drawings.

# gtk2 pixmaps (on linux ?) have a current limit 
# of short unsigned INT , highest pixels is 
# 32767 is (8bit int max) -1     

# xsize exagerated for demonstration puposes
my $xsize = 9000; # maxsize = 32767
my $ysize = 100;
my $area;
 
my %allocated_colors;
my ($x0,$y0,$x1,$y1,$width,) = (0,0,0,0);

# Create the window
my $window = new Gtk2::Window ( "toplevel" );
$window->signal_connect ("delete_event", sub { Gtk2->main_quit; });
$window->set_border_width (10);
$window->set_size_request(640,480);
$window->set_position('center');

my $vbox = Gtk2::VBox->new( 0, 0 );
$window->add($vbox);
$vbox->set_border_width(2);



my $hbox = Gtk2::HBox->new( 0, 0 );
$vbox->pack_start($hbox,1,1,0);
$hbox->set_size_request(320,240);
$hbox->set_border_width(2);

my $hbox1 = Gtk2::HBox->new( 0, 0 );
$vbox->pack_start($hbox1,0,0,0);
$hbox1->set_border_width(2);

my $button1 = Gtk2::Button->new('Draw');
$hbox1->pack_start( $button1, FALSE, FALSE, 2);
$button1->signal_connect( clicked => sub{ start_drawing($area) }  );

my $button2 = Gtk2::Button->new('Quit');
$hbox1->pack_start( $button2, FALSE, FALSE, 2);
$button2->signal_connect( clicked => sub{ exit; });

my $button3 = Gtk2::Button->new('Save');
$hbox1->pack_start( $button3, FALSE, FALSE, 2);
$button3->signal_connect( clicked => \&save_it);


my $scwin = Gtk2::ScrolledWindow->new();
my $ha1  = $scwin->get_hadjustment;
$scwin->set_policy('always','never');

# you would think we could add the DrawingArea directing
# to the scrolled window, so we need a viewport
# typical warning
# Gtk-WARNING **: gtk_scrolled_window_add(): cannot add non 
# scrollable widget use gtk_scrolled_window_add_with_viewport() 
# we create a viewport, so we can identify it by name $vp

my $vp = Gtk2::Viewport->new (undef,undef);
$scwin->add($vp);
$hbox->pack_start($scwin,1,1,0);

# Create the drawing area.
$area = new Gtk2::DrawingArea;
$area->size ($xsize, $ysize);
$vp->add($area);

$area->set_events ([qw/exposure-mask
                    leave-notify-mask
               button-press-mask
               pointer-motion-mask
               pointer-motion-hint-mask/]);

$area->signal_connect (button_press_event => \&button_press_event);

$window->show_all;

Gtk2->main;
###########################################
sub get_color {
    my ($colormap, $name) = @_;
    my $ret;

    if ($ret = $allocated_colors{$name}) {
        return $ret;
    }

    my $color = Gtk2::Gdk::Color->parse($name);
    $colormap->alloc_color($color,TRUE,TRUE);

    $allocated_colors{$name} = $color;

    return $color;
}
##########################################

sub draw_line {
    my($widget,$line,$color) = @_;
   # see Gdk::Gdk::Window, Gtk2::Gdk::Drawable, Gtk2::Gdk::GC

    my $colormap = $widget->window->get_colormap;

    my $gc = $widget->{gc} || new Gtk2::Gdk::GC $widget->window;
    $gc->set_foreground(get_color($colormap, $color));

    $widget->window->draw_line($gc, @$line);
}

##########################################
# Draw a line in the expose callback
sub start_drawing {
        my $area = shift;
    
        &draw_line($area, [200,30, 9000,100], 'blue');
}
###########################################

sub button_press_event {
  my $widget = shift;    # GtkWidget      *widget
  my $event = shift;    # GdkEventButton *event

  if ($event->button == 1) {
        print join ' ', $event->coords,"\n";
  }
  return TRUE;
}
########################################
sub save_it{

my ($width, $height) = $vp->window->get_size();

print "$width $height\n";

# create blank pixbuf to hold the whole viewable area
 my $lpixbuf = Gtk2::Gdk::Pixbuf->new ('rgb',
                    0,
                    8,
                    $width,
                    $height);


 $lpixbuf->get_from_drawable ($vp->window, 
             undef, 0, 0, 0, 0, $width, $height);

 #only jpeg and png is supported !!!! it's 'jpeg', not 'jpg'
 $lpixbuf->save ("$0-area.jpg", 'jpeg', quality => 100);

return FALSE;
}

########################################


I would be very grateful for any help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sub-folder in share disappears when mounting to other server

Hello, I have 3 solaris 11.2 servers: 1 and 2: are just fileservers with 1 zfs-share server 3: i want to use this one to connect to the rest of our network (windows machines and a few solaris machines) I created the shares on all servers like this (x needs to be replace with the number... (9 Replies)
Discussion started by: Wim_123
9 Replies

2. Shell Programming and Scripting

Gtk2-Perl

Hai how to activate (script2)page of a note book in Gtk2-Perl when one script1 is run successfully... Thanks kiran (1 Reply)
Discussion started by: kiran425
1 Replies

3. Shell Programming and Scripting

Gtk2-Perl

Hai how to activate (script2)page of a note book in Gtk2-Perl when one script1 is run successfully... Thanks kiran (0 Replies)
Discussion started by: kiran425
0 Replies

4. Shell Programming and Scripting

Perl::Gtk2 on Linux --- How to Find the Default Font Being Used?

Hello All, Wasn't sure if this was the correct thread to post this under but figured it has to do with Perl and Gtk2 so why not... Anyway.. How can I find out what the Default font being used is inside a Gtk2::Widget. In this case I'm trying to figure out the font being used inside a... (1 Reply)
Discussion started by: mrm5102
1 Replies

5. UNIX for Advanced & Expert Users

loopback filesystem disappears after reboot

I am running solarix x86 on a dell r810; I have mirrored the two internal 300Gb disks and accepted the default directory structure during the installation. Oracle 11g R2 was then installed with a view to using this machine in a DR scenario. The following steps were performed to create two disks... (3 Replies)
Discussion started by: jabberwocky
3 Replies

6. Shell Programming and Scripting

perl Image::size function

Hi, How to use Image::size function ? because I want to list all image thatare less than size pixels in horizontal resolution. so here is my code : #!/usr/bin/perl -w use Image::Size; use constant GIVEN_WIDTH = '100'; my @filtered_images = grep { my @d = imgsize( $_ ); $d < GIVEN_WIDTH }... (8 Replies)
Discussion started by: guidely
8 Replies

7. Solaris

socket in listen state disappears/closes automatically

Hi, I am using solaris 10. I have opened a socket connection using java in solaris 10 operating system, the port went to LISTEN state and able to create new socket connection and the new connections went to ESTABLISHED state. If I issue the command "netstat -an | grep <<portnumber>>", I... (10 Replies)
Discussion started by: kumar3k
10 Replies

8. Programming

perl/Gtk2: issue with initializing Gtk2

hi everybody, currently i'm playing with perl and Gtk2. i've found a fairly old but nice looking example of a client/server application which is written in perl and Gtk2. the server part works perfect but i can't start the client part and keep getting following error message: $ ./client-gui.pl... (1 Reply)
Discussion started by: pseudocoder
1 Replies

9. UNIX Desktop Questions & Answers

gtk2-perl load error help

I write this code. It show the first dialog correctly. but, no matter i modify the code(load module by use or require, or something i think maybe error) it can't pop-up anymore. and show this warning. Thanks. (I'm sorry for my English.) Tue Mar 16 17:24:36 2010 ... (0 Replies)
Discussion started by: snyh
0 Replies

10. Shell Programming and Scripting

perl window.open without blocker

Hi Everyone, We know that when we open a popup window, if my IE, Yahoo tool bar enable the popup-blocker, then my window will be blocked. like my code. print <<EOD; <script language=JavaScript> var s = new String(window.location.href); if (s.match(/Start/)){ ... (3 Replies)
Discussion started by: jimmy_y
3 Replies
Login or Register to Ask a Question