📄 gtk_tut-12.html
字号:
gtk_widget_show(scrolled_window); /* create the GtkList widget * connect the sigh_print_selection() signal handler * function to the "selection_changed" signal of the GtkList * to print out the selected items each time the selection * has changed */ gtklist=gtk_list_new(); gtk_container_add(GTK_CONTAINER(scrolled_window), gtklist); gtk_widget_show(gtklist); gtk_signal_connect(GTK_OBJECT(gtklist), "selection_changed", GTK_SIGNAL_FUNC(sigh_print_selection), NULL); /* we create a "Prison" to put a list item in ;) */ frame=gtk_frame_new("Prison"); gtk_widget_set_usize(frame, 200, 50); gtk_container_border_width(GTK_CONTAINER(frame), 5); gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT); gtk_container_add(GTK_CONTAINER(vbox), frame); gtk_widget_show(frame); /* connect the sigh_button_event() signal handler to the GtkList * wich will handle the "arresting" of list items */ gtk_signal_connect(GTK_OBJECT(gtklist), "button_release_event", GTK_SIGNAL_FUNC(sigh_button_event), frame); /* create a separator */ separator=gtk_hseparator_new(); gtk_container_add(GTK_CONTAINER(vbox), separator); gtk_widget_show(separator); /* finaly create a button and connect it愀 "clicked" signal * to the destroyment of the window */ button=gtk_button_new_with_label("Close"); gtk_container_add(GTK_CONTAINER(vbox), button); gtk_widget_show(button); gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(window)); /* now we create 5 list items, each having it愀 own * label and add them to the GtkList using gtk_container_add() * also we query the text string from the label and * associate it with the list_item_data_key for each list item */ for (i=0; i<5; i++) { GtkWidget *label; gchar *string; sprintf(buffer, "ListItemContainer with Label #%d", i); label=gtk_label_new(buffer); list_item=gtk_list_item_new(); gtk_container_add(GTK_CONTAINER(list_item), label); gtk_widget_show(label); gtk_container_add(GTK_CONTAINER(gtklist), list_item); gtk_widget_show(list_item); gtk_label_get(GTK_LABEL(label), &string); gtk_object_set_data(GTK_OBJECT(list_item), list_item_data_key, string); } /* here, we are creating another 5 labels, this time * we use gtk_list_item_new_with_label() for the creation * we can憩 query the text string from the label because * we don憩 have the labels pointer and therefore * we just associate the list_item_data_key of each * list item with the same text string * for adding of the list items we put them all into a doubly * linked list (GList), and then add them by a single call to * gtk_list_append_items() * because we use g_list_prepend() to put the items into the * doubly linked list, their order will be descending (instead * of ascending when using g_list_append()) */ dlist=NULL; for (; i<10; i++) { sprintf(buffer, "List Item with Label %d", i); list_item=gtk_list_item_new_with_label(buffer); dlist=g_list_prepend(dlist, list_item); gtk_widget_show(list_item); gtk_object_set_data(GTK_OBJECT(list_item), list_item_data_key, "ListItem with integrated Label"); } gtk_list_append_items(GTK_LIST(gtklist), dlist); /* finaly we want to see the window, don憩 we? ;) */ gtk_widget_show(window); /* fire up the main event loop of gtk */ gtk_main(); /* we get here after gtk_main_quit() has been called which * happens if the main window gets destroyed */ return 0;}/* this is the signal handler that got connected to button * press/release events of the GtkList */voidsigh_button_event (GtkWidget *gtklist, GdkEventButton *event, GtkWidget *frame){ /* we only do something if the third (rightmost mouse button * was released */ if (event->type==GDK_BUTTON_RELEASE && event->button==3) { GList *dlist, *free_list; GtkWidget *new_prisoner; /* fetch the currently selected list item which * will be our next prisoner ;) */ dlist=GTK_LIST(gtklist)->selection; if (dlist) new_prisoner=GTK_WIDGET(dlist->data); else new_prisoner=NULL; /* look for already prisoned list items, we * will put them back into the list * remember to free the doubly linked list that * gtk_container_children() returns */ dlist=gtk_container_children(GTK_CONTAINER(frame)); free_list=dlist; while (dlist) { GtkWidget *list_item; list_item=dlist->data; gtk_widget_reparent(list_item, gtklist); dlist=dlist->next; } g_list_free(free_list); /* if we have a new prisoner, remove him from the * GtkList and put him into the frame "Prison" * we need to unselect the item before */ if (new_prisoner) { GList static_dlist; static_dlist.data=new_prisoner; static_dlist.next=NULL; static_dlist.prev=NULL; gtk_list_unselect_child(GTK_LIST(gtklist), new_prisoner); gtk_widget_reparent(new_prisoner, frame); } }}/* this is the signal handler that gets called if GtkList * emits the "selection_changed" signal */voidsigh_print_selection (GtkWidget *gtklist, gpointer func_data){ GList *dlist; /* fetch the doubly linked list of selected items * of the GtkList, remember to treat this as read-only! */ dlist=GTK_LIST(gtklist)->selection; /* if there are no selected items there is nothing more * to do than just telling the user so */ if (!dlist) { g_print("Selection cleared\n"); return; } /* ok, we got a selection and so we print it */ g_print("The selection is a "); /* get the list item from the doubly linked list * and then query the data associated with list_item_data_key * we then just print it */ while (dlist) { GtkObject *list_item; gchar *item_data_string; list_item=GTK_OBJECT(dlist->data); item_data_string=gtk_object_get_data(list_item, list_item_data_key); g_print("%s ", item_data_string); dlist=dlist->next; } g_print("\n");}</PRE></CODE></BLOCKQUOTE><P><H2><A NAME="ss12.4">12.4 List Item物件</A></H2><P>GtkListItem物件是设计用来做为container的子物件,用来提供selection/deselection的功能.<P>GtkListItem有自己的视窗来接收事件并有其自身的背景颜色, 一般是白色的.<P>因为是由GtkItem而来的, 它也可以用GTK_ITEM(ListItem)巨集.一般GtkListItem只有一个标签, 用来记录例如一个档名.另外还有一个很好用的函数gtk_list_item_new_with_label().若您不想加GtkLabel到GtkListItem,也可以加GtkVBox或GtkArrow.<P><H2><A NAME="ss12.5">12.5 信号</A></H2><P>GtkListItem不产生自己的新的信号,但它继承GtkItem的信号.<P><P><H2><A NAME="ss12.6">12.6 函数</A></H2><P><P><BLOCKQUOTE><CODE><PRE>guint gtk_list_item_get_type (void)</PRE></CODE></BLOCKQUOTE><P>返回`GtkListItem' type identifier.<P><BLOCKQUOTE><CODE><PRE>GtkWidget* gtk_list_item_new (void)</PRE></CODE></BLOCKQUOTE><P>产生新的`GtkListItem' object.新物件返回一个指标给`GtkWidget'物件. `NULL'表示错误.<P><BLOCKQUOTE><CODE><PRE>GtkWidget* gtk_list_item_new_with_label (gchar *LABEL)</PRE></CODE></BLOCKQUOTE><P>产生新的`GtkListItem'物件, 并带一个标签.并返回一个`GtkWidget' object.`NULL'表示错误.<P><BLOCKQUOTE><CODE><PRE>void gtk_list_item_select (GtkListItem *LIST_ITEM)</PRE></CODE></BLOCKQUOTE><P>这个函数基本上是将gtk_item_select (GTK_ITEM (list_item))包装起来.它将会送GtkItem::select信号.*Note GtkItem::, for more info.<P><BLOCKQUOTE><CODE><PRE>void gtk_list_item_deselect (GtkListItem *LIST_ITEM)</PRE></CODE></BLOCKQUOTE><P>这个函数基本上是将gtk_item_deselect (GTK_ITEM (list_item))包装起来.它将会送GtkItem::deselect信号.*Note GtkItem::, for more info.<P><BLOCKQUOTE><CODE><PRE>GtkListItem* GTK_LIST_ITEM (gpointer OBJ)</PRE></CODE></BLOCKQUOTE><P>传一个generic pointer到`GtkListItem*'. *Note Standard Macros::, for more info.<P><BLOCKQUOTE><CODE><PRE>GtkListItemClass* GTK_LIST_ITEM_CLASS (gpointer CLASS)</PRE></CODE></BLOCKQUOTE><P>传一个generic pointer到`GtkListItemClass*'. *Note Standard Macros::, for more info.<P><BLOCKQUOTE><CODE><PRE>gint GTK_IS_LIST_ITEM (gpointer OBJ)</PRE></CODE></BLOCKQUOTE><P>决定generic pointer是否对照到`GtkListItem' object.*Note Standard Macros::, for more info.<P><H2><A NAME="ss12.7">12.7 例子</A></H2><P>Please see the GtkList example on this, which covers the usage of aGtkListItem as well.<P><hr>译注: List物件这一篇本身比较不容易翻译, 因原文本身讲的并不太清楚.此外, 其结构原本就比较繁琐.若您在此糟遇问题, 可来信反应. 译者会想办法将其改善.<br>If you got stuck here, it's mostly not your problem.Don't feel frustration.The List Widget itself is pretty complicated.You may drop me a word if you need. I will try to improve it.<HR><A HREF="gtk_tut-11.html" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-11.html"><IMG SRC="prev.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/prev.gif" ALT="Previous"></A><A HREF="gtk_tut-13.html" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-13.html"><IMG SRC="next.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/next.gif" ALT="Next"></A><A HREF="gtk_tut.html#toc12" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut.html#toc12"><IMG SRC="toc.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/toc.gif" ALT="Contents"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -