标签 Labels
标签(Labels)是 GTK 中最常用的元件,实际上它很简单。因为没有相关联的 X 视窗,标签不能引发信号。如果需要获取或引发信号,可以将它放在一个事件盒中,或放在按钮元件里面。
[adsense]
用以下函式创建一个新标签:
GtkWidget *gtk_label_new( const char *str ); GtkWidget *gtk_label_new_with_mnemonic( const char *str ); |
唯一的参数是要由标签显示的字串。
创建标签后,要改变标签你的文字,用以下函式:
void gtk_label_set_text( GtkLabel *label, const char *str ); |
第一参数是前面创建的标签(用 GTK_LABEL() 巨集转换),第二个参数是新的字串。
如果需要,新字串需要的空间会做自动调整。在字串中放置换行字元,可以创建多行标签。
用以下函式取得标签的当前文字:
const gchar* gtk_label_get_text( GtkLabel *label ); |
不要释放返回的字串,因为 GTK 内部要使用它。
标签的文字可以用以下函式设置对齐方式:
void gtk_label_set_justify( GtkLabel *label, GtkJustification jtype ); |
jtype 的值可以是:
GTK_JUSTIFY_LEFT 左对齐 GTK_JUSTIFY_RIGHT 右对齐 GTK_JUSTIFY_CENTER 居中对齐(预设) GTK_JUSTIFY_FILL 充满 |
标签元件的文字会自动换行。用以下函式来启用“自动换行”:
void gtk_label_set_line_wrap (GtkLabel *label, gboolean wrap); |
wrap参数可取 TRUE 或 FALSE。
如果想要使标签加下划线,可以在标签中设置显示模式:
void gtk_label_set_pattern (GtkLabel *label, const gchar *pattern); |
pattern 参数指定下划线的外观。它由一串下划线和空格组成。下划线指示标签的相应字元应该加一个下划线。例如,"__ __" 将在标签的第1、第2个字元和第8、第9个字元加下划线。
如果你只是想创建一个用下划线代表快捷键(“mnemonic”)的标签,你应该用 gtk_label_new_with_mnemonic() 或 gtk_label_set_text_with_mnemonic(),而不是用 gtk_label_set_pattern()。 |
下面是一个说明这些函式的简短范例。这个范例用框架元件(Frame widget)能更好地示范标签的风格。现在你不用理会这点,框架元件以后再作介绍。
在 GTK+ 2.0 里,标签文字里能包含改变字型等文字属性的标记 ,并且标签能设置为可以被选择(用来复制-贴上)。这些进阶特性在这里并不介绍。
#include <gtk/gtk.h> int main( int argc, char *argv[] ) { static GtkWidget *window = NULL; GtkWidget *hbox; GtkWidget *vbox; GtkWidget *frame; GtkWidget *label; /* 初始化 */ gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); g_signal_connect (window, "destroy", G_CALLBACK (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.\nSecond line\n" \ "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\n" \ "Multi-line label.\nThird line"); 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\nMulti-line label.\n" \ "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 " /* 用一段较长的空白字元来测试空白的自动排列 */\ "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.\n" \ " It supports multiple paragraphs correctly, " \ "and correctly adds "\ "many extra spaces. "); 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 entire width allocated to it. " \ "Here is a sentence to prove "\ "my point. Here is another sentence. "\ "Here comes the sun, do de do de do.\n"\ " This is a new paragraph.\n"\ " 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!\n" "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; }
1 則留言
Comments are closed.