Originally Posted by
Sotos
Hello everyone!
...
First off, I want to try assigning a background image to a window. For that, I'd need the image location in utf-8 format (the "GLib file name encoding") to pass it through gdk_pixbuf_new_from_file(), unless I'm mistaken. I found some basics on how to convert to and from utf-8, but most were for C# or C++, and I'm looking for a clear answer, should you be able to provide one.
I'm not sure that you need the filename in utf8 encoding for gdk_pixbuf_new_from_file(), but even if you do, it is much easier to pass -fexec-charset=utf-8 to the command-line of gcc (perhaps coupled with -finput-charset=XXX where XXX is the encoding of your source files, in libiconv's "terminology").
Now for the real juice (setting a bg-image to a Gtk+2 window), here's a couple of links on how you may do it:
1. c - How does one set the background of a GtkWindow to an image? - Stack Overflow
Note: (an irrelevant note to your question) In the code presented at the above link, add g_signal_connect( window, "destroy", G_CALLBACK(gtk_main_quit), NULL ); somewhere between creating the window and calling the gtk main loop.
2. View topic - How to load a background image in a window.
Here's a bit more generalized form of the function shown at the above link (could be further improved):
Code:
#include <gtk/gtk.h>
/*********************************************//**
*
*************************************************
*/
GtkWidget *SetupWindow(
const gchar *title, /* title of the window */
const gchar *bgfname, /* filename of image to be used as background */
gboolean Transient, /* should the windows be transient? */
GtkWindow *parent /* parent window for transient */
)
{
GdkPixmap *background = NULL;
GdkPixbuf *pixbuf = NULL;
GtkStyle *style = NULL;
GError *error = NULL;
GtkWidget *window = NULL;
pixbuf = gdk_pixbuf_new_from_file( bgfname, &error );
if ( NULL != error ) {
if ( GDK_PIXBUF_ERROR == error->domain ) {
g_print( "Pixbuf Related Error:\n" );
}
if ( G_FILE_ERROR == error->domain ) {
g_print( "File Error: Check file permissions and state:\n" );
}
g_printerr ("%s\n", error[0].message );
return NULL;
}
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), title );
// 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 ) {
gtk_window_set_transient_for( GTK_WINDOW(window), parent );
}
// gtk_widget_show( window );
return(window);
}
/*********************************************//**
*
*************************************************
*/
int main( int argc, char *argv[] )
{
GtkWidget *w = NULL;
gtk_init( &argc, &argv );
w = SetupWindow( "Tile", "appicon1_128.png", TRUE, NULL );
g_signal_connect( w, "destroy", G_CALLBACK(gtk_main_quit), NULL );
gtk_widget_show_all( w );
gtk_main();
return 0;
}
Secondly, while I haven't tried it yet, I assume that the image won't be able to surpass the borders of the window. Could the image's edges be transparent? Will that work through pixbuf? Also, could I somehow bypass showing the border of the window (including the "minimize" and "close" buttons)?
You can use gtk_window_set_decorated() after you have created your window (and before displaying it). Note however that it depends on the windows-manager of your OS whether it will honor the request or not.
Lastly, if I want to rig up sounds to work with some events in GTK+, do you have a recommended library to work with?
...
I don't have 1st-hand experience with a stand-alone sound library, so I can't really recommend, but if you are looking for a more spherical frameworks (instead of GUI ones) you may want to have a look at SDL2, Allegro5 or SFML (the latter is mainly for C++ but among others it also offers c-bindings, called csfml I think).