hegyi@1174: #include hegyi@1174: hegyi@1174: /* This is a callback function. The data arguments are ignored hegyi@1174: * in this example. More on callbacks below. */ hegyi@1174: static void hello( GtkWidget *widget, hegyi@1174: gpointer data ) hegyi@1174: { hegyi@1174: g_print ("Hello World\n"); hegyi@1174: } hegyi@1174: hegyi@1174: static gboolean delete_event( GtkWidget *widget, hegyi@1174: GdkEvent *event, hegyi@1174: gpointer data ) hegyi@1174: { hegyi@1174: /* If you return FALSE in the "delete_event" signal handler, hegyi@1174: * GTK will emit the "destroy" signal. Returning TRUE means hegyi@1174: * you don't want the window to be destroyed. hegyi@1174: * This is useful for popping up 'are you sure you want to quit?' hegyi@1174: * type dialogs. */ hegyi@1174: hegyi@1174: g_print ("delete event occurred\n"); hegyi@1174: hegyi@1174: /* Change TRUE to FALSE and the main window will be destroyed with hegyi@1174: * a "delete_event". */ hegyi@1174: hegyi@1174: return TRUE; hegyi@1174: } hegyi@1174: hegyi@1174: /* Another callback */ hegyi@1174: static void destroy( GtkWidget *widget, hegyi@1174: gpointer data ) hegyi@1174: { hegyi@1174: gtk_main_quit (); hegyi@1174: } hegyi@1174: hegyi@1174: int main( int argc, hegyi@1174: char *argv[] ) hegyi@1174: { hegyi@1174: /* GtkWidget is the storage type for widgets */ hegyi@1174: GtkWidget *window; hegyi@1174: GtkWidget *button; hegyi@1174: hegyi@1174: /* This is called in all GTK applications. Arguments are parsed hegyi@1174: * from the command line and are returned to the application. */ hegyi@1174: gtk_init (&argc, &argv); hegyi@1174: hegyi@1174: /* create a new window */ hegyi@1174: window = gtk_window_new (GTK_WINDOW_TOPLEVEL); hegyi@1174: hegyi@1174: /* When the window is given the "delete_event" signal (this is given hegyi@1174: * by the window manager, usually by the "close" option, or on the hegyi@1174: * titlebar), we ask it to call the delete_event () function hegyi@1174: * as defined above. The data passed to the callback hegyi@1174: * function is NULL and is ignored in the callback function. */ hegyi@1174: g_signal_connect (G_OBJECT (window), "delete_event", hegyi@1174: G_CALLBACK (delete_event), NULL); hegyi@1174: hegyi@1174: /* Here we connect the "destroy" event to a signal handler. hegyi@1174: * This event occurs when we call gtk_widget_destroy() on the window, hegyi@1174: * or if we return FALSE in the "delete_event" callback. */ hegyi@1174: g_signal_connect (G_OBJECT (window), "destroy", hegyi@1174: G_CALLBACK (destroy), NULL); hegyi@1174: hegyi@1174: /* Sets the border width of the window. */ hegyi@1174: gtk_container_set_border_width (GTK_CONTAINER (window), 10); hegyi@1174: hegyi@1174: /* Creates a new button with the label "Hello World". */ hegyi@1174: button = gtk_button_new_with_label ("Hello World"); hegyi@1174: hegyi@1174: /* When the button receives the "clicked" signal, it will call the hegyi@1174: * function hello() passing it NULL as its argument. The hello() hegyi@1174: * function is defined above. */ hegyi@1174: g_signal_connect (G_OBJECT (button), "clicked", hegyi@1174: G_CALLBACK (hello), NULL); hegyi@1174: hegyi@1174: /* This will cause the window to be destroyed by calling hegyi@1174: * gtk_widget_destroy(window) when "clicked". Again, the destroy hegyi@1174: * signal could come from here, or the window manager. */ hegyi@1174: g_signal_connect_swapped (G_OBJECT (button), "clicked", hegyi@1174: G_CALLBACK (gtk_widget_destroy), hegyi@1174: G_OBJECT (window)); hegyi@1174: hegyi@1174: /* This packs the button into the window (a gtk container). */ hegyi@1174: gtk_container_add (GTK_CONTAINER (window), button); hegyi@1174: hegyi@1174: /* The final step is to display this newly created widget. */ hegyi@1174: gtk_widget_show (button); hegyi@1174: hegyi@1174: /* and the window */ hegyi@1174: gtk_widget_show (window); hegyi@1174: hegyi@1174: /* All GTK applications must have a gtk_main(). Control ends here hegyi@1174: * and waits for an event to occur (like a key press or hegyi@1174: * mouse event). */ hegyi@1174: gtk_main (); hegyi@1174: hegyi@1174: return 0; hegyi@1174: }