📄 sec-packingusingtables.html
字号:
<HTML><HEAD><TITLE>Packing Using Tables</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="Packing Widgets"HREF="ch-packingwidgets.html"><LINKREL="PREVIOUS"TITLE="Packing Demonstration Program"HREF="sec-packingdemonstrationprogram.html"><LINKREL="NEXT"TITLE="Table Packing Example"HREF="sec-tablepackingexamples.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-packingdemonstrationprogram.html"><<< Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 4. Packing Widgets</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sec-tablepackingexamples.html">Next >>></A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-PACKINGUSINGTABLES">4.4. Packing Using Tables</A></H1><P>Let's take a look at another way of packing - Tables. These can beextremely useful in certain situations.</P><P>Using tables, we create a grid that we can place widgets in. Thewidgets may take up as many spaces as we specify.</P><P>The first thing to look at, of course, is the gtk_table_new function:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkWidget *gtk_table_new( gint rows, gint columns, gint homogeneous );</PRE></TD></TR></TABLE><P>The first argument is the number of rows to make in the table, whilethe second, obviously, is the number of columns.</P><P>The homogeneous argument has to do with how the table's boxes aresized. If homogeneous is TRUE, the table boxes are resized to the sizeof the largest widget in the table. If homogeneous is FALSE, the sizeof a table boxes is dictated by the tallest widget in its same row,and the widest widget in its column.</P><P>The rows and columns are laid out from 0 to n, where n was the numberspecified in the call to gtk_table_new. So, if you specify rows = 2and columns = 2, the layout would look something like this:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 0 1 20+----------+----------+ | | |1+----------+----------+ | | |2+----------+----------+</PRE></TD></TR></TABLE><P>Note that the coordinate system starts in the upper left hand corner.To place a widget into a box, use the following function:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_table_attach( GtkTable *table, GtkWidget *child, gint left_attach, gint right_attach, gint top_attach, gint bottom_attach, gint xoptions, gint yoptions, gint xpadding, gint ypadding );</PRE></TD></TR></TABLE><P>The first argument ("table") is the table you've created and thesecond ("child") the widget you wish to place in the table.</P><P>The left and right attach arguments specify where to place the widget,and how many boxes to use. If you want a button in the lower righttable entry of our 2x2 table, and want it to fill that entry ONLY,left_attach would be = 1, right_attach = 2, top_attach = 1,bottom_attach = 2.</P><P>Now, if you wanted a widget to take up the whole top row of our 2x2table, you'd use left_attach = 0, right_attach = 2, top_attach = 0,bottom_attach = 1.</P><P>The xoptions and yoptions are used to specify packing options and maybe bitwise OR'ed together to allow multiple options.</P><P>These options are:</P><P></P><UL><LI><P><TTCLASS="LITERAL">GTK_FILL</TT> - If the table box is larger than the widget, and<TTCLASS="LITERAL">GTK_FILL</TT> is specified, the widget will expand to use all the roomavailable.</P></LI><LI><P><TTCLASS="LITERAL">GTK_SHRINK</TT> - If the table widget was allocated less spacethen was requested (usually by the user resizing the window), then thewidgets would normally just be pushed off the bottom of the window anddisappear. If <TTCLASS="LITERAL">GTK_SHRINK</TT> is specified, the widgets will shrinkwith the table.</P></LI><LI><P><TTCLASS="LITERAL">GTK_EXPAND</TT> - This will cause the table to expand to use upany remaining space in the window.</P></LI></UL><P>Padding is just like in boxes, creating a clear area around the widgetspecified in pixels.</P><P>gtk_table_attach() has a LOT of options. So, there's a shortcut:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_table_attach_defaults( GtkTable *table, GtkWidget *widget, gint left_attach, gint right_attach, gint top_attach, gint bottom_attach );</PRE></TD></TR></TABLE><P>The X and Y options default to <TTCLASS="LITERAL">GTK_FILL | GTK_EXPAND</TT>, and X and Ypadding are set to 0. The rest of the arguments are identical to theprevious function.</P><P>We also have gtk_table_set_row_spacing() andgtk_table_set_col_spacing(). These places spacing between the rows atthe specified row or column.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_table_set_row_spacing( GtkTable *table, gint row, gint spacing );</PRE></TD></TR></TABLE><P>and</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_table_set_col_spacing ( GtkTable *table, gint column, gint spacing );</PRE></TD></TR></TABLE><P>Note that for columns, the space goes to the right of the column, andfor rows, the space goes below the row.</P><P>You can also set a consistent spacing of all rows and/or columns with:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_table_set_row_spacings( GtkTable *table, gint spacing );</PRE></TD></TR></TABLE><P>And,</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_table_set_col_spacings( GtkTable *table, gint spacing );</PRE></TD></TR></TABLE><P>Note that with these calls, the last row and last column do not getany spacing.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="sec-packingdemonstrationprogram.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-tablepackingexamples.html">Next >>></A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Packing Demonstration Program</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ch-packingwidgets.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Table Packing Example</TD></TR></TABLE></DIV> </td> </tr></table> </td> </tr></table></body></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -