?? gtk_tut-11.html
字號:
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.7">
<TITLE>GTK v1.2 Tutorial: CList Widget</TITLE>
<LINK HREF="gtk_tut-12.html" REL=next>
<LINK HREF="gtk_tut-10.html" REL=previous>
<LINK HREF="gtk_tut.html#toc11" REL=contents>
</HEAD>
<BODY TEXT="#CCCCCC" BGCOLOR="#000000" LINK="#33cc00" VLINK="#009900" ALINK="#FF0000">
<A HREF="gtk_tut-12.html">Next</A>
<A HREF="gtk_tut-10.html">Previous</A>
<A HREF="gtk_tut.html#toc11">Contents</A>
<HR>
<H2><A NAME="s11">11. CList Widget</A></H2>
<P>The CList widget has replaced the List widget (which is still
available).
<P>The CList widget is a multi-column list widget that is capable of
handling literally thousands of rows of information. Each column can
optionally have a title, which itself is optionally active, allowing
us to bind a function to its selection.
<P>
<H2><A NAME="ss11.1">11.1 Creating a CList widget</A>
</H2>
<P>Creating a CList is quite straightforward, once you have learned
about widgets in general. It provides the almost standard two ways,
that is the hard way, and the easy way. But before we create it, there
is one thing we should figure out beforehand: how many columns should
it have?
<P>Not all columns have to be visible and can be used to store data that
is related to a certain cell in the list.
<P>
<BLOCKQUOTE><CODE>
<PRE>
GtkWidget *gtk_clist_new ( gint columns );
GtkWidget *gtk_clist_new_with_titles( gint columns,
gchar *titles[] );
</PRE>
</CODE></BLOCKQUOTE>
<P>The first form is very straightforward, the second might require some
explanation. Each column can have a title associated with it, and this
title can be a label or a button that reacts when we click on it. If
we use the second form, we must provide pointers to the title texts,
and the number of pointers should equal the number of columns
specified. Of course we can always use the first form, and manually
add titles later.
<P>Note: The CList widget does not have its own scrollbars and should
be placed within a ScrolledWindow widget if your require this
functionality. This is a change from the GTK 1.0 implementation.
<P>
<H2><A NAME="ss11.2">11.2 Modes of operation</A>
</H2>
<P>There are several attributes that can be used to alter the behaviour of
a CList. First there is
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_set_selection_mode( GtkCList *clist,
GtkSelectionMode mode );
</PRE>
</CODE></BLOCKQUOTE>
<P>which, as the name implies, sets the selection mode of the
CList. The first argument is the CList widget, and the second
specifies the cell selection mode (they are defined in gtkenums.h). At
the time of this writing, the following modes are available to us:
<P>
<UL>
<LI> <CODE>GTK_SELECTION_SINGLE</CODE> - The selection is either NULL or contains
a GList pointer for a single selected item.
</LI>
<LI> <CODE>GTK_SELECTION_BROWSE</CODE> - The selection is NULL if the list
contains no widgets or insensitive ones only, otherwise it contains a
GList pointer for one GList structure, and therefore exactly one list
item.
</LI>
<LI> <CODE>GTK_SELECTION_MULTIPLE</CODE> - The selection is NULL if no list items
are selected or a GList pointer for the first selected item. That in
turn points to a GList structure for the second selected item and so
on. This is currently the <B>default</B> for the CList widget.
</LI>
<LI> <CODE>GTK_SELECTION_EXTENDED</CODE> - The selection is always NULL.</LI>
</UL>
<P>Others might be added in later revisions of GTK.
<P>We can also define what the border of the CList widget should look
like. It is done through
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_set_shadow_type( GtkCList *clist,
GtkShadowType border );
</PRE>
</CODE></BLOCKQUOTE>
<P>The possible values for the second argument are
<P>
<BLOCKQUOTE><CODE>
<PRE>
GTK_SHADOW_NONE
GTK_SHADOW_IN
GTK_SHADOW_OUT
GTK_SHADOW_ETCHED_IN
GTK_SHADOW_ETCHED_OUT
</PRE>
</CODE></BLOCKQUOTE>
<P>
<H2><A NAME="ss11.3">11.3 Working with titles</A>
</H2>
<P>When you create a CList widget, you will also get a set of title
buttons automatically. They live in the top of the CList window, and
can act either as normal buttons that respond to being pressed, or
they can be passive, in which case they are nothing more than a
title. There are four different calls that aid us in setting the
status of the title buttons.
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_column_title_active( GtkCList *clist,
gint column );
void gtk_clist_column_title_passive( GtkCList *clist,
gint column );
void gtk_clist_column_titles_active( GtkCList *clist );
void gtk_clist_column_titles_passive( GtkCList *clist );
</PRE>
</CODE></BLOCKQUOTE>
<P>An active title is one which acts as a normal button, a passive one is
just a label. The first two calls above will activate/deactivate the
title button above the specific column, while the last two calls
activate/deactivate all title buttons in the supplied clist widget.
<P>But of course there are those cases when we don't want them at all,
and so they can be hidden and shown at will using the following two
calls.
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_column_titles_show( GtkCList *clist );
void gtk_clist_column_titles_hide( GtkCList *clist );
</PRE>
</CODE></BLOCKQUOTE>
<P>For titles to be really useful we need a mechanism to set and change
them, and this is done using
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_set_column_title( GtkCList *clist,
gint column,
gchar *title );
</PRE>
</CODE></BLOCKQUOTE>
<P>Note that only the title of one column can be set at a time, so if all
the titles are known from the beginning, then I really suggest using
gtk_clist_new_with_titles (as described above) to set them. It saves
you coding time, and makes your program smaller. There are some cases
where getting the job done the manual way is better, and that's when
not all titles will be text. CList provides us with title buttons
that can in fact incorporate whole widgets, for example a pixmap. It's
all done through
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_set_column_widget( GtkCList *clist,
gint column,
GtkWidget *widget );
</PRE>
</CODE></BLOCKQUOTE>
<P>which should require no special explanation.
<P>
<H2><A NAME="ss11.4">11.4 Manipulating the list itself</A>
</H2>
<P>It is possible to change the justification for a column, and it is
done through
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_set_column_justification( GtkCList *clist,
gint column,
GtkJustification justification );
</PRE>
</CODE></BLOCKQUOTE>
<P>The GtkJustification type can take the following values:
<P>
<UL>
<LI><CODE>GTK_JUSTIFY_LEFT</CODE> - The text in the column will begin from the
left edge.
</LI>
<LI><CODE>GTK_JUSTIFY_RIGHT</CODE> - The text in the column will begin from the
right edge.
</LI>
<LI><CODE>GTK_JUSTIFY_CENTER</CODE> - The text is placed in the center of the
column.
</LI>
<LI><CODE>GTK_JUSTIFY_FILL</CODE> - The text will use up all available space in
the column. It is normally done by inserting extra blank spaces
between words (or between individual letters if it's a single
word). Much in the same way as any ordinary WYSIWYG text editor.</LI>
</UL>
<P>The next function is a very important one, and should be standard in
the setup of all CList widgets. When the list is created, the width
of the various columns are chosen to match their titles, and since
this is seldom the right width we have to set it using
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_set_column_width( GtkCList *clist,
gint column,
gint width );
</PRE>
</CODE></BLOCKQUOTE>
<P>Note that the width is given in pixels and not letters. The same goes
for the height of the cells in the columns, but as the default value
is the height of the current font this isn't as critical to the
application. Still, it is done through
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_set_row_height( GtkCList *clist,
gint height );
</PRE>
</CODE></BLOCKQUOTE>
<P>Again, note that the height is given in pixels.
<P>We can also move the list around without user interaction, however, it
does require that we know what we are looking for. Or in other words,
we need the row and column of the item we want to scroll to.
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_moveto( GtkCList *clist,
gint row,
gint column,
gfloat row_align,
gfloat col_align );
</PRE>
</CODE></BLOCKQUOTE>
<P>The gfloat row_align is pretty important to understand. It's a value
between 0.0 and 1.0, where 0.0 means that we should scroll the list so
the row appears at the top, while if the value of row_align is 1.0,
the row will appear at the bottom instead. All other values between
0.0 and 1.0 are also valid and will place the row between the top and
the bottom. The last argument, gfloat col_align works in the same way,
though 0.0 marks left and 1.0 marks right instead.
<P>Depending on the application's needs, we don't have to scroll to an
item that is already visible to us. So how do we know if it is
visible? As usual, there is a function to find that out as well.
<P>
<BLOCKQUOTE><CODE>
<PRE>
GtkVisibility gtk_clist_row_is_visible( GtkCList *clist,
gint row );
</PRE>
</CODE></BLOCKQUOTE>
<P>The return value is is one of the following:
<P>
<BLOCKQUOTE><CODE>
<PRE>
GTK_VISIBILITY_NONE
GTK_VISIBILITY_PARTIAL
GTK_VISIBILITY_FULL
</PRE>
</CODE></BLOCKQUOTE>
<P>Note that it will only tell us if a row is visible. Currently there is
no way to determine this for a column. We can get partial information
though, because if the return is <CODE>GTK_VISIBILITY_PARTIAL</CODE>, then
some of it is hidden, but we don't know if it is the row that is being
cut by the lower edge of the listbox, or if the row has columns that
are outside.
<P>We can also change both the foreground and background colors of a
particular row. This is useful for marking the row selected by the
user, and the two functions that is used to do it are
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_set_foreground( GtkCList *clist,
gint row,
GdkColor *color );
void gtk_clist_set_background( GtkCList *clist,
gint row,
GdkColor *color );
</PRE>
</CODE></BLOCKQUOTE>
<P>Please note that the colors must have been previously allocated.
<P>
<H2><A NAME="ss11.5">11.5 Adding rows to the list</A>
</H2>
<P>We can add rows in three ways. They can be prepended or appended to
the list using
<P>
<BLOCKQUOTE><CODE>
<PRE>
gint gtk_clist_prepend( GtkCList *clist,
gchar *text[] );
gint gtk_clist_append( GtkCList *clist,
gchar *text[] );
</PRE>
</CODE></BLOCKQUOTE>
<P>The return value of these two functions indicate the index of the row
that was just added. We can insert a row at a given place using
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_insert( GtkCList *clist,
gint row,
gchar *text[] );
</PRE>
</CODE></BLOCKQUOTE>
<P>In these calls we have to provide a collection of pointers that are
the texts we want to put in the columns. The number of pointers should
equal the number of columns in the list. If the text[] argument is
NULL, then there will be no text in the columns of the row. This is
useful, for example, if we want to add pixmaps instead (something that
has to be done manually).
<P>Also, please note that the numbering of both rows and columns start at 0.
<P>To remove an individual row we use
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_remove( GtkCList *clist,
gint row );
</PRE>
</CODE></BLOCKQUOTE>
<P>There is also a call that removes all rows in the list. This is a lot
faster than calling gtk_clist_remove once for each row, which is the
only alternative.
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_clear( GtkCList *clist );
</PRE>
</CODE></BLOCKQUOTE>
<P>There are also two convenience functions that should be used when a
lot of changes have to be made to the list. This is to prevent the
list flickering while being repeatedly updated, which may be highly
annoying to the user. So instead it is a good idea to freeze the list,
do the updates to it, and finally thaw it which causes the list to be
updated on the screen.
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_freeze( GtkCList * clist );
void gtk_clist_thaw( GtkCList * clist );
</PRE>
</CODE></BLOCKQUOTE>
<P>
<H2><A NAME="ss11.6">11.6 Setting text and pixmaps in the cells</A>
</H2>
<P>A cell can contain a pixmap, text or both. To set them the following
functions are used.
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_clist_set_text( GtkCList *clist,
gint row,
gint column,
const gchar *text );
void gtk_clist_set_pixmap( GtkCList *clist,
gint row,
gint column,
GdkPixmap *pixmap,
GdkBitmap *mask );
void gtk_clist_set_pixtext( GtkCList *clist,
gint row,
gint column,
gchar *text,
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -