gtk_tut.sgml

来自「gtk1.2的教程」· SGML 代码 · 共 1,828 行 · 第 1/5 页

SGML
1,828
字号
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.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.The xoptions and yoptions are used to specify packing options and maybe bitwise OR'ed together to allow multiple options.These options are:<itemize><item><tt/GTK_FILL/ - If the table box is larger than the widget, and<tt/GTK_FILL/ is specified, the widget will expand to use all the roomavailable.<item><tt/GTK_SHRINK/ - 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 <tt/GTK_SHRINK/ is specified, the widgets will shrinkwith the table.<item><tt/GTK_EXPAND/ - This will cause the table to expand to use upany remaining space in the window.</itemize>Padding is just like in boxes, creating a clear area around the widgetspecified in pixels.gtk_table_attach() has a LOT of options.  So, there's a shortcut:<tscreen><verb>void gtk_table_attach_defaults( GtkTable  *table,                                GtkWidget *widget,                                gint       left_attach,                                gint       right_attach,                                gint       top_attach,                                gint       bottom_attach );</verb></tscreen>The X and Y options default to <tt/GTK_FILL | GTK_EXPAND/, and X and Ypadding are set to 0. The rest of the arguments are identical to theprevious function.We also have gtk_table_set_row_spacing() andgtk_table_set_col_spacing(). These places spacing between the rows atthe specified row or column.<tscreen><verb>void gtk_table_set_row_spacing( GtkTable *table,                                gint      row,                                gint      spacing );</verb></tscreen>and<tscreen><verb>void gtk_table_set_col_spacing ( GtkTable *table,                                 gint      column,                                 gint      spacing );</verb></tscreen>Note that for columns, the space goes to the right of the column, andfor rows, the space goes below the row.You can also set a consistent spacing of all rows and/or columns with:<tscreen><verb>void gtk_table_set_row_spacings( GtkTable *table,                                 gint      spacing );</verb></tscreen>And,<tscreen><verb>void gtk_table_set_col_spacings( GtkTable *table,                                 gint      spacing );</verb></tscreen>Note that with these calls, the last row and last column do not getany spacing.<!-- ----------------------------------------------------------------- --><sect1>Table Packing Example<p>Here we make a window with three buttons in a 2x2 table.The first two buttons will be placed in the upper row.A third, quit button, is placed in the lower row, spanning both columns.Which means it should look something like this:<? <CENTER> ><?<IMG SRC="gtk_tut_table.gif" VSPACE="15" HSPACE="10" ALT="Table Packing Example Image" WIDTH="180" HEIGHT="120">><? </CENTER> >Here's the source code:<tscreen><verb>/* example-start table table.c */#include <gtk/gtk.h>/* Our callback. * The data passed to this function is printed to stdout */void callback( GtkWidget *widget,               gpointer   data ){    g_print ("Hello again - %s was pressed\n", (char *) data);}/* This callback quits the program */gint delete_event( GtkWidget *widget,                   GdkEvent  *event,                   gpointer   data ){    gtk_main_quit ();    return(FALSE);}int main( int   argc,          char *argv[] ){    GtkWidget *window;    GtkWidget *button;    GtkWidget *table;    gtk_init (&amp;argc, &amp;argv);    /* Create a new window */    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);    /* Set the window title */    gtk_window_set_title (GTK_WINDOW (window), "Table");    /* Set a handler for delete_event that immediately     * exits GTK. */    gtk_signal_connect (GTK_OBJECT (window), "delete_event",                        GTK_SIGNAL_FUNC (delete_event), NULL);    /* Sets the border width of the window. */    gtk_container_set_border_width (GTK_CONTAINER (window), 20);    /* Create a 2x2 table */    table = gtk_table_new (2, 2, TRUE);    /* Put the table in the main window */    gtk_container_add (GTK_CONTAINER (window), table);    /* Create first button */    button = gtk_button_new_with_label ("button 1");    /* When the button is clicked, we call the "callback" function     * with a pointer to "button 1" as its argument */    gtk_signal_connect (GTK_OBJECT (button), "clicked",              GTK_SIGNAL_FUNC (callback), (gpointer) "button 1");    /* Insert button 1 into the upper left quadrant of the table */    gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 1, 0, 1);    gtk_widget_show (button);    /* Create second button */    button = gtk_button_new_with_label ("button 2");    /* When the button is clicked, we call the "callback" function     * with a pointer to "button 2" as its argument */    gtk_signal_connect (GTK_OBJECT (button), "clicked",              GTK_SIGNAL_FUNC (callback), (gpointer) "button 2");    /* Insert button 2 into the upper right quadrant of the table */    gtk_table_attach_defaults (GTK_TABLE(table), button, 1, 2, 0, 1);    gtk_widget_show (button);    /* Create "Quit" button */    button = gtk_button_new_with_label ("Quit");    /* When the button is clicked, we call the "delete_event" function     * and the program exits */    gtk_signal_connect (GTK_OBJECT (button), "clicked",                        GTK_SIGNAL_FUNC (delete_event), NULL);    /* Insert the quit button into the both      * lower quadrants of the table */    gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 2, 1, 2);    gtk_widget_show (button);    gtk_widget_show (table);    gtk_widget_show (window);    gtk_main ();    return 0;}/* example-end */</verb></tscreen><!-- ***************************************************************** --><sect>Widget Overview<!-- ***************************************************************** --><p>The general steps to creating a widget in GTK are:<enum><item> gtk_*_new - one of various functions to create a new widget.These are all detailed in this section.<item> Connect all signals and events we wish to use to theappropriate handlers.<item> Set the attributes of the widget.<item> Pack the widget into a container using the appropriate callsuch as gtk_container_add() or gtk_box_pack_start().<item> gtk_widget_show() the widget.</enum>gtk_widget_show() lets GTK know that we are done setting theattributes of the widget, and it is ready to be displayed. You mayalso use gtk_widget_hide to make it disappear again. The order inwhich you show the widgets is not important, but I suggest showing thewindow last so the whole window pops up at once rather than seeing theindividual widgets come up on the screen as they're formed. Thechildren of a widget (a window is a widget too) will not be displayeduntil the window itself is shown using the gtk_widget_show() function.<!-- ----------------------------------------------------------------- --><sect1> Casting<p>You'll notice as you go on that GTK uses a type casting system. Thisis always done using macros that both test the ability to cast thegiven item, and perform the cast. Some common ones you will see are:<tscreen><verb>  GTK_WIDGET(widget)  GTK_OBJECT(object)  GTK_SIGNAL_FUNC(function)  GTK_CONTAINER(container)  GTK_WINDOW(window)  GTK_BOX(box)</verb></tscreen>These are all used to cast arguments in functions. You'll see them in theexamples, and can usually tell when to use them simply by looking at thefunction's declaration.As you can see below in the class hierarchy, all GtkWidgets arederived from the Object base class. This means you can use a widgetin any place the function asks for an object - simply use the<tt/GTK_OBJECT()/ macro.For example:<tscreen><verb>gtk_signal_connect( GTK_OBJECT(button), "clicked",                    GTK_SIGNAL_FUNC(callback_function), callback_data);</verb></tscreen> This casts the button into an object, and provides a cast for thefunction pointer to the callback.Many widgets are also containers. If you look in the class hierarchybelow, you'll notice that many widgets derive from the Containerclass. Any one of these widgets may be used with the<tt/GTK_CONTAINER/ macro to pass them to functions that ask forcontainers.Unfortunately, these macros are not extensively covered in thetutorial, but I recommend taking a look through the GTK headerfiles. It can be very educational. In fact, it's not difficult tolearn how a widget works just by looking at the function declarations.<!-- ----------------------------------------------------------------- --><sect1>Widget Hierarchy<p>For your reference, here is the class hierarchy tree used to implement widgets.<tscreen><verb> GtkObject  +GtkWidget  | +GtkMisc  | | +GtkLabel  | | | +GtkAccelLabel  | | | `GtkTipsQuery  | | +GtkArrow  | | +GtkImage  | | `GtkPixmap  | +GtkContainer  | | +GtkBin  | | | +GtkAlignment  | | | +GtkFrame  | | | | `GtkAspectFrame  | | | +GtkButton  | | | | +GtkToggleButton  | | | | | `GtkCheckButton  | | | | |   `GtkRadioButton  | | | | `GtkOptionMenu  | | | +GtkItem  | | | | +GtkMenuItem  | | | | | +GtkCheckMenuItem  | | | | | | `GtkRadioMenuItem  | | | | | `GtkTearoffMenuItem  | | | | +GtkListItem  | | | | `GtkTreeItem  | | | +GtkWindow  | | | | +GtkColorSelectionDialog  | | | | +GtkDialog  | | | | | `GtkInputDialog  | | | | +GtkDrawWindow  | | | | +GtkFileSelection  | | | | +GtkFontSelectionDialog  | | | | `GtkPlug  | | | +GtkEventBox  | | | +GtkHandleBox  | | | +GtkScrolledWindow  | | | `GtkViewport  | | +GtkBox  | | | +GtkButtonBox  | | | | +GtkHButtonBox  | | | | `GtkVButtonBox  | | | +GtkVBox  | | | | +GtkColorSelection  | | | | `GtkGammaCurve  | | | `GtkHBox  | | |   +GtkCombo  | | |   `GtkStatusbar  | | +GtkCList  | | | `GtkCTree  | | +GtkFixed  | | +GtkNotebook  | | | `GtkFontSelection  | | +GtkPaned  | | | +GtkHPaned  | | | `GtkVPaned  | | +GtkLayout  | | +GtkList  | | +GtkMenuShell  | | | +GtkMenuBar  | | | `GtkMenu  | | +GtkPacker  | | +GtkSocket  | | +GtkTable  | | +GtkToolbar  | | `GtkTree  | +GtkCalendar  | +GtkDrawingArea  | | `GtkCurve  | +GtkEditable  | | +GtkEntry  | | | `GtkSpinButton  | | `GtkText  | +GtkRuler  | | +GtkHRuler  | | `GtkVRuler  | +GtkRange  | | +GtkScale  | | | +GtkHScale  |

⌨️ 快捷键说明

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