diff --git a/c/gtk-tutorial/helloworld/helloworld_with_box.c b/c/gtk-tutorial/helloworld/helloworld_with_box.c index d05e776..71f6389 100644 --- a/c/gtk-tutorial/helloworld/helloworld_with_box.c +++ b/c/gtk-tutorial/helloworld/helloworld_with_box.c @@ -1,36 +1,20 @@ #include -/* This is a callback function. The data arguments are ignored - * in this example. More on callbacks below. */ -void hello( GtkWidget *widget, - gpointer data ) +/* Our new improved callback. The data passed to this function + * is printed to stdout. */ +void callback( GtkWidget *widget, + gpointer data ) { - g_print ("Hello World\n"); + g_print ("Hello again - %s was pressed\n", (gchar *) data); } +/* another callback */ gint delete_event( GtkWidget *widget, GdkEvent *event, - gpointer data ) -{ - /* If you return FALSE in the "delete_event" signal handler, - * GTK will emit the "destroy" signal. Returning TRUE means - * you don't want the window to be destroyed. - * This is useful for popping up 'are you sure you want to quit?' - * type dialogs. */ - - g_print ("delete event occurred\n"); - - /* Change TRUE to FALSE and the main window will be destroyed with - * a "delete_event". */ - - return TRUE; -} - -/* Another callback */ -void destroy( GtkWidget *widget, - gpointer data ) + gpointer data ) { gtk_main_quit (); + return FALSE; } int main( int argc, @@ -39,60 +23,71 @@ int main( int argc, /* GtkWidget is the storage type for widgets */ GtkWidget *window; GtkWidget *button; - + GtkWidget *box1; + /* This is called in all GTK applications. Arguments are parsed * from the command line and are returned to the application. */ gtk_init (&argc, &argv); - - /* create a new window */ + + /* Create a new window */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL); - - /* When the window is given the "delete_event" signal (this is given - * by the window manager, usually by the "close" option, or on the - * titlebar), we ask it to call the delete_event () function - * as defined above. The data passed to the callback - * function is NULL and is ignored in the callback function. */ + + /* This is a new call, which just sets the title of our + * 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. */ g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (delete_event), NULL); - - /* Here we connect the "destroy" event to a signal handler. - * This event occurs when we call gtk_widget_destroy() on the window, - * or if we return FALSE in the "delete_event" callback. */ - g_signal_connect (G_OBJECT (window), "destroy", - G_CALLBACK (destroy), NULL); - + /* Sets the border width of the window. */ gtk_container_set_border_width (GTK_CONTAINER (window), 10); + + /* We create a box to pack widgets into. This is described in detail + * in the "packing" section. The box is not really visible, it + * is just used as a tool to arrange widgets. */ + box1 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0); + + /* Put the box into the main window. */ + gtk_container_add (GTK_CONTAINER (window), box1); + + /* Creates a new button with the label "Button 1". */ + button = gtk_button_new_with_label ("Button 1"); - /* Creates a new button with the label "Hello World". */ - button = gtk_button_new_with_label ("Hello World"); - - /* When the button receives the "clicked" signal, it will call the - * function hello() passing it NULL as its argument. The hello() - * function is defined above. */ + /* Now when the button is clicked, we call the "callback" function + * with a pointer to "button 1" as its argument */ g_signal_connect (G_OBJECT (button), "clicked", - G_CALLBACK (hello), NULL); - - /* 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. */ - g_signal_connect_swapped (G_OBJECT (button), "clicked", - G_CALLBACK (gtk_widget_destroy), - G_OBJECT (window)); - - /* This packs the button into the window (a gtk container). */ - gtk_container_add (GTK_CONTAINER (window), button); - - /* The final step is to display this newly created widget. */ + G_CALLBACK (callback), (gpointer) "button 1"); + + /* Instead of gtk_container_add, we pack this button into the invisible + * box, which has been packed into the window. */ + gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0); + + /* Always remember this step, this tells GTK that our preparation for + * this button is complete, and it can now be displayed. */ gtk_widget_show (button); - - /* and the window */ + + /* Do these same steps again to create a second button */ + button = gtk_button_new_with_label ("Button 2"); + + /* Call the same callback function with a different argument, + * passing a pointer to "button 2" instead. */ + g_signal_connect (G_OBJECT (button), "clicked", + G_CALLBACK (callback), (gpointer) "button 2"); + + gtk_box_pack_start(GTK_BOX (box1), button, TRUE, TRUE, 0); + + /* 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. */ + gtk_widget_show (button); + + gtk_widget_show (box1); + gtk_widget_show (window); - /* 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). */ + /* Rest in gtk_main and wait for the fun to begin! */ gtk_main (); - + return 0; } diff --git a/c/gtk-tutorial/helloworld/run.sh b/c/gtk-tutorial/helloworld/run_helloworld.sh similarity index 100% rename from c/gtk-tutorial/helloworld/run.sh rename to c/gtk-tutorial/helloworld/run_helloworld.sh diff --git a/c/gtk-tutorial/helloworld/run_helloworld_with_box.sh b/c/gtk-tutorial/helloworld/run_helloworld_with_box.sh new file mode 100755 index 0000000..05d01bc --- /dev/null +++ b/c/gtk-tutorial/helloworld/run_helloworld_with_box.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +gcc -Wall -g helloworld_with_box.c -o helloworld_with_box `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0` && ./helloworld_with_box