📄 sec-treeexample.html
字号:
<HTML><HEAD><TITLE>Tree Example</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.49"><LINKREL="HOME"TITLE="GTK+ 1.2 Tutorial"HREF="gtk-tut.html"><LINKREL="UP"TITLE="Tree Widget"HREF="ch-treewidget.html"><LINKREL="PREVIOUS"TITLE="Tree Item Widget"HREF="sec-treeitemwidget.html"><LINKREL="NEXT"TITLE="Menu Widget"HREF="ch-menuwidget.html"></HEAD></head><body bgcolor="#FFFFFF" marginheight=0 marginwidth=0 width="100%"><table cellspacing=6 border=0 cellpadding=0 width="100%"> <tr> <td bgcolor="#FFFFFF" valign=top nowrap> <centeR><A HREF="/"><img src="/images/gtk-logo-rgb.gif" width=107 height=140 border=0></A> <font face="helvetica,lucidia" color="#000000"><BR><BR><b>GTK+<BR>The GIMP Toolkit</b></center><BR><table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>General</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/">Introduction</A><BR><A HREF="/screenshots/">Screenshots</A><br><A HREF="/download/">Download</A><br><A HREF="/mailinglists.html">Mailing Lists</A><BR><A HREF="/bindings.html">Language Bindings</A><BR><A HREF="http://gtk.themes.org/">Themes</A><BR><A HREF="/bugs.html">Bug Tracker</A><BR></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Documentation</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/faq/">FAQ</A><br><A HREF="/tutorial/">Tutorial</A><BR><A HREF="/api/">API Reference</A><br><A HREF="/books.html">Published Books</A><BR></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Projects</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.pango.org/">Pango</A><BR><A HREF="http://sources.redhat.com/inti/">Inti</A><BR><A HREF="http://www.gnome.org/">GNOME</A><BR><A HREF="http://user.sgic.fi/~tml/gimp/win32/">GTK+ for Win32</A><br><A HREF="http://people.redhat.com/sopwith/gtkfb/">GtkFB (Framebuffer)</A><br><A HREF="http://www.directfb.org/gtk.xml">GTK+ on DirectFB</A><BR><A HREF="/beos/">GTK+ for BeOS</A></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B><B>Applications</B></B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.gimp.org/">GIMP</A><BR><A HREF="http://www.abiword.org/">Abiword</A><BR><A HREF="http://www.lysator.liu.se/~alla/dia/dia.html">Dia</A><BR><A HREF="http://glade.pn.org/">Glade</A><BR><A HREF="http://www.gnucash.org/">GnuCash</A><BR><A HREF="http://www.gnome.org/projects/gnumeric/">Gnumeric</A><BR><BR><A HREF="http://www.gnome.org/applist/">GNOME Software Map</A><br></td></tr></table></td></tr></table><BR> </td> <td bgcolor="#ffffff" valign=top width="99%"><font face="lucida,helvetica"><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GTK+ 1.2 Tutorial</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="sec-treeitemwidget.html"><<< Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 13. Tree Widget</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="ch-menuwidget.html">Next >>></A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-TREEEXAMPLE">13.6. Tree Example</A></H1><P>This is somewhat like the tree example in testgtk.c, but a lot lesscomplete (although much better commented). It puts up a window with atree, and connects all the signals for the relevant objects, so youcan see when they are emitted.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">/* example-start tree tree.c */#include <gtk/gtk.h>/* for all the GtkItem:: and GtkTreeItem:: signals */static void cb_itemsignal( GtkWidget *item, gchar *signame ){ gchar *name; GtkLabel *label; /* It's a Bin, so it has one child, which we know to be a label, so get that */ label = GTK_LABEL (GTK_BIN (item)->child); /* Get the text of the label */ gtk_label_get (label, &name); /* Get the level of the tree which the item is in */ g_print ("%s called for item %s->%p, level %d\n", signame, name, item, GTK_TREE (item->parent)->level);}/* Note that this is never called */static void cb_unselect_child( GtkWidget *root_tree, GtkWidget *child, GtkWidget *subtree ){ g_print ("unselect_child called for root tree %p, subtree %p, child %p\n", root_tree, subtree, child);}/* Note that this is called every time the user clicks on an item, whether it is already selected or not. */static void cb_select_child (GtkWidget *root_tree, GtkWidget *child, GtkWidget *subtree){ g_print ("select_child called for root tree %p, subtree %p, child %p\n", root_tree, subtree, child);}static void cb_selection_changed( GtkWidget *tree ){ GList *i; g_print ("selection_change called for tree %p\n", tree); g_print ("selected objects are:\n"); i = GTK_TREE_SELECTION(tree); while (i){ gchar *name; GtkLabel *label; GtkWidget *item; /* Get a GtkWidget pointer from the list node */ item = GTK_WIDGET (i->data); label = GTK_LABEL (GTK_BIN (item)->child); gtk_label_get (label, &name); g_print ("\t%s on level %d\n", name, GTK_TREE (item->parent)->level); i = i->next; }}int main( int argc, char *argv[] ){ GtkWidget *window, *scrolled_win, *tree; static gchar *itemnames[] = {"Foo", "Bar", "Baz", "Quux", "Maurice"}; gint i; gtk_init (&argc, &argv); /* a generic toplevel window */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_signal_connect (GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC (gtk_main_quit), NULL); gtk_container_set_border_width (GTK_CONTAINER(window), 5); /* A generic scrolled window */ scrolled_win = gtk_scrolled_window_new (NULL, NULL); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_widget_set_usize (scrolled_win, 150, 200); gtk_container_add (GTK_CONTAINER(window), scrolled_win); gtk_widget_show (scrolled_win); /* Create the root tree */ tree = gtk_tree_new(); g_print ("root tree is %p\n", tree); /* connect all GtkTree:: signals */ gtk_signal_connect (GTK_OBJECT(tree), "select_child", GTK_SIGNAL_FUNC(cb_select_child), tree); gtk_signal_connect (GTK_OBJECT(tree), "unselect_child", GTK_SIGNAL_FUNC(cb_unselect_child), tree); gtk_signal_connect (GTK_OBJECT(tree), "selection_changed", GTK_SIGNAL_FUNC(cb_selection_changed), tree); /* Add it to the scrolled window */ gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(scrolled_win), tree); /* Set the selection mode */ gtk_tree_set_selection_mode (GTK_TREE(tree), GTK_SELECTION_MULTIPLE); /* Show it */ gtk_widget_show (tree); for (i = 0; i < 5; i++){ GtkWidget *subtree, *item; gint j; /* Create a tree item */ item = gtk_tree_item_new_with_label (itemnames[i]); /* Connect all GtkItem:: and GtkTreeItem:: signals */ gtk_signal_connect (GTK_OBJECT(item), "select", GTK_SIGNAL_FUNC(cb_itemsignal), "select"); gtk_signal_connect (GTK_OBJECT(item), "deselect", GTK_SIGNAL_FUNC(cb_itemsignal), "deselect"); gtk_signal_connect (GTK_OBJECT(item), "toggle", GTK_SIGNAL_FUNC(cb_itemsignal), "toggle"); gtk_signal_connect (GTK_OBJECT(item), "expand", GTK_SIGNAL_FUNC(cb_itemsignal), "expand"); gtk_signal_connect (GTK_OBJECT(item), "collapse", GTK_SIGNAL_FUNC(cb_itemsignal), "collapse"); /* Add it to the parent tree */ gtk_tree_append (GTK_TREE(tree), item); /* Show it - this can be done at any time */ gtk_widget_show (item); /* Create this item's subtree */ subtree = gtk_tree_new(); g_print ("-> item %s->%p, subtree %p\n", itemnames[i], item, subtree); /* This is still necessary if you want these signals to be called for the subtree's children. Note that selection_change will be signalled for the root tree regardless. */ gtk_signal_connect (GTK_OBJECT(subtree), "select_child", GTK_SIGNAL_FUNC(cb_select_child), subtree); gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child", GTK_SIGNAL_FUNC(cb_unselect_child), subtree); /* This has absolutely no effect, because it is completely ignored in subtrees */ gtk_tree_set_selection_mode (GTK_TREE(subtree), GTK_SELECTION_SINGLE); /* Neither does this, but for a rather different reason - the view_mode and view_line values of a tree are propagated to subtrees when they are mapped. So, setting it later on would actually have a (somewhat unpredictable) effect */ gtk_tree_set_view_mode (GTK_TREE(subtree), GTK_TREE_VIEW_ITEM); /* Set this item's subtree - note that you cannot do this until AFTER the item has been added to its parent tree! */ gtk_tree_item_set_subtree (GTK_TREE_ITEM(item), subtree); for (j = 0; j < 5; j++){ GtkWidget *subitem; /* Create a subtree item, in much the same way */ subitem = gtk_tree_item_new_with_label (itemnames[j]); /* Connect all GtkItem:: and GtkTreeItem:: signals */ gtk_signal_connect (GTK_OBJECT(subitem), "select", GTK_SIGNAL_FUNC(cb_itemsignal), "select"); gtk_signal_connect (GTK_OBJECT(subitem), "deselect", GTK_SIGNAL_FUNC(cb_itemsignal), "deselect"); gtk_signal_connect (GTK_OBJECT(subitem), "toggle", GTK_SIGNAL_FUNC(cb_itemsignal), "toggle"); gtk_signal_connect (GTK_OBJECT(subitem), "expand", GTK_SIGNAL_FUNC(cb_itemsignal), "expand"); gtk_signal_connect (GTK_OBJECT(subitem), "collapse", GTK_SIGNAL_FUNC(cb_itemsignal), "collapse"); g_print ("-> -> item %s->%p\n", itemnames[j], subitem); /* Add it to its parent tree */ gtk_tree_append (GTK_TREE(subtree), subitem); /* Show it */ gtk_widget_show (subitem); } } /* Show the window and loop endlessly */ gtk_widget_show (window); gtk_main(); return 0;}/* example-end */</PRE></TD></TR></TABLE></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="sec-treeitemwidget.html"><<< Previous</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gtk-tut.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="ch-menuwidget.html">Next >>></A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Tree Item Widget</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ch-treewidget.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Menu Widget</TD></TR></TABLE></DIV> </td> </tr></table> </td> </tr></table></body></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -