src/work/peter/gtk-helloworld.cc
changeset 1179 cfe0ed224c2e
equal deleted inserted replaced
0:db488fbd2e4a -1:000000000000
     1 #include <gtk/gtk.h>
       
     2 
       
     3 /* This is a callback function. The data arguments are ignored
       
     4  * in this example. More on callbacks below. */
       
     5 static void hello( GtkWidget *widget,
       
     6                    gpointer   data )
       
     7 {
       
     8     g_print ("Hello World\n");
       
     9 }
       
    10 
       
    11 static gboolean delete_event( GtkWidget *widget,
       
    12                               GdkEvent  *event,
       
    13                               gpointer   data )
       
    14 {
       
    15     /* If you return FALSE in the "delete_event" signal handler,
       
    16      * GTK will emit the "destroy" signal. Returning TRUE means
       
    17      * you don't want the window to be destroyed.
       
    18      * This is useful for popping up 'are you sure you want to quit?'
       
    19      * type dialogs. */
       
    20 
       
    21     g_print ("delete event occurred\n");
       
    22 
       
    23     /* Change TRUE to FALSE and the main window will be destroyed with
       
    24      * a "delete_event". */
       
    25 
       
    26     return TRUE;
       
    27 }
       
    28 
       
    29 /* Another callback */
       
    30 static void destroy( GtkWidget *widget,
       
    31                      gpointer   data )
       
    32 {
       
    33     gtk_main_quit ();
       
    34 }
       
    35 
       
    36 int main( int   argc,
       
    37           char *argv[] )
       
    38 {
       
    39     /* GtkWidget is the storage type for widgets */
       
    40     GtkWidget *window;
       
    41     GtkWidget *button;
       
    42     
       
    43     /* This is called in all GTK applications. Arguments are parsed
       
    44      * from the command line and are returned to the application. */
       
    45     gtk_init (&argc, &argv);
       
    46     
       
    47     /* create a new window */
       
    48     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
       
    49     
       
    50     /* When the window is given the "delete_event" signal (this is given
       
    51      * by the window manager, usually by the "close" option, or on the
       
    52      * titlebar), we ask it to call the delete_event () function
       
    53      * as defined above. The data passed to the callback
       
    54      * function is NULL and is ignored in the callback function. */
       
    55     g_signal_connect (G_OBJECT (window), "delete_event",
       
    56 		      G_CALLBACK (delete_event), NULL);
       
    57     
       
    58     /* Here we connect the "destroy" event to a signal handler.  
       
    59      * This event occurs when we call gtk_widget_destroy() on the window,
       
    60      * or if we return FALSE in the "delete_event" callback. */
       
    61     g_signal_connect (G_OBJECT (window), "destroy",
       
    62 		      G_CALLBACK (destroy), NULL);
       
    63     
       
    64     /* Sets the border width of the window. */
       
    65     gtk_container_set_border_width (GTK_CONTAINER (window), 10);
       
    66     
       
    67     /* Creates a new button with the label "Hello World". */
       
    68     button = gtk_button_new_with_label ("Hello World");
       
    69     
       
    70     /* When the button receives the "clicked" signal, it will call the
       
    71      * function hello() passing it NULL as its argument.  The hello()
       
    72      * function is defined above. */
       
    73     g_signal_connect (G_OBJECT (button), "clicked",
       
    74 		      G_CALLBACK (hello), NULL);
       
    75     
       
    76     /* This will cause the window to be destroyed by calling
       
    77      * gtk_widget_destroy(window) when "clicked".  Again, the destroy
       
    78      * signal could come from here, or the window manager. */
       
    79     g_signal_connect_swapped (G_OBJECT (button), "clicked",
       
    80 			      G_CALLBACK (gtk_widget_destroy),
       
    81                               G_OBJECT (window));
       
    82     
       
    83     /* This packs the button into the window (a gtk container). */
       
    84     gtk_container_add (GTK_CONTAINER (window), button);
       
    85     
       
    86     /* The final step is to display this newly created widget. */
       
    87     gtk_widget_show (button);
       
    88     
       
    89     /* and the window */
       
    90     gtk_widget_show (window);
       
    91     
       
    92     /* All GTK applications must have a gtk_main(). Control ends here
       
    93      * and waits for an event to occur (like a key press or
       
    94      * mouse event). */
       
    95     gtk_main ();
       
    96     
       
    97     return 0;
       
    98 }