- Prison Code Breaker Diary -

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

Categories

Let's start our Gtk+ programming section after everything has been setup perfectly.

To begin with Gtk+ programming, let's head first to the most fundamental or basic control of graphical user interface (GUI), Window.
In Gtk+ framework, we call Widget instead of Control. Well, I will use Widget from now on.

First, we need to look up where documetation about window in Gtk+, or GtkWindow.

Refer this link: GtkWindow Reference

And then, we begin with a small GtkWindow program and dissect.


/*
* @author [JaPh]
* @date July 14, 2009
* @file GtkWindowApp.c
* @site http://xjaphx.blogspot.com/
*/

/* A - HEADERS */
#include <gtk/gtk.h>

/* B - MAIN */
int
main( int argc, char *argv[] )
{
/* 1. declare */
/* declare a widget, which will be a main window */
GtkWidget *window;

/* 2. Init */
/* initiate Gtk+ application */
gtk_init( &argc, &argv );

/* 3. properties settings */
/* set Window Type, TOP LEVEL */
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
/* set window title */
gtk_window_set_title( GTK_WINDOW(window), "[JaPh]Lesson 1: GtkWindow" );
/* set window size, apply for all kinds of widgets */
gtk_widget_set_size_request( window, 400, 400 );

/* 4. signal & event connection */
/* signal handler on destroy application */
g_signal_connect( window, "destroy", G_CALLBACK( gtk_main_quit ), NULL );

/* 5. display & activate */
/* show window interface */
gtk_widget_show_all( window );

/* activate main application */
gtk_main( );

/* 6. return */
/* program return */
return 0;
}

Go ahead and try to compile it.

~ ] $ gcc -Wall -g -o GtkWindowApp GtkWindowApp.c $(pkg-config --cflags --libs gtk+-2.0)

If you're using any IDE, then make sure you configure them correctly.

The code is self-explained already. However, I want to notice something about structure of the code.

The above code is also an example of basic structure of an Gtk+ application.
The sample shows 2 sections: A & B. Under section B, there are 6 steps following main code structure. And that's all you need to do.
Usually, there'll be section C, which will show up below to define functions used in program.

The code above is just a simple window show up with title, nothing else.
Now, let's do something else.
Learn a new function: g_print ( click here to view g_print reference )

There you go, now we will print out the opacity level of our window created above.
Add this line before step 4:


g_print("Window Opacity: %1.2f", gtk_window_get_opacity( GTK_WINDOW(window) ));

After compile and run, you'll see the result show in console window about the window opacity level.
It's fun, isn't it?

Now, it's your turn to change something inside the code and see the effects.
Also, look up the documentation to use more functions.

Have fun!@

0 comments

Post a Comment