GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Nov 23, 2014 1:01 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: How to load a background image in a window.
PostPosted: Wed Mar 21, 2007 1:22 pm 
Offline
Familiar Face

Joined: Wed Mar 21, 2007 1:02 pm
Posts: 16
I am programming in C using the latest stable GTK.

I want to have a background image in my window. I tried adding the image but then it goes
above the other widgets, I want it behind everything. I spent ages trawling the web trying to figure this one out.

Thanks.
TBlue


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 2:23 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Your main GtkWindow widget has a GdkWindow, which can be accessed with GTK_WIDGET(window)->window. In fact, every widget has an associated GdkWindow. In any case, you should check out the gdk_window_set_back_pixmap(), found at http://developer.gnome.org/doc/API/2.0/ ... ack-pixmap

Also, you should check out gdk_window_shape_combine_mask(). Use this function to set the GdkWindows of all of the other widgets in the window wath an alpha mask (transparency).

I've never done this, but I'm pretty sure this is how you would do it. I would recommend you check out Google Code Search or Krugle for examples of how to use these functions.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 4:23 pm 
Offline
Familiar Face

Joined: Wed Mar 21, 2007 1:02 pm
Posts: 16
Thanks for the reply,

I tried using this code, to load and then display the background.

GdkPixmap *BackPixMap;
BackPixMap=gdk_pixmap_create_from_xpm (mainwindow,NULL,NULL,"pics/fb.xpm");
gdk_window_set_back_pixmap(GTK_WIDGET(mainwindow)->window,BackPixMap,FALSE);

This generates an runtime error..

Gdk-CRITICAL **: gdk_pixmap_colormap_create_from_xpm: assertion `drawable == NULL || GDK_IS_DRAWABLE (drawable)' failed

Can anyone point me to an example that loads say a jpg or bmp into the background of a window ?

Thanks.
Tblue


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 5:44 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
mainwindow is your GtkWindow widget, right? Well, GTK_WIDGET (mainwindow)->window is your drawable because GdkWindow is a GdkDrawable. (Yes, the whole GtkWindow/GdkWindow thing is confusing, but it is the necessary terminology).

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject: Background image...
PostPosted: Mon Apr 09, 2007 12:11 pm 
Offline
Familiar Face

Joined: Wed Mar 21, 2007 1:02 pm
Posts: 16
For completeness heres a copy of the routine I used to create windows with a background image...

You probably need to customise it a little, Im working on an embedded device that supports only 640x480.

GtkWidget *SetupWindow(gchar *data,gboolean Transient)
{
GdkPixmap *background;
GdkPixbuf *pixbuf;
GdkScreen *ourscreen;
GdkColormap *colormap;
GtkStyle *style;
GdkColor fg;
GdkColor bg;
GError *error = NULL;
GdkRectangle *rect;
GtkWidget *window;

pixbuf = gdk_pixbuf_new_from_file ("pics/fb.png",&error);
if (error != NULL) {
if (error->domain == GDK_PIXBUF_ERROR) {
g_print ("Pixbuf Related Error:\n");
}
if (error->domain == G_FILE_ERROR) {
g_print ("File Error: Check file permissions and state:\n");
}

g_printerr ("%s\n", error[0].message);
exit(1);
}
gdk_pixbuf_render_pixmap_and_mask (pixbuf, &background, NULL, 0);
style = gtk_style_new ();
style->bg_pixmap[0] = background;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), data);
// gtk_window_maximize(GTK_WINDOW(window));
gtk_window_set_modal (GTK_WINDOW (window),TRUE);
gtk_window_set_default_size(GTK_WINDOW(window),640,480);
gtk_widget_set_style (GTK_WIDGET(window), GTK_STYLE(style));
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER_ALWAYS);
gtk_container_set_border_width(GTK_CONTAINER(window), 14);
if(Transient==TRUE)
gtk_window_set_transient_for(GTK_WINDOW (window),GTK_WINDOW(mainwindow));
gtk_widget_show (window);
return(window);
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group