GDK+ Hello World

Post Reply
User avatar
Administrator
Site Admin
Posts: 1623
Joined: Thu Mar 22, 2018 10:19 am

GDK+ Hello World

Post by Administrator » Thu Dec 06, 2018 9:50 pm

A modified version of the application shown at https://www.codeguru.com/cpp/cpp/creati ... h-gtk.html

hello.c

Code: Select all

    #include <gtk/gtk.h>
     
    static int counter = 0;
     
    void greet( GtkWidget *widget, gpointer   data )
    {
       // printf equivalent in GTK+
       g_print ("Hello World! Welcome to GTK\n");
       g_print ("%s clicked %d times\n",
          (char*)data, ++counter);
    }
     
    void destroy( GtkWidget *widget,gpointer   data )
    {
       gtk_main_quit ();
    }
     
    int main( int   argc,char *argv[] )
    {
     
       GtkWidget *window;
       GtkWidget *button;
       gtk_init (&argc, &argv);
     
       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
     
       g_signal_connect (window, "destroy",
       G_CALLBACK (destroy), NULL);
       /* Let's set the border width of the window to 20.
        * You may play with the value and see the
        * difference. */
       gtk_container_set_border_width
          (GTK_CONTAINER (window), 20);
     
       button = gtk_button_new_with_label ("Hello World!");
     
       g_signal_connect (GTK_OBJECT(button),
          "clicked",G_CALLBACK (greet),
          "button");
     
       gtk_container_add (GTK_CONTAINER (window), button);
     
       gtk_widget_show_all(window); 
     
       gtk_main ();
     
       return 0;
    }
Take a look at the terminal after running this application.

Binary Compressed
hello.zip
(3.34 KiB) Downloaded 113 times
Decompress and run with

Code: Select all

./hello

Post Reply