### 标签 Labels
标签(Labels)是 GTK 中最常用的构件,实际上它很简单。因为没有相关联的 X 窗口,标签不能引发信号。如果需要获取或引发信号,可以将它放在一个事件盒中,或放在按钮构件里面。
下面是Labels的使用一个实例,点击按钮,可以打印原来标签的内容,并设置新的内容,具体代码如下:
~~~
/*File:label.c
*Date:2013-12-13
*Author:sjin
*Mail:413977243@qq.com
*/
#include<gtk/gtk.h>
GtkWidget *label1;
void callBack(GtkWidget *widget,gpointer data)
{
printf("label1 = %s\n",gtk_label_get_text(GTK_LABEL(label1)));
gtk_label_set_text(GTK_LABEL(label1),(char*)data);
sleep(1);
gtk_main_quit();
}
int main(int argc, char*argv[])
{
GtkWidget *window;
GtkWidget *box;
GtkWidget *label2;
GtkWidget *label3;
GtkWidget *label4;
GtkWidget *frame1;
GtkWidget *frame2;
GtkWidget *frame3;
GtkWidget *frame4;
GtkWidget *button;
gchar *title ;
//初始化GTK
gtk_init(&argc,&argv);
//创建最上层主窗口
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
//连接信号"delete_event",使得窗口关闭时发生
g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
//设置窗口标题
gtk_window_set_title(GTK_WINDOW(window),"多种样式的标签");
//设定窗口的默认宽高
//gtk_window_set_default_size(GTK_WINDOW(window),200,300);
//设定窗口的位置,让窗口总在最前端
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
//设定容器边框的宽度
gtk_container_set_border_width(GTK_CONTAINER(window),20);
//创建盒子
box = gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),box);
//创建框架1
frame1 = gtk_frame_new("标签一");
label1 = gtk_label_new("这是第一个标签,居左边的。This is the first label.");
gtk_container_add(GTK_CONTAINER(frame1),label1);
/*void gtk_label_set_justify(GtkLabel *label, GtkJustification jtype);
* jtype 值:GTK_JUSTIFY_LEFT :左对齐
* GTK_JUSTIFY_RIGHT:右对齐
* GTK_JUSTIFY_CENTER:居中对齐(默认 )
* GTK_JUSTIFY_FILL:充满
*/
gtk_label_set_justify(GTK_LABEL(label1),GTK_JUSTIFY_LEFT);
gtk_box_pack_start(GTK_BOX(box),frame1,FALSE,FALSE,5);
frame2 = gtk_frame_new("标签二");
label2 = gtk_label_new("这是第二个标签,它是多行的。\n这还是第二个标签的内容,它是居右边的。");
gtk_container_add(GTK_CONTAINER(frame2),label2);
gtk_label_set_justify(GTK_LABEL(label2),GTK_JUSTIFY_RIGHT);
//设置下划线
gtk_label_set_pattern(GTK_LABEL(label2),"__ __ __ ");
gtk_box_pack_start(GTK_BOX(box),frame2,FALSE,FALSE,5);
frame3 = gtk_frame_new("标签三");
label3 = gtk_label_new(NULL);
//标记语言形式
title = "<span foreground=\"red\"><big><i>这是第三个标签。\n它被格式化成红色了,并且字体也大了。</i></big></span>";
gtk_container_add(GTK_CONTAINER(frame3),label3);
gtk_label_set_markup(GTK_LABEL(label3),title);
gtk_box_pack_start(GTK_BOX(box),frame3,FALSE,FALSE,5);
frame4 = gtk_frame_new("标签四");
label4 = gtk_label_new("这也是一个多行标签,它的换行方式和上一个有所不同,主要是编辑手段不一样了,请仔细查看一下源码就会明白怎么回事了。");
gtk_label_set_justify(GTK_LABEL(label1),GTK_JUSTIFY_LEFT);
//自动换行函数
gtk_label_set_line_wrap(GTK_LABEL(label4),TRUE);
gtk_container_add(GTK_CONTAINER(frame4),label4);
gtk_box_pack_start(GTK_BOX(box),frame4,FALSE,FALSE,5);
button = gtk_button_new_with_label("set label1");
gtk_box_pack_start(GTK_BOX(box),button,FALSE,FALSE,5);
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(callBack),"set label1 111");
gtk_container_add(GTK_CONTAINER(window),button);
gtk_widget_show(button);
gtk_widget_show_all(window);
gtk_main();
return FALSE;
}
~~~
运行结果:
![](https://box.kancloud.cn/2016-08-24_57bd77976cd0f.jpg)
### 箭头 Arrows
箭头构件(Arrow widget)画一个箭头,面向几种不同的方向,并有几种不同的风格。在许多应用程序中,常用于创建带箭头的按钮。和标签构件一样,它不能引发信号。下面是一个简单的示例:
~~~
/*File:arrow.c
*Date:2013-12-15
*Author:sjin
*Mail:413977243@qq.com
* */
#include<gtk/gtk.h>
//创建自己按钮的函数
GtkWidget * create_arrow_button(GtkArrowType arrowtype, GtkShadowType shadowtype )
{
GtkWidget *button;
GtkWidget *arrow;
button = gtk_button_new();
/*void gtk_arrow_new(GtkArrowType arrow_type,GtkShadowType shadow_type);
* 创建箭头,
* arrow_type:箭头方向;
* GTK_ARROW_UP:向上
* GTK_ARROW_DOWN:向下
* GTK_ARROW_LEFT:向左
* GTK_ARROW_RIGHT:向右
* shadow_type:箭头阴影类型
* GTK_SHADOW_IN:
* GTK_SHADOW_OUT():
* GTK_SHADOW_ETCHED_IN:
* GTK_SHADOW_ETCHED_OUT:
*
*/
arrow = gtk_arrow_new(arrowtype,shadowtype);
gtk_container_add(GTK_CONTAINER(button),arrow);
gtk_widget_show(arrow);
return button;
}
void callBack(GtkWidget *widget,gchar *data)
{
printf("### %s is press\n",(char *)data);
}
//主函数
int main(int argc, char*argv[])
{
GtkWidget *window;
GtkWidget *box;
GtkWidget *arrow1;
GtkWidget *arrow2;
GtkWidget *arrow3;
GtkWidget *arrow4;
gchar *title = "方向按钮";
//初始化GTK
gtk_init(&argc,&argv);
//创建最上层主窗口
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
//连接信号"delete_event",使得窗口关闭时发生
g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
//设置窗口标题
gtk_window_set_title(GTK_WINDOW(window),title);
//设定窗口的默认宽高
//gtk_window_set_default_size(GTK_WINDOW(window),200,300);
//设定窗口的位置,让窗口总在最前端
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
//设定容器边框的宽度
gtk_container_set_border_width(GTK_CONTAINER(window),20);
//创建盒子
box = gtk_hbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),box);
gtk_widget_show(box);
//创建带箭头的按钮,调用自定义的函数
arrow1 = create_arrow_button(GTK_ARROW_LEFT,GTK_SHADOW_IN);
g_signal_connect(G_OBJECT(arrow1),"clicked",G_CALLBACK(callBack),"向左");
gtk_box_pack_start(GTK_BOX(box),arrow1,FALSE,FALSE,13);
gtk_widget_show(arrow1);
arrow2 = create_arrow_button(GTK_ARROW_UP,GTK_SHADOW_OUT);
g_signal_connect(G_OBJECT(arrow2),"clicked",G_CALLBACK(callBack),"向上");
gtk_box_pack_start(GTK_BOX(box),arrow2,FALSE,FALSE,13);
gtk_widget_show(arrow2);
arrow3 = create_arrow_button(GTK_ARROW_DOWN,GTK_SHADOW_ETCHED_IN);
g_signal_connect(G_OBJECT(arrow3),"clicked",G_CALLBACK(callBack),"向下");
gtk_box_pack_start(GTK_BOX(box),arrow3,FALSE,FALSE,13);
gtk_widget_show(arrow3);
arrow4 = create_arrow_button(GTK_ARROW_RIGHT,GTK_SHADOW_ETCHED_OUT);
g_signal_connect(G_OBJECT(arrow4),"clicked",G_CALLBACK(callBack),"向右");
gtk_box_pack_start(GTK_BOX(box),arrow4,FALSE,FALSE,13);
gtk_widget_show(arrow4);
gtk_widget_show(window);
gtk_main();
return FALSE;
}
~~~
![](https://box.kancloud.cn/2016-08-24_57bd779786af5.jpg)
### Tooltips:
工具提示对象(Tooltips)就是当鼠标指针移到按钮或其它构件上并停留几秒时,弹出的文本串。 工具提示对象很容易使用。具体看下面的示例。
~~~
/*File:tooltip.c
*Date:2013-12-15
*Author:sjin
*Mail:413977243@qq.com
*/
#include <gtk/gtk.h>
GtkTooltips *tooltips;
int ch = 0;
void callback(GtkWidget *widget,gpointer data)
{
//禁用已经激活的工具提示
if(ch){
gtk_tooltips_disable(tooltips);
printf("0000\n");
ch = 0;
}else{
ch = 1;
printf("1111\n");
gtk_tooltips_enable(tooltips);
}
}
int main(int argc, char*argv[])
{
GtkWidget *window;
GtkWidget *button;
//初始化GTK
gtk_init(&argc,&argv);
//创建最上层主窗口
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
//连接信号"delete_event",使得窗口关闭时发生
g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
//设置窗口标题
gtk_window_set_title(GTK_WINDOW(window),"工具提示对象");
//设定窗口的默认宽高
//gtk_window_set_default_size(GTK_WINDOW(window),200,300);
//设定窗口的位置,让窗口总在最前端
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
//设定容器边框的宽度
gtk_container_set_border_width(GTK_CONTAINER(window),20);
//创建按钮
button = gtk_button_new_with_label("关闭工具提示");
gtk_container_add(GTK_CONTAINER(window),button);
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(callback),NULL);
gtk_widget_show(button);
//创建工具提示
tooltips = gtk_tooltips_new();
gtk_tooltips_set_tip(tooltips,button,"这是一个工具提示按钮",NULL);
gtk_tooltips_disable(tooltips);
gtk_widget_show_all(window);
gtk_main();
return FALSE;
}
~~~
### 进度条 Progress Bars
进度条用于显示正在进行的操作的状态。
~~~
#include <gtk/gtk.h>
typedef struct _ProgressData {
GtkWidget *window;
GtkWidget *pbar;
int timer;
gboolean activity_mode;
}ProgressData;
/* 更新进度条,这样就能够看到进度条的移动 */
gint progress_timeout( gpointer data )
{
ProgressData *pdata =(ProgressData *)data;
gdouble new_val;
if (pdata->activity_mode)
gtk_progress_bar_pulse (GTK_PROGRESS_BAR (pdata->pbar));
else
{
/* 使用在调整对象中设置的取值范围计算进度条的值 */
new_val =gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (pdata->pbar)) + 0.01;
if (new_val >1.0)
new_val =0.0;
/* 设置进度条的新值 */
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pdata->pbar), new_val);
}
/* 这是一个 timeout 函数,返回 TRUE,这样它就能够继续被调用 */
return TRUE;
}
/* 回调函数,切换在进度条你的滑槽上的文本显示 */
void toggle_show_text( GtkWidget *widget,
ProgressData *pdata )
{
const gchar *text;
text =gtk_progress_bar_get_text (GTK_PROGRESS_BAR (pdata->pbar));
if (text && *text)
gtk_progress_bar_set_text (GTK_PROGRESS_BAR (pdata->pbar), "");
else
gtk_progress_bar_set_text (GTK_PROGRESS_BAR (pdata->pbar), "some text");
}
/* 回调函数,切换进度条的活动模式 */
void toggle_activity_mode( GtkWidget *widget,
ProgressData *pdata )
{
pdata->activity_mode = !pdata->activity_mode;
if (pdata->activity_mode)
gtk_progress_bar_pulse (GTK_PROGRESS_BAR (pdata->pbar));
else
/**/
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pdata->pbar), 0.0);
}
/* 回调函数,切换进度条的移动方向 */
void toggle_orientation( GtkWidget *widget,
ProgressData *pdata )
{
/*获得滚动条的移动方向*/
switch (gtk_progress_bar_get_orientation (GTK_PROGRESS_BAR (pdata->pbar))) {
case GTK_PROGRESS_LEFT_TO_RIGHT:
/*设置滚动条移动方向*/
gtk_progress_bar_set_orientation (GTK_PROGRESS_BAR (pdata->pbar),
GTK_PROGRESS_RIGHT_TO_LEFT);
break;
case GTK_PROGRESS_RIGHT_TO_LEFT:
gtk_progress_bar_set_orientation (GTK_PROGRESS_BAR (pdata->pbar),
GTK_PROGRESS_LEFT_TO_RIGHT);
break;
default:
break;
// 什么也不做
}
}
/* 清除分配的内存,删除定时器(timer) */
void destroy_progress( GtkWidget *widget,ProgressData *pdata)
{
gtk_timeout_remove (pdata->timer);
pdata->timer =0;
pdata->window =NULL;
g_free (pdata);
gtk_main_quit ();
}
int main( int argc,char *argv[])
{
ProgressData *pdata;
GtkWidget *align;
GtkWidget *separator;
GtkWidget *table;
GtkWidget *button;
GtkWidget *check;
GtkWidget *vbox;
gtk_init (&argc, &argv);
/* 为传递到回调函数中的数据分配内存 */
pdata =g_malloc (sizeof (ProgressData));
pdata->window =gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_resizable (GTK_WINDOW (pdata->window), TRUE);
g_signal_connect (G_OBJECT (pdata->window), "destroy",G_CALLBACK (destroy_progress),pdata);
gtk_window_set_title (GTK_WINDOW (pdata->window), "进度条练习");
gtk_container_set_border_width (GTK_CONTAINER (pdata->window), 0);
vbox =gtk_vbox_new (FALSE, 5);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
gtk_container_add (GTK_CONTAINER (pdata->window), vbox);
gtk_widget_show (vbox);
/* 创建一个居中对齐的对象 */
align =gtk_alignment_new (0.5, 0.5, 0, 0);
gtk_box_pack_start (GTK_BOX(vbox), align, FALSE, FALSE, 5);
gtk_widget_show (align);
/* 创建进度条 */
pdata->pbar =gtk_progress_bar_new ();
gtk_container_add (GTK_CONTAINER (align), pdata->pbar);
gtk_widget_show (pdata->pbar);
/* 加一个定时器(timer),以更新进度条的值 */
pdata->timer =gtk_timeout_add (100, progress_timeout, pdata);
separator =gtk_hseparator_new ();
gtk_box_pack_start (GTK_BOX(vbox), separator, FALSE, FALSE, 0);
gtk_widget_show (separator);
/* 行数、列数、同质性(homogeneous) */
table =gtk_table_new (2, 2, FALSE);
gtk_box_pack_start (GTK_BOX(vbox), table, FALSE, TRUE, 0);
gtk_widget_show (table);
/* 添加一个复选按钮,以选择是否显示在滑槽里的文本 */
check =gtk_check_button_new_with_label ("Show text");
gtk_table_attach (GTK_TABLE (table), check, 0, 1, 0, 1,GTK_EXPAND |GTK_FILL, GTK_EXPAND |GTK_FILL,5, 5);
g_signal_connect (G_OBJECT (check), "clicked",G_CALLBACK (toggle_show_text),pdata);
gtk_widget_show (check);
/* 添加一个复选按钮,切换活动状态 */
check =gtk_check_button_new_with_label ("Activity mode");
gtk_table_attach (GTK_TABLE (table), check, 0, 1, 1, 2,GTK_EXPAND |GTK_FILL, GTK_EXPAND |GTK_FILL,5, 5);
g_signal_connect (G_OBJECT (check), "clicked",G_CALLBACK (toggle_activity_mode),pdata);
gtk_widget_show (check);
/* 添加一个复选按钮,切换移动方向 */
check =gtk_check_button_new_with_label ("Right to Left");
gtk_table_attach (GTK_TABLE (table), check, 0, 1, 2, 3,GTK_EXPAND |GTK_FILL, GTK_EXPAND |GTK_FILL,5, 5);
g_signal_connect (G_OBJECT (check), "clicked",G_CALLBACK (toggle_orientation),pdata);
gtk_widget_show (check);
/* 添加一个按钮,用来退出应用程序 */
button =gtk_button_new_with_label ("close");
g_signal_connect_swapped (G_OBJECT (button), "clicked",G_CALLBACK (gtk_widget_destroy),pdata->window);
gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);
/* 将按钮设置为能缺省的构件 */
GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
/* 将缺省焦点设置到这个按钮上,使之成为缺省按钮,只要按回车键
* 就相当于点击了这个按钮 */
//译者注: 能缺省的构件在获取焦点后成为缺省构件,用户按方向键等可以切换焦点。
gtk_widget_grab_default (button);
gtk_widget_show (button);
gtk_widget_show (pdata->window);
gtk_main ();
return 0;
}
~~~
运行如下:
![](https://box.kancloud.cn/2016-08-24_57bd77979ae82.jpg)
参考 资料:
[http://guoyinghui2012.blog.163.com/blog/static/20871720020126294943228/](http://guoyinghui2012.blog.163.com/blog/static/20871720020126294943228/)