標籤(GTKLabel) -- GTK開發資料


用過之後,你將會發現GTK裏的label控件比windows系統的label功能強大很多,很多.
與此同時,使用起來感覺也麻煩很多.

首先,在hello world的基礎上添加一點代碼.


#include <gtk/gtk.h>

void destroy (GtkWidget *widget, gpointer *data)
{
    gtk_main_quit ();
}

int main(int argc, char *argv[])
{
  GtkWidget *frmMain, *lblTest;

	gtk_init (&argc, &argv);

	frmMain = gtk_window_new (GTK_WINDOW_TOPLEVEL);

	gtk_signal_connect (GTK_OBJECT (frmMain), "destroy",GTK_SIGNAL_FUNC (destroy), NULL);

	gtk_container_border_width (GTK_CONTAINER (frmMain), 10);
	
	lblTest = gtk_label_new("label for test!");
	gtk_container_add(GTK_CONTAINER(frmMain), lblTest);

  gtk_widget_show (frmMain);
  gtk_widget_show(lblTest);

  gtk_main ();

  return 0;
}
這裏,添加了一個GtkWidget變量 *lblTest;
就是我們的label.
然後lblTest = gtk_label_new("label for test!");
這裏就相當於Windows編程中的create,創建了一個TLabel,當然這裏是GtkLabel.參數就是lable的Caption
然後gtk_container_add(GTK_CONTAINER(frmMain), lblTest);
這裏相當於Windows編程中的setparent,將這個label置於一個交窗口中,否則它不知道應該顯示在哪裏.
最後多了一行gtk_widget_show(lblTest);
與gtk_widget_show (frmMain);一樣,相當於WIndows中的setVisible(*,TRUE);有了此句,纔會顯示出此控件.

*排版;
1.長度.
這裏的label控件大多沒有類似vc,delphi中setwidth,setheight的方法.
甚至Label這裏基本沒有width,height這個屬性。
要改變寬度只有一個方法gtk_label_set_width_chars(GTK_LABEL(lblTest), 20);
設定label可以顯示多少個字符,從而調整label的長度.
如果你做過java,則不用急於瞭解這些,因爲gtk的排版與java類似,後面會有介紹.
2.對齊.
對齊的方法應該是gtk_label_set_justify(GTK_LABEL(lblTest), GTK_JUSTIFY_LEFT);
但我試了一下不起作用,這裏介紹一個更強大的對齊方法.
gtk_misc_set_alignment(GTK_MISC(lblTest), 1, 0);
第一個參數是lblTest,第二個參數是左右方向的對齊值,第三個參數是上下方向的對齊值;
對齊值的取值範圍爲0-1.取0時,爲左對齊,取1時,爲右對齊,取0.5時,爲中間對齊.
當然,你也可以取0.3,0.8試一下.這可是我從來沒見過的對齊方式.
*其它屬性;
1.可選.
windows中標準label是不可以做任何操作的,這裏可以給label做一個可選設定,用戶可以用鼠標選擇並進行復制等操作.
gtk_label_set_selectable (GTK_LABEL(lblTest), TRUE); 2.字體,大小,顏色.
這裏的設置比較麻煩一點,GTKLABEL本身並沒有提供直接的方法來操作這些屬性,其它庫的操作方法見後面補充。
而是提供了一種類似html瀏覽器的方法,需要在代碼中設定.
如:

gtk_label_set_markup(GTK_LABEL(lblTest),
   "<span foreground='red' underline='double' underline_color='blue' font_desc='32'>label for test!</span>");
這裏的<span>標籤就是用來操作label顯示的屬性的,包括背景色,字體色,字體,字體大小,下劃線,等等.
其中foreground='red' 就是定義前景色(字體顏色)爲紅色,也可以foreground='#00FF00',這兩個值是相等的.
underline='double' 是下劃線爲雙線,還可以設值爲'none', 'single', 'double', 'low', 'error' 等.
underline_color='blue' 是下劃線的顏色.
此外,還可以選的屬性有:
font_desc 字體大小,值如'12',官方文檔說明爲字體描述,但我測試結果爲字體大小
size 官方文檔爲字體大小,但我測試結果不起作用.
stype 樣式,值如'normal', 'oblique', 'italic' 等.
weight 字型,值如'ultralight', 'light', 'normal', 'bold', 'ultrabold'等
stretch 拉伸,值如'ultracondensed', 'extracondensed', 'condensed', 'semicondensed', 'normal', 'semiexpanded', 'expanded', 'extraexpanded', 'ultraexpanded'
屬性值不一一列出,你可以參考安裝時程序攜帶的文檔,相對位置爲"share/gtk-doc/html/pango/PangoMarkupFormat.html"
注意:官方文檔中有好多錯誤,可以參考但不可全信.
主要內容如下:

The root tag of a marked-up document is <markup>, but pango_parse_markup() allows you to
omit this tag, so you will most likely never need to use it. The most
general markup tag is <span>, then there are some convenience
tags. <span> has the following attributes:



<span> attributes


font_desc


A font description string, such as "Sans Italic 12". See
pango_font_description_from_string()
for a description of the format of the string representation . Note that any
other span attributes will override this description. So if you have
"Sans Italic" and also a style="normal" attribute, you will get Sans normal,
not italic.

font_family


A font family name

face


Synonym for font_family

size


Font size in 1024ths of a point, or one of the absolute sizes
'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large',
'xx-large', or one of the relative sizes 'smaller' or 'larger'.
If you want to specify a absolute size, it's usually easier
to take advantage of the ability to specify a partial
font description using 'font_desc'; you can use
font_desc='12.5' rather than
size='12800'.

