GTK, desarrollo de interfaces graficas (II)
25 07 2006
Para poder ahondar en la programación de interfaces gráficas con GTK es necesario tener un buen conocimiento de todos los objetos que podemos crear, sus características y todos las señales/eventos ante las cuales pueden reaccionar. Esta lista es muy extensa y únicamente te remito a la referencia de GTK2 para que puedas consultarlo segun tus necesidades.
Sin embargo hay muchas mas características de las librerías que necesitamos conocer para realizar aplicaciones mas completas. Hare un pequeño resumen algunas de estas funciones.
boolean gtk_init_check(&argc, &argv)
Te permite verificar antes de ejecutar gtk_init que se va poder inizializar sin problemas GTK, es decir que la llamada a gtk_init no fallara.
g_signal_handler_disconnect( gpointer object, gulong id);
Elimina el registro de un determinado evento en tu aplicación. g_signal_connect y g_signal_connect_swapped retornan el valor id que se debe utilizar en esta función como parámetro.
Tambien puedes deshabilitar y habilitar el registro de un evento utilizando estas funciones
g_signal_handler_block (gpointer object, gulong id);
g_signal_handler_block_by_func (gpointer object, GCallback func, gpointer data);
g_signal_handler_unblock (gpointer object, gulong id);
g_signal_handler_unblock_by_func (gpointer object, GCallback func, gpointer data);
Para poder modificar el titulo de una ventana, suponiendo que window es una ventana:
gtk_window_set_title (GTK_WINDOW (window), "Nuevo titulo");
Creacion de una caja (elemento invisible que puede contener varios objetos)
GtkWidget box;
box es ahora un objeto
box = gtk_hbox_new (FALSE,0);
box es ahora una objeto tipo caja horizontal, los objetos que contendrá se colocaran uno al lado del otro de izquierda a derecha o a la inversa segun los parámetros que se le pasen o:
gtk_vbox_new (FALSE,0);
La caja vertical con los objetos uno encima del otro, de arriba a abajo o a la inversa.
Siendo window un objeto ventana incluimos la caja en la ventana como siempre con:
gtk_container_add(GTK_CONTAINER (window,box);
ahora creamos y asignamos las señales a los objetos que deseamos añadir a la caja cuando tengamos listo CADA objeto ejecutamos esto
gtk_box_pack_start (GTK_BOX(box), button, TRUE, TRUE, 0);
suponindo que button es un objeto claro y:
gtk_widget_show(button);
Atencion: Podemos reutilizar la variable button después de esto para definir otros objetos.
Finalmente ejecutamos
gtk_windget_show(box);
y
gtk_widget_show(window);
Aqui tenemos un ejemplo completo:
is printed to stdout. /
static void callback( GtkWidget widget,
gpointer data )
{
g_print ("Hello again - %s was pressed\n", (gchar ) data);
}
/ another callback /
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
gtk_main_quit ();
return FALSE;
}
int main( int argc,
char argv[] )
{
/ 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 /
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/ 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);
/ 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_hbox_new (FALSE, 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");
/ 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 (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);
/ 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);
/ Rest in gtk_main and wait for the fun to begin! */
gtk_main ();
return 0;
}
Nos vemos ...
Comentarios :
No hay comentarios »
Sin embargo hay muchas mas características de las librerías que necesitamos conocer para realizar aplicaciones mas completas. Hare un pequeño resumen algunas de estas funciones.
boolean gtk_init_check(&argc, &argv)
Te permite verificar antes de ejecutar gtk_init que se va poder inizializar sin problemas GTK, es decir que la llamada a gtk_init no fallara.
g_signal_handler_disconnect( gpointer object, gulong id);
Elimina el registro de un determinado evento en tu aplicación. g_signal_connect y g_signal_connect_swapped retornan el valor id que se debe utilizar en esta función como parámetro.
Tambien puedes deshabilitar y habilitar el registro de un evento utilizando estas funciones
g_signal_handler_block (gpointer object, gulong id);
g_signal_handler_block_by_func (gpointer object, GCallback func, gpointer data);
g_signal_handler_unblock (gpointer object, gulong id);
g_signal_handler_unblock_by_func (gpointer object, GCallback func, gpointer data);
Para poder modificar el titulo de una ventana, suponiendo que window es una ventana:
gtk_window_set_title (GTK_WINDOW (window), "Nuevo titulo");
Creacion de una caja (elemento invisible que puede contener varios objetos)
GtkWidget box;
box es ahora un objeto
box = gtk_hbox_new (FALSE,0);
box es ahora una objeto tipo caja horizontal, los objetos que contendrá se colocaran uno al lado del otro de izquierda a derecha o a la inversa segun los parámetros que se le pasen o:
gtk_vbox_new (FALSE,0);
La caja vertical con los objetos uno encima del otro, de arriba a abajo o a la inversa.
Siendo window un objeto ventana incluimos la caja en la ventana como siempre con:
gtk_container_add(GTK_CONTAINER (window,box);
ahora creamos y asignamos las señales a los objetos que deseamos añadir a la caja cuando tengamos listo CADA objeto ejecutamos esto
gtk_box_pack_start (GTK_BOX(box), button, TRUE, TRUE, 0);
suponindo que button es un objeto claro y:
gtk_widget_show(button);
Atencion: Podemos reutilizar la variable button después de esto para definir otros objetos.
Finalmente ejecutamos
gtk_windget_show(box);
y
gtk_widget_show(window);
Aqui tenemos un ejemplo completo:
Our new improved callback. The data passed to this function
#include
/
is printed to stdout. /
static void callback( GtkWidget widget,
gpointer data )
{
g_print ("Hello again - %s was pressed\n", (gchar ) data);
}
/ another callback /
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
gtk_main_quit ();
return FALSE;
}
int main( int argc,
char argv[] )
{
/ 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 /
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/ 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);
/ 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_hbox_new (FALSE, 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");
/ 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 (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);
/ 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);
/ Rest in gtk_main and wait for the fun to begin! */
gtk_main ();
return 0;
}
Nos vemos ...
Categorías : Articulos programación
Referencias : No hay referencias »



Referencias
No hay referencias