Archive

Archive for the ‘gtk’ Category

Splash Screen with GTK+

October 12, 2009 5 comments

The code:


#include <gtk/gtk.h>
static void destroy (GtkWidget*, gpointer);
static gboolean delete_event (GtkWidget*, GdkEvent*, gpointer);
int main (int argc,
          char *argv[])
{
  GtkWidget *window, *image;
  gtk_init (&argc, &argv);
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "A Splash Screen Demo");
  gtk_container_set_border_width (GTK_CONTAINER (window), 0);
  gtk_widget_set_size_request (window, 800, 480);
  gtk_window_set_decorated(GTK_WINDOW (window), FALSE);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_resizable(GTK_WINDOW(window), FALSE);

  /* Connect the main window to the destroy and delete-event signals. */
  g_signal_connect (G_OBJECT (window), "destroy",
                    G_CALLBACK (destroy), NULL);
  g_signal_connect (G_OBJECT (window), "delete_event",
                    G_CALLBACK (delete_event), NULL);

  image=gtk_image_new_from_file("wait.png");
  gtk_container_add(GTK_CONTAINER(window), image);

  gtk_widget_show_all (window);
  gtk_main ();
  return 0;
}

/* Stop the GTK+ main loop function when the window is destroyed. */
static void
destroy (GtkWidget *window,
          gpointer data)
{
   gtk_main_quit ();
}
/* Return FALSE to destroy the widget. By returning TRUE, you can cancel
  * a delete-event. This can be used to confirm quitting the application. */
static gboolean delete_event (GtkWidget *window,
               GdkEvent *event,
               gpointer data)
{
    return FALSE;
}

And here’s the included “Please wait” image file in .png format:

The png file used by the splash screen demo program

The png file used by the splash screen demo program

To compile:


gcc -Wall -g demo.c -o splash `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`

Now test it:


./splash
# use Alt + F4 to close and exit.
Categories: gtk, Programming