Re: Size (Width) of a widget



On Tue, 19 Aug 2008 15:11:13 +0200
Matthias SchÃfer <m schaefer koios de> wrote:

Hi,
is there a easy way to get the actual size of a widget? In my case, I  
need the size (more precise the width) of a EventBox.

This will do it.  The thing you need to test is the expose or configure event 
in the event callback, so you will get new values when the window is resized.
I'm sure this can be made more efficient by returning from the event callback
unless it's a configure or expose.

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

my $window = Gtk2::Window->new('toplevel');
$window->set_title('Z');
$window ->signal_connect( 'destroy' => \&delete_event );
$window->set_border_width(10);
#$window->set_size_request(300,200);

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

my $hbox= Gtk2::HBox->new( FALSE, 6 );
$vbox->pack_end($hbox,FALSE,FALSE,0);
$hbox->set_border_width(2);

my $frame0 = Gtk2::Frame->new('Controls');
$vbox->pack_end( $frame0, FALSE, FALSE, 0 );
$frame0->set_border_width(3);

$vbox->pack_end (Gtk2::HSeparator->new, FALSE, FALSE, 0);

my $hbox0 = Gtk2::HBox->new( FALSE, 6 );
$frame0->add($hbox0);
$hbox0->set_border_width(3);

my $button = Gtk2::Button->new_from_stock('gtk-quit');
$hbox0->pack_end( $button, FALSE, FALSE, 0 );
$button->signal_connect( clicked => \&delete_event );

my $frame1 = Gtk2::Frame->new('Some Frame');
$vbox->pack_start( $frame1, FALSE, FALSE, 0 );
$frame1->set_border_width(3);

#labels don't respond to tooltips, since they have no
#underlying window, so we must put the label in an event box.
my $label =  Gtk2::Label->new( "This label is just for you\n" );
$label->set_justify('left');

my $eventb1 = Gtk2::EventBox->new();
$eventb1->set_border_width(3);
$eventb1->add($label);

$frame1->add($eventb1);

my $tooltip0 = Gtk2::Tooltips->new;
my $tip_text = 'dims';
$tooltip0->set_tip($eventb1, $tip_text, undef);

my $tooltip1 = Gtk2::Tooltips->new;
my $tip_text1 = 'Just Another Exit Button';
$tooltip1->set_tip($button, $tip_text1, undef);

$window->signal_connect(event_after => \&event_after);

$window->show_all();
Gtk2->main;
#####################################
sub delete_event {
Gtk2->main_quit;
return FALSE;
}  

sub event_after {
  my ($mw, $event) = @_;
  print "\t",$event->type,"\n";
  
  # get new  size after a window resize
  my $rect = $eventb1->allocation;
  my $cw = $rect->width;
  my $ch = $rect->height;
  print "$cw $ch\n"; 
  $tooltip0->set_tip($eventb1, "$cw $ch", undef);
  
  return FALSE;
}
__END__

zentara

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/Remember_How_Lucky_You_Are.html 



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