標籤 Labels
標籤(Labels)是 GTK 中最常用的元件,實際上它很簡單。因為沒有相關聯的 X 視窗,標籤不能引發信號。如果需要獲取或引發信號,可以將它放在一個事件盒中,或放在按鈕元件裡面。
用以下函式創建一個新標籤:
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 linen" "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-Justifiedn" "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-JustifiednMulti-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 則留言