#include <config.h>
#include <gtk/gtk.h>
/* User clicked the "Add List" button. */
void button_add_clicked( gpointer data )
{
int indx;
/* 字符串数组,用于加到l i s t中,4行2列* /
gchar *drink[4][2] = { { "Milk", "3 Oz" },
{ "Water", "6 l" },
{ "Carrots", "2" },
{ "Snakes", "55" } };
/* 下面将文本真正加到l i s t中。对每行添加一次.
*/
for (indx = 0; indx < 4; indx++)
gtk_clist_append ((GtkCList *)data, drink[indx]);
return;
}
/* 用户点击"Clear List" 按钮时的回调函数. */
void button_clear_clicked( gpointer data )
{
/* 用g t k _ c l i s t _ c l e a r函数清除列表。比用
* g t k _ c l i s t _ r e m o v e函数逐行清除要快.
*/
gtk_clist_clear ((GtkCList *)data);
return;
}
/* 用户点击"Hide/Show titles" 按钮时的回调函数. */
void button_hide_show_clicked( gpointer data )
{
/* flag是用于记录可见/不可见状态的标志。0 = 当前不可见 */
static short int flag = 0;
if (flag == 0)
{
/* 隐藏标题,将f l a g设置为1 */
gtk_clist_column_titles_hide ((GtkCList *)data);
flag++;
}
else
{
/* 形式标题,将f l a g设置为0 */
gtk_clist_column_titles_show ((GtkCList *)data);
flag--;
}
return;
}
/* 用户选中某一行时的回调函数. */
void selection_made( GtkWidget *clist,
gint row,
gint column,
GdkEventButton *event,
gpointer data )
{
gchar *text;
/* 取得存储在被选中的行和列的单元格上的文本
* 当鼠标点击时,我们用t e x t参数接收一个指针
*/
gtk_clist_get_text (GTK_CLIST (clist), row, column, &text);
/* J打印一些关于选中了哪一行的信息 */
g_print ("You selected row %d. More specifically you clicked in "
"column %d, and the text in this cell is %s\n\n",
row, column, text);
return;
}
int main( int argc,
gchar *argv[] )
{
GtkWidget *window;
GtkWidget *vbox, *hbox;
GtkWidget *scrolled_window, *clist;
GtkWidget *button_add, *button_clear, *button_hide_show;
gchar *titles[2] = { "Ingredients", "Amount" };
gtk_init(&argc, &argv);
window=gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request (GTK_WIDGET (window), 300, 150);
gtk_window_set_title (GTK_WINDOW (window), "GtkCList Example");
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit),
NULL);
vbox=gtk_vbox_new (FALSE, 5);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);
/* 创建一个滚动窗口构件,将G t k C L i s t组装到里面。
* 这样使得内容超出列表时,可以用滚动条浏览 */
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
gtk_widget_show (scrolled_window);
/* 创建G t k C L i s t构件。本例中,我们使用了两列 */
clist = gtk_clist_new_with_titles (2, titles);
/* 当作出选择时,我们要知道选择了哪一个单元格。使用
* selection_made 回调函数,代码在下面可以看见 */
g_signal_connect (G_OBJECT (clist), "select_row",
G_CALLBACK (selection_made),
NULL);
/* 不一定要设置边框的阴影,但是效果挺不错 :) */
gtk_clist_set_shadow_type (GTK_CLIST (clist), GTK_SHADOW_OUT);
/*很重要的一点,我们设置列宽,让文本能容纳在列中。
* 注意,列编号是从0开始的
* 本例中是0和1
*/
gtk_clist_set_column_width (GTK_CLIST (clist), 0, 150);
/* 将G t k C L i s t构件添加到滚动窗口构件中,然后显示. */
gtk_container_add (GTK_CONTAINER (scrolled_window), clist);
gtk_widget_show (clist);
/* C创建按钮,把它们加到窗口中.
*/
hbox = gtk_hbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
gtk_widget_show (hbox);
button_add = gtk_button_new_with_label ("Add List");
button_clear = gtk_button_new_with_label ("Clear List");
button_hide_show = gtk_button_new_with_label ("Hide/Show titles");
gtk_box_pack_start (GTK_BOX (hbox), button_add, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (hbox), button_clear, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (hbox), button_hide_show, TRUE, TRUE, 0);
/* 为三个按钮的点击设置回调函数 */
g_signal_connect_swapped (G_OBJECT (button_add), "clicked",
G_CALLBACK (button_add_clicked),
clist);
g_signal_connect_swapped (G_OBJECT (button_clear), "clicked",
G_CALLBACK (button_clear_clicked),
clist);
g_signal_connect_swapped (G_OBJECT (button_hide_show), "clicked",
G_CALLBACK (button_hide_show_clicked),
clist);
gtk_widget_show (button_add);
gtk_widget_show (button_clear);
gtk_widget_show (button_hide_show);
/*界面已经完全设置好了,下面可以显示窗口,进入
* gtk_main主循环
*/
gtk_widget_show (window);
gtk_main();
return 0;
}