1998-03-23 06:36:09 +00:00
|
|
|
|
2004-03-06 03:38:59 +00:00
|
|
|
#include <config.h>
|
1998-03-23 06:36:09 +00:00
|
|
|
#include <gtk/gtk.h>
|
1998-12-07 15:19:00 +00:00
|
|
|
|
|
|
|
/* Our new improved callback. The data passed to this function
|
|
|
|
* is printed to stdout. */
|
|
|
|
void callback( GtkWidget *widget,
|
|
|
|
gpointer data )
|
|
|
|
{
|
2002-02-19 19:47:16 +00:00
|
|
|
g_print ("Hello again - %s was pressed\n", (gchar *) data);
|
1998-03-23 06:36:09 +00:00
|
|
|
}
|
1998-12-07 15:19:00 +00:00
|
|
|
|
1998-03-23 06:36:09 +00:00
|
|
|
/* another callback */
|
1999-11-13 23:06:46 +00:00
|
|
|
gint delete_event( GtkWidget *widget,
|
1998-12-07 15:19:00 +00:00
|
|
|
GdkEvent *event,
|
1999-11-13 23:06:46 +00:00
|
|
|
gpointer data )
|
1998-12-07 15:19:00 +00:00
|
|
|
{
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_main_quit ();
|
|
|
|
return FALSE;
|
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;
|
|
|
|
GtkWidget *box1;
|
|
|
|
|
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. */
|
1998-03-23 06:36:09 +00:00
|
|
|
gtk_init (&argc, &argv);
|
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Create a new window */
|
1998-03-23 06:36:09 +00:00
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
|
1999-11-13 23:06:46 +00:00
|
|
|
/* This is a new call, which just sets the title of our
|
1998-03-23 06:36:09 +00:00
|
|
|
* new window to "Hello Buttons!" */
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Hello Buttons!");
|
|
|
|
|
|
|
|
/* Here we just set a handler for delete_event that immediately
|
|
|
|
* exits GTK. */
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_connect (G_OBJECT (window), "delete_event",
|
|
|
|
G_CALLBACK (delete_event), NULL);
|
1998-03-23 06:36:09 +00:00
|
|
|
|
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-03-23 06:36:09 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* We create a box to pack widgets into. This is described in detail
|
|
|
|
* in the "packing" section. The box is not really visible, it
|
1998-03-23 06:36:09 +00:00
|
|
|
* is just used as a tool to arrange widgets. */
|
2002-02-19 01:25:26 +00:00
|
|
|
box1 = gtk_hbox_new (FALSE, 0);
|
1998-03-23 06:36:09 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Put the box into the main window. */
|
1998-03-23 06:36:09 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), box1);
|
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Creates a new button with the label "Button 1". */
|
1998-03-23 06:36:09 +00:00
|
|
|
button = gtk_button_new_with_label ("Button 1");
|
2002-02-16 23:52:30 +00:00
|
|
|
|
1998-03-23 06:36:09 +00:00
|
|
|
/* Now when the button is clicked, we call the "callback" function
|
1998-12-07 15:19:00 +00:00
|
|
|
* with a pointer to "button 1" as its argument */
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_connect (G_OBJECT (button), "clicked",
|
2002-08-24 11:53:26 +00:00
|
|
|
G_CALLBACK (callback), (gpointer) "button 1");
|
1998-12-07 15:19:00 +00:00
|
|
|
|
|
|
|
/* Instead of gtk_container_add, we pack this button into the invisible
|
1998-03-23 06:36:09 +00:00
|
|
|
* box, which has been packed into the window. */
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
|
1998-12-07 15:19:00 +00:00
|
|
|
|
|
|
|
/* Always remember this step, this tells GTK that our preparation for
|
|
|
|
* this button is complete, and it can now be displayed. */
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_widget_show (button);
|
1998-12-07 15:19:00 +00:00
|
|
|
|
|
|
|
/* Do these same steps again to create a second button */
|
1998-03-23 06:36:09 +00:00
|
|
|
button = gtk_button_new_with_label ("Button 2");
|
1998-12-07 15:19:00 +00:00
|
|
|
|
|
|
|
/* Call the same callback function with a different argument,
|
1998-03-23 06:36:09 +00:00
|
|
|
* passing a pointer to "button 2" instead. */
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_connect (G_OBJECT (button), "clicked",
|
2002-08-24 11:53:26 +00:00
|
|
|
G_CALLBACK (callback), (gpointer) "button 2");
|
1998-12-07 15:19:00 +00:00
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_box_pack_start(GTK_BOX (box1), button, TRUE, TRUE, 0);
|
1998-12-07 15:19:00 +00:00
|
|
|
|
1998-03-23 06:36:09 +00:00
|
|
|
/* The order in which we show the buttons is not really important, but I
|
|
|
|
* recommend showing the window last, so it all pops up at once. */
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_widget_show (button);
|
1998-03-23 06:36:09 +00:00
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_widget_show (box1);
|
1998-03-23 06:36:09 +00:00
|
|
|
|
|
|
|
gtk_widget_show (window);
|
2002-02-16 23:52:30 +00:00
|
|
|
|
1998-12-07 15:19:00 +00:00
|
|
|
/* Rest in gtk_main and wait for the fun to begin! */
|
1998-03-23 06:36:09 +00:00
|
|
|
gtk_main ();
|
|
|
|
|
2002-02-16 23:52:30 +00:00
|
|
|
return 0;
|
1998-12-07 15:19:00 +00:00
|
|
|
}
|