/*File:arrow.c
*Date:2013-12-15
*Author:sjin
*Mail:[email protected]
* */
#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;
}
Tooltips:
工具提示对象(Tooltips)就是当鼠标指针移到按钮或其它构件上并停留几秒时,弹出的文本串。 工具提示对象很容易使用。具体看下面的示例。
/*File:tooltip.c
*Date:2013-12-15
*Author:sjin
*Mail:[email protected]
*/
#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;
}
运行如下:
参考 资料:
http://guoyinghui2012.blog.163.com/blog/static/20871720020126294943228/