### 状态栏 Statusbars
状态栏(Statusbars)是一些简单的构件,一般用来显示文本消息。它将文本消息压入到一个栈里面,当弹出当前消息时,将重新显示前一条文本消息。看下面具体的代码实现:
![](https://box.kancloud.cn/2016-08-24_57bd779869703.jpg)
~~~
/*File:statusbar.c
*Date:2013-12-22
*Author:sjin
*Mail:413977243@qq.com
*/
#include <gtk/gtk.h>
#include <stdlib.h>
#include <glib.h>
GtkWidget *status_bar;
void push_item(GtkWidget *widget,gpointer data)
{
static int count = 1;
char buf[20] = {'\0'};
g_snprintf(buf,20,"Item %d",count++);
//将新消息加到状态栏
gtk_statusbar_push(GTK_STATUSBAR(status_bar),GPOINTER_TO_INT(data),buf);
return;
}
void pop_item(GtkWidget *widget,gpointer data)
{
//删除在栈中给定上下文标识符最上面的一个消息
gtk_statusbar_pop(GTK_STATUSBAR(status_bar),GPOINTER_TO_INT(data));
return;
}
int main(int argc, char*argv[])
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *vbox;
gint context_id;
//初始化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 状态栏工具");
//设定窗口的默认宽高
//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);
vbox = gtk_vbox_new(FALSE,1);
gtk_container_add(GTK_CONTAINER(window),vbox);
gtk_widget_show(vbox);
//创建一个状态栏
status_bar = gtk_statusbar_new();
gtk_box_pack_start(GTK_BOX(vbox),status_bar,TRUE,TRUE,0);
gtk_widget_show(status_bar);
//获得上下文的标识符
context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(status_bar),"Statusbar example");
button = gtk_button_new_with_label("push_item");
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(push_item),GINT_TO_POINTER(context_id));
gtk_box_pack_start(GTK_BOX(vbox),button,TRUE,TRUE,2);
gtk_widget_show(button);
button = gtk_button_new_with_label("pop last item");
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(pop_item),GINT_TO_POINTER(context_id));
gtk_box_pack_start(GTK_BOX(vbox),button,TRUE,TRUE,2);
gtk_widget_show(button);
gtk_widget_show_all(window);
gtk_main();
return FALSE;
}
~~~
### 文本输入构件 Text Entries
文本输入构件(Entry widget)允许在一个单行文本框里输入和显示一行文本。文本可以用函数进行操作,如将新的文本替换、前插、追加到文本输入构件的当前内容中。
![](https://box.kancloud.cn/2016-08-24_57bd7798801bb.jpg)
~~~
/*File:text_entry.c
*Date:2013-12-28
*Author:sjin
*Mail:413977243@qq.com
*/
#include<gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
static GtkWidget* entry1;
static GtkWidget* entry2;
void on_button_clicked (GtkWidget* button,gpointer data)
{
/*void gtk_entry_set_text(Gtk_Entry *entry,const gchr *text)
* 将新的内容取代文本输入构件当前的内容。
*const gchar *gtk_entry_get_text(GtkEntry *entry)
* 获得当前文本输入构件的内容
*/
if((int)data == 1){
gtk_entry_set_text(GTK_ENTRY(entry1),"");
gtk_entry_set_text(GTK_ENTRY(entry2),"");
} else if ((int)data == 2){
const gchar* username = gtk_entry_get_text(GTK_ENTRY(entry1));
const gchar* password = gtk_entry_get_text(GTK_ENTRY(entry2));
g_print("用户名是:%s",username);
g_print("\n");
g_print("密码是:%s\n",password);
} else if((int)data == 3){
/*改变文本空的可编辑状态*/
gtk_editable_set_editable(GTK_EDITABLE(entry1),GTK_TOGGLE_BUTTON(button)->active);
gtk_editable_set_editable(GTK_EDITABLE(entry2),GTK_TOGGLE_BUTTON(button)->active);
}
}
int main(int argc,char* argv[])
{
GtkWidget* window;
GtkWidget* box;
GtkWidget* box1;
GtkWidget* box2;
GtkWidget* box3;
GtkWidget* label1;
GtkWidget* label2;
GtkWidget* button;
GtkWidget* sep;
//初始化
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),"登录窗口");
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);
box1 = gtk_hbox_new(FALSE,0);
gtk_box_pack_start(GTK_BOX(box),box1,FALSE,FALSE,5);
box2 = gtk_hbox_new(FALSE,0);
gtk_box_pack_start(GTK_BOX(box),box2,FALSE,FALSE,5);
sep = gtk_hseparator_new();//分割线
gtk_box_pack_start(GTK_BOX(box),sep,FALSE,FALSE,5);
box3 = gtk_hbox_new(FALSE,0);
gtk_box_pack_start(GTK_BOX(box),box3,TRUE,TRUE,5);
label1 = gtk_label_new("用户名:");
entry1 = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(box1),label1,FALSE,FALSE,5);
gtk_box_pack_start(GTK_BOX(box1),entry1,FALSE,FALSE,5);
label2 = gtk_label_new("密 码:");
entry2 = gtk_entry_new();
/*设置输入文本不可见*/
gtk_entry_set_visibility(GTK_ENTRY(entry2),FALSE);
gtk_box_pack_start(GTK_BOX(box2),label2,FALSE,FALSE,5);
gtk_box_pack_start(GTK_BOX(box2),entry2,FALSE,FALSE,5);
button = gtk_check_button_new_with_label("Editable");
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),(gpointer)3);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),TRUE);
gtk_box_pack_start(GTK_BOX(box3),button,TRUE,TRUE,10);
gtk_widget_show(button);
button = gtk_button_new_with_label("清空");
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),(gpointer)1);
gtk_box_pack_start(GTK_BOX(box3),button,TRUE,TRUE,10);
gtk_widget_show(button);
button = gtk_button_new_with_label("确认");
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(on_button_clicked),(gpointer)2);
g_signal_connect_swapped(G_OBJECT(button),"clicked",G_CALLBACK(gtk_widget_destroy),window);
gtk_box_pack_start(GTK_BOX(box3),button,TRUE,TRUE,5);
gtk_widget_show(button);
gtk_widget_show_all(window);
gtk_main();
return FALSE;
}
~~~
### 组合框 Combo Box
组合框(combo box)是另一个很简单的构件,实际上它仅仅是其它构件的集合。从用户的观点来说,这个构件是由一个文本输入构件和一个下拉菜单组成的,用户可以从一个预先定义的列表里面选择一个选项,同时,用户也可以直接在文本框里面输入文本.
![](https://box.kancloud.cn/2016-08-24_57bd7799478b4.jpg)
~~~
#include <gtk/gtk.h>
void callback(GtkWidget *button,gpointer data)
{
printf("组合框发生改变\n");
if((int)data == 1){
gchar *ptr = gtk_entry_get_text(GTK_ENTRY(button));
printf("组合框A发生改变,内容是:%s\n",ptr);
}else if((int)data == 2) {
gchar *ptr = gtk_entry_get_text(GTK_ENTRY(button));
printf("组合框A发生改变,内容是:%s\n",ptr);
}
}
GtkWidget *create_item (gint i)
{
GtkWidget *item;
GtkWidget *label;
GtkWidget *hbox;
GtkWidget *image;
hbox = gtk_hbox_new(FALSE,0);
switch(i){
case 1:
image = gtk_image_new_from_stock(GTK_STOCK_YES,GTK_ICON_SIZE_MENU);
label = gtk_label_new("列表项一");
break;
case 2:
image = gtk_image_new_from_stock(GTK_STOCK_NO,GTK_ICON_SIZE_MENU);
label = gtk_label_new("列表项二");
break;
case 3:
image = gtk_image_new_from_stock(GTK_STOCK_HELP,GTK_ICON_SIZE_MENU);
label = gtk_label_new("列表项三");
break;
case 4:
image = gtk_image_new_from_stock(GTK_STOCK_OK,GTK_ICON_SIZE_MENU);
label = gtk_label_new("列表项四");
break;
case 5:
image = gtk_image_new_from_stock(GTK_STOCK_CANCEL,GTK_ICON_SIZE_MENU);
label = gtk_label_new("列表项五");
break;
}
gtk_box_pack_start(GTK_BOX(hbox),image,FALSE,FALSE,2);
gtk_box_pack_start(GTK_BOX(hbox),label,FALSE,FALSE,2);
item = gtk_list_item_new();
gtk_container_add(GTK_CONTAINER(item),hbox);
gtk_widget_show_all(item);
return item;
}
int main(int argc,char* argv[])
{
GtkWidget *window;
GtkWidget *combo;
GtkWidget *label;
GtkWidget *vbox;
GList *items = NULL;
GtkWidget *item;
items =g_list_append(items,"列表项A");
items =g_list_append(items,"列表项B");
items =g_list_append(items,"列表项C");
items =g_list_append(items,"列表项D");
items =g_list_append(items,"列表项E");
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
gtk_window_set_title(GTK_WINDOW(window),"下拉列表框");
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window),20);
vbox = gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),vbox);
label = gtk_label_new("组合框A");
gtk_box_pack_start(GTK_BOX(vbox),label,FALSE,FALSE,5);
combo = gtk_combo_new();
gtk_box_pack_start(GTK_BOX(vbox),combo,FALSE,FALSE,5);
gtk_combo_set_popdown_strings(GTK_COMBO(combo),items);
g_signal_connect(G_OBJECT(GTK_COMBO(combo)->entry),"changed",G_CALLBACK(callback),(gpointer)1);
/************另一种创建的方式****************/
label = gtk_label_new("组合框B");
gtk_box_pack_start(GTK_BOX(vbox),label,FALSE,FALSE,5);
combo = gtk_combo_new();
gtk_box_pack_start(GTK_BOX(vbox),combo,FALSE,FALSE,5);
item = create_item(1);
gtk_combo_set_item_string(GTK_COMBO(combo),GTK_ITEM(item),"项目一");
gtk_container_add(GTK_CONTAINER(GTK_COMBO(combo)->list),item);
item = create_item(2);
gtk_combo_set_item_string(GTK_COMBO(combo),GTK_ITEM(item),"项目二");
gtk_container_add(GTK_CONTAINER(GTK_COMBO(combo)->list),item);
item = create_item(3);
gtk_combo_set_item_string(GTK_COMBO(combo),GTK_ITEM(item),"项目三");
gtk_container_add(GTK_CONTAINER(GTK_COMBO(combo)->list),item);
item = create_item(4);
gtk_combo_set_item_string(GTK_COMBO(combo),GTK_ITEM(item),"项目四");
gtk_container_add(GTK_CONTAINER(GTK_COMBO(combo)->list),item);
item = create_item(5);
gtk_combo_set_item_string(GTK_COMBO(combo),GTK_ITEM(item),"项目五");
gtk_container_add(GTK_CONTAINER(GTK_COMBO(combo)->list),item);
g_signal_connect(G_OBJECT(GTK_COMBO(combo)->entry),"changed",G_CALLBACK(callback),(gpointer)2);
gtk_widget_show_all(window);
gtk_main();
return FALSE;
}
~~~