📄 sec-treeitemwidget.html
字号:
<HTML><HEAD><TITLE>Tree Item Widget</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 Widget Internals"HREF="sec-treewidgetinternals.html"><LINKREL="NEXT"TITLE="Tree Example"HREF="sec-treeexample.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-treewidgetinternals.html"><<< Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 13. Tree Widget</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sec-treeexample.html">Next >>></A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-TREEITEMWIDGET">13.5. Tree Item Widget</A></H1><P>The TreeItem widget, like CListItem, is derived from Item,which in turn is derived from Bin. Therefore, the item itself is ageneric container holding exactly one child widget, which can be ofany type. The TreeItem widget has a number of extra fields, butthe only one we need be concerned with is the <TTCLASS="LITERAL">subtree</TT> field.</P><P>The definition for the TreeItem struct looks like this:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">struct _GtkTreeItem{ GtkItem item; GtkWidget *subtree; GtkWidget *pixmaps_box; GtkWidget *plus_pix_widget, *minus_pix_widget; GList *pixmaps; /* pixmap node for this items color depth */ guint expanded : 1;};</PRE></TD></TR></TABLE><P>The <TTCLASS="LITERAL">pixmaps_box</TT> field is an EventBox which catches clicks onthe plus/minus symbol which controls expansion and collapsing. The<TTCLASS="LITERAL">pixmaps</TT> field points to an internal data structure. Sinceyou can always obtain the subtree of a TreeItem in a (relatively)type-safe manner with the <TTCLASS="LITERAL">GTK_TREE_ITEM_SUBTREE (Item)</TT> macro,it's probably advisable never to touch the insides of a TreeItemunless you <ICLASS="EMPHASIS">really</I> know what you're doing.</P><P>Since it is directly derived from an Item it can be treated as such byusing the <TTCLASS="LITERAL">GTK_ITEM (TreeItem)</TT> macro. A TreeItem usually holds alabel, so the convenience function gtk_list_item_new_with_label() isprovided. The same effect can be achieved using code like thefollowing, which is actually copied verbatim fromgtk_tree_item_new_with_label():</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">tree_item = gtk_tree_item_new ();label_widget = gtk_label_new (label);gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);gtk_container_add (GTK_CONTAINER (tree_item), label_widget);gtk_widget_show (label_widget);</PRE></TD></TR></TABLE><P>As one is not forced to add a Label to a TreeItem, you couldalso add an HBox or an Arrow, or even a Notebook (though yourapp will likely be quite unpopular in this case) to the TreeItem.</P><P>If you remove all the items from a subtree, it will be destroyed andunparented, unless you reference it beforehand, and the TreeItemwhich owns it will be collapsed. So, if you want it to stick around,do something like the following:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">gtk_widget_ref (tree);owner = GTK_TREE(tree)->tree_owner;gtk_container_remove (GTK_CONTAINER(tree), item);if (tree->parent == NULL){ gtk_tree_item_expand (GTK_TREE_ITEM(owner)); gtk_tree_item_set_subtree (GTK_TREE_ITEM(owner), tree);}else gtk_widget_unref (tree);</PRE></TD></TR></TABLE><P>Finally, drag-n-drop <ICLASS="EMPHASIS">does</I> work with TreeItems. You justhave to make sure that the TreeItem you want to make into a dragitem or a drop site has not only been added to a Tree, but thateach successive parent widget has a parent itself, all the way back toa toplevel or dialog window, when you call gtk_widget_dnd_drag_set()or gtk_widget_dnd_drop_set(). Otherwise, strange things will happen.</P><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN1847">13.5.1. Signals</A></H2><P>TreeItem inherits the "select", "deselect", and "toggle" signalsfrom Item. In addition, it adds two signals of its own, "expand"and "collapse".</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void select( GtkItem *tree_item );</PRE></TD></TR></TABLE><P>This signal is emitted when an item is about to be selected, eitherafter it has been clicked on by the user, or when the program callsgtk_tree_item_select(), gtk_item_select(), or gtk_tree_select_child().</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void deselect( GtkItem *tree_item );</PRE></TD></TR></TABLE><P>This signal is emitted when an item is about to be unselected, eitherafter it has been clicked on by the user, or when the program callsgtk_tree_item_deselect() or gtk_item_deselect(). In the case ofTreeItems, it is also emitted by gtk_tree_unselect_child(), andsometimes gtk_tree_select_child().</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void toggle( GtkItem *tree_item );</PRE></TD></TR></TABLE><P>This signal is emitted when the program calls gtk_item_toggle(). Theeffect it has when emitted on a TreeItem is to callgtk_tree_select_child() (and never gtk_tree_unselect_child()) on theitem's parent tree, if the item has a parent tree. If it doesn't,then the highlight is reversed on the item.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void expand( GtkTreeItem *tree_item );</PRE></TD></TR></TABLE><P>This signal is emitted when the tree item's subtree is about to beexpanded, that is, when the user clicks on the plus sign next to theitem, or when the program calls gtk_tree_item_expand().</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void collapse( GtkTreeItem *tree_item );</PRE></TD></TR></TABLE><P>This signal is emitted when the tree item's subtree is about to becollapsed, that is, when the user clicks on the minus sign next to theitem, or when the program calls gtk_tree_item_collapse().</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN1860">13.5.2. Functions and Macros</A></H2><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">guint gtk_tree_item_get_type( void );</PRE></TD></TR></TABLE><P>Returns the "GtkTreeItem" type identifier.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkWidget* gtk_tree_item_new( void );</PRE></TD></TR></TABLE><P>Create a new TreeItem object. The new widget is returned as apointer to a GtkWidget object. NULL is returned on failure.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkWidget* gtk_tree_item_new_with_label (gchar *label);</PRE></TD></TR></TABLE><P>Create a new TreeItem object, having a single GtkLabel as the solechild. The new widget is returned as a pointer to a GtkWidgetobject. NULL is returned on failure.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_tree_item_select( GtkTreeItem *tree_item );</PRE></TD></TR></TABLE><P>This function is basically a wrapper around a call to<TTCLASS="LITERAL">gtk_item_select (GTK_ITEM (tree_item))</TT> which will emit theselect signal.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_tree_item_deselect( GtkTreeItem *tree_item );</PRE></TD></TR></TABLE><P>This function is basically a wrapper around a call togtk_item_deselect (GTK_ITEM (tree_item)) which will emit the deselectsignal.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_tree_item_set_subtree( GtkTreeItem *tree_item, GtkWidget *subtree );</PRE></TD></TR></TABLE><P>This function adds a subtree to tree_item, showing it if tree_item isexpanded, or hiding it if tree_item is collapsed. Again, remember thatthe tree_item must have already been added to a tree for this to work.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_tree_item_remove_subtree( GtkTreeItem *tree_item );</PRE></TD></TR></TABLE><P>This removes all of tree_item's subtree's children (thus unreferencingand destroying it, any of its children's subtrees, and so on...), thenremoves the subtree itself, and hides the plus/minus sign.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_tree_item_expand( GtkTreeItem *tree_item );</PRE></TD></TR></TABLE><P>This emits the "expand" signal on tree_item, which expands it.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_tree_item_collapse( GtkTreeItem *tree_item );</PRE></TD></TR></TABLE><P>This emits the "collapse" signal on tree_item, which collapses it.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkTreeItem *GTK_TREE_ITEM (gpointer obj)</PRE></TD></TR></TABLE><P>Cast a generic pointer to "GtkTreeItem *".</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkTreeItemClass *GTK_TREE_ITEM_CLASS (gpointer obj)</PRE></TD></TR></TABLE><P>Cast a generic pointer to "GtkTreeItemClass".</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">gint GTK_IS_TREE_ITEM (gpointer obj)</PRE></TD></TR></TABLE><P>Determine if a generic pointer refers to a "GtkTreeItem" object.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkWidget GTK_TREE_ITEM_SUBTREE (gpointer obj)</PRE></TD></TR></TABLE><P>Returns a tree item's subtree (<TTCLASS="LITERAL">obj</TT> should point to a"GtkTreeItem" object).</P></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="sec-treewidgetinternals.html"><<< Previous</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gtk-tut.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sec-treeexample.html">Next >>></A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Tree Widget Internals</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ch-treewidget.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Tree Example</TD></TR></TABLE></DIV> </td> </tr></table> </td> </tr></table></body></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -