gtk_tut-31.html

来自「gtk1.2的教程」· HTML 代码 · 共 654 行 · 第 1/2 页

HTML
654
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE>GTK v1.2 Tutorial: List Widget</TITLE> <LINK HREF="gtk_tut-30.html" REL=previous> <LINK HREF="gtk_tut.html#toc31" REL=contents></HEAD><BODY TEXT="#CCCCCC" BGCOLOR="#000000" LINK="#33cc00" VLINK="#009900" ALINK="#FF0000">Next<A HREF="gtk_tut-30.html">Previous</A><A HREF="gtk_tut.html#toc31">Contents</A><HR><H2><A NAME="s31">31. List Widget</A></H2><P>NOTE: The List widget has been superseded by the CList widget. It isdetailed here just for completeness.<P>The List widget is designed to act as a vertical container forwidgets that should be of the type ListItem.<P>A List widget has its own window to receive events and its ownbackground color which is usually white. As it is directly derivedfrom a Container it can be treated as such by using theGTK_CONTAINER(List) macro, see the Container widget for more onthis. One should already be familiar with the usage of a GList andits related functions g_list_*() to be able to use the List widgetto it full extent.<P>There is one field inside the structure definition of the Listwidget that will be of greater interest to us, this is:<P><BLOCKQUOTE><CODE><PRE>struct _GtkList{  ...  GList *selection;  guint selection_mode;  ...}; </PRE></CODE></BLOCKQUOTE><P>The selection field of a List points to a linked list of all itemsthat are currently selected, or NULL if the selection is empty.  So tolearn about the current selection we read the GTK_LIST()->selectionfield, but do not modify it since the internal fields are maintainedby the gtk_list_*() functions.<P>The selection_mode of the List determines the selection facilitiesof a List and therefore the contents of the GTK_LIST()->selectionfield. The selection_mode may be one of the following:<P><UL><LI> <CODE>GTK_SELECTION_SINGLE</CODE> - The selection is either NULLor contains a GList pointerfor a single selected item.</LI><LI> <CODE>GTK_SELECTION_BROWSE</CODE> -  The selection is NULL if the listcontains no widgets or insensitiveones only, otherwise it containsa GList pointer for one GListstructure, and therefore exactlyone list item.</LI><LI> <CODE>GTK_SELECTION_MULTIPLE</CODE> -  The selection is NULL if no listitems are selected or a GList pointerfor the first selected item. Thatin turn points to a GList structurefor the second selected item and soon.</LI><LI> <CODE>GTK_SELECTION_EXTENDED</CODE> - The selection is always NULL.</LI></UL><P>The default is <CODE>GTK_SELECTION_MULTIPLE</CODE>.<P><H2><A NAME="ss31.1">31.1 Signals</A></H2><P><BLOCKQUOTE><CODE><PRE>void selection_changed( GtkList *list );</PRE></CODE></BLOCKQUOTE><P>This signal will be invoked whenever the selection field of a Listhas changed. This happens when a child of thekList got selected ordeselected.<P><BLOCKQUOTE><CODE><PRE>void select_child( GtkList   *list,                   GtkWidget *child);</PRE></CODE></BLOCKQUOTE><P>This signal is invoked when a child of the List is about to getselected. This happens mainly on calls to gtk_list_select_item(),gtk_list_select_child(), button presses and sometimes indirectlytriggered on some else occasions where children get added to orremoved from the List.<P><BLOCKQUOTE><CODE><PRE>void unselect_child( GtkList   *list,                     GtkWidget *child );</PRE></CODE></BLOCKQUOTE><P>This signal is invoked when a child of the List is about to getdeselected. This happens mainly on calls to gtk_list_unselect_item(),gtk_list_unselect_child(), button presses and sometimes indirectlytriggered on some else occasions where children get added to orremoved from the List.<P><H2><A NAME="ss31.2">31.2 Functions</A></H2><P><BLOCKQUOTE><CODE><PRE>guint gtk_list_get_type( void );</PRE></CODE></BLOCKQUOTE><P>Returns the "GtkList" type identifier.<P><BLOCKQUOTE><CODE><PRE>GtkWidget *gtk_list_new( void );</PRE></CODE></BLOCKQUOTE><P>Create a new List object. The new widget is returned as a pointerto a GtkWidget object. NULL is returned on failure.<P><BLOCKQUOTE><CODE><PRE>void gtk_list_insert_items( GtkList *list,                            GList   *items,                            gint     position );</PRE></CODE></BLOCKQUOTE><P>Insert list items into the list, starting at <CODE>position</CODE>.<CODE>items</CODE> is a doubly linked list where each nodes data pointer isexpected to point to a newly created ListItem. The GList nodes of<CODE>items</CODE> are taken over by the list.<P><BLOCKQUOTE><CODE><PRE>void gtk_list_append_items( GtkList *list,                            GList   *items);</PRE></CODE></BLOCKQUOTE><P>Insert list items just like gtk_list_insert_items() at the end of thelist. The GList nodes of <CODE>items</CODE> are taken over by the list.<P><BLOCKQUOTE><CODE><PRE>void gtk_list_prepend_items( GtkList *list,                             GList   *items);</PRE></CODE></BLOCKQUOTE><P>Insert list items just like gtk_list_insert_items() at the verybeginning of the list. The GList nodes of <CODE>items</CODE> are taken over bythe list.<P><BLOCKQUOTE><CODE><PRE>void gtk_list_remove_items( GtkList *list,                            GList   *items);</PRE></CODE></BLOCKQUOTE><P>Remove list items from the list. <CODE>items</CODE> is a doubly linked listwhere each nodes data pointer is expected to point to a direct childof list. It is the callers responsibility to make a call tog_list_free(items) afterwards. Also the caller has to destroy the listitems himself.<P><BLOCKQUOTE><CODE><PRE>void gtk_list_clear_items( GtkList *list,                           gint start,                           gint end );</PRE></CODE></BLOCKQUOTE><P>Remove and destroy list items from the list. A widget is affected ifits current position within the list is in the range specified by<CODE>start</CODE> and <CODE>end</CODE>.<P><BLOCKQUOTE><CODE><PRE>void gtk_list_select_item( GtkList *list,                           gint     item );</PRE></CODE></BLOCKQUOTE><P>Invoke the select_child signal for a list item specified through itscurrent position within the list.<P><BLOCKQUOTE><CODE><PRE>void gtk_list_unselect_item( GtkList *list,                             gint     item);</PRE></CODE></BLOCKQUOTE><P>Invoke the unselect_child signal for a list item specified through itscurrent position within the list.<P><BLOCKQUOTE><CODE><PRE>void gtk_list_select_child( GtkList *list,                            GtkWidget *child);</PRE></CODE></BLOCKQUOTE><P>Invoke the select_child signal for the specified child.<P><BLOCKQUOTE><CODE><PRE>void gtk_list_unselect_child( GtkList   *list,                              GtkWidget *child);</PRE></CODE></BLOCKQUOTE><P>Invoke the unselect_child signal for the specified child.<P><BLOCKQUOTE><CODE><PRE>gint gtk_list_child_position( GtkList *list,                              GtkWidget *child);</PRE></CODE></BLOCKQUOTE><P>Return the position of <CODE>child</CODE> within the list. "-1" is returned onfailure.<P><BLOCKQUOTE><CODE><PRE>void gtk_list_set_selection_mode( GtkList         *list,                                  GtkSelectionMode mode );</PRE></CODE></BLOCKQUOTE><P>Set the selection mode MODE which can be of GTK_SELECTION_SINGLE,GTK_SELECTION_BROWSE, GTK_SELECTION_MULTIPLE orGTK_SELECTION_EXTENDED.<P><BLOCKQUOTE><CODE><PRE>GtkList *GTK_LIST( gpointer obj );</PRE></CODE></BLOCKQUOTE><P>Cast a generic pointer to "GtkList *".<P><BLOCKQUOTE><CODE><PRE>GtkListClass *GTK_LIST_CLASS( gpointer class);</PRE></CODE></BLOCKQUOTE><P>Cast a generic pointer to "GtkListClass *". <P><BLOCKQUOTE><CODE><PRE>gint GTK_IS_LIST( gpointer obj);</PRE></CODE></BLOCKQUOTE><P>Determine if a generic pointer refers to a "GtkList" object.<P><H2><A NAME="ss31.3">31.3 Example</A></H2><P>Following is an example program that will print out the changes of theselection of a List, and lets you "arrest" list items into a prisonby selecting them with the rightmost mouse button.<P><BLOCKQUOTE><CODE><PRE>/* example-start list list.c *//* Include the GTK header files * Include stdio.h, we need that for the printf() function */#include        &lt;gtk/gtk.h>#include        &lt;stdio.h>/* This is our data identification string to store * data in list items */const gchar *list_item_data_key="list_item_data";/* prototypes for signal handler that we are going to connect * to the List widget */static void  sigh_print_selection( GtkWidget *gtklist,                                   gpointer   func_data);static void  sigh_button_event( GtkWidget      *gtklist,                                GdkEventButton *event,                                GtkWidget      *frame );/* Main function to set up the user interface */gint main( int    argc,           gchar *argv[] ){                                      GtkWidget *separator;    GtkWidget *window;    GtkWidget *vbox;    GtkWidget *scrolled_window;    GtkWidget *frame;    GtkWidget *gtklist;    GtkWidget *button;    GtkWidget *list_item;    GList *dlist;    guint i;    gchar buffer[64];            /* Initialize GTK (and subsequently GDK) */    gtk_init(&amp;argc, &amp;argv);            /* Create a window to put all the widgets in     * connect gtk_main_quit() to the "destroy" event of     * the window to handle window manager close-window-events     */    window=gtk_window_new(GTK_WINDOW_TOPLEVEL);    gtk_window_set_title(GTK_WINDOW(window), "GtkList Example");    gtk_signal_connect(GTK_OBJECT(window),                       "destroy",                       GTK_SIGNAL_FUNC(gtk_main_quit),                       NULL);            /* Inside the window we need a box to arrange the widgets     * vertically */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?