- Prison Code Breaker Diary -

=> aka: Nhật Kí Code Tù

Categories

Let’s work out together with Gtk+ 2.0.

Well, you need to setup your environment with Gtk+, already support by Gnome if using Gnome/Linux.

Let’s do this: “Hello World


#include 

void
destroy( void )
{
gtk_main_quit( );
}

int
main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *button;

gtk_init( &argc, &argv );

window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_container_border_width( GTK_CONTAINER( window ), 10 );

g_signal_connect( GTK_OBJECT(window ), "destroy", G_CALLBACK( destroy ), NULL );

button = gtk_button_new_with_label( "Hello World" );

g_signal_connect( GTK_OBJECT( button ), "clicked", G_CALLBACK( hello ), NULL );
g_signal_connect_object( GTK_OBJECT( button ), "clicked", G_CALLBACK( gtk_widget_destroy ), GTK_OBJECT( window ) );

gtk_container_add( GTK_CONTAINER ( window ), button );

gtk_widget_show( button );
gtk_widget_show( window );

gtk_main ();

return 0;
}

There you have the source, well, named it “hello.c”

To make it work, let’s compile

japh@server ~] $ gcc -Wall -g -o hello hello.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)

Then just run ….

Have fun….


[Note: source code updated - 13 August 2009]

3 comments

  1. Unknown  
    This comment has been removed by the author.
  2. Unknown  

    Hello.

    Nice to see someone trying to help newcomers with sample collection.

    But on the other hand, bad examples are in my humble opinion even worse than nothing at all. Teaching something that's wrong is just, well, wrong.

    In your "Hello, World" application, you use gtk_signal_connect, which is deprecated in GTK+-2.0 series. You should teach people to use g_signal_connect instead.

    Don't feel discouraged by my comment. Keep doing what you do, but just make sure things that you post are correct and recent enough.

  3. Pete Houston  

    Thanks tadeboro!

    This post was written when I was just a beginner to Gtk+.

    If you see any other mistakes, warn me; I'll try to fix it up.

Post a Comment