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