in reply to Perl-Gtk2::How to set a window background image.

Hi, I got it to work, yeah!! I narrowed down my segfault to some weird problem where you get the $graphicContext. Your method didn't work for me, and below is what I had to do. I'll post a better example, after I look at it awhile. It is important to match the size of the image to the window, etc. But here is the sub that works for me.
sub set_bg{ my $pixbuff = Gtk2::Gdk::Pixbuf->new_from_file("1Zen16.png"); #set the new pixbuff in a vbox my $gdkwindow = $vbox1->window; # how to get the drawable, $widget->get_parent_window # did not work for me # although the drawable seems to have # the identical hash # ???? my ($drawable, $x_offset, $y_offset) = $gdkwindow->get_internal_paint_info; print "$drawable, $x_offset, $y_offset\n"; my $gc = Gtk2::Gdk::GC->new ($gdkwindow, undef); print "$gc\n"; $pixbuff->render_to_drawable($drawable, $gc, 0,0,0,0,500,430,'normal',0,0); #this was needed, to make the image actually change Gtk2->main_iteration while Gtk2->events_pending; $window->show_all(); return FALSE; }

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re^2: Perl-Gtk2::How to set a window background image.
by turo (Friar) on Feb 15, 2006 at 09:09 UTC

    zentara i need time to digest this ... I suposse, i must read a gtk2 tutorial in C, and then read seriusly the Gtk2-perl docs (maybe this weekend)...

    For what i wanted to do, i finally use xpm images, preload them onto a pixmap, and then use the Gtk2::RcStyle for change my style everytime i want to...
    But, this invitation to master the Gtk2, calls me like the Dark Power ... (Gtk2 for perl is the simplest (forgetting our problems) API for programming user graphics interfaces ... but now ... now Gtk2 will be as powerfull for us like programming over X11 with the Xlibs ... )

    Forgetting my delirium; I've changed your "set_bg" version. It works fine, but i've simplified it:

    sub set_bg{ my $pixbuff = Gtk2::Gdk::Pixbuf->new_from_file("1Zen16.png"); #set the new pixbuff in a vbox my $gdkwindow = $vbox1->window; my $gc = Gtk2::Gdk::GC->new ($gdkwindow, undef); $pixbuff->render_to_drawable($gdkwindow, $gc, 0,0,0,0,500,430,'normal',0,0); return FALSE; }
    When you paint over a drawable, you only need to Sync the window, but this is a job for the Gtk2::main, no?

    I'm anxious for the weekend to come!

    turo

    perl -Te 'print map { chr((ord)-((10,20,2,7)[$i++])) } split //,"turo"'
      i need time to digest this

      I agree. There are some complications which set in, when you reset the background of a window that is already mapped to the screen. I noticed that if the dimensions of the render_to_drawable is not exactly the same as pixbuf dimensions, x-y errors would occur. These can be avoided by some logic to get the pixbuf x and y size, etc. But that brings up the problem of what happens if you resize the window? Will the image resize? How would you tile the image? etc.

      I was thinking about it last night, and the gtk c-libs developers probably didn't want to bloat the code with all the logic it would take to enable swappable background images. So we see it can be done, but the provided methods of using the styles and xpm files, is easier, and will probably work well for most applications.


      I'm not really a human, but I play one on earth. flash japh
        Hi! I think this is my first contribution hehe... I Got this code from the php cookbook, and converted it to Perl. It worked for me :)
        my $back_pixbuf = Gtk2::Gdk::Pixbuf->new_from_file("apple.png"); my ($pixmap,$mask) = $back_pixbuf->render_pixmap_and_mask(255); my $style = $window->get_style(); $style=$style->copy(); $style->bg_pixmap("normal",$pixmap); $window->set_style($style);
        NOTE: apple.png NOT INCLUDED.