Can't position Gnome2::Canvas within Gtk2::Frame

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Can't position Gnome2::Canvas within Gtk2::Frame

Les Trol
Hello everyone,
 
I'm yet another refugee from PerlMonks, who suggested I ask here.
 

I'm trying to draw a coloured bar inside a Gtk2::Frame.

The bar is drawn on a Gnome2::Canvas, contained within the frame. The bar should be as long as possible, but a fixed height. The bar should also be exactly in the middle of the frame.

My problem is that I can't find the size of the Gtk2::Frame that's actually drawn, therefore I can't set the size of the Gnome2::Canvas that would fill it, therefore I can't draw the coloured bar in the right place.

The example script draws a red bar whose width isn't long enough, and whose vertical position is slightly off of centre.

(The call to Gnome2::Canvas->get_size() just returns default values of 100x100, which is not related to the frame's actual size.)

---

#!/usr/bin/perl

use strict;
use diagnostics;
use warnings;
use Gtk2 '-init';
use Glib qw(TRUE FALSE);
use Gnome2::Canvas;
# Draw a Gtk2 window
my $window = Gtk2::Window->new();
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
$window->set_default_size(500, 500);
$window->set_border_width(5);
my $vBox = Gtk2::VBox->new(FALSE, 0);
$window->add($vBox);
# Draw a Gtk2::Frame containing a Gnome2::Canvas
# The frame should be as long as possible, but only 50 pixels high
my $frameHeight = 50;
     
my $frame = Gtk2::Frame->new(undef);
$frame->set_border_width(0);
$frame->set_size_request(-1, $frameHeight);
      
my $canvas = Gnome2::Canvas->new();
$canvas->set_center_scroll_region(FALSE);
$frame->add($canvas);
$vBox->pack_start($frame, FALSE, FALSE, 0);
# Draw a red bar, almost as long as the frame, but with a fixed height
my ($width, $height) = $canvas->get_size();
# Put the bar roughly in the middle; it's the best I can do at the moment
my $rect = Gnome2::Canvas::Item->new(
    $canvas->root(),
    'Gnome2::Canvas::Rect',       
     x1 => 20,                                    
     y1 => 20,                                         
     x2 => ($width - 40),
     y2 => ($frameHeight - 20),          
     fill_color => '#FF0000',
     outline_color => '#000000',
);
$rect->raise_to_top();  
$window->show_all();
Gtk2->main;
 

_______________________________________________
gtk-perl-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: Can't position Gnome2::Canvas within Gtk2::Frame

Gtk+ - Perl mailing list

Hi Les,

I am not familiar with the Gnome canvas but you could try this using an event box and drawing on that. That might be easier.

Eric 

#!/usr/bin/perl
use strict;
use diagnostics;
use warnings;
use Gtk2 '-init';
use Glib qw(TRUE FALSE);
# Draw a Gtk2 window
my $window = Gtk2::Window->new();
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
$window->set_default_size(500, 500);
$window->set_border_width(5);

my $eBox = Gtk2::EventBox->new();
$eBox->set_app_paintable(TRUE);
$eBox->signal_connect('expose_event' => \&draw_event_box);

my $Label1 = Gtk2::Label->new("Label1");
$Label1->set_markup("<span foreground='green' font='25'>Label1</span>");

my $Label2 = Gtk2::Label->new("Label2");
$Label2->set_markup("<span foreground='red' font='25'>Label2</span>");

my $vBox = Gtk2::VBox->new(FALSE, 0);
$vBox->pack_start_defaults($Label1);
$vBox->pack_start_defaults($Label2);

$eBox->add($vBox);
$window->add($eBox);

$window->show_all();
Gtk2->main;

sub draw_event_box
{
  my ($widget, $event, $user_data) = @_;
  my $cr = Gtk2::Gdk::Cairo::Context->create( $widget->window );
  my $rectangle = $widget->allocation;

  #Paint background.
  $cr->set_source_rgb(0, 1, 1);
  $cr->paint;

  #Draw yellow line in middle.
  $cr->set_source_rgb(1, 1, 0);
  $cr->set_line_width(10);
  $cr->move_to(0, ($rectangle->height) / 2);
  $cr->line_to($rectangle->width, ($rectangle->height) / 2);
  $cr->stroke();

  #Draw frame.
  $cr->set_source_rgb(0, 0, 1);
  $cr->set_line_width(15);
  $cr->rectangle(0, 0, $rectangle->width, $rectangle->height);
  $cr->stroke();
       
  return FALSE;
}
 




_______________________________________________
gtk-perl-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: Can't position Gnome2::Canvas within Gtk2::Frame

Les Trol
Thanks a bunch!
 
I merged your code into mine, and played with it a bit, and stumbled on a working solution using Gnome2::Canvas. (Your script using Gtk2::Gdk::Cairo::Context also works perfectly.)
 
---
 
#!/usr/bin/perl
use strict;
use diagnostics;
use warnings;
 
use Gtk2 '-init';
use Glib qw(TRUE FALSE);
use Gnome2::Canvas;
 
# Draw a Gtk2 window
my $window = Gtk2::Window->new();
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
$window->set_default_size(500, 500);
$window->set_border_width(5);
 
my $vBox = Gtk2::VBox->new(FALSE, 0);
$window->add($vBox);
 
# Draw a Gtk2::Frame containing a Gnome2::Canvas
# The frame should be as long as possible, but only 50 pixels high
my $frame = Gtk2::Frame->new(undef);
$frame->set_border_width(0);
$frame->set_size_request(-1, 50);
 
my $canvas = Gnome2::Canvas->new();
$frame->add($canvas);
$vBox->pack_start($frame, FALSE, FALSE, 0);
 
# The ->show_all() call here makes everything work (for some reason)
$window->show_all();
 
my $rect = $canvas->allocation();
$canvas->set_scroll_region(
        0,
        0,
        $rect->width,
        $rect->height,
);
 
# Draw a red bar, almost as long as the frame, but with a fixed height,
#   centred in the middle
my ($width, $height) = $canvas->get_size();
# (The Gtk2::Frame is 490x50, so the Gnome2::Canvas should be 488x48)
#print "canvas w $width h $height\n";
 
my $canvasItem = Gnome2::Canvas::Item->new(
    $canvas->root(),
    'Gnome2::Canvas::Rect',       
     x1 => 20,                                    
     y1 => 20,                                                  
     x2 => ($width - 20),
     y2 => ($height - 20),          
     fill_color => '#FF0000',
     outline_color => '#000000',
);
 
$canvasItem->raise_to_top();
 
$window->show_all();
Gtk2->main;

_______________________________________________
gtk-perl-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: Can't position Gnome2::Canvas within Gtk2::Frame

Gtk+ - Perl mailing list


Well, first time for me using Gnome canvas. Easy apt-get on Ubuntu and your code works.

When you call show all on the top level window it goes through the widgets that have been added to it and does a few things for each widget. You can connect to a callback if you need to do or get something at a certain time.

I usually work with GTK3 and cairo is the drawing library that is used there. If you really get into drawing there is a lot that you can do in GTK3. You can draw with cairo or opengl. Also transparency works well so that adds to what you can do. There is a frame clock for animation. There is also CSS that you can set widget colors and properties with.

Eric

#!/usr/bin/perl
use strict;
use diagnostics;
use warnings;
use Gtk2 '-init';
use Glib qw(TRUE FALSE);
# Draw a Gtk2 window
my $window = Gtk2::Window->new();
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
$window->set_default_size(200, 200);
$window->set_border_width(5);

my $button1 = Gtk2::Button->new_with_label("Button1");
$button1->signal_connect('size_allocate' => \&allocate_button);
$button1->signal_connect('realize' => \&realize_button);
$button1->signal_connect('map' => \&map_button);
$button1->signal_connect('clicked' => \&click_button);

$window->add($button1);

print "Show All\n";
$window->show_all();
Gtk2->main;

sub allocate_button
{
  print "Allocate\n";
}
sub realize_button
{
  print "Realize\n";
}
sub map_button
{
  print "Map\n";
}
sub click_button
{
  print "Click\n";
}



_______________________________________________
gtk-perl-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-perl-list