Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->Linux技术 ->系统管理 ->正文

GTK v1.2 Tutorial(英文)

来源:Linux-cn.com 作者:Webmaster 时间:2007-05-05 点击: [收藏] [投稿]

/* example-start eventbox eventbox.c */
#include <gtk/gtk.h>
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *event_box;
GtkWidget *label;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Event Box");
gtk_signal_connect (GTK_OBJECT (window), "destroy",
GTK_SIGNAL_FUNC (gtk_exit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
/* Create an EventBox and add it to our toplevel window */
event_box = gtk_event_box_new ();
gtk_container_add (GTK_CONTAINER(window), event_box);
gtk_widget_show (event_box);
/* Create a long label */
label = gtk_label_new ("Click here to quit, quit, quit, quit, quit");
gtk_container_add (GTK_CONTAINER (event_box), label);
gtk_widget_show (label);
/* Clip it short. */
gtk_widget_set_usize (label, 110, 20);
/* And bind an action to it */
gtk_widget_set_events (event_box, GDK_BUTTON_PRESS_MASK);
gtk_signal_connect (GTK_OBJECT(event_box), "button_press_event",
GTK_SIGNAL_FUNC (gtk_exit), NULL);
/* Yet one more thing you need an X window for ... */
gtk_widget_realize (event_box);
gdk_window_set_cursor (event_box->window, gdk_cursor_new (GDK_HAND1));
gtk_widget_show (window);
gtk_main ();
return(0);
}
/* example-end */

10.2 The Alignment widget

  The alignment widget allows you to place a widget within its window at a position and size relative to the size of the Alignment widget itself. For example, it can be very useful for centering a widget within the window.

  There are only two functions associated with the Alignment widget:


GtkWidget* gtk_alignment_new( gfloat xalign,
gfloat yalign,gfloat xscale,gfloat yscale );
void gtk_alignment_set( GtkAlignment *alignment,
gfloatxalign,gfloatyalign,gfloatxscale,gfloatyscale );

  The first function creates a new Alignment widget with the specified parameters. The second function allows the alignment paramters of an exisiting Alignment widget to be altered.

  All four alignment parameters are floating point numbers which can range from 0.0 to 1.0. The xalign and yalign arguments affect the position of the widget placed within the Alignment widget. The xscale and yscale arguments effect the amount of space allocated to the widget.

  A child widget can be added to this Alignment widget using:


gtk_container_add( GTK_CONTAINER(alignment), child_widget );

  For an example of using an Alignment widget, refer to the example for the Progress Bar widget.

10.3 Fixed Container

  The Fixed container allows you to place widgets at a fixed position within it's window, relative to it's upper left hand corner. The position of the widgets can be changed dynamically.

  There are only three functions associated with the fixed widget:


GtkWidget* gtk_fixed_new( void );
void gtk_fixed_put( GtkFixed*fixed,
GtkWidget *widget,gint16 x,gint16 y );
void gtk_fixed_move( GtkFixed*fixed,
GtkWidget *widget,gint16 x,gint16 y );

  The function gtk_fixed_new allows you to create a new Fixed container.

  gtk_fixed_put places widget in the container fixed at the position specified by x and y.

  gtk_fixed_move allows the specified widget to be moved to a new position.

  The following example illustrates how to use the Fixed Container.


/* example-start fixed fixed.c */
#include <gtk/gtk.h>
/* I'm going to be lazy and use some global variables to
 * store the position of the widget within the fixed
 * container */
gint x=50;
gint y=50;
/* This callback function moves the button to a new position
 * in the Fixed container. */
void move_button( GtkWidget *widget,
GtkWidget *fixed )
{
x = (x+30)%300;
y = (y+50)%300;
gtk_fixed_move( GTK_FIXED(fixed), widget, x, y); 
}
int main( int argc,
char *argv[] )
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window;
GtkWidget *fixed;
GtkWidget *button;
gint i;
/* Initialise GTK */
gtk_init(&argc, &argv);
/* Create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Fixed Container");
/* Here we connect the "destroy" event to a signal handler */ 
gtk_signal_connect (GTK_OBJECT (window), "destroy",
GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
/* Sets the border width of the window. */
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
/* Create a Fixed Container */
fixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), fixed);
gtk_widget_show(fixed);
for (i = 1 ; i <= 3 ; i++) {
/* Creates a new button with the label "Press me" */
button = gtk_button_new_with_label ("Press me");
/* When the button receives the "clicked" signal, it will call the
 * function move_button() passing it the Fixed Container as its
 * argument. */
gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (move_button), fixed);
/* This packs the button into the fixed containers window. */
gtk_fixed_put (GTK_FIXED (fixed), button, i*50, i*50);
/* The final step is to display this newly created widget. */
gtk_widget_show (button);
}
/* Display the window */
gtk_widget_show (window);
/* Enter the event loop */
gtk_main ();
return(0);
}
/* example-end */


 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:GTK入门导引   下一篇:GDK Reference Manual

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号