We will practice on a new GtkWidget, a widget that show text descriptions, or GtkLabel.
First, you should look through documentation: GtkLabel
It has a lot of properties; well, you will not use them all, except you want to do something else, out of ordinary .
Head first to a small application, a window pops up containing a label show your name.
Note at my comments, that's how I constructed a new label, and simply use gtk_container_add() to add label to window.
/*
* @author [JaPh]
* @date July 16, 2009
* @file GtkLabelApp_01.c
* @site http://xjaphx.blogspot.com/
*/
#include <gtk/gtk.h>
int
main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *label;
gtk_init( &argc, &argv );
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_window_set_title( GTK_WINDOW(window), "[JaPh]Lesson 2: GtkLabel" );
gtk_widget_set_size_request( window, 400, 400 );
/* create a new label */
label = gtk_label_new( "Pete Houston" );
/* add label to window */
gtk_container_add( GTK_CONTAINER(window), label );
g_signal_connect( G_OBJECT(window), "destroy", G_CALLBACK( gtk_main_quit ), NULL );
gtk_widget_show_all( window );
gtk_main( );
return 0;
}
Now do something else harder.
Let's make a program that the window title shows your first name, the label shows your last name at beginning. When users press any key then your names are switched.
Let's do this!
We will the above code but add more things to it.
1. We have to use "key-press-event"=> reference event here
The function handler for event must be in type subjected in documetation.
2. While we switch the value between window title and label text, we need to temporarily store one value in a variable, which must be in type of a string.
For practice, we will use GString of Glib Library => GString reference
Try to make it yourself and compare to mine.
/*
* @author [JaPh]
* @date July 16, 2009
* @file GtkLabelApp.c
* @site http://xjaphx.blogspot.com/
*/
/* A - HEADER */
#include <gtk/gtk.h>
/* @ Declare functions */
gboolean
key_press_event_func( GObject *, GdkEventKey *, GtkLabel *);
/* B - MAIN */
int
main( int argc, char *argv[] )
{
/* 1. declare var */
GtkWidget *window;
GtkWidget *label;
/* 2. init gtk app */
gtk_init( &argc, &argv );
/* 3. widgets attributes */
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_window_set_title( GTK_WINDOW(window), "Pete");
gtk_widget_set_size_request( window, 400, 400 );
label = gtk_label_new("Houston");
/* add label to window */
gtk_container_add( GTK_CONTAINER( window ), label);
/* 4. connect signals */
g_signal_connect( G_OBJECT( window ), "destroy", G_CALLBACK( gtk_main_quit ), NULL );
g_signal_connect( G_OBJECT( window ), "key-press-event", G_CALLBACK( key_press_event_func ), GTK_LABEL( label ));
/* 5. show widgets */
gtk_widget_show_all( window );
/* 6. activate gtk app */
gtk_main();
/* 7. program return */
return 0;
}
/* C - Define functions */
gboolean
key_press_event_func( GObject *window, GdkEventKey *event, GtkLabel *label )
{
/* temp var */
GString *tmp;
/* store title */
tmp = g_string_new( gtk_window_get_title(GTK_WINDOW(window)) );
/* switch title */
gtk_window_set_title(GTK_WINDOW(window), gtk_label_get_text(label));
/* switch label */
gtk_label_set_text(label, tmp->str);
g_string_free( tmp );
}
Still eazy huh?
Do something else, then....When window shows up, the label show your full name, if key A is pressed, label shows your age; if key B is press, show your birthday; if key J is pressed show your job; if X is pressed or double mouse click then Exit program immediately.
You may need this paper on Events to support coding: Events
When you done, try mine
That's all I can tell about GtkLabel, try to practice more about it.
/*
* @author [JaPh]
* @date July 16, 2009
* @file GtkLabel_02.c
* @site http://xjaphx.blogspot.com/
*/
#include <gtk/gtk.h>
gboolean
button_press_handler( GObject *, GdkEventButton *, gpointer );
gboolean
key_press_handler( GObject *, GdkEventKey *, GtkLabel *);
int
main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *label;
gtk_init( &argc, &argv );
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_window_set_title( GTK_WINDOW(window), "[JaPh]Lesson 2: GtkLabel" );
gtk_widget_set_size_request( window, 400, 400 );
label = gtk_label_new( "Fullname: Pete Houston" );
gtk_container_add( GTK_CONTAINER(window), label );
gtk_widget_add_events( window, GDK_BUTTON_PRESS_MASK ); /* mask should be set for button press event */
g_signal_connect( G_OBJECT(window), "button-press-event", G_CALLBACK( button_press_handler ), NULL );
g_signal_connect( G_OBJECT(window), "key-press-event", G_CALLBACK( key_press_handler ), GTK_LABEL(label) );
g_signal_connect( G_OBJECT(window), "destroy", G_CALLBACK( gtk_main_quit ), NULL );
gtk_widget_show_all( window );
gtk_main( );
return 0;
}
gboolean
button_press_handler( GObject *window, GdkEventButton *event, gpointer p)
{
/* double click event */
if( event->type == GDK_2BUTTON_PRESS ) {
gtk_main_quit( );
}
}
gboolean
key_press_handler( GObject *window, GdkEventKey *event, GtkLabel *label )
{
switch( event->keyval ) {
case 'A':
case 'a':
gtk_label_set_text( label, "Age: 22" ); break;
case 'B':
case 'b':
gtk_label_set_text( label, "Birthday: 11-1-1982" ); break;
case 'J':
case 'j':
gtk_label_set_text( label, "Job: Chief Warrior" ); break;
case 'X':
case 'x':
gtk_main_quit( );
}
}
Have fun!@
[Note: source code - (example 2+3) updated - 13 August 2009 ]
August 12, 2009 at 11:52 PM
Hello.
As I said already before, if you decided to make tutorials, it's also your responsibility to make sure they are correct.
Things that should be fixed in this tutorial:
Example 2:
- key-press-event callback handler function's prototype is:
gboolean func( GtkWidget *, GdkEventKey *, gpointer );
- There is a leak in key-press-event handler, since you don't call g_string_free before exiting.
Example 3:
- You do not need to separately include gdk/gdk.h header, since gtk/gtk.h already does this.
- You do not need to separately include widget headers, since gtk/gtk.h already includes them (including single widgets is in fact deprecated).
- key-press-event and button-press-event prototypes are again wrong (both those functions return gboolean value).
- gtk_widget_set_events is quite disruptive function (it may break widgets functionality) and should be avoided in simple applications. Use gtk_widget_add_events instead.
Again, don't feel discouraged, just make sure your sample applications are written properly.
Tadej
August 13, 2009 at 12:28 AM
Thanks!
Source updated!