style


One of 'normal', 'oblique', 'italic'

weight


One of 'ultralight', 'light', 'normal', 'bold', 'ultrabold', 'heavy',
or a numeric weight

variant


'normal' or 'smallcaps'

stretch



One of 'ultracondensed', 'extracondensed', 'condensed',
'semicondensed', 'normal', 'semiexpanded', 'expanded',
'extraexpanded', 'ultraexpanded'

foreground


An RGB color specification such as '#00FF00' or a color name such as
'red'

background


An RGB color specification such as '#00FF00' or a color name such as
'red'

underline


One of 'none', 'single', 'double', 'low', 'error'

underline_color


The color of underlines; an RGB color specification such as '#00FF00'
or a color name such as 'red'

rise


Vertical displacement, in 10000ths of an em. Can be negative for
subscript, positive for superscript.

strikethrough


'true' or 'false' whether to strike through the text

strikethrough_color


The color of strikethrough lines; an RGB color specification such as
'#00FF00' or a color name such as 'red'

fallback


'true' or 'false' whether to enable fallback. If disabled, then characters
will only be used from the closest matching font on the system. No fallback
will be done to other fonts on the system that might contain the characters
in the text. Fallback is enabled by default. Most applications should not
disable fallback.

lang


A language code, indicating the text language

letter_spacing


Inter-letter spacing in 1024ths of a point.

gravity


One of 'south', 'east', 'north', 'west', 'auto'.

gravity_hint


One of 'natural', 'strong', 'line'.

The following convenience tags are provided:

Convenience tags


b


Bold

big


Makes font relatively larger, equivalent to <span size="larger">

i


Italic

s


Strikethrough

sub


Subscript

sup


Superscript

small


Makes font relatively smaller, equivalent to <span size="smaller">

tt


Monospace font

u


Underline


整體看起來這個label就像是一個特定功能的webBrowse.
儘管功能比較強大,但給我們動態控制label屬性帶來很大的麻煩.

*事件
GtkLabel提供了三個事件:move_cursor,copy_clipboard, populate_popup.
第一個事件爲光標移動事件。第二個是內容被複制.
第一個我測試了好長時間,沒有觸發,而且頭文件中函數原型與官方文檔定義不符.
第二個測試成功,當我選擇label中內容並複製後,窗體的標題被修改.

總代碼如下:

#include <gtk/gtk.h>

GtkWidget *frmMain;

void destroy (GtkWidget *widget, gpointer *data)
{
    gtk_main_quit ();
}
void lblStrCopy (GtkLabel *label) {
  gtk_window_set_title (GTK_WINDOW (frmMain), "Hello GKT+");
}
int main(int argc, char *argv[])
{
  GtkWidget *lblTest;

	gtk_init (&argc, &argv);

	frmMain = gtk_window_new (GTK_WINDOW_TOPLEVEL);

	gtk_signal_connect (GTK_OBJECT (frmMain), "destroy",GTK_SIGNAL_FUNC (destroy), NULL);

	gtk_container_border_width (GTK_CONTAINER (frmMain), 10);
	//Label about
	lblTest = gtk_label_new("label for test!");
	gtk_container_add(GTK_CONTAINER(frmMain), lblTest);
	gtk_label_set_width_chars(GTK_LABEL(lblTest),30);
	gtk_misc_set_alignment(GTK_MISC(lblTest), 1, 0);
	gtk_label_set_selectable (GTK_LABEL(lblTest), TRUE);
  gtk_label_set_markup(GTK_LABEL(lblTest), 
    "label for test!");

  gtk_signal_connect (GTK_OBJECT (lblTest), "copy_clipboard",GTK_SIGNAL_FUNC (lblStrCopy), NULL);

  gtk_widget_show (frmMain);
  gtk_widget_show(lblTest);

  gtk_main ();

  return 0;
}



補充:
良久之後,才發現,原來可以用pango和gdk庫來操作label的顏色,字體,大小等.
其實不僅僅是label,其它的一些gtk控件也可以用同樣的方法操作.
看代碼:

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <pango/pango.h>

GtkWidget *frmMain;

void destroy (GtkWidget *widget, gpointer *data)
{
    gtk_main_quit ();
}

int main(int argc, char *argv[])
{
  GtkWidget *lbl1;

  gtk_init (&argc, &argv);

  frmMain = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_signal_connect (GTK_OBJECT (frmMain), "destroy",GTK_SIGNAL_FUNC (destroy), NULL);
  gtk_container_border_width (GTK_CONTAINER (frmMain), 10);
  //button Create;
  lbl1 = gtk_label_new("Quit");
  gtk_container_add(GTK_CONTAINER(frmMain), lbl1);
 
  GdkColor color1;
  gdk_color_parse ("red", &color1);
  gtk_widget_modify_fg(lbl1, GTK_STATE_NORMAL, &color1);

  PangoFontDescription *font1 = pango_font_description_from_string("Sans");
  pango_font_description_set_size (font1, 25 * PANGO_SCALE);
  gtk_widget_modify_font (lbl1, font1);
  
  //Show everything;
  gtk_widget_show (frmMain);
  gtk_widget_show (lbl1);

  gtk_main ();

  return 0;
}


比之前多引用了兩個頭文件:gdk和pango.
用gtk_widget_modify_fg函數來改變控件的前景色,用gtk_widget_modify_font來改變控件的字體(包含字體大小).
當然,在makefile中別忘了也要加上這兩個庫文件:gdk-win32-2.0.lib和pango-1.0.lib.


2008.11.22修改.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章