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

GTK v1.2 Tutorial(英文)

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

  You will notice that the program does not call gtk_signal_connect for the "delete_event", but only for the "destroy" signal. This will still perform the desired function, because an unhandled "delete_event" will result in a "destroy" signal being given to the window.

9. Miscellaneous Widgets

9.1 Labels

  Labels are used a lot in GTK, and are relatively simple. Labels emit no signals as they do not have an associated X window. If you need to catch signals, or do clipping, place it inside a EventBox widget or a Button widget.

  To create a new label, use:


GtkWidget *gtk_label_new( char *str );

  The sole argument is the string you wish the label to display.

  To change the label's text after creation, use the function:


void gtk_label_set_text( GtkLabel *label,char *str );

  The first argument is the label you created previously (cast using the GTK_LABEL() macro), and the second is the new string.

  The space needed for the new string will be automatically adjusted if needed. You can produce multi-line labels by putting line breaks in the label string.

  To retrieve the current string, use:


void gtk_label_get( GtkLabel*label,char **str );

  The first argument is the label you've created, and the second, the return for the string. Do not free the return string, as it is used internally by GTK.

  The label text can be justified using:


void gtk_label_set_justify( GtkLabel *label,
GtkJustificationjtype );

  Values for jtype are:

  GTK_JUSTIFY_LEFT

  GTK_JUSTIFY_RIGHT

  GTK_JUSTIFY_CENTER (the default)

  GTK_JUSTIFY_FILL

  The label widget is also capable of line wrapping the text automatically. This can be activated using:


void gtk_label_set_line_wrap (GtkLabel *label,gbooleanwrap);

  The wrap argument takes a TRUE or FALSE value.

  If you want your label underlined, then you can set a pattern on the label:


void gtk_label_set_pattern (GtkLabel*label,const gchar *pattern);

  The pattern argument indicates how the underlining should look. It consists of a string of underscore and space characters. An underscore indicates that the corresponding character in the label should be underlined. For example, the string

  "__ __"

  would underline the first two characters and eight and ninth characters.

  Below is a short example to illustrate these functions. This example makes use of the Frame widget to better demonstrate the label styles. You can ignore this for now as the Frame widget is explained later on.


/* example-start label label.c */
#include <gtk/gtk.h>
int main( int argc,
char *argv[] )
{
static GtkWidget *window = NULL;
GtkWidget *hbox;
GtkWidget *vbox;
GtkWidget *frame;
GtkWidget *label;
/* Initialise GTK */
gtk_init(&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_signal_connect (GTK_OBJECT (window), "destroy",
GTK_SIGNAL_FUNC(gtk_main_quit),
NULL);
gtk_window_set_title (GTK_WINDOW (window), "Label");
vbox = gtk_vbox_new (FALSE, 5);
hbox = gtk_hbox_new (FALSE, 5);
gtk_container_add (GTK_CONTAINER (window), hbox);
gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER (window), 5);
frame = gtk_frame_new ("Normal Label");
label = gtk_label_new ("This is a Normal label");
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
frame = gtk_frame_new ("Multi-line Label");
label = gtk_label_new ("This is a Multi-line label.
Second line
" 
 "Third line");
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
frame = gtk_frame_new ("Left Justified Label");
label = gtk_label_new ("This is a Left-Justified
" 
 "Multi-line label.
Thirdline");
gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
frame = gtk_frame_new ("Right Justified Label");
label = gtk_label_new ("This is a Right-Justified
Multi-line label.
" 
 "Fourth line, (j/k)");
gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_RIGHT);
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
vbox = gtk_vbox_new (FALSE, 5);
gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
frame = gtk_frame_new ("Line wrapped label");
label = gtk_label_new ("This is an example of a line-wrapped label.It " 
 "should not be taking up the entire " /* big space to test spacing */
 "width allocated to it, but automatically " 
 "wraps the words to fit." 
 "The time has come, for all good men, to come to " 
 "the aid of their party." 
 "The sixth sheik's six sheep's sick.
" 
 " It supports multiple paragraphs correctly, " 
 "andcorrectly adds "
 "manyextraspaces. ");
gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
frame = gtk_frame_new ("Filled, wrapped label");
label = gtk_label_new ("This is an example of a line-wrapped, filled label." 
 "It should be taking "
 "up the entirewidth allocated to it." 
 "Here is a sentence to prove "
 "my point.Here is another sentence. "
 "Here comes the sun, do de do de do.
"
 "This is a new paragraph.
"
 "This is another newer, longer, better " 
 "paragraph.It is coming to an end, "
 "unfortunately.");
gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_FILL);
gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
frame = gtk_frame_new ("Underlined label");
label = gtk_label_new ("This label is underlined!
"
 "This one is underlined in quite a funky fashion");
gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
gtk_label_set_pattern (GTK_LABEL (label),
 "_________________________ _ _________ _ ______ __ _______ ___");
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
gtk_widget_show_all (window);
gtk_main ();
return(0);
}
/* example-end */


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



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

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