1
votes

I am relatively new to gtk programming in C. I'm using Mac OS 10.9 with XQuartz, with gtk3 (3.14.9_0+x11 installed through macports). I'm trying to get the parent window to display an icon in the title bar. I found the following example from http://zetcode.com/tutorials/gtktutorial/firstprograms/

#include <gtk/gtk.h>

GdkPixbuf *create_pixbuf(const gchar * filename)
{
   GdkPixbuf *pixbuf;
   GError *error = NULL;
   pixbuf = gdk_pixbuf_new_from_file(filename, &error);
   if(!pixbuf) {
      fprintf(stderr, "%s\n", error->message);
      g_error_free(error);
   }

   return pixbuf;
}

int main( int argc, char *argv[])
{
  GtkWidget *window;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "icon");
  gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_icon(GTK_WINDOW(window), create_pixbuf("web.png"));
  gtk_widget_show(window);

  g_signal_connect_swapped(G_OBJECT(window), "destroy",
      G_CALLBACK(gtk_main_quit), NULL);

  gtk_main();

  return 0;
}

The program compiles with no errors or warnings, and the window displays when I run it with no errors reported, except that there is no icon other than the usual X11 icon in the title bar. I made sure that I had the web.png file in my working directory, and I've run out of ideas about what might be wrong. Are there any special requirements for the PNG file that is being converted to GdkPixbuf? And if so, why do I not receive a message in stderr? Thanks very much for any help you can provide.

1

1 Answers

0
votes

Because of the environment you're working in, you may be having issues with the encoding of the filename. Pixbuf is expecting a GLib encoded filename. You can find more info about it here.

Try using the g_filename_from_utf8() function and pass the resulting GLib encoded string to gdk_pixbuf_new_from_file().