/**
* 带有图像的标签
* 本例使用GtkEventBox作为容器,将GtkImage和GtkLabel组合为一个复合Widget
* 通过GtkLayout实现图像和文字的放置位置
* 本例中使用的图像均来自GTK的STOCK
* 不同的背景色用于区别标签,便于查看效果
* gcc -o 007.exe 007.c `pkg-config --cflags --libs gtk+-2.0`
* ./007.exe
*/
#include
int main(int argc,char** argv)
{
gtk_init(&argc,&argv);
GtkWidget* main_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width(GTK_CONTAINER(main_win),5);
GtkWidget* vb = gtk_vbox_new(FALSE,5);
gtk_container_add(GTK_CONTAINER(main_win),vb);
//图像居左,文本居右
//使用水平布局
GtkWidget* hbox = gtk_hbox_new(FALSE,2);
//采用两种颜色显示标签背景色
GdkColor red_color;
GdkColor blue_color;
gdk_color_parse("red",&red_color);
gdk_color_parse("blue",&blue_color);
//创建图像和文本,并将其pack到水平布局中
GtkWidget* icon = gtk_image_new_from_stock(GTK_STOCK_OPEN,GTK_ICON_SIZE_MENU);
GtkWidget* text = gtk_label_new("Open File");
gtk_box_pack_start(GTK_BOX(hbox),icon,FALSE,FALSE,0);
gtk_box_pack_start(GTK_BOX(hbox),text,FALSE,FALSE,0);
//将水平布局放入到GtkEventBox中,同时修改其背景颜色
GtkWidget* b = gtk_event_box_new();
gtk_widget_modify_bg(b,GTK_STATE_NORMAL,&red_color);
gtk_container_add(GTK_CONTAINER(b),hbox);
gtk_box_pack_start(GTK_BOX(vb),b,FALSE,FALSE,0);
//文本居左,图像居右
hbox = gtk_hbox_new(FALSE,2);
icon = gtk_image_new_from_stock(GTK_STOCK_OPEN,GTK_ICON_SIZE_MENU);
text = gtk_label_new("Open File");
gtk_box_pack_start(GTK_BOX(hbox),text,FALSE,FALSE,0);
gtk_box_pack_start(GTK_BOX(hbox),icon,FALSE,FALSE,0);
b = gtk_event_box_new();
gtk_widget_modify_bg(b,GTK_STATE_NORMAL,&blue_color);
gtk_container_add(GTK_CONTAINER(b),hbox);
gtk_box_pack_start(GTK_BOX(vb),b,FALSE,FALSE,0);
//图像居上,文本居下
hbox = gtk_vbox_new(FALSE,2);
icon = gtk_image_new_from_stock(GTK_STOCK_OPEN,GTK_ICON_SIZE_MENU);
text = gtk_label_new("Open File");
gtk_box_pack_start(GTK_BOX(hbox),icon,FALSE,FALSE,0);
gtk_box_pack_start(GTK_BOX(hbox),text,FALSE,FALSE,0);
b = gtk_event_box_new();
gtk_widget_modify_bg(b,GTK_STATE_NORMAL,&red_color);
gtk_container_add(GTK_CONTAINER(b),hbox);
gtk_box_pack_start(GTK_BOX(vb),b,FALSE,FALSE,0);
//文本居上,图像居下
hbox = gtk_vbox_new(FALSE,2);
icon = gtk_image_new_from_stock(GTK_STOCK_OPEN,GTK_ICON_SIZE_MENU);
text = gtk_label_new("Open File");
gtk_box_pack_start(GTK_BOX(hbox),text,FALSE,FALSE,0);
gtk_box_pack_start(GTK_BOX(hbox),icon,FALSE,FALSE,0);
b = gtk_event_box_new();
gtk_widget_modify_bg(b,GTK_STATE_NORMAL,&blue_color);
gtk_container_add(GTK_CONTAINER(b),hbox);
gtk_box_pack_start(GTK_BOX(vb),b,FALSE,FALSE,0);
g_signal_connect_swapped(G_OBJECT(main_win),"destroy",G_CALLBACK(gtk_main_quit),NULL);
gtk_widget_show_all(main_win);
gtk_main();
return 0;
}