src/work/peter/gtk-helloworld.cc
changeset 1174 5dccf1916ed8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/peter/gtk-helloworld.cc	Thu Feb 24 14:44:17 2005 +0000
     1.3 @@ -0,0 +1,98 @@
     1.4 +#include <gtk/gtk.h>
     1.5 +
     1.6 +/* This is a callback function. The data arguments are ignored
     1.7 + * in this example. More on callbacks below. */
     1.8 +static void hello( GtkWidget *widget,
     1.9 +                   gpointer   data )
    1.10 +{
    1.11 +    g_print ("Hello World\n");
    1.12 +}
    1.13 +
    1.14 +static gboolean delete_event( GtkWidget *widget,
    1.15 +                              GdkEvent  *event,
    1.16 +                              gpointer   data )
    1.17 +{
    1.18 +    /* If you return FALSE in the "delete_event" signal handler,
    1.19 +     * GTK will emit the "destroy" signal. Returning TRUE means
    1.20 +     * you don't want the window to be destroyed.
    1.21 +     * This is useful for popping up 'are you sure you want to quit?'
    1.22 +     * type dialogs. */
    1.23 +
    1.24 +    g_print ("delete event occurred\n");
    1.25 +
    1.26 +    /* Change TRUE to FALSE and the main window will be destroyed with
    1.27 +     * a "delete_event". */
    1.28 +
    1.29 +    return TRUE;
    1.30 +}
    1.31 +
    1.32 +/* Another callback */
    1.33 +static void destroy( GtkWidget *widget,
    1.34 +                     gpointer   data )
    1.35 +{
    1.36 +    gtk_main_quit ();
    1.37 +}
    1.38 +
    1.39 +int main( int   argc,
    1.40 +          char *argv[] )
    1.41 +{
    1.42 +    /* GtkWidget is the storage type for widgets */
    1.43 +    GtkWidget *window;
    1.44 +    GtkWidget *button;
    1.45 +    
    1.46 +    /* This is called in all GTK applications. Arguments are parsed
    1.47 +     * from the command line and are returned to the application. */
    1.48 +    gtk_init (&argc, &argv);
    1.49 +    
    1.50 +    /* create a new window */
    1.51 +    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    1.52 +    
    1.53 +    /* When the window is given the "delete_event" signal (this is given
    1.54 +     * by the window manager, usually by the "close" option, or on the
    1.55 +     * titlebar), we ask it to call the delete_event () function
    1.56 +     * as defined above. The data passed to the callback
    1.57 +     * function is NULL and is ignored in the callback function. */
    1.58 +    g_signal_connect (G_OBJECT (window), "delete_event",
    1.59 +		      G_CALLBACK (delete_event), NULL);
    1.60 +    
    1.61 +    /* Here we connect the "destroy" event to a signal handler.  
    1.62 +     * This event occurs when we call gtk_widget_destroy() on the window,
    1.63 +     * or if we return FALSE in the "delete_event" callback. */
    1.64 +    g_signal_connect (G_OBJECT (window), "destroy",
    1.65 +		      G_CALLBACK (destroy), NULL);
    1.66 +    
    1.67 +    /* Sets the border width of the window. */
    1.68 +    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    1.69 +    
    1.70 +    /* Creates a new button with the label "Hello World". */
    1.71 +    button = gtk_button_new_with_label ("Hello World");
    1.72 +    
    1.73 +    /* When the button receives the "clicked" signal, it will call the
    1.74 +     * function hello() passing it NULL as its argument.  The hello()
    1.75 +     * function is defined above. */
    1.76 +    g_signal_connect (G_OBJECT (button), "clicked",
    1.77 +		      G_CALLBACK (hello), NULL);
    1.78 +    
    1.79 +    /* This will cause the window to be destroyed by calling
    1.80 +     * gtk_widget_destroy(window) when "clicked".  Again, the destroy
    1.81 +     * signal could come from here, or the window manager. */
    1.82 +    g_signal_connect_swapped (G_OBJECT (button), "clicked",
    1.83 +			      G_CALLBACK (gtk_widget_destroy),
    1.84 +                              G_OBJECT (window));
    1.85 +    
    1.86 +    /* This packs the button into the window (a gtk container). */
    1.87 +    gtk_container_add (GTK_CONTAINER (window), button);
    1.88 +    
    1.89 +    /* The final step is to display this newly created widget. */
    1.90 +    gtk_widget_show (button);
    1.91 +    
    1.92 +    /* and the window */
    1.93 +    gtk_widget_show (window);
    1.94 +    
    1.95 +    /* All GTK applications must have a gtk_main(). Control ends here
    1.96 +     * and waits for an event to occur (like a key press or
    1.97 +     * mouse event). */
    1.98 +    gtk_main ();
    1.99 +    
   1.100 +    return 0;
   1.101 +}