Home About Meetings Directions Mailing Lists Jobs

Chris Lansdown gtk talk 13 June 2000 example2.c

From LILUG

Note: The following attempts to use much of the original formatting that Chris Lansdown used in his handout while still working in the wikitext format!

/* GPL note would go here */
/* We're going to need both the gtk headers 
   and the header file that defines printf() */
#include <gtk/gtk.h>
#include <stdio.h>
/* This is our first callback. 
   Not very exciting but it does make use of the data 
   element that gtk allows us to pass to ourselves.  
   Notice that we have to cast 
   from the gpointer type to the char * type for printing. */
void say_hello_cb(GtkWidget *widget, gpointer data) {
   printf("Hello %s!\n", (char *)data);
   /* This type of callback doesn't have 
   a return value so we use a blank return statement*/
   return; 
}
/* Where execution starts */
int main(int argc, char **argv)
{
  /* We need a window, a box, and five buttons.  We will, however, reuse 
    the button variable */
  GtkWidget *window, *box, *button;
  /* Initialize Gtk */
  gtk_init(&argc, &argv);
  
  /* Create and set up our main window */
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_signal_connect(GTK_OBJECT(window), "delete_event", gtk_main_quit, NULL);
  gtk_window_set_title(GTK_WINDOW(window), "A fancy hello world");
  
  /* Create the box */
  box = gtk_hbox_new(FALSE, 5);
  
  /* Attach it to the window */
  gtk_container_add(GTK_CONTAINER(window), box);
  /* Create and pack the label.  Since the label doesn't do anything but 
  sit there in our window, we don't need to keep track of it. Consequently 
  we just pass it to the packing function directly instead of storing it 
  in a variable. */
  
  gtk_box_pack_start(GTK_BOX(box), 
  gtk_label_new("Say hello to the: "), FALSE, 0, 0);
  
  /* Now we will create our buttons.  For each one, we attach a callback to 
  the clicked signal (this is the name of the signal generated when a user 
  clicks the button). We attach a string as the data so that we don't need 
  to differentiate between the buttons.  Finally we pack them left to right. */
  button = gtk_button_new_with_label("World");
  
  gtk_signal_connect(GTK_OBJECT(button), "clicked", say_hello_cb, "World");
  
  gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);


  button = gtk_button_new_with_label("Solar System");
  
  gtk_signal_connect(GTK_OBJECT(button), "clicked", say_hello_cb, 
    "Solar System");
  
  gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);


  button = gtk_button_new_with_label("Galaxy");
  
  gtk_signal_connect(GTK_OBJECT(button), "clicked", say_hello_cb, "Galaxy");
  
  gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);


  button = gtk_button_new_with_label("Universe");
  
  gtk_signal_connect(GTK_OBJECT(button), "clicked", say_hello_cb, "Universe");
  
  gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);


  button = gtk_button_new_with_label("Everything");
  
  gtk_signal_connect(GTK_OBJECT(button), "clicked", say_hello_cb, "Everything");
  
  gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
  
  /* Show the window and everything in it. */
  gtk_widget_show_all(window);
  
  /* give control to gtk */
  gtk_main();
  
  /* When Gtk exits, return success */
  return 0;
}