⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gtk_tut-4.html

📁 gtk是linux一款强大的夸平台的图形化开发工具
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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: Packing Widgets</TITLE> <LINK HREF="gtk_tut-5.html" REL=next> <LINK HREF="gtk_tut-3.html" REL=previous> <LINK HREF="gtk_tut.html#toc4" REL=contents></HEAD><BODY BGCOLOR="#FFFFFF"><A HREF="gtk_tut-5.html">Next</A><A HREF="gtk_tut-3.html">Previous</A><A HREF="gtk_tut.html#toc4">Contents</A><HR NOSHADE><H2><A NAME="s4">4. Packing Widgets</A></H2><P>When creating an application, you'll want to put more than one widgetinside a window. Our first <EM>helloworld</EM> example only used onewidget so we could simply use a gtk_container_add call to "pack" thewidget into the window. But when you want to put more than one widgetinto a window, how do you control where that widget is positioned?This is where packing comes in.<P><H2><A NAME="ss4.1">4.1 Theory of Packing Boxes</A></H2><P>Most packing is done by creating boxes as in the example above. Theseare invisible widget containers that we can pack our widgets intowhich come in two forms, a horizontal box, and a vertical box. Whenpacking widgets into a horizontal box, the objects are insertedhorizontally from left to right or right to left depending on the callused. In a vertical box, widgets are packed from top to bottom or viceversa. You may use any combination of boxes inside or beside otherboxes to create the desired effect.<P>To create a new horizontal box, we use a call to gtk_hbox_new(), andfor vertical boxes, gtk_vbox_new().The gtk_box_pack_start() andgtk_box_pack_end() functions are used to place objects inside of thesecontainers. The gtk_box_pack_start() function will start at the topand work its way down in a vbox, and pack left to right in an hbox.gtk_box_pack_end() will do the opposite, packing from bottom to top ina vbox, and right to left in an hbox. Using these functions allow usto right justify or left justify our widgets and may be mixed in anyway to achieve the desired effect. We will use gtk_box_pack_start() inmost of our examples. An object may be another container or awidget. In fact, many widgets are actually containers themselves,including the button, but we usually only use a label inside a button.<P>By using these calls, GTK knows where you want to place your widgetsso it can do automatic resizing and other nifty things. There's also anumber of options as to how your widgets should be packed. As you canimagine, this method gives us a quite a bit of flexibility whenplacing and creating widgets.<P><H2><A NAME="ss4.2">4.2 Details of Boxes</A></H2><P>Because of this flexibility, packing boxes in GTK can be confusing atfirst. There are a lot of options, and it's not immediately obvious howthey all fit together. In the end however, there are basically fivedifferent styles.<P> <CENTER ><IMG SRC="gtk_tut_packbox1.gif" VSPACE="15" HSPACE="10" WIDTH="528"HEIGHT="235" ALT="Box Packing Example Image"> </CENTER ><P>Each line contains one horizontal box (hbox) with several buttons. Thecall to gtk_box_pack is shorthand for the call to pack each of thebuttons into the hbox. Each of the buttons is packed into the hbox thesame way (i.e. same arguments to the gtk_box_pack_start() function).<P>This is the declaration of the gtk_box_pack_start function.<P><BLOCKQUOTE><CODE><PRE>void gtk_box_pack_start( GtkBox    *box,                         GtkWidget *child,                         gint       expand,                         gint       fill,                         gint       padding );</PRE></CODE></BLOCKQUOTE><P>The first argument is the box you are packing the object into, thesecond is the object. The objects will all be buttons for now, sowe'll be packing buttons into boxes.<P>The expand argument to gtk_box_pack_start() and gtk_box_pack_end()controls whether the widgets are laid out in the box to fill in allthe extra space in the box so the box is expanded to fill the areaalloted to it (TRUE).  Or the box is shrunk to just fit the widgets(FALSE). Setting expand to FALSE will allow you to do right and leftjustification of your widgets.  Otherwise, they will all expand to fitinto the box, and the same effect could be achieved by using only oneof gtk_box_pack_start or gtk_box_pack_end.<P>The fill argument to the gtk_box_pack functions control whether theextra space is allocated to the objects themselves (TRUE), or as extrapadding in the box around these objects (FALSE). It only has an effectif the expand argument is also TRUE.<P>When creating a new box, the function looks like this:<P><BLOCKQUOTE><CODE><PRE>GtkWidget *gtk_hbox_new (gint homogeneous,                         gint spacing);</PRE></CODE></BLOCKQUOTE><P>The homogeneous argument to gtk_hbox_new (and the same forgtk_vbox_new) controls whether each object in the box has the samesize (i.e. the same width in an hbox, or the same height in avbox). If it is set, the expand argument to the gtk_box_pack routinesis always turned on.<P>What's the difference between spacing (set when the box is created)and padding (set when elements are packed)? Spacing is added betweenobjects, and padding is added on either side of an object. Thefollowing figure should make it clearer:<P> <CENTER ><IMG ALIGN="center" SRC="gtk_tut_packbox2.gif" WIDTH="509"HEIGHT="213" VSPACE="15" HSPACE="10"ALT="Box Packing Example Image"> </CENTER ><P>Here is the code used to create the above images. I've commented itfairly heavily so hopefully you won't have any problems followingit. Compile it yourself and play with it.<P><H2><A NAME="ss4.3">4.3 Packing Demonstration Program</A></H2><P><BLOCKQUOTE><CODE><PRE>/* example-start packbox packbox.c */#include &lt;stdio.h>#include "gtk/gtk.h"void delete_event( GtkWidget *widget,                   GdkEvent  *event,                   gpointer   data ){    gtk_main_quit ();}/* Make a new hbox filled with button-labels. Arguments for the  * variables we're interested are passed in to this function.  * We do not show the box, but do show everything inside. */GtkWidget *make_box( gint homogeneous,                     gint spacing,                     gint expand,                     gint fill,                     gint padding ) {    GtkWidget *box;    GtkWidget *button;    char padstr[80];        /* Create a new hbox with the appropriate homogeneous     * and spacing settings */    box = gtk_hbox_new (homogeneous, spacing);        /* Create a series of buttons with the appropriate settings */    button = gtk_button_new_with_label ("gtk_box_pack");    gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);    gtk_widget_show (button);        button = gtk_button_new_with_label ("(box,");    gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);    gtk_widget_show (button);        button = gtk_button_new_with_label ("button,");    gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);    gtk_widget_show (button);        /* Create a button with the label depending on the value of     * expand. */    if (expand == TRUE)            button = gtk_button_new_with_label ("TRUE,");    else            button = gtk_button_new_with_label ("FALSE,");        gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);    gtk_widget_show (button);        /* This is the same as the button creation for "expand"     * above, but uses the shorthand form. */    button = gtk_button_new_with_label (fill ? "TRUE," : "FALSE,");    gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);    gtk_widget_show (button);        sprintf (padstr, "%d);", padding);        button = gtk_button_new_with_label (padstr);    gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);    gtk_widget_show (button);        return box;}int main( int   argc,          char *argv[]) {    GtkWidget *window;    GtkWidget *button;    GtkWidget *box1;    GtkWidget *box2;    GtkWidget *separator;    GtkWidget *label;    GtkWidget *quitbox;    int which;        /* Our init, don't forget this! :) */    gtk_init (&amp;argc, &amp;argv);        if (argc != 2) {        fprintf (stderr, "usage: packbox num, where num is 1, 2, or 3.\n");        /* This just does cleanup in GTK and exits with an exit status of 1. */        gtk_exit (1);    }        which = atoi (argv[1]);    /* Create our window */    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);    /* You should always remember to connect the destroy signal to the     * main window. This is very important for proper intuitive     * behavior */    gtk_signal_connect (GTK_OBJECT (window), "delete_event",                        GTK_SIGNAL_FUNC (delete_event), NULL);    gtk_container_set_border_width (GTK_CONTAINER (window), 10);        /* We create a vertical box (vbox) to pack the horizontal boxes into.     * This allows us to stack the horizontal boxes filled with buttons one     * on top of the other in this vbox. */    box1 = gtk_vbox_new (FALSE, 0);        /* which example to show. These correspond to the pictures above. */    switch (which) {    case 1:        /* create a new label. */        label = gtk_label_new ("gtk_hbox_new (FALSE, 0);");                /* Align the label to the left side.  We'll discuss this function and          * others in the section on Widget Attributes. */        gtk_misc_set_alignment (GTK_MISC (label), 0, 0);        /* Pack the label into the vertical box (vbox box1).  Remember that          * widgets added to a vbox will be packed one on top of the other in         * order. */        gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);                /* Show the label */        gtk_widget_show (label);                /* Call our make box function - homogeneous = FALSE, spacing = 0,         * expand = FALSE, fill = FALSE, padding = 0 */        box2 = make_box (FALSE, 0, FALSE, FALSE, 0);        gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);        gtk_widget_show (box2);        /* Call our make box function - homogeneous = FALSE, spacing = 0,         * expand = FALSE, fill = FALSE, padding = 0 */        box2 = make_box (FALSE, 0, TRUE, FALSE, 0);        gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);        gtk_widget_show (box2);                /* Args are: homogeneous, spacing, expand, fill, padding */        box2 = make_box (FALSE, 0, TRUE, TRUE, 0);        gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);        gtk_widget_show (box2);                /* Creates a separator, we'll learn more about these later,          * but they are quite simple. */        separator = gtk_hseparator_new ();                /* Cack the separator into the vbox. Remember each of these         * widgets are being packed into a vbox, so they'll be stacked         * vertically. */        gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);        gtk_widget_show (separator);                /* Create another new label, and show it. */        label = gtk_label_new ("gtk_hbox_new (TRUE, 0);");        gtk_misc_set_alignment (GTK_MISC (label), 0, 0);        gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);        gtk_widget_show (label);                /* Args are: homogeneous, spacing, expand, fill, padding */        box2 = make_box (TRUE, 0, TRUE, FALSE, 0);        gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);        gtk_widget_show (box2);                /* Args are: homogeneous, spacing, expand, fill, padding */        box2 = make_box (TRUE, 0, TRUE, TRUE, 0);        gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);        gtk_widget_show (box2);                /* Another new separator. */        separator = gtk_hseparator_new ();        /* The last 3 arguments to gtk_box_pack_start are:         * expand, fill, padding. */        gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);        gtk_widget_show (separator);                break;    case 2:        /* Create a new label, remember box1 is a vbox as created          * near the beginning of main() */        label = gtk_label_new ("gtk_hbox_new (FALSE, 10);");        gtk_misc_set_alignment (GTK_MISC (label), 0, 0);        gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);        gtk_widget_show (label);                /* Args are: homogeneous, spacing, expand, fill, padding */        box2 = make_box (FALSE, 10, TRUE, FALSE, 0);        gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);        gtk_widget_show (box2);                /* Args are: homogeneous, spacing, expand, fill, padding */        box2 = make_box (FALSE, 10, TRUE, TRUE, 0);        gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);        gtk_widget_show (box2);                separator = gtk_hseparator_new ();        /* The last 3 arguments to gtk_box_pack_start are:         * expand, fill, padding. */        gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);        gtk_widget_show (separator);                label = gtk_label_new ("gtk_hbox_new (FALSE, 0);");        gtk_misc_set_alignment (GTK_MISC (label), 0, 0);        gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);        gtk_widget_show (label);                /* Args are: homogeneous, spacing, expand, fill, padding */        box2 = make_box (FALSE, 0, TRUE, FALSE, 10);        gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);        gtk_widget_show (box2);                /* Args are: homogeneous, spacing, expand, fill, padding */        box2 = make_box (FALSE, 0, TRUE, TRUE, 10);        gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);        gtk_widget_show (box2);                separator = gtk_hseparator_new ();        /* The last 3 arguments to gtk_box_pack_start are: expand, fill, padding. */

⌨️ 快捷键说明

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