Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

apress foundations_of gtk plus development 2007 phần 2 pdf
Nội dung xem thử
Mô tả chi tiết
CHAPTER 3 ■ CONTAINER WIDGETS 53
You can easily set the exact position of the resize bar with gtk_paned_set_position(). The
position is calculated in pixels with respect to the top or left side of the container. If you set the
position of the bar to zero, it will be moved all the way to the top or left if the widget allows
shrinking.
void gtk_paned_set_position (GtkPaned *paned,
gint position);
Most applications will want to remember the position of the resize bar, so it can be
restored to the same location when the user next loads the application. The current position
of the resize bar can be retrieved with gtk_paned_get_position().
gint gtk_paned_get_position (GtkPaned *paned);
GtkPaned provides multiple signals, but one of the most useful is move-handle, which will
tell you when the resizing bar has been moved. If you want to remember the position of the
resize bar, this will tell you when you need to retrieve a new value. A full list of GtkPaned signals
can be found in Appendix B.
Tables
So far, all of the layout container widgets I have covered only allow children to be packed in one
dimension. The GtkTable widget, however, allows you to pack children in two-dimensional space.
One advantage of using the GtkTable widget over using multiple GtkHBox and GtkVBox widgets is that children in adjacent rows and columns are automatically aligned with each other,
which is not the case with boxes within boxes. However, this is also a disadvantage, because
you will not always want everything to be lined up in this way.
Figure 3-4 shows a simple table that contains three widgets. Notice that the single label
spans two columns. This illustrates the fact that tables allow one widget to span multiple columns and/or rows as long as the region is rectangular.
Figure 3-4. A table containing a label widget that spans multiple columns
FALSE TRUE The widget will not resize itself to take up additional space available in the
pane, but the user will be able to make it smaller than its size requisition.
FALSE FALSE The widget will not resize itself to take up additional space available in the
pane, and the available space must be greater than or equal to the widget’s size
requisition.
resize shrink Result
7931ch03.fm Page 53 Wednesday, March 7, 2007 8:54 PM
54 CHAPTER 3 ■ CONTAINER WIDGETS
Listing 3-4 creates the GtkTable widget shown in Figure 3-4, inserting two GtkLabel widgets and a GtkEntry widget into the two-by-two area (you will learn how to use the GtkEntry
widget in Chapter 4, but this gives you a taste of what is to come).
Listing 3-4. GtkTable Displaying Name (tables.c)
#include <gtk/gtk.h>
int main (int argc,
char *argv[])
{
GtkWidget *window, *table, *label, *label2, *name;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Tables");
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_widget_set_size_request (window, 150, 100);
table = gtk_table_new (2, 2, TRUE);
label = gtk_label_new ("Enter the following information ...");
label2 = gtk_label_new ("Name: ");
name = gtk_entry_new ();
/* Attach the two labels and entry widget to their parent container. */
gtk_table_attach (GTK_TABLE (table), label, 0, 2, 0, 1,
GTK_EXPAND, GTK_SHRINK, 0, 0);
gtk_table_attach (GTK_TABLE (table), label2, 0, 1, 1, 2,
GTK_EXPAND, GTK_SHRINK, 0, 0);
gtk_table_attach (GTK_TABLE (table), name, 1, 2, 1, 2,
GTK_EXPAND, GTK_SHRINK, 0, 0);
/* Add five pixels of spacing between every row and every column. */
gtk_table_set_row_spacings (GTK_TABLE (table), 5);
gtk_table_set_col_spacings (GTK_TABLE (table), 5);
gtk_container_add (GTK_CONTAINER (window), table);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
7931ch03.fm Page 54 Wednesday, March 7, 2007 8:54 PM
CHAPTER 3 ■ CONTAINER WIDGETS 55
Table Packing
When creating a table with gtk_table_new(), you must specify the number of columns, the
number of rows, and whether table cells should be homogeneous.
GtkWidget* gtk_table_new (guint rows,
guint columns,
gboolean homogeneous);
The number of columns and rows can be changed after creating the table with
gtk_table_resize(), but you should use the correct numbers initially, if possible, to avoid
confusion on the part of the user. You do not want to get in the habit of liberally changing
user interfaces when it is not completely necessary.
void gtk_table_resize (GtkTable *table,
guint rows,
guint columns);
The function gtk_table_set_homogeneous() can also be used to reset the homogeneous
property after creation, but you should use the desired value initially here as well. The user
should have control of resizing after the initial user interface is set.
void gtk_table_set_homogeneous (GtkTable *table,
gboolean homogeneous);
Packing a new widget is performed with gtk_table_attach(). The second parameter,
child, refers to the child widget that you are adding to the table.
void gtk_table_attach (GtkTable *table,
GtkWidget *child,
guint left,
guint right,
guint top,
guint bottom,
GtkAttachOptions xoptions,
GtkAttachOptions yoptions,
guint xpadding,
guint ypadding);
The left, right, top, and bottom variables describe the location where the child widget
should be placed within the table. For example, the first GtkLabel in Listing 3-4 was attached
with the following command:
gtk_table_attach (GTK_TABLE (table), label, 0, 2, 0, 1,
GTK_EXPAND, GTK_SHRINK, 0, 0);
7931ch03.fm Page 55 Wednesday, March 7, 2007 8:54 PM
56 CHAPTER 3 ■ CONTAINER WIDGETS
The GtkLabel widget is attached directly to the first column and row of the table, because
x coordinates are added, followed by y coordinates. It is then attached to the second row on the
bottom and the third column on the right. The packing from the example in Listing 3-4 is
shown in Figure 3-5.
Figure 3-5. Table packing
If you choose to have two columns, there will be three zero-indexed column attach points
labeled. The same logic applies to row attach points if there are two columns.
As previously stated, if a widget spans multiple cells, it must take up a rectangular area. A
widget could span two rows and one column with (0,1,0,2) or the whole table with (0,2,0,2).
The best way to remember the order in which the attach points are specified is that both x coordinates come first, followed by the y coordinates. After specifying attach points, you need to
give attach options for the horizontal and vertical directions. In our example, children are set
to expand in the x direction and shrink in the y direction. There are three values in the
GtkAttachOptions enumeration:
• GTK_EXPAND: The widget should take up extra space allocated to it by the table. This space
is allocated evenly between all children that specify this option.
• GTK_SHRINK: The widget should shrink so that it will only take up enough space to be rendered. This is often used so that extra space is taken up by other widgets.
• GTK_FILL: The widget should fill all allocated space instead of filling the extra space with
padding.
It is possible to give multiple attach option parameters by using a bitwise or operator. For
example, you can use GTK_EXPAND | GTK_FILL, so the child will take up extra space and fill it
instead of adding padding.
7931ch03.fm Page 56 Wednesday, March 7, 2007 8:54 PM