Home > gtk, Programming > Splash Screen with GTK+

Splash Screen with GTK+

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.
About these ads
Categories: gtk, Programming
  1. chris
    July 7, 2012 at 2:45 am

    Thanks, this was an older post, but it helped me create my for Splash image on RHEL 6 with gtk2. I have MUCH more to learn.

  2. Andrew
    August 22, 2012 at 11:48 am

    Yep, use this recently as well :) great to have such a great language to work in and such an excellent example :) Thanks!

  3. kadora
    September 15, 2012 at 11:28 am

    how can I make the screen close by itself after a specific amount of time

    • September 17, 2012 at 9:26 pm

      You can call some sleep function in C before the program terminates. Or if you invoke the program through external scripting language (Bash, for example), you can write a simple script to achieve that:

      #!/usr/bin/env bash
      ./splash &
      last_pid=$!
      sleep 5
      kill $last_pid
      
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: