杂项构件

标签 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个字符加下划线。

Note

如果你只是想创建一个用下划线代表快捷键("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 (G_OBJECT (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;
}