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